|
20 | 20 | import static org.junit.Assert.assertNotEquals; |
21 | 21 | import static org.junit.Assert.assertNotNull; |
22 | 22 | import static org.junit.Assert.assertNull; |
| 23 | +import static org.junit.Assert.assertTrue; |
23 | 24 | import static org.junit.Assert.fail; |
24 | 25 |
|
| 26 | +import java.io.ByteArrayOutputStream; |
25 | 27 | import java.io.File; |
26 | 28 | import java.io.FileInputStream; |
27 | 29 | import java.io.IOException; |
28 | 30 | import java.io.InputStreamReader; |
| 31 | +import java.io.OutputStream; |
29 | 32 | import java.io.Reader; |
| 33 | +import java.nio.ByteBuffer; |
30 | 34 | import java.nio.charset.StandardCharsets; |
| 35 | +import java.nio.file.Files; |
31 | 36 | import org.apache.logging.log4j.core.LoggerContext; |
32 | 37 | import org.apache.logging.log4j.core.appender.RollingFileAppender; |
33 | 38 | import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction; |
@@ -188,4 +193,40 @@ public void testCreateParentDir() { |
188 | 193 | manager.close(); |
189 | 194 | } |
190 | 195 | } |
| 196 | + |
| 197 | + @Test |
| 198 | + @Issue("https://github.com/apache/logging-log4j2/issues/2592") |
| 199 | + public void testRolloverOfDeletedFile() throws IOException { |
| 200 | + final Configuration configuration = new NullConfiguration(); |
| 201 | + final File file = File.createTempFile("testRolloverOfDeletedFile", "log"); |
| 202 | + file.deleteOnExit(); |
| 203 | + final String testContent = "Test"; |
| 204 | + try (final OutputStream os = |
| 205 | + new ByteArrayOutputStream(); // use a dummy OutputStream so that the real file can be deleted |
| 206 | + final RollingFileManager manager = new RollingFileManager( |
| 207 | + configuration.getLoggerContext(), |
| 208 | + file.getAbsolutePath(), |
| 209 | + "testRolloverOfDeletedFile.log.%d{yyyy-MM-dd}", |
| 210 | + os, |
| 211 | + true, |
| 212 | + false, |
| 213 | + 0, |
| 214 | + System.currentTimeMillis(), |
| 215 | + OnStartupTriggeringPolicy.createPolicy(1), |
| 216 | + DefaultRolloverStrategy.newBuilder().build(), |
| 217 | + file.getName(), |
| 218 | + PatternLayout.createDefaultLayout(configuration), |
| 219 | + null, |
| 220 | + null, |
| 221 | + null, |
| 222 | + false, |
| 223 | + ByteBuffer.allocate(256))) { |
| 224 | + assertTrue(file.delete()); |
| 225 | + manager.setRenameEmptyFiles(true); |
| 226 | + manager.rollover(); |
| 227 | + assertEquals(file.getAbsolutePath(), manager.getFileName()); |
| 228 | + manager.writeBytes(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length()); |
| 229 | + } |
| 230 | + assertEquals(testContent, new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII)); |
| 231 | + } |
191 | 232 | } |
0 commit comments