From f72311412835ae41510520bb43d3c4928fc946c5 Mon Sep 17 00:00:00 2001 From: Yasser Afifi Date: Wed, 4 Jan 2017 13:10:02 +0000 Subject: [PATCH 1/2] fixed handlerMapping code and added tests --- ...ontroller.java => BaeldungController.java} | 4 +- ...TwoController.java => TestController.java} | 4 +- ...nfiguration_BeanNameUrlHandlerMapping.java | 2 +- ...ppConfiguration_ConfiguringPriorities.java | 24 +++++------ ...Configuration_SimpleUrlHandlerMapping.java | 2 +- .../XML/BeanNameUrlHandlerMappingConfig.xml | 2 +- .../HandlerMappingConfiguringPriorities.xml | 8 ++-- .../XML/SimpleUrlHandlerMappingConfig.xml | 4 +- .../BeanNameMappingConfigTest.java | 42 +++++++++++++++++++ ...ControllerClassNameHandlerMappingTest.java | 41 ++++++++++++++++++ .../HandlerMappingPriorityConfigTest.java | 41 ++++++++++++++++++ .../SimpleUrlMappingConfigTest.java | 41 ++++++++++++++++++ 12 files changed, 190 insertions(+), 25 deletions(-) rename spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/{WelcomeBaeldungController.java => BaeldungController.java} (79%) rename spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/{WelcomeTwoController.java => TestController.java} (79%) create mode 100644 spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/BeanNameMappingConfigTest.java create mode 100644 spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/ControllerClassNameHandlerMappingTest.java create mode 100644 spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java create mode 100644 spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/SimpleUrlMappingConfigTest.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeBaeldungController.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/BaeldungController.java similarity index 79% rename from spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeBaeldungController.java rename to spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/BaeldungController.java index c8769c5607..9cc55d3554 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeBaeldungController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/BaeldungController.java @@ -6,12 +6,12 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; -public class WelcomeBaeldungController extends AbstractController { +public class BaeldungController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("Inside Welcome Baeldung Controller"); - ModelAndView model = new ModelAndView("welcome"); + ModelAndView model = new ModelAndView("baeldung"); return model; } diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeTwoController.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/TestController.java similarity index 79% rename from spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeTwoController.java rename to spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/TestController.java index 2a63e57fe4..f0d69de8b0 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeTwoController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/TestController.java @@ -6,12 +6,12 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; -public class WelcomeTwoController extends AbstractController { +public class TestController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("Inside Welcome two Controller"); - ModelAndView model = new ModelAndView("welcome"); + ModelAndView model = new ModelAndView("test"); return model; } diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_BeanNameUrlHandlerMapping.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_BeanNameUrlHandlerMapping.java index 9078444c51..5ed738a8c4 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_BeanNameUrlHandlerMapping.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_BeanNameUrlHandlerMapping.java @@ -25,7 +25,7 @@ public class WebAppConfiguration_BeanNameUrlHandlerMapping { return bean; } - @Bean("/welcome") + @Bean("/beanNameUrl") public WelcomeController welcome() { WelcomeController welcome = new WelcomeController(); return welcome; diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ConfiguringPriorities.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ConfiguringPriorities.java index ba9968caca..ba3607faf3 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ConfiguringPriorities.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ConfiguringPriorities.java @@ -9,9 +9,9 @@ import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; -import com.baeldung.controller.WelcomeBaeldungController; +import com.baeldung.controller.BaeldungController; import com.baeldung.controller.WelcomeController; -import com.baeldung.controller.WelcomeTwoController; +import com.baeldung.controller.TestController; @Configuration public class WebAppConfiguration_ConfiguringPriorities { @@ -24,32 +24,32 @@ public class WebAppConfiguration_ConfiguringPriorities { } @Bean("/welcome") - public WelcomeBaeldungController welcomeBaeldungController() { - WelcomeBaeldungController welcomeBaeldungController = new WelcomeBaeldungController(); - return welcomeBaeldungController; + public BaeldungController welcomeBaeldungController() { + BaeldungController baeldungController = new BaeldungController(); + return baeldungController; } @Bean public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); Map urlMap = new HashMap<>(); - urlMap.put("/welcome", welcome()); + urlMap.put("/welcome", test()); simpleUrlHandlerMapping.setUrlMap(urlMap); simpleUrlHandlerMapping.setOrder(0); return simpleUrlHandlerMapping; } @Bean - public WelcomeTwoController welcome() { - WelcomeTwoController bean = new WelcomeTwoController(); - return bean; + public TestController test() { + TestController test = new TestController(); + return test; } @Bean public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() { - ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping(); - controllerClassNameHandlerMapping.setOrder(1); - return controllerClassNameHandlerMapping; + ControllerClassNameHandlerMapping bean = new ControllerClassNameHandlerMapping(); + bean.setOrder(1); + return bean; } @Bean diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java index 48504ed76d..3b8534500d 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java @@ -27,7 +27,7 @@ public class WebAppConfiguration_SimpleUrlHandlerMapping { public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); Map urlMap = new HashMap<>(); - urlMap.put("/welcome", welcome()); + urlMap.put("/simpleUrlWelcome", welcome()); simpleUrlHandlerMapping.setUrlMap(urlMap); return simpleUrlHandlerMapping; } diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMappingConfig.xml b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMappingConfig.xml index c8b68b99aa..2da079540c 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMappingConfig.xml +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMappingConfig.xml @@ -23,7 +23,7 @@ class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> - + diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/HandlerMappingConfiguringPriorities.xml b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/HandlerMappingConfiguringPriorities.xml index 5666d25748..9d20db25ea 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/HandlerMappingConfiguringPriorities.xml +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/HandlerMappingConfiguringPriorities.xml @@ -17,19 +17,19 @@ class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> - + - /welcome=welcome - /*/welcome=welcome + /welcome=test + /*/welcome=test - + - /test=welcome - /*/test=welcome + /simpleUrlWelcome=welcome + /*/simpleUrlWelcome=welcome diff --git a/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/BeanNameMappingConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/BeanNameMappingConfigTest.java new file mode 100644 index 0000000000..9cc992bb3c --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/BeanNameMappingConfigTest.java @@ -0,0 +1,42 @@ +package come.baeldung.test; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.config.WebAppConfiguration_BeanNameUrlHandlerMapping; + + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebAppConfiguration_BeanNameUrlHandlerMapping.class) +public class BeanNameMappingConfigTest { + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); + } + + @Test + public void whenBeanNameMapping_thenMappedOK() throws Exception { + mockMvc.perform(get("/beanNameUrl")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print()); + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/ControllerClassNameHandlerMappingTest.java b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/ControllerClassNameHandlerMappingTest.java new file mode 100644 index 0000000000..24fd865c11 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/ControllerClassNameHandlerMappingTest.java @@ -0,0 +1,41 @@ +package come.baeldung.test; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.config.WebAppConfiguration_ControllerClassNameHandlerMapping; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebAppConfiguration_ControllerClassNameHandlerMapping.class) +public class ControllerClassNameHandlerMappingTest { + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); + } + + @Test + public void whenControllerClassNameMapping_thenMappedOK() throws Exception { + mockMvc.perform(get("/welcometest")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print()); + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java new file mode 100644 index 0000000000..33f2793089 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java @@ -0,0 +1,41 @@ +package come.baeldung.test; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.config.WebAppConfiguration_ConfiguringPriorities; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebAppConfiguration_ConfiguringPriorities.class) +public class HandlerMappingPriorityConfigTest { + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); + } + + @Test + public void whenBeanNameMapping_thenMappedOK() throws Exception { + mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print()); + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/SimpleUrlMappingConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/SimpleUrlMappingConfigTest.java new file mode 100644 index 0000000000..8c9405abfc --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/SimpleUrlMappingConfigTest.java @@ -0,0 +1,41 @@ +package come.baeldung.test; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.config.WebAppConfiguration_SimpleUrlHandlerMapping; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebAppConfiguration_SimpleUrlHandlerMapping.class) +public class SimpleUrlMappingConfigTest { + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); + } + + @Test + public void whenSimpleUrlMapping_thenMappedOK() throws Exception { + mockMvc.perform(get("/simpleUrlWelcome")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print()); + } +} \ No newline at end of file From 6d5feb75dcd2ec4de75b5191dd51d4206c4848e6 Mon Sep 17 00:00:00 2001 From: Yasser Afifi Date: Wed, 4 Jan 2017 13:40:02 +0000 Subject: [PATCH 2/2] fixed names --- .../HandlerMapping/HandlerMappingPriorityConfigTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java index 33f2793089..cfb51fd5cc 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/HandlerMapping/HandlerMappingPriorityConfigTest.java @@ -35,7 +35,7 @@ public class HandlerMappingPriorityConfigTest { } @Test - public void whenBeanNameMapping_thenMappedOK() throws Exception { + public void whenConfiguringPriorities_thenMappedOK() throws Exception { mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print()); } } \ No newline at end of file