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 ch.qos.logback.core.util.StatusPrinter;
17  import org.junit.jupiter.api.Assertions;
18  import org.junit.jupiter.api.BeforeEach;
19  import org.junit.jupiter.api.Test;
20  
21  import ch.qos.logback.core.Context;
22  import ch.qos.logback.core.ContextBase;
23  import ch.qos.logback.core.status.Status;
24  import ch.qos.logback.core.status.testUtil.StatusChecker;
25  
26  import static org.junit.jupiter.api.Assertions.assertTrue;
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      StatusChecker statusChecker = new StatusChecker(context);
41      @BeforeEach
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 doublePolicySet() {
55          rfa.setTriggeringPolicy(new SizeBasedTriggeringPolicy<>());
56          statusChecker.assertContainsMatch(Status.WARN, "A triggering policy of type " );
57      }
58  
59      @Test
60      public void singleDate() {
61          // Tuesday December 20th 17:59:01 CET 2011
62          long startTime = 1324400341553L;
63          tbrp.setFileNamePattern("foo-%d{yyyy-MM'T'mm}.log");
64          timeBasedFNATP.setCurrentTime(startTime);
65          tbrp.start();
66  
67          timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE);
68          boolean result = timeBasedFNATP.isTriggeringEvent(null, null);
69          StatusPrinter.print(context);
70          assertTrue(result);
71          String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
72          Assertions.assertEquals("foo-2011-12T59.log", elapsedPeriodsFileName);
73      }
74  
75      // see "log rollover should be configurable using %d multiple times in file name
76      // pattern"
77      // http://jira.qos.ch/browse/LBCORE-242
78  
79      @Test
80      public void multiDate() {
81          // Tuesday December 20th 17:59:01 CET 2011
82          long startTime = 1324400341553L;
83          tbrp.setFileNamePattern("foo-%d{yyyy-MM, AUX}/%d{mm}.log");
84          tbrp.start();
85  
86          timeBasedFNATP.setCurrentTime(startTime);
87          timeBasedFNATP.start();
88  
89          timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE);
90          boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
91          assertTrue(triggerred);
92          String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
93          Assertions.assertEquals("foo-2011-12/59.log", elapsedPeriodsFileName);
94      }
95  
96      @Test
97      public void withTimeZone() {
98          // Tuesday December 20th 17:59:01 CET 2011
99          long startTime = 1324400341553L;
100         tbrp.setFileNamePattern("foo-%d{yyyy-MM-dd, GMT+5}.log");
101         tbrp.start();
102 
103         timeBasedFNATP.setCurrentTime(startTime);
104         timeBasedFNATP.start();
105 
106         timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE + 2 * MILLIS_IN_HOUR);
107         boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
108         assertTrue(triggerred);
109         String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
110         Assertions.assertEquals("foo-2011-12-20.log", elapsedPeriodsFileName);
111     }
112 
113     @Test
114     public void extraIntegerTokenInFileNamePatternShouldBeDetected() {
115         String pattern = "test-%d{yyyy-MM-dd'T'HH}-%i.log.zip";
116         tbrp.setFileNamePattern(pattern);
117         tbrp.start();
118 
119         Assertions.assertFalse(tbrp.isStarted());
120         StatusChecker statusChecker = new StatusChecker(context);
121         statusChecker.assertContainsMatch(Status.ERROR, "Filename pattern .{37} contains an integer token converter");
122     }
123 }