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 pattern"
70      // http://jira.qos.ch/browse/LBCORE-242
71  
72      @Test
73      public void multiDate() {
74          // Tuesday December 20th 17:59:01 CET 2011
75          long startTime = 1324400341553L;
76          tbrp.setFileNamePattern("foo-%d{yyyy-MM, AUX}/%d{mm}.log");
77          tbrp.start();
78  
79          timeBasedFNATP.setCurrentTime(startTime);
80          timeBasedFNATP.start();
81  
82          timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE);
83          boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
84          assertTrue(triggerred);
85          String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
86          assertEquals("foo-2011-12/59.log", elapsedPeriodsFileName);
87      }
88  
89      @Test
90      public void withTimeZone() {
91          // Tuesday December 20th 17:59:01 CET 2011
92          long startTime = 1324400341553L;
93          tbrp.setFileNamePattern("foo-%d{yyyy-MM-dd, GMT+5}.log");
94          tbrp.start();
95  
96          timeBasedFNATP.setCurrentTime(startTime);
97          timeBasedFNATP.start();
98  
99          timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE + 2 * MILLIS_IN_HOUR);
100         boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
101         assertTrue(triggerred);
102         String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
103         assertEquals("foo-2011-12-20.log", elapsedPeriodsFileName);
104     }
105 
106     @Test
107     public void extraIntegerTokenInFileNamePatternShouldBeDetected() {
108         String pattern = "test-%d{yyyy-MM-dd'T'HH}-%i.log.zip";
109         tbrp.setFileNamePattern(pattern);
110         tbrp.start();
111         
112         assertFalse(tbrp.isStarted());
113         StatusChecker statusChecker = new StatusChecker(context);
114         statusChecker.assertContainsMatch(Status.ERROR, "Filename pattern .{37} contains an integer token converter");
115     }
116 }