KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 import junit.framework.TestCase;
12
13 /**
14  * Unit test for {@link DataDirectoryHelper}.
15  */

16 public class DataDirectoryHelperTest extends TestCase {
17
18   private File JavaDoc baseFile;
19   private File JavaDoc expectedDir;
20   private DataDirectoryHelper helper;
21
22   public void setUp() throws Exception JavaDoc {
23     String JavaDoc root = TestConfigObject.getInstance().tempDirectoryRoot() + File.separator + "temp-DataDirectoryHelperTest";
24
25     this.baseFile = new File JavaDoc(root);
26     this.expectedDir = new File JavaDoc(this.baseFile, ClassBasedDirectoryTreeTest.joinWithFileSeparator(new String JavaDoc[] { "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 JavaDoc {
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 JavaDoc fnfe) {
42       // ok
43
}
44   }
45
46   public void testGetDirectory() throws Exception JavaDoc {
47     assertTrue(this.expectedDir.exists());
48
49     File JavaDoc dataFile = new File JavaDoc(this.expectedDir, "foo.txt");
50     assertTrue(dataFile.createNewFile());
51     assertTrue(dataFile.exists());
52
53     File JavaDoc 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 JavaDoc {
65     File JavaDoc theFile = new File JavaDoc(this.expectedDir, "foo.txt");
66     assertTrue(theFile.createNewFile());
67     assertTrue(theFile.exists());
68
69     File JavaDoc 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 JavaDoc fnfe) {
77       // ok
78
}
79   }
80
81 }
Popular Tags