use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver in project spring-framework by spring-projects.
the class AbstractTagTests method createPageContext.
protected MockPageContext createPageContext() {
MockServletContext sc = new MockServletContext();
SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
wac.setServletContext(sc);
wac.setNamespace("test");
wac.refresh();
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
if (inDispatcherServlet()) {
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
LocaleResolver lr = new AcceptHeaderLocaleResolver();
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
ThemeResolver tr = new FixedThemeResolver();
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
} else {
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
}
return new MockPageContext(sc, request, response);
}
use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver in project spring-framework by spring-projects.
the class ViewResolverTests method testXmlViewResolver.
@Test
public void testXmlViewResolver() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.registerSingleton("testBean", TestBean.class);
wac.setServletContext(new MockServletContext());
wac.refresh();
TestBean testBean = (TestBean) wac.getBean("testBean");
XmlViewResolver vr = new XmlViewResolver();
vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
vr.setApplicationContext(wac);
View view1 = vr.resolveViewName("example1", Locale.getDefault());
assertTrue("Correct view class", TestView.class.equals(view1.getClass()));
assertTrue("Correct URL", "/example1.jsp".equals(((InternalResourceView) view1).getUrl()));
View view2 = vr.resolveViewName("example2", Locale.getDefault());
assertTrue("Correct view class", JstlView.class.equals(view2.getClass()));
assertTrue("Correct URL", "/example2new.jsp".equals(((InternalResourceView) view2).getUrl()));
ServletContext sc = new MockServletContext();
Map model = new HashMap();
TestBean tb = new TestBean();
model.put("tb", tb);
HttpServletRequest request = new MockHttpServletRequest(sc);
HttpServletResponse response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
view1.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
assertTrue("Correct test2 attribute", testBean.equals(request.getAttribute("test2")));
request = new MockHttpServletRequest(sc);
response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
view2.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
assertTrue("Correct test2 attribute", "testvalue2".equals(request.getAttribute("test2")));
}
use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver in project spring-framework by spring-projects.
the class FreeMarkerMacroTests method setUp.
@Before
public void setUp() throws Exception {
wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
// final Template expectedTemplate = new Template();
fc = new FreeMarkerConfigurer();
fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
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();
}
use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver in project spring-framework by spring-projects.
the class FreeMarkerViewTests method keepExistingContentType.
@Test
public void keepExistingContentType() throws Exception {
FreeMarkerView fv = new FreeMarkerView();
WebApplicationContext wac = mock(WebApplicationContext.class);
MockServletContext sc = new MockServletContext();
Map<String, FreeMarkerConfig> configs = new HashMap<>();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
configs.put("configurer", configurer);
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
given(wac.getServletContext()).willReturn(sc);
fv.setUrl("templateName");
fv.setApplicationContext(wac);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.US);
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
HttpServletResponse response = new MockHttpServletResponse();
response.setContentType("myContentType");
Map<String, Object> model = new HashMap<>();
model.put("myattr", "myvalue");
fv.render(model, request, response);
assertEquals("myContentType", response.getContentType());
}
use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver in project spring-framework by spring-projects.
the class FreeMarkerViewTests method validTemplateName.
@Test
public void validTemplateName() throws Exception {
FreeMarkerView fv = new FreeMarkerView();
WebApplicationContext wac = mock(WebApplicationContext.class);
MockServletContext sc = new MockServletContext();
Map<String, FreeMarkerConfig> configs = new HashMap<>();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
configs.put("configurer", configurer);
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
given(wac.getServletContext()).willReturn(sc);
fv.setUrl("templateName");
fv.setApplicationContext(wac);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.US);
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
HttpServletResponse response = new MockHttpServletResponse();
Map<String, Object> model = new HashMap<>();
model.put("myattr", "myvalue");
fv.render(model, request, response);
assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
Aggregations