use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class PropertyTest method testEmptyAttribute.
@Test
public void testEmptyAttribute() throws Exception {
final org.apache.logging.log4j.Logger logger = LogManager.getLogger();
logger.info("msg");
final ListAppender app = (ListAppender) context.getRequiredAppender("List");
assertNotNull("No ListAppender", app);
final List<String> messages = app.getMessages();
assertNotNull("No Messages", messages);
assertEquals("message count" + messages, 1, messages.size());
//<Property name="emptyElementKey" />
//<Property name="emptyAttributeKey" value="" />
//<Property name="emptyAttributeKey2" value=""></Property>
//<Property name="elementKey">elementValue</Property>
//<Property name="attributeKey" value="attributeValue" />
//<Property name="attributeWithEmptyElementKey" value="attributeValue2"></Property>
//<Property name="bothElementAndAttributeKey" value="attributeValue3">elementValue3</Property>
final String expect = // ${sys:elementKey}
"1=elementValue" + // ${sys:emptyElementKey}
",2=" + // ${sys:emptyAttributeKey}
",a=" + // ${sys:emptyAttributeKey2}
",b=" + // ${sys:attributeKey}
",3=attributeValue" + // ${sys:attributeWithEmptyElementKey}
",4=attributeValue2" + // ${sys:bothElementAndAttributeKey}
",5=elementValue3,m=msg";
assertEquals(expect, messages.get(0));
app.clear();
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class IoBuilderCallerInfoTesting method assertMessages.
public void assertMessages(final String msg, final int size, final String methodName) {
final ListAppender appender = ctx.getListAppender("ClassAndMethod");
assertEquals(msg + ".size", size, appender.getMessages().size());
for (final String message : appender.getMessages()) {
assertEquals(msg + " has incorrect caller info", this.getClass().getName() + '.' + methodName, message);
}
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class IoBuilderTest method testNoArgBuilderCallerClassInfo.
@Test
public void testNoArgBuilderCallerClassInfo() throws Exception {
try (final PrintStream ps = IoBuilder.forLogger().buildPrintStream()) {
ps.println("discarded");
final ListAppender app = context.getListAppender("IoBuilderTest");
final List<String> messages = app.getMessages();
assertThat(messages, not(empty()));
assertThat(messages, hasSize(1));
final String message = messages.get(0);
assertThat(message, startsWith(getClass().getName() + ".testNoArgBuilderCallerClassInfo"));
app.clear();
}
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class CallerInformationTest method testClassLogger.
@Test
public void testClassLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Class").clear();
final Logger logger = Logger.getLogger("ClassLogger");
logger.info("Ignored message contents.");
logger.warn("Verifying the caller class is still correct.");
logger.error("Hopefully nobody breaks me!");
final List<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 3, messages.size());
for (final String message : messages) {
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
}
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class CategoryTest method testClassName.
@Test
public void testClassName() {
final Category category = Category.getInstance("TestCategory");
final Layout<String> layout = PatternLayout.newBuilder().withPattern("%d %p %C{1.} [%t] %m%n").build();
final ListAppender appender = new ListAppender("List2", null, layout, false, false);
appender.start();
category.setAdditivity(false);
category.getLogger().addAppender(appender);
category.error("Test Message");
final List<String> msgs = appender.getMessages();
assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1);
final String msg = msgs.get(0);
appender.clear();
final String threadName = Thread.currentThread().getName();
final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Strings.LINE_SEPARATOR;
assertTrue("Incorrect message " + Strings.dquote(msg) + " expected " + Strings.dquote(expected), msg.endsWith(expected));
}
Aggregations