litesoft
@ 943
litesoft / trunk / Java / ScarPlus / test / com / esotericsoftware / utils / FileSupportTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
package com.esotericsoftware.utils; import java.io.*; import java.util.*; import org.junit.*; import static org.junit.Assert.*; public class FileSupportTest { private static class TestFileSystem implements IFileSystem { private boolean mWindows; private String[] mCanonicalExistingPaths; private String mCurrentPath; private Map<String, String> mCurrentDrivePaths = new HashMap<String, String>(); public TestFileSystem( boolean pWindows, String... pCanonicalExistingPaths ) { mWindows = pWindows; mCanonicalExistingPaths = pCanonicalExistingPaths; } public TestFileSystem setCurrentPath( String pCurrentPath ) { mCurrentPath = pCurrentPath; return isWindows() ? addDriveAt( FileSupport.getWindowsDriveIndicator( mCurrentPath ), mCurrentPath.substring( 2 ) ) : this; } public TestFileSystem addDriveAt( String pDriveIndicator, String pCurrentCanonicalPath ) { mCurrentDrivePaths.put( pDriveIndicator, pCurrentCanonicalPath ); return this; } @Override public boolean isWindows() { return mWindows; } @Override public char separatorChar() { return mWindows ? '\\' : '/'; } @Override public String canonicalCurrentPath() { return mCurrentPath; } @Override public boolean exists( String path ) { return null != findCanonicalExistingPath( path ); } @Override public String canonicalizeNormalizedExisting( String path ) { String zPath = findCanonicalExistingPath( path ); if ( zPath != null ) { return zPath; } throw new WrappedIOException( new FileNotFoundException( path ) ); } private String findCanonicalExistingPath( String pPath ) { if ( pPath.equals( ".." ) ) { pPath += "" + separatorChar(); } if ( !isWindows() ) { if ( pPath.startsWith( "../" ) ) { String zFront = mCurrentPath; do { zFront = removeLastDirReference( zFront ); pPath = pPath.substring( 3 ); } while ( pPath.startsWith( "..\\" ) ); pPath = (pPath.length() == 0) ? zFront : zFront + "/" + pPath; } } else { if ( !pPath.startsWith( FileSupport.WINDOWS_UNC_PATH_PREFIX ) ) { String zDriveIndicator = FileSupport.getWindowsDriveIndicator( pPath ); pPath = FileSupport.removeFromFront( zDriveIndicator, pPath ); if ( zDriveIndicator.length() == 0 ) { zDriveIndicator = FileSupport.getWindowsDriveIndicator( mCurrentPath ); } if ( pPath.equals( "." ) ) { pPath = mCurrentDrivePaths.get( zDriveIndicator ); } else if ( pPath.startsWith( ".\\" ) ) { pPath = mCurrentDrivePaths.get( zDriveIndicator ) + pPath.substring( 1 ); } else if ( pPath.startsWith( "..\\" ) ) { String zFront = mCurrentDrivePaths.get( zDriveIndicator ); do { zFront = removeLastDirReference( zFront ); pPath = pPath.substring( 3 ); } while ( pPath.startsWith( "..\\" ) ); pPath = (pPath.length() == 0) ? zFront : zFront + "\\" + pPath; } pPath = zDriveIndicator + pPath; } } if ( isWindows() ) { for ( String zPath : mCanonicalExistingPaths ) { if ( zPath.equalsIgnoreCase( pPath ) ) { return zPath; } } } else { for ( String zPath : mCanonicalExistingPaths ) { if ( zPath.equals( pPath ) ) { return zPath; } } } return null; } private String removeLastDirReference( String pPath ) { int at = pPath.lastIndexOf( separatorChar() ); return (at == -1) ? pPath : pPath.substring( 0, at ); } } @Test public void test_normalizePath() { IFileSystem zFileSystem = new TestFileSystem( true ); assertEquals( "\\\\TheServer\\Fred", FileSupport.normalizePath( zFileSystem, "\\\\TheServer\\Fred" ) ); assertEquals( "\\\\TheServer\\Fred", FileSupport.normalizePath( zFileSystem, "\\\\TheServer/Fred" ) ); assertEquals( ".", FileSupport.normalizePath( zFileSystem, "." ) ); assertEquals( "..", FileSupport.normalizePath( zFileSystem, ".." ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, "./Fred" ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, ".//.////./Fred////./." ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, ".\\Fred" ) ); assertEquals( "..\\Fred", FileSupport.normalizePath( zFileSystem, "../Fred" ) ); assertEquals( "..\\Fred", FileSupport.normalizePath( zFileSystem, "..\\Fred" ) ); assertEquals( "..\\..\\..\\Fred", FileSupport.normalizePath( zFileSystem, "..\\..\\..\\Fred" ) ); assertEquals( "C:.", FileSupport.normalizePath( zFileSystem, "C:" ) ); // Just Our Drive Letter is equivalent to "." assertEquals( "R:\\Fred", FileSupport.normalizePath( zFileSystem, "R:\\Fred" ) ); assertEquals( "R:\\Fred", FileSupport.normalizePath( zFileSystem, "R:/Fred" ) ); assertEquals( "R:.", FileSupport.normalizePath( zFileSystem, "R:" ) ); // Just the Other Drive Letter is equivalent to "." assertEquals( "R:Fred", FileSupport.normalizePath( zFileSystem, "R:.\\Fred" ) ); assertEquals( "R:Fred", FileSupport.normalizePath( zFileSystem, "R:./Fred" ) ); assertEquals( "R:..\\Fred", FileSupport.normalizePath( zFileSystem, "R:..\\Fred" ) ); assertEquals( "R:..\\Fred", FileSupport.normalizePath( zFileSystem, "R:../Fred" ) ); assertEquals( "\\Fred", FileSupport.normalizePath( zFileSystem, "\\Fred" ) ); assertEquals( "\\Fred", FileSupport.normalizePath( zFileSystem, "/Fred" ) ); // Now all the funky up Levels... assertEquals( ".", FileSupport.normalizePath( zFileSystem, "a\\.." ) ); assertEquals( ".", FileSupport.normalizePath( zFileSystem, "a/.." ) ); assertEquals( "\\", FileSupport.normalizePath( zFileSystem, "\\Fred\\.." ) ); assertEquals( "\\", FileSupport.normalizePath( zFileSystem, "/Fred/.." ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, "Fred\\Wilma\\.." ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, "Fred/Wilma/.." ) ); assertEquals( "\\Fred", FileSupport.normalizePath( zFileSystem, "\\Fred\\Wilma\\.." ) ); assertEquals( "\\Fred", FileSupport.normalizePath( zFileSystem, "/Fred/Wilma/.." ) ); assertEquals( "Wilma", FileSupport.normalizePath( zFileSystem, "Fred\\..\\Wilma" ) ); assertEquals( "Wilma", FileSupport.normalizePath( zFileSystem, "Fred/../Wilma" ) ); assertEquals( "\\Wilma", FileSupport.normalizePath( zFileSystem, "\\Fred\\..\\Wilma" ) ); assertEquals( "\\Wilma", FileSupport.normalizePath( zFileSystem, "/Fred/../Wilma" ) ); assertEquals( "\\..\\Wilma", FileSupport.normalizePath( zFileSystem, "\\..\\Wilma" ) ); assertEquals( "\\..\\Wilma", FileSupport.normalizePath( zFileSystem, "/../Wilma" ) ); zFileSystem = new TestFileSystem( false ); assertEquals( ".", FileSupport.normalizePath( zFileSystem, "." ) ); assertEquals( "..", FileSupport.normalizePath( zFileSystem, ".." ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, "./Fred" ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, ".//.////./Fred////./." ) ); assertEquals( "../Fred", FileSupport.normalizePath( zFileSystem, "../Fred" ) ); assertEquals( "../../../Fred", FileSupport.normalizePath( zFileSystem, "../../../Fred" ) ); assertEquals( "/Fred", FileSupport.normalizePath( zFileSystem, "/Fred" ) ); // Now all the funky up Levels... assertEquals( ".", FileSupport.normalizePath( zFileSystem, "a/.." ) ); assertEquals( "/", FileSupport.normalizePath( zFileSystem, "/Fred/.." ) ); assertEquals( "Fred", FileSupport.normalizePath( zFileSystem, "Fred/Wilma/.." ) ); assertEquals( "/Fred", FileSupport.normalizePath( zFileSystem, "/Fred/Wilma/.." ) ); assertEquals( "Wilma", FileSupport.normalizePath( zFileSystem, "Fred/../Wilma" ) ); assertEquals( "/Wilma", FileSupport.normalizePath( zFileSystem, "/Fred/../Wilma" ) ); assertEquals( "/../Wilma", FileSupport.normalizePath( zFileSystem, "/../Wilma" ) ); } @Test public void test_isAbsoluteNormalizedPath() { IFileSystem zFileSystem = new TestFileSystem( true ).setCurrentPath( "C:\\" ); assertAbsolute( zFileSystem, "C:\\Flintstone", "\\\\TheServer\\Fred" ); assertRelative( zFileSystem, "C:\\Flintstone", "." ); assertRelative( zFileSystem, "C:\\Flintstone", "Fred" ); assertRelative( zFileSystem, "C:\\Flintstone", "..\\Fred" ); assertRelative( zFileSystem, "C:\\Flintstone", "C:." ); // Just Our Drive Letter is equivalent to "." assertAbsolute( zFileSystem, "D:\\Flintstone", "C:." ); // Just Our Drive Letter is equivalent to "." assertAbsolute( zFileSystem, "C:\\Flintstone", "R:\\Fred" ); assertAbsolute( zFileSystem, "C:\\Flintstone", "R:." ); // Just the Other Drive Letter is equivalent to "." assertAbsolute( zFileSystem, "C:\\Flintstone", "R:Fred" ); assertAbsolute( zFileSystem, "C:\\Flintstone", "R:..\\Fred" ); assertAbsolute( zFileSystem, "C:\\Flintstone", "\\Fred" ); zFileSystem = new TestFileSystem( false ).setCurrentPath( "/" ); assertRelative( zFileSystem, "/Flintstone", "Fred" ); assertRelative( zFileSystem, "/Flintstone", "../Fred" ); assertAbsolute( zFileSystem, "/Flintstone", "/Fred" ); } private void assertAbsolute( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelative, String pPath ) { assertTrue( "!Absolute?: " + pPath, FileSupport.isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, pPath ) ); } private void assertRelative( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelative, String pPath ) { assertFalse( "!Relative?: " + pPath, FileSupport.isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, pPath ) ); } @Test public void test_canonicalizeNormalizedPath() { IFileSystem zFileSystem = new TestFileSystem( false, // "/", // "/Fred", // "/Flintstone", // "/Flintstone/Fred", // "/Flintstone/Wilma", // "/TheServer/Fred" ).setCurrentPath( "/Flintstone" ); assertEquals( "/", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", ".." ) ); assertEquals( "/Flintstone", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", "." ) ); assertEquals( "/Flintstone/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", "Fred" ) ); assertEquals( "/Flintstone/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone/Wilma", "../Fred" ) ); assertEquals( "/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone/Wilma", "/Fred" ) ); assertEquals( "/Flintstone/Wilma/Pebbles", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone", "Wilma/Pebbles" ) ); assertEquals( "/TheServer/Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "/Flintstone/Wilma", "/TheServer/Fred" ) ); zFileSystem = new TestFileSystem( true, // "C:\\", // "C:\\Fred", // "C:\\Flintstone", // "C:\\Flintstone\\Fred", // "C:\\Flintstone\\Wilma", // "R:\\", // "R:\\Barney", // "R:\\Rubble", // "R:\\Rubble\\Barney", // "R:\\Rubble\\Betty", // "\\\\TheServer\\Fred" ).setCurrentPath( "C:\\Flintstone" ).addDriveAt( "R:", "\\Rubble" ); assertEquals( "\\\\TheServer\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "\\\\TheServer\\Fred" ) ); assertEquals( "C:\\", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", ".." ) ); assertEquals( "C:\\Flintstone", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", "." ) ); assertEquals( "C:\\Flintstone\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", "Fred" ) ); assertEquals( "C:\\Flintstone\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "..\\Fred" ) ); assertEquals( "C:\\Flintstone\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Fred", "C:." ) ); assertEquals( "C:\\Fred", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "\\Fred" ) ); assertEquals( "C:\\Flintstone\\Wilma\\Pebbles", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone", "wilma\\Pebbles" ) ); assertEquals( "R:\\Barney", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:\\Barney" ) ); assertEquals( "R:\\Rubble", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:." ) ); assertEquals( "R:\\Rubble\\Barney", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:Barney" ) ); assertEquals( "R:\\Barney", FileSupport.canonicalizeNormalizedPath( zFileSystem, "C:\\Flintstone\\Wilma", "R:..\\Barney" ) ); } } |
Commits for litesoft/trunk/Java/ScarPlus/test/com/esotericsoftware/utils/FileSupportTest.java
Revision | Author | Commited | Message |
---|---|---|---|
943 Diff | GeorgeS | Tue 03 Jun, 2014 04:25:50 +0000 | Extracting commonfoundation |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
315 Diff | GeorgeS | Sun 17 Jul, 2011 15:48:36 +0000 | |
309 Diff | GeorgeS | Mon 11 Jul, 2011 03:16:47 +0000 | |
308 Diff | GeorgeS | Sun 10 Jul, 2011 23:55:06 +0000 | |
307 | GeorgeS | Sun 10 Jul, 2011 01:43:51 +0000 |