• 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

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
    private WebResponse _response;
48
    private String _baseTarget;
49

50
    private Applet _applet;
51
    private HashMap _parameters;
52
    private String[] _parameterNames;
53

54
    private final String CLASS_EXTENSION = ".class";
1✔
55
    private HTMLAppletElement _element;
56

57
    public WebApplet(WebResponse response, HTMLAppletElement element, String baseTarget) {
58
        super(element);
1✔
59
        _element = element;
1✔
60
        _response = response;
1✔
61
        _baseTarget = baseTarget;
1✔
62
    }
1✔
63

64
    /**
65
     * Returns the URL of the codebase used to find the applet classes
66
     */
67
    public URL getCodeBaseURL() throws MalformedURLException {
68
        return new URL(_response.getURL(), getCodeBase());
1✔
69
    }
70

71
    private String getCodeBase() {
72
        final String codeBaseAttribute = _element.getCodeBase();
1✔
73
        return codeBaseAttribute.endsWith("/") ? codeBaseAttribute : codeBaseAttribute + "/";
1!
74
    }
75

76
    /**
77
     * Returns the name of the applet main class.
78
     */
79
    public String getMainClassName() {
80
        String className = _element.getCode();
1✔
81
        if (className.endsWith(CLASS_EXTENSION)) {
1✔
82
            className = className.substring(0, className.lastIndexOf(CLASS_EXTENSION));
1✔
83
        }
84
        return className.replace('/', '.').replace('\\', '.');
1✔
85
    }
86

87
    /**
88
     * Returns the width of the panel in which the applet will be drawn.
89
     */
90
    public int getWidth() {
91
        return Integer.parseInt(getAttribute("width"));
1✔
92
    }
93

94
    /**
95
     * Returns the height of the panel in which the applet will be drawn.
96
     */
97
    public int getHeight() {
98
        return Integer.parseInt(getAttribute("height"));
1✔
99
    }
100

101
    /**
102
     * Returns the archive specification.
103
     */
104
    public String getArchiveSpecification() {
105
        String specification = getParameter("archive");
1✔
106
        if (specification == null) {
1!
107
            specification = getAttribute("archive");
1✔
108
        }
109
        return specification;
1✔
110
    }
111

112
    List getArchiveList() throws MalformedURLException {
113
        ArrayList al = new ArrayList<>();
1✔
114
        StringTokenizer st = new StringTokenizer(getArchiveSpecification(), ",");
1✔
115
        while (st.hasMoreTokens()) {
1!
116
            al.add(new URL(getCodeBaseURL(), st.nextToken()));
×
117
        }
118
        return al;
1✔
119
    }
120

121
    /**
122
     * Returns an array containing the names of the parameters defined for the applet.
123
     */
124
    public String[] getParameterNames() {
125
        if (_parameterNames == null) {
1✔
126
            ArrayList al = new ArrayList(getParameterMap().keySet());
1✔
127
            _parameterNames = (String[]) al.toArray(new String[al.size()]);
1✔
128
        }
129
        return _parameterNames;
1✔
130
    }
131

132
    /**
133
     * Returns the value of the specified applet parameter, or null if not defined.
134
     */
135
    public String getParameter(String name) {
136
        return (String) getParameterMap().get(name);
1✔
137
    }
138

139
    private Map getParameterMap() {
140
        if (_parameters == null) {
1✔
141
            _parameters = new HashMap<>();
1✔
142
            NodeList nl = ((Element) getNode()).getElementsByTagName("param");
1✔
143
            for (int i = 0; i < nl.getLength(); i++) {
1✔
144
                Node n = nl.item(i);
1✔
145
                _parameters.put(NodeUtils.getNodeAttribute(n, "name", ""), NodeUtils.getNodeAttribute(n, "value", ""));
1✔
146
            }
147
        }
148
        return _parameters;
1✔
149
    }
150

151
    public Applet getApplet()
152
            throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException,
153
            IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
154
        if (_applet == null) {
1✔
155
            ClassLoader cl = new URLClassLoader(getClassPath(), null);
1✔
156
            Object o = cl.loadClass(getMainClassName()).getDeclaredConstructor().newInstance();
1✔
157
            if (!(o instanceof Applet)) {
1!
158
                throw new RuntimeException(getMainClassName() + " is not an Applet");
×
159
            }
160
            _applet = (Applet) o;
1✔
161
            _applet.setStub(new AppletStubImpl(this));
1✔
162
        }
163
        return _applet;
1✔
164
    }
165

166
    private URL[] getClassPath() throws MalformedURLException {
167
        List classPath = getArchiveList();
1✔
168
        classPath.add(getCodeBaseURL());
1✔
169
        return (URL[]) classPath.toArray(new URL[classPath.size()]);
1✔
170
    }
171

172
    String getBaseTarget() {
173
        return _baseTarget;
1✔
174
    }
175

176
    WebApplet[] getAppletsInPage() {
177
        try {
178
            return _response.getApplets();
1✔
179
        } catch (SAXException e) {
×
180
            HttpUnitUtils.handleException(e); // should never happen.
×
181
            return null;
×
182
        }
183
    }
184

185
    void sendRequest(URL url, String target) {
186
        WebRequest wr = new GetMethodWebRequest(null, url.toExternalForm(), target);
1✔
187
        try {
188
            _response.getWindow().getResponse(wr);
1✔
189
        } catch (IOException e) {
×
190
            e.printStackTrace(); // To change body of catch statement use Options | File Templates.
×
191
            throw new RuntimeException(e.toString());
×
192
        } catch (SAXException e) {
×
193
        }
1✔
194
    }
1✔
195

196
    @Override
197
    public ScriptableDelegate newScriptable() {
198
        return new HTMLElementScriptable(this);
×
199
    }
200

201
    @Override
202
    public ScriptableDelegate getParentDelegate() {
203
        return _response.getDocumentScriptable();
×
204
    }
205

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