View Javadoc
1   package org.apache.archiva.common.utils;
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 org.apache.commons.lang.StringUtils;
23  
24  import java.io.File;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * PathUtilTest 
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          // Some JVM's retain the "." at the end of the path.  Drop it.
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          // Some JVM's retain the "." at the end of the path.  Drop it.
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  }