1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j;
19
20 import junit.framework.TestCase;
21
22 import org.apache.log4j.spi.LoggingEvent;
23
24
25
26
27
28
29 public class LayoutTest extends TestCase {
30
31
32
33 private final String contentType;
34
35
36
37
38 private final boolean ignoresThrowable;
39
40
41
42
43 private final String header;
44
45
46
47
48 private final String footer;
49
50
51
52
53
54 public LayoutTest(final String testName) {
55 super(testName);
56 contentType = "text/plain";
57 ignoresThrowable = true;
58 header = null;
59 footer = null;
60 }
61
62
63
64
65
66
67
68
69
70 protected LayoutTest(
71 final String testName, final String expectedContentType,
72 final boolean expectedIgnoresThrowable, final String expectedHeader,
73 final String expectedFooter) {
74 super(testName);
75 contentType = expectedContentType;
76 ignoresThrowable = expectedIgnoresThrowable;
77 header = expectedHeader;
78 footer = expectedFooter;
79 }
80
81
82
83
84 public void testLineSep() {
85 assertEquals(System.getProperty("line.separator"), Layout.LINE_SEP);
86 }
87
88
89
90
91 public void testLineSepLen() {
92 assertEquals(Layout.LINE_SEP.length(), Layout.LINE_SEP_LEN);
93 }
94
95
96
97
98
99 protected Layout createLayout() {
100 return new MockLayout();
101 }
102
103
104
105
106 public void testGetContentType() {
107 assertEquals(contentType, createLayout().getContentType());
108 }
109
110
111
112
113 public void testIgnoresThrowable() {
114 assertEquals(ignoresThrowable, createLayout().ignoresThrowable());
115 }
116
117
118
119
120 public void testGetHeader() {
121 assertEquals(header, createLayout().getHeader());
122 }
123
124
125
126
127 public void testGetFooter() {
128 assertEquals(footer, createLayout().getFooter());
129 }
130
131
132
133
134
135 public void testFormat() throws Exception {
136 Logger logger = Logger.getLogger("org.apache.log4j.LayoutTest");
137 LoggingEvent event =
138 new LoggingEvent(
139 "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
140 String result = createLayout().format(event);
141 assertEquals("Mock", result);
142 }
143
144
145
146
147 private static final class MockLayout extends Layout {
148
149
150
151 public String format(final LoggingEvent event) {
152 return "Mock";
153 }
154
155
156
157
158 public void activateOptions() {
159 }
160
161
162
163
164 public boolean ignoresThrowable() {
165 return true;
166 }
167 }
168 }