Skip to content

Commit

Permalink
Also test level 0 files.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrés Alcarraz <[email protected]>
  • Loading branch information
alcarraz committed Feb 9, 2024
1 parent 063716b commit 0cea909
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,39 +438,58 @@ public void testMaxAgeFeatureWhenThereIsNonLogFiles() throws Exception {
@Test
public void testDeleteOldLogsWithCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException {
Path parent = logRotationTestDirectory.getDirectory().resolve("parent");
//create a file at level 0 in condition to be deleted.
Path level0file = Files.createTempFile(logRotationTestDirectory.getDirectory(), "child", ".log");
assertThat("level 0 file should have been created", level0file.toFile(), is(anExistingFile()));
Files.setLastModifiedTime(level0file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
//create a file at level 1 in condition to be deleted.
Files.createDirectory(parent);
Path child = Files.createTempFile(parent, "child", ".log");
assertThat("file should have been created", child.toFile(), is(anExistingFile()));
Files.setLastModifiedTime(child, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
Path level1file = Files.createTempFile(parent, "child", ".log");
assertThat("level 1 file should have been created", level1file.toFile(), is(anExistingFile()));
Files.setLastModifiedTime(level1file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough

DailyLogListener listener = new DailyLogListener();
SimpleConfiguration cfg = new SimpleConfiguration();
cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString());
cfg.put("max-depth-deletion", "2");
cfg.put("delete-regex", "^child.*\\.log");
cfg.put("maxage", "1000");
cfg.put("maxage", "1"); //created files are much older than 1s
QFactory.autoconfigure(listener, cfg);
listener.setConfiguration(cfg);

listener.deleteOldLogs();
assertThat("file should have been deleted", child.toFile(), is(not(anExistingFile())));
assertThat("level 1 file should have been deleted", level1file.toFile(), is(not(anExistingFile())));
assertThat("level 0 file should have been deleted", level0file.toFile(), is(not(anExistingFile())));
Files.delete(parent);

}
@Test
public void testDeleteOldLogsWithoutCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException {
Path parent = logRotationTestDirectory.getDirectory().resolve("parent");
//create a file at level 0 in condition to be deleted.
Path level0file = Files.createTempFile(logRotationTestDirectory.getDirectory(), "child", ".log");
assertThat("level 0 file should have been created", level0file.toFile(), is(anExistingFile()));
Files.setLastModifiedTime(level0file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
//create a file at level 1 in condition to be deleted.
Files.createDirectory(parent);
Path child = Files.createTempFile(parent, "child", ".log");
assertThat("file should have been created", child.toFile(), is(anExistingFile()));
Files.setLastModifiedTime(child, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
Path level1file = Files.createTempFile(parent, "child", ".log");
assertThat("level 1 file should have been created", level1file.toFile(), is(anExistingFile()));
Files.setLastModifiedTime(level1file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough

DailyLogListener listener = new DailyLogListener();
SimpleConfiguration cfg = new SimpleConfiguration();
cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString());
cfg.put("delete-regex", "^child.*\\.log");
cfg.put("maxage", "1000");
QFactory.autoconfigure(listener, cfg);
listener.setConfiguration(cfg);

listener.deleteOldLogs();
assertThat("file should have not been deleted", child.toFile(), is(anExistingFile()));
Files.delete(child); //so it doesn't give problems in windows
assertThat("level 1 file should have not been deleted", level1file.toFile(), is(anExistingFile()));
assertThat("level 0 file should have been deleted", level0file.toFile(), is(not(anExistingFile())));

Files.delete(level1file); //so it doesn't give problems in windows
Files.delete(parent);

}
@Test
Expand Down

0 comments on commit 0cea909

Please sign in to comment.