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