01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
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: import java.io.IOException;
11:
12: /**
13: * Fetches temporary directories (and files) for use in tests. The directory is cleaned the first time it is fetched;
14: * files may exist (or no), as appropriate.
15: */
16: public class TempDirectoryHelper extends BaseDirectoryHelper {
17:
18: private final boolean cleanDir;
19:
20: public TempDirectoryHelper(Class theClass, boolean cleanDir)
21: throws IOException {
22: this (theClass, TestConfigObject.getInstance()
23: .tempDirectoryRoot(), cleanDir);
24: }
25:
26: public TempDirectoryHelper(Class theClass) throws IOException {
27: this (theClass, TestConfigObject.getInstance()
28: .tempDirectoryRoot(), true);
29: }
30:
31: // For internal use and tests only.
32: TempDirectoryHelper(Class theClass, String directoryPath,
33: boolean cleanDir) {
34: super (theClass, directoryPath);
35: this .cleanDir = cleanDir;
36: }
37:
38: protected File fetchDirectory() throws IOException {
39: File root = getRoot();
40: if (!root.exists()) {
41: root.mkdirs();
42: }
43: String shortClassName = getTargetClass().getName();
44: String[] tokens = shortClassName.split("\\.");
45: if (tokens.length > 1) {
46: shortClassName = tokens[tokens.length - 1];
47: }
48: File out = new File(root, shortClassName);
49: if ((!out.exists()) && (!out.mkdirs())) {
50: FileNotFoundException fnfe = new FileNotFoundException(
51: "Directory '" + out.getAbsolutePath()
52: + "' can't be created.");
53: throw fnfe;
54: }
55:
56: if (cleanDir) {
57: FileUtils.cleanDirectory(out);
58: }
59: return out;
60: }
61:
62: }
|