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