View Javadoc

1   /* FileUtilsTest
2    * 
3    * Created on Apr 7, 2005
4    *
5    * Copyright (C) 2005 Internet Archive.
6    * 
7    * This file is part of the Heritrix web crawler (crawler.archive.org).
8    * 
9    * Heritrix is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser Public License as published by
11   * the Free Software Foundation; either version 2.1 of the License, or
12   * any later version.
13   * 
14   * Heritrix is distributed in the hope that it will be useful, 
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU Lesser Public License
20   * along with Heritrix; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22   */
23  package org.archive.util;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  
29  /***
30   * @author stack
31   * @version $Date: 2006-09-20 22:40:21 +0000 (Wed, 20 Sep 2006) $, $Revision: 4644 $
32   */
33  public class FileUtilsTest extends TmpDirTestCase {
34      private String srcDirName = FileUtilsTest.class.getName() + ".srcdir";
35      private File srcDirFile = null;
36      private String tgtDirName = FileUtilsTest.class.getName() + ".tgtdir";
37      private File tgtDirFile = null;
38      
39      protected void setUp() throws Exception {
40          super.setUp();
41          this.srcDirFile = new File(getTmpDir(), srcDirName);
42          this.srcDirFile.mkdirs();
43          this.tgtDirFile = new File(getTmpDir(), tgtDirName);
44          this.tgtDirFile.mkdirs();
45          addFiles();
46      }
47   
48      private void addFiles() throws IOException {
49          addFiles(3, this.getName());
50      }
51      
52      private void addFiles(final int howMany, final String baseName)
53      throws IOException {
54          for (int i = 0; i < howMany; i++) {
55              File.createTempFile(baseName, null, this.srcDirFile);
56          }
57      }
58      
59      protected void tearDown() throws Exception {
60          super.tearDown();
61          FileUtils.deleteDir(this.srcDirFile);
62          FileUtils.deleteDir(this.tgtDirFile);
63      }
64  
65      public void testCopyFiles() throws IOException {
66          FileUtils.copyFiles(this.srcDirFile, this.tgtDirFile);
67          File [] srcFiles = this.srcDirFile.listFiles();
68          for (int i = 0; i < srcFiles.length; i++) {
69              File tgt = new File(this.tgtDirFile, srcFiles[i].getName());
70              assertTrue("Tgt doesn't exist " + tgt.getAbsolutePath(),
71                  tgt.exists());
72          }
73      }
74      
75      public void testCopyFile() {
76          // Test exception copying nonexistent file.
77          File [] srcFiles = this.srcDirFile.listFiles();
78          srcFiles[0].delete();
79          IOException e = null;
80          try {
81          FileUtils.copyFile(srcFiles[0],
82              new File(this.tgtDirFile, srcFiles[0].getName()));
83          } catch (IOException ioe) {
84              e = ioe;
85          }
86          assertNotNull("Didn't get expected IOE", e);
87      }
88      
89      public void testSyncDirectories() throws IOException {
90          FileUtils.syncDirectories(this.srcDirFile, null, this.tgtDirFile);
91          addFiles(1, "xxxxxx");
92          FileUtils.syncDirectories(this.srcDirFile, null, this.tgtDirFile);
93          assertEquals("Not equal", this.srcDirFile.list().length,
94              this.tgtDirFile.list().length);
95      }
96  }