1 23 package org.archive.util; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 28 29 33 public class FileUtilsTest extends TmpDirTestCase { 34 private String srcDirName = FileUtilsTest.class.getName() + ".srcdir"; 35 private File srcDirFile = null; 36 private String tgtDirName = FileUtilsTest.class.getName() + ".tgtdir"; 37 private File tgtDirFile = null; 38 39 protected void setUp() throws Exception { 40 super.setUp(); 41 this.srcDirFile = new File (getTmpDir(), srcDirName); 42 this.srcDirFile.mkdirs(); 43 this.tgtDirFile = new File (getTmpDir(), tgtDirName); 44 this.tgtDirFile.mkdirs(); 45 addFiles(); 46 } 47 48 private void addFiles() throws IOException { 49 addFiles(3, this.getName()); 50 } 51 52 private void addFiles(final int howMany, final String baseName) 53 throws IOException { 54 for (int i = 0; i < howMany; i++) { 55 File.createTempFile(baseName, null, this.srcDirFile); 56 } 57 } 58 59 protected void tearDown() throws Exception { 60 super.tearDown(); 61 FileUtils.deleteDir(this.srcDirFile); 62 FileUtils.deleteDir(this.tgtDirFile); 63 } 64 65 public void testCopyFiles() throws IOException { 66 FileUtils.copyFiles(this.srcDirFile, this.tgtDirFile); 67 File [] srcFiles = this.srcDirFile.listFiles(); 68 for (int i = 0; i < srcFiles.length; i++) { 69 File tgt = new File (this.tgtDirFile, srcFiles[i].getName()); 70 assertTrue("Tgt doesn't exist " + tgt.getAbsolutePath(), 71 tgt.exists()); 72 } 73 } 74 75 public void testCopyFile() { 76 File [] srcFiles = this.srcDirFile.listFiles(); 78 srcFiles[0].delete(); 79 IOException e = null; 80 try { 81 FileUtils.copyFile(srcFiles[0], 82 new File (this.tgtDirFile, srcFiles[0].getName())); 83 } catch (IOException ioe) { 84 e = ioe; 85 } 86 assertNotNull("Didn't get expected IOE", e); 87 } 88 89 public void testSyncDirectories() throws IOException { 90 FileUtils.syncDirectories(this.srcDirFile, null, this.tgtDirFile); 91 addFiles(1, "xxxxxx"); 92 FileUtils.syncDirectories(this.srcDirFile, null, this.tgtDirFile); 93 assertEquals("Not equal", this.srcDirFile.list().length, 94 this.tgtDirFile.list().length); 95 } 96 } 97 | Popular Tags |