Java Code Examples for org.springframework.web.context.support.StaticWebApplicationContext#refresh()

The following examples show how to use org.springframework.web.context.support.StaticWebApplicationContext#refresh() . 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: 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 2
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 3
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 4
Source File: ViewResolverTests.java    From spring-analysis-note 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 5
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 6
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyAndCustomContextAttribute() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

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

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

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	proxyConfig.addInitParameter("contextAttribute", "CUSTOM_ATTR");
	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 7
Source File: VelocityViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testVelocityViewResolver() 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();

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

	View view = vr.resolveViewName("test", Locale.CANADA);
	assertEquals("Correct view class", VelocityView.class, view.getClass());
	assertEquals("Correct URL", "prefix_test_suffix", ((VelocityView) view).getUrl());

	view = vr.resolveViewName("non-existing", Locale.CANADA);
	assertNull(view);

	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: HandlerMappingIntrospectorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void getMatchableWhereHandlerMappingDoesNotImplementMatchableInterface() throws Exception {
	StaticWebApplicationContext cxt = new StaticWebApplicationContext();
	cxt.registerSingleton("hm1", TestHandlerMapping.class);
	cxt.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest();
	getIntrospector(cxt).getMatchableHandlerMapping(request);
}
 
Example 9
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

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

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
 
Example 10
Source File: FreeMarkerViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void freeMarkerViewResolver() throws Exception {
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());

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

	FreeMarkerViewResolver vr = new FreeMarkerViewResolver();
	vr.setPrefix("prefix_");
	vr.setSuffix("_suffix");
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("test", Locale.CANADA);
	assertEquals("Correct view class", FreeMarkerView.class, view.getClass());
	assertEquals("Correct URL", "prefix_test_suffix", ((FreeMarkerView) view).getUrl());

	view = vr.resolveViewName("non-existing", Locale.CANADA);
	assertNull(view);

	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 11
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void staticWebApplicationContext() {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();

	assertHasStandardServletEnvironment(ctx);

	registerEnvironmentBeanDefinition(ctx);

	ctx.setEnvironment(prodWebEnv);
	ctx.refresh();

	assertHasEnvironment(ctx, prodWebEnv);
	assertEnvironmentBeanRegistered(ctx);
	assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}
 
Example 12
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void controllerAdviceWithNestedException() {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

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

	IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message"));
	assertNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));
}
 
Example 13
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 14
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetFilterLifecycle() 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);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	proxyConfig.addInitParameter("targetFilterLifecycle", "true");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);
	assertEquals(proxyConfig, targetFilter.filterConfig);

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

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

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 15
Source File: RequestAndSessionScopedBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void testPutBeanInSession() throws Exception {
	String targetBeanName = "target";
	HttpServletRequest request = new MockHttpServletRequest();
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setScope(WebApplicationContext.SCOPE_SESSION);
	bd.getPropertyValues().add("name", "abc");
	wac.registerBeanDefinition(targetBeanName, bd);
	wac.refresh();

	TestBean target = (TestBean) wac.getBean(targetBeanName);
	assertEquals("abc", target.getName());
	assertSame(target, request.getSession().getAttribute(targetBeanName));

	RequestContextHolder.setRequestAttributes(null);
	try {
		wac.getBean(targetBeanName);
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
	}


}
 
Example 16
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveViewNameRedirectView() throws Exception {
	request.addHeader("Accept", "application/json");
	request.setRequestURI("/test");

	StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
	webAppContext.setServletContext(new MockServletContext());
	webAppContext.refresh();

	UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
	urlViewResolver.setApplicationContext(webAppContext);
	ViewResolver xmlViewResolver = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));

	View xmlView = mock(View.class, "application_xml");
	View jsonView = mock(View.class, "application_json");
	viewResolver.setDefaultViews(Arrays.asList(jsonView));

	viewResolver.afterPropertiesSet();

	String viewName = "redirect:anotherTest";
	Locale locale = Locale.ENGLISH;

	given(xmlViewResolver.resolveViewName(viewName, locale)).willReturn(xmlView);
	given(jsonView.getContentType()).willReturn("application/json");

	View actualView = viewResolver.resolveViewName(viewName, locale);
	assertEquals("Invalid view", RedirectView.class, actualView.getClass());
}
 
Example 17
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
	// create a parent context that includes a mapping
	StaticWebApplicationContext parent = new StaticWebApplicationContext();
	parent.setServletContext(getServletContext());
	parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));

	parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
	parent.refresh();

	DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
	// will have parent
	complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
	complexDispatcherServlet.setNamespace("test");

	ServletConfig config = new MockServletConfig(getServletContext(), "complex");
	config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
	complexDispatcherServlet.init(config);

	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);

	assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(),
			response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
 
Example 18
Source File: RequestMappingHandlerAdapterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void modelAttributeAdviceInParentContext() throws Exception {
	StaticWebApplicationContext parent = new StaticWebApplicationContext();
	parent.registerSingleton("maa", ModelAttributeAdvice.class);
	parent.refresh();
	this.webAppContext.setParent(parent);
	this.webAppContext.refresh();

	HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
	this.handlerAdapter.afterPropertiesSet();
	ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);

	assertEquals("lAttr1", mav.getModel().get("attr1"));
	assertEquals("gAttr2", mav.getModel().get("attr2"));
}
 
Example 19
Source File: OpenSessionInViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenSessionInViewFilterAsyncScenario() throws Exception {
	final SessionFactory sf = mock(SessionFactory.class);
	Session session = mock(Session.class);

	// Initial request during which concurrent handling starts..

	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sf);

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");

	final AtomicInteger count = new AtomicInteger(0);

	final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
	filter.init(filterConfig);

	final FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
			assertTrue(TransactionSynchronizationManager.hasResource(sf));
			count.incrementAndGet();
		}
	};

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	filter.doFilter(this.request, this.response, filterChain);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	assertEquals(1, count.get());
	verify(session, never()).close();

	// Async dispatch after concurrent handling produces result ...

	this.request.setAsyncStarted(false);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	filter.doFilter(this.request, this.response, filterChain);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	assertEquals(2, count.get());

	verify(session).setFlushMode(FlushMode.MANUAL);
	verify(session).close();

	wac.close();
}
 
Example 20
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithAttributes() throws Exception {
	MockServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map map = new HashMap();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
	Map attributes = ((InternalResourceView) view).getStaticAttributes();
	assertEquals("value1", attributes.get("key1"));
	assertEquals(new Integer(2), attributes.get("key2"));

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
	attributes = ((InternalResourceView) view).getStaticAttributes();
	assertEquals("value1", attributes.get("key1"));
	assertEquals(new Integer(2), attributes.get("key2"));

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
	assertEquals("value1", request.getAttribute("key1"));
	assertEquals(new Integer(2), request.getAttribute("key2"));
}