Source code: org/activemq/io/util/ByteArrayCompressionTest.java
1 /**
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.io.util;
19
20
21 import junit.framework.TestCase;
22
23 /**
24 *
25 * ByteArrayCompressionTest
26 */
27 public class ByteArrayCompressionTest extends TestCase {
28 static final int DATA_SIZE = 32 * 1024;
29 byte[] testData;
30 /*
31 * @see TestCase#setUp()
32 */
33 protected void setUp() throws Exception {
34 super.setUp();
35 testData = new byte[DATA_SIZE];
36 for (int i =0; i < testData.length; i++){
37 testData[i] = 'k';
38 }
39 }
40
41 /*
42 * @see TestCase#tearDown()
43 */
44 protected void tearDown() throws Exception {
45 super.tearDown();
46 }
47
48 public void testCompression() throws Exception{
49 ByteArray ba = new ByteArray(testData);
50 ByteArrayCompression test = new ByteArrayCompression();
51 test.setCompressionLimit(DATA_SIZE/2);
52 assertFalse(ByteArrayCompression.isCompressed(ba));
53 ByteArray deflated = test.deflate(ba);
54 assertFalse(deflated == ba);
55 assertTrue(ByteArrayCompression.isCompressed(deflated));
56 assertTrue(deflated.getLength() < ba.getLength());
57 ByteArray inflated = test.inflate(ba);
58
59 assertFalse(ByteArrayCompression.isCompressed(inflated));
60
61 assertTrue(isSame(ba,inflated));
62
63 }
64
65 protected boolean isSame (ByteArray b1, ByteArray b2){
66 boolean result = b1.getLength() == b2.getLength();
67 for (int i =0;i < b1.getLength(); i++){
68 result &= b1.get(i) == b2.get(i);
69 }
70 return result;
71 }
72
73
74 }