|
@@ -1,6 +1,7 @@ |
1 |
1 |
|
package com.esotericsoftware.utils; |
2 |
2 |
|
|
3 |
3 |
|
import java.io.*; |
|
4 |
+ |
import java.util.*; |
4 |
5 |
|
|
5 |
6 |
|
import org.junit.*; |
6 |
7 |
|
|
|
@@ -12,6 +13,8 @@ |
12 |
13 |
|
{ |
13 |
14 |
|
private boolean mWindows; |
14 |
15 |
|
private String[] mCanonicalExistingPaths; |
|
16 |
+ |
private String mCurrentPath; |
|
17 |
+ |
private Map<String, String> mCurrentDrivePaths = new HashMap<String, String>(); |
15 |
18 |
|
|
16 |
19 |
|
public TestFileSystem( boolean pWindows, String... pCanonicalExistingPaths ) |
17 |
20 |
|
{ |
|
@@ -19,6 +22,18 @@ |
19 |
22 |
|
mCanonicalExistingPaths = pCanonicalExistingPaths; |
20 |
23 |
|
} |
21 |
24 |
|
|
|
25 |
+ |
public TestFileSystem setCurrentPath( String pCurrentPath ) |
|
26 |
+ |
{ |
|
27 |
+ |
mCurrentPath = pCurrentPath; |
|
28 |
+ |
return isWindows() ? addDriveAt( FileSupport.getWindowsDriveIndicator( mCurrentPath ), mCurrentPath.substring( 2 ) ) : this; |
|
29 |
+ |
} |
|
30 |
+ |
|
|
31 |
+ |
public TestFileSystem addDriveAt( String pDriveIndicator, String pCurrentCanonicalPath ) |
|
32 |
+ |
{ |
|
33 |
+ |
mCurrentDrivePaths.put( pDriveIndicator, pCurrentCanonicalPath ); |
|
34 |
+ |
return this; |
|
35 |
+ |
} |
|
36 |
+ |
|
22 |
37 |
|
@Override |
23 |
38 |
|
public boolean isWindows() |
24 |
39 |
|
{ |
|
@@ -32,6 +47,12 @@ |
32 |
47 |
|
} |
33 |
48 |
|
|
34 |
49 |
|
@Override |
|
50 |
+ |
public String canonicalCurrentPath() |
|
51 |
+ |
{ |
|
52 |
+ |
return mCurrentPath; |
|
53 |
+ |
} |
|
54 |
+ |
|
|
55 |
+ |
@Override |
35 |
56 |
|
public boolean exists( String path ) |
36 |
57 |
|
{ |
37 |
58 |
|
return null != findCanonicalExistingPath( path ); |
|
@@ -51,15 +72,84 @@ |
51 |
72 |
|
|
52 |
73 |
|
private String findCanonicalExistingPath( String pPath ) |
53 |
74 |
|
{ |
54 |
|
- |
for ( String zPath : mCanonicalExistingPaths ) |
|
75 |
+ |
if ( pPath.equals( ".." ) ) |
|
76 |
+ |
{ |
|
77 |
+ |
pPath += "" + separatorChar(); |
|
78 |
+ |
} |
|
79 |
+ |
if ( !isWindows() ) |
|
80 |
+ |
{ |
|
81 |
+ |
if ( pPath.startsWith( "../" ) ) |
|
82 |
+ |
{ |
|
83 |
+ |
String zFront = mCurrentPath; |
|
84 |
+ |
do |
|
85 |
+ |
{ |
|
86 |
+ |
zFront = removeLastDirReference( zFront ); |
|
87 |
+ |
pPath = pPath.substring( 3 ); |
|
88 |
+ |
} |
|
89 |
+ |
while ( pPath.startsWith( "..\\" ) ); |
|
90 |
+ |
pPath = (pPath.length() == 0) ? zFront : zFront + "/" + pPath; |
|
91 |
+ |
} |
|
92 |
+ |
} |
|
93 |
+ |
else |
|
94 |
+ |
{ |
|
95 |
+ |
if ( !pPath.startsWith( FileSupport.WINDOWS_UNC_PATH_PREFIX ) ) |
|
96 |
+ |
{ |
|
97 |
+ |
String zDriveIndicator = FileSupport.getWindowsDriveIndicator( pPath ); |
|
98 |
+ |
pPath = FileSupport.removeFromFront( zDriveIndicator, pPath ); |
|
99 |
+ |
if ( zDriveIndicator.length() == 0 ) |
|
100 |
+ |
{ |
|
101 |
+ |
zDriveIndicator = FileSupport.getWindowsDriveIndicator( mCurrentPath ); |
|
102 |
+ |
} |
|
103 |
+ |
if ( pPath.equals( "." ) ) |
|
104 |
+ |
{ |
|
105 |
+ |
pPath = mCurrentDrivePaths.get( zDriveIndicator ); |
|
106 |
+ |
} |
|
107 |
+ |
else if ( pPath.startsWith( ".\\" ) ) |
|
108 |
+ |
{ |
|
109 |
+ |
pPath = mCurrentDrivePaths.get( zDriveIndicator ) + pPath.substring( 1 ); |
|
110 |
+ |
} |
|
111 |
+ |
else if ( pPath.startsWith( "..\\" ) ) |
|
112 |
+ |
{ |
|
113 |
+ |
String zFront = mCurrentDrivePaths.get( zDriveIndicator ); |
|
114 |
+ |
do |
|
115 |
+ |
{ |
|
116 |
+ |
zFront = removeLastDirReference( zFront ); |
|
117 |
+ |
pPath = pPath.substring( 3 ); |
|
118 |
+ |
} |
|
119 |
+ |
while ( pPath.startsWith( "..\\" ) ); |
|
120 |
+ |
pPath = (pPath.length() == 0) ? zFront : zFront + "\\" + pPath; |
|
121 |
+ |
} |
|
122 |
+ |
pPath = zDriveIndicator + pPath; |
|
123 |
+ |
} |
|
124 |
+ |
} |
|
125 |
+ |
if ( isWindows() ) |
|
126 |
+ |
{ |
|
127 |
+ |
for ( String zPath : mCanonicalExistingPaths ) |
|
128 |
+ |
{ |
|
129 |
+ |
if ( zPath.equalsIgnoreCase( pPath ) ) |
|
130 |
+ |
{ |
|
131 |
+ |
return zPath; |
|
132 |
+ |
} |
|
133 |
+ |
} |
|
134 |
+ |
} |
|
135 |
+ |
else |
55 |
136 |
|
{ |
56 |
|
- |
if ( zPath.equalsIgnoreCase( pPath ) ) |
|
137 |
+ |
for ( String zPath : mCanonicalExistingPaths ) |
57 |
138 |
|
{ |
58 |
|
- |
return zPath; |
|
139 |
+ |
if ( zPath.equals( pPath ) ) |
|
140 |
+ |
{ |
|
141 |
+ |
return zPath; |
|
142 |
+ |
} |
59 |
143 |
|
} |
60 |
144 |
|
} |
61 |
145 |
|
return null; |
62 |
146 |
|
} |
|
147 |
+ |
|
|
148 |
+ |
private String removeLastDirReference( String pPath ) |
|
149 |
+ |
{ |
|
150 |
+ |
int at = pPath.lastIndexOf( separatorChar() ); |
|
151 |
+ |
return (at == -1) ? pPath : pPath.substring( 0, at ); |
|
152 |
+ |
} |
63 |
153 |
|
} |
64 |
154 |
|
|
65 |
155 |
|
@Test |
|
@@ -68,9 +158,12 @@ |
68 |
158 |
|
{ |
69 |
159 |
|
IFileSystem zFileSystem = new TestFileSystem( true ); |
70 |
160 |
|
|
71 |
|
- |
assertEquals( "\\\\TheServer\\Fred", FileSupport.normalizePath( zFileSystem, "\\\\TheServer\\Fred" )); |
|
161 |
+ |
assertEquals( "\\\\TheServer\\Fred", FileSupport.normalizePath( zFileSystem, "\\\\TheServer\\Fred" ) ); |
72 |
162 |
|
assertEquals( "\\\\TheServer\\Fred", FileSupport.normalizePath( zFileSystem, "\\\\TheServer/Fred" ) ); |
73 |
163 |
|
|
|
164 |
+ |
assertEquals( ".", FileSupport.normalizePath( zFileSystem, "." ) ); |
|
165 |
+ |
assertEquals( "..", FileSupport.normalizePath( zFileSystem, ".." ) ); |
|
166 |
+ |
|
74 |
167 |
|
assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, "./Fred" ) ); |
75 |
168 |
|
|
76 |
169 |
|
assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, ".//.////./Fred////./." ) ); |
|
@@ -120,6 +213,9 @@ |
120 |
213 |
|
|
121 |
214 |
|
zFileSystem = new TestFileSystem( false ); |
122 |
215 |
|
|
|
216 |
+ |
assertEquals( ".", FileSupport.normalizePath( zFileSystem, "." ) ); |
|
217 |
+ |
assertEquals( "..", FileSupport.normalizePath( zFileSystem, ".." ) ); |
|
218 |
+ |
|
123 |
219 |
|
assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, "./Fred" ) ); |
124 |
220 |
|
|
125 |
221 |
|
assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, ".//.////./Fred////./." ) ); |
|
@@ -148,44 +244,98 @@ |
148 |
244 |
|
public void test_isAbsoluteNormalizedPath() |
149 |
245 |
|
throws IOException |
150 |
246 |
|
{ |
151 |
|
- |
IFileSystem zFileSystem = new TestFileSystem( true, // |
152 |
|
- |
"C:\\Fred", // |
153 |
|
- |
"C:\\Flintstones\\Fred", // |
154 |
|
- |
"\\\\TheServer\\Fred" ); |
155 |
|
- |
assertAbsolute( zFileSystem, "\\\\TheServer\\Fred" ); |
|
247 |
+ |
IFileSystem zFileSystem = new TestFileSystem( true ).setCurrentPath( "C:\\" ); |
|
248 |
+ |
assertAbsolute( zFileSystem, "C:\\Flintstone", "\\\\TheServer\\Fred" ); |
|
249 |
+ |
|
|
250 |
+ |
assertRelative( zFileSystem, "C:\\Flintstone", "." ); |
|
251 |
+ |
assertRelative( zFileSystem, "C:\\Flintstone", "Fred" ); |
|
252 |
+ |
assertRelative( zFileSystem, "C:\\Flintstone", "..\\Fred" ); |
156 |
253 |
|
|
157 |
|
- |
assertRelative( zFileSystem, "Fred" ); |
158 |
|
- |
assertRelative( zFileSystem, "..\\Fred" ); |
|
254 |
+ |
assertRelative( zFileSystem, "C:\\Flintstone", "C:." ); // Just Our Drive Letter is equivalent to "." |
159 |
255 |
|
|
160 |
|
- |
assertRelative( zFileSystem, "C:." ); // Just Our Drive Letter is equivalent to "." |
|
256 |
+ |
assertAbsolute( zFileSystem, "D:\\Flintstone", "C:." ); // Just Our Drive Letter is equivalent to "." |
161 |
257 |
|
|
162 |
|
- |
assertAbsolute( zFileSystem, "R:\\Fred" ); |
|
258 |
+ |
assertAbsolute( zFileSystem, "C:\\Flintstone", "R:\\Fred" ); |
163 |
259 |
|
|
164 |
|
- |
assertAbsolute( zFileSystem, "R:." ); // Just the Other Drive Letter is equivalent to "." |
|
260 |
+ |
assertAbsolute( zFileSystem, "C:\\Flintstone", "R:." ); // Just the Other Drive Letter is equivalent to "." |
165 |
261 |
|
|
166 |
|
- |
assertAbsolute( zFileSystem, "R:Fred" ); |
167 |
|
- |
assertAbsolute( zFileSystem, "R:..\\Fred" ); |
|
262 |
+ |
assertAbsolute( zFileSystem, "C:\\Flintstone", "R:Fred" ); |
|
263 |
+ |
assertAbsolute( zFileSystem, "C:\\Flintstone", "R:..\\Fred" ); |
168 |
264 |
|
|
169 |
|
- |
assertAbsolute( zFileSystem, "\\Fred" ); |
|
265 |
+ |
assertAbsolute( zFileSystem, "C:\\Flintstone", "\\Fred" ); |
170 |
266 |
|
|
171 |
|
- |
zFileSystem = new TestFileSystem( false, // |
172 |
|
- |
"/Fred", // |
173 |
|
- |
"/Flintstones/Fred", // |
174 |
|
- |
"/TheServer/Fred" ); |
|
267 |
+ |
zFileSystem = new TestFileSystem( false ).setCurrentPath( "/" ); |
175 |
268 |
|
|
176 |
|
- |
assertRelative( zFileSystem, "Fred" ); |
177 |
|
- |
assertRelative( zFileSystem, "../Fred" ); |
|
269 |
+ |
assertRelative( zFileSystem, "/Flintstone", "Fred" ); |
|
270 |
+ |
assertRelative( zFileSystem, "/Flintstone", "../Fred" ); |
178 |
271 |
|
|
179 |
|
- |
assertAbsolute( zFileSystem, "/Fred" ); |
|
272 |
+ |
assertAbsolute( zFileSystem, "/Flintstone", "/Fred" ); |
180 |
273 |
|
} |
181 |
274 |
|
|
182 |
|
- |
private void assertAbsolute( IFileSystem pFileSystem, String pPath ) |
|
275 |
+ |
private void assertAbsolute( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelative, String pPath ) |
183 |
276 |
|
{ |
184 |
|
- |
assertTrue( "!Absolute?: " + pPath, FileSupport.isAbsoluteNormalizedPath( pFileSystem, "C:", pPath ) ); |
|
277 |
+ |
assertTrue( "!Absolute?: " + pPath, FileSupport.isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, pPath ) ); |
185 |
278 |
|
} |
186 |
279 |
|
|
187 |
|
- |
private void assertRelative( IFileSystem pFileSystem, String pPath ) |
|
280 |
+ |
private void assertRelative( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelative, String pPath ) |
188 |
281 |
|
{ |
189 |
|
- |
assertFalse( "!Relative?: " + pPath, FileSupport.isAbsoluteNormalizedPath( pFileSystem, "C:", pPath ) ); |
|
282 |
+ |
assertFalse( "!Relative?: " + pPath, FileSupport.isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, pPath ) ); |
|
283 |
+ |
} |
|
284 |
+ |
|
|
285 |
+ |
@Test |
|
286 |
+ |
public void test_canonicalizeNormalizedPath() |
|
287 |
+ |
throws IOException |
|
288 |
+ |
{ |
|
289 |
+ |
IFileSystem zFileSystem = new TestFileSystem( false, // |
|
290 |
+ |
"/", // |
|
291 |
+ |
"/Fred", // |
|
292 |
+ |
"/Flintstone", // |
|
293 |
+ |
"/Flintstone/Fred", // |
|
294 |
+ |
"/Flintstone/Wilma", // |
|
295 |
+ |
"/TheServer/Fred" ).setCurrentPath( "/Flintstone" ); |
|
296 |
+ |
|
|
297 |
+ |
assertEquals( "/", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", ".." ) ); |
|
298 |
+ |
assertEquals( "/Flintstone", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", "." ) ); |
|
299 |
+ |
assertEquals( "/Flintstone/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", "Fred" ) ); |
|
300 |
+ |
assertEquals( "/Flintstone/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone/Wilma", "../Fred" ) ); |
|
301 |
+ |
|
|
302 |
+ |
assertEquals( "/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone/Wilma", "/Fred" ) ); |
|
303 |
+ |
|
|
304 |
+ |
assertEquals( "/Flintstone/Wilma/Pebbles", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", "Wilma/Pebbles" ) ); |
|
305 |
+ |
|
|
306 |
+ |
assertEquals( "/TheServer/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone/Wilma", "/TheServer/Fred" ) ); |
|
307 |
+ |
|
|
308 |
+ |
zFileSystem = new TestFileSystem( true, // |
|
309 |
+ |
"C:\\", // |
|
310 |
+ |
"C:\\Fred", // |
|
311 |
+ |
"C:\\Flintstone", // |
|
312 |
+ |
"C:\\Flintstone\\Fred", // |
|
313 |
+ |
"C:\\Flintstone\\Wilma", // |
|
314 |
+ |
"R:\\", // |
|
315 |
+ |
"R:\\Barney", // |
|
316 |
+ |
"R:\\Rubble", // |
|
317 |
+ |
"R:\\Rubble\\Barney", // |
|
318 |
+ |
"R:\\Rubble\\Betty", // |
|
319 |
+ |
"\\\\TheServer\\Fred" ).setCurrentPath( "C:\\Flintstone" ).addDriveAt( "R:", "\\Rubble" ); |
|
320 |
+ |
|
|
321 |
+ |
assertEquals( "\\\\TheServer\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "\\\\TheServer\\Fred" ) ); |
|
322 |
+ |
|
|
323 |
+ |
assertEquals( "C:\\", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", ".." ) ); |
|
324 |
+ |
assertEquals( "C:\\Flintstone", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", "." ) ); |
|
325 |
+ |
assertEquals( "C:\\Flintstone\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", "Fred" ) ); |
|
326 |
+ |
assertEquals( "C:\\Flintstone\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "..\\Fred" ) ); |
|
327 |
+ |
|
|
328 |
+ |
assertEquals( "C:\\Flintstone\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Fred", "C:." ) ); |
|
329 |
+ |
|
|
330 |
+ |
assertEquals( "C:\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "\\Fred" ) ); |
|
331 |
+ |
|
|
332 |
+ |
assertEquals( "C:\\Flintstone\\Wilma\\Pebbles", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", "wilma\\Pebbles" ) ); |
|
333 |
+ |
|
|
334 |
+ |
assertEquals( "R:\\Barney", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:\\Barney" ) ); |
|
335 |
+ |
|
|
336 |
+ |
assertEquals( "R:\\Rubble", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:." ) ); |
|
337 |
+ |
|
|
338 |
+ |
assertEquals( "R:\\Rubble\\Barney", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:Barney" ) ); |
|
339 |
+ |
assertEquals( "R:\\Barney", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:..\\Barney" ) ); |
190 |
340 |
|
} |
191 |
341 |
|
} |