Search in sources :

Example 1 with TestAppender

use of nl.nn.adapterframework.testutil.TestAppender in project iaf by ibissource.

the class DatabaseClassLoaderTest method testExceptionHandlingWARN.

/**
 * This test makes sure that when the config can't be found, it only throws a WARN error in the log4j logger
 * @throws Exception
 */
@Test
public void testExceptionHandlingWARN() throws Exception {
    TestAppender appender = TestAppender.newBuilder().build();
    TestAppender.addToRootLogger(appender);
    boolean makeSureNoExceptionIsThrown = false;
    try {
        mockDatabase(true);
        appConstants.put("configurations." + getConfigurationName() + ".reportLevel", "WARN");
        ClassLoaderManager manager = new ClassLoaderManager(ibisContext);
        // Does not throw an exception
        ClassLoader config = manager.get(getConfigurationName());
        makeSureNoExceptionIsThrown = true;
        assertNull(config);
    } finally {
        TestAppender.removeAppender(appender);
        assertTrue(makeSureNoExceptionIsThrown);
    }
    List<LogEvent> log = appender.getLogEvents();
    LogEvent firstLogEntry = log.get(log.size() - 1);
    assertEquals(ClassLoaderManager.class.getCanonicalName(), firstLogEntry.getLoggerName());
    assertEquals(Level.WARN, firstLogEntry.getLevel());
    String msg = firstLogEntry.getMessage().getFormattedMessage();
    assertThat(msg, Matchers.startsWith(ERROR_PREFIX));
    assertThat(msg, Matchers.endsWith(ERROR_SUFFIX));
}
Also used : TestAppender(nl.nn.adapterframework.testutil.TestAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) ClassLoaderManager(nl.nn.adapterframework.configuration.ClassLoaderManager) Test(org.junit.Test)

Example 2 with TestAppender

use of nl.nn.adapterframework.testutil.TestAppender in project iaf by ibissource.

the class DatabaseClassLoaderTest method testExceptionHandlingDEBUG.

/**
 * This test makes sure that when the config can't be found, it only throws a DEBUG error in the log4j logger
 * @throws Exception
 */
@Test
public void testExceptionHandlingDEBUG() throws Exception {
    TestAppender appender = TestAppender.newBuilder().build();
    TestAppender.addToRootLogger(appender);
    boolean makeSureNoExceptionIsThrown = false;
    try {
        mockDatabase(true);
        appConstants.put("configurations." + getConfigurationName() + ".reportLevel", "DEBUG");
        ClassLoaderManager manager = new ClassLoaderManager(ibisContext);
        // Does not throw an exception
        ClassLoader config = manager.get(getConfigurationName());
        makeSureNoExceptionIsThrown = true;
        assertNull(config);
    } finally {
        TestAppender.removeAppender(appender);
    }
    assertTrue(makeSureNoExceptionIsThrown);
    List<LogEvent> log = appender.getLogEvents();
    LogEvent firstLogEntry = log.get(log.size() - 1);
    assertEquals(ClassLoaderManager.class.getCanonicalName(), firstLogEntry.getLoggerName());
    assertEquals(Level.DEBUG, firstLogEntry.getLevel());
    String msg = firstLogEntry.getMessage().getFormattedMessage();
    assertThat(msg, Matchers.startsWith(ERROR_PREFIX));
    assertThat(msg, Matchers.endsWith(ERROR_SUFFIX));
}
Also used : TestAppender(nl.nn.adapterframework.testutil.TestAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) ClassLoaderManager(nl.nn.adapterframework.configuration.ClassLoaderManager) Test(org.junit.Test)

Example 3 with TestAppender

use of nl.nn.adapterframework.testutil.TestAppender in project iaf by ibissource.

the class DatabaseClassLoaderTest method testExceptionHandlingINFO.

/**
 * This test makes sure that when the config can't be found, it only throws an INFO error in the log4j logger
 * @throws Exception
 */
@Test
public void testExceptionHandlingINFO() throws Exception {
    TestAppender appender = TestAppender.newBuilder().build();
    TestAppender.addToRootLogger(appender);
    boolean makeSureNoExceptionIsThrown = false;
    try {
        mockDatabase(true);
        appConstants.put("configurations." + getConfigurationName() + ".reportLevel", "INFO");
        ClassLoaderManager manager = new ClassLoaderManager(ibisContext);
        // Does not throw an exception
        ClassLoader config = manager.get(getConfigurationName());
        makeSureNoExceptionIsThrown = true;
        assertNull(config);
    } finally {
        TestAppender.removeAppender(appender);
    }
    assertTrue(makeSureNoExceptionIsThrown);
    List<LogEvent> log = appender.getLogEvents();
    LogEvent firstLogEntry = log.get(log.size() - 1);
    assertEquals(ApplicationMessageEvent.class.getCanonicalName(), firstLogEntry.getLoggerName());
    assertEquals(Level.INFO, firstLogEntry.getLevel());
    String msg = firstLogEntry.getMessage().getFormattedMessage();
    // Ignore the log4j prefix
    assertThat(msg, StringContains.containsString(ERROR_PREFIX));
    assertThat(msg, Matchers.endsWith(ERROR_SUFFIX));
}
Also used : TestAppender(nl.nn.adapterframework.testutil.TestAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) ClassLoaderManager(nl.nn.adapterframework.configuration.ClassLoaderManager) ApplicationMessageEvent(nl.nn.adapterframework.lifecycle.ApplicationMessageEvent) Test(org.junit.Test)

