001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.File;
020: import java.util.Date;
021:
022: import org.apache.commons.io.testtools.*;
023:
024: /**
025: * This is used to test FileUtils for correctness.
026: *
027: * @author <a href="mailto:alban.peignier@free.fr">Alban Peignier</a>
028: */
029: public class FileUtilsFileNewerTestCase extends FileBasedTestCase {
030:
031: // Test data
032: private static final int FILE1_SIZE = 1;
033: private static final int FILE2_SIZE = 1024 * 4 + 1;
034:
035: private File m_testFile1;
036: private File m_testFile2;
037:
038: public FileUtilsFileNewerTestCase(String name) {
039: super (name);
040:
041: m_testFile1 = new File(getTestDirectory(), "file1-test.txt");
042: m_testFile2 = new File(getTestDirectory(), "file2-test.txt");
043: }
044:
045: /** @see junit.framework.TestCase#setUp() */
046: protected void setUp() throws Exception {
047: getTestDirectory().mkdirs();
048: createFile(m_testFile1, FILE1_SIZE);
049: createFile(m_testFile2, FILE2_SIZE);
050: }
051:
052: /** @see junit.framework.TestCase#tearDown() */
053: protected void tearDown() throws Exception {
054: m_testFile1.delete();
055: m_testFile2.delete();
056: }
057:
058: /**
059: * Tests the <code>isFileNewer(File, *)</code> methods which a "normal" file.
060: *
061: * @see FileUtils#isFileNewer(File, long)
062: * @see FileUtils#isFileNewer(File, Date)
063: * @see FileUtils#isFileNewer(File, File)
064: */
065: public void testIsFileNewer() {
066: if (!m_testFile1.exists())
067: throw new IllegalStateException(
068: "The m_testFile1 should exist");
069:
070: long fileLastModified = m_testFile1.lastModified();
071: final long TWO_SECOND = 2000;
072:
073: testIsFileNewer("two second earlier is not newer", m_testFile1,
074: fileLastModified + TWO_SECOND, false);
075: testIsFileNewer("same time is not newer", m_testFile1,
076: fileLastModified, false);
077: testIsFileNewer("two second later is newer", m_testFile1,
078: fileLastModified - TWO_SECOND, true);
079: }
080:
081: /**
082: * Tests the <code>isFileNewer(File, *)</code> methods which a not existing file.
083: *
084: * @see FileUtils#isFileNewer(File, long)
085: * @see FileUtils#isFileNewer(File, Date)
086: * @see FileUtils#isFileNewer(File, File)
087: */
088: public void testIsFileNewerImaginaryFile() {
089: File imaginaryFile = new File(getTestDirectory(),
090: "imaginaryFile");
091: if (imaginaryFile.exists())
092: throw new IllegalStateException("The imaginary File exists");
093:
094: testIsFileNewer("imaginary file can be newer", imaginaryFile,
095: m_testFile2.lastModified(), false);
096: }
097:
098: /**
099: * Tests the <code>isFileNewer(File, *)</code> methods which the specified conditions.
100: * <p/>
101: * Creates :
102: * <ul>
103: * <li>a <code>Date</code> which represents the time reference</li>
104: * <li>a temporary file with the same last modification date than the time reference</li>
105: * </ul>
106: * Then compares (with the needed <code>isFileNewer</code> method) the last modification date of
107: * the specified file with the specified time reference, the created <code>Date</code> and the temporary
108: * file.
109: * <br/>
110: * The test is successfull if the three comparaisons return the specified wanted result.
111: *
112: * @param description describes the tested situation
113: * @param file the file of which the last modification date is compared
114: * @param timeMillis the time reference measured in milliseconds since the epoch
115: *
116: * @see FileUtils#isFileNewer(File, long)
117: * @see FileUtils#isFileNewer(File, Date)
118: * @see FileUtils#isFileNewer(File, File)
119: */
120: protected void testIsFileNewer(String description, File file,
121: long time, boolean wantedResult) {
122: assertEquals(description + " - time", wantedResult, FileUtils
123: .isFileNewer(file, time));
124: assertEquals(description + " - date", wantedResult, FileUtils
125: .isFileNewer(file, new Date(time)));
126:
127: File temporaryFile = m_testFile2;
128:
129: temporaryFile.setLastModified(time);
130: assertEquals(
131: "The temporary file hasn't the right last modification date",
132: time, temporaryFile.lastModified());
133: assertEquals(description + " - file", wantedResult, FileUtils
134: .isFileNewer(file, temporaryFile));
135: }
136:
137: /**
138: * Tests the <code>isFileNewer(File, long)</code> method without specifying a <code>File</code>.
139: * <br/>
140: * The test is successfull if the method throws an <code>IllegalArgumentException</code>.
141: */
142: public void testIsFileNewerNoFile() {
143: try {
144: FileUtils.isFileNewer(null, 0);
145: fail("File not specified");
146: } catch (IllegalArgumentException e) {
147: }
148: }
149:
150: /**
151: * Tests the <code>isFileNewer(File, Date)</code> method without specifying a <code>Date</code>.
152: * <br/>
153: * The test is successfull if the method throws an <code>IllegalArgumentException</code>.
154: */
155: public void testIsFileNewerNoDate() {
156: try {
157: FileUtils.isFileNewer(m_testFile1, (Date) null);
158: fail("Date not specified");
159: } catch (IllegalArgumentException e) {
160: }
161: }
162:
163: /**
164: * Tests the <code>isFileNewer(File, File)</code> method without specifying a reference <code>File</code>.
165: * <br/>
166: * The test is successfull if the method throws an <code>IllegalArgumentException</code>.
167: */
168: public void testIsFileNewerNoFileReference() {
169: try {
170: FileUtils.isFileNewer(m_testFile1, (File) null);
171: fail("Reference file not specified");
172: } catch (IllegalArgumentException e) {
173: }
174: }
175: }
|