• 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

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

10
import com.meterware.httpunit.scripting.ScriptableDelegate;
11

12
import java.applet.Applet;
13
import java.io.IOException;
14
import java.lang.reflect.InvocationTargetException;
15
import java.net.MalformedURLException;
16
import java.net.URL;
17
import java.net.URLClassLoader;
18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.StringTokenizer;
23

24
import org.w3c.dom.Element;
25
import org.w3c.dom.Node;
26
import org.w3c.dom.NodeList;
27
import org.w3c.dom.html.HTMLAppletElement;
28
import org.xml.sax.SAXException;
29

30
/**
31
 * This class represents the embedding of an applet in a web page.
32
 **/
33
public class WebApplet extends HTMLElementBase {
34

35
    /** The response. */
36
    private WebResponse _response;
37

38
    /** The base target. */
39
    private String _baseTarget;
40

41
    /** The applet. */
42
    private Applet _applet;
43

44
    /** The parameters. */
45
    private HashMap _parameters;
46

47
    /** The parameter names. */
48
    private String[] _parameterNames;
49

50
    /** The class extension. */
51
    private final String CLASS_EXTENSION = ".class";
1✔
52

53
    /** The element. */
54
    private HTMLAppletElement _element;
55

56
    /**
57
     * Instantiates a new web applet.
58
     *
59
     * @param response
60
     *            the response
61
     * @param element
62
     *            the element
63
     * @param baseTarget
64
     *            the base target
65
     */
66
    public WebApplet(WebResponse response, HTMLAppletElement element, String baseTarget) {
67
        super(element);
1✔
68
        _element = element;
1✔
69
        _response = response;
1✔
70
        _baseTarget = baseTarget;
1✔
71
    }
1✔
72

73
    /**
74
     * Returns the URL of the codebase used to find the applet classes.
75
     *
76
     * @return the code base URL
77
     *
78
     * @throws MalformedURLException
79
     *             the malformed URL exception
80
     */
81
    public URL getCodeBaseURL() throws MalformedURLException {
82
        return new URL(_response.getURL(), getCodeBase());
1✔
83
    }
84

85
    /**
86
     * Gets the code base.
87
     *
88
     * @return the code base
89
     */
90
    private String getCodeBase() {
91
        final String codeBaseAttribute = _element.getCodeBase();
1✔
92
        return codeBaseAttribute.endsWith("/") ? codeBaseAttribute : codeBaseAttribute + "/";
1!
93
    }
94

95
    /**
96
     * Returns the name of the applet main class.
97
     *
98
     * @return the main class name
99
     */
100
    public String getMainClassName() {
101
        String className = _element.getCode();
1✔
102
        if (className.endsWith(CLASS_EXTENSION)) {
1✔
103
            className = className.substring(0, className.lastIndexOf(CLASS_EXTENSION));
1✔
104
        }
105
        return className.replace('/', '.').replace('\\', '.');
1✔
106
    }
107

108
    /**
109
     * Returns the width of the panel in which the applet will be drawn.
110
     *
111
     * @return the width
112
     */
113
    public int getWidth() {
114
        return Integer.parseInt(getAttribute("width"));
1✔
115
    }
116

117
    /**
118
     * Returns the height of the panel in which the applet will be drawn.
119
     *
120
     * @return the height
121
     */
122
    public int getHeight() {
123
        return Integer.parseInt(getAttribute("height"));
1✔
124
    }
125

126
    /**
127
     * Returns the archive specification.
128
     *
129
     * @return the archive specification
130
     */
131
    public String getArchiveSpecification() {
132
        String specification = getParameter("archive");
1✔
133
        if (specification == null) {
1!
134
            specification = getAttribute("archive");
1✔
135
        }
136
        return specification;
1✔
137
    }
138

139
    /**
140
     * Gets the archive list.
141
     *
142
     * @return the archive list
143
     *
144
     * @throws MalformedURLException
145
     *             the malformed URL exception
146
     */
147
    List getArchiveList() throws MalformedURLException {
148
        ArrayList al = new ArrayList<>();
1✔
149
        StringTokenizer st = new StringTokenizer(getArchiveSpecification(), ",");
1✔
150
        while (st.hasMoreTokens()) {
1!
151
            al.add(new URL(getCodeBaseURL(), st.nextToken()));
×
152
        }
153
        return al;
1✔
154
    }
155

156
    /**
157
     * Returns an array containing the names of the parameters defined for the applet.
158
     *
159
     * @return the parameter names
160
     */
161
    public String[] getParameterNames() {
162
        if (_parameterNames == null) {
1✔
163
            ArrayList al = new ArrayList(getParameterMap().keySet());
1✔
164
            _parameterNames = (String[]) al.toArray(new String[al.size()]);
1✔
165
        }
166
        return _parameterNames;
1✔
167
    }
168

169
    /**
170
     * Returns the value of the specified applet parameter, or null if not defined.
171
     *
172
     * @param name
173
     *            the name
174
     *
175
     * @return the parameter
176
     */
177
    public String getParameter(String name) {
178
        return (String) getParameterMap().get(name);
1✔
179
    }
180

181
    /**
182
     * Gets the parameter map.
183
     *
184
     * @return the parameter map
185
     */
186
    private Map getParameterMap() {
187
        if (_parameters == null) {
1✔
188
            _parameters = new HashMap<>();
1✔
189
            NodeList nl = ((Element) getNode()).getElementsByTagName("param");
1✔
190
            for (int i = 0; i < nl.getLength(); i++) {
1✔
191
                Node n = nl.item(i);
1✔
192
                _parameters.put(NodeUtils.getNodeAttribute(n, "name", ""), NodeUtils.getNodeAttribute(n, "value", ""));
1✔
193
            }
194
        }
195
        return _parameters;
1✔
196
    }
197

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

235
    /**
236
     * Gets the class path.
237
     *
238
     * @return the class path
239
     *
240
     * @throws MalformedURLException
241
     *             the malformed URL exception
242
     */
243
    private URL[] getClassPath() throws MalformedURLException {
244
        List classPath = getArchiveList();
1✔
245
        classPath.add(getCodeBaseURL());
1✔
246
        return (URL[]) classPath.toArray(new URL[classPath.size()]);
1✔
247
    }
248

249
    /**
250
     * Gets the base target.
251
     *
252
     * @return the base target
253
     */
254
    String getBaseTarget() {
255
        return _baseTarget;
1✔
256
    }
257

258
    /**
259
     * Gets the applets in page.
260
     *
261
     * @return the applets in page
262
     */
263
    WebApplet[] getAppletsInPage() {
264
        try {
265
            return _response.getApplets();
1✔
266
        } catch (SAXException e) {
×
267
            HttpUnitUtils.handleException(e); // should never happen.
×
268
            return null;
×
269
        }
270
    }
271

272
    /**
273
     * Send request.
274
     *
275
     * @param url
276
     *            the url
277
     * @param target
278
     *            the target
279
     */
280
    void sendRequest(URL url, String target) {
281
        WebRequest wr = new GetMethodWebRequest(null, url.toExternalForm(), target);
1✔
282
        try {
283
            _response.getWindow().getResponse(wr);
1✔
284
        } catch (IOException e) {
×
285
            e.printStackTrace(); // To change body of catch statement use Options | File Templates.
×
286
            throw new RuntimeException(e.toString());
×
287
        } catch (SAXException e) {
×
288
        }
1✔
289
    }
1✔
290

291
    @Override
292
    public ScriptableDelegate newScriptable() {
293
        return new HTMLElementScriptable(this);
×
294
    }
295

296
    @Override
297
    public ScriptableDelegate getParentDelegate() {
298
        return _response.getDocumentScriptable();
×
299
    }
300

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