• 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

70.0
/src/main/java/com/meterware/servletunit/ServletRunner.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.servletunit;
21

22
import com.meterware.httpunit.FrameSelector;
23
import com.meterware.httpunit.HttpUnitUtils;
24
import com.meterware.httpunit.WebRequest;
25
import com.meterware.httpunit.WebResponse;
26

27
import jakarta.servlet.http.HttpSession;
28

29
import java.io.File;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.net.MalformedURLException;
33
import java.util.Dictionary;
34
import java.util.Hashtable;
35

36
import javax.xml.parsers.DocumentBuilder;
37

38
import org.w3c.dom.Document;
39
import org.xml.sax.EntityResolver;
40
import org.xml.sax.InputSource;
41
import org.xml.sax.SAXException;
42

43
/**
44
 * This class acts as a test environment for servlets.
45
 **/
46
public class ServletRunner {
47

48
    /**
49
     * Default constructor, which defines no servlets.
50
     */
51
    public ServletRunner() {
1✔
52
        _application = new WebApplication();
1✔
53
        completeInitialization(null);
1✔
54
    }
1✔
55

56
    /**
57
     * Constructor which expects the full path to the web.xml for the application.
58
     *
59
     * @deprecated as of 1.6, use {@link #ServletRunner(File)}
60
     *
61
     * @param webXMLFileSpec
62
     *            the full path to the web.xml file
63
     */
64
    @Deprecated
65
    public ServletRunner(String webXMLFileSpec) throws IOException, SAXException {
×
66
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXMLFileSpec));
×
67
        completeInitialization(null);
×
68
    }
×
69

70
    /**
71
     * Constructor which expects the full path to the web.xml for the application and a context path under which to
72
     * mount it.
73
     *
74
     * @deprecated as of 1.6, use {@link #ServletRunner(File,String)}
75
     *
76
     * @param webXMLFileSpec
77
     *            the full path to the web.xml file
78
     * @param contextPath
79
     *            the context path
80
     */
81
    @Deprecated
82
    public ServletRunner(String webXMLFileSpec, String contextPath) throws IOException, SAXException {
83
        this(new File(webXMLFileSpec), contextPath);
×
84
    }
×
85

86
    /**
87
     * Constructor which expects a File object representing the web.xml for the application.
88
     *
89
     * @param webXml
90
     *            the web.xml file
91
     */
92
    public ServletRunner(File webXml) throws IOException, SAXException {
1✔
93
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXml));
1✔
94
        completeInitialization(null);
1✔
95
    }
1✔
96

97
    /**
98
     * Constructor which expects a File object representing the web.xml for the application and a context path under
99
     * which to mount it.
100
     *
101
     * @param webXml
102
     *            the web.xml file
103
     * @param contextPath
104
     *            the context path
105
     */
106
    public ServletRunner(File webXml, String contextPath) throws IOException, SAXException {
1✔
107
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXml),
1✔
108
                webXml.getParentFile().getParentFile(), contextPath);
1✔
109
        completeInitialization(contextPath);
1✔
110
    }
1✔
111

112
    /**
113
     * constructor with entity Resolver as asked for in Bug report 1222269 by jim - jafergus
114
     *
115
     * @param webXMLFileSpec
116
     * @param resolver
117
     *
118
     * @throws IOException
119
     * @throws SAXException
120
     */
121
    public ServletRunner(String webXMLFileSpec, EntityResolver resolver) throws IOException, SAXException {
×
122
        DocumentBuilder parser = HttpUnitUtils.newParser();
×
123
        parser.setEntityResolver(resolver);
×
124
        _application = new WebApplication(parser.parse(webXMLFileSpec));
×
125
        completeInitialization(null);
×
126
    }
×
127

128
    /**
129
     * Constructor which expects an input stream containing the web.xml for the application.
130
     **/
131
    public ServletRunner(InputStream webXML) throws IOException, SAXException {
132
        this(webXML, null);
1✔
133
    }
1✔
134

135
    /**
136
     * Constructor which expects an input stream containing the web.xml for the application.
137
     *
138
     * @param webXML
139
     * @param contextPath
140
     *
141
     * @throws IOException
142
     * @throws SAXException
143
     */
144
    public ServletRunner(InputStream webXML, String contextPath) throws IOException, SAXException {
1✔
145
        InputSource inputSource = new InputSource(webXML);
1✔
146
        Document doc = HttpUnitUtils.parse(inputSource);
1✔
147
        try {
148
            _application = new WebApplication(doc, contextPath);
1✔
149
            completeInitialization(contextPath);
1✔
150
        } catch (java.net.MalformedURLException mue) {
×
151
            throw mue;
×
152
        }
1✔
153
    }
1✔
154

155
    /**
156
     * Registers a servlet class to be run.
157
     **/
158
    public void registerServlet(String resourceName, String servletClassName) {
159
        _application.registerServlet(resourceName, servletClassName, null);
1✔
160
    }
1✔
161

