forked from woowacourse/java-mvc
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π0λ¨κ³, 1λ¨κ³ μλ£ #5
Open
chabin37
wants to merge
3
commits into
gdsc-konkuk:24HwangChabin
Choose a base branch
from
chabin37:main
base: 24HwangChabin
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+397
β39
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
### 1λ¨κ³ | ||
|
||
``AnnotationHandlerMappingTest`` | ||
|
||
#### ν μ€νΈ μΌμ΄μ€μ€λͺ | ||
|
||
```java | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
``` | ||
|
||
Servletν΄λμ€λ₯Ό κ°μ Έμ΄ | ||
|
||
```java | ||
when(request.getAttribute("id")).thenReturn("gugu"); | ||
when(request.getRequestURI()).thenReturn("/get-test"); | ||
when(request.getMethod()).thenReturn("GET"); | ||
``` | ||
|
||
`id`κ° νΈμΆλλ©΄ `gugu`λ°ν, `URI`νΈμΆλλ©΄ `/get-test`λ°ν, `Method`νΈμΆλλ©΄ `GET`λ°ν μ€μ | ||
|
||
```java | ||
final var handlerExecution = (HandlerExecution) handlerMapping.getHandler(request); | ||
final var modelAndView = handlerExecution.handle(request, response); | ||
``` | ||
|
||
(κΈ°μ‘΄μ μ΄λ―Έ μ€μ λ) νΈλ€λ¬λ₯Ό νΈμΆν΄μ requestμ λ§λ `handlerExecution` νΈμΆ, μ΄λ₯Ό ν΅ν΄ `modelAndView` λ°ν | ||
|
||
```java | ||
assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); | ||
``` | ||
|
||
ν μ€νΈκ° μννκ² λμνλμ§ νμΈ | ||
|
||
1. request λ₯Ό Servletμ΄ λ°κ³ , μ΄λ₯Ό HandlerMappingμ ν΅ν΄ μ ν©ν HandlerControllerλ₯Ό μ°Ύμ μ μ©μν΄ | ||
2. μ¬κΈ°μλ Keyλ₯Ό ν΅ν΄ μ°Ύκ³ , μ΄λ₯Ό Execution μ ν΅ν΄ μ€νμν€λ κ·Έλ¦Όκ°μ. | ||
`HandlerExecution`ν΄λμ€ λν μμ . `ModelAndView`νμ λ°νμ. Viewνλλ nullλ‘ μ΄κΈ°ν. | ||
idλ₯Ό μ‘°νμ requestμμ idμ ν΄λΉνλ attributeλ₯Ό μ°Ύμμ, μ΄λ₯Ό λ¬Άμ΄ `ModelAndView`λ‘ λ°ννλλ‘μμ . | ||
3. λ°λΌμ ν€λ₯Ό μμ±νκ³ , HandlerExecutionμ μμ±ν΄ Map μλ£ν `handlerExecutions`μ | ||
`handlerExecutions.put(new HandlerKey("/get-test", RequestMethod.GET), new HandlerExecution());` | ||
4. μ΄ν `getHandler`μ, μ λ ₯λ νλΌλ©ν° ν λλ‘ HandlerKeyλ₯Ό μμ±, `return handlerExecutions.get(handlerKey);` | ||
|
||
#### Postν μ€νΈλ λΉμ·ν λ°©λ²μΌλ‘ ν΄κ²° | ||
|
||
--- |
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
mvc/src/main/java/com/interface21/webmvc/servlet/mvc/DispatcherServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.interface21.webmvc.servlet.mvc; | ||
|
||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.View; | ||
import com.interface21.webmvc.servlet.mvc.tobe.HandlerExecution; | ||
import com.interface21.webmvc.servlet.mvc.tobe.HandlerMapping; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DispatcherServlet implements HandlerMapping { | ||
private final HandlerMapping AnnotationHandlerMapping; | ||
private final HandlerMapping ManualHandlerMapping; | ||
|
||
public DispatcherServlet(HandlerMapping handlerMapping1, HandlerMapping handlerMapping2) { | ||
AnnotationHandlerMapping = handlerMapping1; | ||
ManualHandlerMapping = handlerMapping2; | ||
} | ||
|
||
/** | ||
* μλ μλ λ λ©μλ μ€ νλλ§ μμ΄μΌ νλλ°, | ||
* Annotation κ³Ό Manual λ λ€ μ¬μ© κ°λ₯νκ² νκΈ° μν΄μ μ΄ λ°©λ²μ΄..? | ||
*/ | ||
public Map<String, Object> doDispatchWithAnnotationHandlerMapping(HttpServletRequest request, HttpServletResponse response) throws Exception { | ||
var handlerExecution = (HandlerExecution) AnnotationHandlerMapping.getHandler(request); | ||
var modelAndView=handlerExecution.handle(request,response); | ||
return modelAndView.getModel(); | ||
} | ||
public Map<String, Object> doDispatchWithManualHandlerMapping(HttpServletRequest request, HttpServletResponse response) throws Exception { | ||
var handlerExecution = (HandlerExecution) ManualHandlerMapping.getHandler(request); | ||
var modelAndView=handlerExecution.handle(request,response); | ||
return modelAndView.getModel(); | ||
} | ||
|
||
@Override | ||
public Object getHandler(HttpServletRequest request) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
mvc/src/main/java/com/interface21/webmvc/servlet/mvc/tobe/HandlerMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.interface21.webmvc.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public interface HandlerMapping { | ||
Object getHandler(HttpServletRequest request); | ||
} |
33 changes: 33 additions & 0 deletions
33
mvc/src/main/java/com/interface21/webmvc/servlet/mvc/tobe/ManualHandlerMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.interface21.webmvc.servlet.mvc.tobe; | ||
|
||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ManualHandlerMapping implements HandlerMapping { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); | ||
|
||
private final Object[] basePackage; | ||
private final Map<HandlerKey, HandlerExecution> handlerExecutions; | ||
|
||
public ManualHandlerMapping(final Object... basePackage) { | ||
this.basePackage = basePackage; | ||
this.handlerExecutions = new HashMap<>(); | ||
} | ||
|
||
public void initialize() { | ||
log.info("Initialized AnnotationHandlerMapping!"); | ||
handlerExecutions.put(new HandlerKey("/get-test", RequestMethod.GET), new HandlerExecution()); | ||
handlerExecutions.put(new HandlerKey("/post-test", RequestMethod.POST), new HandlerExecution()); | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
HandlerKey handlerKey = new HandlerKey(request.getRequestURI(), RequestMethod.valueOf(request.getMethod())); | ||
return handlerExecutions.get(handlerKey); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
mvc/src/test/java/com/interface21/webmvc/servlet/mvc/DispatcherServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.interface21.webmvc.servlet.mvc; | ||
|
||
import com.interface21.webmvc.servlet.mvc.tobe.AnnotationHandlerMapping; | ||
import com.interface21.webmvc.servlet.mvc.tobe.ManualHandlerMapping; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class DispatcherServletTest { | ||
private DispatcherServlet dispatcherServlet; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
AnnotationHandlerMapping annotationHandlerMapping = new AnnotationHandlerMapping("com.interface21.webmvc.servlet"); | ||
ManualHandlerMapping manualHandlerMapping = new ManualHandlerMapping(); | ||
annotationHandlerMapping.initialize(); | ||
manualHandlerMapping.initialize(); | ||
dispatcherServlet=new DispatcherServlet(annotationHandlerMapping, manualHandlerMapping); | ||
} | ||
|
||
@Test | ||
void AnnotationHandlerMapping() throws Exception { | ||
final var requestGet = mock(HttpServletRequest.class); | ||
final var responseGet = mock(HttpServletResponse.class); | ||
|
||
final var requestPost = mock(HttpServletRequest.class); | ||
final var responsePOST = mock(HttpServletResponse.class); | ||
|
||
when(requestGet.getAttribute("id")).thenReturn("gugu"); | ||
when(requestGet.getRequestURI()).thenReturn("/get-test"); | ||
when(requestGet.getMethod()).thenReturn("GET"); | ||
|
||
when(requestPost.getAttribute("id")).thenReturn("gugu"); | ||
when(requestPost.getRequestURI()).thenReturn("/post-test"); | ||
when(requestPost.getMethod()).thenReturn("POST"); | ||
|
||
final var modelGet = dispatcherServlet.doDispatchWithAnnotationHandlerMapping(requestGet, responseGet); | ||
final var modelPost = dispatcherServlet.doDispatchWithAnnotationHandlerMapping(requestPost, responsePOST); | ||
assertThat(modelGet.get("id")).isEqualTo("gugu"); | ||
assertThat(modelPost.get("id")).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void ManualHandlerMapping() throws Exception { | ||
final var requestGet = mock(HttpServletRequest.class); | ||
final var responseGet = mock(HttpServletResponse.class); | ||
|
||
final var requestPost = mock(HttpServletRequest.class); | ||
final var responsePOST = mock(HttpServletResponse.class); | ||
|
||
when(requestGet.getAttribute("id")).thenReturn("gugu"); | ||
when(requestGet.getRequestURI()).thenReturn("/get-test"); | ||
when(requestGet.getMethod()).thenReturn("GET"); | ||
|
||
when(requestPost.getAttribute("id")).thenReturn("gugu"); | ||
when(requestPost.getRequestURI()).thenReturn("/post-test"); | ||
when(requestPost.getMethod()).thenReturn("POST"); | ||
|
||
final var modelGet = dispatcherServlet.doDispatchWithManualHandlerMapping(requestGet, responseGet); | ||
final var modelPost = dispatcherServlet.doDispatchWithManualHandlerMapping(requestPost, responsePOST); | ||
assertThat(modelGet.get("id")).isEqualTo("gugu"); | ||
assertThat(modelPost.get("id")).isEqualTo("gugu"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
mvc/src/test/java/com/interface21/webmvc/servlet/mvc/tobe/ManaualHandlerMappingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.interface21.webmvc.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ManaualHandlerMappingTest { | ||
|
||
private ManualHandlerMapping handlerMapping; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
handlerMapping = new ManualHandlerMapping("samples"); | ||
handlerMapping.initialize(); | ||
} | ||
|
||
@Test | ||
void get() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
|
||
when(request.getAttribute("id")).thenReturn("gugu"); | ||
when(request.getRequestURI()).thenReturn("/get-test"); | ||
when(request.getMethod()).thenReturn("GET"); | ||
|
||
final var handlerExecution = (HandlerExecution) handlerMapping.getHandler(request); | ||
final var modelAndView = handlerExecution.handle(request, response); | ||
|
||
assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void post() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
|
||
when(request.getAttribute("id")).thenReturn("gugu"); | ||
when(request.getRequestURI()).thenReturn("/post-test"); | ||
when(request.getMethod()).thenReturn("POST"); | ||
|
||
final var handlerExecution = (HandlerExecution) handlerMapping.getHandler(request); | ||
final var modelAndView = handlerExecution.handle(request, response); | ||
|
||
assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ DispatcherServletμ app ν¨ν€μ§ μμ μμ΅λλ€! λ€μ νμΈν΄λ³΄κ³ μ μ©ν΄λ³΄μ μΌ ν κ² κ°μ΅λλ€!