Java Code Examples for org.apache.logging.log4j.core.config.Configuration#initialize()

The following examples show how to use org.apache.logging.log4j.core.config.Configuration#initialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptConditionTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectFilesToDelete2() {
    final Configuration config = new DefaultConfiguration();
    config.initialize(); // creates the ScriptManager

    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 Script script = new Script("test", "javascript", scriptText);
    final ScriptCondition condition = new ScriptCondition(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());
}
 
Example 2
Source File: ScriptConditionTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectFilesToDelete() {
    final Configuration config = new DefaultConfiguration();
    config.initialize(); // creates the ScriptManager

    final Script script = new Script("test", "javascript", "pathList;"); // script that returns pathList
    final ScriptCondition condition = new ScriptCondition(script, config);
    final List<PathWithAttributes> pathList = new ArrayList<>();
    final Path base = Paths.get("baseDirectory");
    final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
    assertSame(result, pathList);
}
 
Example 3
Source File: ScriptConditionTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
@Category(Scripts.Groovy.class)
public void testSelectFilesToDelete3() {
    final Configuration config = new DefaultConfiguration();
    config.initialize(); // creates the ScriptManager

    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 Script script = new Script("test", "groovy", scriptText);
    final ScriptCondition condition = new ScriptCondition(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());
}