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 junit.framework.TestCase;
25  
26  import java.io.File;
27  
28  /**
29   * BaseFileTest
30   *
31   *
32   */
33  public class BaseFileTest
34      extends TestCase
35  {
36      public void testFileString()
37      {
38          File repoDir = new File( "/home/user/foo/repository" );
39          String pathFile = "path/to/resource.xml";
40          BaseFile file = new BaseFile( repoDir, pathFile );
41  
42          assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
43          assertRelativePath( "path/to/resource.xml", file );
44          assertBasedir( "/home/user/foo/repository", file );
45      }
46  
47      public void testFileFile()
48      {
49          File repoDir = new File( "/home/user/foo/repository" );
50          File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
51          BaseFile file = new BaseFile( repoDir, pathFile );
52  
53          assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
54          assertRelativePath( "path/to/resource.xml", file );
55          assertBasedir( "/home/user/foo/repository", file );
56      }
57  
58      public void testStringFile()
59      {
60          String repoDir = "/home/user/foo/repository";
61          File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
62          BaseFile file = new BaseFile( repoDir, pathFile );
63  
64          assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
65          assertRelativePath( "path/to/resource.xml", file );
66          assertBasedir( "/home/user/foo/repository", file );
67      }
68  
69      public void testFileThenSetBaseString()
70      {
71          String repoDir = "/home/user/foo/repository";
72          File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
73          BaseFile file = new BaseFile( pathFile );
74          file.setBaseDir( repoDir );
75  
76          assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
77          assertRelativePath( "path/to/resource.xml", file );
78          assertBasedir( "/home/user/foo/repository", file );
79      }
80  
81      public void testFileThenSetBaseFile()
82      {
83          File repoDir = new File( "/home/user/foo/repository" );
84          File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
85          BaseFile file = new BaseFile( pathFile );
86          file.setBaseDir( repoDir );
87  
88          assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
89          assertRelativePath( "path/to/resource.xml", file );
90          assertBasedir( "/home/user/foo/repository", file );
91      }
92  
93      public void testStringThenSetBaseString()
94      {
95          String repoDir = "/home/user/foo/repository";
96          String pathFile = "/home/user/foo/repository/path/to/resource.xml";
97          BaseFile file = new BaseFile( pathFile );
98          file.setBaseDir( repoDir );
99  
100         assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
101         assertRelativePath( "path/to/resource.xml", file );
102         assertBasedir( "/home/user/foo/repository", file );
103     }
104 
105     public void testStringThenSetBaseFile()
106     {
107         File repoDir = new File( "/home/user/foo/repository" );
108         String pathFile = "/home/user/foo/repository/path/to/resource.xml";
109         BaseFile file = new BaseFile( pathFile );
110         file.setBaseDir( repoDir );
111 
112         assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
113         assertRelativePath( "path/to/resource.xml", file );
114         assertBasedir( "/home/user/foo/repository", file );
115     }
116 
117     private void assertAbsolutePath( String expectedPath, BaseFile actualFile )
118     {
119         assertEquals( new File( expectedPath ).getAbsolutePath(), actualFile.getAbsolutePath() );
120     }
121 
122     private void assertRelativePath( String expectedPath, BaseFile actualFile )
123     {
124         assertEquals( expectedPath, StringUtils.replace( actualFile.getRelativePath(), "\\", "/" ) );
125     }
126 
127     private void assertBasedir( String expectedPath, BaseFile actualFile )
128     {
129         assertEquals( new File( expectedPath ), actualFile.getBaseDir() );
130     }
131 }