|
15 | 15 | */
|
16 | 16 | package org.springframework.web.servlet.mvc.method.annotation;
|
17 | 17 |
|
18 |
| -import static org.junit.Assert.*; |
| 18 | +import static org.junit.Assert.assertArrayEquals; |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertTrue; |
19 | 22 |
|
| 23 | +import java.lang.reflect.Method; |
20 | 24 | import java.util.Arrays;
|
21 | 25 | import java.util.Collections;
|
| 26 | +import java.util.HashSet; |
22 | 27 | import java.util.Map;
|
| 28 | +import java.util.Set; |
23 | 29 |
|
24 | 30 | import org.junit.Before;
|
25 | 31 | import org.junit.Test;
|
26 | 32 | import org.springframework.http.MediaType;
|
| 33 | +import org.springframework.stereotype.Controller; |
27 | 34 | import org.springframework.util.StringValueResolver;
|
28 | 35 | import org.springframework.web.accept.ContentNegotiationManager;
|
29 | 36 | import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
|
| 37 | +import org.springframework.web.bind.annotation.RequestMapping; |
30 | 38 | import org.springframework.web.context.support.StaticWebApplicationContext;
|
31 |
| -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 39 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
32 | 40 |
|
33 | 41 | /**
|
34 | 42 | * Tests for {@link RequestMappingHandlerMapping}.
|
@@ -63,6 +71,35 @@ public void useRegsiteredSuffixPatternMatch() {
|
63 | 71 | assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
|
64 | 72 | }
|
65 | 73 |
|
| 74 | + @Test |
| 75 | + public void useRegsiteredSuffixPatternMatchInitialization() { |
| 76 | + |
| 77 | + Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON); |
| 78 | + PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions); |
| 79 | + ContentNegotiationManager manager = new ContentNegotiationManager(strategy); |
| 80 | + |
| 81 | + final Set<String> extensions = new HashSet<String>(); |
| 82 | + |
| 83 | + RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() { |
| 84 | + @Override |
| 85 | + protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { |
| 86 | + extensions.addAll(getFileExtensions()); |
| 87 | + return super.getMappingForMethod(method, handlerType); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + StaticWebApplicationContext wac = new StaticWebApplicationContext(); |
| 92 | + wac.registerSingleton("testController", TestController.class); |
| 93 | + wac.refresh(); |
| 94 | + |
| 95 | + hm.setContentNegotiationManager(manager); |
| 96 | + hm.setUseRegisteredSuffixPatternMatch(true); |
| 97 | + hm.setApplicationContext(wac); |
| 98 | + hm.afterPropertiesSet(); |
| 99 | + |
| 100 | + assertEquals(Collections.singleton("json"), extensions); |
| 101 | + } |
| 102 | + |
66 | 103 | @Test
|
67 | 104 | public void useSuffixPatternMatch() {
|
68 | 105 | assertTrue(this.handlerMapping.useSuffixPatternMatch());
|
@@ -93,4 +130,13 @@ public String resolveStringValue(String value) {
|
93 | 130 | assertArrayEquals(new String[] { "/foo", "/foo/bar" }, result);
|
94 | 131 | }
|
95 | 132 |
|
| 133 | + |
| 134 | + @Controller |
| 135 | + static class TestController { |
| 136 | + |
| 137 | + @RequestMapping |
| 138 | + public void handle() { |
| 139 | + } |
| 140 | + } |
| 141 | + |
96 | 142 | }
|
0 commit comments