View Javadoc

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;
19  
20  import junit.framework.TestCase;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.Calendar;
25  import java.util.TimeZone;
26  import java.util.Date;
27  import java.text.SimpleDateFormat;
28  
29  /***
30     Exhaustive test of the DailyRollingFileAppender compute algorithm.
31  
32     @author Ceki Gülcü
33     @author Curt Arnold
34   */
35  public class DRFATestCase extends TestCase {
36  
37      /***
38       * Create new test.
39       * @param name test name.
40       */
41    public DRFATestCase(final String name) {
42      super(name);
43    }
44  
45      /***
46       * Reset configuration after every test.
47       */
48    public void tearDown() {
49        LogManager.resetConfiguration();
50    }
51  
52      /***
53       * Test prediction of check period.
54       */
55    public
56    void testComputeCheckPeriod() {
57      DailyRollingFileAppender drfa = new DailyRollingFileAppender();
58      drfa.setName("testComputeCheckPeriod");
59      drfa.setDatePattern("yyyy-MM-dd.'log'");
60      drfa.activateOptions();
61  
62      int x = drfa.computeCheckPeriod();
63      int y = DailyRollingFileAppender.TOP_OF_DAY;
64      assertEquals(drfa.computeCheckPeriod(),
65           DailyRollingFileAppender.TOP_OF_DAY);
66  
67      drfa.setDatePattern("yyyy-MM-dd mm.'log'");
68      assertEquals(drfa.computeCheckPeriod(),
69           DailyRollingFileAppender.TOP_OF_MINUTE);
70  
71      drfa.setDatePattern("yyyy-MM-dd a.'log'");
72      assertEquals(drfa.computeCheckPeriod(),
73           DailyRollingFileAppender.HALF_DAY);
74  
75      drfa.setDatePattern("yyyy-MM-dd HH.'log'");
76      assertEquals(drfa.computeCheckPeriod(),
77           DailyRollingFileAppender.TOP_OF_HOUR);
78  
79      drfa.setDatePattern("yyyy-MM.'log'");
80      assertEquals(drfa.computeCheckPeriod(),
81           DailyRollingFileAppender.TOP_OF_MONTH);
82  
83      drfa.setDatePattern("'log'HH'log'");
84      assertEquals(drfa.computeCheckPeriod(),
85           DailyRollingFileAppender.TOP_OF_HOUR);
86    }
87  
88  
89      /***
90       *   Test of RollingCalendar.
91       */
92    public
93    void testRC1() {
94      RollingCalendar rc = new RollingCalendar();
95      rc.setType(DailyRollingFileAppender.TOP_OF_DAY);
96  
97      Calendar c = Calendar.getInstance();
98  
99      // jan, mar, may, july, aug, oct, dec have 31 days
100     int [] M31 = {0,2,4,6,7,9,11};
101 
102     for(int i = 0; i < M31.length; i ++) {
103       for(int d = 1; d <=31; d++) {
104     for(int h = 0; h < 23; h++) {
105       c.clear();
106       c.set(Calendar.YEAR, 20);
107       c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
108       c.set(Calendar.DAY_OF_MONTH, d);
109       c.set(Calendar.HOUR_OF_DAY, h);
110       c.set(Calendar.MINUTE, 10);
111       c.set(Calendar.SECOND, 10);
112       c.set(Calendar.MILLISECOND, 88);
113 
114       c.setTime(rc.getNextCheckDate(c.getTime()));
115       if(d == 31) {
116         assertEquals(c.get(Calendar.MONTH),(Calendar.JANUARY+M31[i]+1)%12);
117         assertEquals(c.get(Calendar.DAY_OF_MONTH), 1);
118       } else {
119         assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
120         assertEquals(c.get(Calendar.DAY_OF_MONTH), d+1);
121       }
122       assertEquals(c.get(Calendar.HOUR_OF_DAY), 0);
123       assertEquals(c.get(Calendar.MINUTE), 0);
124       assertEquals(c.get(Calendar.SECOND), 0);
125       assertEquals(c.get(Calendar.MILLISECOND), 0);
126     }
127       }
128     }
129   }
130 
131     /***
132      * RollingCalendar test.
133      */
134   public
135   void testRC2() {
136     RollingCalendar rc = new RollingCalendar();
137 
138     rc.setType(DailyRollingFileAppender.TOP_OF_HOUR);
139 
140     Calendar c = Calendar.getInstance();
141     TimeZone tz = c.getTimeZone();
142 
143     // jan, mar, may, july, aug, oct, dec have 31 days
144     int [] M31 = {0,2,4,6,7,9,11};
145 
146     for(int i = 0; i < M31.length; i ++) {
147       System.out.println("Month = "+(M31[i]+1));
148       for(int d = 1; d <= 31; d++) {
149     for(int h = 0; h < 23; h++) {
150       for(int m = 0; m <= 59; m++) {
151         c.clear();
152         c.set(Calendar.YEAR, 20);
153         c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
154         c.set(Calendar.DAY_OF_MONTH, d);
155         c.set(Calendar.HOUR_OF_DAY, h);
156         c.set(Calendar.MINUTE, m);
157         c.set(Calendar.SECOND, 12);
158         c.set(Calendar.MILLISECOND, 88);
159 
160         boolean dltState0 = c.getTimeZone().inDaylightTime(c.getTime());
161         c.setTime(rc.getNextCheckDate(c.getTime()));
162         boolean dltState1 = c.getTimeZone().inDaylightTime(c.getTime());
163 
164         assertEquals(c.get(Calendar.MILLISECOND), 0);
165         assertEquals(c.get(Calendar.SECOND), 0);
166         assertEquals(c.get(Calendar.MINUTE), 0);
167 
168         if(dltState0 == dltState1) {
169           assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+1)%24);
170         } else {
171           // returning to standard time
172           if(dltState0) {
173         assertEquals(c.get(Calendar.HOUR_OF_DAY), h);
174           } else { // switching to day light saving time
175         //System.err.println("m="+m+", h="+h+", d="+d+", i="+i);
176         //if(h==2) {
177         // System.err.println(c);
178         //}
179         //assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+2)%24);
180           }
181         }
182 
183         if(h == 23) {
184           assertEquals(c.get(Calendar.DAY_OF_MONTH), (d+1)%32);
185           if(d == 31) {
186         assertEquals(c.get(Calendar.MONTH),
187                  (Calendar.JANUARY+M31[i]+1)%12);
188           } else {
189         assertEquals(c.get(Calendar.MONTH),
190                  Calendar.JANUARY+M31[i]);
191           }
192         } else {
193           assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
194           assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
195         }
196       }
197     }
198       }
199     }
200   }
201 
202     /***
203      * RollingCalendar test.
204      */
205   public
206   void testRC3() {
207     RollingCalendar rc = new RollingCalendar();
208 
209     rc.setType(DailyRollingFileAppender.TOP_OF_MINUTE);
210 
211     int[] S = {0, 1, 5, 10, 21, 30, 59};
212     int[] M = {0, 1, 5, 10, 21, 30, 59};
213     Calendar c = Calendar.getInstance();
214 
215     // jan, mar, may, july, aug, oct, dec have 31 days
216     int [] M31 = {2,9,0,4,6,7,11};
217 
218     for(int i = 0; i < M31.length; i ++) {
219       System.out.println("Month = "+(M31[i]+1));
220       for(int d = 1; d <= 31; d++) {
221     for(int h = 0; h < 23; h++) {
222       for(int m = 0; m < M.length; m++) {
223         for(int s = 0; s < S.length; s++) {
224           c.clear();
225           c.set(Calendar.YEAR, 20);
226           c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
227           c.set(Calendar.DAY_OF_MONTH, d);
228           c.set(Calendar.HOUR_OF_DAY, h);
229           c.set(Calendar.MINUTE, M[m]);
230           c.set(Calendar.SECOND, S[s]);
231           c.set(Calendar.MILLISECOND, 88);
232           c.add(Calendar.MILLISECOND, 1);
233 
234           boolean dltState0 = c.getTimeZone().inDaylightTime(c.getTime());
235 
236           c.setTime(rc.getNextCheckDate(c.getTime()));
237           c.add(Calendar.MILLISECOND, 0);
238           boolean dltState1 = c.getTimeZone().inDaylightTime(c.getTime());
239 
240           assertEquals(c.get(Calendar.MILLISECOND), 0);
241           assertEquals(c.get(Calendar.SECOND), 0);
242           assertEquals(c.get(Calendar.MINUTE), (M[m]+1)%60);
243 
244           if(M[m] == 59) {
245         if(dltState0 == dltState1) {
246           assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+1)%24);
247         }
248         if(h == 23) {
249           assertEquals(c.get(Calendar.DAY_OF_MONTH), (d+1)%32);
250           if(d == 31) {
251               assertEquals(c.get(Calendar.MONTH),
252                  (Calendar.JANUARY+M31[i]+1)%12);
253           } else {
254             assertEquals(c.get(Calendar.MONTH),
255                  Calendar.JANUARY+M31[i]);
256           }
257         } else {
258           assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
259         }
260           } else {
261         // allow discrepancies only if we are switching from std to dls time
262         if(c.get(Calendar.HOUR_OF_DAY) != h) {
263           c.add(Calendar.HOUR_OF_DAY, +1);
264           boolean dltState2 = c.getTimeZone().inDaylightTime(c.getTime());
265           if(dltState1 == dltState2) {
266             fail("No switch");
267           }
268         }
269         assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
270         assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
271           }
272         }
273       }
274     }
275       }
276     }
277   }
278 
279 
280     /***
281      * Common test code for 3 parameter constructor.
282      *
283      * @throws IOException if IOException during test.
284      */
285    public void test3Param(final String datePattern,
286                           final String filename) throws IOException {
287        Layout layout = new SimpleLayout();
288        DailyRollingFileAppender appender =
289                new DailyRollingFileAppender(layout, filename, datePattern);
290        assertEquals(datePattern, appender.getDatePattern());
291        Logger root = Logger.getRootLogger();
292        root.addAppender(appender);
293        root.info("Hello, World");
294        assertTrue(new File(filename).exists());
295     }
296 
297     /***
298      * Creates an appender with an unrecognized top-of-year pattern.
299      *
300      * @throws IOException if IOException during test.
301      */
302     public void testTopOfYear() throws IOException {
303         try {
304             test3Param("'.'yyyy", "output/drfa_topOfYear.log");
305             fail("Expected illegal state exception.");
306         } catch(IllegalStateException ex) {
307             assertNotNull(ex);
308         }
309     }
310 
311     /***
312      * Creates an appender with a top-of-month pattern.
313      *
314      * @throws IOException if IOException during test.
315      */
316     public void testTopOfMonth() throws IOException {
317         test3Param("'.'yyyy-MM", "output/drfa_topOfMonth.log");
318     }
319 
320 
321     /***
322      * Creates an appender with a top-of-week pattern.
323      *
324      * @throws IOException if IOException during test.
325      */
326     public void testTopOfWeek() throws IOException {
327         test3Param("'.'yyyy-w", "output/drfa_topOfWeek.log");
328     }
329 
330     /***
331      * Creates an appender with a top-of-day pattern.
332      *
333      * @throws IOException if IOException during test.
334      */
335     public void testTopOfDay() throws IOException {
336         test3Param("'.'yyyy-MM-dd", "output/drfa_topOfDay.log");
337     }
338 
339 
340     /***
341      * Creates an appender with a half day pattern.
342      *
343      * @throws IOException if IOException during test.
344      */
345     public void testHalfDay() throws IOException {
346         test3Param("'.'yyyy-MM-dd-a", "output/drfa_halfDay.log");
347     }
348 
349     /***
350      * Creates an appender with a top-of-hour pattern.
351      *
352      * @throws IOException if IOException during test.
353      */
354     public void testTopOfHour() throws IOException {
355         test3Param("'.'yyyy-MM-dd-HH", "output/drfa_topOfHour.log");
356     }
357 
358     /***
359      * Creates an appender with a top-of-day pattern.
360      *
361      * @throws IOException if IOException during test.
362      */
363     public void testTopOfMinute() throws IOException {
364         test3Param("'.'yyyy-MM-dd-HH-mm", "output/drfa_topOfMinute.log");
365     }
366 
367     /***
368      * Attempts to rollOver with no date pattern set.
369      *
370      * @throws IOException if IOException during test.
371      */
372     public void testRolloverNoPattern() throws IOException {
373         Layout layout = new SimpleLayout();
374         DailyRollingFileAppender appender =
375                 new DailyRollingFileAppender(layout, "output/drfa_nopattern.log", null);
376 
377         VectorErrorHandler errorHandler = new VectorErrorHandler();
378         appender.setErrorHandler(errorHandler);
379         appender.rollOver();
380         assertEquals(1, errorHandler.size());
381         assertEquals("Missing DatePattern option in rollOver().",
382                 errorHandler.getMessage(0));
383     }
384 
385     /***
386      * Tests rollOver with a minute periodicity.
387      *
388      * @throws IOException
389      * @throws InterruptedException
390      */
391     public void testMinuteRollover() throws IOException, InterruptedException {
392         Layout layout = new SimpleLayout();
393         String filename = "output/drfa_minuteRollover.log";
394         String pattern = "'.'yyyy-MM-dd-HH-mm";
395 
396         DailyRollingFileAppender appender =
397                 new DailyRollingFileAppender(layout,
398                         filename,
399                         pattern);
400         Logger root = Logger.getRootLogger();
401         root.addAppender(appender);
402         File firstFile =
403                 new File(filename + new SimpleDateFormat(pattern).format(new Date()));
404         root.info("Hello, World");
405         //
406         //   create a file by that name so it has to be deleted
407         //       on rollover
408         firstFile.createNewFile();
409         assertTrue(firstFile.exists());
410         assertEquals(0, firstFile.length());
411 
412         Calendar cal = Calendar.getInstance();
413         long now = cal.getTime().getTime();
414         cal.set(Calendar.SECOND, 3);
415         cal.set(Calendar.MILLISECOND, 0);
416         cal.add(Calendar.MINUTE, 1);
417         long until = cal.getTime().getTime();
418         Thread.sleep(until - now);
419         root.info("Hello, World");
420         assertTrue(firstFile.exists());
421         assertTrue(firstFile.length() > 0);
422 
423     }
424 
425 }