1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.web

File BlossomRequestMappingHandlerAdapterTest.java

 

Code metrics

0
48
13
2
191
114
13
0.27
3.69
6.5
1

Classes

Class Line # Actions
BlossomRequestMappingHandlerAdapterTest 73 45 0% 12 4
0.929824693%
BlossomRequestMappingHandlerAdapterTest.BlossomRequestMappingHandlerAdapterTestConfig 82 3 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Tests for BlossomRequestMappingHandlerAdapter.
70    *
71    * @since 3.0.2
72    */
 
73    public class BlossomRequestMappingHandlerAdapterTest extends MgnlTestCase {
74   
 
75  6 toggle @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   
 
82    public static class BlossomRequestMappingHandlerAdapterTestConfig {
83   
 
84  6 toggle @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   
 
92  1 toggle @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   
 
113  1 toggle @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   
 
135  0 toggle public void methodWithContextArgument(Context context) {
136    }
137   
 
138  1 toggle @Test
139    public void testArgumentResolversResolvesContext() throws Exception {
140  1 assertResolvesArgument(MgnlContext.getInstance(), "methodWithContextArgument", Context.class, 0);
141    }
142   
 
143  0 toggle public void methodWithWebContextArgument(WebContext webContext) {
144    }
145   
 
146  1 toggle @Test
147    public void testArgumentResolversResolvesWebContext() throws Exception {
148  1 assertResolvesArgument(MgnlContext.getWebContext(), "methodWithWebContextArgument", WebContext.class, 0);
149    }
150   
 
151  0 toggle public void methodWithUserArgument(User user) {
152    }
153   
 
154  1 toggle @Test
155    public void testArgumentResolversResolvesUser() throws Exception {
156  1 assertResolvesArgument(MgnlContext.getUser(), "methodWithUserArgument", User.class, 0);
157    }
158   
 
159  0 toggle public void methodWithMgnlUserArgument(MgnlUser mgnlUser) {
160    }
161   
 
162  1 toggle @Test
163    public void testArgumentResolversResolvesMgnlUser() throws Exception {
164  1 assertResolvesArgument(MgnlContext.getUser(), "methodWithMgnlUserArgument", MgnlUser.class, 0);
165    }
166   
 
167  4 toggle 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    }