• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / scriptable-dataset / #19

27 Aug 2023 02:10AM UTC coverage: 90.291%. Remained the same
#19

push

github

hazendaz
[pom] Override dbunit usage of ojdbc8 with latest 19.20.0.0

old one has a lot of extra junk

93 of 103 relevant lines covered (90.29%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.0
/src/main/java/de/gmorling/scriptabledataset/ScriptableTable.java
1
/*
2
 * scriptable-dataset (https://github.com/hazendaz/scriptable-dataset)
3
 *
4
 * Copyright 2011-2023 Hazendaz.
5
 *
6
 * All rights reserved. This program and the accompanying materials
7
 * are made available under the terms of The Apache Software License,
8
 * Version 2.0 which accompanies this distribution, and is available at
9
 * https://www.apache.org/licenses/LICENSE-2.0.txt
10
 *
11
 * Contributors:
12
 *     Gunnar Morling
13
 *     Hazendaz (Jeremy Landis).
14
 */
15
package de.gmorling.scriptabledataset;
16

17
import de.gmorling.scriptabledataset.handlers.ScriptInvocationHandler;
18
import de.gmorling.scriptabledataset.handlers.StandardHandlerConfig;
19

20
import java.util.ArrayList;
21
import java.util.Collections;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Map.Entry;
26

27
import javax.script.ScriptEngine;
28
import javax.script.ScriptEngineManager;
29

30
import org.dbunit.dataset.DataSetException;
31
import org.dbunit.dataset.ITable;
32
import org.dbunit.dataset.ITableMetaData;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

36
/**
37
 * ITable implementation, that allows the usage of script statements as field values.
38
 *
39
 * @author Gunnar Morling
40
 */
41
public class ScriptableTable implements ITable {
42

43
    /** The logger. */
44
    private final Logger logger = LoggerFactory.getLogger(ScriptableTable.class);
1✔
45

46
    /** The wrapped. */
47
    private ITable wrapped;
48

49
    /** The engines by prefix. */
50
    private Map<String, ScriptEngine> enginesByPrefix = new HashMap<>();
1✔
51

52
    /** The handlers by prefix. */
53
    private Map<String, List<ScriptInvocationHandler>> handlersByPrefix = new HashMap<>();
1✔
54

55
    /**
56
     * Creates a new ScriptableTable.
57
     *
58
     * @param wrapped
59
     *            The ITable to be wrapped by this scriptable table. May not be null.
60
     * @param configurations
61
     *            An list with configurations
62
     */
63
    public ScriptableTable(ITable wrapped, List<ScriptableDataSetConfig> configurations) {
1✔
64

65
        this.wrapped = wrapped;
1✔
66

67
        ScriptEngineManager manager = new ScriptEngineManager();
1✔
68

69
        // load the engines
70
        for (ScriptableDataSetConfig oneConfig : configurations) {
1✔
71

72
            ScriptEngine engine = manager.getEngineByName(oneConfig.getLanguageName());
1✔
73

74
            if (engine != null) {
1✔
75
                enginesByPrefix.put(oneConfig.getPrefix(), engine);
1✔
76

77
                List<ScriptInvocationHandler> handlers = getAllHandlers(oneConfig);
1✔
78

79
                for (ScriptInvocationHandler oneHandler : handlers) {
1✔
80
                    oneHandler.setScriptEngine(engine);
1✔
81
                }
1✔
82

83
                handlersByPrefix.put(oneConfig.getPrefix(), handlers);
1✔
84

85
                logger.info("Registered scripting engine {} for language {}.", engine, oneConfig.getLanguageName());
1✔
86
            } else {
1✔
87
                throw new RuntimeException(
1✔
88
                        "No scripting engine found for language \"" + oneConfig.getLanguageName() + "\".");
1✔
89
            }
90
        }
1✔
91
    }
1✔
92

93
    @Override
94
    public int getRowCount() {
95
        return wrapped.getRowCount();
×
96
    }
97

98
    @Override
99
    public ITableMetaData getTableMetaData() {
100
        return wrapped.getTableMetaData();
1✔
101
    }
102

103
    @Override
104
    public Object getValue(int row, String column) throws DataSetException {
105

106
        Object theValue = wrapped.getValue(row, column);
1✔
107

108
        // only strings can be processed
109
        if (theValue instanceof String) {
1✔
110
            String script = (String) theValue;
1✔
111

112
            for (Entry<String, ScriptEngine> oneEntry : enginesByPrefix.entrySet()) {
1✔
113

114
                String prefix = oneEntry.getKey();
1✔
115

116
                // found engine for prefix
117
                if (script.startsWith(prefix)) {
1✔
118

119
                    ScriptEngine engine = oneEntry.getValue();
1✔
120
                    script = script.substring(prefix.length());
1✔
121

122
                    List<ScriptInvocationHandler> handlers = handlersByPrefix.get(oneEntry.getKey());
1✔
123

124
                    try {
125

126
                        // preInvoke
127
                        for (ScriptInvocationHandler handler : handlers) {
1✔
128
                            script = handler.preInvoke(script);
1✔
129
                        }
1✔
130

131
                        logger.debug("Executing script: {}", script);
1✔
132

133
                        // the actual script evaluation
134
                        theValue = engine.eval(script);
1✔
135

136
                        // call postInvoke in reversed order
137
                        Collections.reverse(handlers);
1✔
138
                        for (ScriptInvocationHandler handler : handlers) {
1✔
139
                            theValue = handler.postInvoke(theValue);
1✔
140
                        }
1✔
141

142
                    } catch (Exception e) {
×
143
                        throw new RuntimeException(e);
×
144
                    }
1✔
145
                }
146
            }
1✔
147
        }
148

149
        return theValue;
1✔
150
    }
151

152
    /**
153
     * Returns a list with all standard handlers registered for the language of the config and all handlers declared in
154
     * the config itself.
155
     *
156
     * @param config
157
     *            A config object.
158
     *
159
     * @return A list with handlers. Never null.
160
     */
161
    private List<ScriptInvocationHandler> getAllHandlers(ScriptableDataSetConfig config) {
162

163
        List<ScriptInvocationHandler> theValue = new ArrayList<>();
1✔
164

165
        // standard handlers for the language
166
        theValue.addAll(StandardHandlerConfig.getStandardHandlersByLanguage(config.getLanguageName()));
1✔
167

168
        // custom handlers
169
        theValue.addAll(config.getHandlers());
1✔
170

171
        return theValue;
1✔
172
    }
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc