001: /* TmpDirTestCase
002: *
003: * $Id: TmpDirTestCase.java 4928 2007-02-21 10:19:40Z gojomo $
004: *
005: * Created on Dec 31, 2003.
006: *
007: * Copyright (C) 2003 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.util;
026:
027: import java.io.File;
028: import java.io.IOException;
029:
030: import junit.framework.TestCase;
031:
032: /**
033: * Base class for TestCases that want access to a tmp dir for the writing
034: * of files.
035: *
036: * @author stack
037: */
038: public abstract class TmpDirTestCase extends TestCase {
039: /**
040: * Name of the system property that holds pointer to tmp directory into
041: * which we can safely write files.
042: */
043: private static final String TEST_TMP_SYSTEM_PROPERTY_NAME = "testtmpdir";
044:
045: /**
046: * Default test tmp.
047: */
048: private static final String DEFAULT_TEST_TMP_DIR = File.separator
049: + "tmp" + File.separator + "heritrix-junit-tests";
050:
051: /**
052: * Directory to write temporary files to.
053: */
054: private File tmpDir = null;
055:
056: public TmpDirTestCase() {
057: super ();
058: }
059:
060: public TmpDirTestCase(String testName) {
061: super (testName);
062: }
063:
064: /*
065: * @see TestCase#setUp()
066: */
067: protected void setUp() throws Exception {
068: super .setUp();
069: String tmpDirStr = System
070: .getProperty(TEST_TMP_SYSTEM_PROPERTY_NAME);
071: tmpDirStr = (tmpDirStr == null) ? DEFAULT_TEST_TMP_DIR
072: : tmpDirStr;
073: this .tmpDir = new File(tmpDirStr);
074: if (!this .tmpDir.exists()) {
075: this .tmpDir.mkdirs();
076: }
077:
078: if (!this .tmpDir.canWrite()) {
079: throw new IOException(this .tmpDir.getAbsolutePath()
080: + " is unwriteable.");
081: }
082: }
083:
084: /*
085: * @see TestCase#tearDown()
086: */
087: protected void tearDown() throws Exception {
088: super .tearDown();
089: }
090:
091: /**
092: * @return Returns the tmpDir.
093: */
094: public File getTmpDir() {
095: return this .tmpDir;
096: }
097:
098: /**
099: * Delete any files left over from previous run.
100: *
101: * @param basename Base name of files we're to clean up.
102: */
103: public void cleanUpOldFiles(String basename) {
104: cleanUpOldFiles(getTmpDir(), basename);
105: }
106:
107: /**
108: * Delete any files left over from previous run.
109: *
110: * @param prefix Base name of files we're to clean up.
111: * @param basedir Directory to start cleaning in.
112: */
113: public void cleanUpOldFiles(File basedir, String prefix) {
114: File[] files = FileUtils.getFilesWithPrefix(basedir, prefix);
115: if (files != null) {
116: for (int i = 0; i < files.length; i++) {
117: FileUtils.deleteDir(files[i]);
118: }
119: }
120: }
121: }
|