|
20 | 20 |
|
21 | 21 | import static org.apache.commons.lang3.JavaVersion.JAVA_14;
|
22 | 22 | import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
|
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.hamcrest.Matchers.not; |
23 | 25 | import static org.jpos.util.LogFileTestUtils.getStringFromCompressedFile;
|
24 | 26 | import static org.jpos.util.LogFileTestUtils.getStringFromFile;
|
25 | 27 | import static org.junit.jupiter.api.Assertions.*;
|
| 28 | +import static org.hamcrest.io.FileMatchers.anExistingFile; |
| 29 | +import static org.hamcrest.MatcherAssert.*; |
26 | 30 |
|
27 | 31 | import java.io.*;
|
28 | 32 | import java.nio.file.Files;
|
29 | 33 | import java.nio.file.LinkOption;
|
30 | 34 | import java.nio.file.Path;
|
31 | 35 | import java.nio.file.Paths;
|
| 36 | +import java.nio.file.attribute.FileTime; |
32 | 37 | import java.text.DateFormat;
|
33 | 38 | import java.text.SimpleDateFormat;
|
| 39 | +import java.time.Instant; |
| 40 | +import java.time.temporal.ChronoUnit; |
34 | 41 | import java.util.Date;
|
35 | 42 | import java.util.Properties;
|
36 | 43 | import java.util.zip.Deflater;
|
|
40 | 47 | import org.jpos.core.ConfigurationException;
|
41 | 48 | import org.jpos.core.SimpleConfiguration;
|
42 | 49 | import org.jpos.core.SubConfiguration;
|
| 50 | +import org.jpos.q2.QFactory; |
| 51 | +import org.junit.jupiter.api.AfterEach; |
43 | 52 | import org.junit.jupiter.api.BeforeEach;
|
44 | 53 | import org.junit.jupiter.api.Disabled;
|
45 | 54 | import org.junit.jupiter.api.Test;
|
@@ -427,6 +436,65 @@ public void testMaxAgeFeatureWhenThereIsNonLogFiles() throws Exception {
|
427 | 436 | listener.destroy();
|
428 | 437 | }
|
429 | 438 |
|
| 439 | + @Test |
| 440 | + public void testDeleteOldLogsWithCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException { |
| 441 | + Path parent = logRotationTestDirectory.getDirectory().resolve("parent"); |
| 442 | + //create a file at level 0 in condition to be deleted. |
| 443 | + Path level0file = Files.createTempFile(logRotationTestDirectory.getDirectory(), "child", ".log"); |
| 444 | + assertThat("level 0 file should have been created", level0file.toFile(), is(anExistingFile())); |
| 445 | + Files.setLastModifiedTime(level0file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough |
| 446 | + //create a file at level 1 in condition to be deleted. |
| 447 | + Files.createDirectory(parent); |
| 448 | + Path level1file = Files.createTempFile(parent, "child", ".log"); |
| 449 | + assertThat("level 1 file should have been created", level1file.toFile(), is(anExistingFile())); |
| 450 | + Files.setLastModifiedTime(level1file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough |
| 451 | + |
| 452 | + try (DailyLogListener listener = new DailyLogListener()) { |
| 453 | + SimpleConfiguration cfg = new SimpleConfiguration(); |
| 454 | + cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString()); |
| 455 | + cfg.put("max-depth-deletion", "2"); |
| 456 | + cfg.put("delete-regex", "^child.*\\.log"); |
| 457 | + cfg.put("maxage", "1"); //created files are much older than 1s |
| 458 | + QFactory.autoconfigure(listener, cfg); |
| 459 | + listener.setConfiguration(cfg); |
| 460 | + |
| 461 | + listener.deleteOldLogs(); |
| 462 | + } |
| 463 | + assertThat("level 1 file should have been deleted", level1file.toFile(), is(not(anExistingFile()))); |
| 464 | + assertThat("level 0 file should have been deleted", level0file.toFile(), is(not(anExistingFile()))); |
| 465 | + Files.delete(parent); |
| 466 | + |
| 467 | + } |
| 468 | + @Test |
| 469 | + public void testDeleteOldLogsWithoutCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException { |
| 470 | + Path parent = logRotationTestDirectory.getDirectory().resolve("parent"); |
| 471 | + //create a file at level 0 in condition to be deleted. |
| 472 | + Path level0file = Files.createTempFile(logRotationTestDirectory.getDirectory(), "child", ".log"); |
| 473 | + assertThat("level 0 file should have been created", level0file.toFile(), is(anExistingFile())); |
| 474 | + Files.setLastModifiedTime(level0file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough |
| 475 | + //create a file at level 1 in condition to be deleted. |
| 476 | + Files.createDirectory(parent); |
| 477 | + Path level1file = Files.createTempFile(parent, "child", ".log"); |
| 478 | + assertThat("level 1 file should have been created", level1file.toFile(), is(anExistingFile())); |
| 479 | + Files.setLastModifiedTime(level1file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough |
| 480 | + |
| 481 | + try (DailyLogListener listener = new DailyLogListener()) { |
| 482 | + SimpleConfiguration cfg = new SimpleConfiguration(); |
| 483 | + cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString()); |
| 484 | + cfg.put("delete-regex", "^child.*\\.log"); |
| 485 | + cfg.put("maxage", "1000"); |
| 486 | + QFactory.autoconfigure(listener, cfg); |
| 487 | + listener.setConfiguration(cfg); |
| 488 | + |
| 489 | + listener.deleteOldLogs(); |
| 490 | + } |
| 491 | + assertThat("level 1 file should have not been deleted", level1file.toFile(), is(anExistingFile())); |
| 492 | + assertThat("level 0 file should have been deleted", level0file.toFile(), is(not(anExistingFile()))); |
| 493 | + |
| 494 | + Files.delete(level1file); //so it doesn't give problems in windows |
| 495 | + Files.delete(parent); |
| 496 | + |
| 497 | + } |
430 | 498 | @Test
|
431 | 499 | @Disabled("This feature doesn't work in Windows so we reverted the patch c94ff02f2")
|
432 | 500 | public void testLogRotateAbortsWhenCreatingNewFileFails() throws Exception {
|
|
0 commit comments