    1   /* TmpDirTestCase
    2    *
    3    * $Id: TmpDirTestCase.java 5214 2007-06-19 01:54:05Z gojomo $
    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;
    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       * @see TestCase#setUp()
    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 }