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.helper;
015
016import static org.junit.Assert.assertTrue;
017
018import java.io.File;
019import java.io.FileInputStream;
020import java.io.FileOutputStream;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024
025import org.junit.Before;
026import org.junit.Test;
027
028import ch.qos.logback.core.Context;
029import ch.qos.logback.core.ContextBase;
030import ch.qos.logback.core.testUtil.CoreTestConstants;
031import ch.qos.logback.core.testUtil.StatusChecker;
032import ch.qos.logback.core.util.Compare;
033
034/**
035 * @author Ceki Gulcu
036 */
037public class CompressTest {
038
039    Context context = new ContextBase();
040
041    @Before
042    public void setUp() throws IOException {
043        // Copy source files
044        // Delete output files
045        {
046            File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.copy");
047            File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.txt");
048
049            copy(source, dest);
050            File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz");
051            target.mkdirs();
052            target.delete();
053        }
054        {
055            File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.copy");
056            File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt");
057            copy(source, dest);
058            File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.gz");
059            target.mkdirs();
060            target.delete();
061        }
062        {
063            File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.copy");
064            File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.txt");
065            copy(source, dest);
066            File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress3.txt.zip");
067            target.mkdirs();
068            target.delete();
069        }
070    }
071
072    @Test
073    public void test1() throws Exception {
074        Compressor compressor = new Compressor(CompressionMode.GZ);
075        compressor.setContext(context);
076        compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.txt", CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz", null);
077
078        StatusChecker checker = new StatusChecker(context);
079        assertTrue(checker.isErrorFree(0));
080        assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz", CoreTestConstants.TEST_SRC_PREFIX + "witness/compress1.txt.gz"));
081    }
082
083    @Test
084    public void test2() throws Exception {
085        Compressor compressor = new Compressor(CompressionMode.GZ);
086        compressor.setContext(context);
087        compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt", CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt", null);
088
089        StatusChecker checker = new StatusChecker(context);
090        assertTrue(checker.isErrorFree(0));
091
092        assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.gz", CoreTestConstants.TEST_SRC_PREFIX + "witness/compress2.txt.gz"));
093    }
094
095    @Test
096    public void test3() throws Exception {
097        Compressor compressor = new Compressor(CompressionMode.ZIP);
098        compressor.setContext(context);
099        compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.txt", CoreTestConstants.OUTPUT_DIR_PREFIX + "compress3.txt", "compress3.txt");
100        StatusChecker checker = new StatusChecker(context);
101        assertTrue(checker.isErrorFree(0));
102
103        // we don't know how to compare .zip files
104        // assertTrue(Compare.compare(CoreTestConstants.OUTPUT_DIR_PREFIX
105        // + "compress3.txt.zip", CoreTestConstants.TEST_SRC_PREFIX
106        // + "witness/compress3.txt.zip"));
107    }
108
109    private void copy(File src, File dst) throws IOException {
110        InputStream in = new FileInputStream(src);
111        OutputStream out = new FileOutputStream(dst);
112        byte[] buf = new byte[1024];
113        int len;
114        while ((len = in.read(buf)) > 0) {
115            out.write(buf, 0, len);
116        }
117        in.close();
118        out.close();
119    }
120
121}