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

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

0.81 hits per line

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

87.62
/src/main/java/com/meterware/httpunit/javascript/ScriptingEngineImpl.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit.javascript;
21

22
import com.meterware.httpunit.HttpUnitUtils;
23
import com.meterware.httpunit.ScriptException;
24
import com.meterware.httpunit.scripting.ScriptingEngine;
25

26
import java.util.ArrayList;
27

28
import org.mozilla.javascript.Context;
29
import org.mozilla.javascript.EcmaError;
30
import org.mozilla.javascript.EvaluatorException;
31
import org.mozilla.javascript.Function;
32
import org.mozilla.javascript.JavaScriptException;
33
import org.mozilla.javascript.ScriptableObject;
34
import org.mozilla.javascript.Undefined;
35

36
public abstract class ScriptingEngineImpl extends ScriptableObject implements ScriptingEngine {
1✔
37

38
    private static final long serialVersionUID = 1L;
39

40
    private static final Object[] NO_ARGS = {};
1✔
41

42
    private static ArrayList _errorMessages = new ArrayList<>();
1✔
43

44
    /**
45
     * clear the list of error Messages
46
     */
47
    static public void clearErrorMessages() {
48
        _errorMessages.clear();
1✔
49
    }
1✔
50

51
    /**
52
     * access to the list of error Messages that were collected
53
     *
54
     * @return the array with error Messages
55
     */
56
    static public String[] getErrorMessages() {
57
        return (String[]) _errorMessages.toArray(new String[_errorMessages.size()]);
1✔
58
    }
59

60
    /**
61
     * handle Exceptions
62
     *
63
     * @param e
64
     *            - the exception to handle
65
     * @param badScript
66
     *            - the script that caused the problem
67
     */
68
    static public void handleScriptException(Exception e, String badScript) {
69
        String errorMessage = badScript == null ? e.getMessage() : badScript + " failed: " + e;
1✔
70
        if (!(e instanceof EcmaError) && !(e instanceof EvaluatorException) && !(e instanceof ScriptException)
1!
71
                && !(e instanceof JavaScriptException)) {
72
            HttpUnitUtils.handleException(e);
1✔
73
            throw new RuntimeException(errorMessage);
1✔
74
        }
75
        if (JavaScript.isThrowExceptionsOnError()) {
1✔
76
            HttpUnitUtils.handleException(e);
1✔
77
            if (e instanceof ScriptException) {
1✔
78
                throw (ScriptException) e;
1✔
79
            }
80
            throw new ScriptException(errorMessage);
1✔
81
        }
82
        _errorMessages.add(errorMessage);
1✔
83
    }
1✔
84

85
    // --------------------------------------- ScriptingEngine methods
86
    // ------------------------------------------------------
87

88
    @Override
89
    public boolean supportsScriptLanguage(String language) {
90
        return language == null || language.toLowerCase().startsWith("javascript");
1✔
91
    }
92

93
    /**
94
     * run the given script
95
     *
96
     * @param language
97
     *            - the language of the script
98
     * @param script
99
     *            - the script to run
100
     */
101
    @Override
102
    public String runScript(String language, String script) {
103
        if (!supportsScriptLanguage(language)) {
1✔
104
            return "";
1✔
105
        }
106
        try {
107
            script = script.trim();
1✔
108
            if (script.startsWith("<!--")) {
1✔
109
                script = withoutFirstLine(script);
1✔
110
                if (script.endsWith("-->")) {
1✔
111
                    script = script.substring(0, script.lastIndexOf("-->"));
1✔
112
                }
113
            }
114
            Context context = Context.enter();
1✔
115
            context.initStandardObjects(null);
1✔
116
            context.evaluateString(this, script, "httpunit", 0, null);
1✔
117
            return getDocumentWriteBuffer();
1✔
118
        } catch (Exception e) {
×
119
            handleScriptException(e, "Script '" + script + "'");
×
120
            return "";
×
121
        } finally {
122
            discardDocumentWriteBuffer();
1✔
123
            Context.exit();
1✔
124
        }
125
    }
126

127
    /**
128
     * handle the event that has the given script attached by compiling the eventScript as a function and executing it
129
     *
130
     * @param eventScript
131
     *            - the script to use
132
     *
133
     * @deprecated since 1.7 - use doEventScript instead
134
     */
135
    @Deprecated
136
    @Override
137
    public boolean doEvent(String eventScript) {
138
        return doEventScript(eventScript);
×
139
    }
140

141
    /**
142
     * handle the event that has the given script attached by compiling the eventScript as a function and executing it
143
     *
144
     * @param eventScript
145
     *            - the script to use
146
     */
147
    @Override
148
    public boolean doEventScript(String eventScript) {
149
        if (eventScript.isEmpty()) {
1!
150
            return true;
×
151
        }
152
        try {
153
            Context context = Context.enter();
1✔
154
            context.initStandardObjects(null);
1✔
155
            context.setOptimizationLevel(-1);
1✔
156
            // wrap the eventScript into a function
157
            Function f = context.compileFunction(this, "function x() { " + eventScript + "}", "httpunit", 0, null);
1✔
158
            // call the function with no arguments
159
            Object result = f.call(context, this, this, NO_ARGS);
1✔
160
            // return the result of the function or false if it is not boolean
161
            return !(result instanceof Boolean) || ((Boolean) result).booleanValue();
1✔
162
        } catch (Exception e) {
1✔
163
            handleScriptException(e, "Event '" + eventScript + "'");
1✔
164
            return false;
1✔
165
        } finally {
166
            Context.exit();
1✔
167
        }
168
    }
169

170
    /**
171
     * get the event Handler script for the event e.g. onchange, onmousedown, onclick, onmouseup execute the script if
172
     * it's assigned by calling doEvent for the script
173
     *
174
     * @param eventName
175
     *
176
     * @return
177
     */
178
    @Override
179
    public boolean handleEvent(String eventName) {
180
        throw new RuntimeException("pseudo - abstract handleEvent called ");
×
181
    }
182

183
    /**
184
     * Evaluates the specified string as JavaScript. Will return null if the script has no return value.
185
     *
186
     * @param expression
187
     *            - the expression to evaluate
188
     */
189
    @Override
190
    public Object evaluateExpression(String expression) {
191
        try {
192
            Context context = Context.enter();
1✔
193
            context.initStandardObjects(null);
1✔
194
            Object result = context.evaluateString(this, expression, "httpunit", 0, null);
1✔
195
            return result == null || result instanceof Undefined ? null : result;
1!
196
        } catch (Exception e) {
1✔
197
            handleScriptException(e, "URL '" + expression + "'");
1✔
198
            return null;
1✔
199
        } finally {
200
            Context.exit();
1✔
201
        }
202
    }
203

204
    // ------------------------------------------ protected methods
205
    // ---------------------------------------------------------
206

207
    protected String getDocumentWriteBuffer() {
208
        throw new IllegalStateException("may not run runScript() from " + getClass());
×
209
    }
210

211
    protected void discardDocumentWriteBuffer() {
212
        throw new IllegalStateException("may not run runScript() from " + getClass());
×
213
    }
214

215
    private String withoutFirstLine(String script) {
216
        for (int i = 0; i < script.length(); i++) {
1✔
217
            if (isLineTerminator(script.charAt(i))) {
1✔
218
                return script.substring(i).trim();
1✔
219
            }
220
        }
221
        return "";
1✔
222
    }
223

224
    private boolean isLineTerminator(char c) {
225
        return c == 0x0A || c == 0x0D;
1!
226
    }
227
}
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