1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.archive.util;
26
27 import java.io.File;
28 import java.io.IOException;
29
30 import junit.framework.TestCase;
31
32
33 /***
34 * Base class for TestCases that want access to a tmp dir for the writing
35 * of files.
36 *
37 * @author stack
38 */
39 public abstract class TmpDirTestCase extends TestCase
40 {
41 /***
42 * Name of the system property that holds pointer to tmp directory into
43 * which we can safely write files.
44 */
45 private static final String TEST_TMP_SYSTEM_PROPERTY_NAME = "testtmpdir";
46
47 /***
48 * Default test tmp.
49 */
50 private static final String DEFAULT_TEST_TMP_DIR = File.separator + "tmp" +
51 File.separator + "heritrix-junit-tests";
52
53 /***
54 * Directory to write temporary files to.
55 */
56 private File tmpDir = null;
57
58
59 public TmpDirTestCase()
60 {
61 super();
62 }
63
64 public TmpDirTestCase(String testName)
65 {
66 super(testName);
67 }
68
69
70
71
72 protected void setUp() throws Exception {
73 super.setUp();
74 String tmpDirStr = System.getProperty(TEST_TMP_SYSTEM_PROPERTY_NAME);
75 tmpDirStr = (tmpDirStr == null)? DEFAULT_TEST_TMP_DIR: tmpDirStr;
76 this.tmpDir = new File(tmpDirStr);
77 if (!this.tmpDir.exists())
78 {
79 this.tmpDir.mkdirs();
80 }
81
82 if (!this.tmpDir.canWrite())
83 {
84 throw new IOException(this.tmpDir.getAbsolutePath() +
85 " is unwriteable.");
86 }
87 }
88
89 /***
90 * @return Returns the tmpDir.
91 */
92 public File getTmpDir()
93 {
94 return this.tmpDir;
95 }
96
97 /***
98 * Delete any files left over from previous run.
99 *
100 * @param basename Base name of files we're to clean up.
101 */
102 public void cleanUpOldFiles(String basename) {
103 cleanUpOldFiles(getTmpDir(), basename);
104 }
105
106 /***
107 * Delete any files left over from previous run.
108 *
109 * @param prefix Base name of files we're to clean up.
110 * @param basedir Directory to start cleaning in.
111 */
112 public void cleanUpOldFiles(File basedir, String prefix) {
113 File [] files = FileUtils.getFilesWithPrefix(basedir, prefix);
114 if (files != null) {
115 for (int i = 0; i < files.length; i++) {
116 FileUtils.deleteDir(files[i]);
117 }
118 }
119 }
120 }