KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.tc.util.Assert;
9
10 import java.io.File JavaDoc;
11 import java.io.FileNotFoundException JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 /**
15  * Represents a tree of directories, based on class name. Used for the test data and directory trees.
16  */

17 public class ClassBasedDirectoryTree {
18
19   private final File JavaDoc root;
20
21   public ClassBasedDirectoryTree(File JavaDoc root) throws FileNotFoundException JavaDoc {
22     Assert.assertNotNull(root);
23     if ((!root.exists()) || (!root.isDirectory())) throw new FileNotFoundException JavaDoc(
24                                                                                    "Root '"
25                                                                                                                                                                       + root
26                                                                                                                                                                           .getAbsolutePath()
27                                                                                                                                                                       + "' does not exist, or is not a directory..");
28     this.root = root;
29   }
30
31   public File JavaDoc getDirectory(Class JavaDoc theClass) throws IOException JavaDoc {
32     String JavaDoc[] parts = theClass.getName().split("\\.");
33     File JavaDoc destFile = buildFile(this.root, parts, 0);
34
35     if (destFile.exists() && (!destFile.isDirectory())) throw new FileNotFoundException JavaDoc(
36                                                                                         "'"
37                                                                                                                                                                                 + destFile
38                                                                                                                                                                                 + "' exists, but is not a directory.");
39     return destFile;
40   }
41
42   public File JavaDoc getOrMakeDirectory(Class JavaDoc theClass) throws IOException JavaDoc {
43     File JavaDoc destFile = getDirectory(theClass);
44     if (!destFile.exists()) FileUtils.forceMkdir(destFile);
45     return destFile;
46   }
47
48   private File JavaDoc buildFile(File JavaDoc base, String JavaDoc[] parts, int startWhere) {
49     if (startWhere >= parts.length) return base;
50     else return buildFile(new File JavaDoc(base, parts[startWhere]), parts, startWhere + 1);
51   }
52
53 }
Popular Tags