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", CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz", null);
77  
78          StatusChecker checker = new StatusChecker(context);
79          assertTrue(checker.isErrorFree(0));
80          assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz", CoreTestConstants.TEST_SRC_PREFIX + "witness/compress1.txt.gz"));
81      }
82  
83      @Test
84      public void test2() throws Exception {
85          Compressor compressor = new Compressor(CompressionMode.GZ);
86          compressor.setContext(context);
87          compressor.compress(CoreTestConstants.TEST_SRC_PREFIX + "input/compress2.txt", CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt", null);
88  
89          StatusChecker checker = new StatusChecker(context);
90          assertTrue(checker.isErrorFree(0));
91  
92          assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt.gz", CoreTestConstants.TEST_SRC_PREFIX + "witness/compress2.txt.gz"));
93      }
94  
95      @Test
96      public void test3() throws Exception {
97          Compressor compressor = new Compressor(CompressionMode.ZIP);
98          compressor.setContext(context);
99          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 }