1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.compression;
21
22 import java.io.IOException;
23 import java.nio.charset.Charset;
24
25 import junit.framework.TestCase;
26
27 import org.apache.mina.core.buffer.IoBuffer;
28
29
30
31
32
33 public class ZlibTest extends TestCase {
34 private Zlib deflater = null;
35
36 private Zlib inflater = null;
37
38 @Override
39 protected void setUp() throws Exception {
40 deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
41 inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
42 }
43
44 public void testCompression() throws Exception {
45 String strInput = "";
46
47
48
49 for (int i = 0; i < 10; i++) {
50 strInput += "The quick brown fox jumps over the lazy dog. ";
51 }
52 IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
53
54
55
56 for (int i = 0; i < 5; i++) {
57 IoBuffer byteCompressed = deflater.deflate(byteInput);
58 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
59 String strOutput = byteUncompressed.getString(Charset.forName(
60 "UTF8").newDecoder());
61 assertTrue(strOutput.equals(strInput));
62 }
63 }
64
65 public void testCorruptedData() throws Exception {
66 String strInput = "Hello World";
67 IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
68
69 IoBuffer byteCompressed = deflater.deflate(byteInput);
70
71
72 byteCompressed.put(5, (byte) 0xa);
73 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
74 String strOutput = byteUncompressed.getString(Charset.forName("UTF8")
75 .newDecoder());
76 assertFalse(strOutput.equals(strInput));
77 }
78
79 public void testCorruptedHeader() throws Exception {
80 String strInput = "Hello World";
81 IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
82
83 IoBuffer byteCompressed = deflater.deflate(byteInput);
84
85
86 byteCompressed.put(0, (byte) 0xca);
87 try {
88 inflater.inflate(byteCompressed);
89 } catch (IOException e) {
90 assertTrue(true);
91 return;
92 }
93 assertTrue(false);
94 }
95
96 public void testFragments() throws Exception {
97 String strInput = "";
98 for (int i = 0; i < 10; i++) {
99 strInput += "The quick brown fox jumps over the lazy dog. ";
100 }
101 IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
102 IoBuffer byteCompressed = null;
103
104 for (int i = 0; i < 5; i++) {
105 byteCompressed = deflater.deflate(byteInput);
106 if (i == 0) {
107
108
109
110 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
111 String strOutput = byteUncompressed.getString(Charset.forName(
112 "UTF8").newDecoder());
113 assertTrue(strOutput.equals(strInput));
114 }
115 }
116
117
118 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
119 String strOutput = byteUncompressed.getString(Charset.forName("UTF8")
120 .newDecoder());
121 assertTrue(strOutput.equals(strInput));
122 }
123 }