Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package org.springframework.batch.infrastructure.item.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -39,24 +39,32 @@
class AbstractFileItemWriterTests {

@Test
void testFailedFileDeletionThrowsException() {
void testFailedFileDeletionThrowsException() throws Exception {

File outputFile = new File("target/data/output.tmp");
File mocked = Mockito.spy(outputFile);
outputFile.getParentFile().mkdirs();

TestFileItemWriter writer = new TestFileItemWriter();

writer.setResource(new FileSystemResource(mocked));
writer.setResource(new FileSystemResource(outputFile));
writer.setShouldDeleteIfEmpty(true);
writer.setName(writer.getClass().getSimpleName());

writer.open(new ExecutionContext());

when(mocked.delete()).thenReturn(false);
// Keep the file open so Files.delete(...) cannot delete it (Windows)
RandomAccessFile lock = new RandomAccessFile(outputFile, "rw");

ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close,
"Expected exception when file deletion fails");
try {
ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close);

assertEquals("Failed to delete empty file on close", exception.getMessage(), "Wrong exception message");
assertNotNull(exception.getCause(), "Exception should have a cause");
assertEquals("Failed to delete empty file on close", exception.getMessage());

assertNotNull(exception.getCause());
assertTrue(exception.getCause() instanceof IOException);
}
finally {
lock.close();
}
}

private static class TestFileItemWriter extends AbstractFileItemWriter<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Collections;

import javax.xml.stream.XMLEventFactory;
Expand Down Expand Up @@ -832,19 +833,29 @@ void testOpenAndCloseTagsInComplexCallbacksRestart() throws Exception {
* indicate the deletion attempt failed.
*/
@Test
void testFailedFileDeletionThrowsException() throws IOException {
File mockedFile = spy(resource.getFile());
writer.setResource(new FileSystemResource(mockedFile));
void testFailedFileDeletionThrowsException() throws Exception {

File outputFile = resource.getFile();

writer.setResource(new FileSystemResource(outputFile));
writer.setShouldDeleteIfEmpty(true);
writer.open(executionContext);

when(mockedFile.delete()).thenReturn(false);
// Keep the file locked so Files.delete(...) fails on Windows
RandomAccessFile lock = new RandomAccessFile(outputFile, "rw");

try {
ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close,
"Expected exception when file deletion fails");

ItemStreamException exception = assertThrows(ItemStreamException.class, () -> writer.close(),
"Expected exception when file deletion fails");
assertEquals("Failed to delete empty file on close", exception.getMessage());

assertEquals("Failed to delete empty file on close", exception.getMessage(), "Wrong exception message");
assertNotNull(exception.getCause(), "Exception should have a cause");
assertNotNull(exception.getCause());
assertTrue(exception.getCause() instanceof IOException);
}
finally {
lock.close();
}
}

private void initWriterForSimpleCallbackTests() throws Exception {
Expand Down