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