View Javadoc
1   
2   package org.apache.maven.doxia.site.decoration.inheritance;
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  import java.net.URI;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   *
29   * @author ltheussl
30   *
31   * @since 1.2
32   */
33  public class URIPathDescriptorTest
34      extends TestCase
35  {
36      private static final String BASE_URL = "http://maven.apache.org/";
37  
38      /**
39       * Test of constructor, of class URIPathDescriptor.
40       *
41       * @throws Exception
42       */
43      public void testConstructor()
44          throws Exception
45      {
46          final String expected = BASE_URL + "doxia";
47  
48          final URIPathDescriptor path = new URIPathDescriptor( BASE_URL, "doxia" );
49          assertEquals( expected, path.toString() );
50          assertEquals( BASE_URL, path.getBaseURI().toString() );
51          assertEquals( "doxia", path.getLink().toString() );
52  
53          URIPathDescriptor compare = new URIPathDescriptor( "http://maven.apache.org", "/doxia" );
54          assertEquals( expected, compare.toString() );
55  
56          compare = new URIPathDescriptor( "http://maven.apache.org/./doxia/../", "/sub/./sub/../../doxia" );
57          assertEquals( expected, compare.toString() );
58  
59          compare = new URIPathDescriptor( "http://maven.apache.org/doxia", "" );
60          assertEquals( expected + "/", compare.toString() );
61  
62          compare = new URIPathDescriptor( "file:///C:\\Foo\\bar1", "" );
63          assertEquals( "file:///C:/Foo/bar1/", compare.getBaseURI().toString() );
64          // toString() calls resolve() which removes two slashes because authority is empty
65          assertEquals( "file:/C:/Foo/bar1/", compare.toString() );
66  
67          compare = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/foo/", "bar" );
68          assertEquals( "file:/C:/Documents%20and%20Settings/foo/bar", compare.toString() );
69  
70          compare = new URIPathDescriptor( "file:////Users/", "user" );
71          assertEquals( "file:/Users/user", compare.toString() );
72  
73          compare = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/foo/", "bar" );
74          assertEquals( "file:/C:/Documents%20and%20Settings/foo/bar", compare.toString() );
75  
76          compare = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/foo/", "bar" );
77          // toString() calls resolve() which removes the colon if port is empty, C is the host here!
78          assertEquals( "file://C/Documents%20and%20Settings/foo/bar", compare.toString() );
79  
80          compare = new URIPathDescriptor( "file://C:8080/Documents%20and%20Settings/foo/", "bar" );
81          assertEquals( "file://C:8080/Documents%20and%20Settings/foo/bar", compare.toString() );
82  
83          compare = new URIPathDescriptor( "C:\\Foo\\bar", "bar" );
84          assertEquals( "C:/Foo/bar/bar", compare.toString() ); // NOTE: C: is the scheme here!
85  
86          assertFailure( "/doxia", BASE_URL );
87          assertFailure( "file:///C:/Documents and Settings/foo/", "bar" );
88      }
89  
90      /**
91       * Test of resolveLink method, of class URIPathDescriptor.
92       *
93       * @throws Exception
94       */
95      public void testResolveLink()
96          throws Exception
97      {
98          final String expected = BASE_URL + "source";
99  
100         URIPathDescriptor oldPath = new URIPathDescriptor( BASE_URL, "source" );
101         assertEquals( expected, oldPath.resolveLink().toString() );
102 
103         oldPath = new URIPathDescriptor( BASE_URL, "source/" );
104         assertEquals( expected + "/", oldPath.resolveLink().toString() );
105 
106         oldPath = new URIPathDescriptor( BASE_URL, "/source" );
107         assertEquals( expected, oldPath.resolveLink().toString() );
108 
109         oldPath = new URIPathDescriptor( "http://maven.apache.org", "source" );
110         assertEquals( expected, oldPath.resolveLink().toString() );
111 
112         oldPath = new URIPathDescriptor( BASE_URL, "source/index.html" );
113         assertEquals( expected + "/index.html", oldPath.resolveLink().toString() );
114 
115         oldPath = new URIPathDescriptor( BASE_URL, "source/index.html?var=foo&var2=bar" );
116         assertEquals( expected + "/index.html?var=foo&var2=bar", oldPath.resolveLink().toString() );
117 
118         oldPath = new URIPathDescriptor( "file:////Users/", "user" );
119         assertEquals( "file:/Users/user", oldPath.resolveLink().toString() );
120 
121         oldPath = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "source" );
122         // resolve() removes two slashes because authority is empty
123         assertEquals( "file:/C:/Documents%20and%20Settings/source", oldPath.resolveLink().toString() );
124 
125         oldPath = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/", "source" );
126         // resolve() removes the colon if port is empty
127         assertEquals( "file://C/Documents%20and%20Settings/source", oldPath.resolveLink().toString() );
128 
129         oldPath = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/", "source" );
130         assertEquals( "file:/C:/Documents%20and%20Settings/source", oldPath.resolveLink().toString() );
131     }
132 
133     /**
134      * Test of rebaseLink method, of class URIPathDescriptor.
135      *
136      * @throws Exception
137      */
138     public void testRebaseLink()
139         throws Exception
140     {
141         URIPathDescriptor oldPath = new URIPathDescriptor( BASE_URL, "source" );
142         assertEquals( "../source", oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
143         assertEquals( "http://maven.apache.org/source", oldPath.rebaseLink( null ).toString() );
144         assertEquals( "http://maven.apache.org/source", oldPath.rebaseLink( "C:/Documents and Settings/" ).toString() );
145 
146         oldPath = new URIPathDescriptor( BASE_URL, "./" );
147         assertEquals( "", oldPath.rebaseLink( "http://maven.apache.org/" ).toString() );
148 
149         oldPath = new URIPathDescriptor( BASE_URL, "" );
150         assertEquals( "", oldPath.rebaseLink( "http://maven.apache.org/" ).toString() );
151 
152         oldPath = new URIPathDescriptor( BASE_URL, "source/index.html" );
153         assertEquals( "../source/index.html", oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
154 
155         oldPath = new URIPathDescriptor( BASE_URL, "source/index.html?var=foo&var2=bar" );
156         assertEquals( "../source/index.html?var=foo&var2=bar",
157             oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
158 
159         oldPath = new URIPathDescriptor( "scp://people.apache.org/", "source" );
160         assertEquals( "../source", oldPath.rebaseLink( "scp://people.apache.org/doxia" ).toString() );
161 
162         oldPath = new URIPathDescriptor( BASE_URL, "banner/left" );
163         assertEquals( "../banner/left", oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
164 
165         oldPath = new URIPathDescriptor( BASE_URL, "index.html?var=foo&var2=bar" );
166         assertEquals( "../index.html?var=foo&var2=bar",
167             oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
168 
169         oldPath = new URIPathDescriptor( "http://jakarta.apache.org/", "banner/left" );
170         assertEquals( "http://jakarta.apache.org/banner/left", oldPath.rebaseLink( BASE_URL ).toString() );
171 
172         oldPath = new URIPathDescriptor( "file:////Users/", "user" );
173         assertEquals( "../user", oldPath.rebaseLink( "file:////Users/target" ).toString() );
174         assertEquals( "../user", oldPath.rebaseLink( "file:/Users/target" ).toString() );
175 
176         oldPath = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "source" );
177         assertEquals( "../source", oldPath.rebaseLink( "file:///C:/Documents%20and%20Settings/target" ).toString() );
178 
179         oldPath = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/", "source" );
180         assertEquals( "../source", oldPath.rebaseLink( "file://C:/Documents%20and%20Settings/target" ).toString() );
181 
182         oldPath = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/", "source" );
183         assertEquals( "../source", oldPath.rebaseLink( "file:/C:/Documents%20and%20Settings/target" ).toString() );
184     }
185 
186     /**
187      * Test of relativizeLink method, of class URIPathDescriptor.
188      *
189      * @throws Exception
190      */
191     public void testRelativizeLink()
192         throws Exception
193     {
194         URIPathDescriptor path = new URIPathDescriptor( BASE_URL, "source" );
195         assertEquals( "source", path.relativizeLink().toString() );
196 
197         path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/source" );
198         assertEquals( "source", path.relativizeLink().toString() );
199 
200         path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/" );
201         assertEquals( "./", path.relativizeLink().toString() );
202 
203         path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org" );
204         assertEquals( "./", path.relativizeLink().toString() );
205 
206         path = new URIPathDescriptor( "http://maven.apache.org", BASE_URL );
207         assertEquals( "./", path.relativizeLink().toString() );
208 
209         path = new URIPathDescriptor( "http://maven.apache.org", "http://maven.apache.org" );
210         assertEquals( "./", path.relativizeLink().toString() );
211 
212         path = new URIPathDescriptor( "http://maven.apache.org/doxia/", "http://maven.apache.org/source/" );
213         assertEquals( "../source/", path.relativizeLink().toString() );
214 
215         path = new URIPathDescriptor( "http://maven.apache.org/doxia", "http://maven.apache.org/source" );
216         assertEquals( "../source", path.relativizeLink().toString() );
217 
218         path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/index.html" );
219         assertEquals( "index.html", path.relativizeLink().toString() );
220 
221         path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/index.html?var=foo&var2=bar" );
222         assertEquals( "index.html?var=foo&var2=bar", path.relativizeLink().toString() );
223 
224         path = new URIPathDescriptor( "file:////Users/", "index.html" );
225         assertEquals( "index.html", path.relativizeLink().toString() );
226 
227         path = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "index.html" );
228         assertEquals( "index.html", path.relativizeLink().toString() );
229 
230         path = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/", "index.html" );
231         assertEquals( "index.html", path.relativizeLink().toString() );
232 
233         path = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/", "index.html" );
234         assertEquals( "index.html", path.relativizeLink().toString() );
235     }
236 
237     /**
238      * Test of sameSite method, of class URIPathDescriptor.
239      *
240      * @throws Exception
241      */
242     public void testSameSite()
243         throws Exception
244     {
245         final URIPathDescriptor path = new URIPathDescriptor( BASE_URL, "doxia" );
246 
247         assertTrue( path.sameSite( new URI( "http://maven.apache.org/" ) ) );
248         assertTrue( path.sameSite( new URI( "http://maven.apache.org" ) ) );
249         assertTrue( path.sameSite( new URI( "HTTP://maven.apache.org/" ) ) );
250         assertTrue( path.sameSite( new URI( "http://MAVEN.apache.org/" ) ) );
251         assertTrue( path.sameSite( new URI( "http://maven.apache.org/wagon/index.html" ) ) );
252 
253         assertFalse( path.sameSite( null ) );
254         assertFalse( path.sameSite( new URI( "https://maven.apache.org/" ) ) );
255         assertFalse( path.sameSite( new URI( "http://ant.apache.org/" ) ) );
256         assertFalse( path.sameSite( new URI( "http://maven.apache.org:80" ) ) );
257         assertFalse( path.sameSite( new URI( "/usr/share/bin/" ) ) );
258         assertFalse( path.sameSite( new URI( "http:///maven.apache.org/" ) ) );
259 
260         final URIPathDescriptor nullHost = new URIPathDescriptor( "http:///maven.apache.org/", "doxia" );
261         assertTrue( nullHost.sameSite( new URI( "http:///maven.apache.org/" ) ) );
262         assertFalse( nullHost.sameSite( new URI( "http://maven.apache.org/" ) ) );
263 
264         URIPathDescriptor newPath = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "source" );
265         assertTrue( newPath.sameSite( new URI( "file:///C:/Documents%20and%20Settings/" ) ) );
266         assertFalse( newPath.sameSite( new URI( "file://C:/Documents%20and%20Settings/" ) ) );
267         // authority is empty
268         assertTrue( newPath.sameSite( new URI( "file:/C:/Documents%20and%20Settings/" ) ) );
269     }
270 
271     private static void assertFailure( final String base, final String link )
272     {
273         try
274         {
275             final URIPathDescriptor test = new URIPathDescriptor( base, link );
276             fail( "Should fail: " + test.toString() );
277         }
278         catch ( IllegalArgumentException ex )
279         {
280             assertNotNull( ex );
281         }
282     }
283 }