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 java.io.File;
17  import java.io.FileOutputStream;
18  import java.io.IOException;
19  
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import ch.qos.logback.core.encoder.EchoEncoder;
25  import ch.qos.logback.core.rolling.testUtil.ScaffoldingForRollingTests;
26  import ch.qos.logback.core.testUtil.EnvUtilForTests;
27  import ch.qos.logback.core.util.StatusPrinter;
28  
29  /**
30   * A rather exhaustive set of tests. Tests include leaving the file option
31   * blank, or setting it, with and without compression, and tests with or without
32   * stopping/restarting the RollingFileAppender.
33   * <p>
34   * The regression tests log a few times using a RollingFileAppender. Then, they
35   * predict the names of the files which should be generated and compare them
36   * with witness files.
37   * <p>
38   * <pre>
39   *                Compression     file option    Stop/Restart
40   *     Test1      NO              BLANK           NO
41   *     Test2      YES             BLANK           NO
42   *     Test3      NO              BLANK           YES
43   *     Test4      NO              SET             YES
44   *     Test5      NO              SET             NO
45   *     Test6      YES             SET             NO
46   * </pre>
47   *
48   * @author Ceki G&uuml;lc&uuml;
49   */
50  public class TimeBasedRollingTest extends ScaffoldingForRollingTests {
51  
52      static final int NO_RESTART = 0;
53      static final int WITH_RESTART = 1;
54      static final int WITH_RESTART_AND_LONG_WAIT = 2000;
55  
56      static final boolean FILE_OPTION_SET = true;
57      static final boolean FILE_OPTION_BLANK = false;
58  
59      RollingFileAppender<Object> rfa1 = new RollingFileAppender<Object>();
60      TimeBasedRollingPolicy<Object> tbrp1 = new TimeBasedRollingPolicy<Object>();
61  
62      RollingFileAppender<Object> rfa2 = new RollingFileAppender<Object>();
63      TimeBasedRollingPolicy<Object> tbrp2 = new TimeBasedRollingPolicy<Object>();
64  
65      EchoEncoder<Object> encoder = new EchoEncoder<Object>();
66  
67      RolloverChecker rolloverChecker;
68  
69      @Before
70      @Override
71      public void setUp() {
72          super.setUp();
73      }
74  
75      @After
76      public void tearDown() {
77      }
78  
79      void initRFA(RollingFileAppender<Object> rfa, String filename) {
80          rfa.setContext(context);
81          rfa.setEncoder(encoder);
82          if (filename != null) {
83              rfa.setFile(filename);
84          }
85      }
86  
87      void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp, String filenamePattern, long givenTime) {
88          tbrp.setContext(context);
89          tbrp.setFileNamePattern(filenamePattern);
90          tbrp.setParent(rfa);
91          tbrp.timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
92          tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime);
93          rfa.setRollingPolicy(tbrp);
94          tbrp.start();
95          rfa.start();
96      }
97  
98      void genericTest(String testId, String patternPrefix, String compressionSuffix, boolean fileOptionIsSet, int waitDuration) throws IOException {
99          String fileName = fileOptionIsSet ? testId2FileName(testId) : null;
100         initRFA(rfa1, fileName);
101 
102         String fileNamePatternStr = randomOutputDir + patternPrefix + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}" + compressionSuffix;
103 
104         initTRBP(rfa1, tbrp1, fileNamePatternStr, currentTime);
105 
106         // compute the current filename
107         addExpectedFileName_ByDate(fileNamePatternStr, getMillisOfCurrentPeriodsStart());
108 
109         incCurrentTime(1100);
110         tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
111 
112         for (int i = 0; i < 3; i++) {
113             rfa1.doAppend("Hello---" + i);
114             addExpectedFileNamedIfItsTime_ByDate(fileNamePatternStr);
115             incCurrentTime(500);
116             tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
117             add(tbrp1.compressionFuture);
118             add(tbrp1.cleanUpFuture);
119         }
120         rfa1.stop();
121         waitForJobsToComplete();
122 
123         if (waitDuration != NO_RESTART) {
124             doRestart(testId, patternPrefix, fileOptionIsSet, waitDuration);
125         }
126         waitForJobsToComplete();
127 
128         massageExpectedFilesToCorresponToCurrentTarget(fileName, fileOptionIsSet);
129         StatusPrinter.print(context);
130         rolloverChecker.check(expectedFilenameList);
131     }
132 
133     void defaultTest(String testId, String patternPrefix, String compressionSuffix, boolean fileOptionIsSet, int waitDuration) throws IOException {
134         boolean withCompression = compressionSuffix.length() > 0;
135         rolloverChecker = new DefaultRolloverChecker(testId, withCompression, compressionSuffix);
136         genericTest(testId, patternPrefix, compressionSuffix, fileOptionIsSet, waitDuration);
137     }
138 
139     void doRestart(String testId, String patternPart, boolean fileOptionIsSet, int waitDuration) {
140         // change the timestamp of the currently actively file
141         File activeFile = new File(rfa1.getFile());
142         activeFile.setLastModified(currentTime);
143 
144         incCurrentTime(waitDuration);
145 
146         String filePatternStr = randomOutputDir + patternPart + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}";
147 
148         String fileName = fileOptionIsSet ? testId2FileName(testId) : null;
149         initRFA(rfa2, fileName);
150         initTRBP(rfa2, tbrp2, filePatternStr, currentTime);
151         for (int i = 0; i < 3; i++) {
152             rfa2.doAppend("World---" + i);
153             addExpectedFileNamedIfItsTime_ByDate(filePatternStr);
154             incCurrentTime(100);
155             tbrp2.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
156             add(tbrp2.compressionFuture);
157             add(tbrp1.cleanUpFuture);
158         }
159         rfa2.stop();
160     }
161 
162     @Test
163     public void noCompression_FileBlank_NoRestart_1() throws IOException {
164         defaultTest("test1", "test1", "", FILE_OPTION_BLANK, NO_RESTART);
165     }
166 
167     @Test
168     public void withCompression_FileBlank_NoRestart_2() throws IOException {
169         defaultTest("test2", "test2", ".gz", FILE_OPTION_BLANK, NO_RESTART);
170     }
171 
172     @Test
173     public void noCompression_FileBlank_StopRestart_3() throws IOException {
174         defaultTest("test3", "test3", "", FILE_OPTION_BLANK, WITH_RESTART);
175     }
176 
177     @Test
178     public void noCompression_FileSet_StopRestart_4() throws IOException {
179         defaultTest("test4", "test4", "", FILE_OPTION_SET, WITH_RESTART);
180     }
181 
182     @Test
183     public void noCompression_FileSet_StopRestart_WithLongWait_4B() throws IOException {
184         defaultTest("test4B", "test4B", "", FILE_OPTION_SET, WITH_RESTART_AND_LONG_WAIT);
185     }
186 
187     @Test
188     public void noCompression_FileSet_NoRestart_5() throws IOException {
189         defaultTest("test5", "test5", "", FILE_OPTION_SET, NO_RESTART);
190     }
191 
192     @Test
193     public void withCompression_FileSet_NoRestart_6() throws IOException {
194         defaultTest("test6", "test6", ".gz", FILE_OPTION_SET, NO_RESTART);
195     }
196 
197     // LOGBACK-168
198     @Test
199     public void withMissingTargetDirWithCompression() throws IOException {
200         defaultTest("test7", "%d{yyyy-MM-dd, aux}/test7", ".gz", FILE_OPTION_SET, NO_RESTART);
201     }
202 
203     @Test
204     public void withMissingTargetDirWithZipCompression() throws IOException {
205         defaultTest("test8", "%d{yyyy-MM-dd, aux}/test8", ".zip", FILE_OPTION_SET, NO_RESTART);
206     }
207 
208     @Test
209     public void failed_rename() throws IOException {
210         if (!EnvUtilForTests.isWindows())
211             return;
212 
213         FileOutputStream fos = null;
214         try {
215             String fileName = testId2FileName("failed_rename");
216             File file = new File(fileName);
217             file.getParentFile().mkdirs();
218 
219             fos = new FileOutputStream(fileName);
220 
221             String testId = "failed_rename";
222             rolloverChecker = new ZRolloverChecker(testId);
223             genericTest(testId, "failed_rename", "", FILE_OPTION_SET, NO_RESTART);
224 
225         } finally {
226             StatusPrinter.print(context);
227             if (fos != null)
228                 fos.close();
229         }
230     }
231 
232     
233     
234 }