|
17 | 17 | package org.apache.logging.log4j.core.appender.rolling;
|
18 | 18 |
|
19 | 19 | import static org.junit.Assert.assertEquals;
|
20 |
| -import static org.junit.Assert.assertNotEquals; |
21 | 20 | import static org.junit.Assert.assertNotNull;
|
22 | 21 | import static org.junit.Assert.assertNull;
|
| 22 | +import static org.junit.Assert.assertTrue; |
23 | 23 | import static org.junit.Assert.fail;
|
24 | 24 |
|
| 25 | +import java.io.ByteArrayOutputStream; |
25 | 26 | import java.io.File;
|
26 | 27 | import java.io.FileInputStream;
|
27 | 28 | import java.io.IOException;
|
28 | 29 | import java.io.InputStreamReader;
|
| 30 | +import java.io.OutputStream; |
29 | 31 | import java.io.Reader;
|
| 32 | +import java.nio.ByteBuffer; |
30 | 33 | import java.nio.charset.StandardCharsets;
|
| 34 | +import java.nio.file.Files; |
31 | 35 | import org.apache.logging.log4j.core.LoggerContext;
|
32 | 36 | import org.apache.logging.log4j.core.appender.RollingFileAppender;
|
33 | 37 | import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
|
@@ -138,21 +142,18 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
|
138 | 142 | assertNotNull(manager);
|
139 | 143 | manager.initialize();
|
140 | 144 |
|
141 |
| - // Get the initialTime of this original log file |
142 |
| - final long initialTime = manager.getFileTime(); |
143 |
| - |
144 | 145 | // Log something to ensure that the existing file size is > 0
|
145 | 146 | final String testContent = "Test";
|
146 | 147 | manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
|
147 | 148 |
|
148 | 149 | // Trigger rollover that will fail
|
149 | 150 | manager.rollover();
|
150 | 151 |
|
151 |
| - // If the rollover fails, then the size should not be reset |
152 |
| - assertNotEquals(0, manager.getFileSize()); |
| 152 | + // If the rollover fails, then the log file should be unchanged |
| 153 | + assertEquals(file.getAbsolutePath(), manager.getFileName()); |
153 | 154 |
|
154 |
| - // The initialTime should not have changed |
155 |
| - assertEquals(initialTime, manager.getFileTime()); |
| 155 | + // The logged content should be unchanged |
| 156 | + assertEquals(testContent, new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII)); |
156 | 157 | }
|
157 | 158 |
|
158 | 159 | @Test
|
@@ -188,4 +189,39 @@ public void testCreateParentDir() {
|
188 | 189 | manager.close();
|
189 | 190 | }
|
190 | 191 | }
|
| 192 | + |
| 193 | + @Test |
| 194 | + @Issue("https://github.com/apache/logging-log4j2/issues/2592") |
| 195 | + public void testRolloverOfDeletedFile() throws IOException { |
| 196 | + final File file = File.createTempFile("testRolloverOfDeletedFile", "log"); |
| 197 | + file.deleteOnExit(); |
| 198 | + final String testContent = "Test"; |
| 199 | + try (final OutputStream os = |
| 200 | + new ByteArrayOutputStream(); // use a dummy OutputStream so that the real file can be deleted |
| 201 | + final RollingFileManager manager = new RollingFileManager( |
| 202 | + null, |
| 203 | + file.getAbsolutePath(), |
| 204 | + "testRolloverOfDeletedFile.log.%d{yyyy-MM-dd}", |
| 205 | + os, |
| 206 | + true, |
| 207 | + false, |
| 208 | + 0, |
| 209 | + System.currentTimeMillis(), |
| 210 | + OnStartupTriggeringPolicy.createPolicy(1), |
| 211 | + DefaultRolloverStrategy.newBuilder().build(), |
| 212 | + file.getName(), |
| 213 | + null, |
| 214 | + null, |
| 215 | + null, |
| 216 | + null, |
| 217 | + false, |
| 218 | + ByteBuffer.allocate(256))) { |
| 219 | + assertTrue(file.delete()); |
| 220 | + manager.setRenameEmptyFiles(true); |
| 221 | + manager.rollover(); |
| 222 | + assertEquals(file.getAbsolutePath(), manager.getFileName()); |
| 223 | + manager.writeBytes(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length()); |
| 224 | + } |
| 225 | + assertEquals(testContent, new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII)); |
| 226 | + } |
191 | 227 | }
|
0 commit comments