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

hazendaz / scriptable-dataset / 327

03 May 2025 03:32AM UTC coverage: 90.4%. Remained the same
327

push

github

hazendaz
Move try/catch to actual location of error and use correct exception

23 of 26 branches covered (88.46%)

Branch coverage included in aggregate %.

8 of 9 new or added lines in 1 file covered. (88.89%)

1 existing line in 1 file now uncovered.

90 of 99 relevant lines covered (90.91%)

0.91 hits per line

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

93.85
/src/main/java/de/gmorling/scriptabledataset/ScriptableTable.java
1
/*
2
 * scriptable-dataset (https://github.com/hazendaz/scriptable-dataset)
3
 *
4
 * Copyright 2011-2025 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
import javax.script.ScriptException;
30

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

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

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

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

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

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

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

64
        this.wrapped = wrapped;
1✔
65

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

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

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

73
            if (engine == null) {
1✔
74
                throw new RuntimeException(
1✔
75
                        "No scripting engine found for language \"" + oneConfig.getLanguageName() + "\".");
1✔
76
            }
77
            enginesByPrefix.put(oneConfig.getPrefix(), engine);
1✔
78

79
            List<ScriptInvocationHandler> handlers = getAllHandlers(oneConfig);
1✔
80

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

85
            handlersByPrefix.put(oneConfig.getPrefix(), handlers);
1✔
86

87
            logger.info("Registered scripting engine {} for language {}.", engine, oneConfig.getLanguageName());
1✔
88
        }
1✔
89
    }
1✔
90

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

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

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

104
        Object theValue = wrapped.getValue(row, column);
1✔
105

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

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

112
                String prefix = oneEntry.getKey();
1✔
113

114
                // found engine for prefix
115
                if (script.startsWith(prefix)) {
1✔
116

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

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

122
                    // preInvoke
123
                    for (ScriptInvocationHandler handler : handlers) {
1✔
124
                        script = handler.preInvoke(script);
1✔
125
                    }
1✔
126

127
                    logger.debug("Executing script: {}", script);
1✔
128

129
                    // the actual script evaluation
130
                    try {
131
                        theValue = engine.eval(script);
1✔
NEW
132
                    } catch (ScriptException e) {
×
UNCOV
133
                        throw new RuntimeException(e);
×
134
                    }
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
            }
1✔
143
        }
144

145
        return theValue;
1✔
146
    }
147

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

159
        List<ScriptInvocationHandler> theValue = new ArrayList<>(
1✔
160
                StandardHandlerConfig.getStandardHandlersByLanguage(config.getLanguageName()));
1✔
161

162
        // custom handlers
163
        theValue.addAll(config.getHandlers());
1✔
164

165
        return theValue;
1✔
166
    }
167
}
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