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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

0.81 hits per line

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

81.25
/src/main/java/com/meterware/httpunit/WebApplet.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;
21

22
import com.meterware.httpunit.scripting.ScriptableDelegate;
23

24
import java.applet.Applet;
25
import java.io.IOException;
26
import java.lang.reflect.InvocationTargetException;
27
import java.net.MalformedURLException;
28
import java.net.URL;
29
import java.net.URLClassLoader;
30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.StringTokenizer;
35

36
import org.w3c.dom.Element;
37
import org.w3c.dom.Node;
38
import org.w3c.dom.NodeList;
39
import org.w3c.dom.html.HTMLAppletElement;
40
import org.xml.sax.SAXException;
41

42
/**
43
 * This class represents the embedding of an applet in a web page.
44
 **/
45
public class WebApplet extends HTMLElementBase {
46

47
    /** The response. */
48
    private WebResponse _response;
49

50
    /** The base target. */
51
    private String _baseTarget;
52

53
    /** The applet. */
54
    private Applet _applet;
55

56
    /** The parameters. */
57
    private HashMap _parameters;
58

59
    /** The parameter names. */
60
    private String[] _parameterNames;
61

62
    /** The class extension. */
63
    private final String CLASS_EXTENSION = ".class";
1✔
64

65
    /** The element. */
66
    private HTMLAppletElement _element;
67

68
    /**
69
     * Instantiates a new web applet.
70
     *
71
     * @param response
72
     *            the response
73
     * @param element
74
     *            the element
75
     * @param baseTarget
76
     *            the base target
77
     */
78
    public WebApplet(WebResponse response, HTMLAppletElement element, String baseTarget) {
79
        super(element);
1✔
80
        _element = element;
1✔
81
        _response = response;
1✔
82
        _baseTarget = baseTarget;
1✔
83
    }
1✔
84

85
    /**
86
     * Returns the URL of the codebase used to find the applet classes.
87
     *
88
     * @return the code base URL
89
     *
90
     * @throws MalformedURLException
91
     *             the malformed URL exception
92
     */
93
    public URL getCodeBaseURL() throws MalformedURLException {
94
        return new URL(_response.getURL(), getCodeBase());
1✔
95
    }
96

97
    /**
98
     * Gets the code base.
99
     *
100
     * @return the code base
101
     */
102
    private String getCodeBase() {
103
        final String codeBaseAttribute = _element.getCodeBase();
1✔
104
        return codeBaseAttribute.endsWith("/") ? codeBaseAttribute : codeBaseAttribute + "/";
1!
105
    }
106

107
    /**
108
     * Returns the name of the applet main class.
109
     *
110
     * @return the main class name
111
     */
112
    public String getMainClassName() {
113
        String className = _element.getCode();
1✔
114
        if (className.endsWith(CLASS_EXTENSION)) {
1✔
115
            className = className.substring(0, className.lastIndexOf(CLASS_EXTENSION));
1✔
116
        }
117
        return className.replace('/', '.').replace('\\', '.');
1✔
118
    }
119

120
    /**
121
     * Returns the width of the panel in which the applet will be drawn.
122
     *
123
     * @return the width
124
     */
125
    public int getWidth() {
126
        return Integer.parseInt(getAttribute("width"));
1✔
127
    }
128

129
    /**
130
     * Returns the height of the panel in which the applet will be drawn.
131
     *
132
     * @return the height
133
     */
134
    public int getHeight() {
135
        return Integer.parseInt(getAttribute("height"));
1✔
136
    }
137

138
    /**
139
     * Returns the archive specification.
140
     *
141
     * @return the archive specification
142
     */
143
    public String getArchiveSpecification() {
144
        String specification = getParameter("archive");
1✔
145
        if (specification == null) {
1!
146
            specification = getAttribute("archive");
1✔
147
        }
148
        return specification;
1✔
149
    }
150

151
    /**
152
     * Gets the archive list.
153
     *
154
     * @return the archive list
155
     *
156
     * @throws MalformedURLException
157
     *             the malformed URL exception
158
     */
159
    List getArchiveList() throws MalformedURLException {
160
        ArrayList al = new ArrayList<>();
1✔
161
        StringTokenizer st = new StringTokenizer(getArchiveSpecification(), ",");
1✔
162
        while (st.hasMoreTokens()) {
1!
163
            al.add(new URL(getCodeBaseURL(), st.nextToken()));
×
164
        }
165
        return al;
1✔
166
    }
167

168
    /**
169
     * Returns an array containing the names of the parameters defined for the applet.
170
     *
171
     * @return the parameter names
172
     */
173
    public String[] getParameterNames() {
174
        if (_parameterNames == null) {
1✔
175
            ArrayList al = new ArrayList(getParameterMap().keySet());
1✔
176
            _parameterNames = (String[]) al.toArray(new String[al.size()]);
1✔
177
        }
178
        return _parameterNames;
1✔
179
    }
180

181
    /**
182
     * Returns the value of the specified applet parameter, or null if not defined.
183
     *
184
     * @param name
185
     *            the name
186
     *
187
     * @return the parameter
188
     */
189
    public String getParameter(String name) {
190
        return (String) getParameterMap().get(name);
1✔
191
    }
192

193
    /**
194
     * Gets the parameter map.
195
     *
196
     * @return the parameter map
197
     */
198
    private Map getParameterMap() {
199
        if (_parameters == null) {
1✔
200
            _parameters = new HashMap<>();
1✔
201
            NodeList nl = ((Element) getNode()).getElementsByTagName("param");
1✔
202
            for (int i = 0; i < nl.getLength(); i++) {
1✔
203
                Node n = nl.item(i);
1✔
204
                _parameters.put(NodeUtils.getNodeAttribute(n, "name", ""), NodeUtils.getNodeAttribute(n, "value", ""));
1✔
205
            }
206
        }
207
        return _parameters;
1✔
208
    }
209

210
    /**
211
     * Gets the applet.
212
     *
213
     * @return the applet
214
     *
215
     * @throws MalformedURLException
216
     *             the malformed URL exception
217
     * @throws ClassNotFoundException
218
     *             the class not found exception
219
     * @throws InstantiationException
220
     *             the instantiation exception
221
     * @throws IllegalAccessException
222
     *             the illegal access exception
223
     * @throws IllegalArgumentException
224
     *             the illegal argument exception
225
     * @throws InvocationTargetException
226
     *             the invocation target exception
227
     * @throws NoSuchMethodException
228
     *             the no such method exception
229
     * @throws SecurityException
230
     *             the security exception
231
     */
232
    public Applet getApplet()
233
            throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException,
234
            IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
235
        if (_applet == null) {
1✔
236
            ClassLoader cl = new URLClassLoader(getClassPath(), null);
1✔
237
            Object o = cl.loadClass(getMainClassName()).getDeclaredConstructor().newInstance();
1✔
238
            if (!(o instanceof Applet)) {
1!
239
                throw new RuntimeException(getMainClassName() + " is not an Applet");
×
240
            }
241
            _applet = (Applet) o;
1✔
242
            _applet.setStub(new AppletStubImpl(this));
1✔
243
        }
244
        return _applet;
1✔
245
    }
