KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.test;
6
7 import org.apache.commons.io.FileUtils;
8
9 import java.io.File JavaDoc;
10 import java.io.FileNotFoundException JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 /**
14  * Fetches temporary directories (and files) for use in tests. The directory is cleaned the first time it is fetched;
15  * files may exist (or no), as appropriate.
16  */

17 public class TempDirectoryHelper extends BaseDirectoryHelper {
18
19   private final boolean cleanDir;
20
21   public TempDirectoryHelper(Class JavaDoc theClass, boolean cleanDir) throws IOException JavaDoc {
22     this(theClass, TestConfigObject.getInstance().tempDirectoryRoot(), cleanDir);
23   }
24
25   public TempDirectoryHelper(Class JavaDoc theClass) throws IOException JavaDoc {
26     this(theClass, TestConfigObject.getInstance().tempDirectoryRoot(), true);
27   }
28
29   // For internal use and tests only.
30
TempDirectoryHelper(Class JavaDoc theClass, String JavaDoc directoryPath, boolean cleanDir) {
31     super(theClass, directoryPath);
32     this.cleanDir = cleanDir;
33   }
34
35   protected File JavaDoc fetchDirectory() throws IOException JavaDoc {
36     File JavaDoc root = getRoot();
37     if (!root.exists()) {
38       root.mkdirs();
39     }
40     ClassBasedDirectoryTree tree = new ClassBasedDirectoryTree(getRoot());
41     File JavaDoc out = tree.getOrMakeDirectory(getTargetClass());
42     if ((!out.exists()) && (!out.mkdirs())) {
43       FileNotFoundException JavaDoc fnfe = new FileNotFoundException JavaDoc("Directory '" + out.getAbsolutePath()
44           + "' can't be created.");
45       throw fnfe;
46     }
47
48     if (cleanDir) {
49       FileUtils.cleanDirectory(out);
50     }
51     return out;
52   }
53
54 }
Popular Tags