Search in sources :

Example 1 with DummyFileAttributes

use of org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes in project logging-log4j2 by apache.

the class PathSortByModificationTimeTest method testCompareRecentFirst.

@Test
public void testCompareRecentFirst() {
    final PathSorter sorter = PathSortByModificationTime.createSorter(true);
    final Path p1 = Paths.get("aaa");
    final Path p2 = Paths.get("bbb");
    final DummyFileAttributes a1 = new DummyFileAttributes();
    final DummyFileAttributes a2 = new DummyFileAttributes();
    a1.lastModified = FileTime.fromMillis(100);
    a2.lastModified = FileTime.fromMillis(222);
    assertEquals(1, sorter.compare(path(p1, a1), path(p1, a2)), "same path, 2nd more recent");
    assertEquals(1, sorter.compare(path(p1, a1), path(p2, a2)), "path ignored, 2nd more recent");
    assertEquals(1, sorter.compare(path(p2, a1), path(p1, a2)), "path ignored, 2nd more recent");
    assertEquals(-1, sorter.compare(path(p1, a2), path(p1, a1)), "same path, 1st more recent");
    assertEquals(-1, sorter.compare(path(p1, a2), path(p2, a1)), "path ignored, 1st more recent");
    assertEquals(-1, sorter.compare(path(p2, a2), path(p1, a1)), "path ignored, 1st more recent");
    assertEquals(0, sorter.compare(path(p1, a1), path(p1, a1)), "same path, same time");
    assertEquals(1, sorter.compare(path(p1, a1), path(p2, a1)), "p2 < p1, same time");
    assertEquals(-1, sorter.compare(path(p2, a1), path(p1, a1)), "p2 < p1, same time");
}
Also used : Path(java.nio.file.Path) DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) Test(org.junit.jupiter.api.Test)

Example 2 with DummyFileAttributes

use of org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes in project logging-log4j2 by apache.

the class IfLastModifiedTest method testAcceptCallsNestedConditionsOnlyIfPathAccepted.

@Test
public void testAcceptCallsNestedConditionsOnlyIfPathAccepted() {
    final CountingCondition counter = new CountingCondition(true);
    final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"), counter);
    final DummyFileAttributes attrs = new DummyFileAttributes();
    final long oldEnough = 33 * 1000 + 5;
    attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - oldEnough);
    assertTrue(filter.accept(null, null, attrs));
    assertEquals(1, counter.getAcceptCount());
    assertTrue(filter.accept(null, null, attrs));
    assertEquals(2, counter.getAcceptCount());
    assertTrue(filter.accept(null, null, attrs));
    assertEquals(3, counter.getAcceptCount());
    final long tooYoung = 33 * 1000 - 5;
    attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - tooYoung);
    assertFalse(filter.accept(null, null, attrs));
    // no increase
    assertEquals(3, counter.getAcceptCount());
    assertFalse(filter.accept(null, null, attrs));
    assertEquals(3, counter.getAcceptCount());
    assertFalse(filter.accept(null, null, attrs));
    assertEquals(3, counter.getAcceptCount());
}
Also used : DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) Test(org.junit.jupiter.api.Test)

Example 3 with DummyFileAttributes

use of org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes in project logging-log4j2 by apache.

the class IfLastModifiedTest method testAcceptsIfFileAgeEqualToDuration.

@Test
public void testAcceptsIfFileAgeEqualToDuration() {
    final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"));
    final DummyFileAttributes attrs = new DummyFileAttributes();
    final long age = 33 * 1000;
    attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age);
    assertTrue(filter.accept(null, null, attrs));
}
Also used : DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) Test(org.junit.jupiter.api.Test)

Example 4 with DummyFileAttributes

use of org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes in project logging-log4j2 by apache.

the class IfAccumulatedFileSizeTest method testAcceptIfExceedThreshold.

@Test
public void testAcceptIfExceedThreshold() {
    final String[] sizes = { "2KB", "3MB", "4GB", "5TB" };
    for (final String size : sizes) {
        final IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition(size);
        final DummyFileAttributes attribs = new DummyFileAttributes();
        attribs.size = condition.getThresholdBytes() + 1;
        assertTrue(condition.accept(null, null, attribs));
    }
}
Also used : DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) Test(org.junit.jupiter.api.Test)

Example 5 with DummyFileAttributes

use of org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes in project logging-log4j2 by apache.

the class IfAccumulatedFileSizeTest method testAcceptCallsNestedConditionsOnlyIfPathAccepted.

@Test
public void testAcceptCallsNestedConditionsOnlyIfPathAccepted() {
    final CountingCondition counter = new CountingCondition(true);
    final IfAccumulatedFileSize condition = IfAccumulatedFileSize.createFileSizeCondition("2KB", counter);
    final DummyFileAttributes attribs = new DummyFileAttributes();
    final long quarter = condition.getThresholdBytes() / 4;
    attribs.size = quarter;
    assertFalse(condition.accept(null, null, attribs));
    assertEquals(0, counter.getAcceptCount());
    assertFalse(condition.accept(null, null, attribs));
    assertEquals(0, counter.getAcceptCount());
    assertFalse(condition.accept(null, null, attribs));
    assertEquals(0, counter.getAcceptCount());
    assertFalse(condition.accept(null, null, attribs));
    assertEquals(0, counter.getAcceptCount());
    assertTrue(condition.accept(null, null, attribs));
    assertEquals(1, counter.getAcceptCount());
    assertTrue(condition.accept(null, null, attribs));
    assertEquals(2, counter.getAcceptCount());
}
Also used : DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) Test(org.junit.jupiter.api.Test)

Aggregations

DummyFileAttributes (org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes)13 Test (org.junit.jupiter.api.Test)13 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)2 PathWithAttributes (org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)2 ScriptPlugin (org.apache.logging.log4j.script.ScriptPlugin)2 Tag (org.junit.jupiter.api.Tag)1