01: /* FileUtilsTest
02: *
03: * Created on Apr 7, 2005
04: *
05: * Copyright (C) 2005 Internet Archive.
06: *
07: * This file is part of the Heritrix web crawler (crawler.archive.org).
08: *
09: * 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: * @author stack
30: * @version $Date: 2006-09-20 22:40:21 +0000 (Wed, 20 Sep 2006) $, $Revision: 4644 $
31: */
32: public class FileUtilsTest extends TmpDirTestCase {
33: private String srcDirName = FileUtilsTest.class.getName()
34: + ".srcdir";
35: private File srcDirFile = null;
36: private String tgtDirName = FileUtilsTest.class.getName()
37: + ".tgtdir";
38: private File tgtDirFile = null;
39:
40: protected void setUp() throws Exception {
41: super .setUp();
42: this .srcDirFile = new File(getTmpDir(), srcDirName);
43: this .srcDirFile.mkdirs();
44: this .tgtDirFile = new File(getTmpDir(), tgtDirName);
45: this .tgtDirFile.mkdirs();
46: addFiles();
47: }
48:
49: private void addFiles() throws IOException {
50: addFiles(3, this .getName());
51: }
52:
53: private void addFiles(final int howMany, final String baseName)
54: throws IOException {
55: for (int i = 0; i < howMany; i++) {
56: File.createTempFile(baseName, null, this .srcDirFile);
57: }
58: }
59:
60: protected void tearDown() throws Exception {
61: super .tearDown();
62: FileUtils.deleteDir(this .srcDirFile);
63: FileUtils.deleteDir(this .tgtDirFile);
64: }
65:
66: public void testCopyFiles() throws IOException {
67: FileUtils.copyFiles(this .srcDirFile, this .tgtDirFile);
68: File[] srcFiles = this .srcDirFile.listFiles();
69: for (int i = 0; i < srcFiles.length; i++) {
70: File tgt = new File(this .tgtDirFile, srcFiles[i].getName());
71: assertTrue("Tgt doesn't exist " + tgt.getAbsolutePath(),
72: tgt.exists());
73: }
74: }
75:
76: public void testCopyFile() {
77: // Test exception copying nonexistent file.
78: File[] srcFiles = this .srcDirFile.listFiles();
79: srcFiles[0].delete();
80: IOException e = null;
81: try {
82: FileUtils.copyFile(srcFiles[0], new File(this .tgtDirFile,
83: srcFiles[0].getName()));
84: } catch (IOException ioe) {
85: e = ioe;
86: }
87: assertNotNull("Didn't get expected IOE", e);
88: }
89:
90: public void testSyncDirectories() throws IOException {
91: FileUtils.syncDirectories(this .srcDirFile, null,
92: this .tgtDirFile);
93: addFiles(1, "xxxxxx");
94: FileUtils.syncDirectories(this .srcDirFile, null,
95: this .tgtDirFile);
96: assertEquals("Not equal", this.srcDirFile.list().length,
97: this.tgtDirFile.list().length);
98: }
99: }
|