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   * @version $Id: PathDescriptorTest.java 736602 2009-01-22 10:45:40Z vsiveton $
34   */
35  public class PathDescriptorTest
36      extends TestCase
37  {
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      public void testRelPath()
56          throws Exception
57      {
58          String path = "relativePath";
59  
60          PathDescriptor desc = new PathDescriptor( path );
61  
62          assertTrue( desc.isFile() );
63          assertTrue( desc.isRelative() );
64          assertNull( desc.getBaseUrl() );
65          assertNull( desc.getPathUrl() );
66          assertNotNull( desc.getPath() );
67          assertNotNull( desc.getLocation() );
68          assertEquals( "wrong path", path, desc.getPath() );
69          assertEquals( "wrong location", path, desc.getLocation() );
70      }
71  
72      public void testEmptyAbsPath()
73          throws Exception
74      {
75          String path = "";
76  
77          PathDescriptor desc = new PathDescriptor( "/" + path );
78  
79          assertTrue( desc.isFile() );
80          assertTrue( desc.isRelative() );
81          assertNull( desc.getBaseUrl() );
82          assertNull( desc.getPathUrl() );
83          assertNotNull( desc.getPath() );
84          assertNotNull( desc.getLocation() );
85          assertEquals( "wrong path", path, desc.getPath() );
86          assertEquals( "wrong location", path, desc.getLocation() );
87      }
88  
89      public void testEmptyRelPath()
90          throws Exception
91      {
92          String path = "";
93  
94          PathDescriptor desc = new PathDescriptor( path );
95  
96          assertTrue( desc.isFile() );
97          assertTrue( desc.isRelative() );
98          assertNull( desc.getBaseUrl() );
99          assertNull( desc.getPathUrl() );
100         assertNotNull( desc.getPath() );
101         assertNotNull( desc.getLocation() );
102         assertEquals( "wrong path", path, desc.getPath() );
103         assertEquals( "wrong location", path, desc.getLocation() );
104     }
105 
106     public void testNullPath()
107         throws Exception
108     {
109         String path = null;
110 
111         PathDescriptor desc = new PathDescriptor( path );
112 
113         assertTrue( desc.isFile() );
114         assertTrue( desc.isRelative() );
115         assertNull( desc.getBaseUrl() );
116         assertNull( desc.getPathUrl() );
117         assertNull( desc.getPath() );
118         assertNull( desc.getLocation() );
119         assertEquals( "wrong path", path, desc.getPath() );
120         assertEquals( "wrong location", path, desc.getLocation() );
121     }
122 
123     public void testNullBaseAbsPath()
124         throws Exception
125     {
126         String base = null;
127         String path = "absolutePath";
128 
129         PathDescriptor desc = new PathDescriptor( base, "/" + path );
130 
131         assertTrue( desc.isFile() );
132         assertTrue( desc.isRelative() );
133         assertNull( desc.getBaseUrl() );
134         assertNull( desc.getPathUrl() );
135         assertNotNull( desc.getPath() );
136         assertNotNull( desc.getLocation() );
137         assertEquals( "wrong path", path, desc.getPath() );
138         assertEquals( "wrong location", path, desc.getLocation() );
139     }
140 
141     public void testNullBaseRelPath()
142         throws Exception
143     {
144         String base = null;
145         String path = "relativePath";
146 
147         PathDescriptor desc = new PathDescriptor( base, path );
148 
149         assertTrue( desc.isFile() );
150         assertTrue( desc.isRelative() );
151         assertNull( desc.getBaseUrl() );
152         assertNull( desc.getPathUrl() );
153         assertNotNull( desc.getPath() );
154         assertNotNull( desc.getLocation() );
155         assertEquals( "wrong path", path, desc.getPath() );
156         assertEquals( "wrong location", path, desc.getLocation() );
157     }
158 
159     public void testNullBaseEmptyAbsPath()
160         throws Exception
161     {
162         String base = null;
163         String path = "";
164 
165         PathDescriptor desc = new PathDescriptor( base, "/" + path );
166 
167         assertTrue( desc.isFile() );
168         assertTrue( desc.isRelative() );
169         assertNull( desc.getBaseUrl() );
170         assertNull( desc.getPathUrl() );
171         assertNotNull( desc.getPath() );
172         assertNotNull( desc.getLocation() );
173         assertEquals( "wrong path", path, desc.getPath() );
174         assertEquals( "wrong location", path, desc.getLocation() );
175     }
176 
177     public void testNullBaseEmptyRelPath()
178         throws Exception
179     {
180         String base = null;
181         String path = "";
182 
183         PathDescriptor desc = new PathDescriptor( base, path );
184 
185         assertTrue( desc.isFile() );
186         assertTrue( desc.isRelative() );
187         assertNull( desc.getBaseUrl() );
188         assertNull( desc.getPathUrl() );
189         assertNotNull( desc.getPath() );
190         assertNotNull( desc.getLocation() );
191         assertEquals( "wrong path", path, desc.getPath() );
192         assertEquals( "wrong location", path, desc.getLocation() );
193     }
194 
195     public void testNullBaseNullPath()
196         throws Exception
197     {
198         String base = null;
199         String path = null;
200 
201         PathDescriptor desc = new PathDescriptor( base, path );
202 
203         assertTrue( desc.isFile() );
204         assertTrue( desc.isRelative() );
205         assertNull( desc.getBaseUrl() );
206         assertNull( desc.getPathUrl() );
207         assertNull( desc.getPath() );
208         assertNull( desc.getLocation() );
209         assertEquals( "wrong path", path, desc.getPath() );
210         assertEquals( "wrong location", path, desc.getLocation() );
211     }
212 
213     public void testUrlBaseAbsPath()
214         throws Exception
215     {
216         String base = "http://maven.apache.org/";
217         String path = "absolutePath";
218 
219         PathDescriptor desc = new PathDescriptor( base, "/" + path );
220 
221         assertFalse( desc.isFile() );
222         assertFalse( desc.isRelative() );
223         assertNotNull( desc.getBaseUrl() );
224         assertNotNull( desc.getPathUrl() );
225         assertNotNull( desc.getPath() );
226         assertNotNull( desc.getLocation() );
227         assertEquals( "wrong path", "/" + path, desc.getPath() );
228         assertEquals( "wrong location", base + path, desc.getLocation() );
229     }
230 
231     public void testUrlBaseRelPath()
232         throws Exception
233     {
234         String base = "http://maven.apache.org/";
235         String path = "relativePath";
236 
237         PathDescriptor desc = new PathDescriptor( base, path );
238 
239         assertFalse( desc.isFile() );
240         assertFalse( desc.isRelative() );
241         assertNotNull( desc.getBaseUrl() );
242         assertNotNull( desc.getPathUrl() );
243         assertNotNull( desc.getPath() );
244         assertNotNull( desc.getLocation() );
245         assertEquals( "wrong path", "/" + path, desc.getPath() );
246         assertEquals( "wrong location", base + path, desc.getLocation() );
247     }
248 
249     public void testUrlBaseEmptyAbsPath()
250         throws Exception
251     {
252         String base = "http://maven.apache.org/";
253         String path = "";
254 
255         PathDescriptor desc = new PathDescriptor( base, "/" + path );
256 
257         assertFalse( desc.isFile() );
258         assertFalse( desc.isRelative() );
259         assertNotNull( desc.getBaseUrl() );
260         assertNotNull( desc.getPathUrl() );
261         assertNotNull( desc.getPath() );
262         assertNotNull( desc.getLocation() );
263         assertEquals( "wrong path", "/" + path, desc.getPath() );
264         assertEquals( "wrong location", base + path, desc.getLocation() );
265     }
266 
267     public void testUrlBaseEmptyRelPath()
268         throws Exception
269     {
270         String base = "http://maven.apache.org/";
271         String path = "";
272 
273         PathDescriptor desc = new PathDescriptor( base, path );
274 
275         assertFalse( desc.isFile() );
276         assertFalse( desc.isRelative() );
277         assertNotNull( desc.getBaseUrl() );
278         assertNotNull( desc.getPathUrl() );
279         assertNotNull( desc.getPath() );
280         assertNotNull( desc.getLocation() );
281         assertEquals( "wrong path", "/" + path, desc.getPath() );
282         assertEquals( "wrong location", base + path, desc.getLocation() );
283     }
284 
285     public void testUrlBaseNullPath()
286         throws Exception
287     {
288         String base = "http://maven.apache.org/";
289         String path = null;
290 
291         PathDescriptor desc = new PathDescriptor( base, path );
292 
293         assertFalse( desc.isFile() );
294         assertFalse( desc.isRelative() );
295         assertNotNull( desc.getBaseUrl() );
296         assertNotNull( desc.getPathUrl() );
297         assertNotNull( desc.getPath() );
298         assertNotNull( desc.getLocation() );
299         assertEquals( "wrong path", "/", desc.getPath() );
300         assertEquals( "wrong location", base, desc.getLocation() );
301     }
302 
303     public void testFileBaseAbsPath()
304         throws Exception
305     {
306         String base = "/tmp/foo";
307         String path = "absolutePath";
308 
309         PathDescriptor desc = new PathDescriptor( "file://" + base, "/" + path );
310 
311         assertTrue( desc.isFile() );
312         assertFalse( desc.isRelative() );
313         assertNotNull( desc.getBaseUrl() );
314         assertNotNull( desc.getPathUrl() );
315         assertNotNull( desc.getPath() );
316         assertNotNull( desc.getLocation() );
317         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
318         {
319             String s = StringUtils.replace( new File( base + "/" + path ).toURL().toString(), "file:", "" );
320             assertEquals( "wrong path", s, desc.getPath() );
321             assertEquals( "wrong location", s, desc.getLocation() );
322         }
323         else
324         {
325             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
326             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
327         }
328     }
329 
330     public void testFileBaseRelPath()
331         throws Exception
332     {
333         String base = "/tmp/foo";
334         String path = "relativePath";
335 
336         PathDescriptor desc = new PathDescriptor( "file://" + base, path );
337 
338         assertTrue( desc.isFile() );
339         assertFalse( desc.isRelative() );
340         assertNotNull( desc.getBaseUrl() );
341         assertNotNull( desc.getPathUrl() );
342         assertNotNull( desc.getPath() );
343         assertNotNull( desc.getLocation() );
344         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
345         {
346             String s = StringUtils.replace( new File( base + "/" + path ).toURL().toString(), "file:", "" );
347             assertEquals( "wrong path", s, desc.getPath() );
348             assertEquals( "wrong location", s, desc.getLocation() );
349         }
350         else
351         {
352             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
353             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
354         }
355     }
356 
357     public void testFileBaseEmptyAbsPath()
358         throws Exception
359     {
360         String base = "/tmp/foo";
361         String path = "";
362 
363         PathDescriptor desc = new PathDescriptor( "file://" + base, "/" + path );
364 
365         assertTrue( desc.isFile() );
366         assertFalse( desc.isRelative() );
367         assertNotNull( desc.getBaseUrl() );
368         assertNotNull( desc.getPathUrl() );
369         assertNotNull( desc.getPath() );
370         assertNotNull( desc.getLocation() );
371         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
372         {
373             String s = StringUtils.replace( new File( base ).toURL().toString(), "file:", "" );
374             assertEquals( "wrong path", s, desc.getPath() );
375             assertEquals( "wrong location", s, desc.getLocation() );
376         }
377         else
378         {
379             assertEquals( "wrong path", base, desc.getPath() );
380             assertEquals( "wrong location", base, desc.getLocation() );
381         }
382     }
383 
384     public void testFileBaseEmptyRelPath()
385         throws Exception
386     {
387         String base = "/tmp/foo";
388         String path = "";
389 
390         PathDescriptor desc = new PathDescriptor( "file://" + base, path );
391 
392         assertTrue( desc.isFile() );
393         assertFalse( desc.isRelative() );
394         assertNotNull( desc.getBaseUrl() );
395         assertNotNull( desc.getPathUrl() );
396         assertNotNull( desc.getPath() );
397         assertNotNull( desc.getLocation() );
398         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
399         {
400             String s = StringUtils.replace( new File( base ).toURL().toString(), "file:", "" );
401             assertEquals( "wrong path", s, desc.getPath() );
402             assertEquals( "wrong location", s, desc.getLocation() );
403         }
404         else
405         {
406             assertEquals( "wrong path", base, desc.getPath() );
407             assertEquals( "wrong location", base, desc.getLocation() );
408         }
409     }
410 
411     public void testFileBaseNullPath()
412         throws Exception
413     {
414         String base = "/tmp/foo";
415         String path = null;
416 
417         PathDescriptor desc = new PathDescriptor( "file://" + base, path );
418 
419         assertTrue( desc.isFile() );
420         assertFalse( desc.isRelative() );
421         assertNotNull( desc.getBaseUrl() );
422         assertNotNull( desc.getPathUrl() );
423         assertNotNull( desc.getPath() );
424         assertNotNull( desc.getLocation() );
425         assertEquals( "wrong path", base, desc.getPath() );
426         assertEquals( "wrong location", base, desc.getLocation() );
427     }
428 
429     public void testPathBaseAbsPath()
430         throws Exception
431     {
432         String base = "/tmp/foo";
433         String path = "absolutePath";
434 
435         PathDescriptor desc = new PathDescriptor( base, "/" + path );
436 
437         assertTrue( desc.isFile() );
438         assertFalse( desc.isRelative() );
439         assertNotNull( desc.getBaseUrl() );
440         assertNotNull( desc.getPathUrl() );
441         assertNotNull( desc.getPath() );
442         assertNotNull( desc.getLocation() );
443         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
444         {
445             String s = StringUtils.replace( new File( base + "/" + path ).toURL().toString(), "file:", "" );
446             assertEquals( "wrong path", s, desc.getPath() );
447             assertEquals( "wrong location", s, desc.getLocation() );
448         }
449         else
450         {
451             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
452             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
453         }
454     }
455 
456     public void testPathBaseRelPath()
457         throws Exception
458     {
459         String base = "/tmp/foo";
460         String path = "relativePath";
461 
462         PathDescriptor desc = new PathDescriptor( base, path );
463 
464         assertTrue( desc.isFile() );
465         assertFalse( desc.isRelative() );
466         assertNotNull( desc.getBaseUrl() );
467         assertNotNull( desc.getPathUrl() );
468         assertNotNull( desc.getPath() );
469         assertNotNull( desc.getLocation() );
470         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
471         {
472             String s = StringUtils.replace( new File( base + "/" + path ).toURL().toString(), "file:", "" );
473             assertEquals( "wrong path", s, desc.getPath() );
474             assertEquals( "wrong location", s, desc.getLocation() );
475         }
476         else
477         {
478             assertEquals( "wrong path", base + "/" + path, desc.getPath() );
479             assertEquals( "wrong location", base + "/" + path, desc.getLocation() );
480         }
481     }
482 
483     public void testPathBaseEmptyAbsPath()
484         throws Exception
485     {
486         String base = "/tmp/foo";
487         String path = "";
488 
489         PathDescriptor desc = new PathDescriptor( base, "/" + path );
490 
491         assertTrue( desc.isFile() );
492         assertFalse( desc.isRelative() );
493         assertNotNull( desc.getBaseUrl() );
494         assertNotNull( desc.getPathUrl() );
495         assertNotNull( desc.getPath() );
496         assertNotNull( desc.getLocation() );
497         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
498         {
499             String s = StringUtils.replace( new File( base ).toURL().toString(), "file:", "" );
500             assertEquals( "wrong path", s, desc.getPath() );
501             assertEquals( "wrong location", s, desc.getLocation() );
502         }
503         else
504         {
505             assertEquals( "wrong path", base, desc.getPath() );
506             assertEquals( "wrong location", base, desc.getLocation() );
507         }
508     }
509 
510     public void testPathBaseEmptyRelPath()
511         throws Exception
512     {
513         String base = "/tmp/foo";
514         String path = "";
515 
516         PathDescriptor desc = new PathDescriptor( base, path );
517 
518         assertTrue( desc.isFile() );
519         assertFalse( desc.isRelative() );
520         assertNotNull( desc.getBaseUrl() );
521         assertNotNull( desc.getPathUrl() );
522         assertNotNull( desc.getPath() );
523         assertNotNull( desc.getLocation() );
524         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
525         {
526             String s = StringUtils.replace( new File( base ).toURL().toString(), "file:", "" );
527             assertEquals( "wrong path", s, desc.getPath() );
528             assertEquals( "wrong location", s, desc.getLocation() );
529         }
530         else
531         {
532             assertEquals( "wrong path", base, desc.getPath() );
533             assertEquals( "wrong location", base, desc.getLocation() );
534         }
535     }
536 
537     public void testPathBaseNullPath()
538         throws Exception
539     {
540         String base = "/tmp/foo";
541         String path = null;
542 
543         PathDescriptor desc = new PathDescriptor( base, path );
544 
545         assertTrue( desc.isFile() );
546         assertFalse( desc.isRelative() );
547         assertNotNull( desc.getBaseUrl() );
548         assertNotNull( desc.getPathUrl() );
549         assertNotNull( desc.getPath() );
550         assertNotNull( desc.getLocation() );
551         if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
552         {
553             String s = StringUtils.replace( new File( base ).toURL().toString(), "file:", "" );
554             assertEquals( "wrong path", s, desc.getPath() );
555             assertEquals( "wrong location", s, desc.getLocation() );
556         }
557         else
558         {
559             assertEquals( "wrong path", base, desc.getPath() );
560             assertEquals( "wrong location", base, desc.getLocation() );
561         }
562     }
563 }