246

247
    /**
248
     * Gets the class path.
249
     *
250
     * @return the class path
251
     *
252
     * @throws MalformedURLException
253
     *             the malformed URL exception
254
     */
255
    private URL[] getClassPath() throws MalformedURLException {
256
        List classPath = getArchiveList();
1✔
257
        classPath.add(getCodeBaseURL());
1✔
258
        return (URL[]) classPath.toArray(new URL[classPath.size()]);
1✔
259
    }
260

261
    /**
262
     * Gets the base target.
263
     *
264
     * @return the base target
265
     */
266
    String getBaseTarget() {
267
        return _baseTarget;
1✔
268
    }
269

270
    /**
271
     * Gets the applets in page.
272
     *
273
     * @return the applets in page
274
     */
275
    WebApplet[] getAppletsInPage() {
276
        try {
277
            return _response.getApplets();
1✔
278
        } catch (SAXException e) {
×
279
            HttpUnitUtils.handleException(e); // should never happen.
×
280
            return null;
×
281
        }
282
    }
283

284
    /**
285
     * Send request.
286
     *
287
     * @param url
288
     *            the url
289
     * @param target
290
     *            the target
291
     */
292
    void sendRequest(URL url, String target) {
293
        WebRequest wr = new GetMethodWebRequest(null, url.toExternalForm(), target);
1✔
294
        try {
295
            _response.getWindow().getResponse(wr);
1✔
296
        } catch (IOException e) {
×
297
            e.printStackTrace(); // To change body of catch statement use Options | File Templates.
×
298
            throw new RuntimeException(e.toString());
×
299
        } catch (SAXException e) {
×
300
        }
1✔
301
    }
1✔
302

303
    @Override
304
    public ScriptableDelegate newScriptable() {
305
        return new HTMLElementScriptable(this);
×
306
    }
307

308
    @Override
309
    public ScriptableDelegate getParentDelegate() {
310
        return _response.getDocumentScriptable();
×
311
    }
312

313
}
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