View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertTrue;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import ch.qos.logback.core.Context;
24  import ch.qos.logback.core.ContextBase;
25  import ch.qos.logback.core.status.Status;
26  import ch.qos.logback.core.testUtil.StatusChecker;
27  
28  /**
29   * @author Ceki Gülcü
30   */
31  public class TimeBasedFileNamingAndTriggeringPolicyBaseTest {
32  
33      static long MILLIS_IN_MINUTE = 60 * 1000;
34      static long MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE;
35  
36      Context context = new ContextBase();
37      RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
38      TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
39      DefaultTimeBasedFileNamingAndTriggeringPolicy<Object> timeBasedFNATP = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
40  
41      @Before
42      public void setUp() {
43          rfa.setContext(context);
44          tbrp.setContext(context);
45          timeBasedFNATP.setContext(context);
46  
47          rfa.setRollingPolicy(tbrp);
48          tbrp.setParent(rfa);
49          tbrp.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedFNATP);
50          timeBasedFNATP.setTimeBasedRollingPolicy(tbrp);
51      }
52  
53      @Test
54      public void singleDate() {
55          // Tuesday December 20th 17:59:01 CET 2011
56          long startTime = 1324400341553L;
57          tbrp.setFileNamePattern("foo-%d{yyyy-MM'T'mm}.log");
58          tbrp.start();
59  
60          timeBasedFNATP.setCurrentTime(startTime);
61          timeBasedFNATP.start();
62  
63          timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE);
64          timeBasedFNATP.isTriggeringEvent(null, null);
65          String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
66          assertEquals("foo-2011-12T59.log", elapsedPeriodsFileName);
67      }
68  
69      // see "log rollover should be configurable using %d multiple times in file name
70      // pattern"
71      // http://jira.qos.ch/browse/LBCORE-242
72  
73      @Test
74      public void multiDate() {
75          // Tuesday December 20th 17:59:01 CET 2011
76          long startTime = 1324400341553L;
77          tbrp.setFileNamePattern("foo-%d{yyyy-MM, AUX}/%d{mm}.log");
78          tbrp.start();
79  
80          timeBasedFNATP.setCurrentTime(startTime);
81          timeBasedFNATP.start();
82  
83          timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE);
84          boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
85          assertTrue(triggerred);
86          String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
87          assertEquals("foo-2011-12/59.log", elapsedPeriodsFileName);
88      }
89  
90      @Test
91      public void withTimeZone() {
92          // Tuesday December 20th 17:59:01 CET 2011
93          long startTime = 1324400341553L;
94          tbrp.setFileNamePattern("foo-%d{yyyy-MM-dd, GMT+5}.log");
95          tbrp.start();
96  
97          timeBasedFNATP.setCurrentTime(startTime);
98          timeBasedFNATP.start();
99  
100         timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE + 2 * MILLIS_IN_HOUR);
101         boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
102         assertTrue(triggerred);
103         String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
104         assertEquals("foo-2011-12-20.log", elapsedPeriodsFileName);
105     }
106 
107     @Test
108     public void extraIntegerTokenInFileNamePatternShouldBeDetected() {
109         String pattern = "test-%d{yyyy-MM-dd'T'HH}-%i.log.zip";
110         tbrp.setFileNamePattern(pattern);
111         tbrp.start();
112 
113         assertFalse(tbrp.isStarted());
114         StatusChecker statusChecker = new StatusChecker(context);
115         statusChecker.assertContainsMatch(Status.ERROR, "Filename pattern .{37} contains an integer token converter");
116     }
117 }