16
16
17
17
package org .springframework .web .servlet .mvc .method .annotation ;
18
18
19
- import java .lang .annotation .Documented ;
20
19
import java .lang .annotation .ElementType ;
21
20
import java .lang .annotation .Retention ;
22
21
import java .lang .annotation .RetentionPolicy ;
28
27
import java .util .Map ;
29
28
import java .util .Set ;
30
29
31
- import org .junit .Before ;
32
30
import org .junit .Test ;
33
31
32
+ import org .springframework .core .annotation .AliasFor ;
34
33
import org .springframework .http .MediaType ;
35
34
import org .springframework .stereotype .Controller ;
36
- import org .springframework .util .StringValueResolver ;
37
35
import org .springframework .web .accept .ContentNegotiationManager ;
38
36
import org .springframework .web .accept .PathExtensionContentNegotiationStrategy ;
39
37
import org .springframework .web .bind .annotation .RequestMapping ;
47
45
* Tests for {@link RequestMappingHandlerMapping}.
48
46
*
49
47
* @author Rossen Stoyanchev
48
+ * @author Sam Brannen
50
49
*/
51
50
public class RequestMappingHandlerMappingTests {
52
51
53
- private RequestMappingHandlerMapping handlerMapping ;
52
+ private final StaticWebApplicationContext wac = new StaticWebApplicationContext () ;
54
53
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 );
63
57
}
64
58
65
59
@@ -97,8 +91,7 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
97
91
}
98
92
};
99
93
100
- StaticWebApplicationContext wac = new StaticWebApplicationContext ();
101
- wac .registerSingleton ("testController" , MetaAnnotationController .class );
94
+ wac .registerSingleton ("testController" , ComposedAnnotationController .class );
102
95
wac .refresh ();
103
96
104
97
hm .setContentNegotiationManager (manager );
@@ -127,12 +120,9 @@ public void useSuffixPatternMatch() {
127
120
128
121
@ Test
129
122
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
+ );
136
126
137
127
String [] patterns = new String [] { "/foo" , "/${pattern}/bar" };
138
128
String [] result = this .handlerMapping .resolveEmbeddedValuesInPatterns (patterns );
@@ -141,36 +131,37 @@ public String resolveStringValue(String value) {
141
131
}
142
132
143
133
@ 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 );
147
138
148
139
assertNotNull (info );
149
140
assertEquals (Collections .singleton ("/input" ), info .getPatternsCondition ().getPatterns ());
150
141
}
151
142
152
143
153
144
@ Controller
154
- static class MetaAnnotationController {
145
+ static class ComposedAnnotationController {
155
146
156
147
@ RequestMapping
157
148
public void handle () {
158
149
}
159
150
160
- @ PostJson (path = "/input" )
151
+ @ PostJson ("/input" )
161
152
public void handleInput () {
162
153
}
163
-
164
154
}
165
155
166
156
@ RequestMapping (method = RequestMethod .POST ,
167
157
produces = MediaType .APPLICATION_JSON_VALUE ,
168
158
consumes = MediaType .APPLICATION_JSON_VALUE )
169
- @ Target ({ ElementType .METHOD , ElementType . TYPE } )
159
+ @ Target (ElementType .METHOD )
170
160
@ Retention (RetentionPolicy .RUNTIME )
171
- @ Documented
172
161
@interface PostJson {
173
- String [] path () default {};
162
+
163
+ @ AliasFor (annotation = RequestMapping .class , attribute = "path" )
164
+ String [] value () default {};
174
165
}
175
166
176
167
}
0 commit comments