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

hazendaz / httpunit / 646

06 Dec 2025 08:11PM UTC coverage: 80.526% (+0.02%) from 80.509%
646

push

github

hazendaz
Cleanup array usage

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

4 of 4 new or added lines in 3 files covered. (100.0%)

532 existing lines in 26 files now uncovered.

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

0.0
/src/main/java/com/meterware/httpunit/dom/DomBasedScriptingEngineFactory.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.dom;
21

22
import com.meterware.httpunit.HTMLElement;
23
import com.meterware.httpunit.HttpUnitUtils;
24
import com.meterware.httpunit.WebResponse;
25
import com.meterware.httpunit.javascript.JavaScript;
26
import com.meterware.httpunit.javascript.ScriptingEngineImpl;
27
import com.meterware.httpunit.scripting.ScriptingEngineFactory;
28
import com.meterware.httpunit.scripting.ScriptingHandler;
29

30
import org.mozilla.javascript.Context;
31
import org.mozilla.javascript.EcmaError;
32
import org.mozilla.javascript.Function;
33
import org.mozilla.javascript.JavaScriptException;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
import org.w3c.dom.html.HTMLDocument;
37

38
/**
39
 * The scripting engine factory which relies directly on the DOM.
40
 */
41
public class DomBasedScriptingEngineFactory implements ScriptingEngineFactory {
×
42

43
    /** The Constant logger. */
44
    private static final Logger logger = LoggerFactory.getLogger(DomBasedScriptingEngineFactory.class);
×
45

46
    /**
47
     * check whether this ScriptingEngineFactory is enabled
48
     */
49
    @Override
50
    public boolean isEnabled() {
51
        try {
52
            Class.forName("org.mozilla.javascript.Context");
×
53
            return true;
×
54
        } catch (Exception e) {
×
55
            logger.warn("Rhino classes (js.jar) not found - Javascript disabled");
×
56
            return false;
×
57
        }
58
    }
59

60
    /**
61
     * associate me with a webresponse
62
     *
63
     * @param response
64
     *            - the WebResponse to use
65
     */
66
    @Override
67
    public void associate(WebResponse response) {
68
        try {
69
            // JavaScript.run( response ); // can't do this (yet?)
70
        } catch (RuntimeException e) {
71
            throw e;
72
        } catch (Exception e) {
73
            HttpUnitUtils.handleException(e);
74
            throw new RuntimeException(e.toString());
75
        }
76
    }
×
77

78
    /**
79
     * load.
80
     *
81
     * @param response
82
     *            the response
83
     */
84
    @Override
85
    public void load(WebResponse response) {
UNCOV
86
        Function onLoadEvent = null;
×
87
        try {
88
            Context context = Context.enter();
×
UNCOV
89
            context.initStandardObjects(null);
×
90

91
            HTMLDocument htmlDocument = ((DomWindow) response.getScriptingHandler()).getDocument();
×
92
            if (!(htmlDocument instanceof HTMLDocumentImpl)) {
×
UNCOV
93
                return;
×
94
            }
95

96
            HTMLBodyElementImpl body = (HTMLBodyElementImpl) htmlDocument.getBody();
×
97
            if (body == null) {
×
UNCOV
98
                return;
×
99
            }
100
            onLoadEvent = body.getOnloadEvent();
×
101
            if (onLoadEvent == null) {
×
UNCOV
102
                return;
×
103
            }
104
            onLoadEvent.call(context, body, body, new Object[0]);
×
UNCOV
105
        } catch (JavaScriptException | EcmaError ee) {
×
106
            // throw ee;
UNCOV
107
            ScriptingEngineImpl.handleScriptException(ee, onLoadEvent.toString());
×
108
        } finally {
UNCOV
109
            Context.exit();
×
110
        }
UNCOV
111
    }
×
112

113
    /**
114
     * setter for the throwExceptions flag
115
     *
116
     * @param throwExceptions
117
     *            - true if Exceptions should be thrown
118
     */
119
    @Override
120
    public void setThrowExceptionsOnError(boolean throwExceptions) {
121
        JavaScript.setThrowExceptionsOnError(throwExceptions);
×
UNCOV
122
    }
×
123

124
    /**
125
     * getter for the throwExceptions flag
126
     *
127
     * @return - true if Exceptions should be thrown
128
     */
129
    @Override
130
    public boolean isThrowExceptionsOnError() {
UNCOV
131
        return JavaScript.isThrowExceptionsOnError();
×
132
    }
133

134
    @Override
135
    public String[] getErrorMessages() {
UNCOV
136
        return ScriptingEngineImpl.getErrorMessages();
×
137
    }
138

139
    @Override
140
    public void clearErrorMessages() {
141
        ScriptingEngineImpl.clearErrorMessages();
×
UNCOV
142
    }
×
143

144
    @Override
145
    public ScriptingHandler createHandler(HTMLElement elementBase) {
UNCOV
146
        return (ScriptingHandler) elementBase.getNode();
×
147
    }
148

149
    @Override
150
    public ScriptingHandler createHandler(WebResponse response) {
UNCOV
151
        return response.createDomScriptingHandler();
×
152
    }
153

154
    /**
155
     * handle Exceptions
156
     *
157
     * @param e
158
     *            - the exception to handle
159
     * @param badScript
160
     *            - the script that caused the problem
161
     */
162
    @Override
163
    public void handleScriptException(Exception e, String badScript) {
164
        ScriptingEngineImpl.handleScriptException(e, badScript);
×
UNCOV
165
    }
×
166
}
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