Skip to content

Commit 71bfef4

Browse files
author
Gary Gregory
committed
2 parents 3ac63df + 291a35e commit 71bfef4

6 files changed

Lines changed: 41 additions & 25 deletions

File tree

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ The <action> type attribute can be add,update,fix,remove.
116116
<action dev="ggregory" type="update" due-to="Dependabot">
117117
Update spotbugs from 4.0.6 to 4.1.1 #134.
118118
</action>
119+
<action issue="IO-681" dev="sebb" type="add">
120+
IO-681 IOUtils.close(Closeable) should allow a list of closeables
121+
</action>
122+
<action issue="IO-672" dev="sebb" type="fix">
123+
Copying a File sets last modified date to 01 January 1970
124+
</action>
119125
</release>
120126
<!-- The release date is the date RC is cut -->
121127
<release version="2.7" date="2020-05-24" description="Java 8 required.">

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,15 +1394,17 @@ private static boolean doCopyFile(final File srcFile, final File destFile, final
13941394

13951395
final Path srcPath = srcFile.toPath();
13961396
final Path destPath = destFile.toPath();
1397-
final long newLastModifed = preserveFileDate ? srcFile.lastModified() : destFile.lastModified();
13981397
Files.copy(srcPath, destPath, copyOptions);
13991398

14001399
// TODO IO-386: Do we still need this check?
14011400
checkEqualSizes(srcFile, destFile, Files.size(srcPath), Files.size(destPath));
14021401
// TODO IO-386: Do we still need this check?
14031402
checkEqualSizes(srcFile, destFile, srcFile.length(), destFile.length());
14041403

1405-
return destFile.setLastModified(newLastModifed);
1404+
if (preserveFileDate) {
1405+
return destFile.setLastModified(srcFile.lastModified());
1406+
}
1407+
return true;
14061408
}
14071409

