Skip to content

Commit 4cb5b1d

Browse files
committed
Reproduce issue 2592
1 parent 031d4da commit 4cb5b1d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
import static org.junit.Assert.assertNotEquals;
2121
import static org.junit.Assert.assertNotNull;
2222
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertTrue;
2324
import static org.junit.Assert.fail;
2425

26+
import java.io.ByteArrayOutputStream;
2527
import java.io.File;
2628
import java.io.FileInputStream;
2729
import java.io.IOException;
2830
import java.io.InputStreamReader;
31+
import java.io.OutputStream;
2932
import java.io.Reader;
33+
import java.nio.ByteBuffer;
3034
import java.nio.charset.StandardCharsets;
35+
import java.nio.file.Files;
3136
import org.apache.logging.log4j.core.LoggerContext;
3237
import org.apache.logging.log4j.core.appender.RollingFileAppender;
3338
import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
@@ -188,4 +193,40 @@ public void testCreateParentDir() {
188193
manager.close();
189194
}
190195
}
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+
}
191232
}

0 commit comments

Comments
 (0)