Skip to content

Commit 74b0511

Browse files
committed
Polish RequestMappingHandlerMappingTests
1 parent 48b512c commit 74b0511

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.web.servlet.mvc.method.annotation;
1818

19-
import java.lang.annotation.Documented;
2019
import java.lang.annotation.ElementType;
2120
import java.lang.annotation.Retention;
2221
import java.lang.annotation.RetentionPolicy;
@@ -28,12 +27,11 @@
2827
import java.util.Map;
2928
import java.util.Set;
3029

31-
import org.junit.Before;
3230
import org.junit.Test;
3331

32+
import org.springframework.core.annotation.AliasFor;
3433
import org.springframework.http.MediaType;
3534
import org.springframework.stereotype.Controller;
36-
import org.springframework.util.StringValueResolver;
3735
import org.springframework.web.accept.ContentNegotiationManager;
3836
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
3937
import org.springframework.web.bind.annotation.RequestMapping;
@@ -47,19 +45,15 @@
4745
* Tests for {@link RequestMappingHandlerMapping}.
4846
*
4947
* @author Rossen Stoyanchev
48+
* @author Sam Brannen
5049
*/
5150
public class RequestMappingHandlerMappingTests {
5251

53-
private RequestMappingHandlerMapping handlerMapping;
52+
private final StaticWebApplicationContext wac = new StaticWebApplicationContext();
5453

55-
private StaticWebApplicationContext applicationContext;
56-
57-
58-
@Before
59-
public void setup() {
60-
this.handlerMapping = new RequestMappingHandlerMapping();
61-
this.applicationContext = new StaticWebApplicationContext();
62-
this.handlerMapping.setApplicationContext(applicationContext);
54+
private final RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
55+
{
56+
this.handlerMapping.setApplicationContext(wac);
6357
}
6458

6559

@@ -97,8 +91,7 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
9791
}
9892
};
9993

100-
StaticWebApplicationContext wac = new StaticWebApplicationContext();
101-
wac.registerSingleton("testController", MetaAnnotationController.class);
94+
wac.registerSingleton("testController", ComposedAnnotationController.class);
10295
wac.refresh();
10396

10497
hm.setContentNegotiationManager(manager);
@@ -127,12 +120,9 @@ public void useSuffixPatternMatch() {
127120

128121
@Test
129122
public void resolveEmbeddedValuesInPatterns() {
130-
this.handlerMapping.setEmbeddedValueResolver(new StringValueResolver() {
131-
@Override
132-
public String resolveStringValue(String value) {
133-
return "/${pattern}/bar".equals(value) ? "/foo/bar" : value;
134-
}
135-
});
123+
this.handlerMapping.setEmbeddedValueResolver(
124+
value -> "/${pattern}/bar".equals(value) ? "/foo/bar" : value
125+
);
136126

137127
String[] patterns = new String[] { "/foo", "/${pattern}/bar" };
138128
String[] result = this.handlerMapping.resolveEmbeddedValuesInPatterns(patterns);
@@ -141,36 +131,37 @@ public String resolveStringValue(String value) {
141131
}
142132

143133
@Test
144-
public void resolveRequestMappingViaMetaAnnotation() throws Exception {
145-
Method method = MetaAnnotationController.class.getMethod("handleInput");
146-
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, MetaAnnotationController.class);
134+
public void resolveRequestMappingViaComposedAnnotation() throws Exception {
135+
Class<?> clazz = ComposedAnnotationController.class;
136+
Method method = clazz.getMethod("handleInput");
137+
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);
147138

148139
assertNotNull(info);
149140
assertEquals(Collections.singleton("/input"), info.getPatternsCondition().getPatterns());
150141
}
151142

152143

153144
@Controller
154-
static class MetaAnnotationController {
145+
static class ComposedAnnotationController {
155146

156147
@RequestMapping
157148
public void handle() {
158149
}
159150

160-
@PostJson(path="/input")
151+
@PostJson("/input")
161152
public void handleInput() {
162153
}
163-
164154
}
165155

166156
@RequestMapping(method = RequestMethod.POST,
167157
produces = MediaType.APPLICATION_JSON_VALUE,
168158
consumes = MediaType.APPLICATION_JSON_VALUE)
169-
@Target({ElementType.METHOD, ElementType.TYPE})
159+
@Target(ElementType.METHOD)
170160
@Retention(RetentionPolicy.RUNTIME)
171-
@Documented
172161
@interface PostJson {
173-
String[] path() default {};
162+
163+
@AliasFor(annotation = RequestMapping.class, attribute = "path")
164+
String[] value() default {};
174165
}
175166

176167
}

0 commit comments

Comments
 (0)