1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.url.internal.standard

File ContextAndActionURLNormalizerTest.java

 

Code metrics

0
49
11
1
198
135
12
0.24
4.45
11
1.09

Classes

Class Line # Actions
ContextAndActionURLNormalizerTest 57 49 0% 12 1
0.9833333598.3%
 

Contributing tests

This file is covered by 9 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.standard;
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.configuration.ConfigurationSource;
35    import org.xwiki.container.Container;
36    import org.xwiki.container.servlet.ServletRequest;
37    import org.xwiki.environment.Environment;
38    import org.xwiki.environment.internal.ServletEnvironment;
39    import org.xwiki.model.ModelContext;
40    import org.xwiki.model.reference.WikiReference;
41    import org.xwiki.test.mockito.MockitoComponentMockingRule;
42    import org.xwiki.url.ExtendedURL;
43    import org.xwiki.url.URLNormalizer;
44   
45    import static org.junit.Assert.assertEquals;
46    import static org.junit.Assert.assertSame;
47    import static org.junit.Assert.fail;
48    import static org.mockito.Mockito.mock;
49    import static org.mockito.Mockito.when;
50   
51    /**
52    * Unit tests for {@link org.xwiki.url.internal.stadard.ContextAndActionURLNormalizer}.
53    *
54    * @version $Id: 8eba2ff6f513b4325772b1d61ac3c87a293224f3 $
55    * @since 7.4M1
56    */
 
57    public class ContextAndActionURLNormalizerTest
58    {
59    private ServletEnvironment environment;
60   
61    private Container container;
62   
63    private ServletContext servletContext;
64   
65    private ConfigurationSource xwikiCfg;
66   
67    private ModelContext modelContext;
68   
69    private StandardURLConfiguration urlConfiguration;
70   
71    private ExtendedURL testURL = new ExtendedURL(Arrays.asList("one", "two"));
72   
73    @Rule
74    public MockitoComponentMockingRule<URLNormalizer<ExtendedURL>> mocker =
75    new MockitoComponentMockingRule<URLNormalizer<ExtendedURL>>(ContextAndActionURLNormalizer.class);
76   
 
77  9 toggle @Before
78    public void configure() throws Exception
79    {
80    // Configure the super class
81  9 this.container = this.mocker.getInstance(Container.class);
82  9 this.environment = mock(ServletEnvironment.class);
83  9 this.mocker.registerComponent(Environment.class, this.environment);
84  9 this.servletContext = mock(ServletContext.class);
85  9 when(this.environment.getServletContext()).thenReturn(this.servletContext);
86  9 ServletRegistration sr = mock(ServletRegistration.class);
87  9 when(this.servletContext.getServletRegistration("action")).thenReturn(sr);
88  9 when(sr.getMappings()).thenReturn(Arrays.asList("/bin/*", "/wiki/*", "/testbin/*"));
89   
90    // Configure the tested class
91  9 this.xwikiCfg = this.mocker.getInstance(ConfigurationSource.class, "xwikicfg");
92  9 this.modelContext = this.mocker.getInstance(ModelContext.class);
93  9 this.urlConfiguration = this.mocker.getInstance(StandardURLConfiguration.class);
94  9 when(this.urlConfiguration.getEntityPathPrefix()).thenReturn("bin");
95  9 when(this.urlConfiguration.getWikiPathPrefix()).thenReturn("wiki");
96    }
97   
 
98  1 toggle @Test
99    public void normalizeWithNoKnownContextPathThrowsException() throws Exception
100    {
101  1 this.mocker.registerMockComponent(Environment.class);
102  1 try {
103  1 this.mocker.getComponentUnderTest().normalize(this.testURL);
104  0 fail("Should have thrown an exception");
105    } catch (RuntimeException expected) {
106  1 assertEquals("Failed to normalize the URL [/one/two] since the application's Servlet context couldn't be "
107    + "computed.", expected.getMessage());
108    }
109    }
110   
 
111  1 toggle @Test
112    public void normalizeUsesTheSpecifiedConfiguration() throws Exception
113    {
114  1 when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("good");
115  1 when(this.servletContext.getContextPath()).thenReturn("/bad");
116   
117  1 assertEquals("/good/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
118    }
119   
 
120  1 toggle @Test
121    public void normalizeRemovesLeadingAndTrailingSlashFromConfiguration() throws Exception
122    {
123  1 when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("/xwiki/");
124   
125  1 assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
126    }
127   
 
128  1 toggle @Test
129    public void normalizeWithRootConfiguration() throws Exception
130    {
131  1 when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("");
132  1 when(this.servletContext.getContextPath()).thenReturn("/bad");
133   
134  1 assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
135    }
136   
 
137  1 toggle @Test
138    public void normalizeWithSlashRootConfiguration() throws Exception
139    {
140  1 when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("/");
141  1 when(this.servletContext.getContextPath()).thenReturn("/bad");
142   
143  1 assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
144    }
145   
 
146  1 toggle @Test
147    public void normalizeWithNoConfigurationUsesServletContext() throws Exception
148    {
149  1 when(this.servletContext.getContextPath()).thenReturn("/xwiki");
150   
151  1 assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
152    }
153   
 
154  1 toggle @Test
155    public void normalizeWithNoConfigurationAndRootServletContext() throws Exception
156    {
157  1 when(this.servletContext.getContextPath()).thenReturn("");
158   
159  1 assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
160    }
161   
 
162  1 toggle @Test
163    public void normalizeFromVirtualWikiRequestPreservesWikiPath() throws Exception
164    {
165  1 when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("xwiki");
166   
167  1 HttpServletRequest req = createMockRequest();
168  1 when(req.getServletPath()).thenReturn("/wiki");
169  1 when(this.modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("dev"));
170   
171  1 assertEquals("/xwiki/wiki/dev/one/two",
172    this.mocker.getComponentUnderTest().normalize(this.testURL).serialize());
173    }
174   
 
175  1 toggle @Test
176    public void normalizePreservesParameters() throws Exception
177    {
178  1 when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("xwiki");
179   
180  1 Map<String, List<String>> params = new HashMap<>();
181  1 params.put("age", Arrays.asList("32"));
182  1 params.put("colors", Arrays.asList("red", "blue"));
183  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"), params);
184   
185  1 assertSame(params, this.mocker.getComponentUnderTest().normalize(extendedURL).getParameters());
186    }
187   
 
188  1 toggle private HttpServletRequest createMockRequest()
189    {
190  1 ServletRequest request = mock(ServletRequest.class);
191  1 when(this.container.getRequest()).thenReturn(request);
192   
193  1 HttpServletRequest httpRequest = mock(HttpServletRequest.class);
194  1 when(request.getHttpServletRequest()).thenReturn(httpRequest);
195   
196  1 return httpRequest;
197    }
198    }