01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.test;
05:
06: import org.apache.commons.io.FileUtils;
07:
08: import java.io.File;
09: import java.io.FileNotFoundException;
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 baseFile;
19: private File expectedDir;
20: private DataDirectoryHelper helper;
21:
22: public void setUp() throws Exception {
23: String root = TestConfigObject.getInstance()
24: .tempDirectoryRoot()
25: + File.separator + "temp-DataDirectoryHelperTest";
26:
27: this .baseFile = new File(root);
28: this .expectedDir = new File(
29: this .baseFile,
30: ClassBasedDirectoryTreeTest
31: .joinWithFileSeparator(new String[] { "com",
32: "tc", "test", "DataDirectoryHelperTest" }));
33: if (this .expectedDir.exists())
34: FileUtils.deleteDirectory(this .expectedDir);
35: assertTrue(this .expectedDir.mkdirs());
36:
37: this .helper = new DataDirectoryHelper(getClass(), root);
38: }
39:
40: public void testFailsIfNonexistent() throws Exception {
41: assertTrue(this .expectedDir.delete());
42: assertFalse(this .expectedDir.exists());
43:
44: try {
45: this .helper.getDirectory();
46: fail("Didn't get exception on getDirectory() with no directory there");
47: } catch (FileNotFoundException fnfe) {
48: // ok
49: }
50: }
51:
52: public void testGetDirectory() throws Exception {
53: assertTrue(this .expectedDir.exists());
54:
55: File dataFile = new File(this .expectedDir, "foo.txt");
56: assertTrue(dataFile.createNewFile());
57: assertTrue(dataFile.exists());
58:
59: File theDirectory = this .helper.getDirectory();
60: assertEquals(this .expectedDir.getAbsolutePath(), theDirectory
61: .getAbsolutePath());
62: assertTrue(theDirectory.exists());
63: assertTrue(dataFile.exists());
64:
65: theDirectory = this .helper.getDirectory();
66: assertEquals(this .expectedDir.getAbsolutePath(), theDirectory
67: .getAbsolutePath());
68: assertTrue(theDirectory.exists());
69: assertTrue(dataFile.exists());
70: }
71:
72: public void testGetFile() throws Exception {
73: File theFile = new File(this .expectedDir, "foo.txt");
74: assertTrue(theFile.createNewFile());
75: assertTrue(theFile.exists());
76:
77: File fromHelper = this .helper.getFile("foo.txt");
78: assertTrue(theFile.exists());
79: assertEquals(theFile.getAbsolutePath(), fromHelper
80: .getAbsolutePath());
81:
82: try {
83: this .helper.getFile("nonexistent.txt");
84: fail("Didn't get exception on get of nonexistent file");
85: } catch (FileNotFoundException fnfe) {
86: // ok
87: }
88: }
89:
90: }
|