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

File ExtendedURLURLNormalizerTest.java

 

Code metrics

0
35
8
1
143
97
9
0.26
4.38
8
1.12

Classes

Class Line # Actions
ExtendedURLURLNormalizerTest 51 35 0% 9 1
0.976744297.7%
 

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   
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.configuration.ConfigurationSource;
33    import org.xwiki.environment.Environment;
34    import org.xwiki.environment.internal.ServletEnvironment;
35    import org.xwiki.test.mockito.MockitoComponentMockingRule;
36    import org.xwiki.url.ExtendedURL;
37    import org.xwiki.url.URLNormalizer;
38   
39    import static org.junit.Assert.assertEquals;
40    import static org.junit.Assert.assertSame;
41    import static org.junit.Assert.fail;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.when;
44   
45    /**
46    * Unit tests for {@link org.xwiki.url.internal.container.ExtendedURLURLNormalizer}.
47    *
48    * @version $Id: 7b7e5efc07b6eed681205472b7016ebe9a4d328b $
49    * @since 6.1M2
50    */
 
51    public class ExtendedURLURLNormalizerTest
52    {
53    private ConfigurationSource configurationSource;
54   
55    @Rule
56    public MockitoComponentMockingRule<URLNormalizer<ExtendedURL>> mocker =
57    new MockitoComponentMockingRule<URLNormalizer<ExtendedURL>>(ExtendedURLURLNormalizer.class);
58   
 
59  7 toggle @Before
60    public void configure() throws Exception
61    {
62  7 this.configurationSource = this.mocker.getInstance(ConfigurationSource.class, "xwikicfg");
63    }
64   
 
65  1 toggle @Test
66    public void normalizeWhenConfigurationPropertyDefined() throws Exception
67    {
68  1 when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn("xwiki");
69   
70  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
71  1 assertEquals("/xwiki/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize());
72    }
73   
 
74  1 toggle @Test
75    public void normalizeWhenConfigurationPropertyDefinedButWithLeadingAndTrailingSlash() throws Exception
76    {
77  1 when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn("/xwiki/");
78   
79  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
80  1 assertEquals("/xwiki/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize());
81    }
82   
 
83  1 toggle @Test
84    public void normalizeWhenNoConfigurationPropertyButEnvironment() throws Exception
85    {
86  1 ServletContext sc = mock(ServletContext.class);
87  1 ServletEnvironment environment = mock(ServletEnvironment.class);
88  1 this.mocker.registerComponent(Environment.class, environment);
89  1 when(environment.getServletContext()).thenReturn(sc);
90  1 when(sc.getContextPath()).thenReturn("/xwiki");
91   
92  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
93  1 assertEquals("/xwiki/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize());
94    }
95   
 
96  1 toggle @Test
97    public void normalizeWhenNoConfigurationPropertyButEnvironmentWithRootContext() throws Exception
98    {
99  1 ServletContext sc = mock(ServletContext.class);
100  1 ServletEnvironment environment = mock(ServletEnvironment.class);
101  1 this.mocker.registerComponent(Environment.class, environment);
102  1 when(environment.getServletContext()).thenReturn(sc);
103  1 when(sc.getContextPath()).thenReturn("");
104   
105  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
106  1 assertEquals("/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize());
107    }
108   
 
109  1 toggle @Test
110    public void normalizeWhenNoConfigurationPropertyAndNoServletEnvironment() throws Exception
111    {
112  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
113  1 try {
114  1 this.mocker.getComponentUnderTest().normalize(extendedURL);
115  0 fail("Should have thrown an exception");
116    } catch (RuntimeException expected) {
117  1 assertEquals("Failed to normalize the URL [/one/two] since the application's Servlet context couldn't be "
118    + "computed.", expected.getMessage());
119    }
120    }
121   
 
122  1 toggle @Test
123    public void normalizeExtendedURLWithParameters() throws Exception
124    {
125  1 when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn("xwiki");
126   
127  1 Map<String, List<String>> params = new HashMap<>();
128  1 params.put("age", Arrays.asList("32"));
129  1 params.put("colors", Arrays.asList("red", "blue"));
130   
131  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"), params);
132  1 assertSame(params, this.mocker.getComponentUnderTest().normalize(extendedURL).getParameters());
133    }
134   
 
135  1 toggle @Test
136    public void normalizeWhenRootWebapp() throws Exception
137    {
138  1 when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn("");
139   
140  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
141  1 assertEquals("/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize());
142    }
143    }