1 package org.apache.archiva.common.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23
24 import java.io.File;
25
26 import junit.framework.TestCase;
27
28
29
30
31
32
33 public class PathUtilTest
34 extends TestCase
35 {
36 public void testToRelativeWithoutSlash()
37 {
38 assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository",
39 "/home/user/foo/repository/path/to/resource.xml" ) );
40 }
41
42 public void testToRelativeWithSlash()
43 {
44 assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository/",
45 "/home/user/foo/repository/path/to/resource.xml" ) );
46 }
47
48 public void testToUrlRelativePath()
49 {
50 File workingDir = new File( "." );
51
52 String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
53
54
55 if ( workingDirname.endsWith( "/." ) )
56 {
57 workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
58 }
59
60 if ( !workingDirname.startsWith( "/" ) )
61 {
62 workingDirname = "/" + workingDirname;
63 }
64
65 String path = "path/to/resource.xml";
66 String expectedPath = "file:" + workingDirname + "/" + path;
67
68 assertEquals( expectedPath, PathUtil.toUrl( path ) );
69 }
70
71 public void testToUrlUsingFileUrl()
72 {
73 File workingDir = new File( "." );
74
75 String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
76
77
78 if ( workingDirname.endsWith( "/." ) )
79 {
80 workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
81 }
82
83 if ( !workingDirname.startsWith( "/" ) )
84 {
85 workingDirname = "/" + workingDirname;
86 }
87
88 String path = "path/to/resource.xml";
89 String expectedPath = "file:" + workingDirname + "/" + path;
90
91 assertEquals( expectedPath, PathUtil.toUrl( expectedPath ) );
92 }
93 }