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