162
    private void completeInitialization(String contextPath) {
163
        _context = new ServletUnitContext(contextPath, _application.getServletContext(), _application);
1✔
164
        _application.registerServlet("*.jsp", _jspServletDescriptor.getClassName(),
1✔
165
                _jspServletDescriptor.getInitializationParameters(null, null));
1✔
166
    }
1✔
167

168
    /**
169
     * Registers a servlet class to be run, specifying initialization parameters.
170
     **/
171
    public void registerServlet(String resourceName, String servletClassName, Hashtable initParameters) {
172
        _application.registerServlet(resourceName, servletClassName, initParameters);
×
173
    }
×
174

175
    /**
176
     * Returns the response from the specified servlet.
177
     *
178
     * @exception SAXException
179
     *                thrown if there is an error parsing the response
180
     **/
181
    public WebResponse getResponse(WebRequest request) throws MalformedURLException, IOException, SAXException {
182
        return getClient().getResponse(request);
1✔
183
    }
184

185
    /**
186
     * Returns the response from the specified servlet using GET.
187
     *
188
     * @exception SAXException
189
     *                thrown if there is an error parsing the response
190
     **/
191
    public WebResponse getResponse(String url) throws MalformedURLException, IOException, SAXException {
192
        return getClient().getResponse(url);
×
193
    }
194

195
    /**
196
     * Returns the session to be used by the next request.
197
     *
198
     * @param create
199
     *            if true, will create a new session if no valid session is defined.
200
     */
201
    public HttpSession getSession(boolean create) {
202
        return getClient().getSession(create);
1✔
203
    }
204

205
    /**
206
     * Returns the value of the named context parameter found in the application definition.
207
     *
208
     * @param name
209
     *            - the name of the parameter to get
210
     *
211
     * @return - the context parameter with the given name
212
     */
213
    public String getContextParameter(String name) {
214
        Object value = _application.getContextParameters().get(name);
1✔
215
        return value == null ? null : value.toString();
1!
216
    }
217

218
    /**
219
     * Sets a application context parameter.
220
     *
221
     * @param name
222
     *            - the name of the parameter to set
223
     * @param value
224
     *            - the value of the parameter to set
225
     *
226
     * @deprecated - test case for this function deactivated wf 2007-12-30
227
     */
228
    @Deprecated
229
    public void setContextParameter(String name, Object value) {
230
        getApplication().getServletContext().setAttribute(name, value);
×
231
    }
×
232

233
    /**
234
     * Shuts down the servlet container, returning any resources held by it. Calls the destroy method of each active
235
     * servlet, then notifies ContextListeners of server shutdown.
236
     */
237
    public void shutDown() {
238
        _application.shutDown();
1✔
239
    }
1✔
240

241
    /**
242
     * Creates and returns a new web client that communicates with this servlet runner.
243
     **/
244
    public ServletUnitClient newClient() {
245
        return ServletUnitClient.newClient(_factory);
1✔
246
    }
247

248
    public static class JasperJSPServletDescriptor implements JSPServletDescriptor {
1✔
249

250
        @Override
251
        public String getClassName() {
252
            return "org.apache.jasper.servlet.JspServlet";
1✔
253
        }
254

255
        @Override
256
        public Hashtable getInitializationParameters(String classPath, String workingDirectory) {
257
            Hashtable params = new Hashtable<>();
1✔
258
            if (classPath != null) {
1!
259
                params.put("classpath", classPath);
×
260
            }
261
            if (workingDirectory != null) {
1!
262
                params.put("scratchdir", workingDirectory);
×
263
            }
264
            return params;
1✔
265
        }
266
    }
267

268
    public static final JSPServletDescriptor JASPER_DESCRIPTOR = new JasperJSPServletDescriptor();
1✔
269

270
    // -------------------------------------------- package methods
271
    // ---------------------------------------------------------
272

273
    ServletUnitContext getContext() {
274
        return _context;
1✔
275
    }
276

277
    WebApplication getApplication() {
278
        return _application;
1✔
279
    }
280

281
    // ---------------------------- private members ------------------------------------
282

283
    private static JSPServletDescriptor _jspServletDescriptor = JASPER_DESCRIPTOR;
1✔
284

285
    private WebApplication _application;
286

287
    private ServletUnitClient _client;
288

289
    private ServletUnitContext _context;
290

291
    private InvocationContextFactory _factory = new InvocationContextFactory() {
1✔
292
        @Override
293
        public InvocationContext newInvocation(ServletUnitClient client, FrameSelector targetFrame, WebRequest request,
294
                Dictionary clientHeaders, byte[] messageBody) throws IOException, MalformedURLException {
295
            return new InvocationContextImpl(client, ServletRunner.this, targetFrame, request, clientHeaders,
1✔
296
                    messageBody);
297
        }
298

299
        @Override
300
        public HttpSession getSession(String sessionId, boolean create) {
301
            return _context.getValidSession(sessionId, null, create);
1✔
302
        }
303
    };
304

305
    private ServletUnitClient getClient() {
306
        if (_client == null) {
1✔
307
            _client = newClient();
1✔
308
        }
309
        return _client;
1✔
310
    }
311

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