Skip to content
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

[MVC 1, 2단계] - 박스터 미션 제출합니다. #341

Closed
wants to merge 14 commits into from
Closed
Prev Previous commit
Next Next commit
feat: handler 등록하는 클래스 추가
  • Loading branch information
drunkenhw committed Sep 12, 2023
commit 00c8016a2e9d25209c3a6463e2d3a53fd0a16543
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package webmvc.org.springframework.web.servlet.mvc;

import jakarta.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class HandlerMappingRegistry {

private final List<HandlerMapping> handlerMappings;

public HandlerMappingRegistry(List<HandlerMapping> handlerMappings) {
this.handlerMappings = handlerMappings;
}

public HandlerMappingRegistry() {
this.handlerMappings = new ArrayList<>();
}

public void addHandlerMapping(HandlerMapping handlerMapping) {
handlerMappings.add(handlerMapping);
}

public Optional<Object> getHandler(HttpServletRequest request) {
return handlerMappings.stream()
.map(handlerMapping -> handlerMapping.getHandler(request))
.findAny();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package webmvc.org.springframework.web.servlet.mvc;

import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class HandlerMappingRegistryTest {

@Test
void HandlerMapping을_등록하고_가져온다() throws Exception {
var handlerMappingRegistry = new HandlerMappingRegistry();
var handlerMapping = new AnnotationHandlerMapping("samples");
handlerMapping.initialize();
var request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn("/get-test");
when(request.getMethod()).thenReturn("GET");

handlerMappingRegistry.addHandlerMapping(handlerMapping);

assertThat(handlerMappingRegistry.getHandler(request)).isPresent();
}
}