Fix formatting files

This commit is contained in:
alex-semenyuk 2015-07-22 22:22:39 +02:00
parent 877b8d2987
commit 23bf5a3042
5 changed files with 123 additions and 137 deletions

View File

@ -13,41 +13,40 @@ import org.springframework.web.servlet.view.RedirectView;
@RequestMapping("/") @RequestMapping("/")
public class RedirectController { public class RedirectController {
@RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET)
public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) {
model.addAttribute("attribute", "redirectWithXMLConfig"); model.addAttribute("attribute", "redirectWithXMLConfig");
return new ModelAndView("RedirectedUrl", model); return new ModelAndView("RedirectedUrl", model);
} }
@RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET)
public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) {
model.addAttribute("attribute", "redirectWithRedirectPrefix"); model.addAttribute("attribute", "redirectWithRedirectPrefix");
return new ModelAndView("redirect:/redirectedUrl", model); return new ModelAndView("redirect:/redirectedUrl", model);
} }
@RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET)
public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes");
redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes");
return new RedirectView("redirectedUrl"); return new RedirectView("redirectedUrl");
} }
@RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET)
public RedirectView redirectWithUsingRedirectView(final ModelMap model) { public RedirectView redirectWithUsingRedirectView(final ModelMap model) {
model.addAttribute("attribute", "redirectWithRedirectView"); model.addAttribute("attribute", "redirectWithRedirectView");
return new RedirectView("redirectedUrl"); return new RedirectView("redirectedUrl");
} }
@RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET) @RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET)
public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) { public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) {
model.addAttribute("attribute", "redirectWithForwardPrefix"); model.addAttribute("attribute", "redirectWithForwardPrefix");
return new ModelAndView("forward:/redirectedUrl", model); return new ModelAndView("forward:/redirectedUrl", model);
} }
@RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET)
public ModelAndView redirection(final ModelMap model, public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) {
@ModelAttribute("flashAttribute") final Object flashAttribute) { model.addAttribute("redirectionAttribute", flashAttribute);
model.addAttribute("redirectionAttribute", flashAttribute); return new ModelAndView("redirection", model);
return new ModelAndView("redirection", model); }
}
} }

View File

@ -1,31 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
>
<context:component-scan base-package="org.baeldung.web" /> <context:component-scan base-package="org.baeldung.web" />
<mvc:annotation-driven /> <mvc:annotation-driven />
<bean <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
class="org.springframework.web.servlet.view.InternalResourceViewResolver" /> <bean class="org.springframework.web.servlet.view.XmlViewResolver">
<bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location">
<property name="location"> <value>/WEB-INF/spring-views.xml</value>
<value>/WEB-INF/spring-views.xml</value> </property>
</property> <property name="order" value="0" />
<property name="order" value="0" /> </bean>
</bean>
<bean id="multipartResolver" <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- max upload size in bytes -->
<!-- max upload size in bytes --> <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<!-- max size of file in memory (in bytes) --> <!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB --> <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
</bean> </bean>
</beans>
</beans>

View File

@ -1,10 +1,10 @@
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> >
<bean id="RedirectedUrl" class="org.springframework.web.servlet.view.RedirectView"> <bean id="RedirectedUrl" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="redirectedUrl" /> <property name="url" value="redirectedUrl" />
</bean> </bean>
</beans> </beans>

View File

@ -1,42 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"
id="WebApp_ID" version="3.0"> >
<display-name>Spring MVC Application</display-name> <display-name>Spring MVC Application</display-name>
<!-- Spring root --> <!-- Spring root -->
<context-param> <context-param>
<param-name>contextClass</param-name> <param-name>contextClass</param-name>
<param-value> <param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value> </param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>contextConfigLocation</param-name> <param-name>contextConfigLocation</param-name>
<param-value>org.baeldung.config</param-value> <param-value>org.baeldung.config</param-value>
</context-param> </context-param>
<listener> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </listener>
<!-- Spring child --> <!-- Spring child -->
<servlet> <servlet>
<servlet-name>api</servlet-name> <servlet-name>api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup> <load-on-startup>1</load-on-startup>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>api</servlet-name> <servlet-name>api</servlet-name>
<url-pattern>/</url-pattern> <url-pattern>/</url-pattern>
</servlet-mapping> </servlet-mapping>
<welcome-file-list> <welcome-file-list>
<welcome-file /> <welcome-file />
</welcome-file-list> </welcome-file-list>
</web-app> </web-app>

View File

@ -26,56 +26,42 @@ import org.springframework.web.context.WebApplicationContext;
@WebAppConfiguration @WebAppConfiguration
public class RedirectControllerTest { public class RedirectControllerTest {
private MockMvc mockMvc; private MockMvc mockMvc;
@Autowired @Autowired
protected WebApplicationContext wac; protected WebApplicationContext wac;
@Before @Before
public void setup() { public void setup() {
mockMvc = webAppContextSetup(wac).build(); mockMvc = webAppContextSetup(wac).build();
} }
@Test @Test
public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception {
mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()) mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig")))
.andExpect(view().name("RedirectedUrl")) .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig"));
.andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) }
.andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig"));
}
@Test @Test
public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception {
mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()) mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix")))
.andExpect(view().name("redirect:/redirectedUrl")) .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix"));
.andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) }
.andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix"));
}
@Test @Test
public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception {
throws Exception { mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes")))
mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()) .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes"));
.andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) }
.andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes")))
.andExpect(model().attribute("flashAttribute", is(nullValue())))
.andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes"));
}
@Test @Test
public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception {
throws Exception { mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView"));
mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()) }
.andExpect(model().attribute("attribute", is("redirectWithRedirectView")))
.andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView"));
}
@Test @Test
public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception {
mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()) mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl"));
.andExpect(view().name("forward:/redirectedUrl")) }
.andExpect(model().attribute("attribute", is("redirectWithForwardPrefix")))
.andExpect(forwardedUrl("/redirectedUrl"));
}
} }