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