• 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

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

10
import com.meterware.httpunit.FrameSelector;
11
import com.meterware.httpunit.HttpUnitUtils;
12
import com.meterware.httpunit.WebRequest;
13
import com.meterware.httpunit.WebResponse;
14

15
import jakarta.servlet.http.HttpSession;
16

17
import java.io.File;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.net.MalformedURLException;
21
import java.nio.file.Path;
22
import java.util.Dictionary;
23
import java.util.Hashtable;
24

25
import javax.xml.parsers.DocumentBuilder;
26

27
import org.w3c.dom.Document;
28
import org.xml.sax.EntityResolver;
29
import org.xml.sax.InputSource;
30
import org.xml.sax.SAXException;
31

32
/**
33
 * This class acts as a test environment for servlets.
34
 **/
35
public class ServletRunner {
36

37
    /**
38
     * Default constructor, which defines no servlets.
39
     */
40
    public ServletRunner() {
1✔
41
        _application = new WebApplication();
1✔
42
        completeInitialization(null);
1✔
43
    }
1✔
44

45
    /**
46
     * Constructor which expects the full path to the web.xml for the application.
47
     *
48
     * @param webXMLFileSpec
49
     *            the full path to the web.xml file
50
     *
51
     * @throws IOException
52
     *             Signals that an I/O exception has occurred.
53
     * @throws SAXException
54
     *             the SAX exception
55
     *
56
     * @deprecated as of 1.6, use {@link #ServletRunner(File)}
57
     */
58
    @Deprecated
59
    public ServletRunner(String webXMLFileSpec) throws IOException, SAXException {
×
60
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXMLFileSpec));
×
61
        completeInitialization(null);
×
62
    }
×
63

64
    /**
65
     * Constructor which expects the full path to the web.xml for the application and a context path under which to
66
     * mount it.
67
     *
68
     * @param webXMLFileSpec
69
     *            the full path to the web.xml file
70
     * @param contextPath
71
     *            the context path
72
     *
73
     * @throws IOException
74
     *             Signals that an I/O exception has occurred.
75
     * @throws SAXException
76
     *             the SAX exception
77
     *
78
     * @deprecated as of 1.6, use {@link #ServletRunner(File,String)}
79
     */
80
    @Deprecated
81
    public ServletRunner(String webXMLFileSpec, String contextPath) throws IOException, SAXException {
82
        this(Path.of(webXMLFileSpec).toFile(), contextPath);
×
83
    }
×
84

85
    /**
86
     * Constructor which expects a File object representing the web.xml for the application.
87
     *
88
     * @param webXml
89
     *            the web.xml file
90
     *
91
     * @throws IOException
92
     *             Signals that an I/O exception has occurred.
93
     * @throws SAXException
94
     *             the SAX exception
95
     */
96
    public ServletRunner(File webXml) throws IOException, SAXException {
1✔
97
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXml));
1✔
98
        completeInitialization(null);
1✔
99
    }
1✔
100

101
    /**
102
     * Constructor which expects a File object representing the web.xml for the application and a context path under
103
     * which to mount it.
104
     *
105
     * @param webXml
106
     *            the web.xml file
107
     * @param contextPath
108
     *            the context path
109
     *
110
     * @throws IOException
111
     *             Signals that an I/O exception has occurred.
112
     * @throws SAXException
113
     *             the SAX exception
114
     */
115
    public ServletRunner(File webXml, String contextPath) throws IOException, SAXException {
1✔
116
        _application = new WebApplication(HttpUnitUtils.newParser().parse(webXml),
1✔
117
                webXml.getParentFile().getParentFile(), contextPath);
1✔
118
        completeInitialization(contextPath);
1✔
119
    }
1✔
120

121
    /**
122
     * constructor with entity Resolver as asked for in Bug report 1222269 by jim - jafergus.
123
     *
124
     * @param webXMLFileSpec
125
     *            the web XML file spec
126
     * @param resolver
127
     *            the resolver
128
     *
129
     * @throws IOException
130
     *             Signals that an I/O exception has occurred.
131
     * @throws SAXException
132
     *             the SAX exception
133
     */
