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 * Tests for Layout.
27 *
28 */
29 public class LayoutTest extends TestCase {
30 /***
31 * Expected content type.
32 */
33 private final String contentType;
34
35 /***
36 * Expected value for ignoresThrowable.
37 */
38 private final boolean ignoresThrowable;
39
40 /***
41 * Expected value for header.
42 */
43 private final String header;
44
45 /***
46 * Expected value for footer.
47 */
48 private final String footer;
49
50 /***
51 * Construct a new instance of LayoutTest.
52 * @param testName test name.
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 * Constructor for use by derived tests.
64 * @param testName name of test.
65 * @param expectedContentType expected value for getContentType().
66 * @param expectedIgnoresThrowable expected value for ignoresThrowable().
67 * @param expectedHeader expected value for getHeader().
68 * @param expectedFooter expected value for getFooter().
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 * Tests Layout.LINE_SEP.
83 */
84 public void testLineSep() {
85 assertEquals(System.getProperty("line.separator"), Layout.LINE_SEP);
86 }
87
88 /***
89 * Tests Layout.LINE_SEP.
90 */
91 public void testLineSepLen() {
92 assertEquals(Layout.LINE_SEP.length(), Layout.LINE_SEP_LEN);
93 }
94
95 /***
96 * Creates layout for test.
97 * @return new instance of Layout.
98 */
99 protected Layout createLayout() {
100 return new MockLayout();
101 }
102
103 /***
104 * Tests getContentType.
105 */
106 public void testGetContentType() {
107 assertEquals(contentType, createLayout().getContentType());
108 }
109
110 /***
111 * Tests ignoresThrowable.
112 */
113 public void testIgnoresThrowable() {
114 assertEquals(ignoresThrowable, createLayout().ignoresThrowable());
115 }
116
117 /***
118 * Tests getHeader.
119 */
120 public void testGetHeader() {
121 assertEquals(header, createLayout().getHeader());
122 }
123
124 /***
125 * Tests getFooter.
126 */
127 public void testGetFooter() {
128 assertEquals(footer, createLayout().getFooter());
129 }
130
131 /***
132 * Tests format.
133 * @throws Exception derived tests, particular XMLLayoutTest, may throw exceptions.
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 * Concrete Layout class for tests.
146 */
147 private static final class MockLayout extends Layout {
148 /***
149 * @{inheritDoc}
150 */
151 public String format(final LoggingEvent event) {
152 return "Mock";
153 }
154
155 /***
156 * @{inheritDoc}
157 */
158 public void activateOptions() {
159 }
160
161 /***
162 * @{inheritDoc}
163 */
164 public boolean ignoresThrowable() {
165 return true;
166 }
167 }
168 }