1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38 public class FileUtilsFileNewerTest {
39
40
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
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
70
71
72
73
74
75
76
77 @Test
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
122
123
124
125
126
127
128
129 @Test
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
141
142
143
144
145 @Test
146 public void testIsFileNewerNoDate() {
147 assertThrows(NullPointerException.class, () -> FileUtils.isFileNewer(testFile1, (Date) null), "date");
148 }
149
150
151
152
153
154
155
156 @Test
157 public void testIsFileNewerNoFile() {
158 assertThrows(NullPointerException.class, () -> FileUtils.isFileNewer(null, 0), "file");
159 }
160
161
162
163
164
165
166
167 @Test
168 public void testIsFileNewerNoFileReference() {
169 assertThrows(NullPointerException.class, () -> FileUtils.isFileNewer(testFile1, (File) null), "reference");
170 }
171 }