134
    public ServletRunner(String webXMLFileSpec, EntityResolver resolver) throws IOException, SAXException {
×
135
        DocumentBuilder parser = HttpUnitUtils.newParser();
×
136
        parser.setEntityResolver(resolver);
×
137
        _application = new WebApplication(parser.parse(webXMLFileSpec));
×
138
        completeInitialization(null);
×
139
    }
×
140

141
    /**
142
     * Constructor which expects an input stream containing the web.xml for the application.
143
     *
144
     * @param webXML
145
     *            the web XML
146
     *
147
     * @throws IOException
148
     *             Signals that an I/O exception has occurred.
149
     * @throws SAXException
150
     *             the SAX exception
151
     */
152
    public ServletRunner(InputStream webXML) throws IOException, SAXException {
153
        this(webXML, null);
1✔
154
    }
1✔
155

156
    /**
157
     * Constructor which expects an input stream containing the web.xml for the application.
158
     *
159
     * @param webXML
160
     *            the web XML
161
     * @param contextPath
162
     *            the context path
163
     *
164
     * @throws IOException
165
     *             Signals that an I/O exception has occurred.
166
     * @throws SAXException
167
     *             the SAX exception
168
     */
169
    public ServletRunner(InputStream webXML, String contextPath) throws IOException, SAXException {
1✔
170
        InputSource inputSource = new InputSource(webXML);
1✔
171
        Document doc = HttpUnitUtils.parse(inputSource);
1✔
172
        try {
173
            _application = new WebApplication(doc, contextPath);
1✔
174
            completeInitialization(contextPath);
1✔
175
        } catch (java.net.MalformedURLException mue) {
×
176
            throw mue;
×
177
        }
1✔
178
    }
1✔
179

180
    /**
181
     * Registers a servlet class to be run.
182
     *
183
     * @param resourceName
184
     *            the resource name
185
     * @param servletClassName
186
     *            the servlet class name
187
     */
188
    public void registerServlet(String resourceName, String servletClassName) {
189
        _application.registerServlet(resourceName, servletClassName, null);
1✔
190
    }
1✔
191

192
    /**
193
     * Complete initialization.
194
     *
195
     * @param contextPath
196
     *            the context path
197
     */
198
    private void completeInitialization(String contextPath) {
199
        _context = new ServletUnitContext(contextPath, _application.getServletContext(), _application);
1✔
200
        _application.registerServlet("*.jsp", _jspServletDescriptor.getClassName(),
1✔
201
                _jspServletDescriptor.getInitializationParameters(null, null));
1✔
202
    }
1✔
203

204
    /**
205
     * Registers a servlet class to be run, specifying initialization parameters.
206
     *
207
     * @param resourceName
208
     *            the resource name
209
     * @param servletClassName
210
     *            the servlet class name
211
     * @param initParameters
212
     *            the init parameters
213
     */
214
    public void registerServlet(String resourceName, String servletClassName, Hashtable initParameters) {
215
        _application.registerServlet(resourceName, servletClassName, initParameters);
×
216
    }
×
217

218
    /**
219
     * Returns the response from the specified servlet.
220
     *
221
     * @param request
222
     *            the request
223
     *
224
     * @return the response
225
     *
226
     * @throws MalformedURLException
227
     *             the malformed URL exception
228
     * @throws IOException
229
     *             Signals that an I/O exception has occurred.
230
     *
231
     * @exception SAXException
232
     *                thrown if there is an error parsing the response
233
     */
234
    public WebResponse getResponse(WebRequest request) throws MalformedURLException, IOException, SAXException {
235
        return getClient().getResponse(request);
1✔
236
    }
237

238
    /**
239
     * Returns the response from the specified servlet using GET.
240
     *
241
     * @param url
242
     *            the url
243
     *
244
     * @return the response
245
     *
246
     * @throws MalformedURLException
247
     *             the malformed URL exception
248
     * @throws IOException
249
     *             Signals that an I/O exception has occurred.
250
     *
251
     * @exception SAXException
252
     *                thrown if there is an error parsing the response
253
     */
254
    public WebResponse getResponse(String url) throws MalformedURLException, IOException, SAXException {
255
        return getClient().getResponse(url);
×
256
    }
257

258
    /**
259
     * Returns the session to be used by the next request.
260
     *
261
     * @param create
262
     *            if true, will create a new session if no valid session is defined.
263
     *
264
     * @return the session
265
     */
