org.springframework.web.context.support.StaticWebApplicationContext Java Examples

The following examples show how to use org.springframework.web.context.support.StaticWebApplicationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VelocityMacroTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());

	final Template expectedTemplate = new Template();
	VelocityConfig vc = new VelocityConfig() {
		@Override
		public VelocityEngine getVelocityEngine() {
			return new TestVelocityEngine(TEMPLATE_FILE, expectedTemplate);
		}
	};
	wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #2
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #3
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCacheRemoval() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	View cached = vr.resolveViewName("example1", Locale.getDefault());
	if (view != cached) {
		fail("Caching doesn't work");
	}

	vr.removeFromCache("example1", Locale.getDefault());
	cached = vr.resolveViewName("example1", Locale.getDefault());
	if (view == cached) {
		// the chance of having the same reference (hashCode) twice if negligible).
		fail("View wasn't removed from cache");
	}
}
 
Example #4
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testXmlViewResolverDefaultLocation() {
	StaticWebApplicationContext wac = new StaticWebApplicationContext() {
		@Override
		protected Resource getResourceByPath(String path) {
			assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
			return super.getResourceByPath(path);
		}
	};
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	XmlViewResolver vr = new XmlViewResolver();
	try {
		vr.setApplicationContext(wac);
		vr.afterPropertiesSet();
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
	}
}
 
Example #5
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBeanNameViewResolver() throws ServletException {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	MutablePropertyValues pvs1 = new MutablePropertyValues();
	pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
	wac.registerSingleton("example1", InternalResourceView.class, pvs1);
	MutablePropertyValues pvs2 = new MutablePropertyValues();
	pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
	wac.registerSingleton("example2", JstlView.class, pvs2);
	BeanNameViewResolver vr = new BeanNameViewResolver();
	vr.setApplicationContext(wac);
	wac.refresh();

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", InternalResourceView.class, view.getClass());
	assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
}
 
Example #6
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void noCleanupAfterInclude() throws ServletException, IOException {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setAttribute("test1", "value1");
	request.setAttribute("test2", "value2");
	WebApplicationContext wac = new StaticWebApplicationContext();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	TestBean command = new TestBean();
	request.setAttribute("command", command);

	request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
	simpleDispatcherServlet.setCleanupAfterInclude(false);
	simpleDispatcherServlet.service(request, response);

	assertEquals("value1", request.getAttribute("test1"));
	assertEquals("value2", request.getAttribute("test2"));
	assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
}
 
Example #7
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	vr.setPrefix("/WEB-INF/");
	vr.setSuffix(".jsp");
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "/WEB-INF/example1.jsp", ((InternalResourceView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "/WEB-INF/example2.jsp", ((InternalResourceView) view).getUrl());

	view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
	assertEquals("Correct view class", RedirectView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());

	view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
	assertEquals("Correct view class", InternalResourceView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
 
Example #8
Source File: VelocityRenderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());

	final Template expectedTemplate = new Template();
	VelocityConfig vc = new VelocityConfig() {
		@Override
		public VelocityEngine getVelocityEngine() {
			return new TestVelocityEngine("test.vm", expectedTemplate);
		}
	};
	wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #9
Source File: FreeMarkerMacroTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	ServletContext sc = new MockServletContext();
	wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);

	// final Template expectedTemplate = new Template();
	fc = new FreeMarkerConfigurer();
	fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
	fc.setServletContext(sc);
	fc.afterPropertiesSet();

	wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #10
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFilterName() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc, "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #11
Source File: ResponseEntityExceptionHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void controllerAdvice() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
	resolver.setApplicationContext(ctx);
	resolver.afterPropertiesSet();

	ServletRequestBindingException ex = new ServletRequestBindingException("message");
	assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));

	assertEquals(400, this.servletResponse.getStatus());
	assertEquals("error content", this.servletResponse.getContentAsString());
	assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
 
Example #12
Source File: ResponseEntityExceptionHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	DispatcherServlet servlet = new DispatcherServlet(ctx);
	servlet.init(new MockServletConfig());
	try {
		servlet.service(this.servletRequest, this.servletResponse);
	}
	catch (ServletException ex) {
		assertTrue(ex.getCause() instanceof IllegalStateException);
		assertTrue(ex.getCause().getCause() instanceof ServletRequestBindingException);
	}
}
 
Example #13
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #14
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getHandlerMappedInterceptors() throws Exception {
	String path = "/foo";
	HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
	MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);

	TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
	mapping.registerHandler(new TestController());
	mapping.setInterceptors(new Object[] { mappedInterceptor });
	mapping.setApplicationContext(new StaticWebApplicationContext());

	HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertSame(interceptor, chain.getInterceptors()[0]);

	chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
	assertNull(chain);
}
 
Example #15
Source File: RequestMappingInfoHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void mappedInterceptors() throws Exception {
	String path = "/foo";
	HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
	MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);

	TestRequestMappingInfoHandlerMapping hm = new TestRequestMappingInfoHandlerMapping();
	hm.registerHandler(new TestController());
	hm.setInterceptors(new Object[] { mappedInterceptor });
	hm.setApplicationContext(new StaticWebApplicationContext());

	HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path));
	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertSame(interceptor, chain.getInterceptors()[0]);

	chain = hm.getHandler(new MockHttpServletRequest("GET", "/invalid"));
	assertNull(chain);
}
 
Example #16
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testXmlViewResolverDefaultLocation() {
	StaticWebApplicationContext wac = new StaticWebApplicationContext() {
		@Override
		protected Resource getResourceByPath(String path) {
			assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
			return super.getResourceByPath(path);
		}
	};
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	XmlViewResolver vr = new XmlViewResolver();
	try {
		vr.setApplicationContext(wac);
		vr.afterPropertiesSet();
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
	}
}
 
