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.helper;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import ch.qos.logback.core.Context;
29  import ch.qos.logback.core.ContextBase;
30  import ch.qos.logback.core.testUtil.CoreTestConstants;
31  import ch.qos.logback.core.testUtil.StatusChecker;
32  import ch.qos.logback.core.util.Compare;
33  
34  /**
35   * @author Ceki Gulcu
36   */
37  public class CompressTest {
38  
39      Context context = new ContextBase();
40  
41      @Before
42      public void setUp() throws IOException {
43          // Copy source files
44          // Delete output files
45          {
46              File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.copy");
47              File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.txt");
48  
49              copy(source, dest);
50              File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz");
51              target.mkdirs();
52              target.delete();
53          }
54          {
55              File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.copy");
56              File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt");
57              copy(source, dest);
58              File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.gz");
59              target.mkdirs();
60              target.delete();
61          }
62          {
63              File source = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.copy");
64              File dest = new File(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.txt");
65              copy(source, dest);
66              File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress3.txt.zip");
67              target.mkdirs();
68              target.delete();
69          }
70      }
71  
72      @Test
73      public void test1() throws Exception {
74          Compressor compressor = new Compressor(CompressionMode.GZ);
75          compressor.setContext(context);
76          compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress1.txt",
77                  CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz", null);
78  
79          StatusChecker checker = new StatusChecker(context);
80          assertTrue(checker.isErrorFree(0));
81          assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz",
82                  CoreTestConstants.TEST_SRC_PREFIX + "witness/compress1.txt.gz"));
83      }
84  
85      @Test
86      public void test2() throws Exception {
87          Compressor compressor = new Compressor(CompressionMode.GZ);
88          compressor.setContext(context);
89          compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt",
90                  CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt", null);
91  
92          StatusChecker checker = new StatusChecker(context);
93          assertTrue(checker.isErrorFree(0));
94  
95          assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.gz",
96                  CoreTestConstants.TEST_SRC_PREFIX + "witness/compress2.txt.gz"));
97      }
98  
99      @Test
100     public void test3() throws Exception {
101         Compressor compressor = new Compressor(CompressionMode.ZIP);
102         compressor.setContext(context);
103         compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress3.txt",
104                 CoreTestConstants.OUTPUT_DIR_PREFIX + "compress3.txt", "compress3.txt");
105         StatusChecker checker = new StatusChecker(context);
106         assertTrue(checker.isErrorFree(0));
107 
108         // we don't know how to compare .zip files
109         // assertTrue(Compare.compare(CoreTestConstants.OUTPUT_DIR_PREFIX
110         // + "compress3.txt.zip", CoreTestConstants.TEST_SRC_PREFIX
111         // + "witness/compress3.txt.zip"));
112     }
113 
114     private void copy(File src, File dst) throws IOException {
115         InputStream in = new FileInputStream(src);
116         OutputStream out = new FileOutputStream(dst);
117         byte[] buf = new byte[1024];
118         int len;
119         while ((len = in.read(buf)) > 0) {
120             out.write(buf, 0, len);
121         }
122         in.close();
123         out.close();
124     }
125 
126 }