diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java index 2350a4040e..05c6c91c63 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java @@ -2,7 +2,6 @@ import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; @@ -21,7 +20,6 @@ import java.util.Map; import web.org.springframework.web.bind.annotation.RequestMapping; import web.org.springframework.web.bind.annotation.RequestMethod; -import webmvc.org.springframework.web.servlet.ModelAndView; public class AnnotationHandlerMapping { diff --git a/mvc/src/test/java/duplicate/case3/TestController.java b/mvc/src/test/java/duplicate/case3/TestController.java new file mode 100644 index 0000000000..f65271beeb --- /dev/null +++ b/mvc/src/test/java/duplicate/case3/TestController.java @@ -0,0 +1,28 @@ +package duplicate.case3; + +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; + +@Controller +public class TestController { + + private static final Logger log = LoggerFactory.getLogger(samples.TestController.class); + + @RequestMapping(value = "/get-test", method = { + RequestMethod.GET, + RequestMethod.GET, + RequestMethod.GET, + RequestMethod.GET, + RequestMethod.GET + }) + public ModelAndView duplicatedMethod1(final HttpServletRequest request, + final HttpServletResponse response) { + return null; + } +} diff --git a/mvc/src/test/java/samples/TestController.java b/mvc/src/test/java/samples/TestController.java index 1f0e4acfb3..578582d34d 100644 --- a/mvc/src/test/java/samples/TestController.java +++ b/mvc/src/test/java/samples/TestController.java @@ -30,4 +30,21 @@ public ModelAndView save(final HttpServletRequest request, final HttpServletResp modelAndView.addObject("id", request.getAttribute("id")); return modelAndView; } + + @RequestMapping(value = "/multi-method-test", method = {RequestMethod.GET, RequestMethod.POST} ) + public ModelAndView multiHandle(final HttpServletRequest request, final HttpServletResponse response) { + log.info("test controller multi-handle method"); + String method = request.getMethod(); + if("GET".equals(method)){ + final var modelAndView = new ModelAndView(new JspView("")); + modelAndView.addObject("id", "getPooh"); + return modelAndView; + } + if("POST".equals(method)){ + final var modelAndView = new ModelAndView(new JspView("")); + modelAndView.addObject("id", "postPooh"); + return modelAndView; + } + throw new IllegalArgumentException("해당 요청을 Handling 할 수 없는 핸들러입니다."); + } } diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java index 494f21936f..c69bcce2b0 100644 --- a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java @@ -21,7 +21,7 @@ void setUp() { } @Test - void methodDuplicationException_case1() { + void methodDuplicationException_existMappingInOtherClass_case1() { // given & when & then AnnotationHandlerMapping duplicatedHandlerMapping = new AnnotationHandlerMapping( "samples", @@ -32,7 +32,7 @@ void methodDuplicationException_case1() { } @Test - void methodDuplicationException_case2() { + void methodDuplicationException_existMappingIntSameClass_case2() { // given & when & then AnnotationHandlerMapping duplicatedHandlerMapping = new AnnotationHandlerMapping( "duplicate.case2"); @@ -41,6 +41,36 @@ void methodDuplicationException_case2() { .hasMessage("Duplicated HandlerKey"); } + @Test + void methodDuplicationException_duplicateMappingInOneMethod_case3() { + // given & when & then + AnnotationHandlerMapping duplicatedHandlerMapping = new AnnotationHandlerMapping( + "duplicate.case3"); + assertThatThrownBy(() -> duplicatedHandlerMapping.initialize()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Duplicated HandlerKey"); + } + + @Test + void oneMethodCanCreateManyHttpRequestMappings() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + + when(request.getAttribute("id")).thenReturn("gugu"); + when(request.getRequestURI()).thenReturn("/multi-method-test"); + when(request.getMethod()).thenReturn("GET"); + + final var handlerExecution = (HandlerExecution) handlerMapping.getHandler(request); + final var getModelAndView = handlerExecution.handle(request, response); + + when(request.getMethod()).thenReturn("POST"); + + final var postModelAndView = handlerExecution.handle(request, response); + + assertThat(getModelAndView.getObject("id")).isEqualTo("getPooh"); + assertThat(postModelAndView.getObject("id")).isEqualTo("postPooh"); + } + @Test void get() throws Exception { final var request = mock(HttpServletRequest.class);