/** * Create a new {@code MockServletContext} using the supplied resource base * path and resource loader. * <p>Registers a {@link MockRequestDispatcher} for the Servlet named * {@literal 'default'}. * @param resourceBasePath the root directory of the WAR (should not end with a slash) * @param resourceLoader the ResourceLoader to use (or null for the default) * @see #registerNamedDispatcher */ public MockServletContext(String resourceBasePath, @Nullable ResourceLoader resourceLoader) { this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); this.resourceBasePath = resourceBasePath; // Use JVM temp dir as ServletContext temp dir. String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY); if (tempDir != null) { this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File(tempDir)); } registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }
/** * Set the name of the <em>default</em> {@code Servlet}. * <p>Also {@link #unregisterNamedDispatcher unregisters} the current default * {@link RequestDispatcher} and {@link #registerNamedDispatcher replaces} * it with a {@link MockRequestDispatcher} for the provided * {@code defaultServletName}. * @param defaultServletName the name of the <em>default</em> {@code Servlet}; * never {@code null} or empty * @see #getDefaultServletName */ public void setDefaultServletName(String defaultServletName) { Assert.hasText(defaultServletName, "defaultServletName must not be null or empty"); unregisterNamedDispatcher(this.defaultServletName); this.defaultServletName = defaultServletName; registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }
@Test public void registerAndUnregisterNamedDispatcher() throws Exception { final String name = "test-servlet"; final String url = "/test"; assertNull(sc.getNamedDispatcher(name)); sc.registerNamedDispatcher(name, new MockRequestDispatcher(url)); RequestDispatcher namedDispatcher = sc.getNamedDispatcher(name); assertNotNull(namedDispatcher); MockHttpServletResponse response = new MockHttpServletResponse(); namedDispatcher.forward(new MockHttpServletRequest(sc), response); assertEquals(url, response.getForwardedUrl()); sc.unregisterNamedDispatcher(name); assertNull(sc.getNamedDispatcher(name)); }
/** * Create a new {@code MockServletContext} using the supplied resource base * path and resource loader. * <p>Registers a {@link MockRequestDispatcher} for the Servlet named * {@literal 'default'}. * @param resourceBasePath the root directory of the WAR (should not end with a slash) * @param resourceLoader the ResourceLoader to use (or null for the default) * @see #registerNamedDispatcher */ public MockServletContext(String resourceBasePath, @Nullable ResourceLoader resourceLoader) { this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); this.resourceBasePath = resourceBasePath; // Use JVM temp dir as ServletContext temp dir. String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY); if (tempDir != null) { this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File(tempDir)); } registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }
/** * Create a new {@code MockServletContext} using the supplied resource base * path and resource loader. * <p>Registers a {@link MockRequestDispatcher} for the Servlet named * {@literal 'default'}. * @param resourceBasePath the root directory of the WAR (should not end with a slash) * @param resourceLoader the ResourceLoader to use (or null for the default) * @see #registerNamedDispatcher */ public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) { this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : ""); // Use JVM temp dir as ServletContext temp dir. String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY); if (tempDir != null) { this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File(tempDir)); } registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }
/** * Set the name of the <em>default</em> {@code Servlet}. * <p>Also {@link #unregisterNamedDispatcher unregisters} the current default * {@link RequestDispatcher} and {@link #registerNamedDispatcher replaces} * it with a {@link MockRequestDispatcher} for the provided * {@code defaultServletName}. * @param defaultServletName the name of the <em>default</em> {@code Servlet}; * never {@code null} or empty * @see #getDefaultServletName */ public void setDefaultServletName(String defaultServletName) { Assert.hasText(defaultServletName, "defaultServletName must not be null or empty"); unregisterNamedDispatcher(this.defaultServletName); this.defaultServletName = defaultServletName; registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }
/** * Set the name of the <em>default</em> {@code Servlet}. * <p>Also {@link #unregisterNamedDispatcher unregisters} the current default * {@link RequestDispatcher} and {@link #registerNamedDispatcher replaces} * it with a {@link MockRequestDispatcher} for the provided * {@code defaultServletName}. * @param defaultServletName the name of the <em>default</em> {@code Servlet}; * never {@code null} or empty * @see #getDefaultServletName */ public void setDefaultServletName(String defaultServletName) { Assert.hasText(defaultServletName, "defaultServletName must not be null or empty"); unregisterNamedDispatcher(this.defaultServletName); this.defaultServletName = defaultServletName; registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }