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 org.junit.jupiter.api.AfterEach;
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.Appender;
22  import ch.qos.logback.core.Context;
23  import ch.qos.logback.core.ContextBase;
24  import ch.qos.logback.core.appender.AbstractAppenderTest;
25  import ch.qos.logback.core.testUtil.DummyEncoder;
26  import ch.qos.logback.core.status.Status;
27  import ch.qos.logback.core.testUtil.CoreTestConstants;
28  import ch.qos.logback.core.testUtil.RandomUtil;
29  import ch.qos.logback.core.status.testUtil.StatusChecker;
30  //import ch.qos.logback.core.util.StatusPrinter;
31  
32  public class RollingFileAppenderTest extends AbstractAppenderTest<Object> {
33  
34      RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
35      Context context = new ContextBase();
36  
37      TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
38      int diff = RandomUtil.getPositiveInt();
39      String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
40  
41      @BeforeEach
42      public void setUp() throws Exception {
43          // noStartTest fails if the context is set in setUp
44          // rfa.setContext(context);
45  
46          rfa.setEncoder(new DummyEncoder<Object>());
47          rfa.setName("test");
48          tbrp.setContext(context);
49          tbrp.setParent(rfa);
50      }
51  
52      @AfterEach
53      public void tearDown() throws Exception {
54      }
55  
56      @Override
57      protected Appender<Object> getAppender() {
58          return rfa;
59      }
60  
61      @Override
62      protected Appender<Object> getConfiguredAppender() {
63          rfa.setContext(context);
64          tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log");
65          tbrp.start();
66          rfa.setRollingPolicy(tbrp);
67  
68          rfa.start();
69          return rfa;
70      }
71  
72      @Test
73      public void testPrudentModeLogicalImplications() {
74          rfa.setContext(context);
75          // prudent mode will force "file" property to be null
76          rfa.setFile("some non null value");
77          rfa.setAppend(false);
78          rfa.setPrudent(true);
79  
80          tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log");
81          tbrp.start();
82          rfa.setRollingPolicy(tbrp);
83  
84          rfa.start();
85  
86          Assertions.assertTrue(rfa.isAppend());
87          Assertions.assertNull(rfa.rawFileProperty());
88          Assertions.assertTrue(rfa.isStarted());
89      }
90  
91      @Test
92      public void testPrudentModeLogicalImplicationsOnCompression() {
93          rfa.setContext(context);
94          rfa.setAppend(false);
95          rfa.setPrudent(true);
96  
97          tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log.zip");
98          tbrp.start();
99          rfa.setRollingPolicy(tbrp);
100 
101         rfa.start();
102 
103         StatusChecker checker = new StatusChecker(context);
104         Assertions.assertFalse(rfa.isStarted());
105         Assertions.assertEquals(Status.ERROR, checker.getHighestLevel(0));
106     }
107 
108     @Test
109     public void testFilePropertyAfterRollingPolicy() {
110         rfa.setContext(context);
111         rfa.setRollingPolicy(tbrp);
112         rfa.setFile("x");
113         // StatusPrinter.print(context);
114         StatusChecker statusChecker = new StatusChecker(context.getStatusManager());
115         statusChecker.assertContainsMatch(Status.ERROR, "File property must be set before any triggeringPolicy ");
116     }
117 
118     @Test
119     public void testFilePropertyAfterTriggeringPolicy() {
120         rfa.setContext(context);
121         rfa.setTriggeringPolicy(new SizeBasedTriggeringPolicy<Object>());
122         rfa.setFile("x");
123         StatusChecker statusChecker = new StatusChecker(context.getStatusManager());
124         statusChecker.assertContainsMatch(Status.ERROR, "File property must be set before any triggeringPolicy ");
125     }
126 
127     @Test
128     public void testFileNameWithParenthesis() {
129         // if ')' is not escaped, the test throws
130         // java.lang.IllegalStateException: FileNamePattern
131         // [.../program(x86)/toto-%d.log] does not contain a valid
132         // DateToken
133         rfa.setContext(context);
134         tbrp.setFileNamePattern(randomOutputDir + "program(x86)/toto-%d.log");
135         tbrp.start();
136         rfa.setRollingPolicy(tbrp);
137         rfa.start();
138         rfa.doAppend("hello");
139     }
140 
141     @Test
142     public void stopTimeBasedRollingPolicy() {
143         rfa.setContext(context);
144 
145         tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log.zip");
146         tbrp.start();
147         rfa.setRollingPolicy(tbrp);
148         rfa.start();
149 
150         //StatusPrinter.print(context);
151         Assertions.assertTrue(tbrp.isStarted());
152         Assertions.assertTrue(rfa.isStarted());
153         rfa.stop();
154         Assertions.assertFalse(rfa.isStarted());
155         Assertions.assertFalse(tbrp.isStarted());
156 
157     }
158 
159     @Test
160     public void stopFixedWindowRollingPolicy() {
161         rfa.setContext(context);
162         rfa.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-.log");
163 
164         FixedWindowRollingPolicy fwRollingPolicy = new FixedWindowRollingPolicy();
165         fwRollingPolicy.setContext(context);
166         fwRollingPolicy.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%i.log.zip");
167         fwRollingPolicy.setParent(rfa);
168         fwRollingPolicy.start();
169         SizeBasedTriggeringPolicy<Object> sbTriggeringPolicy = new SizeBasedTriggeringPolicy<Object>();
170         sbTriggeringPolicy.setContext(context);
171         sbTriggeringPolicy.start();
172 
173         rfa.setRollingPolicy(fwRollingPolicy);
174         rfa.setTriggeringPolicy(sbTriggeringPolicy);
175 
176         rfa.start();
177 
178         // StatusPrinter.print(context);
179         Assertions.assertTrue(fwRollingPolicy.isStarted());
180         Assertions.assertTrue(sbTriggeringPolicy.isStarted());
181         Assertions.assertTrue(rfa.isStarted());
182         rfa.stop();
183         Assertions.assertFalse(rfa.isStarted());
184         Assertions.assertFalse(fwRollingPolicy.isStarted());
185         Assertions.assertFalse(sbTriggeringPolicy.isStarted());
186 
187     }
188 
189     /**
190      * Test for http://jira.qos.ch/browse/LOGBACK-796
191      */
192     @Test
193     public void testFileShouldNotMatchFileNamePattern() {
194         rfa.setContext(context);
195         rfa.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX + "x-2013-04.log");
196         tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "x-%d{yyyy-MM}.log");
197         tbrp.start();
198 
199         rfa.setRollingPolicy(tbrp);
200         rfa.start();
201         StatusChecker statusChecker = new StatusChecker(context);
202         final String msg = "File property collides with fileNamePattern. Aborting.";
203         boolean containsMatch = statusChecker.containsMatch(Status.ERROR, msg);
204         Assertions.assertTrue(containsMatch, "Missing error: " + msg);
205     }
206 
207     @Test
208     public void collidingTimeformat() {
209         rfa.setContext(context);
210         rfa.setAppend(false);
211         rfa.setPrudent(true);
212 
213         tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d{dd}.log.zip");
214         tbrp.start();
215         rfa.setRollingPolicy(tbrp);
216 
217         rfa.start();
218 
219         StatusChecker checker = new StatusChecker(context);
220         Assertions.assertFalse(rfa.isStarted());
221         Assertions.assertEquals(Status.ERROR, checker.getHighestLevel(0));
222         // StatusPrinter.print(context);
223         checker.assertContainsMatch("The date format in FileNamePattern will result");
224     }
225 
226     @Test
227     public void collidingFileNamePattern() {
228         String filenamePattern = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "-collision-%d.log.zip";
229 
230         RollingFileAppender<Object> appender0 = new RollingFileAppender<Object>();
231         appender0.setName("FA0");
232         appender0.setContext(context);
233         appender0.setEncoder(new DummyEncoder<Object>());
234         TimeBasedRollingPolicy<Object> tbrp0 = new TimeBasedRollingPolicy<Object>();
235         tbrp0.setContext(context);
236         tbrp0.setFileNamePattern(filenamePattern);
237         tbrp0.setParent(appender0);
238         tbrp0.start();
239         appender0.setRollingPolicy(tbrp0);
240         appender0.start();
241         Assertions.assertTrue(appender0.isStarted());
242 
243         RollingFileAppender<Object> appender1 = new RollingFileAppender<Object>();
244         appender1.setName("FA1");
245         appender1.setFile("X");
246         appender1.setContext(context);
247         appender1.setEncoder(new DummyEncoder<Object>());
248         TimeBasedRollingPolicy<Object> tbrp1 = new TimeBasedRollingPolicy<Object>();
249         tbrp1.setContext(context);
250         tbrp1.setFileNamePattern(filenamePattern);
251         tbrp1.setParent(appender1);
252         tbrp1.start();
253         appender1.setRollingPolicy(tbrp1);
254         appender1.start();
255 
256         // StatusPrinter.print(context);
257 
258         Assertions.assertFalse(appender1.isStarted());
259         StatusChecker checker = new StatusChecker(context);
260         checker.assertContainsMatch(Status.ERROR, "'FileNamePattern' option has the same value");
261     }
262 
263 }