1 package com.atlassian.logging.log4j;
2
3 import org.apache.log4j.Level;
4 import org.apache.log4j.Logger;
5 import org.apache.log4j.spi.LoggingEvent;
6 import org.junit.Before;
7 import org.junit.Test;
8
9 import static org.hamcrest.CoreMatchers.equalTo;
10 import static org.junit.Assert.assertThat;
11
12
13
14
15 public class FilteredPatternLayoutTest
16 {
17 private static final Logger log = Logger.getLogger(FilteredPatternLayoutTest.class);
18 private FilteredPatternLayout filteredPatternLayout;
19
20 @Before
21 public void setUp() throws Exception
22 {
23 filteredPatternLayout = new FilteredPatternLayout();
24 filteredPatternLayout.activateOptions();
25 }
26
27
28 @Test
29 public void test_that_collapsing_occurs() throws Exception
30 {
31
32 LoggingEvent loggingEvent = new LoggingEvent("x", log, Level.INFO, "Some message to be logged", null);
33
34 filteredPatternLayout.setConversionPattern("%Q{3} %m%n");
35
36
37 String actual = filteredPatternLayout.format(loggingEvent);
38
39
40 assertThat(actual, equalTo("c.a.l.l.FilteredPatternLayoutTest Some message to be logged\n"));
41 }
42
43
44
45
46 @Test
47 public void test_that_collapsing_never_occurs_in_stacktraces() throws Exception
48 {
49 RuntimeException rte = new RuntimeException("RTE");
50
51 LoggingEvent loggingEvent = new LoggingEvent("x", log, Level.INFO, "Some message to be logged", rte);
52
53 filteredPatternLayout.setConversionPattern("%q{2} %m%n");
54
55 String expected = "c.a.logging.log4j.FilteredPatternLayoutTest Some message to be logged\n"
56 + "java.lang.RuntimeException: RTE\n"
57 + "\tat com.atlassian.logging.log4j.FilteredPatternLayoutTest.";
58
59
60
61 String actual = filteredPatternLayout.format(loggingEvent);
62
63
64 assertThat(actual.startsWith(expected), equalTo(true));
65 }
66 }