001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.test;
005:
006: import org.apache.commons.io.FileUtils;
007:
008: import java.io.File;
009: import java.io.FileNotFoundException;
010: import java.io.IOException;
011:
012: /**
013: * Unit test for {@link ClassBasedDirectoryTree}.
014: */
015: public class ClassBasedDirectoryTreeTest extends TCTestCase {
016:
017: private File root;
018: private ClassBasedDirectoryTree tree;
019:
020: public void setUp() throws Exception {
021: this .root = new File("ClassBasedDirectoryTreeTest");
022:
023: if (this .root.exists())
024: FileUtils.deleteDirectory(this .root);
025: FileUtils.forceMkdir(this .root);
026: this .tree = new ClassBasedDirectoryTree(root);
027: }
028:
029: public void testConstructor() throws Exception {
030: try {
031: new ClassBasedDirectoryTree(null);
032: fail("Didn't get NPE on null root");
033: } catch (NullPointerException npe) {
034: // ok
035: }
036:
037: try {
038: new ClassBasedDirectoryTree(new File(
039: "thisdirectoryshouldneverexist"));
040: fail("Didn't get exception on nonexistent root");
041: } catch (FileNotFoundException fnfe) {
042: // ok
043: }
044:
045: File tempFile = new File("tempfileforcbdtt");
046: assertTrue(tempFile.createNewFile());
047: tempFile.deleteOnExit();
048:
049: try {
050: new ClassBasedDirectoryTree(tempFile);
051: fail("Didn't get exception on file root");
052: } catch (FileNotFoundException fnfe) {
053: // ok
054: }
055:
056: assertTrue(tempFile.delete());
057: }
058:
059: public void testGetNonexistentDirectory() throws Exception {
060: File expectedFile = new File(this .root.getAbsolutePath()
061: + File.separator
062: + joinWithFileSeparator(new String[] { "com", "tc",
063: "test", "ClassBasedDirectoryTreeTest" }));
064: File theTest = this .tree.getDirectory(getClass());
065:
066: assertEquals(expectedFile.getAbsolutePath(), theTest
067: .getAbsolutePath());
068: assertFalse(theTest.exists());
069: assertFalse(new File(this .root, "com").exists()); // make sure it didn't create any of the path at all
070: }
071:
072: public void testGetExtantDirectory() throws Exception {
073: File expectedFile = new File(this .root.getAbsolutePath()
074: + File.separator
075: + joinWithFileSeparator(new String[] { "com", "tc",
076: "test", "ClassBasedDirectoryTreeTest" }));
077: assertTrue(expectedFile.mkdirs());
078: File otherFile = new File(expectedFile, "test.txt");
079: assertTrue(otherFile.createNewFile());
080:
081: File theTest = this .tree.getDirectory(getClass());
082:
083: assertEquals(expectedFile.getAbsolutePath(), theTest
084: .getAbsolutePath());
085: assertTrue(expectedFile.exists() && expectedFile.isDirectory());
086: assertTrue(otherFile.exists()); // make sure it didn't clean the directory
087: }
088:
089: public void testGetOrMakeExtantDirectory() throws Exception {
090: File expectedFile = new File(this .root.getAbsolutePath()
091: + File.separator
092: + joinWithFileSeparator(new String[] { "com", "tc",
093: "test", "ClassBasedDirectoryTreeTest" }));
094: assertTrue(expectedFile.mkdirs());
095: File otherFile = new File(expectedFile, "test.txt");
096: assertTrue(otherFile.createNewFile());
097:
098: File theTest = this .tree.getOrMakeDirectory(getClass());
099:
100: assertEquals(expectedFile.getAbsolutePath(), theTest
101: .getAbsolutePath());
102: assertTrue(expectedFile.exists() && expectedFile.isDirectory());
103: assertTrue(otherFile.exists()); // make sure it didn't clean the directory
104: }
105:
106: public void testFileInWay() throws Exception {
107: File expectedFile = new File(this .root.getAbsolutePath()
108: + File.separator
109: + joinWithFileSeparator(new String[] { "com", "tc",
110: "test", "ClassBasedDirectoryTreeTest" }));
111: assertTrue(expectedFile.getParentFile().mkdirs());
112: assertTrue(expectedFile.createNewFile());
113:
114: try {
115: File theTest = this .tree.getDirectory(getClass());
116: fail("Didn't get exception with file in the way");
117: theTest.equals(null);
118: } catch (IOException ioe) {
119: // ok
120: }
121:
122: try {
123: this .tree.getOrMakeDirectory(getClass());
124: fail("Didn't get exception with file in the way");
125: } catch (IOException ioe) {
126: // ok
127: }
128: }
129:
130: public void testFailsIfFileInWayOfPath() throws Exception {
131: File expectedFile = new File(this .root.getAbsolutePath()
132: + File.separator
133: + joinWithFileSeparator(new String[] { "com", "tc",
134: "test", "ClassBasedDirectoryTreeTest" }));
135: assertTrue(expectedFile.getParentFile().getParentFile()
136: .mkdirs());
137: assertTrue(expectedFile.getParentFile().createNewFile());
138:
139: File theTest = this .tree.getDirectory(getClass());
140: assertEquals(expectedFile.getAbsolutePath(), theTest
141: .getAbsolutePath());
142: assertFalse(theTest.exists());
143:
144: try {
145: this .tree.getOrMakeDirectory(getClass());
146: fail("Didn't get exception with file in the way higher up the path");
147: } catch (IOException ioe) {
148: // ok
149: }
150: }
151:
152: static String joinWithFileSeparator(String[] s) {
153: StringBuffer out = new StringBuffer();
154: for (int i = 0; i < s.length; ++i) {
155: if (i > 0)
156: out.append(File.separator);
157: out.append(s[i]);
158: }
159:
160: return out.toString();
161: }
162:
163: protected void tearDown() throws Exception {
164: if (this.root.exists())
165: FileUtils.deleteDirectory(this.root);
166: }
167:
168: }
|