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.IOException;
17  import java.util.List;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  import ch.qos.logback.core.encoder.EchoEncoder;
23  import ch.qos.logback.core.rolling.testUtil.ScaffoldingForRollingTests;
24  import ch.qos.logback.core.testUtil.CoreTestConstants;
25  import ch.qos.logback.core.util.FileSize;
26  import ch.qos.logback.core.util.StatusPrinter;
27  
28  public class SizeBasedRollingTest extends ScaffoldingForRollingTests {
29  
30      RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
31      FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy();
32      SizeBasedTriggeringPolicy<Object> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<Object>();
33      EchoEncoder<Object> encoder = new EchoEncoder<Object>();
34  
35      @Before
36      public void setUp() {
37          super.setUp();
38          fwrp.setContext(context);
39          fwrp.setParent(rfa);
40          rfa.setContext(context);
41          sizeBasedTriggeringPolicy.setContext(context);
42      }
43  
44      private void initRFA(String filename) {
45          rfa.setEncoder(encoder);
46          if (filename != null) {
47              rfa.setFile(filename);
48          }
49      }
50  
51      /**
52       * Test whether FixedWindowRollingPolicy throws an exception when the
53       * ActiveFileName is not set.
54       */
55      @Test(expected = IllegalStateException.class)
56      public void activeFileNameNotSet() {
57          sizeBasedTriggeringPolicy.setMaxFileSize(new FileSize(100));
58          sizeBasedTriggeringPolicy.start();
59  
60          fwrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "sizeBased-test1.%i");
61          fwrp.start();
62          // The absence of activeFileName option should cause an exception.
63      }
64  
65      void generic(String testName, String fileName, String filenamePattern, List<String> expectedFilenameList) throws InterruptedException, IOException {
66          rfa.setName("ROLLING");
67          initRFA(randomOutputDir + fileName);
68  
69          sizeBasedTriggeringPolicy.setMaxFileSize(new FileSize(100));
70          fwrp.setMinIndex(0);
71          fwrp.setFileNamePattern(randomOutputDir + filenamePattern);
72  
73          rfa.triggeringPolicy = sizeBasedTriggeringPolicy;
74          rfa.rollingPolicy = fwrp;
75  
76          fwrp.start();
77          sizeBasedTriggeringPolicy.start();
78          rfa.start();
79  
80          int runLength = 40;
81          String prefix = "hello";
82          for (int i = 0; i < runLength; i++) {
83              Thread.sleep(10);
84              rfa.doAppend(prefix + i);
85          }
86          rfa.stop();
87  
88          StatusPrinter.print(context);
89          existenceCheck(expectedFilenameList);
90          reverseSortedContentCheck(randomOutputDir, runLength, prefix);
91      }
92  
93      @Test
94      public void smoke() throws IOException, InterruptedException {
95          expectedFilenameList.add(randomOutputDir + "a-sizeBased-smoke.log");
96          expectedFilenameList.add(randomOutputDir + "sizeBased-smoke.0");
97          expectedFilenameList.add(randomOutputDir + "sizeBased-smoke.1");
98          generic("zipped", "a-sizeBased-smoke.log", "sizeBased-smoke.%i", expectedFilenameList);
99  
100     }
101 
102     @Test
103     public void gz() throws IOException, InterruptedException {
104         expectedFilenameList.add(randomOutputDir + "a-sbr-gzed.log");
105         expectedFilenameList.add(randomOutputDir + "sbr-gzed.0.gz");
106         expectedFilenameList.add(randomOutputDir + "sbr-gzed.1.gz");
107         generic("gzed", "a-sbr-gzed.log", "sbr-gzed.%i.gz", expectedFilenameList);
108     }
109 
110     // see also LBCORE-199
111     @Test
112     public void zipped() throws IOException, InterruptedException {
113         expectedFilenameList.add(randomOutputDir + "a-sbr-zipped.log");
114         expectedFilenameList.add(randomOutputDir + "sbr-zipped.0.zip");
115         expectedFilenameList.add(randomOutputDir + "sbr-zipped.1.zip");
116         generic("zipped", "a-sbr-zipped.log", "sbr-zipped.%i.zip", expectedFilenameList);
117 
118         List<String> zipFiles = filterElementsInListBySuffix(".zip");
119         zipEntryNameCheck(zipFiles, "sbr-zipped.20\\d{2}-\\d{2}-\\d{2}_\\d{4}");
120     }
121 }