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: */package com.tc.test;
04:
05: import org.apache.commons.io.FileUtils;
06:
07: import com.tc.util.Assert;
08:
09: import java.io.File;
10: import java.io.FileNotFoundException;
11: import java.io.IOException;
12:
13: /**
14: * Represents a tree of directories, based on class name. Used for the test data and directory trees.
15: */
16: public class ClassBasedDirectoryTree {
17:
18: private final File root;
19:
20: public ClassBasedDirectoryTree(File root)
21: throws FileNotFoundException {
22: Assert.assertNotNull(root);
23: if ((!root.exists()) || (!root.isDirectory()))
24: throw new FileNotFoundException("Root '"
25: + root.getAbsolutePath()
26: + "' does not exist, or is not a directory..");
27: this .root = root;
28: }
29:
30: public File getDirectory(Class theClass) throws IOException {
31: String[] parts = theClass.getName().split("\\.");
32: File destFile = buildFile(this .root, parts, 0);
33:
34: if (destFile.exists() && (!destFile.isDirectory()))
35: throw new FileNotFoundException("'" + destFile
36: + "' exists, but is not a directory.");
37: return destFile;
38: }
39:
40: public File getOrMakeDirectory(Class theClass) throws IOException {
41: File destFile = getDirectory(theClass);
42: if (!destFile.exists())
43: FileUtils.forceMkdir(destFile);
44: return destFile;
45: }
46:
47: private File buildFile(File base, String[] parts, int startWhere) {
48: if (startWhere >= parts.length)
49: return base;
50: else
51: return buildFile(new File(base, parts[startWhere]), parts,
52: startWhere + 1);
53: }
54:
55: }
|