Skip to content

Commit

Permalink
feat: 서블릿의 인자 갯수 검증 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Sep 14, 2023
1 parent d662531 commit eec9b31
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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;

import java.lang.reflect.Method;
import java.util.Arrays;
Expand Down Expand Up @@ -51,7 +50,7 @@ private Object getInstance(final Class<?> classType) {

private List<Method> getMethods(final Class<?> controller) {
return Arrays.stream(controller.getDeclaredMethods())
.filter(method -> method.getReturnType().equals(ModelAndView.class))
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import static java.lang.String.format;

public class HandlerExecution {

private static final int REQUEST_INDEX = 0;
private static final int RESPONSE_INDEX = 1;
private static final int VALID_ARGUMENTS_SIZE = 2;

private final Object invoker;
private final Method method;
Expand All @@ -24,6 +27,9 @@ public HandlerExecution(final Object invoker, final Method method) {
private void validate(final Method method) {
final Parameter[] parameters = method.getParameters();

if (parameters.length != VALID_ARGUMENTS_SIZE) {
throw new IllegalStateException(format("인자는 %d개여야 합니다.", VALID_ARGUMENTS_SIZE));
}
if (parameters[REQUEST_INDEX].getType() != HttpServletRequest.class) {
throw new IllegalStateException("HttpServletRequest가 필요합니다.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ void construct_fail3() throws NoSuchMethodException {
.hasMessage("ModelAndView를 리턴해야 합니다.");
}

@Test
@DisplayName("handle 메서드의 시그니처와 다르면 예외가 발생한다 - 인자 갯수")
void construct_fail4() throws NoSuchMethodException {
//given
final IllegalController object = new IllegalController();
final Method method = object.getClass().getMethod("invalidArgumentsSize", HttpServletRequest.class,
HttpServletResponse.class, int.class);

//when, then
assertThatThrownBy(() -> new HandlerExecution(object, method))
.isInstanceOf(IllegalStateException.class)
.hasMessage("인자는 2개여야 합니다.");
}

private static class IllegalController {

public ModelAndView noRequest(final int illegal, final HttpServletResponse response) {
Expand All @@ -77,5 +91,10 @@ public ModelAndView noResponse(final HttpServletRequest request, final int illeg
public Integer noView(final HttpServletRequest request, final HttpServletResponse response) {
return null;
}

public ModelAndView invalidArgumentsSize(final HttpServletRequest request, final HttpServletResponse response,
final int illegal) {
return null;
}
}
}

0 comments on commit eec9b31

Please sign in to comment.