1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.helpers;
19
20 import org.apache.log4j.Layout;
21 import org.apache.log4j.LayoutTest;
22 import org.apache.log4j.spi.LoggingEvent;
23
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26
27 import java.util.TimeZone;
28 import java.util.Calendar;
29
30
31
32
33
34
35 public class DateLayoutTest extends LayoutTest {
36
37
38
39
40 public DateLayoutTest(final String testName) {
41 super(testName);
42 }
43
44
45
46
47
48
49
50
51
52 protected DateLayoutTest(
53 final String testName, final String expectedContentType,
54 final boolean expectedIgnoresThrowable, final String expectedHeader,
55 final String expectedFooter) {
56 super(
57 testName, expectedContentType, expectedIgnoresThrowable, expectedHeader,
58 expectedFooter);
59 }
60
61
62
63
64 protected Layout createLayout() {
65 return new MockLayout();
66 }
67
68
69
70
71 public void testNullDateFormat() {
72 assertEquals("NULL", DateLayout.NULL_DATE_FORMAT);
73 }
74
75
76
77
78 public void testRelativeTimeDateFormat() {
79 assertEquals("RELATIVE", DateLayout.RELATIVE_TIME_DATE_FORMAT);
80 }
81
82
83
84
85
86 public void testDateFormatOption() {
87 assertEquals("DateFormat", DateLayout.DATE_FORMAT_OPTION);
88 }
89
90
91
92
93
94 public void testTimeZoneOption() {
95 assertEquals("TimeZone", DateLayout.TIMEZONE_OPTION);
96 }
97
98
99
100
101
102
103 public void testGetOptionStrings() {
104 String[] options = ((DateLayout) createLayout()).getOptionStrings();
105 assertEquals(2, options.length);
106 }
107
108
109
110
111
112 public void testSetOptionDateFormat() {
113 DateLayout layout = (DateLayout) createLayout();
114 layout.setOption("dAtefOrmat", "foobar");
115 assertEquals("FOOBAR", layout.getDateFormat());
116 }
117
118
119
120
121
122 public void testSetOptionTimeZone() {
123 DateLayout layout = (DateLayout) createLayout();
124 layout.setOption("tImezOne", "+05:00");
125 assertEquals("+05:00", layout.getTimeZone());
126 }
127
128
129
130
131 public void testSetDateFormat() {
132 DateLayout layout = (DateLayout) createLayout();
133 layout.setDateFormat("ABSOLUTE");
134 assertEquals("ABSOLUTE", layout.getDateFormat());
135 }
136
137
138
139
140 public void testSetTimeZone() {
141 DateLayout layout = (DateLayout) createLayout();
142 layout.setTimeZone("+05:00");
143 assertEquals("+05:00", layout.getTimeZone());
144 }
145
146
147
148
149 public void testSetDateFormatNull() {
150 DateLayout layout = (DateLayout) createLayout();
151 layout.setDateFormat((String) null, null);
152 }
153
154
155
156
157 public void testSetDateFormatNullString() {
158 DateLayout layout = (DateLayout) createLayout();
159 layout.setDateFormat("NuLL", null);
160 }
161
162
163
164
165 public void testSetDateFormatRelative() {
166 DateLayout layout = (DateLayout) createLayout();
167 layout.setDateFormat("rElatIve", TimeZone.getDefault());
168 }
169
170
171
172
173 public void testSetDateFormatAbsolute() {
174 DateLayout layout = (DateLayout) createLayout();
175 layout.setDateFormat("aBsolUte", TimeZone.getDefault());
176 }
177
178
179
180
181 public void testSetDateFormatDateTime() {
182 DateLayout layout = (DateLayout) createLayout();
183 layout.setDateFormat("dAte", TimeZone.getDefault());
184 }
185
186
187
188
189 public void testSetDateFormatISO8601() {
190 DateLayout layout = (DateLayout) createLayout();
191 layout.setDateFormat("iSo8601", TimeZone.getDefault());
192 }
193
194
195
196
197 public void testSetDateFormatSimple() {
198 DateLayout layout = (DateLayout) createLayout();
199 layout.setDateFormat("HH:mm:ss", TimeZone.getDefault());
200 }
201
202
203
204
205 public void testActivateOptions() {
206 DateLayout layout = (DateLayout) createLayout();
207 layout.setDateFormat("HH:mm:ss");
208 layout.setTimeZone("+05:00");
209 layout.activateOptions();
210 }
211
212
213
214
215 public void testSetDateFormatWithFormat() {
216 DateFormat format = new SimpleDateFormat("HH:mm");
217 DateLayout layout = (DateLayout) createLayout();
218 layout.setDateFormat(format, TimeZone.getDefault());
219 }
220
221
222
223
224
225
226 public void testISO8601Format() {
227 DateFormat format = new ISO8601DateFormat();
228 Calendar calendar = Calendar.getInstance();
229 calendar.clear();
230 calendar.set(1970, 0, 1, 0, 0, 0);
231 String actual = format.format(calendar.getTime());
232 assertEquals("1970-01-01 00:00:00,000", actual);
233 }
234
235
236
237
238
239 public void testDateTimeFormat() {
240 DateFormat format = new DateTimeDateFormat();
241 Calendar calendar = Calendar.getInstance();
242 calendar.clear();
243 calendar.set(1970, 0, 1, 0, 0, 0);
244 String actual = format.format(calendar.getTime());
245 SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss,SSS");
246 String expected = df.format(calendar.getTime());
247 assertEquals(expected, actual);
248 }
249
250
251
252
253 private static final class MockLayout extends DateLayout {
254
255
256
257 public MockLayout() {
258
259
260 assertNotNull(pos);
261 assertNotNull(date);
262 assertNull(dateFormat);
263 }
264
265
266
267
268 public String format(final LoggingEvent event) {
269 return "Mock";
270 }
271
272
273
274
275 public void activateOptions() {
276 }
277
278
279
280
281 public boolean ignoresThrowable() {
282 return true;
283 }
284 }
285 }