Skip to content

Commit 7c6a1a1

Browse files
committed
Improve tests for detection of @MVC annotations
Issue: SPR-9601
1 parent 060b37c commit 7c6a1a1

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HandlerMethodAnnotationDetectionTests.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotNull;
2121

22+
import java.lang.reflect.Method;
2223
import java.text.SimpleDateFormat;
2324
import java.util.Arrays;
2425
import java.util.Collection;
2526
import java.util.Date;
2627

28+
import org.aopalliance.aop.Advice;
2729
import org.junit.Test;
2830
import org.junit.runner.RunWith;
2931
import org.junit.runners.Parameterized;
3032
import org.junit.runners.Parameterized.Parameters;
33+
import org.springframework.aop.Pointcut;
3134
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
3235
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
3336
import org.springframework.aop.support.DefaultPointcutAdvisor;
37+
import org.springframework.aop.support.StaticMethodMatcherPointcut;
3438
import org.springframework.beans.factory.support.RootBeanDefinition;
3539
import org.springframework.beans.propertyeditors.CustomDateEditor;
40+
import org.springframework.core.annotation.AnnotationUtils;
3641
import org.springframework.mock.web.MockHttpServletRequest;
3742
import org.springframework.mock.web.MockHttpServletResponse;
3843
import org.springframework.stereotype.Controller;
@@ -91,21 +96,29 @@ public static Collection<Object[]> handlerTypes() {
9196

9297
private ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver();
9398

94-
public HandlerMethodAnnotationDetectionTests(Class<?> controllerType, boolean useAutoProxy) {
99+
public HandlerMethodAnnotationDetectionTests(final Class<?> controllerType, boolean useAutoProxy) {
95100
GenericWebApplicationContext context = new GenericWebApplicationContext();
96101
context.registerBeanDefinition("controller", new RootBeanDefinition(controllerType));
102+
context.registerBeanDefinition("handlerMapping", new RootBeanDefinition(RequestMappingHandlerMapping.class));
103+
context.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(RequestMappingHandlerAdapter.class));
104+
context.registerBeanDefinition("exceptionResolver", new RootBeanDefinition(ExceptionHandlerExceptionResolver.class));
97105
if (useAutoProxy) {
98106
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
99107
autoProxyCreator.setBeanFactory(context.getBeanFactory());
100108
context.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
101-
context.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
109+
context.registerBeanDefinition("controllerAdvice", new RootBeanDefinition(ControllerAdvice.class));
102110
}
103111
context.refresh();
104112

105-
handlerMapping.setApplicationContext(context);
106-
handlerMapping.afterPropertiesSet();
107-
handlerAdapter.afterPropertiesSet();
108-
exceptionResolver.afterPropertiesSet();
113+
this.handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
114+
this.handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
115+
this.exceptionResolver = context.getBean(ExceptionHandlerExceptionResolver.class);
116+
}
117+
118+
class TestPointcut extends StaticMethodMatcherPointcut {
119+
public boolean matches(Method method, Class<?> clazz) {
120+
return method.getName().equals("hashCode");
121+
}
109122
}
110123

111124
@Test
@@ -232,11 +245,11 @@ static interface MappingInterface {
232245
/**
233246
* CONTROLLER WITH INTERFACE
234247
*
235-
* No AOP:
236-
* All annotations can be on interface methods except parameter annotations.
237-
*
238248
* JDK Dynamic proxy:
239249
* All annotations must be on the interface.
250+
*
251+
* Without AOP:
252+
* Annotations can be on interface methods except parameter annotations.
240253
*/
241254
static class InterfaceController implements MappingInterface {
242255

@@ -391,4 +404,21 @@ public String handleException(Exception exception) {
391404
static class SupportClassController extends MappingSupportClass {
392405
}
393406

407+
408+
static class ControllerAdvice extends DefaultPointcutAdvisor {
409+
410+
public ControllerAdvice() {
411+
super(getControllerPointcut(), new SimpleTraceInterceptor());
412+
}
413+
414+
private static StaticMethodMatcherPointcut getControllerPointcut() {
415+
return new StaticMethodMatcherPointcut() {
416+
public boolean matches(Method method, Class<?> targetClass) {
417+
return ((AnnotationUtils.findAnnotation(targetClass, Controller.class) != null) ||
418+
(AnnotationUtils.findAnnotation(targetClass, RequestMapping.class) != null));
419+
}
420+
};
421+
}
422+
}
423+
394424
}

0 commit comments

Comments
 (0)