KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > ClassBasedDirectoryTreeTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test;
5
6 import org.apache.commons.io.FileUtils;
7
8 import java.io.File JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.io.IOException JavaDoc;
11
12 /**
13  * Unit test for {@link ClassBasedDirectoryTree}.
14  */

15 public class ClassBasedDirectoryTreeTest extends TCTestCase {
16
17   private File JavaDoc root;
18   private ClassBasedDirectoryTree tree;
19
20   public void setUp() throws Exception JavaDoc {
21     this.root = new File JavaDoc("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 JavaDoc {
29     try {
30       new ClassBasedDirectoryTree(null);
31       fail("Didn't get NPE on null root");
32     } catch (NullPointerException JavaDoc npe) {
33       // ok
34
}
35
36     try {
37       new ClassBasedDirectoryTree(new File JavaDoc("thisdirectoryshouldneverexist"));
38       fail("Didn't get exception on nonexistent root");
39     } catch (FileNotFoundException JavaDoc fnfe) {
40       // ok
41
}
42
43     File JavaDoc tempFile = new File JavaDoc("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 JavaDoc fnfe) {
51       // ok
52
}
53
54     assertTrue(tempFile.delete());
55   }
56
57   public void testGetNonexistentDirectory() throws Exception JavaDoc {
58     File JavaDoc expectedFile = new File JavaDoc(this.root.getAbsolutePath()
59                                  + File.separator
60                                  + joinWithFileSeparator(new String JavaDoc[] { "com", "tc", "test",
61                                      "ClassBasedDirectoryTreeTest" }));
62     File JavaDoc theTest = this.tree.getDirectory(getClass());
63
64     assertEquals(expectedFile.getAbsolutePath(), theTest.getAbsolutePath());
65     assertFalse(theTest.exists());
66     assertFalse(new File JavaDoc(this.root, "com").exists()); // make sure it didn't create any of the path at all
67
}
68
69   public void testGetExtantDirectory() throws Exception JavaDoc {
70     File JavaDoc expectedFile = new File JavaDoc(this.root.getAbsolutePath()
71                                  + File.separator
72                                  + joinWithFileSeparator(new String JavaDoc[] { "com", "tc", "test",
73                                      "ClassBasedDirectoryTreeTest" }));
74     assertTrue(expectedFile.mkdirs());
75     File JavaDoc otherFile = new File JavaDoc(expectedFile, "test.txt");
76     assertTrue(otherFile.createNewFile());
77
78     File JavaDoc theTest = this.tree.getDirectory(getClass());
79
80     assertEquals(expectedFile.getAbsolutePath(), theTest.getAbsolutePath());
81     assertTrue(expectedFile.exists() && expectedFile.isDirectory());
82     assertTrue(otherFile.exists()); // make sure it didn't clean the directory
83
}
84
85   public void testGetOrMakeExtantDirectory() throws Exception JavaDoc {
86     File JavaDoc expectedFile = new File JavaDoc(this.root.getAbsolutePath()
87                                  + File.separator
88                                  + joinWithFileSeparator(new String JavaDoc[] { "com", "tc", "test",
89                                      "ClassBasedDirectoryTreeTest" }));
90     assertTrue(expectedFile.mkdirs());
91     File JavaDoc otherFile = new File JavaDoc(expectedFile, "test.txt");
92     assertTrue(otherFile.createNewFile());
93
94     File JavaDoc theTest = this.tree.getOrMakeDirectory(getClass());
95
96     assertEquals(expectedFile.getAbsolutePath(), theTest.getAbsolutePath());
97     assertTrue(expectedFile.exists() && expectedFile.isDirectory());
98     assertTrue(otherFile.exists()); // make sure it didn't clean the directory
99
}
100
101   public void testFileInWay() throws Exception JavaDoc {
102     File JavaDoc expectedFile = new File JavaDoc(this.root.getAbsolutePath()
103                                  + File.separator
104                                  + joinWithFileSeparator(new String JavaDoc[] { "com", "tc", "test",
105                                      "ClassBasedDirectoryTreeTest" }));
106     assertTrue(expectedFile.getParentFile().mkdirs());
107     assertTrue(expectedFile.createNewFile());
108
109     try {
110       File JavaDoc theTest = this.tree.getDirectory(getClass());
111       fail("Didn't get exception with file in the way");
112       theTest.equals(null);
113     } catch (IOException JavaDoc ioe) {
114       // ok
115
}
116
117     try {
118       this.tree.getOrMakeDirectory(getClass());
119       fail("Didn't get exception with file in the way");
120     } catch (IOException JavaDoc ioe) {
121       // ok
122
}
123   }
124
125   public void testFailsIfFileInWayOfPath() throws Exception JavaDoc {
126     File JavaDoc expectedFile = new File JavaDoc(this.root.getAbsolutePath()
127                                  + File.separator
128                                  + joinWithFileSeparator(new String JavaDoc[] { "com", "tc", "test",
129                                      "ClassBasedDirectoryTreeTest" }));
130     assertTrue(expectedFile.getParentFile().getParentFile().mkdirs());
131     assertTrue(expectedFile.getParentFile().createNewFile());
132
133     File JavaDoc 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 JavaDoc ioe) {
141       // ok
142
}
143   }
144
145   static String JavaDoc joinWithFileSeparator(String JavaDoc[] s) {
146     StringBuffer JavaDoc out = new StringBuffer JavaDoc();
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 JavaDoc {
156     if (this.root.exists()) FileUtils.deleteDirectory(this.root);
157   }
158
159 }
Popular Tags