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:
10: import junit.framework.TestCase;
11:
12: /**
13: * Unit test for {@link TempDirectoryHelper}.
14: */
15: public class TempDirectoryHelperTest extends TestCase {
16:
17: private File baseFile;
18: private File expectedDir;
19: private TempDirectoryHelper helper;
20:
21: public void setUp() throws Exception {
22:
23: String root = TestConfigObject.getInstance()
24: .tempDirectoryRoot()
25: + File.separator + "temp-TempDirectoryHelperTest";
26: this .baseFile = new File(root);
27: if (!this .baseFile.exists())
28: assertTrue(this .baseFile.mkdirs());
29: this .expectedDir = new File(this .baseFile,
30: "TempDirectoryHelperTest");
31: if (this .expectedDir.exists())
32: FileUtils.deleteDirectory(this .expectedDir);
33: this .helper = new TempDirectoryHelper(getClass(), root, true);
34: }
35:
36: public void testCreates() throws Exception {
37: assertFalse(this .expectedDir.exists());
38:
39: File theDir = this .helper.getDirectory();
40: assertEquals(this .expectedDir.getAbsolutePath(), theDir
41: .getAbsolutePath());
42:
43: assertTrue(this .expectedDir.exists());
44: assertTrue(this .expectedDir.isDirectory());
45: }
46:
47: public void testCleans() throws Exception {
48: File theFile = new File(this .expectedDir, "foo.txt");
49: assertTrue(this .expectedDir.mkdirs());
50: assertTrue(theFile.createNewFile());
51: assertTrue(theFile.exists());
52:
53: this .helper.getDirectory();
54: assertFalse(theFile.exists());
55: }
56:
57: public void testDoesNotReclean() throws Exception {
58: this .helper.getDirectory();
59:
60: File theFile = new File(this .expectedDir, "foo.txt");
61: assertTrue(theFile.createNewFile());
62: assertTrue(theFile.exists());
63:
64: this .helper.getDirectory();
65: assertTrue(theFile.exists());
66: }
67:
68: public void testGetFile() throws Exception {
69: File theFile = new File(this .expectedDir, "foo.txt");
70: assertTrue(this .expectedDir.mkdirs());
71: assertTrue(theFile.createNewFile());
72: assertTrue(theFile.exists());
73:
74: File otherFile = this .helper.getFile("bar.txt");
75: assertFalse(theFile.exists());
76: assertFalse(otherFile.exists());
77:
78: assertTrue(theFile.createNewFile());
79: assertTrue(otherFile.createNewFile());
80:
81: File thirdFile = this .helper.getFile("baz.txt");
82: assertTrue(theFile.exists());
83: assertTrue(otherFile.exists());
84: assertFalse(thirdFile.exists());
85: assertTrue(thirdFile.createNewFile());
86: assertTrue(thirdFile.exists());
87:
88: File theDir = this.helper.getDirectory();
89: assertTrue(theFile.exists());
90: assertTrue(otherFile.exists());
91: assertEquals(theDir.getAbsolutePath(), theFile.getParentFile()
92: .getAbsolutePath());
93: }
94:
95: }
|