KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > TmpDirTestCase


1 /* TmpDirTestCase
2  *
3  * $Id: TmpDirTestCase.java,v 1.11 2005/03/16 03:24:17 stack-sf Exp $
4  *
5  * Created on Dec 31, 2003.
6  *
7  * Copyright (C) 2003 Internet Archive.
8  *
9  * This file is part of the Heritrix web crawler (crawler.archive.org).
10  *
11  * Heritrix is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * any later version.
15  *
16  * Heritrix is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser Public License
22  * along with Heritrix; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */

25 package org.archive.util;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
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 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 JavaDoc TEST_TMP_SYSTEM_PROPERTY_NAME = "testtmpdir";
46
47     /**
48      * Default test tmp.
49      */

50     private static final String JavaDoc 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 JavaDoc tmpDir = null;
57
58
59     public TmpDirTestCase()
60     {
61         super();
62     }
63
64     public TmpDirTestCase(String JavaDoc testName)
65     {
66         super(testName);
67     }
68
69     /*
70      * @see TestCase#setUp()
71      */

72     protected void setUp() throws Exception JavaDoc {
73         super.setUp();
74         String JavaDoc tmpDirStr = System.getProperty(TEST_TMP_SYSTEM_PROPERTY_NAME);
75         tmpDirStr = (tmpDirStr == null)? DEFAULT_TEST_TMP_DIR: tmpDirStr;
76         this.tmpDir = new File JavaDoc(tmpDirStr);
77         if (!this.tmpDir.exists())
78         {
79             this.tmpDir.mkdirs();
80         }
81
82         if (!this.tmpDir.canWrite())
83         {
84             throw new IOException JavaDoc(this.tmpDir.getAbsolutePath() +
85                  " is unwriteable.");
86         }
87     }
88
89     /*
90      * @see TestCase#tearDown()
91      */

92     protected void tearDown() throws Exception JavaDoc
93     {
94         super.tearDown();
95     }
96
97     /**
98      * @return Returns the tmpDir.
99      */

100     public File JavaDoc getTmpDir()
101     {
102         return this.tmpDir;
103     }
104
105     /**
106      * Delete any files left over from previous run.
107      *
108      * @param basename Base name of files we're to clean up.
109      */

110     public void cleanUpOldFiles(String JavaDoc basename) {
111         cleanUpOldFiles(getTmpDir(), basename);
112     }
113
114     /**
115      * Delete any files left over from previous run.
116      *
117      * @param prefix Base name of files we're to clean up.
118      * @param basedir Directory to start cleaning in.
119      */

120     public void cleanUpOldFiles(File JavaDoc basedir, String JavaDoc prefix) {
121         File JavaDoc [] files = FileUtils.getFilesWithPrefix(basedir, prefix);
122         if (files != null) {
123             for (int i = 0; i < files.length; i++) {
124                 FileUtils.deleteDir(files[i]);
125             }
126         }
127     }
128 }
129
Popular Tags