Search in sources :

Example 6 with DummyFileAttributes

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

the class IfAccumulatedFileSizeTest method testNotAcceptIfBelowThreshold.

@Test
public void testNotAcceptIfBelowThreshold() {
    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;
        assertFalse(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 7 with DummyFileAttributes

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

the class IfAccumulatedFileSizeTest method testNotAcceptOnExactMatch.

@Test
public void testNotAcceptOnExactMatch() {
    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();
        assertFalse(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 8 with DummyFileAttributes

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

the class ScriptConditionTest method testSelectFilesToDelete3.

@Test
@Tag("groovy")
public void testSelectFilesToDelete3() {
    final Configuration config = new DefaultConfiguration();
    // creates the ScriptManager
    config.initialize();
    final List<PathWithAttributes> pathList = new ArrayList<>();
    pathList.add(new PathWithAttributes(Paths.get("/path/1/abc/a.txt"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/2/abc/bbb.txt"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/3/abc/c.txt"), new DummyFileAttributes()));
    final String scriptText = // 
    "" + // 
    "import java.nio.file.*;" + // 
    "def pattern = ~/(\\d*)[\\/\\\\]abc[\\/\\\\].*\\.txt/;" + // 
    "assert pattern.getClass() == java.util.regex.Pattern;" + "def copy = pathList.collect{it};" + // 
    "pathList.each { pathWithAttribs -> \n" + // 
    "  def relative = basePath.relativize pathWithAttribs.path;" + // 
    "  println 'relative path: ' + relative;" + "  def str = relative.toString();" + // 
    "  def m = pattern.matcher(str);" + // 
    "  if (m.find()) {" + // 
    "    def index = m.group(1) as int;" + // 
    "    println 'extracted index: ' + index;" + "    def isOdd = (index % 2) == 1;" + // 
    "    println 'is odd: ' + isOdd;" + "    if (isOdd) { copy.remove pathWithAttribs}" + // 
    "  }" + // 
    "}" + "println copy;" + "copy;";
    final ScriptPlugin script = new ScriptPlugin("test", "groovy", scriptText);
    final ScriptCondition condition = ScriptCondition.createCondition(script, config);
    final Path base = Paths.get("/path");
    final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
    assertEquals(1, result.size());
    assertEquals(Paths.get("/path/2/abc/bbb.txt"), result.get(0).getPath());
}
Also used : Path(java.nio.file.Path) PathWithAttributes(org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) ScriptPlugin(org.apache.logging.log4j.script.ScriptPlugin) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 9 with DummyFileAttributes

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

the class ScriptConditionTest method testSelectFilesToDelete2.

@Test
public void testSelectFilesToDelete2() {
    final Configuration config = new DefaultConfiguration();
    // creates the ScriptManager
    config.initialize();
    final List<PathWithAttributes> pathList = new ArrayList<>();
    pathList.add(new PathWithAttributes(Paths.get("/path/1"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/2"), new DummyFileAttributes()));
    pathList.add(new PathWithAttributes(Paths.get("/path/3"), new DummyFileAttributes()));
    final String scriptText = // 
    "pathList.remove(1);" + "pathList;";
    final ScriptPlugin script = new ScriptPlugin("test", "javascript", scriptText);
    final ScriptCondition condition = ScriptCondition.createCondition(script, config);
    final Path base = Paths.get("baseDirectory");
    final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
    assertSame(result, pathList);
    assertEquals(2, result.size());
    assertEquals(Paths.get("/path/1"), result.get(0).getPath());
    assertEquals(Paths.get("/path/3"), result.get(1).getPath());
}
Also used : Path(java.nio.file.Path) PathWithAttributes(org.apache.logging.log4j.core.appender.rolling.action.PathWithAttributes) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) DummyFileAttributes(org.apache.logging.log4j.core.test.appender.rolling.action.DummyFileAttributes) ScriptPlugin(org.apache.logging.log4j.script.ScriptPlugin) Test(org.junit.jupiter.api.Test)

Example 10 with DummyFileAttributes

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

the class PathSortByModificationTimeTest method testCompareRecentLast.

@Test
public void testCompareRecentLast() {
    final PathSorter sorter = PathSortByModificationTime.createSorter(false);
    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)), "p1 < p2, same time");
    assertEquals(1, sorter.compare(path(p2, a1), path(p1, a1)), "p1 < p2, 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)

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