Example 4 with TestAppender

use of nl.nn.adapterframework.testutil.TestAppender in project iaf by ibissource.

the class TestLogMessages method testXmlLayoutWithUnicodeAndCdata.

@Test
public void testXmlLayoutWithUnicodeAndCdata() {
    TestAppender appender = TestAppender.newBuilder().useIbisXmlLayout().build();
    TestAppender.addToRootLogger(appender);
    try {
        log.debug("my beautiful  aâΔع你好ಡತ  debug <![CDATA[message]]> for me & you --> \"world\"");
        log.info("my beautiful  aâΔع你好ಡತ  info <![CDATA[message]]> for me & you --> \"world\"");
        List<String> logEvents = appender.getLogLines();
        assertEquals(2, logEvents.size());
        String expectedWarn = "<event logger=\"nl.nn.adapterframework.logging.TestLogMessages\" timestamp=\"xxx\" level=\"DEBUG\" thread=\"main\">\n" + "  <message>my beautiful \\u0010 a\\u00E2\\u0394\\u0639\\u4F60\\u597D\\u0CA1\\u0CA4  debug &lt;![CDATA[message]]&gt; for me &amp; you --&gt; \\\"world\\\"</message>\n" + "</event>";
        String expectedError = "<event logger=\"nl.nn.adapterframework.logging.TestLogMessages\" timestamp=\"xxx\" level=\"INFO\" thread=\"main\">\n" + "  <message>my beautiful \\u0010 a\\u00E2\\u0394\\u0639\\u4F60\\u597D\\u0CA1\\u0CA4  info &lt;![CDATA[message]]&gt; for me &amp; you --&gt; \\\"world\\\"</message>\n" + "</event>";
        // Remove the timestamp
        String actualWarn = logEvents.get(0).replaceAll("(?<=timestamp=\").+?(?=\")", "xxx");
        String actualError = logEvents.get(1).replaceAll("(?<=timestamp=\").+?(?=\")", "xxx");
        TestAssertions.assertEqualsIgnoreCRLF(expectedWarn, actualWarn);
        TestAssertions.assertEqualsIgnoreCRLF(expectedError, actualError);
    } finally {
        TestAppender.removeAppender(appender);
    }
}
Also used : TestAppender(nl.nn.adapterframework.testutil.TestAppender) Test(org.junit.Test)

Example 5 with TestAppender

use of nl.nn.adapterframework.testutil.TestAppender in project iaf by ibissource.

the class TestLogMessages method testChangeLogLevel.

@Test
public void testChangeLogLevel() {
    TestAppender appender = TestAppender.newBuilder().useIbisPatternLayout("%level - %m").build();
    TestAppender.addToRootLogger(appender);
    // For tests we use the `nl.nn` logger instead of the rootlogger
    String rootLoggerName = LogUtil.getLogger(this).getName();
    try {
        Configurator.setLevel(rootLoggerName, Level.DEBUG);
        log.debug("debug");
        Configurator.setLevel(rootLoggerName, Level.INFO);
        log.debug("debug");
        log.info("info");
        Configurator.setLevel(rootLoggerName, Level.WARN);
        log.debug("debug");
        log.info("info");
        log.warn("warn");
        Configurator.setLevel(rootLoggerName, Level.ERROR);
        log.debug("debug");
        log.info("info");
        log.warn("warn");
        log.error("error");
        assertEquals(4, appender.getNumberOfAlerts());
    } finally {
        TestAppender.removeAppender(appender);
        Configurator.setLevel(rootLoggerName, Level.DEBUG);
    }
}
Also used : TestAppender(nl.nn.adapterframework.testutil.TestAppender) Test(org.junit.Test)

Aggregations

TestAppender (nl.nn.adapterframework.testutil.TestAppender)14 Test (org.junit.Test)14 ClassLoaderManager (nl.nn.adapterframework.configuration.ClassLoaderManager)3 LogEvent (org.apache.logging.log4j.core.LogEvent)3 URL (java.net.URL)1 List (java.util.List)1 JarFile (java.util.jar.JarFile)1 ApplicationMessageEvent (nl.nn.adapterframework.lifecycle.ApplicationMessageEvent)1 ConfigurationMessageEventListener (nl.nn.adapterframework.testutil.ConfigurationMessageEventListener)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1