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

File ExtendedURLTest.java

 

Code metrics

0
53
11
1
167
118
13
0.25
4.82
11
1.18

Classes

Class Line # Actions
ExtendedURLTest 42 53 0% 13 2
0.9687596.9%
 

Contributing tests

This file is covered by 11 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;
21   
22    import java.net.URI;
23    import java.net.URL;
24    import java.util.Arrays;
25    import java.util.LinkedHashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.junit.Test;
30    import org.xwiki.resource.CreateResourceReferenceException;
31   
32    import static org.hamcrest.CoreMatchers.hasItem;
33    import static org.hamcrest.CoreMatchers.hasItems;
34    import static org.junit.Assert.*;
35   
36    /**
37    * Unit tests for {@link ExtendedURL}.
38    *
39    * @version $Id: 8072bed30aee3cd69ee7918a375ee074b7e5dcce $
40    * @since 5.1M1
41    */
 
42    public class ExtendedURLTest
43    {
 
44  1 toggle @Test
45    public void withoutPrefix() throws Exception
46    {
47  1 URL url = new URL("http://localhost:8080/some/path");
48  1 ExtendedURL extendedURL = new ExtendedURL(url, null);
49  1 assertEquals(new URI("http://localhost:8080/some/path"), extendedURL.getURI());
50  1 assertEquals(2, extendedURL.getSegments().size());
51  1 assertThat(extendedURL.getSegments(), hasItems("some", "path"));
52    }
53   
 
54  1 toggle @Test
55    public void withPrefix() throws Exception
56    {
57  1 URL url = new URL("http://localhost:8080/xwiki/path");
58  1 ExtendedURL extendedURL = new ExtendedURL(url, "xwiki");
59  1 assertEquals(new URI("http://localhost:8080/xwiki/path"), extendedURL.getURI());
60  1 assertEquals(1, extendedURL.getSegments().size());
61  1 assertThat(extendedURL.getSegments(), hasItems("path"));
62    }
63   
 
64  1 toggle @Test
65    public void withPrefixAndNoSlashAfter() throws Exception
66    {
67  1 URL url = new URL("http://localhost:8080/xwiki");
68  1 ExtendedURL extendedURL = new ExtendedURL(url, "xwiki");
69  1 assertEquals(new URI("http://localhost:8080/xwiki"), extendedURL.getURI());
70  1 assertEquals(0, extendedURL.getSegments().size());
71  1 assertTrue(extendedURL.getSegments().isEmpty());
72    }
73   
 
74  1 toggle @Test
75    public void withPrefixStartingWithSlash() throws Exception
76    {
77  1 URL url = new URL("http://localhost:8080/xwiki/path");
78  1 ExtendedURL extendedURL = new ExtendedURL(url, "/xwiki");
79  1 assertEquals(new URI("http://localhost:8080/xwiki/path"), extendedURL.getURI());
80  1 assertEquals(1, extendedURL.getSegments().size());
81  1 assertThat(extendedURL.getSegments(), hasItems("path"));
82    }
83   
 
84  1 toggle @Test
85    public void withPrefixStartingWithSlashAndNoSlashAfter() throws Exception
86    {
87  1 URL url = new URL("http://localhost:8080/xwiki");
88  1 ExtendedURL extendedURL = new ExtendedURL(url, "/xwiki");
89  1 assertEquals(new URI("http://localhost:8080/xwiki"), extendedURL.getURI());
90  1 assertEquals(0, extendedURL.getSegments().size());
91  1 assertTrue(extendedURL.getSegments().isEmpty());
92    }
93   
 
94  1 toggle @Test
95    public void withInvalidPrefix() throws Exception
96    {
97  1 URL url = new URL("http://localhost:8080/some/path");
98  1 try {
99  1 new ExtendedURL(url, "/xwiki");
100  0 fail("Should have thrown an exception here");
101    } catch (CreateResourceReferenceException e) {
102  1 assertEquals("URL Path [/some/path] doesn't start with [/xwiki]", e.getMessage());
103    }
104    }
105   
 
106  1 toggle @Test
107    public void equality() throws Exception
108    {
109  1 ExtendedURL extendedURL1 = new ExtendedURL(new URL("http://localhost:8080/some/path"), null);
110  1 ExtendedURL extendedURL2 = new ExtendedURL(new URL("http://localhost:8080/some/path"), null);
111  1 assertEquals(extendedURL1, extendedURL2);
112    }
113   
 
114  1 toggle @Test
115    public void invalidURL() throws Exception
116    {
117  1 try {
118    // Invalid URL since the space in the page name isn't encoded.
119  1 new ExtendedURL(new URL("http://host/xwiki/bin/view/space/page name"), null);
120  0 fail("Should have thrown an exception here");
121    } catch (CreateResourceReferenceException expected) {
122  1 assertEquals("Invalid URL [http://host/xwiki/bin/view/space/page name]", expected.getMessage());
123    }
124    }
125   
 
126  1 toggle @Test
127    public void withEncodedChars() throws Exception
128    {
129  1 ExtendedURL extendedURL =
130    new ExtendedURL(new URL("http://host/xwiki/bin/view/space/page%20name?param=%2D"), null);
131  1 assertThat(extendedURL.getSegments(), hasItem("page name"));
132  1 assertEquals("param=-", extendedURL.getURI().getQuery());
133    }
134   
135    /**
136    * Verify we ignore path parameters (see section 3.3 of the RFC 2396: http://www.faqs.org/rfcs/rfc2396.html).
137    */
 
138  1 toggle @Test
139    public void ignoreSpecialPathParameters() throws Exception
140    {
141  1 ExtendedURL extendedURL = new ExtendedURL(
142    new URL("http://host/xwiki/bin/view/space;param1=value1/page;param2=value2"), "xwiki");
143  1 assertThat(extendedURL.getSegments(), hasItem("space"));
144  1 assertThat(extendedURL.getSegments(), hasItem("page"));
145   
146    // Ensure we don't remove ";" when they are encoded in order to allow the ";" character to be in document names
147    // for example.
148  1 extendedURL = new ExtendedURL(new URL("http://host/xwiki/bin/view/space/my%3Bpage"), "xwiki");
149  1 assertThat(extendedURL.getSegments(), hasItem("my;page"));
150    }
151   
 
152  1 toggle @Test
153    public void serialize() throws Exception
154    {
155    // Without parameters
156  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "with/slash", "with space"));
157  1 assertEquals("/one/with%2Fslash/with+space", extendedURL.serialize());
158  1 assertEquals("/one/with%2Fslash/with+space", extendedURL.toString());
159   
160    // With parameters
161  1 Map<String, List<String>> parameters = new LinkedHashMap<>();
162  1 parameters.put("key1", Arrays.asList("value1"));
163  1 parameters.put("key2", Arrays.asList("value2", "value3"));
164  1 extendedURL = new ExtendedURL(Arrays.asList("one", "with/slash", "with space"), parameters);
165  1 assertEquals("/one/with%2Fslash/with+space?key1=value1&key2=value2&key2=value3", extendedURL.serialize());
166    }
167    }