/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.io.util;


import junit.framework.TestCase;

/**
 *
 * ByteArrayCompressionTest
 */
public class ByteArrayCompressionTest extends TestCase {
    static final int DATA_SIZE = 32 * 1024;
    byte[] testData;
    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        testData = new byte[DATA_SIZE];
        for (int i =0; i < testData.length; i++){
            testData[i] = 'k';
        }
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testCompression() throws Exception{
        ByteArray ba = new ByteArray(testData);
        ByteArrayCompression test = new ByteArrayCompression();
        test.setCompressionLimit(DATA_SIZE/2);
        assertFalse(ByteArrayCompression.isCompressed(ba));
        ByteArray deflated = test.deflate(ba);
        assertFalse(deflated == ba);
        assertTrue(ByteArrayCompression.isCompressed(deflated));
        assertTrue(deflated.getLength() < ba.getLength());
        ByteArray inflated = test.inflate(ba);
        
        assertFalse(ByteArrayCompression.isCompressed(inflated));
        
        assertTrue(isSame(ba,inflated));
        
    }
    
    protected boolean isSame (ByteArray b1, ByteArray b2){
        boolean result = b1.getLength() == b2.getLength();
        for (int i =0;i < b1.getLength(); i++){
            result &= b1.get(i) == b2.get(i);
        }
        return result;
    }

    
}
