• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.commons.io;
18 
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 
22 import java.io.BufferedOutputStream;
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.attribute.FileTime;
27 import java.util.Date;
28 
29 import org.apache.commons.io.file.attribute.FileTimes;
30 import org.apache.commons.io.test.TestUtils;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.io.TempDir;
34 
35 /**
36  * Tests {@link FileUtils}.
37  */
38 public class FileUtilsFileNewerTest {
39 
40     // Test data
41     private static final int FILE1_SIZE = 1;
42 
43     private static final int FILE2_SIZE = 1024 * 4 + 1;
44     @TempDir
45     public File temporaryFolder;
46 
47     private File testFile1;
48     private File testFile2;
49 
50     @BeforeEach
setUp()51     public void setUp() throws Exception {
52         testFile1 = new File(temporaryFolder, "file1-test.txt");
53         testFile2 = new File(temporaryFolder, "file2-test.txt");
54         if (!testFile1.getParentFile().exists()) {
55             throw new IOException("Cannot create file " + testFile1 + " as the parent directory does not exist");
56         }
57         try (BufferedOutputStream output1 = new BufferedOutputStream(Files.newOutputStream(testFile1.toPath()))) {
58             TestUtils.generateTestData(output1, FILE1_SIZE);
59         }
60         if (!testFile2.getParentFile().exists()) {
61             throw new IOException("Cannot create file " + testFile2 + " as the parent directory does not exist");
62         }
63         try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(testFile2.toPath()))) {
64             TestUtils.generateTestData(output, FILE2_SIZE);
65         }
66     }
67 
68     /**
69      * Tests the {@code isFileNewer(File, *)} methods which a "normal" file.
70      *
71      * @throws IOException
72      *
73      * @see FileUtils#isFileNewer(File, long)
74      * @see FileUtils#isFileNewer(File, Date)
75      * @see FileUtils#isFileNewer(File, File)
76      */
77     @Test
testIsFileNewer()78     public void testIsFileNewer() throws IOException {
79         if (!testFile1.exists()) {
80             throw new IllegalStateException("The testFile1 should exist");
81         }
82 
83         final FileTime fileLastModified = Files.getLastModifiedTime(testFile1.toPath());
84         final long TWO_SECOND = 2;
85 
86         testIsFileNewer("two second earlier is not newer", testFile1, FileTimes.plusSeconds(fileLastModified, TWO_SECOND), false);
87         testIsFileNewer("same time is not newer", testFile1, fileLastModified, false);
88         testIsFileNewer("two second later is newer", testFile1, FileTimes.minusSeconds(fileLastModified, TWO_SECOND), true);
89     }
90 
91     /**
92      * Tests the {@code isFileNewer(File, *)} methods which the specified conditions.
93      *
94      * Creates :
95      * <ul>
96      * <li>a {@code Date} which represents the time reference</li>
97      * <li>a temporary file with the same last modification date as the time reference</li>
98      * </ul>
99      * Then compares (with the needed {@code isFileNewer} method) the last modification date of the specified file with the
100      * specified time reference, the created {@code Date} and the temporary file.
101      * <p>
102      * The test is successful if the three comparisons return the specified wanted result.
103      *
104      * @param description describes the tested situation
105      * @param file the file of which the last modification date is compared
106      * @param fileTime the time reference measured in milliseconds since the epoch
107      * @param wantedResult the expected result
108      * @throws IOException if an I/O error occurs.
109      */
testIsFileNewer(final String description, final File file, final FileTime fileTime, final boolean wantedResult)110     protected void testIsFileNewer(final String description, final File file, final FileTime fileTime, final boolean wantedResult) throws IOException {
111         assertEquals(wantedResult, FileUtils.isFileNewer(file, fileTime), () -> description + " - FileTime");
112         assertEquals(wantedResult, FileUtils.isFileNewer(file, fileTime.toInstant()), () -> description + " - Instant");
113 
114         final File temporaryFile = testFile2;
115         Files.setLastModifiedTime(temporaryFile.toPath(), fileTime);
116         assertEquals(fileTime, Files.getLastModifiedTime(temporaryFile.toPath()), "The temporary file hasn't the right last modification date");
117         assertEquals(wantedResult, FileUtils.isFileNewer(file, temporaryFile), () -> description + " - file");
118     }
119 
120     /**
121      * Tests the {@code isFileNewer(File, *)} methods which a not existing file.
122      *
123      * @throws IOException if an I/O error occurs.
124      *
125      * @see FileUtils#isFileNewer(File, long)
126      * @see FileUtils#isFileNewer(File, Date)
127      * @see FileUtils#isFileNewer(File, File)
128      */
129     @Test
testIsFileNewerImaginaryFile()130     public void testIsFileNewerImaginaryFile() throws IOException {
131         final File imaginaryFile = new File(temporaryFolder, "imaginaryFile");
132         if (imaginaryFile.exists()) {
133             throw new IllegalStateException("The imaginary File exists");
134         }
135 
136         testIsFileNewer("imaginary file can be newer", imaginaryFile, FileUtils.lastModifiedFileTime(testFile2), false);
137     }
138 
139     /**
140      * Tests the {@code isFileNewer(File, Date)} method without specifying a {@code Date}.
141      * <p>
142      * The test is successful if the method throws an {@code IllegalArgumentException}.
143      * </p>
144      */
145     @Test
testIsFileNewerNoDate()146     public void testIsFileNewerNoDate() {
147         assertThrows(NullPointerException.class, () -> FileUtils.isFileNewer(testFile1, (Date) null), "date");
148     }
149 
150     /**
151      * Tests the {@code isFileNewer(File, long)} method without specifying a {@code File}.
152      * <p>
153      * The test is successful if the method throws an {@code IllegalArgumentException}.
154      * </p>
155      */
156     @Test
testIsFileNewerNoFile()157     public void testIsFileNewerNoFile() {
158         assertThrows(NullPointerException.class, () -> FileUtils.isFileNewer(null, 0), "file");
159     }
160 
161     /**
162      * Tests the {@code isFileNewer(File, File)} method without specifying a reference {@code File}.
163      * <p>
164      * The test is successful if the method throws an {@code IllegalArgumentException}.
165      * </p>
166      */
167     @Test
testIsFileNewerNoFileReference()168     public void testIsFileNewerNoFileReference() {
169         assertThrows(NullPointerException.class, () -> FileUtils.isFileNewer(testFile1, (File) null), "reference");
170     }
171 }
172