use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class RedirectViewTests method updateTargetUrl.
@Test
public void updateTargetUrl() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
wac.setServletContext(new MockServletContext());
wac.refresh();
RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
RedirectView rv = new RedirectView();
// Init RedirectView with WebAppCxt
rv.setApplicationContext(wac);
rv.setUrl("/path");
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
rv.render(new ModelMap(), request, response);
verify(mockProcessor).processUrl(request, "/path");
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ThemeResolverTests method internalTest.
private void internalTest(ThemeResolver themeResolver, boolean shouldSet, String defaultName) {
// create mocks
MockServletContext context = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(context);
MockHttpServletResponse response = new MockHttpServletResponse();
// check original theme
String themeName = themeResolver.resolveThemeName(request);
assertThat(defaultName).isEqualTo(themeName);
// set new theme name
try {
themeResolver.setThemeName(request, response, TEST_THEME_NAME);
assertThat(shouldSet).as("able to set theme name").isTrue();
// check new theme namelocale
themeName = themeResolver.resolveThemeName(request);
assertThat(themeName).isEqualTo(TEST_THEME_NAME);
themeResolver.setThemeName(request, response, null);
themeName = themeResolver.resolveThemeName(request);
assertThat(defaultName).isEqualTo(themeName);
} catch (UnsupportedOperationException ex) {
assertThat(shouldSet).as("able to set theme name").isFalse();
}
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class BaseViewTests method renderWithStaticAttributesNoCollision.
/**
* Test attribute passing, NOT CSV parsing.
*/
@Test
public void renderWithStaticAttributesNoCollision() throws Exception {
WebApplicationContext wac = mock(WebApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
TestView tv = new TestView(wac);
tv.setApplicationContext(wac);
Properties p = new Properties();
p.setProperty("foo", "bar");
p.setProperty("something", "else");
tv.setAttributes(p);
Map<String, Object> model = new HashMap<>();
model.put("one", new HashMap<>());
model.put("two", new Object());
tv.render(model, request, response);
checkContainsAll(model, tv.model);
checkContainsAll(p, tv.model);
assertThat(tv.initialized).isTrue();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class BaseViewTests method dynamicModelOverridesPathVariables.
@Test
public void dynamicModelOverridesPathVariables() throws Exception {
WebApplicationContext wac = mock(WebApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
TestView tv = new TestView(wac);
tv.setApplicationContext(wac);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
Map<String, Object> pathVars = new HashMap<>();
pathVars.put("one", "bar");
pathVars.put("something", "else");
request.setAttribute(View.PATH_VARIABLES, pathVars);
Map<String, Object> model = new HashMap<>();
model.put("one", new HashMap<>());
model.put("two", new Object());
tv.render(model, request, response);
checkContainsAll(model, tv.model);
assertThat(tv.model.size()).isEqualTo(3);
assertThat(tv.model.get("something")).isEqualTo("else");
assertThat(tv.initialized).isTrue();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class BaseViewTests method pathVarsOverrideStaticAttributes.
@Test
public void pathVarsOverrideStaticAttributes() throws Exception {
WebApplicationContext wac = mock(WebApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
TestView tv = new TestView(wac);
tv.setApplicationContext(wac);
Properties p = new Properties();
p.setProperty("one", "bar");
p.setProperty("something", "else");
tv.setAttributes(p);
Map<String, Object> pathVars = new HashMap<>();
pathVars.put("one", new HashMap<>());
pathVars.put("two", new Object());
request.setAttribute(View.PATH_VARIABLES, pathVars);
tv.render(new HashMap<>(), request, response);
checkContainsAll(pathVars, tv.model);
assertThat(tv.model.size()).isEqualTo(3);
assertThat(tv.model.get("something")).isEqualTo("else");
assertThat(tv.initialized).isTrue();
}
Aggregations