1 package org.apache.commons.jcs.utils.zip;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.util.zip.GZIPOutputStream;
27
28
29 public class CompressionUtilUnitTest
30 extends TestCase
31 {
32
33 public final void testDecompressByteArray_failure()
34 {
35 try
36 {
37
38 CompressionUtil.decompressByteArray( null );
39
40
41 fail( "excepted an IllegalArgumentException" );
42 }
43 catch ( IllegalArgumentException exception )
44 {
45
46 return;
47 }
48 }
49
50
51
52
53
54
55 public final void testCompressDecompressByteArray_success()
56 throws IOException
57 {
58
59 String text = "This is some text to compress, not a lot, just a bit ";
60
61
62 byte[] compressedText = CompressionUtil.compressByteArray( text.getBytes() );
63 byte[] output = CompressionUtil.decompressByteArray( compressedText );
64
65
66 String result = new String( output );
67 assertNotNull( "decompressed output stream shouldn't have been null ", output );
68 assertEquals( text, result );
69 }
70
71
72
73
74
75
76 public final void testCompressDecompressGzipByteArray_success()
77 throws IOException
78 {
79
80 String text = " This is some text to compress, not a lot, just a bit ";
81
82 ByteArrayOutputStream baos = new ByteArrayOutputStream();
83 GZIPOutputStream os = new GZIPOutputStream( baos );
84
85 os.write( text.getBytes() );
86 os.flush();
87 os.close();
88
89
90 byte[] output = CompressionUtil.decompressGzipByteArray( baos.toByteArray() );
91
92
93 String result = new String( output );
94 assertNotNull( "decompressed output stream shouldn't have been null ", output );
95 assertEquals( text, result );
96 }
97 }