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 import java.io.IOException ; 11 12 15 public class ClassBasedDirectoryTreeTest extends TCTestCase { 16 17 private File root; 18 private ClassBasedDirectoryTree tree; 19 20 public void setUp() throws Exception { 21 this.root = new File ("ClassBasedDirectoryTreeTest"); 22 23 if (this.root.exists()) FileUtils.deleteDirectory(this.root); 24 FileUtils.forceMkdir(this.root); 25 this.tree = new ClassBasedDirectoryTree(root); 26 } 27 28 public void testConstructor() throws Exception { 29 try { 30 new ClassBasedDirectoryTree(null); 31 fail("Didn't get NPE on null root"); 32 } catch (NullPointerException npe) { 33 } 35 36 try { 37 new ClassBasedDirectoryTree(new File ("thisdirectoryshouldneverexist")); 38 fail("Didn't get exception on nonexistent root"); 39 } catch (FileNotFoundException fnfe) { 40 } 42 43 File tempFile = new File ("tempfileforcbdtt"); 44 assertTrue(tempFile.createNewFile()); 45 tempFile.deleteOnExit(); 46 47 try { 48 new ClassBasedDirectoryTree(tempFile); 49 fail("Didn't get exception on file root"); 50 } catch (FileNotFoundException fnfe) { 51 } 53 54 assertTrue(tempFile.delete()); 55 } 56 57 public void testGetNonexistentDirectory() throws Exception { 58 File expectedFile = new File (this.root.getAbsolutePath() 59 + File.separator 60 + joinWithFileSeparator(new String [] { "com", "tc", "test", 61 "ClassBasedDirectoryTreeTest" })); 62 File theTest = this.tree.getDirectory(getClass()); 63 64 assertEquals(expectedFile.getAbsolutePath(), theTest.getAbsolutePath()); 65 assertFalse(theTest.exists()); 66 assertFalse(new File (this.root, "com").exists()); } 68 69 public void testGetExtantDirectory() throws Exception { 70 File expectedFile = new File (this.root.getAbsolutePath() 71 + File.separator 72 + joinWithFileSeparator(new String [] { "com", "tc", "test", 73 "ClassBasedDirectoryTreeTest" })); 74 assertTrue(expectedFile.mkdirs()); 75 File otherFile = new File (expectedFile, "test.txt"); 76 assertTrue(otherFile.createNewFile()); 77 78 File theTest = this.tree.getDirectory(getClass()); 79 80 assertEquals(expectedFile.getAbsolutePath(), theTest.getAbsolutePath()); 81 assertTrue(expectedFile.exists() && expectedFile.isDirectory()); 82 assertTrue(otherFile.exists()); } 84 85 public void testGetOrMakeExtantDirectory() throws Exception { 86 File expectedFile = new File (this.root.getAbsolutePath() 87 + File.separator 88 + joinWithFileSeparator(new String [] { "com", "tc", "test", 89 "ClassBasedDirectoryTreeTest" })); 90 assertTrue(expectedFile.mkdirs()); 91 File otherFile = new File (expectedFile, "test.txt"); 92 assertTrue(otherFile.createNewFile()); 93 94 File theTest = this.tree.getOrMakeDirectory(getClass()); 95 96 assertEquals(expectedFile.getAbsolutePath(), theTest.getAbsolutePath()); 97 assertTrue(expectedFile.exists() && expectedFile.isDirectory()); 98 assertTrue(otherFile.exists()); } 100 101 public void testFileInWay() throws Exception { 102 File expectedFile = new File (this.root.getAbsolutePath() 103 + File.separator 104 + joinWithFileSeparator(new String [] { "com", "tc", "test", 105 "ClassBasedDirectoryTreeTest" })); 106 assertTrue(expectedFile.getParentFile().mkdirs()); 107 assertTrue(expectedFile.createNewFile()); 108 109 try { 110 File theTest = this.tree.getDirectory(getClass()); 111 fail("Didn't get exception with file in the way"); 112 theTest.equals(null); 113 } catch (IOException ioe) { 114 } 116 117 try { 118 this.tree.getOrMakeDirectory(getClass()); 119 fail("Didn't get exception with file in the way"); 120 } catch (IOException ioe) { 121 } 123 } 124 125 public void testFailsIfFileInWayOfPath() throws Exception { 126 File expectedFile = new File (this.root.getAbsolutePath() 127 + File.separator 128 + joinWithFileSeparator(new String [] { "com", "tc", "test", 129 "ClassBasedDirectoryTreeTest" })); 130 assertTrue(expectedFile.getParentFile().getParentFile().mkdirs()); 131 assertTrue(expectedFile.getParentFile().createNewFile()); 132 133 File theTest = this.tree.getDirectory(getClass()); 134 assertEquals(expectedFile.getAbsolutePath(), theTest.getAbsolutePath()); 135 assertFalse(theTest.exists()); 136 137 try { 138 this.tree.getOrMakeDirectory(getClass()); 139 fail("Didn't get exception with file in the way higher up the path"); 140 } catch (IOException ioe) { 141 } 143 } 144 145 static String joinWithFileSeparator(String [] s) { 146 StringBuffer out = new StringBuffer (); 147 for (int i = 0; i < s.length; ++i) { 148 if (i > 0) out.append(File.separator); 149 out.append(s[i]); 150 } 151 152 return out.toString(); 153 } 154 155 protected void tearDown() throws Exception { 156 if (this.root.exists()) FileUtils.deleteDirectory(this.root); 157 } 158 159 } | Popular Tags |