266
    public HttpSession getSession(boolean create) {
267
        return getClient().getSession(create);
1✔
268
    }
269

270
    /**
271
     * Returns the value of the named context parameter found in the application definition.
272
     *
273
     * @param name
274
     *            - the name of the parameter to get
275
     *
276
     * @return - the context parameter with the given name
277
     */
278
    public String getContextParameter(String name) {
279
        Object value = _application.getContextParameters().get(name);
1✔
280
        return value == null ? null : value.toString();
1!
281
    }
282

283
    /**
284
     * Sets a application context parameter.
285
     *
286
     * @param name
287
     *            - the name of the parameter to set
288
     * @param value
289
     *            - the value of the parameter to set
290
     *
291
     * @deprecated - test case for this function deactivated wf 2007-12-30
292
     */
293
    @Deprecated
294
    public void setContextParameter(String name, Object value) {
295
        getApplication().getServletContext().setAttribute(name, value);
×
296
    }
×
297

298
    /**
299
     * Shuts down the servlet container, returning any resources held by it. Calls the destroy method of each active
300
     * servlet, then notifies ContextListeners of server shutdown.
301
     */
302
    public void shutDown() {
303
        _application.shutDown();
1✔
304
    }
1✔
305

306
    /**
307
     * Creates and returns a new web client that communicates with this servlet runner.
308
     *
309
     * @return the servlet unit client
310
     */
311
    public ServletUnitClient newClient() {
312
        return ServletUnitClient.newClient(_factory);
1✔
313
    }
314

315
    /**
316
     * The Class JasperJSPServletDescriptor.
317
     */
318
    public static class JasperJSPServletDescriptor implements JSPServletDescriptor {
1✔
319

320
        @Override
321
        public String getClassName() {
322
            return "org.apache.jasper.servlet.JspServlet";
1✔
323
        }
324

325
        @Override
326
        public Hashtable getInitializationParameters(String classPath, String workingDirectory) {
327
            Hashtable params = new Hashtable<>();
1✔
328
            if (classPath != null) {
1!
329
                params.put("classpath", classPath);
×
330
            }
331
            if (workingDirectory != null) {
1!
332
                params.put("scratchdir", workingDirectory);
×
333
            }
334
            return params;
1✔
335
        }
336
    }
337

338
    /** The Constant JASPER_DESCRIPTOR. */
339
    public static final JSPServletDescriptor JASPER_DESCRIPTOR = new JasperJSPServletDescriptor();
1✔
340

341
    // -------------------------------------------- package methods
342
    // ---------------------------------------------------------
343

344
    /**
345
     * Gets the context.
346
     *
347
     * @return the context
348
     */
349
    ServletUnitContext getContext() {
350
        return _context;
1✔
351
    }
352

353
    /**
354
     * Gets the application.
355
     *
356
     * @return the application
357
     */
358
    WebApplication getApplication() {
359
        return _application;
1✔
360
    }
361

362
    // ---------------------------- private members ------------------------------------
363

364
    /** The jsp servlet descriptor. */
365
    private static JSPServletDescriptor _jspServletDescriptor = JASPER_DESCRIPTOR;
1✔
366

367
    /** The application. */
368
    private WebApplication _application;
369

370
    /** The client. */
371
    private ServletUnitClient _client;
372

373
    /** The context. */
374
    private ServletUnitContext _context;
375

376
    /** The factory. */
377
    private InvocationContextFactory _factory = new InvocationContextFactory() {
1✔
378
        @Override
379
        public InvocationContext newInvocation(ServletUnitClient client, FrameSelector targetFrame, WebRequest request,
380
                Dictionary clientHeaders, byte[] messageBody) throws IOException, MalformedURLException {
381
            return new InvocationContextImpl(client, ServletRunner.this, targetFrame, request, clientHeaders,
1✔
382
                    messageBody);
383
        }
384

385
        @Override
386
        public HttpSession getSession(String sessionId, boolean create) {
387
            return _context.getValidSession(sessionId, null, create);
1✔
388
        }
389
    };
390

391
    /**
392
     * Gets the client.
393
     *
394
     * @return the client
395
     */
396
    private ServletUnitClient getClient() {
397
        if (_client == null) {
1✔
398
            _client = newClient();
1✔
399
        }
400
        return _client;
1✔
401
    }
402

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