|
16 | 16 |
|
17 | 17 | package org.springframework.batch.infrastructure.item.support; |
18 | 18 |
|
19 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
21 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
22 | 20 | import static org.mockito.Mockito.when; |
23 | 21 |
|
24 | 22 | import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.RandomAccessFile; |
25 | 25 |
|
26 | 26 | import org.junit.jupiter.api.Test; |
27 | 27 | import org.mockito.Mockito; |
|
39 | 39 | class AbstractFileItemWriterTests { |
40 | 40 |
|
41 | 41 | @Test |
42 | | - void testFailedFileDeletionThrowsException() { |
| 42 | + void testFailedFileDeletionThrowsException() throws Exception { |
| 43 | + |
43 | 44 | File outputFile = new File("target/data/output.tmp"); |
44 | | - File mocked = Mockito.spy(outputFile); |
| 45 | + outputFile.getParentFile().mkdirs(); |
45 | 46 |
|
46 | 47 | TestFileItemWriter writer = new TestFileItemWriter(); |
47 | | - |
48 | | - writer.setResource(new FileSystemResource(mocked)); |
| 48 | + writer.setResource(new FileSystemResource(outputFile)); |
49 | 49 | writer.setShouldDeleteIfEmpty(true); |
50 | 50 | writer.setName(writer.getClass().getSimpleName()); |
| 51 | + |
51 | 52 | writer.open(new ExecutionContext()); |
52 | 53 |
|
53 | | - when(mocked.delete()).thenReturn(false); |
| 54 | + // Keep the file open so Files.delete(...) cannot delete it (Windows) |
| 55 | + RandomAccessFile lock = new RandomAccessFile(outputFile, "rw"); |
54 | 56 |
|
55 | | - ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close, |
56 | | - "Expected exception when file deletion fails"); |
| 57 | + try { |
| 58 | + ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close); |
57 | 59 |
|
58 | | - assertEquals("Failed to delete empty file on close", exception.getMessage(), "Wrong exception message"); |
59 | | - assertNotNull(exception.getCause(), "Exception should have a cause"); |
| 60 | + assertEquals("Failed to delete empty file on close", exception.getMessage()); |
| 61 | + |
| 62 | + assertNotNull(exception.getCause()); |
| 63 | + assertTrue(exception.getCause() instanceof IOException); |
| 64 | + } |
| 65 | + finally { |
| 66 | + lock.close(); |
| 67 | + } |
60 | 68 | } |
61 | 69 |
|
62 | 70 | private static class TestFileItemWriter extends AbstractFileItemWriter<String> { |
|
0 commit comments