001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling;
015
016import java.io.IOException;
017import java.util.List;
018
019import org.junit.Before;
020import org.junit.Test;
021
022import ch.qos.logback.core.encoder.EchoEncoder;
023import ch.qos.logback.core.rolling.testUtil.ScaffoldingForRollingTests;
024import ch.qos.logback.core.testUtil.CoreTestConstants;
025import ch.qos.logback.core.util.FileSize;
026import ch.qos.logback.core.util.StatusPrinter;
027
028public class SizeBasedRollingTest extends ScaffoldingForRollingTests {
029
030    RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
031    FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy();
032    SizeBasedTriggeringPolicy<Object> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<Object>();
033    EchoEncoder<Object> encoder = new EchoEncoder<Object>();
034
035    @Before
036    public void setUp() {
037        super.setUp();
038        fwrp.setContext(context);
039        fwrp.setParent(rfa);
040        rfa.setContext(context);
041        sizeBasedTriggeringPolicy.setContext(context);
042    }
043
044    private void initRFA(String filename) {
045        rfa.setEncoder(encoder);
046        if (filename != null) {
047            rfa.setFile(filename);
048        }
049    }
050
051    /**
052     * Test whether FixedWindowRollingPolicy throws an exception when the
053     * ActiveFileName is not set.
054     */
055    @Test(expected = IllegalStateException.class)
056    public void activeFileNameNotSet() {
057        sizeBasedTriggeringPolicy.setMaxFileSize(new FileSize(100));
058        sizeBasedTriggeringPolicy.start();
059
060        fwrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "sizeBased-test1.%i");
061        fwrp.start();
062        // The absence of activeFileName option should cause an exception.
063    }
064
065    void generic(String testName, String fileName, String filenamePattern, List<String> expectedFilenameList) throws InterruptedException, IOException {
066        rfa.setName("ROLLING");
067        initRFA(randomOutputDir + fileName);
068
069        sizeBasedTriggeringPolicy.setMaxFileSize(new FileSize(100));
070        fwrp.setMinIndex(0);
071        fwrp.setFileNamePattern(randomOutputDir + filenamePattern);
072
073        rfa.triggeringPolicy = sizeBasedTriggeringPolicy;
074        rfa.rollingPolicy = fwrp;
075
076        fwrp.start();
077        sizeBasedTriggeringPolicy.start();
078        rfa.start();
079
080        int runLength = 40;
081        String prefix = "hello";
082        for (int i = 0; i < runLength; i++) {
083            Thread.sleep(10);
084            rfa.doAppend(prefix + i);
085        }
086        rfa.stop();
087
088        StatusPrinter.print(context);
089        existenceCheck(expectedFilenameList);
090        reverseSortedContentCheck(randomOutputDir, runLength, prefix);
091    }
092
093    @Test
094    public void smoke() throws IOException, InterruptedException {
095        expectedFilenameList.add(randomOutputDir + "a-sizeBased-smoke.log");
096        expectedFilenameList.add(randomOutputDir + "sizeBased-smoke.0");
097        expectedFilenameList.add(randomOutputDir + "sizeBased-smoke.1");
098        generic("zipped", "a-sizeBased-smoke.log", "sizeBased-smoke.%i", expectedFilenameList);
099
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}