Skip to content

Commit cb87c5e

Browse files
committed
Use Files.delete() for better error reporting
Signed-off-by: Elimelec Burghelea <[email protected]>
1 parent ef16008 commit cb87c5e

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import java.nio.channels.FileChannel;
2626
import java.nio.charset.StandardCharsets;
2727
import java.nio.charset.UnsupportedCharsetException;
28+
import java.nio.file.Files;
2829

2930
import org.apache.commons.logging.Log;
3031
import org.apache.commons.logging.LogFactory;
@@ -61,6 +62,7 @@
6162
* @author Mahmoud Ben Hassine
6263
* @author Glenn Renfro
6364
* @author Remi Kaeffer
65+
* @author Elimelec Burghelea
6466
* @since 4.1
6567
*/
6668
public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWriter<T>
@@ -268,11 +270,9 @@ public void close() {
268270
state.close();
269271
if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
270272
try {
271-
if (!resource.getFile().delete()) {
272-
throw new ItemStreamException("Failed to delete empty file on close");
273-
}
273+
Files.delete(resource.getFile().toPath());
274274
}
275-
catch (IOException e) {
275+
catch (IOException | SecurityException e) {
276276
throw new ItemStreamException("Failed to delete empty file on close", e);
277277
}
278278
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2024 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.nio.file.Files;
2122

2223
import org.springframework.batch.item.ItemStreamException;
2324
import org.springframework.util.Assert;
@@ -28,6 +29,7 @@
2829
* @author Peter Zozom
2930
* @author Mahmoud Ben Hassine
3031
* @author Taeik Lim
32+
* @author Elimelec Burghelea
3133
*/
3234
public abstract class FileUtils {
3335

@@ -57,8 +59,11 @@ public static void setUpOutputFile(File file, boolean restarted, boolean append,
5759
if (!overwriteOutputFile) {
5860
throw new ItemStreamException("File already exists: [" + file.getAbsolutePath() + "]");
5961
}
60-
if (!file.delete()) {
61-
throw new IOException("Could not delete file: " + file);
62+
try {
63+
Files.delete(file.toPath());
64+
}
65+
catch (IOException | SecurityException e) {
66+
throw new IOException("Could not delete file: " + file, e);
6267
}
6368
}
6469

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import java.io.UnsupportedEncodingException;
2525
import java.io.Writer;
2626
import java.nio.channels.FileChannel;
27+
import java.nio.file.Files;
2728
import java.util.Collections;
2829
import java.util.List;
2930
import java.util.Map;
@@ -75,6 +76,7 @@
7576
* @author Michael Minella
7677
* @author Parikshit Dutta
7778
* @author Mahmoud Ben Hassine
79+
* @author Elimelec Burghelea
7880
*/
7981
public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T>
8082
implements ResourceAwareItemWriterItemStream<T>, InitializingBean {
@@ -726,11 +728,9 @@ public void close() {
726728
}
727729
if (currentRecordCount == 0 && shouldDeleteIfEmpty) {
728730
try {
729-
if (!resource.getFile().delete()) {
730-
throw new ItemStreamException("Failed to delete empty file on close");
731-
}
731+
Files.delete(resource.getFile().toPath());
732732
}
733-
catch (IOException e) {
733+
catch (IOException | SecurityException e) {
734734
throw new ItemStreamException("Failed to delete empty file on close", e);
735735
}
736736
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractFileItemWriterTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.batch.item.support;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
import static org.junit.jupiter.api.Assertions.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
2222
import static org.mockito.Mockito.when;
2323

@@ -55,8 +55,7 @@ void testFailedFileDeletionThrowsException() {
5555
"Expected exception when file deletion fails");
5656

5757
assertEquals("Failed to delete empty file on close", exception.getMessage(), "Wrong exception message");
58-
// FIXME: update after fix, because we will have a reason
59-
assertNull(exception.getCause(), "Exception should not have a cause");
58+
assertNotNull(exception.getCause(), "Exception should have a cause");
6059
}
6160

6261
private static class TestFileItemWriter extends AbstractFileItemWriter<String> {

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.springframework.batch.item.ItemStreamException;
2828
import org.springframework.util.Assert;
2929

30-
import static org.junit.Assert.assertNull;
3130
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3232
import static org.junit.jupiter.api.Assertions.assertThrows;
3333
import static org.junit.jupiter.api.Assertions.assertTrue;
3434
import static org.junit.jupiter.api.Assertions.fail;
@@ -210,8 +210,7 @@ public boolean delete() {
210210
assertTrue(message.startsWith("Unable to create file"), "Wrong message: " + message);
211211
assertTrue(ex.getCause() instanceof IOException);
212212
assertTrue(ex.getCause().getMessage().startsWith("Could not delete file"), "Wrong message: " + message);
213-
// FIXME: update after fix, because we will have a reason
214-
assertNull(ex.getCause().getCause());
213+
assertNotNull(ex.getCause().getCause(), "Exception should have a cause");
215214
}
216215
finally {
217216
file.delete();

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
import static org.junit.jupiter.api.Assertions.assertEquals;
5050
import static org.junit.jupiter.api.Assertions.assertFalse;
51-
import static org.junit.jupiter.api.Assertions.assertNull;
51+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5252
import static org.junit.jupiter.api.Assertions.assertThrows;
5353
import static org.junit.jupiter.api.Assertions.assertTrue;
5454
import static org.mockito.Mockito.mock;
@@ -852,8 +852,7 @@ void testFailedFileDeletionThrowsException() throws IOException {
852852
"Expected exception when file deletion fails");
853853

854854
assertEquals("Failed to delete empty file on close", exception.getMessage(), "Wrong exception message");
855-
// FIXME: update after fix, because we will have a reason
856-
assertNull(exception.getCause(), "Exception should not have a cause");
855+
assertNotNull(exception.getCause(), "Exception should have a cause");
857856
}
858857

859858
private void initWriterForSimpleCallbackTests() throws Exception {

0 commit comments

Comments
 (0)