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.rolling;
19  
20  import junit.framework.TestCase;
21  import org.apache.log4j.ConsoleAppender;
22  import org.apache.log4j.LogManager;
23  import org.apache.log4j.Logger;
24  import org.apache.log4j.PatternLayout;
25  import org.apache.log4j.util.Compare;
26  import org.apache.log4j.extras.DOMConfigurator;
27  
28  import java.io.FileNotFoundException;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.text.SimpleDateFormat;
32  import java.util.Calendar;
33  import java.util.Date;
34  
35  
36  /***
37   * A rather exhaustive set of tests. Tests include leaving the ActiveFileName
38   * argument blank, or setting it, with and without compression, and tests
39   * with or without stopping/restarting the RollingFileAppender.
40   * 
41   * The regression tests log a few times using a RollingFileAppender. Then, 
42   * they predict the names of the files which sould be generated and compare
43   * them with witness files.
44   * 
45   * <pre>
46           Compression    ActiveFileName  Stop/Restart 
47   Test1      NO              BLANK          NO
48   Test2      NO              BLANK          YES
49   Test3      YES             BLANK          NO
50   Test4      NO                SET          YES 
51   Test5      NO                SET          NO
52   Test6      YES               SET          NO
53   * </pre>
54   * @author Ceki G&uuml;lc&uuml;
55   */
56  public class TimeBasedRollingTest extends TestCase {
57    Logger logger = Logger.getLogger(TimeBasedRollingTest.class);
58  
59    public TimeBasedRollingTest(String name) {
60      super(name);
61    }
62  
63    public void setUp() {
64      Logger root = Logger.getRootLogger();
65      root.addAppender(
66        new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} [%t] %level %c{2}#%M:%L - %m%n")));
67    }
68  
69    public void tearDown() {
70      LogManager.shutdown();
71    }
72  
73    private static boolean compare(final String actual, final String expected)
74            throws IOException {
75        return Compare.compare(TimeBasedRollingTest.class, actual, expected);
76    }
77  
78    private static boolean gzCompare(final String actual, final String expected)
79              throws IOException {
80          return Compare.gzCompare(TimeBasedRollingTest.class, actual, expected);
81    }
82  
83    private static void configure(final String configName) throws IOException {
84        String resourceName = configName;
85        int lastSlash = resourceName.lastIndexOf("/");
86        if (lastSlash >= 0) {
87            resourceName = resourceName.substring(lastSlash + 1);
88        }
89        InputStream is = TimeBasedRollingTest.class.getResourceAsStream(resourceName);
90        if (is == null) {
91            throw new FileNotFoundException("Could not find resource " + resourceName);
92        }
93        DOMConfigurator configurator = new DOMConfigurator();
94        configurator.doConfigure(is, LogManager.getLoggerRepository());
95    }
96  
97    /***
98     * Test rolling without compression, activeFileName left blank, no stop/start
99     */
100   public void test1() throws Exception {
101     PatternLayout layout = new PatternLayout("%c{1} - %m%n");
102     RollingFileAppender rfa = new RollingFileAppender();
103     rfa.setLayout(layout);
104 
105     String datePattern = "yyyy-MM-dd_HH_mm_ss";
106 
107     TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
108     tbrp.setFileNamePattern("test1-%d{" + datePattern + "}");
109     tbrp.activateOptions();
110     rfa.setRollingPolicy(tbrp);
111     rfa.activateOptions();
112     logger.addAppender(rfa);
113 
114     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
115     String[] filenames = new String[4];
116 
117     Calendar cal = Calendar.getInstance();
118 
119     for (int i = 0; i < 4; i++) {
120       filenames[i] = "test1-" + sdf.format(cal.getTime());
121       cal.add(Calendar.SECOND, 1);
122     }
123 
124     System.out.println("Waiting until next second and 100 millis.");
125     delayUntilNextSecond(100);
126     System.out.println("Done waiting.");
127 
128     for (int i = 0; i < 5; i++) {
129       logger.debug("Hello---" + i);
130       Thread.sleep(500);
131     }
132 
133     for (int i = 0; i < 4; i++) {
134       //System.out.println(i + " expected filename [" + filenames[i] + "].");
135     }
136 
137     for (int i = 0; i < 4; i++) {
138       assertTrue(compare(filenames[i], "witness/rolling/tbr-test1." + i));
139     }
140   }
141 
142   /***
143    * No compression, with stop/restart, activeFileName left blank
144    */
145   public void test2() throws Exception {
146     String datePattern = "yyyy-MM-dd_HH_mm_ss";
147 
148     PatternLayout layout1 = new PatternLayout("%c{1} - %m%n");
149     RollingFileAppender rfa1 = new RollingFileAppender();
150     rfa1.setLayout(layout1);
151 
152     TimeBasedRollingPolicy tbrp1 = new TimeBasedRollingPolicy();
153     tbrp1.setFileNamePattern("test2-%d{" + datePattern + "}");
154     tbrp1.activateOptions();
155     rfa1.setRollingPolicy(tbrp1);
156     rfa1.activateOptions();
157     logger.addAppender(rfa1);
158 
159     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
160     String[] filenames = new String[4];
161 
162     Calendar cal = Calendar.getInstance();
163 
164     for (int i = 0; i < 4; i++) {
165       filenames[i] = "test2-" + sdf.format(cal.getTime());
166       cal.add(Calendar.SECOND, 1);
167     }
168 
169     System.out.println("Waiting until next second and 100 millis.");
170     delayUntilNextSecond(100);
171     System.out.println("Done waiting.");
172 
173     for (int i = 0; i <= 2; i++) {
174       logger.debug("Hello---" + i);
175       Thread.sleep(500);
176     }
177 
178     logger.removeAppender(rfa1);
179     rfa1.close();
180 
181     PatternLayout layout2 = new PatternLayout("%c{1} - %m%n");
182     RollingFileAppender rfa2 = new RollingFileAppender();
183     rfa2.setLayout(layout2);
184 
185     TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy();
186     tbrp2.setFileNamePattern("test2-%d{" + datePattern + "}");
187     tbrp2.activateOptions();
188     rfa2.setRollingPolicy(tbrp2);
189     rfa2.activateOptions();
190     logger.addAppender(rfa2);
191 
192     for (int i = 3; i <= 4; i++) {
193       logger.debug("Hello---" + i);
194       Thread.sleep(500);
195     }
196 
197     rfa2.close();
198 
199     for (int i = 0; i < 4; i++) {
200       assertTrue(compare(filenames[i], "witness/rolling/tbr-test2." + i));
201     }
202   }
203 
204   /***
205    * With compression, activeFileName left blank, no stop/restart
206    */
207   public void test3() throws Exception {
208     PatternLayout layout = new PatternLayout("%c{1} - %m%n");
209     RollingFileAppender rfa = new RollingFileAppender();
210     rfa.setLayout(layout);
211 
212     String datePattern = "yyyy-MM-dd_HH_mm_ss";
213 
214     TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
215     tbrp.setFileNamePattern("test3-%d{" + datePattern + "}.gz");
216     tbrp.activateOptions();
217     rfa.setRollingPolicy(tbrp);
218     rfa.activateOptions();
219     logger.addAppender(rfa);
220 
221     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
222     String[] filenames = new String[4];
223 
224     Calendar cal = Calendar.getInstance();
225 
226     for (int i = 0; i < 3; i++) {
227       filenames[i] = "test3-" + sdf.format(cal.getTime()) + ".gz";
228       cal.add(Calendar.SECOND, 1);
229     }
230 
231     filenames[3] = "test3-" + sdf.format(cal.getTime());
232 
233     System.out.println("Waiting until next second and 100 millis.");
234     delayUntilNextSecond(100);
235     System.out.println("Done waiting.");
236 
237     for (int i = 0; i < 5; i++) {
238       logger.debug("Hello---" + i);
239       Thread.sleep(500);
240     }
241 
242     for (int i = 0; i < 4; i++) {
243       //System.out.println(i + " expected filename [" + filenames[i] + "].");
244     }
245 
246     rfa.close();
247 
248     for (int i = 0; i < 3; i++) {
249       assertTrue(gzCompare(filenames[i], "witness/rolling/tbr-test3." + i + ".gz"));
250     }
251 
252     assertTrue(compare(filenames[3], "witness/rolling/tbr-test3.3"));
253   }
254 
255   /***
256    * Without compression, activeFileName set,  with stop/restart
257    */
258   public void test4() throws Exception {
259     String datePattern = "yyyy-MM-dd_HH_mm_ss";
260 
261     PatternLayout layout1 = new PatternLayout("%c{1} - %m%n");
262     RollingFileAppender rfa1 = new RollingFileAppender();
263     rfa1.setLayout(layout1);
264 
265     TimeBasedRollingPolicy tbrp1 = new TimeBasedRollingPolicy();
266     rfa1.setFile("test4.log");
267     tbrp1.setFileNamePattern("test4-%d{" + datePattern + "}");
268     tbrp1.activateOptions();
269     rfa1.setRollingPolicy(tbrp1);
270     rfa1.setAppend(false);
271     rfa1.activateOptions();
272     logger.addAppender(rfa1);
273 
274     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
275     String[] filenames = new String[4];
276 
277     Calendar cal = Calendar.getInstance();
278 
279     for (int i = 0; i < 3; i++) {
280       filenames[i] = "test4-" + sdf.format(cal.getTime());
281       cal.add(Calendar.SECOND, 1);
282     }
283     filenames[3] = "test4.log";
284     
285     System.out.println("Waiting until next second and 100 millis.");
286     delayUntilNextSecond(100);
287     System.out.println("Done waiting.");
288 
289     for (int i = 0; i <= 2; i++) {
290       logger.debug("Hello---" + i);
291       Thread.sleep(500);
292     }
293 
294     logger.removeAppender(rfa1);
295     rfa1.close();
296 
297     PatternLayout layout2 = new PatternLayout("%c{1} - %m%n");
298     RollingFileAppender rfa2 = new RollingFileAppender();
299     rfa2.setLayout(layout2);
300 
301     TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy();
302     tbrp2.setFileNamePattern("test4-%d{" + datePattern + "}");
303     rfa2.setFile("test4.log");
304     tbrp2.activateOptions();
305     rfa2.setRollingPolicy(tbrp2);
306     rfa2.activateOptions();
307     logger.addAppender(rfa2);
308 
309     for (int i = 3; i <= 4; i++) {
310       logger.debug("Hello---" + i);
311       Thread.sleep(500);
312     }
313 
314     rfa2.close();
315 
316     for (int i = 0; i < 4; i++) {
317       assertTrue(compare(filenames[i], "witness/rolling/tbr-test4." + i));
318     }
319   }
320 
321   /***
322    * No compression, activeFileName set,  without stop/restart
323    */
324   public void test5() throws Exception {
325     PatternLayout layout = new PatternLayout("%c{1} - %m%n");
326     RollingFileAppender rfa = new RollingFileAppender();
327     rfa.setLayout(layout);
328 
329     String datePattern = "yyyy-MM-dd_HH_mm_ss";
330 
331     TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
332     tbrp.setFileNamePattern("test5-%d{" + datePattern + "}");
333     rfa.setFile("test5.log");
334     tbrp.activateOptions();
335     rfa.setRollingPolicy(tbrp);
336     rfa.setAppend(false);
337     rfa.activateOptions();
338     logger.addAppender(rfa);
339 
340     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
341     String[] filenames = new String[4];
342 
343     Calendar cal = Calendar.getInstance();
344 
345     for (int i = 0; i < 3; i++) {
346       filenames[i] = "test5-" + sdf.format(cal.getTime());
347       cal.add(Calendar.SECOND, 1);
348     }
349 
350     filenames[3] = "test5.log";
351 
352     System.out.println("Waiting until next second and 100 millis.");
353     delayUntilNextSecond(100);
354     System.out.println("Done waiting.");
355 
356     for (int i = 0; i < 5; i++) {
357       logger.debug("Hello---" + i);
358       Thread.sleep(500);
359     }
360 
361     for (int i = 0; i < 4; i++) {
362       assertTrue(compare(filenames[i], "witness/rolling/tbr-test5." + i));
363     }
364   }
365 
366   /***
367    * With compression, activeFileName set, no stop/restart,
368    */
369   public void test6() throws Exception {
370     PatternLayout layout = new PatternLayout("%c{1} - %m%n");
371     RollingFileAppender rfa = new RollingFileAppender();
372     rfa.setLayout(layout);
373 
374     String datePattern = "yyyy-MM-dd_HH_mm_ss";
375 
376     TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
377     tbrp.setFileNamePattern("test6-%d{" + datePattern + "}.gz");
378     rfa.setFile("test6.log");
379     tbrp.activateOptions();
380     rfa.setRollingPolicy(tbrp);
381     rfa.setAppend(false);
382     rfa.activateOptions();
383     logger.addAppender(rfa);
384 
385     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
386     String[] filenames = new String[4];
387 
388     Calendar cal = Calendar.getInstance();
389 
390     for (int i = 0; i < 3; i++) {
391       filenames[i] = "test6-" + sdf.format(cal.getTime()) + ".gz";
392       cal.add(Calendar.SECOND, 1);
393     }
394 
395     filenames[3] = "test6.log";
396 
397     System.out.println("Waiting until next second and 100 millis.");
398     delayUntilNextSecond(100);
399     System.out.println("Done waiting.");
400 
401     for (int i = 0; i < 5; i++) {
402       logger.debug("Hello---" + i);
403       Thread.sleep(500);
404     }
405 
406     for (int i = 0; i < 4; i++) {
407       //System.out.println(i + " expected filename [" + filenames[i] + "].");
408     }
409 
410     rfa.close();
411 
412     for (int i = 0; i < 3; i++) {
413       assertTrue(gzCompare(filenames[i], "witness/rolling/tbr-test6." + i + ".gz"));
414     }
415 
416     assertTrue(compare(filenames[3], "witness/rolling/tbr-test6.3"));
417   }
418 
419   public void testWithJoran1() throws Exception {
420     configure("./input/rolling/time1.xml");
421 
422     String datePattern = "yyyy-MM-dd_HH_mm_ss";
423 
424     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
425     String[] filenames = new String[4];
426 
427     Calendar cal = Calendar.getInstance();
428 
429     for (int i = 0; i < 4; i++) {
430       filenames[i] = "test1-" + sdf.format(cal.getTime());
431       cal.add(Calendar.SECOND, 1);
432     }
433 
434     System.out.println("Waiting until next second and 100 millis.");
435     delayUntilNextSecond(100);
436     System.out.println("Done waiting.");
437 
438     for (int i = 0; i < 5; i++) {
439       logger.debug("Hello---" + i);
440       Thread.sleep(500);
441     }
442 
443     for (int i = 0; i < 4; i++) {
444       //System.out.println(i + " expected filename [" + filenames[i] + "].");
445     }
446 
447     for (int i = 0; i < 4; i++) {
448       assertTrue(compare(filenames[i], "witness/rolling/tbr-test1." + i));
449     }
450     
451   }
452   
453   public void XXXtestWithJoran10() throws Exception {
454     configure("./input/rolling/time2.xml");
455 
456     String datePattern = "yyyy-MM-dd";
457 
458     SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
459     String[] filenames = new String[0];
460 
461     Calendar cal = Calendar.getInstance();
462 
463     filenames[0] = "test1-" + sdf.format(cal.getTime());
464 
465     for (int i = 0; i < 5; i++) {
466       logger.debug("Hello---" + i);
467       Thread.sleep(500);
468     }
469 
470 
471     for (int i = 0; i < 1; i++) {
472       assertTrue(compare(filenames[i], "witness/rolling/tbr-test10." + i));
473     }
474     
475   }
476   
477   void delayUntilNextSecond(int millis) {
478     long now = System.currentTimeMillis();
479     Calendar cal = Calendar.getInstance();
480     cal.setTime(new Date(now));
481 
482     cal.set(Calendar.MILLISECOND, millis);
483     cal.add(Calendar.SECOND, 1);
484 
485     long next = cal.getTime().getTime();
486 
487     try {
488       Thread.sleep(next - now);
489     } catch (Exception e) {
490     }
491   }
492 
493   void delayUntilNextMinute(int seconds) {
494     long now = System.currentTimeMillis();
495     Calendar cal = Calendar.getInstance();
496     cal.setTime(new Date(now));
497 
498     cal.set(Calendar.SECOND, seconds);
499     cal.add(Calendar.MINUTE, 1);
500 
501     long next = cal.getTime().getTime();
502 
503     try {
504       Thread.sleep(next - now);
505     } catch (Exception e) {
506     }
507   }
508 
509 }