1 4 package com.tc.test; 5 6 import org.apache.commons.io.FileUtils; 7 8 import java.io.File ; 9 import java.io.FileNotFoundException ; 10 11 import junit.framework.TestCase; 12 13 16 public class DataDirectoryHelperTest extends TestCase { 17 18 private File baseFile; 19 private File expectedDir; 20 private DataDirectoryHelper helper; 21 22 public void setUp() throws Exception { 23 String root = TestConfigObject.getInstance().tempDirectoryRoot() + File.separator + "temp-DataDirectoryHelperTest"; 24 25 this.baseFile = new File (root); 26 this.expectedDir = new File (this.baseFile, ClassBasedDirectoryTreeTest.joinWithFileSeparator(new String [] { "com", 27 "tc", "test", "DataDirectoryHelperTest" })); 28 if (this.expectedDir.exists()) FileUtils.deleteDirectory(this.expectedDir); 29 assertTrue(this.expectedDir.mkdirs()); 30 31 this.helper = new DataDirectoryHelper(getClass(), root); 32 } 33 34 public void testFailsIfNonexistent() throws Exception { 35 assertTrue(this.expectedDir.delete()); 36 assertFalse(this.expectedDir.exists()); 37 38 try { 39 this.helper.getDirectory(); 40 fail("Didn't get exception on getDirectory() with no directory there"); 41 } catch (FileNotFoundException fnfe) { 42 } 44 } 45 46 public void testGetDirectory() throws Exception { 47 assertTrue(this.expectedDir.exists()); 48 49 File dataFile = new File (this.expectedDir, "foo.txt"); 50 assertTrue(dataFile.createNewFile()); 51 assertTrue(dataFile.exists()); 52 53 File theDirectory = this.helper.getDirectory(); 54 assertEquals(this.expectedDir.getAbsolutePath(), theDirectory.getAbsolutePath()); 55 assertTrue(theDirectory.exists()); 56 assertTrue(dataFile.exists()); 57 58 theDirectory = this.helper.getDirectory(); 59 assertEquals(this.expectedDir.getAbsolutePath(), theDirectory.getAbsolutePath()); 60 assertTrue(theDirectory.exists()); 61 assertTrue(dataFile.exists()); 62 } 63 64 public void testGetFile() throws Exception { 65 File theFile = new File (this.expectedDir, "foo.txt"); 66 assertTrue(theFile.createNewFile()); 67 assertTrue(theFile.exists()); 68 69 File fromHelper = this.helper.getFile("foo.txt"); 70 assertTrue(theFile.exists()); 71 assertEquals(theFile.getAbsolutePath(), fromHelper.getAbsolutePath()); 72 73 try { 74 this.helper.getFile("nonexistent.txt"); 75 fail("Didn't get exception on get of nonexistent file"); 76 } catch (FileNotFoundException fnfe) { 77 } 79 } 80 81 } | Popular Tags |