14081410
//-----------------------------------------------------------------------

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,15 @@ public static void closeQuietly(final Closeable closeable, final Consumer<IOExce
367367
/**
368368
* Closes the given {@link Closeable} as a null-safe operation.
369369
*
370-
* @param closeable The resource to close, may be null.
370+
* @param closeables The resource(s) to close, may be null.
371371
* @throws IOException if an I/O error occurs.
372372
* @since 2.7
373373
*/
374-
public static void close(final Closeable closeable) throws IOException {
375-
if (closeable != null) {
376-
closeable.close();
374+
public static void close(final Closeable... closeables) throws IOException {
375+
for(Closeable closeable : closeables) {
376+
if (closeable != null) {
377+
closeable.close();
378+
}
377379
}
378380
}
379381

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2223
import static org.junit.jupiter.api.Assertions.assertNotNull;
2324
import static org.junit.jupiter.api.Assertions.assertNull;
2425
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -1144,16 +1145,12 @@ public void testIsFileNewerOlder() throws Exception {
11441145
public void testCopyFile1() throws Exception {
11451146
final File destination = new File(temporaryFolder, "copy1.txt");
11461147

1147-
//Thread.sleep(LAST_MODIFIED_DELAY);
1148-
//This is to slow things down so we can catch if
1149-
//the lastModified date is not ok
1148+
backDateFile(testFile1); // set test file back 10 minutes
11501149

11511150
FileUtils.copyFile(testFile1, destination);
11521151
assertTrue(destination.exists(), "Check Exist");
11531152
assertEquals(testFile1Size, destination.length(), "Check Full copy");
1154-
/* disabled: Thread.sleep doesn't work reliantly for this case
1155-
assertTrue("Check last modified date preserved",
1156-
testFile1.lastModified() == destination.lastModified());*/
1153+
assertEquals(testFile1.lastModified(), destination.lastModified(), "Check last modified date preserved");
11571154
}
11581155

11591156
@Test
@@ -1196,15 +1193,12 @@ public void testCopyFileLarge() throws Exception {
11961193
public void testCopyFile2() throws Exception {
11971194
final File destination = new File(temporaryFolder, "copy2.txt");
11981195

1199-
//Thread.sleep(LAST_MODIFIED_DELAY);
1200-
//This is to slow things down so we can catch if
1201-
//the lastModified date is not ok
1196+
backDateFile(testFile1); // set test file back 10 minutes
12021197

12031198
FileUtils.copyFile(testFile1, destination);
12041199
assertTrue(destination.exists(), "Check Exist");
12051200
assertEquals(testFile2Size, destination.length(), "Check Full copy");
1206-
/* disabled: Thread.sleep doesn't work reliably for this case
1207-
assertTrue(testFile1.lastModified() == destination.lastModified(), "Check last modified date preserved");*/
1201+
assertEquals(testFile1.lastModified() , destination.lastModified(), "Check last modified date preserved");
12081202
}
12091203

12101204
@Test
@@ -1225,16 +1219,15 @@ public void testCopyToSelf() throws Exception {
12251219
public void testCopyFile2WithoutFileDatePreservation() throws Exception {
12261220
final File destination = new File(temporaryFolder, "copy2.txt");
12271221

1228-
//Thread.sleep(LAST_MODIFIED_DELAY);
1229-
//This is to slow things down so we can catch if
1230-
//the lastModified date is not ok
1222+
backDateFile(testFile1); // set test file back 10 minutes
12311223

1224+
final long now = new Date().getTime()-1000L; // destination file time should not be less than this (allowing for granularity)
12321225
FileUtils.copyFile(testFile1, destination, false);
12331226
assertTrue(destination.exists(), "Check Exist");
12341227
assertEquals(testFile2Size, destination.length(), "Check Full copy");
1235-
/* disabled: Thread.sleep doesn't work reliantly for this case
1236-
assertTrue("Check last modified date modified",
1237-
testFile1.lastModified() != destination.lastModified());*/
1228+
final long destLastMod = destination.lastModified();
1229+
assertNotEquals(testFile1.lastModified(), destLastMod, "Check last modified date not same as input");
1230+
assertTrue(destLastMod > now, destLastMod + " > " + now);
12381231
}
12391232

12401233
@Test
@@ -3083,6 +3076,12 @@ public void testIncorrectOutputSize() throws Exception {
30833076
}
30843077
}
30853078

3079+
private void backDateFile(File testFile){
3080+
final long mins10 = 1000*60*10;
3081+
final long lastModified1 = testFile.lastModified();
3082+
testFile.setLastModified(lastModified1-mins10);
3083+
assertNotEquals(testFile.lastModified(), lastModified1, "Should have changed source date"); // ensure it was changed
3084+
}
30863085
/**
30873086
* DirectoryWalker implementation that recursively lists all files and directories.
30883087
*/

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ public void setUp() {
146146
assertThrows(IOException.class, () -> IOUtils.close(new YellOnCloseReader(new StringReader("s"))));
147147
}
148148

149+
@Test public void testCloseMulti() {
150+
Closeable nulCloseable = null;
151+
Closeable [] closeables = {null, null};
152+
assertDoesNotThrow(() -> IOUtils.close(nulCloseable,nulCloseable));
153+
assertDoesNotThrow(() -> IOUtils.close(closeables));
154+
assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"),nulCloseable));
155+
assertThrows(IOException.class, () -> IOUtils.close(nulCloseable, new YellOnCloseReader(new StringReader("s"))));
156+
}
157+
149158
@Test public void testCloseConsumer() {
150159
Closeable nulCloseable = null;
151160
assertDoesNotThrow(() -> IOUtils.close(nulCloseable, null)); // null consumer
@@ -330,7 +339,6 @@ public synchronized void close() throws IOException {
330339

331340
@Test public void testContentEqualsIgnoreEOL() throws Exception {
332341
{
333-
final Reader input1 = new CharArrayReader("".toCharArray());
334342
assertTrue(IOUtils.contentEqualsIgnoreEOL((Reader) null, null));
335343
}
336344
{

src/test/java/org/apache/commons/io/output/NullAppendableTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class NullAppendableTest {
2727

2828
@Test
2929
public void testNull() throws IOException {
30-
final char[] chars = new char[] {'A', 'B', 'C'};
3130
final Appendable appendable = NullAppendable.INSTANCE;
3231
appendable.append('a');
3332
appendable.append("A");

0 commit comments

Comments
 (0)