View Javadoc
1   package org.apache.maven.doxia.site.decoration.inheritance;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.codehaus.plexus.util.Os;
25  import org.codehaus.plexus.util.StringUtils;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Test the PathDescriptor creation under various circumstances.
31   *
32   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
33   */
34  public class PathDescriptorTest
35      extends TestCase
36  {
37      /** @throws Exception */
38      public void testAbsPath()
39          throws Exception
40      {
41          String path = "absolutePath";
42  
43          PathDescriptor desc = new PathDescriptor( "/" + path );
44  
45          assertTrue( desc.isFile() );
46          assertTrue( desc.isRelative() );
47          assertNull( desc.getBaseUrl() );
48          assertNull( desc.getPathUrl() );
49          assertNotNull( desc.getPath() );
50          assertNotNull( desc.getLocation() );
51          assertEquals( "wrong path", path, desc.getPath() );
52          assertEquals( "wrong location", path, desc.getLocation() );
53      }
54  
55      /** @throws Exception */
56      public void testRelPath()
57          throws Exception
58      {
59          String path = "relativePath";
60  
61          PathDescriptor desc = new PathDescriptor( path );
62  
63          assertTrue( desc.isFile() );
64          assertTrue( desc.isRelative() );
65          assertNull( desc.getBaseUrl() );
66          assertNull( desc.getPathUrl() );
67          assertNotNull( desc.getPath() );
68          assertNotNull( desc.getLocation() );
69          assertEquals( "wrong path", path, desc.getPath() );
70          assertEquals( "wrong location", path, desc.getLocation() );
71      }
72  
73      /** @throws Exception */
74      public void testEmptyAbsPath()
75          throws Exception
76      {
77          String path = "";
78  
79          PathDescriptor desc = new PathDescriptor( "/" + path );
80  
81          assertTrue( desc.isFile() );
82          assertTrue( desc.isRelative() );
83          assertNull( desc.getBaseUrl() );
84          assertNull( desc.getPathUrl() );
85          assertNotNull( desc.getPath() );
86          assertNotNull( desc.getLocation() );
87          assertEquals( "wrong path", path, desc.getPath() );
88          assertEquals( "wrong location", path, desc.getLocation() );
89      }
90  
91      /** @throws Exception */
92      public void testEmptyRelPath()
93          throws Exception
94      {
95          String path = "";
96  
97          PathDescriptor desc = new PathDescriptor( path );
98  
99          assertTrue( desc.isFile() );
100         assertTrue( desc.isRelative() );
101         assertNull( desc.getBaseUrl() );
102         assertNull( desc.getPathUrl() );
103         assertNotNull( desc.getPath() );
104         assertNotNull( desc.getLocation() );
105         assertEquals( "wrong path", path, desc.getPath() );
106         assertEquals( "wrong location", path, desc.getLocation() );
107     }
108 
109     /** @throws Exception */
110     public void testNullPath()
111         throws Exception
112     {
113         String path = null;
114 
115         PathDescriptor desc = new PathDescriptor( path );
116 
117         assertTrue( desc.isFile() );
118         assertTrue( desc.isRelative() );
119         assertNull( desc.getBaseUrl() );
120         assertNull( desc.getPathUrl() );
121         assertNull( desc.getPath() );
122         assertNull( desc.getLocation() );
123         assertEquals( "wrong path", path, desc.getPath() );
124         assertEquals( "wrong location", path, desc.getLocation() );
125     }
126 
127     /** @throws Exception */
128     public void testNullBaseAbsPath()
129         throws Exception
130     {
131         String base = null;
132         String path = "absolutePath";
133 
134         PathDescriptor desc = new PathDescriptor( base, "/" + path );
135 
136         assertTrue( desc.isFile() );
137         assertTrue( desc.isRelative() );
138         assertNull( desc.getBaseUrl() );
139         assertNull( desc.getPathUrl() );
140         assertNotNull( desc.getPath() );
141         assertNotNull( desc.getLocation() );
142         assertEquals( "wrong path", path, desc.getPath() );
143         assertEquals( "wrong location", path, desc.getLocation() );
144     }
145 
146     /** @throws Exception */
147     public void testNullBaseRelPath()
148         throws Exception
149     {
150         String base = null;
151         String path = "relativePath";
152 
153         PathDescriptor desc = new PathDescriptor( base, path );
154 
155         assertTrue( desc.isFile() );
156         assertTrue( desc.isRelative() );
157         assertNull( desc.getBaseUrl() );
158         assertNull( desc.getPathUrl() );
159         assertNotNull( desc.getPath() );
160         assertNotNull( desc.getLocation() );
161         assertEquals( "wrong path", path, desc.getPath() );
162         assertEquals( "wrong location", path, desc.getLocation() );
163     }
164 
165     /** @throws Exception */
166     public void testNullBaseEmptyAbsPath()
167         throws Exception
168     {
169         String base = null;
170         String path = "";
171 
172         PathDescriptor desc = new PathDescriptor( base, "/" + path );
173 
174         assertTrue( desc.isFile() );
175         assertTrue( desc.isRelative() );
176         assertNull( desc.getBaseUrl() );
177         assertNull( desc.getPathUrl() );
178         assertNotNull( desc.getPath() );
179         assertNotNull( desc.getLocation() );
180         assertEquals( "wrong path", path, desc.getPath() );
181         assertEquals( "wrong location", path, desc.getLocation() );
182     }
183 
184     /** @throws Exception */
185     public void testNullBaseEmptyRelPath()
186         throws Exception
187     {
188         String base = null;
189         String path = "";
190 
191         PathDescriptor desc = new PathDescriptor( base, path );
192 
193         assertTrue( desc.isFile() );
194         assertTrue( desc.isRelative() );
195         assertNull( desc.getBaseUrl() );
196         assertNull( desc.getPathUrl() );
197         assertNotNull( desc.getPath() );
198         assertNotNull( desc.getLocation() );
199         assertEquals( "wrong path", path, desc.getPath() );
200         assertEquals( "wrong location", path, desc.getLocation() );
201     }
202 
203     /** @throws Exception */
204     public void testNullBaseNullPath()
205         throws Exception
206     {
207         String base = null;
208         String path = null;
209 
210         PathDescriptor desc = new PathDescriptor( base, path );
211 
212         assertTrue( desc.isFile() );
213         assertTrue( desc.isRelative() );
214         assertNull( desc.getBaseUrl() );
215         assertNull( desc.getPathUrl() );
216         assertNull( desc.getPath() );
217         assertNull( desc.getLocation() );
218         assertEquals( "wrong path", path, desc.getPath() );
219         assertEquals( "wrong location", path, desc.getLocation() );
220     }
221 
222     /** @throws Exception */
223     public void testUrlBaseAbsPath()
224         throws Exception
225     {
226         String base = "http://maven.apache.org/";
227         String path = "absolutePath";
228 
229         PathDescriptor desc = new PathDescriptor( base, "/" + path );
230 
231         assertFalse( desc.isFile() );
232         assertFalse( desc.isRelative() );
233         assertNotNull( desc.getBaseUrl() );
234         assertNotNull( desc.getPathUrl() );
235         assertNotNull( desc.getPath() );
236         assertNotNull( desc.getLocation() );
237         assertEquals( "wrong path", "/" + path, desc.getPath() );
238         assertEquals( "wrong location", base + path, desc.getLocation() );
239     }
240 
241     /** @throws Exception */
242     public void testUrlBaseRelPath()
243         throws Exception
244     {
245         String base = "http://maven.apache.org/";
246         String path = "relativePath";
247 
248         PathDescriptor desc = new PathDescriptor( base, path );
249 
250         assertFalse( desc.isFile() );
251         assertFalse( desc.isRelative() );
252         assertNotNull( desc.getBaseUrl() );
253         assertNotNull( desc.getPathUrl() );
254         assertNotNull( desc.getPath() );
255         assertNotNull( desc.getLocation() );
256         assertEquals( "wrong path", "/" + path, desc.getPath() );
257         assertEquals( "wrong location", base + path, desc.getLocation() );
258     }
259 
260     /** @throws Exception */
261     public void testUrlBaseEmptyAbsPath()
262         throws Exception
263     {
264         String base = "http://maven.apache.org/";
265         String path = "";
266 
267         PathDescriptor desc = new PathDescriptor( base, "/" + path );
268 
269         assertFalse( desc.isFile() );
270         assertFalse( desc.isRelative() );
271         assertNotNull( desc.getBaseUrl() );
272         assertNotNull( desc.getPathUrl() );
273         assertNotNull( desc.getPath() );
274         assertNotNull( desc.getLocation() );
275         assertEquals( "wrong path", "/" + path, desc.getPath() );
276         assertEquals( "wrong location", base + path, desc.getLocation() );
277     }
278 
279     /** @throws Exception */
280     public void testUrlBaseEmptyRelPath()
281         throws Exception
282     {
283         String base = "http://maven.apache.org/";
284         String path = "";
285 
286         PathDescriptor desc = new PathDescriptor( base, path );
287 
288         assertFalse( desc.isFile() );
289         assertFalse( desc.isRelative() );
290         assertNotNull( desc.getBaseUrl() );
291         assertNotNull( desc.getPathUrl() );
292         assertNotNull( desc.getPath() );
293         assertNotNull( desc.getLocation() );
294         assertEquals( "wrong path", "/" + path, desc.getPath() );
295         assertEquals( "wrong location", base + path, desc.getLocation() );
296     }
297 
298     /** @throws Exception */
299     public void testUrlBaseNullPath()
300         throws Exception
301     {
302         String base = "http://maven.apache.org/";
303         String path = null;
304 
305         PathDescriptor desc = new PathDescriptor( base, path );
306 
307         assertFalse( desc.isFile() );
308         assertFalse( desc.isRelative() );
309         assertNotNull( desc.getBaseUrl() );
310         assertNotNull( desc.getPathUrl() );
311         assertNotNull( desc.getPath() );
312         assertNotNull( desc.getLocation() );
313         assertEquals( "wrong path", "/", desc.getPath() );
314         assertEquals( "wrong location", base, desc.getLocation() );
315     }
316 
317     /** @throws Exception */
318     public void testFileBaseAbsPath()
319         throws Exception
320     {
321         String base = "/tmp/foo";
322         String path = "absolutePath";
323 
324         PathDescriptor desc = new PathDescriptor( "file://" + base, "/" + path );
325 
326         assertTrue( desc.isFile() );
327         assertFalse( desc.isRelative() );
328         assertNotNull( desc.getBaseUrl() );
329         assertNotNull( desc.getPathUrl() );
330         assertNotNull( desc.getPath() );
331         assertNotNull( desc.getLocation() );
332         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
333         {
334             String s = StringUtils.replace( new File( base + "/" + path ).toURI().toURL().toString(), "file:", "" );
335             assertEquals( "wrong path", s, desc.getPath() );
336             assertEquals( "wrong location", s, desc.getLocation() );
337         }
338         else
339         {
340             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
341             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
342         }
343     }
344 
345     /** @throws Exception */
346     public void testFileBaseRelPath()
347         throws Exception
348     {
349         String base = "/tmp/foo";
350         String path = "relativePath";
351 
352         PathDescriptor desc = new PathDescriptor( "file://" + base, path );
353 
354         assertTrue( desc.isFile() );
355         assertFalse( desc.isRelative() );
356         assertNotNull( desc.getBaseUrl() );
357         assertNotNull( desc.getPathUrl() );
358         assertNotNull( desc.getPath() );
359         assertNotNull( desc.getLocation() );
360         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
361         {
362             String s = StringUtils.replace( new File( base + "/" + path ).toURI().toURL().toString(), "file:", "" );
363             assertEquals( "wrong path", s, desc.getPath() );
364             assertEquals( "wrong location", s, desc.getLocation() );
365         }
366         else
367         {
368             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
369             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
370         }
371     }
372 
373     /** @throws Exception */
374     public void testFileBaseEmptyAbsPath()
375         throws Exception
376     {
377         String base = "/tmp/foo";
378         String path = "";
379 
380         PathDescriptor desc = new PathDescriptor( "file://" + base, "/" + path );
381 
382         assertTrue( desc.isFile() );
383         assertFalse( desc.isRelative() );
384         assertNotNull( desc.getBaseUrl() );
385         assertNotNull( desc.getPathUrl() );
386         assertNotNull( desc.getPath() );
387         assertNotNull( desc.getLocation() );
388         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
389         {
390             String s = StringUtils.replace( new File( base ).toURI().toURL().toString(), "file:", "" );
391             assertEquals( "wrong path", s, desc.getPath() );
392             assertEquals( "wrong location", s, desc.getLocation() );
393         }
394         else
395         {
396             assertEquals( "wrong path", base, desc.getPath() );
397             assertEquals( "wrong location", base, desc.getLocation() );
398         }
399     }
400 
401     /** @throws Exception */
402     public void testFileBaseEmptyRelPath()
403         throws Exception
404     {
405         String base = "/tmp/foo";
406         String path = "";
407 
408         PathDescriptor desc = new PathDescriptor( "file://" + base, path );
409 
410         assertTrue( desc.isFile() );
411         assertFalse( desc.isRelative() );
412         assertNotNull( desc.getBaseUrl() );
413         assertNotNull( desc.getPathUrl() );
414         assertNotNull( desc.getPath() );
415         assertNotNull( desc.getLocation() );
416         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
417         {
418             String s = StringUtils.replace( new File( base ).toURI().toURL().toString(), "file:", "" );
419             assertEquals( "wrong path", s, desc.getPath() );
420             assertEquals( "wrong location", s, desc.getLocation() );
421         }
422         else
423         {
424             assertEquals( "wrong path", base, desc.getPath() );
425             assertEquals( "wrong location", base, desc.getLocation() );
426         }
427     }
428 
429     /** @throws Exception */
430     public void testFileBaseNullPath()
431         throws Exception
432     {
433         String base = "/tmp/foo";
434         String path = null;
435 
436         PathDescriptor desc = new PathDescriptor( "file://" + base, path );
437 
438         assertTrue( desc.isFile() );
439         assertFalse( desc.isRelative() );
440         assertNotNull( desc.getBaseUrl() );
441         assertNotNull( desc.getPathUrl() );
442         assertNotNull( desc.getPath() );
443         assertNotNull( desc.getLocation() );
444         assertEquals( "wrong path", base, desc.getPath() );
445         assertEquals( "wrong location", base, desc.getLocation() );
446     }
447 
448 /*
449     // same as testUrlBaseAbsPath with scp, this fails!? DOXIASITETOOLS-47
450     public void testUriBaseAbsPath()
451         throws Exception
452     {
453         String base = "scp://people.apache.org/";
454         String path = "absolutePath";
455 
456         PathDescriptor desc = new PathDescriptor( base, "/" + path );
457 
458         assertFalse( desc.isFile() );
459         assertFalse( desc.isRelative() );
460         assertNotNull( desc.getBaseUrl() );
461         assertNotNull( desc.getPathUrl() );
462         assertNotNull( desc.getPath() );
463         assertNotNull( desc.getLocation() );
464         assertEquals( "wrong path", "/" + path, desc.getPath() );
465         assertEquals( "wrong location", base + path, desc.getLocation() );
466     }
467 */
468 
469     /** @throws Exception */
470     public void testPathBaseAbsPath()
471         throws Exception
472     {
473         String base = "/tmp/foo";
474         String path = "absolutePath";
475 
476         PathDescriptor desc = new PathDescriptor( base, "/" + path );
477 
478         assertTrue( desc.isFile() );
479         assertFalse( desc.isRelative() );
480         assertNotNull( desc.getBaseUrl() );
481         assertNotNull( desc.getPathUrl() );
482         assertNotNull( desc.getPath() );
483         assertNotNull( desc.getLocation() );
484         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
485         {
486             String s = StringUtils.replace( new File( base + "/" + path ).toURI().toURL().toString(), "file:", "" );
487             assertEquals( "wrong path", s, desc.getPath() );
488             assertEquals( "wrong location", s, desc.getLocation() );
489         }
490         else
491         {
492             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
493             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
494         }
495     }
496 
497     /** @throws Exception */
498     public void testPathBaseRelPath()
499         throws Exception
500     {
501         String base = "/tmp/foo";
502         String path = "relativePath";
503 
504         PathDescriptor desc = new PathDescriptor( base, path );
505 
506         assertTrue( desc.isFile() );
507         assertFalse( desc.isRelative() );
508         assertNotNull( desc.getBaseUrl() );
509         assertNotNull( desc.getPathUrl() );
510         assertNotNull( desc.getPath() );
511         assertNotNull( desc.getLocation() );
512         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
513         {
514             String s = StringUtils.replace( new File( base + "/" + path ).toURI().toURL().toString(), "file:", "" );
515             assertEquals( "wrong path", s, desc.getPath() );
516             assertEquals( "wrong location", s, desc.getLocation() );
517         }
518         else
519         {
520             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
521             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
522         }
523     }
524 
525     /** @throws Exception */
526     public void testPathBaseEmptyAbsPath()
527         throws Exception
528     {
529         String base = "/tmp/foo";
530         String path = "";
531 
532         PathDescriptor desc = new PathDescriptor( base, "/" + path );
533 
534         assertTrue( desc.isFile() );
535         assertFalse( desc.isRelative() );
536         assertNotNull( desc.getBaseUrl() );
537         assertNotNull( desc.getPathUrl() );
538         assertNotNull( desc.getPath() );
539         assertNotNull( desc.getLocation() );
540         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
541         {
542             String s = StringUtils.replace( new File( base ).toURI().toURL().toString(), "file:", "" );
543             assertEquals( "wrong path", s, desc.getPath() );
544             assertEquals( "wrong location", s, desc.getLocation() );
545         }
546         else
547         {
548             assertEquals( "wrong path", base, desc.getPath() );
549             assertEquals( "wrong location", base, desc.getLocation() );
550         }
551     }
552 
553     /** @throws Exception */
554     public void testPathBaseEmptyRelPath()
555         throws Exception
556     {
557         String base = "/tmp/foo";
558         String path = "";
559 
560         PathDescriptor desc = new PathDescriptor( base, path );
561 
562         assertTrue( desc.isFile() );
563         assertFalse( desc.isRelative() );
564         assertNotNull( desc.getBaseUrl() );
565         assertNotNull( desc.getPathUrl() );
566         assertNotNull( desc.getPath() );
567         assertNotNull( desc.getLocation() );
568         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
569         {
570             String s = StringUtils.replace( new File( base ).toURI().toURL().toString(), "file:", "" );
571             assertEquals( "wrong path", s, desc.getPath() );
572             assertEquals( "wrong location", s, desc.getLocation() );
573         }
574         else
575         {
576             assertEquals( "wrong path", base, desc.getPath() );
577             assertEquals( "wrong location", base, desc.getLocation() );
578         }
579     }
580 
581     /** @throws Exception */
582     public void testPathBaseNullPath()
583         throws Exception
584     {
585         String base = "/tmp/foo";
586         String path = null;
587 
588         PathDescriptor desc = new PathDescriptor( base, path );
589 
590         assertTrue( desc.isFile() );
591         assertFalse( desc.isRelative() );
592         assertNotNull( desc.getBaseUrl() );
593         assertNotNull( desc.getPathUrl() );
594         assertNotNull( desc.getPath() );
595         assertNotNull( desc.getLocation() );
596         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
597         {
598             String s = StringUtils.replace( new File( base ).toURI().toURL().toString(), "file:", "" );
599             assertEquals( "wrong path", s, desc.getPath() );
600             assertEquals( "wrong location", s, desc.getLocation() );
601         }
602         else
603         {
604             assertEquals( "wrong path", base, desc.getPath() );
605             assertEquals( "wrong location", base, desc.getLocation() );
606         }
607     }
608 
609     /** @throws Exception */
610     public void testPathRelBase()
611         throws Exception
612     {
613         String base = "../msite-404";
614         String path = "index.html";
615 
616         PathDescriptor desc = new PathDescriptor( base, path );
617 
618         assertTrue( desc.isFile() );
619         assertFalse( desc.isRelative() );
620         assertNotNull( desc.getBaseUrl() );
621         assertNotNull( desc.getPathUrl() );
622         assertNotNull( desc.getPath() );
623         assertNotNull( desc.getLocation() );
624         assertEquals( desc.getPath(), desc.getLocation() );
625         // Hudson doesn't like this?
626         //assertEquals( desc.getPathUrl().toString(), desc.getBaseUrl().toString() + "/" + path );
627     }
628 }