1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests for DateLayout.
33   *
34   */
35  public class DateLayoutTest extends LayoutTest {
36    /**
37     * Construct a new instance of LayoutTest.
38     * @param testName test name.
39     */
40    public DateLayoutTest(final String testName) {
41      super(testName);
42    }
43  
44    /**
45     * Constructor for use by derived tests.
46     * @param testName name of test.
47     * @param expectedContentType expected value for getContentType().
48     * @param expectedIgnoresThrowable expected value for ignoresThrowable().
49     * @param expectedHeader expected value for getHeader().
50     * @param expectedFooter expected value for getFooter().
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     * @{inheritDoc}
63     */
64    protected Layout createLayout() {
65      return new MockLayout();
66    }
67  
68    /**
69     * Tests DateLayout.NULL_DATE_FORMAT constant.
70     */
71    public void testNullDateFormat() {
72      assertEquals("NULL", DateLayout.NULL_DATE_FORMAT);
73    }
74  
75    /**
76     * Tests DateLayout.RELATIVE constant.
77     */
78    public void testRelativeTimeDateFormat() {
79      assertEquals("RELATIVE", DateLayout.RELATIVE_TIME_DATE_FORMAT);
80    }
81  
82    /**
83     * Tests DateLayout.DATE_FORMAT_OPTION constant.
84     * @deprecated since constant is deprecated
85     */
86    public void testDateFormatOption() {
87      assertEquals("DateFormat", DateLayout.DATE_FORMAT_OPTION);
88    }
89  
90    /**
91     * Tests DateLayout.TIMEZONE_OPTION constant.
92     * @deprecated since constant is deprecated
93     */
94    public void testTimeZoneOption() {
95      assertEquals("TimeZone", DateLayout.TIMEZONE_OPTION);
96    }
97  
98    /**
99     * Tests getOptionStrings().
100    * @deprecated since getOptionStrings is deprecated.
101    *
102    */
103   public void testGetOptionStrings() {
104     String[] options = ((DateLayout) createLayout()).getOptionStrings();
105     assertEquals(2, options.length);
106   }
107 
108   /**
109    * Tests setting DateFormat through setOption method.
110    * @deprecated since setOption is deprecated.
111    */
112   public void testSetOptionDateFormat() {
113     DateLayout layout = (DateLayout) createLayout();
114     layout.setOption("dAtefOrmat", "foobar");
115     assertEquals("FOOBAR", layout.getDateFormat());
116   }
117 
118   /**
119    * Tests setting TimeZone through setOption method.
120    * @deprecated since setOption is deprecated.
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    * Tests setDateFormat.
130    */
131   public void testSetDateFormat() {
132     DateLayout layout = (DateLayout) createLayout();
133     layout.setDateFormat("ABSOLUTE");
134     assertEquals("ABSOLUTE", layout.getDateFormat());
135   }
136 
137   /**
138    * Tests setTimeZone.
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    * Tests 2 parameter setDateFormat with null.
148    */
149   public void testSetDateFormatNull() {
150     DateLayout layout = (DateLayout) createLayout();
151     layout.setDateFormat((String) null, null);
152   }
153 
154   /**
155    * Tests 2 parameter setDateFormat with "NULL".
156    */
157   public void testSetDateFormatNullString() {
158     DateLayout layout = (DateLayout) createLayout();
159     layout.setDateFormat("NuLL", null);
160   }
161 
162   /**
163    * Tests 2 parameter setDateFormat with "RELATIVE".
164    */
165   public void testSetDateFormatRelative() {
166     DateLayout layout = (DateLayout) createLayout();
167     layout.setDateFormat("rElatIve", TimeZone.getDefault());
168   }
169 
170   /**
171    * Tests 2 parameter setDateFormat with "ABSOLUTE".
172    */
173   public void testSetDateFormatAbsolute() {
174     DateLayout layout = (DateLayout) createLayout();
175     layout.setDateFormat("aBsolUte", TimeZone.getDefault());
176   }
177 
178   /**
179    * Tests 2 parameter setDateFormat with "DATETIME".
180    */
181   public void testSetDateFormatDateTime() {
182     DateLayout layout = (DateLayout) createLayout();
183     layout.setDateFormat("dAte", TimeZone.getDefault());
184   }
185 
186   /**
187    * Tests 2 parameter setDateFormat with "ISO8601".
188    */
189   public void testSetDateFormatISO8601() {
190     DateLayout layout = (DateLayout) createLayout();
191     layout.setDateFormat("iSo8601", TimeZone.getDefault());
192   }
193 
194   /**
195    * Tests 2 parameter setDateFormat with "HH:mm:ss".
196    */
197   public void testSetDateFormatSimple() {
198     DateLayout layout = (DateLayout) createLayout();
199     layout.setDateFormat("HH:mm:ss", TimeZone.getDefault());
200   }
201 
202   /**
203    * Tests activateOptions.
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    * Tests setDateFormat(DateFormat, TimeZone).
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      * Tests IS08601DateFormat class.
224      * @deprecated since ISO8601DateFormat is deprecated
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      * Tests DateTimeDateFormat class.
237      * @deprecated since DateTimeDateFormat is deprecated
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    * Concrete Layout class for tests.
252    */
253   private static final class MockLayout extends DateLayout {
254     /**
255      * Create new instance of MockLayout.
256      */
257     public MockLayout() {
258       //
259       //  checks that protected fields are properly initialized
260       assertNotNull(pos);
261       assertNotNull(date);
262       assertNull(dateFormat);
263     }
264 
265     /**
266      * @{inheritDoc}
267      */
268     public String format(final LoggingEvent event) {
269       return "Mock";
270     }
271 
272     /**
273      * @{inheritDoc}
274      */
275     public void activateOptions() {
276     }
277 
278     /**
279      * @{inheritDoc}
280      */
281     public boolean ignoresThrowable() {
282       return true;
283     }
284   }
285 }