• 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

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.nio.file.Path;
34
import java.util.Dictionary;
35
import java.util.Hashtable;
36

37
import javax.xml.parsers.DocumentBuilder;
38

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

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

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

57
    /**
58
     * Constructor which expects the full path to the web.xml for the application.
59
     *
60
     * @param webXMLFileSpec
61
     *            the full path to the web.xml file
62
     *
63
     * @throws IOException
64
     *             Signals that an I/O exception has occurred.
65
     * @throws SAXException
66
     *             the SAX exception
67
     *
68
     * @deprecated as of 1.6, use {@link #ServletRunner(File)}
69
     */
70
    @Deprecated
71
    public ServletRunner(String webXMLFileSpec) throws IOException, SAXException {
×
72
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXMLFileSpec));
×
73
        completeInitialization(null);
×
UNCOV
74
    }
×
75

76
    /**
77
     * Constructor which expects the full path to the web.xml for the application and a context path under which to
78
     * mount it.
79
     *
80
     * @param webXMLFileSpec
81
     *            the full path to the web.xml file
82
     * @param contextPath
83
     *            the context path
84
     *
85
     * @throws IOException
86
     *             Signals that an I/O exception has occurred.
87
     * @throws SAXException
88
     *             the SAX exception
89
     *
90
     * @deprecated as of 1.6, use {@link #ServletRunner(File,String)}
91
     */
92
    @Deprecated
93
    public ServletRunner(String webXMLFileSpec, String contextPath) throws IOException, SAXException {
94
        this(Path.of(webXMLFileSpec).toFile(), contextPath);
×
UNCOV
95
    }
×
96

97
    /**
98
     * Constructor which expects a File object representing the web.xml for the application.
99
     *
100
     * @param webXml
101
     *            the web.xml file
102
     *
103
     * @throws IOException
104
     *             Signals that an I/O exception has occurred.
105
     * @throws SAXException
106
     *             the SAX exception
107
     */
108
    public ServletRunner(File webXml) throws IOException, SAXException {
1✔
109
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXml));
1✔
110
        completeInitialization(null);
1✔
111
    }
1✔
112

113
    /**
114
     * Constructor which expects a File object representing the web.xml for the application and a context path under
115
     * which to mount it.
116
     *
117
     * @param webXml
118
     *            the web.xml file
119
     * @param contextPath
120
     *            the context path
121
     *
122
     * @throws IOException
123
     *             Signals that an I/O exception has occurred.
124
     * @throws SAXException
125
     *             the SAX exception
126
     */
127
    public ServletRunner(File webXml, String contextPath) throws IOException, SAXException {
1✔
128
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXml),
1✔
129
                webXml.getParentFile().getParentFile(), contextPath);
1✔
130
        completeInitialization(contextPath);
1✔
131
    }
1✔
132

133
    /**
134
     * constructor with entity Resolver as asked for in Bug report 1222269 by jim - jafergus.
135
     *
136
     * @param webXMLFileSpec
137
     *            the web XML file spec
138
     * @param resolver
139
     *            the resolver
140
     *
141
     * @throws IOException
142
     *             Signals that an I/O exception has occurred.
143
     * @throws SAXException
144
     *             the SAX exception
145
     */
146
    public ServletRunner(String webXMLFileSpec, EntityResolver resolver) throws IOException, SAXException {
×
147
        DocumentBuilder parser = HttpUnitUtils.newParser();
×
148
        parser.setEntityResolver(resolver);
×
149
        _application = new WebApplication(parser.parse(webXMLFileSpec));
×
150
        completeInitialization(null);
×
UNCOV
151
    }
×
152

153
    /**
154
     * Constructor which expects an input stream containing the web.xml for the application.
155
     *
156
     * @param webXML
157
     *            the web XML
158
     *
159
     * @throws IOException
160
     *             Signals that an I/O exception has occurred.
161
     * @throws SAXException
162
     *             the SAX exception
163
     */
164
    public ServletRunner(InputStream webXML) throws IOException, SAXException {
165
        this(webXML, null);
1✔
166
    }
