1   /**
2    * LOGBack: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.rolling.helper;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertTrue;
14  
15  import java.io.File;
16  import java.io.FileInputStream;
17  import java.io.FileOutputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import ch.qos.logback.core.Context;
26  import ch.qos.logback.core.ContextBase;
27  import ch.qos.logback.core.util.Compare;
28  import ch.qos.logback.core.util.Constants;
29  import ch.qos.logback.core.util.StatusPrinter;
30  
31  /**
32   * @author Ceki Gulcu
33   */
34  public class CompressTest  {
35  
36    Context context = new ContextBase();
37  
38    @Before
39    public void setUp() throws IOException {
40      // Copy source files
41      // Delete output files
42      {
43        File source = new File(Constants.TEST_DIR_PREFIX + "input/compress1.copy");
44        File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress1.txt");
45                                                          
46        copy(source, dest);
47        File target = new File(Constants.OUTPUT_DIR_PREFIX
48            + "compress1.txt.gz");
49        target.mkdirs();
50        target.delete();
51      }
52      {
53        File source = new File(Constants.TEST_DIR_PREFIX + "input/compress2.copy");
54        File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress2.txt");
55        copy(source, dest);
56        File target = new File(Constants.OUTPUT_DIR_PREFIX
57            + "compress2.txt.gz");
58        target.mkdirs();
59        target.delete();
60      }
61      {
62        File source = new File(Constants.TEST_DIR_PREFIX + "input/compress3.copy");
63        File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress3.txt");
64        copy(source, dest);
65        File target = new File(Constants.OUTPUT_DIR_PREFIX
66            + "compress3.txt.zip");
67        target.mkdirs();
68        target.delete();
69      }
70    }
71  
72  
73    @Test
74    public void test1() throws Exception {
75      Compressor compressor = new Compressor(CompressionMode.GZ,
76          Constants.TEST_DIR_PREFIX + "input/compress1.txt",
77          Constants.OUTPUT_DIR_PREFIX + "compress1.txt.gz");
78      compressor.setContext(context);
79      compressor.compress();
80  
81      StatusPrinter.print(context.getStatusManager());
82      assertEquals(0, context.getStatusManager().getCount());
83      assertTrue(Compare.gzCompare(Constants.OUTPUT_DIR_PREFIX
84          + "compress1.txt.gz", Constants.TEST_DIR_PREFIX
85          + "witness/compress1.txt.gz"));
86    }
87  
88    @Test
89    public void test2() throws Exception {
90      Compressor compressor = new Compressor(CompressionMode.GZ,
91          Constants.TEST_DIR_PREFIX + "input/compress2.txt",
92          Constants.OUTPUT_DIR_PREFIX + "compress2.txt");
93      compressor.setContext(context);
94      compressor.compress();
95  
96      StatusPrinter.print(context.getStatusManager());
97      assertEquals(0, context.getStatusManager().getCount());
98      assertTrue(Compare.gzCompare(Constants.OUTPUT_DIR_PREFIX
99          + "compress2.txt.gz", Constants.TEST_DIR_PREFIX
100         + "witness/compress2.txt.gz"));
101   }
102 
103   @Test
104   public void test3() throws Exception {
105     Compressor compressor = new Compressor(CompressionMode.ZIP, 
106         Constants.TEST_DIR_PREFIX + "input/compress3.txt",
107         Constants.OUTPUT_DIR_PREFIX + "compress3.txt");
108     compressor.setContext(context);
109     compressor.compress();
110     StatusPrinter.print(context.getStatusManager());
111     assertEquals(0, context.getStatusManager().getCount());
112     // assertTrue(Compare.compare("output/compress3.txt.zip",
113     // "witness/compress3.txt.zip"));
114   }
115 
116   private void copy(File src, File dst) throws IOException {
117     InputStream in = new FileInputStream(src);
118     OutputStream out = new FileOutputStream(dst);
119     byte[] buf = new byte[1024];
120     int len;
121     while ((len = in.read(buf)) > 0) {
122       out.write(buf, 0, len);
123     }
124     in.close();
125     out.close();
126   }
127 
128 }