### Question:
PatternLayout extends PatternLayoutBase<ILoggingEvent> { public String doLayout(ILoggingEvent event) { if (!isStarted()) { return CoreConstants.EMPTY_STRING; } return writeLoopOnConverters(event); } PatternLayout(); Map<String, String> getDefaultConverterMap(); String doLayout(ILoggingEvent event); static final Map<String, String> defaultConverterMap; }### Answer:
@Test public void testOK() { pl.setPattern("%d %le [%t] %lo{30} - %m%n"); pl.start(); String val = pl.doLayout(getEventObject()); String regex = ISO_REGEX + " INFO " + MAIN_REGEX + " c.q.l.c.pattern.ConverterTest - Some message\\s*"; assertThat(val, matchesPattern(regex)); }
@Test public void testNoExeptionHandler() { pl.setPattern("%m%n"); pl.start(); String val = pl.doLayout(le); assertThat(val, containsString("java.lang.Exception: Bogus exception")); }
@Test public void testCompositePattern() { pl.setPattern("%-56(%d %lo{20}) - %m%n"); pl.start(); String val = pl.doLayout(getEventObject()); String regex = ISO_REGEX + " c.q.l.c.p.ConverterTest - Some message\\s*"; assertThat(val, matchesPattern(regex)); }
@Test public void contextProperty() { pl.setPattern("%property{a}"); pl.start(); lc.putProperty("a", "b"); String val = pl.doLayout(getEventObject()); assertEquals("b", val); }
@Test public void testNopExeptionHandler() { pl.setPattern("%nopex %m%n"); pl.start(); String val = pl.doLayout(le); assertThat(val, not(containsString("java.lang.Exception: Bogus exception"))); }
@Test public void testWithParenthesis() { pl.setPattern("\\(%msg:%msg\\) %msg"); pl.start(); le = makeLoggingEvent(aMessage, null); String val = pl.doLayout(le); assertEquals("(Some message:Some message) Some message", val); }
@Test public void testWithLettersComingFromLog4j() { pl.setPattern("%d %p [%t] %c{30} - %m%n"); pl.start(); String val = pl.doLayout(getEventObject()); String regex = ClassicTestConstants.ISO_REGEX + " INFO " + MAIN_REGEX + " c.q.l.c.pattern.ConverterTest - Some message\\s*"; assertThat(val, matchesPattern(regex)); }
@Test public void mdcWithDefaultValue() { String pattern = "%msg %mdc{foo} %mdc{bar:-[null]}"; pl.setPattern(OptionHelper.substVars(pattern, lc)); pl.start(); MDC.put("foo", "foo"); try { String val = pl.doLayout(getEventObject()); assertEquals("Some message foo [null]", val); } finally { MDC.remove("foo"); } }
@Test public void contextNameTest() { pl.setPattern("%contextName"); lc.setName("aValue"); pl.start(); String val = pl.doLayout(getEventObject()); assertEquals("aValue", val); }
@Test public void cnTest() { pl.setPattern("%cn"); lc.setName("aValue"); pl.start(); String val = pl.doLayout(getEventObject()); assertEquals("aValue", val); }
@Test public void smokeReplace() { pl.setPattern("%replace(a1234b){'\\d{4}', 'XXXX'}"); pl.start(); StatusPrinter.print(lc); String val = pl.doLayout(getEventObject()); assertEquals("aXXXXb", val); }
@Test public void replaceNewline() { String pattern = "%replace(A\nB){'\n', '\n\t'}"; String substPattern = OptionHelper.substVars(pattern, null, lc); assertEquals(pattern, substPattern); pl.setPattern(substPattern); pl.start(); StatusPrinter.print(lc); String val = pl.doLayout(makeLoggingEvent("", null)); assertEquals("A\n\tB", val); }