Address -1 from Sebb by only stamping target file with the source's
timestamp if requested and ignoring test assertions on Windows since the
JRE Files class copies the last modified time by default for me on
Microsoft Windows [Version 10.0.16299.1992] and OpenJDK Runtime
Environment (AdoptOpenJDK)(build 1.8.0_265-b01).
MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -1394,6 +1394,7 @@ private static boolean doCopyFile(final File srcFile, final File destFile, final
final Path srcPath = srcFile.toPath();
final Path destPath = destFile.toPath();
+ // On Windows, the last modified time is copied by default.
Files.copy(srcPath, destPath, copyOptions);
// TODO IO-386: Do we still need this check?
@@ -1401,7 +1402,7 @@ private static boolean doCopyFile(final File srcFile, final File destFile, final
// TODO IO-386: Do we still need this check?
checkEqualSizes(srcFile, destFile, srcFile.length(), destFile.length());
- return destFile.setLastModified(preserveFileDate ? srcFile.lastModified() : System.currentTimeMillis());
+ return preserveFileDate ? destFile.setLastModified(srcFile.lastModified()) : true;
}
//-----------------------------------------------------------------------

MODIFY
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.io.test.TestUtils;
+import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -1230,15 +1231,18 @@ public void testCopyFile2WithoutFileDatePreservation() throws Exception {
// destination file time should not be less than this (allowing for granularity)
final long now = System.currentTimeMillis() - 1000L;
+ // On Windows, the last modified time is copied by default.
FileUtils.copyFile(testFile1, destFile, false);
assertTrue(destFile.exists(), "Check Exist");
assertEquals(testFile1Size, destFile.length(), "Check Full copy");
final long destLastMod = getLastModifiedMillis(destFile);
final long unexpected = getLastModifiedMillis(testFile1);
+ if (!SystemUtils.IS_OS_WINDOWS) {
assertNotEquals(unexpected, destLastMod,
"Check last modified date not same as input, delta " + (destLastMod - unexpected));
assertTrue(destLastMod > now, destLastMod + " > " + now);
}
+ }
@Test
public void testCopyDirectoryToDirectory_NonExistingDest() throws Exception {
@@ -1407,10 +1411,13 @@ public void testCopyDirectoryPreserveDates() throws Exception {
final File targetFile = new File(targetDirectory, "hello.txt");
// Test with preserveFileDate disabled
+ // On Windows, the last modified time is copied by default.
FileUtils.copyDirectory(source, target, false);
assertNotEquals(DATE1, getLastModifiedMillis(target));
assertNotEquals(DATE2, getLastModifiedMillis(targetDirectory));
+ if (!SystemUtils.IS_OS_WINDOWS) {
assertNotEquals(DATE3, getLastModifiedMillis(targetFile));
+ }
FileUtils.deleteDirectory(target);
// Test with preserveFileDate enabled

Address -1 from Sebb by only stamping target file with the source's
timestamp if requested and ignoring test assertions on Windows since the
JRE Files class copies the last modified time by default for me on
Microsoft Windows [Version 10.0.16299.1992] and OpenJDK Runtime
Environment (AdoptOpenJDK)(build 1.8.0_265-b01).