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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

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
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit.javascript;
9

10
import com.meterware.httpunit.HttpUnitUtils;
11
import com.meterware.httpunit.ScriptException;
12
import com.meterware.httpunit.scripting.ScriptingEngine;
13

14
import java.util.ArrayList;
15
import java.util.Locale;
16

17
import org.mozilla.javascript.Context;
18
import org.mozilla.javascript.EcmaError;
19
import org.mozilla.javascript.EvaluatorException;
20
import org.mozilla.javascript.Function;
21
import org.mozilla.javascript.JavaScriptException;
22
import org.mozilla.javascript.ScriptableObject;
23
import org.mozilla.javascript.Undefined;
24

25
/**
26
 * The Class ScriptingEngineImpl.
27
 */
28
public abstract class ScriptingEngineImpl extends ScriptableObject implements ScriptingEngine {
1✔
29

30
    /** The Constant serialVersionUID. */
31
    private static final long serialVersionUID = 1L;
32

33
    /** The Constant NO_ARGS. */
34
    private static final Object[] NO_ARGS = {};
1✔
35

36
    /** The error messages. */
37
    private static ArrayList _errorMessages = new ArrayList<>();
1✔
38

39
    /**
40
     * clear the list of error Messages.
41
     */
42
    static public void clearErrorMessages() {
43
        _errorMessages.clear();
1✔
44
    }
1✔
45

46
    /**
47
     * access to the list of error Messages that were collected.
48
     *
49
     * @return the array with error Messages
50
     */
51
    static public String[] getErrorMessages() {
52
        return (String[]) _errorMessages.toArray(new String[_errorMessages.size()]);
1✔
53
    }
54

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

80
    // --------------------------------------- ScriptingEngine methods
81
    // ------------------------------------------------------
82

83
    @Override
84
    public boolean supportsScriptLanguage(String language) {
85
        return language == null || language.toLowerCase(Locale.ENGLISH).startsWith("javascript");
1✔
86
    }
87

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

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

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

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

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

200
    // ------------------------------------------ protected methods
201
    // ---------------------------------------------------------
202

203
    /**
204
     * Gets the document write buffer.
205
     *
206
     * @return the document write buffer
207
     */
208
    protected String getDocumentWriteBuffer() {
209
        throw new IllegalStateException("may not run runScript() from " + getClass());
×
210
    }
211

212
    /**
213
     * Discard document write buffer.
214
     */
215
    protected void discardDocumentWriteBuffer() {
216
        throw new IllegalStateException("may not run runScript() from " + getClass());
×
217
    }
218

219
    /**
220
     * Without first line.
221
     *
222
     * @param script
223
     *            the script
224
     *
225
     * @return the string
226
     */
227
    private String withoutFirstLine(String script) {
228
        for (int i = 0; i < script.length(); i++) {
1✔
229
            if (isLineTerminator(script.charAt(i))) {
1✔
230
                return script.substring(i).trim();
1✔
231
            }
232
        }
233
        return "";
1✔
234
    }
235

236
    /**
237
     * Checks if is line terminator.
238
     *
239
     * @param c
240
     *            the c
241
     *
242
     * @return true, if is line terminator
243
     */
244
    private boolean isLineTerminator(char c) {
245
        return c == 0x0A || c == 0x0D;
1!
246
    }
247
}
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