1✔
167

168
    /**
169
     * Constructor which expects an input stream containing the web.xml for the application.
170
     *
171
     * @param webXML
172
     *            the web XML
173
     * @param contextPath
174
     *            the context path
175
     *
176
     * @throws IOException
177
     *             Signals that an I/O exception has occurred.
178
     * @throws SAXException
179
     *             the SAX exception
180
     */
181
    public ServletRunner(InputStream webXML, String contextPath) throws IOException, SAXException {
1✔
182
        InputSource inputSource = new InputSource(webXML);
1✔
183
        Document doc = HttpUnitUtils.parse(inputSource);
1✔
184
        try {
185
            _application = new WebApplication(doc, contextPath);
1✔
186
            completeInitialization(contextPath);
1✔
187
        } catch (java.net.MalformedURLException mue) {
×
UNCOV
188
            throw mue;
×
189
        }
1✔
190
    }
1✔
191

192
    /**
193
     * Registers a servlet class to be run.
194
     *
195
     * @param resourceName
196
     *            the resource name
197
     * @param servletClassName
198
     *            the servlet class name
199
     */
200
    public void registerServlet(String resourceName, String servletClassName) {
201
        _application.registerServlet(resourceName, servletClassName, null);
1✔
202
    }
1✔
203

204
    /**
205
     * Complete initialization.
206
     *
207
     * @param contextPath
208
     *            the context path
209
     */
210
    private void completeInitialization(String contextPath) {
211
        _context = new ServletUnitContext(contextPath, _application.getServletContext(), _application);
1✔
212
        _application.registerServlet("*.jsp", _jspServletDescriptor.getClassName(),
1✔
213
                _jspServletDescriptor.getInitializationParameters(null, null));
1✔
214
    }
1✔
215

216
    /**
217
     * Registers a servlet class to be run, specifying initialization parameters.
218
     *
219
     * @param resourceName
220
     *            the resource name
221
     * @param servletClassName
222
     *            the servlet class name
223
     * @param initParameters
224
     *            the init parameters
225
     */
226
    public void registerServlet(String resourceName, String servletClassName, Hashtable initParameters) {
227
        _application.registerServlet(resourceName, servletClassName, initParameters);
×
UNCOV
228
    }
×
229

230
    /**
231
     * Returns the response from the specified servlet.
232
     *
233
     * @param request
234
     *            the request
235
     *
236
     * @return the response
237
     *
238
     * @throws MalformedURLException
239
     *             the malformed URL exception
240
     * @throws IOException
241
     *             Signals that an I/O exception has occurred.
242
     *
243
     * @exception SAXException
244
     *                thrown if there is an error parsing the response
245
     */
246
    public WebResponse getResponse(WebRequest request) throws MalformedURLException, IOException, SAXException {
247
        return getClient().getResponse(request);
1✔
248
    }
249

250
    /**
251
     * Returns the response from the specified servlet using GET.
252
     *
253
     * @param url
254
     *            the url
255
     *
256
     * @return the response
257
     *
258
     * @throws MalformedURLException
259
     *             the malformed URL exception
260
     * @throws IOException
261
     *             Signals that an I/O exception has occurred.
262
     *
263
     * @exception SAXException
264
     *                thrown if there is an error parsing the response
265
     */
266
    public WebResponse getResponse(String url) throws MalformedURLException, IOException, SAXException {
UNCOV
267
        return getClient().getResponse(url);
×
268
    }
269

270
    /**
271
     * Returns the session to be used by the next request.
272
     *
273
     * @param create
274
     *            if true, will create a new session if no valid session is defined.
275
     *
276
     * @return the session
277
     */
278
    public HttpSession getSession(boolean create) {
279
        return getClient().getSession(create);
1✔
280
    }
281

282
    /**
283
     * Returns the value of the named context parameter found in the application definition.
284
     *
285
     * @param name
286
     *            - the name of the parameter to get
287
     *
288
     * @return - the context parameter with the given name
289
     */
