1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
58
58
*
59
59
* <p>Used for testing the Spring web framework; only rarely necessary for testing
60
60
* application controllers. As long as application components don't explicitly
61
- * access the ServletContext, ClassPathXmlApplicationContext or
62
- * FileSystemXmlApplicationContext can be used to load the context files for testing,
63
- * even for DispatcherServlet context definitions.
61
+ * access the {@code ServletContext}, {@code ClassPathXmlApplicationContext} or
62
+ * {@code FileSystemXmlApplicationContext} can be used to load the context files
63
+ * for testing, even for {@code DispatcherServlet} context definitions.
64
64
*
65
- * <p>For setting up a full WebApplicationContext in a test environment, you can
66
- * use XmlWebApplicationContext (or GenericWebApplicationContext), passing in an
67
- * appropriate MockServletContext instance. You might want to configure your
68
- * MockServletContext with a FileSystemResourceLoader in that case, to make your
69
- * resource paths interpreted as relative file system locations.
65
+ * <p>For setting up a full {@code WebApplicationContext} in a test environment,
66
+ * you can use {@code AnnotationConfigWebApplicationContext},
67
+ * {@code XmlWebApplicationContext}, or {@code GenericWebApplicationContext},
68
+ * passing in an appropriate {@code MockServletContext} instance. You might want
69
+ * to configure your {@code MockServletContext} with a {@code FileSystemResourceLoader}
70
+ * in that case to ensure that resource paths are interpreted as relative filesystem
71
+ * locations.
70
72
*
71
73
* <p>A common setup is to point your JVM working directory to the root of your
72
74
* web application directory, in combination with filesystem-based resource loading.
73
75
* This allows to load the context files as used in the web application, with
74
76
* relative paths getting interpreted correctly. Such a setup will work with both
75
- * FileSystemXmlApplicationContext (which will load straight from the file system)
76
- * and XmlWebApplicationContext with an underlying MockServletContext (as long as
77
- * the MockServletContext has been configured with a FileSystemResourceLoader).
77
+ * {@code FileSystemXmlApplicationContext} (which will load straight from the
78
+ * filesystem) and {@code XmlWebApplicationContext} with an underlying
79
+ * {@code MockServletContext} (as long as the {@code MockServletContext} has been
80
+ * configured with a {@code FileSystemResourceLoader}).
78
81
*
79
82
* @author Rod Johnson
80
83
* @author Juergen Hoeller
84
+ * @author Sam Brannen
81
85
* @since 1.0.2
82
86
* @see #MockServletContext(org.springframework.core.io.ResourceLoader)
87
+ * @see org.springframework.web.context.support.AnnotationConfigWebApplicationContext
83
88
* @see org.springframework.web.context.support.XmlWebApplicationContext
84
89
* @see org.springframework.web.context.support.GenericWebApplicationContext
85
90
* @see org.springframework.context.support.ClassPathXmlApplicationContext
86
91
* @see org.springframework.context.support.FileSystemXmlApplicationContext
87
92
*/
88
93
public class MockServletContext implements ServletContext {
89
94
90
- private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir" ;
95
+ /** Default Servlet name used by Tomcat, Jetty, JBoss, and GlassFish: {@value}. */
96
+ private static final String COMMON_DEFAULT_SERVLET_NAME = "default" ;
91
97
98
+ private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir" ;
92
99
93
100
private final Log logger = LogFactory .getLog (getClass ());
94
101
102
+ private final Map <String , ServletContext > contexts = new HashMap <String , ServletContext >();
103
+
104
+ private final Map <String , String > initParameters = new LinkedHashMap <String , String >();
105
+
106
+ private final Map <String , Object > attributes = new LinkedHashMap <String , Object >();
107
+
108
+ private final Set <String > declaredRoles = new HashSet <String >();
109
+
110
+ private final Map <String , RequestDispatcher > namedRequestDispatchers = new HashMap <String , RequestDispatcher >();
111
+
95
112
private final ResourceLoader resourceLoader ;
96
113
97
114
private final String resourceBasePath ;
@@ -106,15 +123,9 @@ public class MockServletContext implements ServletContext {
106
123
107
124
private int effectiveMinorVersion = 5 ;
108
125
109
- private final Map <String , ServletContext > contexts = new HashMap <String , ServletContext >();
110
-
111
- private final Map <String , String > initParameters = new LinkedHashMap <String , String >();
112
-
113
- private final Map <String , Object > attributes = new LinkedHashMap <String , Object >();
114
-
115
126
private String servletContextName = "MockServletContext" ;
116
127
117
- private final Set < String > declaredRoles = new HashSet < String >() ;
128
+ private String defaultServletName = COMMON_DEFAULT_SERVLET_NAME ;
118
129
119
130
120
131
/**
@@ -128,7 +139,7 @@ public MockServletContext() {
128
139
129
140
/**
130
141
* Create a new MockServletContext, using a DefaultResourceLoader.
131
- * @param resourceBasePath the WAR root directory (should not end with a slash)
142
+ * @param resourceBasePath the root directory of the WAR (should not end with a slash)
132
143
* @see org.springframework.core.io.DefaultResourceLoader
133
144
*/
134
145
public MockServletContext (String resourceBasePath ) {
@@ -145,9 +156,13 @@ public MockServletContext(ResourceLoader resourceLoader) {
145
156
}
146
157
147
158
/**
148
- * Create a new MockServletContext.
149
- * @param resourceBasePath the WAR root directory (should not end with a slash)
159
+ * Create a new MockServletContext using the supplied resource base path and
160
+ * resource loader.
161
+ * <p>Registers a {@link MockRequestDispatcher} for the Servlet named
162
+ * {@value #COMMON_DEFAULT_SERVLET_NAME}.
163
+ * @param resourceBasePath the root directory of the WAR (should not end with a slash)
150
164
* @param resourceLoader the ResourceLoader to use (or null for the default)
165
+ * @see #registerNamedDispatcher
151
166
*/
152
167
public MockServletContext (String resourceBasePath , ResourceLoader resourceLoader ) {
153
168
this .resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader ());
@@ -158,8 +173,9 @@ public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader
158
173
if (tempDir != null ) {
159
174
this .attributes .put (WebUtils .TEMP_DIR_CONTEXT_ATTRIBUTE , new File (tempDir ));
160
175
}
161
- }
162
176
177
+ registerNamedDispatcher (this .defaultServletName , new MockRequestDispatcher (this .defaultServletName ));
178
+ }
163
179
164
180
/**
165
181
* Build a full resource location for the given path,
@@ -174,7 +190,6 @@ protected String getResourceLocation(String path) {
174
190
return this .resourceBasePath + path ;
175
191
}
176
192
177
-
178
193
public void setContextPath (String contextPath ) {
179
194
this .contextPath = (contextPath != null ? contextPath : "" );
180
195
}
@@ -295,7 +310,60 @@ public RequestDispatcher getRequestDispatcher(String path) {
295
310
}
296
311
297
312
public RequestDispatcher getNamedDispatcher (String path ) {
298
- return null ;
313
+ return this .namedRequestDispatchers .get (path );
314
+ }
315
+
316
+ /**
317
+ * Register a {@link RequestDispatcher} (typically a {@link MockRequestDispatcher})
318
+ * that acts as a wrapper for the named Servlet.
319
+ *
320
+ * @param name the name of the wrapped Servlet
321
+ * @param requestDispatcher the dispatcher that wraps the named Servlet
322
+ * @see #getNamedDispatcher
323
+ * @see #unregisterNamedDispatcher
324
+ */
325
+ public void registerNamedDispatcher (String name , RequestDispatcher requestDispatcher ) {
326
+ Assert .notNull (name , "RequestDispatcher name must not be null" );
327
+ Assert .notNull (requestDispatcher , "RequestDispatcher must not be null" );
328
+ this .namedRequestDispatchers .put (name , requestDispatcher );
329
+ }
330
+
331
+ /**
332
+ * Unregister the {@link RequestDispatcher} with the given name.
333
+ *
334
+ * @param name the name of the dispatcher to unregister
335
+ * @see #getNamedDispatcher
336
+ * @see #registerNamedDispatcher
337
+ */
338
+ public void unregisterNamedDispatcher (String name ) {
339
+ Assert .notNull (name , "RequestDispatcher name must not be null" );
340
+ this .namedRequestDispatchers .remove (name );
341
+ }
342
+
343
+ /**
344
+ * Get the name of the <em>default</em> {@code Servlet}.
345
+ * <p>Defaults to {@value #COMMON_DEFAULT_SERVLET_NAME}.
346
+ * @see #setDefaultServletName
347
+ */
348
+ public String getDefaultServletName () {
349
+ return this .defaultServletName ;
350
+ }
351
+
352
+ /**
353
+ * Set the name of the <em>default</em> {@code Servlet}.
354
+ * <p>Also {@link #unregisterNamedDispatcher unregisters} the current default
355
+ * {@link RequestDispatcher} and {@link #registerNamedDispatcher replaces}
356
+ * it with a {@link MockRequestDispatcher} for the provided
357
+ * {@code defaultServletName}.
358
+ * @param defaultServletName the name of the <em>default</em> {@code Servlet};
359
+ * never {@code null} or empty
360
+ * @see #getDefaultServletName
361
+ */
362
+ public void setDefaultServletName (String defaultServletName ) {
363
+ Assert .hasText (defaultServletName , "defaultServletName must not be null or empty" );
364
+ unregisterNamedDispatcher (this .defaultServletName );
365
+ this .defaultServletName = defaultServletName ;
366
+ registerNamedDispatcher (this .defaultServletName , new MockRequestDispatcher (this .defaultServletName ));
299
367
}
300
368
301
369
public Servlet getServlet (String name ) {
@@ -410,7 +478,7 @@ public Set<String> getDeclaredRoles() {
410
478
411
479
412
480
/**
413
- * Inner factory class used to just introduce a Java Activation Framework
481
+ * Inner factory class used to introduce a Java Activation Framework
414
482
* dependency when actually asked to resolve a MIME type.
415
483
*/
416
484
private static class MimeTypeResolver {
0 commit comments