1 16 package org.apache.commons.io; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.net.URL ; 23 24 import org.apache.commons.io.testtools.FileBasedTestCase; 25 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 import junit.textui.TestRunner; 29 30 38 public class FileUtilsTestCase extends FileBasedTestCase { 39 40 42 45 private static final int TEST_DIRECTORY_SIZE = 0; 46 47 48 private static final int LAST_MODIFIED_DELAY = 600; 49 50 private File testFile1; 51 private File testFile2; 52 53 private static int testFile1Size; 54 private static int testFile2Size; 55 56 public static void main(String [] args) { 57 TestRunner.run(suite()); 58 } 59 60 public static Test suite() { 61 return new TestSuite(FileUtilsTestCase.class); 62 } 63 64 public FileUtilsTestCase(String name) throws IOException { 65 super(name); 66 67 testFile1 = new File (getTestDirectory(), "file1-test.txt"); 68 testFile2 = new File (getTestDirectory(), "file1a-test.txt"); 69 70 testFile1Size = (int)testFile1.length(); 71 testFile2Size = (int)testFile2.length(); 72 } 73 74 75 protected void setUp() throws Exception { 76 getTestDirectory().mkdirs(); 77 createFile(testFile1, testFile1Size); 78 createFile(testFile2, testFile2Size); 79 FileUtils.deleteDirectory(getTestDirectory()); 80 getTestDirectory().mkdirs(); 81 createFile(testFile1, testFile1Size); 82 createFile(testFile2, testFile2Size); 83 } 84 85 86 protected void tearDown() throws Exception { 87 FileUtils.deleteDirectory(getTestDirectory()); 88 } 89 90 92 public void testByteCountToDisplaySize() { 93 assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes"); 94 assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB"); 95 assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024), "1 MB"); 96 assertEquals( 97 FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024), 98 "1 GB"); 99 } 100 101 103 public void testWaitFor() { 104 FileUtils.waitFor(new File (""), -1); 105 106 FileUtils.waitFor(new File (""), 2); 107 } 108 109 111 public void testToURLs() throws Exception { 112 File [] files = new File [] { new File ("file1"), new File ("file2")}; 113 114 URL [] urls = FileUtils.toURLs(files); 115 116 120 } 121 122 124 public void testContentEquals() throws Exception { 125 File file = new File (getTestDirectory(), getName()); 127 assertTrue(FileUtils.contentEquals(file, file)); 128 129 try { 131 FileUtils.contentEquals(getTestDirectory(), getTestDirectory()); 132 fail("Comparing directories should fail with an IOException"); 133 } catch (IOException ioe) { 134 } 136 137 File objFile1 = 139 new File (getTestDirectory(), getName() + ".object"); 140 objFile1.deleteOnExit(); 141 FileUtils.copyURLToFile( 142 getClass().getResource("/java/lang/Object.class"), 143 objFile1); 144 145 File objFile2 = 146 new File (getTestDirectory(), getName() + ".collection"); 147 objFile2.deleteOnExit(); 148 FileUtils.copyURLToFile( 149 getClass().getResource("/java/util/Collection.class"), 150 objFile2); 151 152 assertTrue( 153 "Files should not be equal.", 154 !FileUtils.contentEquals(objFile1, objFile2)); 155 156 file.createNewFile(); 158 assertTrue(FileUtils.contentEquals(file, file)); 159 } 160 161 163 public void testCopyURLToFile() throws Exception { 164 File file = new File (getTestDirectory(), getName()); 166 file.deleteOnExit(); 167 168 String resourceName = "/java/lang/Object.class"; 170 FileUtils.copyURLToFile(getClass().getResource(resourceName), file); 171 172 FileInputStream fis = new FileInputStream (file); 174 try { 175 assertTrue( 176 "Content is not equal.", 177 IOUtils.contentEquals( 178 getClass().getResourceAsStream(resourceName), 179 fis)); 180 } finally { 181 fis.close(); 182 } 183 } 185 186 188 public void testForceMkdir() throws Exception { 189 FileUtils.forceMkdir(getTestDirectory()); 191 192 File testFile = new File (getTestDirectory(), getName()); 194 testFile.deleteOnExit(); 195 testFile.createNewFile(); 196 assertTrue("Test file does not exist.", testFile.exists()); 197 198 try { 200 FileUtils.forceMkdir(testFile); 201 fail("Exception expected."); 202 } catch (IOException ex) {} 203 204 testFile.delete(); 205 206 FileUtils.forceMkdir(testFile); 208 assertTrue("Directory was not created.", testFile.exists()); 209 } 210 211 213 public void testSizeOfDirectory() throws Exception { 214 File file = new File (getTestDirectory(), getName()); 215 216 try { 218 FileUtils.sizeOfDirectory(file); 219 fail("Exception expected."); 220 } catch (IllegalArgumentException ex) {} 221 222 file.createNewFile(); 224 file.deleteOnExit(); 225 226 try { 228 FileUtils.sizeOfDirectory(file); 229 fail("Exception expected."); 230 } catch (IllegalArgumentException ex) {} 231 232 file.delete(); 234 file.mkdir(); 235 236 assertEquals( 237 "Unexpected directory size", 238 TEST_DIRECTORY_SIZE, 239 FileUtils.sizeOfDirectory(file)); 240 } 241 242 244 public void XtestIsFileNewer() {} 246 247 private void log(Object obj) { 249 System.out.println( 250 FileUtilsTestCase.class +" " + getName() + " " + obj); 251 } 252 253 255 public void testCopyFile1() throws Exception { 256 File destination = new File (getTestDirectory(), "copy1.txt"); 257 258 262 FileUtils.copyFile(testFile1, destination); 263 assertTrue("Check Exist", destination.exists()); 264 assertTrue("Check Full copy", destination.length() == testFile1Size); 265 268 } 269 270 public void testCopyFile2() throws Exception { 271 File destination = new File (getTestDirectory(), "copy2.txt"); 272 273 Thread.sleep(LAST_MODIFIED_DELAY); 274 277 FileUtils.copyFile(testFile1, destination); 278 assertTrue("Check Exist", destination.exists()); 279 assertTrue("Check Full copy", destination.length() == testFile2Size); 280 assertTrue("Check last modified date preserved", 281 testFile1.lastModified() == destination.lastModified()); 282 } 283 284 public void testCopyToSelf() throws Exception { 285 File destination = new File (getTestDirectory(), "copy3.txt"); 286 FileUtils.copyFile(testFile1, destination); 288 289 try { 290 FileUtils.copyFile(destination, destination); 291 fail("file copy to self should not be possible"); 292 } catch (IOException ioe) { 293 } 295 } 296 297 public void testCopyFile2WithoutFileDatePreservation() throws Exception { 298 File destination = new File (getTestDirectory(), "copy2.txt"); 299 300 304 FileUtils.copyFile(testFile1, destination, false); 305 assertTrue("Check Exist", destination.exists()); 306 assertTrue("Check Full copy", destination.length() == testFile2Size); 307 310 } 311 312 314 public void testForceDeleteAFile1() throws Exception { 315 File destination = new File (getTestDirectory(), "copy1.txt"); 316 destination.createNewFile(); 317 assertTrue("Copy1.txt doesn't exist to delete", destination.exists()); 318 FileUtils.forceDelete(destination); 319 assertTrue("Check No Exist", !destination.exists()); 320 } 321 322 public void testForceDeleteAFile2() throws Exception { 323 File destination = new File (getTestDirectory(), "copy2.txt"); 324 destination.createNewFile(); 325 assertTrue("Copy2.txt doesn't exist to delete", destination.exists()); 326 FileUtils.forceDelete(destination); 327 assertTrue("Check No Exist", !destination.exists()); 328 } 329 330 332 public void testCopyFile1ToDir() throws Exception { 333 File directory = new File (getTestDirectory(), "subdir"); 334 if (!directory.exists()) 335 directory.mkdirs(); 336 File destination = new File (directory, testFile1.getName()); 337 338 342 FileUtils.copyFileToDirectory(testFile1, directory); 343 assertTrue("Check Exist", destination.exists()); 344 assertTrue("Check Full copy", destination.length() == testFile1Size); 345 348 349 try { 350 FileUtils.copyFileToDirectory(destination, directory); 351 fail("Should not be able to copy a file into the same directory as itself"); 352 } catch (IOException ioe) { 353 } 355 } 356 357 public void testCopyFile2ToDir() throws Exception { 358 File directory = new File (getTestDirectory(), "subdir"); 359 if (!directory.exists()) 360 directory.mkdirs(); 361 File destination = new File (directory, testFile1.getName()); 362 363 367 FileUtils.copyFileToDirectory(testFile1, directory); 368 assertTrue("Check Exist", destination.exists()); 369 assertTrue("Check Full copy", destination.length() == testFile2Size); 370 373 } 374 375 377 public void testForceDeleteDir() throws Exception { 378 FileUtils.forceDelete(getTestDirectory().getParentFile()); 379 assertTrue( 380 "Check No Exist", 381 !getTestDirectory().getParentFile().exists()); 382 } 383 384 private String replaceAll( 385 String text, 386 String lookFor, 387 String replaceWith) { 388 StringBuffer sb = new StringBuffer (text); 389 while (true) { 390 int idx = sb.toString().indexOf(lookFor); 391 if (idx < 0) { 392 break; 393 } 394 sb.replace(idx, idx + lookFor.length(), replaceWith); 395 } 396 return sb.toString(); 397 } 398 399 402 public void testFileUtils() throws Exception { 404 File file1 = new File (getTestDirectory(), "test.txt"); 406 String filename = file1.getAbsolutePath(); 407 408 OutputStream out = new java.io.FileOutputStream (file1); 410 try { 411 out.write("This is a test".getBytes("UTF-8")); 412 } finally { 413 out.close(); 414 } 415 416 File file2 = new File (getTestDirectory(), "test2.txt"); 417 String filename2 = file2.getAbsolutePath(); 418 419 424 428 432 FileUtils.writeStringToFile(file2, filename, "UTF-8"); 433 assertTrue(file2.exists()); 434 assertTrue(file2.length() > 0); 435 436 String file2contents = FileUtils.readFileToString(file2, "UTF-8"); 437 assertTrue( 438 "Second file's contents correct", 439 filename.equals(file2contents)); 440 441 assertTrue(file2.delete()); 442 443 448 String contents = FileUtils.readFileToString(new File (filename), "UTF-8"); 449 assertTrue("FileUtils.fileRead()", contents.equals("This is a test")); 450 451 } 452 453 } 454 | Popular Tags |