1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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 ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.rolling.helper.CompressionMode;
19  import ch.qos.logback.core.rolling.helper.Compressor;
20  //import ch.qos.logback.core.status.testUtil.StatusChecker;
21  import ch.qos.logback.core.status.Status;
22  import ch.qos.logback.core.status.testUtil.StatusChecker;
23  import ch.qos.logback.core.util.Compare;
24  import ch.qos.logback.core.util.StatusPrinter2;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  import java.io.*;
30  
31  import static ch.qos.logback.core.rolling.helper.Compressor.COULD_NOT_OBTAIN_COMPRESSION_STRATEGY_MESSAGE;
32  import static ch.qos.logback.core.rolling.helper.Compressor.XZ_COMPRESSION_STRATEGY_CLASS_NAME;
33  import static ch.qos.logback.core.testUtil.CoreTestConstants.OUTPUT_DIR_PREFIX;
34  import static ch.qos.logback.core.testUtil.CoreTestConstants.TEST_SRC_PREFIX;
35  import static org.junit.jupiter.api.Assertions.fail;
36  
37  /**
38   * @author Ceki Gulcu
39   */
40  public class JDKOnlyCompressTest {
41      Context context = new ContextBase();
42      StatusPrinter2 statusPrinter2 = new StatusPrinter2();
43  
44      final String original1 = TEST_SRC_PREFIX + "input/compress1.original";
45      final String copy1 = TEST_SRC_PREFIX + "input/compress1.txt";
46      final String compressed1 = OUTPUT_DIR_PREFIX + "compress1.txt.gz";
47  
48      final String original2 = TEST_SRC_PREFIX + "input/compress2.original";
49      final String copy2 = TEST_SRC_PREFIX + "input/compress2.txt";
50      final String compressed2 = OUTPUT_DIR_PREFIX + "compress2.txt.gz";
51  
52      final String original3 = TEST_SRC_PREFIX + "input/compress3.original";
53      final String copy3 = TEST_SRC_PREFIX + "input/compress3.txt";
54      final String compressed3 = OUTPUT_DIR_PREFIX + "compress3.txt.zip";
55  
56      final String original4 = TEST_SRC_PREFIX + "input/compress4.original";
57      final String copy4 = TEST_SRC_PREFIX + "input/compress4.txt";
58      final String compressed4 = OUTPUT_DIR_PREFIX + "compress4.txt.xz";
59  
60      @BeforeEach
61      public void setUp() throws IOException {
62  
63      }
64  
65      protected void copySourceFilesAndDeleteCompressedOutputFiles(String originalPathStr, String copyPathStr, String compressedStr) throws IOException {
66          // Copy source files
67          // Delete output files
68  
69          File originalFile = new File(originalPathStr);
70          File copyFile = new File(copyPathStr);
71          copy(originalFile, copyFile);
72          File compressedFile = new File(compressedStr);
73          compressedFile.mkdirs();
74          compressedFile.delete();
75      }
76  
77      protected void copy(File src, File dst) throws IOException {
78          try (InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst);) {
79              byte[] buf = new byte[1024];
80              int len;
81              while ((len = in.read(buf)) > 0) {
82                  out.write(buf, 0, len);
83              }
84          }
85      }
86  
87      @Test
88      public void gzTest1() throws Exception {
89          copySourceFilesAndDeleteCompressedOutputFiles(original1, copy1, compressed1);
90          Compressor compressor = new Compressor(CompressionMode.GZ);
91          compressor.setContext(context);
92          compressor.compress(copy1, compressed1, null);
93  
94  
95          StatusChecker checker = new StatusChecker(context);
96          Assertions.assertTrue(checker.isErrorFree(0));
97          Assertions.assertTrue(Compare.gzCompare(compressed1, TEST_SRC_PREFIX + "witness/compress1.txt.gz"));
98      }
99  
100     @Test
101     public void gzTest2() throws Exception {
102         copySourceFilesAndDeleteCompressedOutputFiles(original2, copy2, compressed2);
103         Compressor compressor = new Compressor(CompressionMode.GZ);
104         compressor.setContext(context);
105         compressor.compress(copy2, compressed2, null);
106 
107         StatusChecker checker = new StatusChecker(context);
108         Assertions.assertTrue(checker.isErrorFree(0));
109 
110         Assertions.assertTrue(Compare.gzCompare(compressed2, TEST_SRC_PREFIX + "witness/compress2.txt.gz"));
111     }
112 
113     @Test
114     public void zipTest() throws Exception {
115         copySourceFilesAndDeleteCompressedOutputFiles(original3, copy3, compressed3);
116         Compressor compressor = new Compressor(CompressionMode.ZIP);
117         compressor.setContext(context);
118         compressor.compress(copy3,  compressed3, "compress3.txt");
119         StatusChecker checker = new StatusChecker(context);
120         Assertions.assertTrue(checker.isErrorFree(0));
121 
122         // we don't know how to compare .zip files
123         // Assertions.assertTrue(Compare.compare(CoreTestConstants.OUTPUT_DIR_PREFIX
124         // + "compress3.txt.zip", CoreTestConstants.TEST_SRC_PREFIX
125         // + "witness/compress3.txt.zip"));
126     }
127 
128     @Test
129     public void xzTest() throws Exception {
130         copySourceFilesAndDeleteCompressedOutputFiles(original4, copy4, compressed4);
131         Compressor compressor = new Compressor(CompressionMode.XZ);
132         compressor.setContext(context);
133         compressor.compress(copy4, compressed4, null);
134         StatusChecker checker = new StatusChecker(context);
135         //statusPrinter2.print(context);
136         checker.assertContainsMatch(Status.ERROR, "Could not instantiate "+XZ_COMPRESSION_STRATEGY_CLASS_NAME);
137         checker.assertContainsMatch(Status.WARN, COULD_NOT_OBTAIN_COMPRESSION_STRATEGY_MESSAGE);
138     }
139 
140 }