1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.url.internal.container

File ContextAndActionURLNormalizerTest.java

 

Code metrics

0
41
9
1
172
113
10
0.24
4.56
9
1.11

Classes

Class Line # Actions
ContextAndActionURLNormalizerTest 54 41 0% 10 1
0.9898%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.url.internal.container;
21   
22    import java.util.Arrays;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import javax.servlet.ServletContext;
28    import javax.servlet.ServletRegistration;
29    import javax.servlet.http.HttpServletRequest;
30   
31    import org.junit.Before;
32    import org.junit.Rule;
33    import org.junit.Test;
34    import org.xwiki.container.Container;
35    import org.xwiki.container.servlet.ServletRequest;
36    import org.xwiki.environment.Environment;
37    import org.xwiki.environment.internal.ServletEnvironment;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39    import org.xwiki.url.ExtendedURL;
40    import org.xwiki.url.URLNormalizer;
41   
42    import static org.junit.Assert.assertEquals;
43    import static org.junit.Assert.assertSame;
44    import static org.junit.Assert.fail;
45    import static org.mockito.Mockito.mock;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link org.xwiki.url.internal.container.ContextAndActionURLNormalizer}.
50    *
51    * @version $Id: 75f60cc6c0defd9caf3bb0bb8027738963ec372a $
52    * @since 7.4M1
53    */
 
54    public class ContextAndActionURLNormalizerTest
55    {
56    private Container container;
57   
58    private ServletEnvironment environment;
59   
60    private ServletContext servletContext;
61   
62    private ExtendedURL testURL = new ExtendedURL(Arrays.asList("one", "two"));
63   
64    @Rule
65    public MockitoComponentMockingRule<URLNormalizer<ExtendedURL>> mocker =
66    new MockitoComponentMockingRule<URLNormalizer<ExtendedURL>>(ContextAndActionURLNormalizer.class);
67   
 
68  7 toggle @Before
69    public void configure() throws Exception
70    {
71  7 this.container = this.mocker.getInstance(Container.class);
72   
73  7 this.environment = mock(ServletEnvironment.class);
74  7 this.mocker.registerComponent(Environment.class, this.environment);
75   
76  7 this.servletContext = mock(ServletContext.class);
77  7 when(this.environment.getServletContext()).thenReturn(this.servletContext);
78   
79  7 ServletRegistration sr = mock(ServletRegistration.class);
80  7 when(this.servletContext.getServletRegistration("action")).thenReturn(sr);
81  7 when(sr.getMappings()).thenReturn(Arrays.asList("/bin/*", "/wiki/*", "/testbin/*"));
82    }
83   
 
84  1 toggle @Test
85    public void normalizeWithNoKnownContextPathThrowsException() throws Exception
86    {
87  1 this.mocker.registerMockComponent(Environment.class);
88  1 try {
89  1 this.mocker.getComponentUnderTest().normalize(this.testURL);
90  0 fail("Should have thrown an exception");
91    } catch (RuntimeException expected) {
92  1 assertEquals("Failed to normalize the URL [/one/two] since the application's Servlet context couldn't be "
93    + "computed.", expected.getMessage());
94    }
95    }
96   
 
97  1 toggle @Test
98    public void normalizeUsesServletContext() throws Exception
99    {
100  1 when(this.servletContext.getContextPath()).thenReturn("/xwiki");
101   
102  1 assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
103    }
104   
 
105  1 toggle @Test
106    public void normalizeWithRootServletContext() throws Exception
107    {
108  1 when(this.servletContext.getContextPath()).thenReturn("");
109   
110  1 assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
111    }
112   
 
113  1 toggle @Test
114    public void normalizeFromAlternateServletMappingPreservesServletMapping() throws Exception
115    {
116  1 when(this.servletContext.getContextPath()).thenReturn("/xwiki");
117   
118  1 HttpServletRequest req = createMockRequest();
119  1 when(req.getServletPath()).thenReturn("/testbin");
120   
121  1 assertEquals("/xwiki/testbin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
122    }
123   
 
124  1 toggle @Test
125    public void normalizeFromRootServletContextAndServletMapping() throws Exception
126    {
127  1 when(this.servletContext.getContextPath()).thenReturn("/");
128   
129  1 when(this.servletContext.getServletRegistration("action").getMappings())
130    .thenReturn(Arrays.asList("/*", "/bin/*", "/wiki/*", "/testbin/*"));
131   
132  1 HttpServletRequest req = createMockRequest();
133  1 when(req.getServletPath()).thenReturn("/");
134   
135  1 assertEquals("/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
136    }
137   
 
138  1 toggle @Test
139    public void normalizeFromNonActionRequestUsesDefaultServletMapping() throws Exception
140    {
141  1 when(this.servletContext.getContextPath()).thenReturn("/xwiki");
142   
143  1 HttpServletRequest req = createMockRequest();
144  1 when(req.getServletPath()).thenReturn("/rest");
145   
146  1 assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
147    }
148   
 
149  1 toggle @Test
150    public void normalizePreservesParameters() throws Exception
151    {
152  1 when(this.servletContext.getContextPath()).thenReturn("/xwiki");
153   
154  1 Map<String, List<String>> params = new HashMap<>();
155  1 params.put("age", Arrays.asList("32"));
156  1 params.put("colors", Arrays.asList("red", "blue"));
157  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"), params);
158   
159  1 assertSame(params, this.mocker.getComponentUnderTest().normalize(extendedURL).getParameters());
160    }
161   
 
162  3 toggle private HttpServletRequest createMockRequest()
163    {
164  3 ServletRequest request = mock(ServletRequest.class);
165  3 when(this.container.getRequest()).thenReturn(request);
166   
167  3 HttpServletRequest httpRequest = mock(HttpServletRequest.class);
168  3 when(request.getHttpServletRequest()).thenReturn(httpRequest);
169   
170  3 return httpRequest;
171    }
172    }