001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.compression;
021:
022: import java.io.IOException;
023: import java.nio.charset.Charset;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.mina.common.IoBuffer;
028:
029: /**
030: * @author The Apache MINA Project (dev@mina.apache.org)
031: * @version $Rev: 581234 $, $Date: 2007-10-02 07:39:48 -0600 (Tue, 02 Oct 2007) $
032: */
033: public class ZlibTest extends TestCase {
034: private Zlib deflater = null;
035:
036: private Zlib inflater = null;
037:
038: @Override
039: protected void setUp() throws Exception {
040: deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
041: inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
042: }
043:
044: public void testCompression() throws Exception {
045: String strInput = "";
046:
047: // increase the count to as many as required to generate a long
048: // string for input
049: for (int i = 0; i < 10; i++) {
050: strInput += "The quick brown fox jumps over the lazy dog. ";
051: }
052: IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
053:
054: // increase the count to have the compression and decompression
055: // done using the same instance of Zlib
056: for (int i = 0; i < 5; i++) {
057: IoBuffer byteCompressed = deflater.deflate(byteInput);
058: IoBuffer byteUncompressed = inflater
059: .inflate(byteCompressed);
060: String strOutput = byteUncompressed.getString(Charset
061: .forName("UTF8").newDecoder());
062: assertTrue(strOutput.equals(strInput));
063: }
064: }
065:
066: public void testCorruptedData() throws Exception {
067: String strInput = "Hello World";
068: IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
069:
070: IoBuffer byteCompressed = deflater.deflate(byteInput);
071: // change the contents to something else. Since this doesn't check
072: // for integrity, it wont throw an exception
073: byteCompressed.put(5, (byte) 0xa);
074: IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
075: String strOutput = byteUncompressed.getString(Charset.forName(
076: "UTF8").newDecoder());
077: assertFalse(strOutput.equals(strInput));
078: }
079:
080: public void testCorruptedHeader() throws Exception {
081: String strInput = "Hello World";
082: IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
083:
084: IoBuffer byteCompressed = deflater.deflate(byteInput);
085: // write a bad value into the zlib header. Make sure that
086: // the decompression fails
087: byteCompressed.put(0, (byte) 0xca);
088: try {
089: inflater.inflate(byteCompressed);
090: } catch (IOException e) {
091: assertTrue(true);
092: return;
093: }
094: assertTrue(false);
095: }
096:
097: public void testFragments() throws Exception {
098: String strInput = "";
099: for (int i = 0; i < 10; i++) {
100: strInput += "The quick brown fox jumps over the lazy dog. ";
101: }
102: IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
103: IoBuffer byteCompressed = null;
104:
105: for (int i = 0; i < 5; i++) {
106: byteCompressed = deflater.deflate(byteInput);
107: if (i == 0) {
108: // decompress the first compressed output since it contains
109: // the zlib header, which will not be generated for further
110: // compressions done with the same instance
111: IoBuffer byteUncompressed = inflater
112: .inflate(byteCompressed);
113: String strOutput = byteUncompressed.getString(Charset
114: .forName("UTF8").newDecoder());
115: assertTrue(strOutput.equals(strInput));
116: }
117: }
118: // check if the last compressed data block can be decompressed
119: // successfully.
120: IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
121: String strOutput = byteUncompressed.getString(Charset.forName(
122: "UTF8").newDecoder());
123: assertTrue(strOutput.equals(strInput));
124: }
125: }
|