KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > FileUtilsFileNewerTestCase


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.io;
18
19 import java.io.File JavaDoc;
20 import java.util.Date JavaDoc;
21
22 import org.apache.commons.io.testtools.*;
23
24 /**
25  * This is used to test FileUtils for correctness.
26  *
27  * @author <a HREF="mailto:alban.peignier@free.fr">Alban Peignier</a>
28  */

29 public class FileUtilsFileNewerTestCase extends FileBasedTestCase {
30
31     // Test data
32
private static final int FILE1_SIZE = 1;
33     private static final int FILE2_SIZE = 1024 * 4 + 1;
34
35     private File JavaDoc m_testFile1;
36     private File JavaDoc m_testFile2;
37
38     public FileUtilsFileNewerTestCase(String JavaDoc name) {
39         super(name);
40         
41         m_testFile1 = new File JavaDoc(getTestDirectory(), "file1-test.txt");
42         m_testFile2 = new File JavaDoc(getTestDirectory(), "file2-test.txt");
43     }
44
45     /** @see junit.framework.TestCase#setUp() */
46     protected void setUp() throws Exception JavaDoc {
47         getTestDirectory().mkdirs();
48         createFile(m_testFile1, FILE1_SIZE);
49         createFile(m_testFile2, FILE2_SIZE);
50     }
51
52     /** @see junit.framework.TestCase#tearDown() */
53     protected void tearDown() throws Exception JavaDoc {
54         m_testFile1.delete();
55         m_testFile2.delete();
56     }
57
58     /**
59      * Tests the <code>isFileNewer(File, *)</code> methods which a "normal" file.
60      *
61      * @see FileUtils#isFileNewer(File, long)
62      * @see FileUtils#isFileNewer(File, Date)
63      * @see FileUtils#isFileNewer(File, File)
64      */

65     public void testIsFileNewer() {
66         if (!m_testFile1.exists())
67             throw new IllegalStateException JavaDoc("The m_testFile1 should exist");
68
69         long fileLastModified = m_testFile1.lastModified();
70         final long ONE_SECOND = 1000;
71
72         testIsFileNewer("one second earlier is not newer" , m_testFile1, fileLastModified + ONE_SECOND, false);
73         testIsFileNewer("same time is not newer" , m_testFile1, fileLastModified, false);
74         testIsFileNewer("one second later is newer" , m_testFile1, fileLastModified - ONE_SECOND, true);
75     }
76
77     /**
78      * Tests the <code>isFileNewer(File, *)</code> methods which a not existing file.
79      *
80      * @see FileUtils#isFileNewer(File, long)
81      * @see FileUtils#isFileNewer(File, Date)
82      * @see FileUtils#isFileNewer(File, File)
83      */

84     public void testIsFileNewerImaginaryFile() {
85         File JavaDoc imaginaryFile = new File JavaDoc(getTestDirectory(), "imaginaryFile");
86         if (imaginaryFile.exists())
87             throw new IllegalStateException JavaDoc("The imaginary File exists");
88
89         testIsFileNewer("imaginary file can be newer" , imaginaryFile, 0, false);
90     }
91
92     /**
93      * Tests the <code>isFileNewer(File, *)</code> methods which the specified conditions.
94      * <p/>
95      * Creates :
96      * <ul>
97      * <li>a <code>Date</code> which represents the time reference</li>
98      * <li>a temporary file with the same last modification date than the time reference</li>
99      * </ul>
100      * Then compares (with the needed <code>isFileNewer</code> method) the last modification date of
101      * the specified file with the specified time reference, the created <code>Date</code> and the temporary
102      * file.
103      * <br/>
104      * The test is successfull if the three comparaisons return the specified wanted result.
105      *
106      * @param description describes the tested situation
107      * @param file the file of which the last modification date is compared
108      * @param timeMillis the time reference measured in milliseconds since the epoch
109      *
110      * @see FileUtils#isFileNewer(File, long)
111      * @see FileUtils#isFileNewer(File, Date)
112      * @see FileUtils#isFileNewer(File, File)
113      */

114     protected void testIsFileNewer(String JavaDoc description, File JavaDoc file, long time, boolean wantedResult) {
115         assertEquals(description + " - time", wantedResult, FileUtils.isFileNewer(file, time));
116         assertEquals(description + " - date", wantedResult, FileUtils.isFileNewer(file, new Date JavaDoc(time)));
117         
118         File JavaDoc temporaryFile = m_testFile2;
119
120         temporaryFile.setLastModified(time);
121         if (temporaryFile.lastModified() != time)
122             throw new IllegalStateException JavaDoc("The temporary file hasn't the right last modification date");
123         assertEquals(description + " - file", wantedResult, FileUtils.isFileNewer(file, temporaryFile));
124     }
125
126     /**
127      * Tests the <code>isFileNewer(File, long)</code> method without specifying a <code>File</code>.
128      * <br/>
129      * The test is successfull if the method throws an <code>IllegalArgumentException</code>.
130      */

131     public void testIsFileNewerNoFile() {
132         try {
133             FileUtils.isFileNewer(null,0);
134             fail("File not specified");
135         } catch (IllegalArgumentException JavaDoc e) {}
136     }
137
138     /**
139      * Tests the <code>isFileNewer(File, Date)</code> method without specifying a <code>Date</code>.
140      * <br/>
141      * The test is successfull if the method throws an <code>IllegalArgumentException</code>.
142      */

143     public void testIsFileNewerNoDate() {
144         try {
145             FileUtils.isFileNewer(m_testFile1, (Date JavaDoc) null);
146             fail("Date not specified");
147         } catch (IllegalArgumentException JavaDoc e) {}
148     }
149
150     /**
151      * Tests the <code>isFileNewer(File, File)</code> method without specifying a reference <code>File</code>.
152      * <br/>
153      * The test is successfull if the method throws an <code>IllegalArgumentException</code>.
154      */

155     public void testIsFileNewerNoFileReference() {
156         try {
157             FileUtils.isFileNewer(m_testFile1, (File JavaDoc) null);
158             fail("Reference file not specified");
159         } catch (IllegalArgumentException JavaDoc e) {}
160     }
161 }
162
Popular Tags