KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
26 import java.io.IOException JavaDoc;
27
28
29 /**
30  * @author stack
31  * @version $Date: 2007/01/13 01:31:39 $, $Revision: 1.3.14.1 $
32  */

33 public class FileUtilsTest extends TmpDirTestCase {
34     private String JavaDoc srcDirName = FileUtilsTest.class.getName() + ".srcdir";
35     private File JavaDoc srcDirFile = null;
36     private String JavaDoc tgtDirName = FileUtilsTest.class.getName() + ".tgtdir";
37     private File JavaDoc tgtDirFile = null;
38     
39     protected void setUp() throws Exception JavaDoc {
40         super.setUp();
41         this.srcDirFile = new File JavaDoc(getTmpDir(), srcDirName);
42         this.srcDirFile.mkdirs();
43         this.tgtDirFile = new File JavaDoc(getTmpDir(), tgtDirName);
44         this.tgtDirFile.mkdirs();
45         addFiles();
46     }
47  
48     private void addFiles() throws IOException JavaDoc {
49         addFiles(3, this.getName());
50     }
51     
52     private void addFiles(final int howMany, final String JavaDoc baseName)
53     throws IOException JavaDoc {
54         for (int i = 0; i < howMany; i++) {
55             File.createTempFile(baseName, null, this.srcDirFile);
56         }
57     }
58     
59     protected void tearDown() throws Exception JavaDoc {
60         super.tearDown();
61         FileUtils.deleteDir(this.srcDirFile);
62         FileUtils.deleteDir(this.tgtDirFile);
63     }
64
65     public void testCopyFiles() throws IOException JavaDoc {
66         FileUtils.copyFiles(this.srcDirFile, this.tgtDirFile);
67         File JavaDoc [] srcFiles = this.srcDirFile.listFiles();
68         for (int i = 0; i < srcFiles.length; i++) {
69             File JavaDoc tgt = new File JavaDoc(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 JavaDoc [] srcFiles = this.srcDirFile.listFiles();
78         srcFiles[0].delete();
79         IOException JavaDoc e = null;
80         try {
81         FileUtils.copyFile(srcFiles[0],
82             new File JavaDoc(this.tgtDirFile, srcFiles[0].getName()));
83         } catch (IOException JavaDoc ioe) {
84             e = ioe;
85         }
86         assertNotNull("Didn't get expected IOE", e);
87     }
88     
89     public void testSyncDirectories() throws IOException JavaDoc {
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 }
97
Popular Tags