Fix new API and convert its tests to use fixed file times
The goal is to avoid the vagueries and randomness of local file systems
and operating systems.
MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -1522,7 +1522,7 @@ public static boolean isEmptyDirectory(final File directory) throws IOException
/**
* Tests if the specified {@link File} is newer than the specified {@link ChronoLocalDate}
- * at the current time.
+ * at the end of day.
*
* <p>Note: The input date is assumed to be in the system default time-zone with the time
* part set to the current time. To use a non-default time-zone use the method
@@ -1538,7 +1538,7 @@ public static boolean isEmptyDirectory(final File directory) throws IOException
* @since 2.8.0
*/
public static boolean isFileNewer(final File file, final ChronoLocalDate chronoLocalDate) {
- return isFileNewer(file, chronoLocalDate, LocalTime.now());
+ return isFileNewer(file, chronoLocalDate, LocalTime.MAX);
}
/**
@@ -1724,7 +1724,7 @@ public static boolean isFileNewer(final File file, final OffsetDateTime offsetDa
/**
* Tests if the specified {@link File} is older than the specified {@link ChronoLocalDate}
- * at the current time.
+ * at the end of day.
*
* <p>Note: The input date is assumed to be in the system default time-zone with the time
* part set to the current time. To use a non-default time-zone use the method
@@ -1742,7 +1742,7 @@ public static boolean isFileNewer(final File file, final OffsetDateTime offsetDa
* @since 2.8.0
*/
public static boolean isFileOlder(final File file, final ChronoLocalDate chronoLocalDate) {
- return isFileOlder(file, chronoLocalDate, LocalTime.now());
+ return isFileOlder(file, chronoLocalDate, LocalTime.MAX);
}
/**

MODIFY
src/test/java/org/apache/commons/io/FileUtilsTest.java
src/test/java/org/apache/commons/io/FileUtilsTest.java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
+import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* This is used to test FileUtils for correctness.
@@ -1595,33 +1599,36 @@ public void testIsEmptyDirectory() throws IOException {
Assertions.assertFalse(FileUtils.isEmptyDirectory(PathUtilsIsEmptyTest.DIR_SIZE_1.toFile()));
}
- @Test
- public void testIsFileNewerOlder() throws Exception {
- final File reference = new File(tempDirFile, "FileUtils-reference.txt");
+ @ParameterizedTest
+ @ValueSource(longs = {1L, 100L, 1_000L, 10_000L, 100_000L, 1_000_000L})
+ public void testIsFileNewerOlder(final long millis) throws Exception {
+ // Files
final File oldFile = new File(tempDirFile, "FileUtils-old.txt");
+ final File refFile = new File(tempDirFile, "FileUtils-reference.txt");
final File newFile = new File(tempDirFile, "FileUtils-new.txt");
final File invalidFile = new File(tempDirFile, "FileUtils-invalid-file.txt");
-
- // Create Files
- if (!oldFile.getParentFile().exists()) {
- throw new IOException("Cannot create file " + oldFile
- + " as the parent directory does not exist");
- }
- try (final OutputStream output1 = new BufferedOutputStream(Files.newOutputStream(oldFile.toPath()))) {
- TestUtils.generateTestData(output1, 0);
+ // Paths
+ final Path oldPath = oldFile.toPath();
+ final Path refPath = refFile.toPath();
+ final Path newPath = newFile.toPath();
+ // FileTimes
+ final FileTime oldFileTime = FileTime.from(millis * 1, TimeUnit.MILLISECONDS);
+ final FileTime refFileTime = FileTime.from(millis * 2, TimeUnit.MILLISECONDS);
+ final FileTime testFileTime = FileTime.from(millis * 3, TimeUnit.MILLISECONDS);
+ final FileTime newFileTime = FileTime.from(millis * 4, TimeUnit.MILLISECONDS);
+
+ // Create fixtures
+ try (final OutputStream output = new BufferedOutputStream(Files.newOutputStream(oldPath))) {
+ TestUtils.generateTestData(output, 0);
}
+ Files.setLastModifiedTime(oldPath, oldFileTime);
- do {
- TestUtils.sleepQuietly(1000);
- if (!reference.getParentFile().exists()) {
- throw new IOException("Cannot create file " + reference + " as the parent directory does not exist");
- }
- try (final OutputStream output = new BufferedOutputStream(Files.newOutputStream(reference.toPath()))) {
+ try (final OutputStream output = new BufferedOutputStream(Files.newOutputStream(refPath))) {
TestUtils.generateTestData(output, 0);
}
- } while (getLastModifiedMillis(oldFile) == getLastModifiedMillis(reference));
+ Files.setLastModifiedTime(refPath, refFileTime);
- final Date date = new Date();
+ final Date date = new Date(testFileTime.toMillis());
final long now = date.getTime();
final Instant instant = date.toInstant();
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
@@ -1629,26 +1636,16 @@ public void testIsFileNewerOlder() throws Exception {
final LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
final LocalDate localDate = zonedDateTime.toLocalDate();
final LocalDate localDatePlusDay = localDate.plusDays(1);
- final LocalTime localTime = LocalTime.ofSecondOfDay(0);
- final OffsetTime offsetTime = OffsetTime.of(localTime, ZoneOffset.UTC);
+ final LocalTime localTime0 = LocalTime.MIDNIGHT;
+ final OffsetTime offsetTime0 = OffsetTime.of(localTime0, ZoneOffset.UTC);
- do {
- try {
- TestUtils.sleep(1000);
- } catch (final InterruptedException ie) {
- // ignore
- }
- if (!newFile.getParentFile().exists()) {
- throw new IOException("Cannot create file " + newFile
- + " as the parent directory does not exist");
- }
- try (final OutputStream output = new BufferedOutputStream(Files.newOutputStream(newFile.toPath()))) {
+ try (final OutputStream output = new BufferedOutputStream(Files.newOutputStream(newPath))) {
TestUtils.generateTestData(output, 0);
}
- } while (getLastModifiedMillis(reference) == getLastModifiedMillis(newFile));
+ Files.setLastModifiedTime(newPath, newFileTime);
- // Test isFileNewer()
- assertFalse(FileUtils.isFileNewer(oldFile, reference), "Old File - Newer - File");
+ // Test
+ assertFalse(FileUtils.isFileNewer(oldFile, refFile), "Old File - Newer - File");
assertFalse(FileUtils.isFileNewer(oldFile, date), "Old File - Newer - Date");
assertFalse(FileUtils.isFileNewer(oldFile, now), "Old File - Newer - Mili");
assertFalse(FileUtils.isFileNewer(oldFile, instant), "Old File - Newer - Instant");
@@ -1657,13 +1654,13 @@ public void testIsFileNewerOlder() throws Exception {
assertFalse(FileUtils.isFileNewer(oldFile, localDateTime), "Old File - Newer - LocalDateTime");
assertFalse(FileUtils.isFileNewer(oldFile, localDateTime, ZoneId.systemDefault()), "Old File - Newer - LocalDateTime,ZoneId");
assertFalse(FileUtils.isFileNewer(oldFile, localDate), "Old File - Newer - LocalDate");
- assertTrue(FileUtils.isFileNewer(oldFile, localDate, localTime), "Old File - Newer - LocalDate,LocalTime");
- assertTrue(FileUtils.isFileNewer(oldFile, localDate, offsetTime), "Old File - Newer - LocalDate,OffsetTime");
+ assertTrue(FileUtils.isFileNewer(oldFile, localDate, localTime0), "Old File - Newer - LocalDate,LocalTime");
+ assertTrue(FileUtils.isFileNewer(oldFile, localDate, offsetTime0), "Old File - Newer - LocalDate,OffsetTime");
assertFalse(FileUtils.isFileNewer(oldFile, localDatePlusDay), "Old File - Newer - LocalDate plus one day");
- assertFalse(FileUtils.isFileNewer(oldFile, localDatePlusDay, localTime), "Old File - Newer - LocalDate plus one day,LocalTime");
- assertFalse(FileUtils.isFileNewer(oldFile, localDatePlusDay, offsetTime), "Old File - Newer - LocalDate plus one day,OffsetTime");
+ assertFalse(FileUtils.isFileNewer(oldFile, localDatePlusDay, localTime0), "Old File - Newer - LocalDate plus one day,LocalTime");
+ assertFalse(FileUtils.isFileNewer(oldFile, localDatePlusDay, offsetTime0), "Old File - Newer - LocalDate plus one day,OffsetTime");
- assertTrue(FileUtils.isFileNewer(newFile, reference), "New File - Newer - File");
+ assertTrue(FileUtils.isFileNewer(newFile, refFile), "New File - Newer - File");
assertTrue(FileUtils.isFileNewer(newFile, date), "New File - Newer - Date");
assertTrue(FileUtils.isFileNewer(newFile, now), "New File - Newer - Mili");
assertTrue(FileUtils.isFileNewer(newFile, instant), "New File - Newer - Instant");
@@ -1672,17 +1669,16 @@ public void testIsFileNewerOlder() throws Exception {
assertTrue(FileUtils.isFileNewer(newFile, localDateTime), "New File - Newer - LocalDateTime");
assertTrue(FileUtils.isFileNewer(newFile, localDateTime, ZoneId.systemDefault()), "New File - Newer - LocalDateTime,ZoneId");
assertFalse(FileUtils.isFileNewer(newFile, localDate), "New File - Newer - LocalDate");
- assertTrue(FileUtils.isFileNewer(newFile, localDate, localTime), "New File - Newer - LocalDate,LocalTime");
- assertTrue(FileUtils.isFileNewer(newFile, localDate, offsetTime), "New File - Newer - LocalDate,OffsetTime");
+ assertTrue(FileUtils.isFileNewer(newFile, localDate, localTime0), "New File - Newer - LocalDate,LocalTime");
+ assertTrue(FileUtils.isFileNewer(newFile, localDate, offsetTime0), "New File - Newer - LocalDate,OffsetTime");
assertFalse(FileUtils.isFileNewer(newFile, localDatePlusDay), "New File - Newer - LocalDate plus one day");
- assertFalse(FileUtils.isFileNewer(newFile, localDatePlusDay, localTime), "New File - Newer - LocalDate plus one day,LocalTime");
- assertFalse(FileUtils.isFileNewer(newFile, localDatePlusDay, offsetTime), "New File - Newer - LocalDate plus one day,OffsetTime");
- assertFalse(FileUtils.isFileNewer(invalidFile, reference), "Invalid - Newer - File");
- final String invalidFileName = invalidFile.getName();
+ assertFalse(FileUtils.isFileNewer(newFile, localDatePlusDay, localTime0), "New File - Newer - LocalDate plus one day,LocalTime");
+ assertFalse(FileUtils.isFileNewer(newFile, localDatePlusDay, offsetTime0), "New File - Newer - LocalDate plus one day,OffsetTime");
+ assertFalse(FileUtils.isFileNewer(invalidFile, refFile), "Invalid - Newer - File");
assertThrows(IllegalArgumentException.class, () -> FileUtils.isFileNewer(newFile, invalidFile));
// Test isFileOlder()
- assertTrue(FileUtils.isFileOlder(oldFile, reference), "Old File - Older - File");
+ assertTrue(FileUtils.isFileOlder(oldFile, refFile), "Old File - Older - File");
assertTrue(FileUtils.isFileOlder(oldFile, date), "Old File - Older - Date");
assertTrue(FileUtils.isFileOlder(oldFile, now), "Old File - Older - Mili");
assertTrue(FileUtils.isFileOlder(oldFile, instant), "Old File - Older - Instant");
@@ -1691,13 +1687,13 @@ public void testIsFileNewerOlder() throws Exception {
assertTrue(FileUtils.isFileOlder(oldFile, localDateTime), "Old File - Older - LocalDateTime");
assertTrue(FileUtils.isFileOlder(oldFile, localDateTime, ZoneId.systemDefault()), "Old File - Older - LocalDateTime,LocalTime");
assertTrue(FileUtils.isFileOlder(oldFile, localDate), "Old File - Older - LocalDate");
- assertFalse(FileUtils.isFileOlder(oldFile, localDate, localTime), "Old File - Older - LocalDate,LocalTime");
- assertFalse(FileUtils.isFileOlder(oldFile, localDate, offsetTime), "Old File - Older - LocalDate,OffsetTime");
+ assertFalse(FileUtils.isFileOlder(oldFile, localDate, localTime0), "Old File - Older - LocalDate,LocalTime");
+ assertFalse(FileUtils.isFileOlder(oldFile, localDate, offsetTime0), "Old File - Older - LocalDate,OffsetTime");
assertTrue(FileUtils.isFileOlder(oldFile, localDatePlusDay), "Old File - Older - LocalDate plus one day");
- assertTrue(FileUtils.isFileOlder(oldFile, localDatePlusDay, localTime), "Old File - Older - LocalDate plus one day,LocalTime");
- assertTrue(FileUtils.isFileOlder(oldFile, localDatePlusDay, offsetTime), "Old File - Older - LocalDate plus one day,OffsetTime");
+ assertTrue(FileUtils.isFileOlder(oldFile, localDatePlusDay, localTime0), "Old File - Older - LocalDate plus one day,LocalTime");
+ assertTrue(FileUtils.isFileOlder(oldFile, localDatePlusDay, offsetTime0), "Old File - Older - LocalDate plus one day,OffsetTime");
- assertFalse(FileUtils.isFileOlder(newFile, reference), "New File - Older - File");
+ assertFalse(FileUtils.isFileOlder(newFile, refFile), "New File - Older - File");
assertFalse(FileUtils.isFileOlder(newFile, date), "New File - Older - Date");
assertFalse(FileUtils.isFileOlder(newFile, now), "New File - Older - Mili");
assertFalse(FileUtils.isFileOlder(newFile, instant), "New File - Older - Instant");
@@ -1706,16 +1702,15 @@ public void testIsFileNewerOlder() throws Exception {
assertFalse(FileUtils.isFileOlder(newFile, localDateTime), "New File - Older - LocalDateTime");
assertFalse(FileUtils.isFileOlder(newFile, localDateTime, ZoneId.systemDefault()), "New File - Older - LocalDateTime,ZoneId");
assertTrue(FileUtils.isFileOlder(newFile, localDate), "New File - Older - LocalDate");
- assertFalse(FileUtils.isFileOlder(newFile, localDate, localTime), "New File - Older - LocalDate,LocalTime");
- assertFalse(FileUtils.isFileOlder(newFile, localDate, offsetTime), "New File - Older - LocalDate,OffsetTime");
+ assertFalse(FileUtils.isFileOlder(newFile, localDate, localTime0), "New File - Older - LocalDate,LocalTime");
+ assertFalse(FileUtils.isFileOlder(newFile, localDate, offsetTime0), "New File - Older - LocalDate,OffsetTime");
assertTrue(FileUtils.isFileOlder(newFile, localDatePlusDay), "New File - Older - LocalDate plus one day");
- assertTrue(FileUtils.isFileOlder(newFile, localDatePlusDay, localTime), "New File - Older - LocalDate plus one day,LocalTime");
- assertTrue(FileUtils.isFileOlder(newFile, localDatePlusDay, offsetTime), "New File - Older - LocalDate plus one day,OffsetTime");
+ assertTrue(FileUtils.isFileOlder(newFile, localDatePlusDay, localTime0), "New File - Older - LocalDate plus one day,LocalTime");
+ assertTrue(FileUtils.isFileOlder(newFile, localDatePlusDay, offsetTime0), "New File - Older - LocalDate plus one day,OffsetTime");
- assertFalse(FileUtils.isFileOlder(invalidFile, reference), "Invalid - Older - File");
+ assertFalse(FileUtils.isFileOlder(invalidFile, refFile), "Invalid - Older - File");
assertThrows(IllegalArgumentException.class, () -> FileUtils.isFileOlder(newFile, invalidFile));
- // ----- Test isFileNewer() exceptions -----
// Null File
assertThrows(NullPointerException.class, () -> FileUtils.isFileNewer(null, now));

Fix new API and convert its tests to use fixed file times

The goal is to avoid the vagueries and randomness of local file systems
and operating systems.