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 junit.framework.TestCase;
25
26 import java.io.File;
27
28
29
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 }