Example #17
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void controllerAdvice() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
	resolver.setApplicationContext(ctx);
	resolver.afterPropertiesSet();

	ServletRequestBindingException ex = new ServletRequestBindingException("message");
	assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));

	assertEquals(400, this.servletResponse.getStatus());
	assertEquals("error content", this.servletResponse.getContentAsString());
	assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
 
Example #18
Source File: VelocityViewResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testVelocityViewResolverWithToolbox() throws Exception {
	VelocityConfig vc = new VelocityConfig() {
		@Override
		public VelocityEngine getVelocityEngine() {
			return new TestVelocityEngine("prefix_test_suffix", new Template());
		}
	};

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.getBeanFactory().registerSingleton("configurer", vc);
	wac.refresh();

	String toolbox = "org/springframework/web/servlet/view/velocity/toolbox.xml";

	VelocityViewResolver vr = new VelocityViewResolver();
	vr.setPrefix("prefix_");
	vr.setSuffix("_suffix");
	vr.setToolboxConfigLocation(toolbox);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("test", Locale.CANADA);
	assertEquals("Correct view class", VelocityToolboxView.class, view.getClass());
	assertEquals("Correct URL", "prefix_test_suffix", ((VelocityView) view).getUrl());
	assertEquals("Correct toolbox", toolbox, ((VelocityToolboxView) view).getToolboxConfigLocation());
}
 
Example #19
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setAttribute("test1", "value1");
	request.setAttribute("test2", "value2");
	WebApplicationContext wac = new StaticWebApplicationContext();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
	simpleDispatcherServlet.service(request, response);

	assertEquals("value1", request.getAttribute("test1"));
	assertEquals("value2", request.getAttribute("test2"));
	assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
	assertNull(request.getAttribute("command"));
}
 
Example #20
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #21
Source File: HandlerMethodMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getCorsConfigWithBeanNameHandler() throws Exception {

	String key = "foo";
	String beanName = "handler1";

	StaticWebApplicationContext context = new StaticWebApplicationContext();
	context.registerSingleton(beanName, MyHandler.class);

	this.mapping.setApplicationContext(context);
	this.mapping.registerMapping(key, beanName, this.method1);
	HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));

	CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod);
	assertNotNull(config);
	assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0));
}
 
Example #22
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setAttribute("test1", "value1");
	request.setAttribute("test2", "value2");
	WebApplicationContext wac = new StaticWebApplicationContext();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
	simpleDispatcherServlet.service(request, response);

	assertEquals("value1", request.getAttribute("test1"));
	assertEquals("value2", request.getAttribute("test2"));
	assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
	assertNull(request.getAttribute("command"));
}
 
Example #23
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testCacheRemoval() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	View cached = vr.resolveViewName("example1", Locale.getDefault());
	if (view != cached) {
		fail("Caching doesn't work");
	}

	vr.removeFromCache("example1", Locale.getDefault());
	cached = vr.resolveViewName("example1", Locale.getDefault());
	if (view == cached) {
		// the chance of having the same reference (hashCode) twice if negligible).
		fail("View wasn't removed from cache");
	}
}
 
Example #24
Source File: HandlerMethodMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void getCorsConfigWithBeanNameHandler() throws Exception {

	String key = "foo";
	String beanName = "handler1";

	StaticWebApplicationContext context = new StaticWebApplicationContext();
	context.registerSingleton(beanName, MyHandler.class);

	this.mapping.setApplicationContext(context);
	this.mapping.registerMapping(key, beanName, this.method1);
	HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));

	CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod);
	assertNotNull(config);
	assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0));
}
 
Example #25
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #26
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyNotInjectedWithRootPreferred() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
	sc.setAttribute("another", wac);

	StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
	wacToUse.setServletContext(sc);
	String beanName = "targetFilter";
	wacToUse.registerSingleton(beanName, MockFilter.class);
	wacToUse.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wacToUse);

	MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
	filterProxy.setServletContext(sc);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #27
Source File: AbstractHtmlElementTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected MockPageContext createAndPopulatePageContext() throws JspException {
	MockPageContext pageContext = createPageContext();
	MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
	((StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request))
			.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
	extendRequest(request);
	extendPageContext(pageContext);
	RequestContext requestContext = new JspAwareRequestContext(pageContext);
	pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
	return pageContext;
}
 
Example #28
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext()
		throws ServletException, IOException {

	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	// wac.refresh();
	// note that the context is not set as the ROOT attribute in the ServletContext!

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", wac);
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #29
Source File: CommonsMultipartResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void withServletContextAndFilter() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
	wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
	wac.refresh();
	wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
	assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));

	MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
	filterConfig.addInitParameter("class", "notWritable");
	filterConfig.addInitParameter("unknownParam", "someValue");
	final MultipartFilter filter = new MultipartFilter();
	filter.init(filterConfig);

	final List<MultipartFile> files = new ArrayList<>();
	final FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
			MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
			files.addAll(request.getFileMap().values());
		}
	};

	FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);

	MockHttpServletRequest originalRequest = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	originalRequest.setMethod("POST");
	originalRequest.setContentType("multipart/form-data");
	originalRequest.addHeader("Content-type", "multipart/form-data");
	filter.doFilter(originalRequest, response, filterChain2);

	CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
	CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
	assertTrue(((MockFileItem) file1.getFileItem()).deleted);
	assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
 
Example #30
Source File: AbstractHtmlElementTagTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected MockPageContext createAndPopulatePageContext() throws JspException {
	MockPageContext pageContext = createPageContext();
	MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
	StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request);
	wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
	extendRequest(request);
	extendPageContext(pageContext);
	RequestContext requestContext = new JspAwareRequestContext(pageContext);
	pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
	return pageContext;
}