• 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

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 java.io.File;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.net.MalformedURLException;
31
import java.nio.file.Path;
32
import java.util.Dictionary;
33
import java.util.Hashtable;
34

35
import javax.servlet.http.HttpSession;
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
     * @param webXMLFileSpec
60
     *            the full path to the web.xml file
61
     *
62
     * @throws IOException
63
     *             Signals that an I/O exception has occurred.
64
     * @throws SAXException
65
     *             the SAX exception
66
     *
67
     * @deprecated as of 1.6, use {@link #ServletRunner(File)}
68
     */
69
    @Deprecated
70
    public ServletRunner(String webXMLFileSpec) throws IOException, SAXException {
×
71
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXMLFileSpec));
×
72
        completeInitialization(null);
×
73
    }
×
74

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

352
    // -------------------------------------------- package methods
353
    // ---------------------------------------------------------
354

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

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

373
    // ---------------------------- private members ------------------------------------
374

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

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

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

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

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

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

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

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