1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.blossom.web; |
35 |
|
|
36 |
|
import static org.junit.Assert.assertFalse; |
37 |
|
import static org.junit.Assert.assertNotNull; |
38 |
|
import static org.junit.Assert.assertSame; |
39 |
|
import static org.junit.Assert.assertTrue; |
40 |
|
import static org.mockito.Mockito.mock; |
41 |
|
import static org.mockito.Mockito.when; |
42 |
|
|
43 |
|
import info.magnolia.cms.security.MgnlUser; |
44 |
|
import info.magnolia.cms.security.User; |
45 |
|
import info.magnolia.context.Context; |
46 |
|
import info.magnolia.context.MgnlContext; |
47 |
|
import info.magnolia.context.WebContext; |
48 |
|
import info.magnolia.module.blossom.view.UuidRedirectViewResolver; |
49 |
|
import info.magnolia.test.MgnlTestCase; |
50 |
|
import info.magnolia.test.mock.MockWebContext; |
51 |
|
|
52 |
|
import java.lang.reflect.Method; |
53 |
|
import java.util.Collections; |
54 |
|
|
55 |
|
import javax.servlet.http.HttpServletRequest; |
56 |
|
|
57 |
|
import org.junit.Test; |
58 |
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
59 |
|
import org.springframework.context.annotation.Bean; |
60 |
|
import org.springframework.core.MethodParameter; |
61 |
|
import org.springframework.mock.web.MockHttpServletRequest; |
62 |
|
import org.springframework.ui.ModelMap; |
63 |
|
import org.springframework.web.context.request.NativeWebRequest; |
64 |
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver; |
65 |
|
import org.springframework.web.method.support.HandlerMethodArgumentResolverComposite; |
66 |
|
import org.springframework.web.method.support.ModelAndViewContainer; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@since |
72 |
|
|
|
|
| 93% |
Uncovered Elements: 4 (57) |
Complexity: 12 |
Complexity Density: 0.27 |
|
73 |
|
public class BlossomRequestMappingHandlerAdapterTest extends MgnlTestCase { |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
75 |
6 |
@Override... |
76 |
|
protected void initContext() { |
77 |
6 |
MockWebContext mockWebContext = new MockWebContext(); |
78 |
6 |
mockWebContext.setUser(mock(MgnlUser.class)); |
79 |
6 |
MgnlContext.setInstance(mockWebContext); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.33 |
|
82 |
|
public static class BlossomRequestMappingHandlerAdapterTestConfig { |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
84 |
6 |
@Bean... |
85 |
|
public BlossomRequestMappingHandlerAdapter handlerAdapter() { |
86 |
6 |
BlossomRequestMappingHandlerAdapter adapter = new BlossomRequestMappingHandlerAdapter(); |
87 |
6 |
adapter.setCustomArgumentResolvers(Collections.<HandlerMethodArgumentResolver>singletonList(new BlossomHandlerMethodArgumentResolver())); |
88 |
6 |
return adapter; |
89 |
|
} |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
92 |
1 |
@Test... |
93 |
|
public void testRedirectViewDetection() { |
94 |
|
|
95 |
1 |
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); |
96 |
1 |
wac.register(BlossomRequestMappingHandlerAdapterTestConfig.class); |
97 |
1 |
wac.refresh(); |
98 |
|
|
99 |
1 |
BlossomRequestMappingHandlerAdapter handlerAdapter = wac.getBean(BlossomRequestMappingHandlerAdapter.class); |
100 |
|
|
101 |
1 |
assertTrue(handlerAdapter.isRedirectViewName(UuidRedirectViewResolver.REDIRECT_MAIN_CONTENT_PLACEHOLDER)); |
102 |
1 |
assertTrue(handlerAdapter.isRedirectViewName(UuidRedirectViewResolver.REDIRECT_CURRENT_CONTENT_PLACEHOLDER)); |
103 |
|
|
104 |
1 |
assertTrue(handlerAdapter.isRedirectViewName("website:123")); |
105 |
1 |
assertTrue(handlerAdapter.isRedirectViewName("dms:123")); |
106 |
|
|
107 |
1 |
assertFalse(handlerAdapter.isRedirectViewName("main.ftl")); |
108 |
1 |
assertFalse(handlerAdapter.isRedirectViewName("main.jsp")); |
109 |
|
|
110 |
1 |
assertFalse(handlerAdapter.isRedirectViewName("workspace:123")); |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
113 |
1 |
@Test... |
114 |
|
public void testRedirectViewDetectionWithCustomPatterns() { |
115 |
|
|
116 |
1 |
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); |
117 |
1 |
wac.register(BlossomRequestMappingHandlerAdapterTestConfig.class); |
118 |
1 |
wac.refresh(); |
119 |
|
|
120 |
1 |
BlossomRequestMappingHandlerAdapter handlerAdapter = wac.getBean(BlossomRequestMappingHandlerAdapter.class); |
121 |
1 |
handlerAdapter.setRedirectPatterns("prefix:*"); |
122 |
|
|
123 |
1 |
assertTrue(handlerAdapter.isRedirectViewName(UuidRedirectViewResolver.REDIRECT_MAIN_CONTENT_PLACEHOLDER)); |
124 |
1 |
assertTrue(handlerAdapter.isRedirectViewName(UuidRedirectViewResolver.REDIRECT_CURRENT_CONTENT_PLACEHOLDER)); |
125 |
|
|
126 |
1 |
assertTrue(handlerAdapter.isRedirectViewName("prefix:123")); |
127 |
|
|
128 |
1 |
assertFalse(handlerAdapter.isRedirectViewName("website:123")); |
129 |
1 |
assertFalse(handlerAdapter.isRedirectViewName("dms:123")); |
130 |
|
|
131 |
1 |
assertFalse(handlerAdapter.isRedirectViewName("main.ftl")); |
132 |
1 |
assertFalse(handlerAdapter.isRedirectViewName("main.jsp")); |
133 |
|
} |
134 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
135 |
0 |
public void methodWithContextArgument(Context context) {... |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
138 |
1 |
@Test... |
139 |
|
public void testArgumentResolversResolvesContext() throws Exception { |
140 |
1 |
assertResolvesArgument(MgnlContext.getInstance(), "methodWithContextArgument", Context.class, 0); |
141 |
|
} |
142 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
143 |
0 |
public void methodWithWebContextArgument(WebContext webContext) {... |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void testArgumentResolversResolvesWebContext() throws Exception { |
148 |
1 |
assertResolvesArgument(MgnlContext.getWebContext(), "methodWithWebContextArgument", WebContext.class, 0); |
149 |
|
} |
150 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
151 |
0 |
public void methodWithUserArgument(User user) {... |
152 |
|
} |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
154 |
1 |
@Test... |
155 |
|
public void testArgumentResolversResolvesUser() throws Exception { |
156 |
1 |
assertResolvesArgument(MgnlContext.getUser(), "methodWithUserArgument", User.class, 0); |
157 |
|
} |
158 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
159 |
0 |
public void methodWithMgnlUserArgument(MgnlUser mgnlUser) {... |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
162 |
1 |
@Test... |
163 |
|
public void testArgumentResolversResolvesMgnlUser() throws Exception { |
164 |
1 |
assertResolvesArgument(MgnlContext.getUser(), "methodWithMgnlUserArgument", MgnlUser.class, 0); |
165 |
|
} |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
|
167 |
4 |
private void assertResolvesArgument(Object expected, String methodName, Class<?> argumentType, int parameterIndex) throws Exception {... |
168 |
|
|
169 |
4 |
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); |
170 |
4 |
wac.register(BlossomRequestMappingHandlerAdapterTestConfig.class); |
171 |
4 |
wac.refresh(); |
172 |
|
|
173 |
4 |
BlossomRequestMappingHandlerAdapter handlerAdapter = wac.getBean(BlossomRequestMappingHandlerAdapter.class); |
174 |
|
|
175 |
4 |
HandlerMethodArgumentResolverComposite argumentResolvers = handlerAdapter.getArgumentResolvers(); |
176 |
|
|
177 |
4 |
Method method = getClass().getMethod(methodName, argumentType); |
178 |
4 |
MethodParameter methodParameter = new MethodParameter(method, parameterIndex); |
179 |
|
|
180 |
4 |
ModelAndViewContainer mavContainer = mock(ModelAndViewContainer.class); |
181 |
4 |
when(mavContainer.getModel()).thenReturn(new ModelMap()); |
182 |
|
|
183 |
4 |
NativeWebRequest nativeWebRequest = mock(NativeWebRequest.class); |
184 |
4 |
when(nativeWebRequest.getNativeRequest(HttpServletRequest.class)).thenReturn(new MockHttpServletRequest()); |
185 |
|
|
186 |
4 |
assertTrue(argumentResolvers.supportsParameter(methodParameter)); |
187 |
4 |
Object actual = argumentResolvers.resolveArgument(methodParameter, mavContainer, nativeWebRequest, null); |
188 |
4 |
assertNotNull(actual); |
189 |
4 |
assertSame(expected, actual); |
190 |
|
} |
191 |
|
} |