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

hazendaz / scriptable-dataset / #124

04 Mar 2024 01:31AM UTC coverage: 89.683% (-0.2%) from 89.922%
#124

push

github

hazendaz
[ci] Modernize the code

23 of 26 branches covered (88.46%)

Branch coverage included in aggregate %.

14 of 14 new or added lines in 2 files covered. (100.0%)

7 existing lines in 3 files now uncovered.

90 of 100 relevant lines covered (90.0%)

0.9 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-2024 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
public class ScriptableTable implements ITable {
40

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

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

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

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

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

63
        this.wrapped = wrapped;
1✔
64

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

121
                    try {
122

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

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

130
                        // the actual script evaluation
131
                        theValue = engine.eval(script);
1✔
132

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

UNCOV
139
                    } catch (Exception e) {
×
UNCOV
140
                        throw new RuntimeException(e);
×
141
                    }
1✔
142
                }
143
            }
1✔
144
        }
145

146
        return theValue;
1✔
147
    }
148

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

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

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

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