Skip to content

Commit 808dcfe

Browse files
committed
move TmpDirTestCase.java from heritrix to webarchive-commons
1 parent da5d63d commit 808dcfe

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* This file is part of the Heritrix web crawler (crawler.archive.org).
3+
*
4+
* Licensed to the Internet Archive (IA) by one or more individual
5+
* contributors.
6+
*
7+
* The IA licenses this file to You under the Apache License, Version 2.0
8+
* (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.archive.util;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
25+
import junit.framework.TestCase;
26+
27+
28+
/**
29+
* Base class for TestCases that want access to a tmp dir for the writing
30+
* of files.
31+
*
32+
* @author stack
33+
*/
34+
public abstract class TmpDirTestCase extends TestCase
35+
{
36+
/**
37+
* Name of the system property that holds pointer to tmp directory into
38+
* which we can safely write files.
39+
*/
40+
public static final String TEST_TMP_SYSTEM_PROPERTY_NAME = "testtmpdir";
41+
42+
/**
43+
* Default test tmp.
44+
*/
45+
public static final String DEFAULT_TEST_TMP_DIR = File.separator + "tmp" +
46+
File.separator + "heritrix-junit-tests";
47+
48+
/**
49+
* Directory to write temporary files to.
50+
*/
51+
private File tmpDir = null;
52+
53+
54+
public TmpDirTestCase()
55+
{
56+
super();
57+
}
58+
59+
public TmpDirTestCase(String testName)
60+
{
61+
super(testName);
62+
}
63+
64+
/*
65+
* @see TestCase#setUp()
66+
*/
67+
protected void setUp() throws Exception {
68+
super.setUp();
69+
this.tmpDir = tmpDir();
70+
}
71+
72+
/**
73+
* @return Returns the tmpDir.
74+
*/
75+
public File getTmpDir()
76+
{
77+
return this.tmpDir;
78+
}
79+
80+
/**
81+
* Delete any files left over from previous run.
82+
*
83+
* @param basename Base name of files we're to clean up.
84+
*/
85+
public void cleanUpOldFiles(String basename) {
86+
cleanUpOldFiles(getTmpDir(), basename);
87+
}
88+
89+
/**
90+
* Delete any files left over from previous run.
91+
*
92+
* @param prefix Base name of files we're to clean up.
93+
* @param basedir Directory to start cleaning in.
94+
*/
95+
public void cleanUpOldFiles(File basedir, String prefix) {
96+
File [] files = FileUtils.getFilesWithPrefix(basedir, prefix);
97+
if (files != null) {
98+
for (int i = 0; i < files.length; i++) {
99+
org.apache.commons.io.FileUtils.deleteQuietly(files[i]);
100+
}
101+
}
102+
}
103+
104+
105+
public static File tmpDir() throws IOException {
106+
String tmpDirStr = System.getProperty(TEST_TMP_SYSTEM_PROPERTY_NAME);
107+
tmpDirStr = (tmpDirStr == null)? DEFAULT_TEST_TMP_DIR: tmpDirStr;
108+
File tmpDir = new File(tmpDirStr);
109+
FileUtils.ensureWriteableDirectory(tmpDir);
110+
111+
if (!tmpDir.canWrite())
112+
{
113+
throw new IOException(tmpDir.getAbsolutePath() +
114+
" is unwriteable.");
115+
}
116+
117+
return tmpDir;
118+
}
119+
}

0 commit comments

Comments
 (0)