290
    public String getContextParameter(String name) {
291
        Object value = _application.getContextParameters().get(name);
1✔
292
        return value == null ? null : value.toString();
1!
293
    }
294

295
    /**
296
     * Sets a application context parameter.
297
     *
298
     * @param name
299
     *            - the name of the parameter to set
300
     * @param value
301
     *            - the value of the parameter to set
302
     *
303
     * @deprecated - test case for this function deactivated wf 2007-12-30
304
     */
305
    @Deprecated
306
    public void setContextParameter(String name, Object value) {
307
        getApplication().getServletContext().setAttribute(name, value);
×
UNCOV
308
    }
×
309

310
    /**
311
     * Shuts down the servlet container, returning any resources held by it. Calls the destroy method of each active
312
     * servlet, then notifies ContextListeners of server shutdown.
313
     */
314
    public void shutDown() {
315
        _application.shutDown();
1✔
316
    }
1✔
317

318
    /**
319
     * Creates and returns a new web client that communicates with this servlet runner.
320
     *
321
     * @return the servlet unit client
322
     */
323
    public ServletUnitClient newClient() {
324
        return ServletUnitClient.newClient(_factory);
1✔
325
    }
326

327
    /**
328
     * The Class JasperJSPServletDescriptor.
329
     */
330
    public static class JasperJSPServletDescriptor implements JSPServletDescriptor {
1✔
331

332
        @Override
333
        public String getClassName() {
334
            return "org.apache.jasper.servlet.JspServlet";
1✔
335
        }
336

337
        @Override
338
        public Hashtable getInitializationParameters(String classPath, String workingDirectory) {
339
            Hashtable params = new Hashtable<>();
1✔
340
            if (classPath != null) {
1!
UNCOV
341
                params.put("classpath", classPath);
×
342
            }
343
            if (workingDirectory != null) {
1!
UNCOV
344
                params.put("scratchdir", workingDirectory);
×
345
            }
346
            return params;
1✔
347
        }
348
    }
349

350
    /** The Constant JASPER_DESCRIPTOR. */
351
    public static final JSPServletDescriptor JASPER_DESCRIPTOR = new JasperJSPServletDescriptor();
1✔
352

353
    // -------------------------------------------- package methods
354
    // ---------------------------------------------------------
355

356
    /**
357
     * Gets the context.
358
     *
359
     * @return the context
360
     */
361
    ServletUnitContext getContext() {
362
        return _context;
1✔
363
    }
364

365
    /**
366
     * Gets the application.
367
     *
368
     * @return the application
369
     */
370
    WebApplication getApplication() {
371
        return _application;
1✔
372
    }
373

374
    // ---------------------------- private members ------------------------------------
375

376
    /** The jsp servlet descriptor. */
377
    private static JSPServletDescriptor _jspServletDescriptor = JASPER_DESCRIPTOR;
1✔
378

379
    /** The application. */
380
    private WebApplication _application;
381

382
    /** The client. */
383
    private ServletUnitClient _client;
384

385
    /** The context. */
386
    private ServletUnitContext _context;
387

388
    /** The factory. */
389
    private InvocationContextFactory _factory = new InvocationContextFactory() {
1✔
390
        @Override
391
        public InvocationContext newInvocation(ServletUnitClient client, FrameSelector targetFrame, WebRequest request,
392
                Dictionary clientHeaders, byte[] messageBody) throws IOException, MalformedURLException {
393
            return new InvocationContextImpl(client, ServletRunner.this, targetFrame, request, clientHeaders,
1✔
394
                    messageBody);
395
        }
396

397
        @Override
398
        public HttpSession getSession(String sessionId, boolean create) {
399
            return _context.getValidSession(sessionId, null, create);
1✔
400
        }
401
    };
402

403
    /**
404
     * Gets the client.
405
     *
406
     * @return the client
407
     */
408
    private ServletUnitClient getClient() {
409
        if (_client == null) {
1✔
410
            _client = newClient();
1✔
411
        }
412
        return _client;
1✔
413
    }
414

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