/*
 * Created on Jan 14, 2005
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.activemq.io.util;


import junit.framework.TestCase;

/**
 *
 * ByteArrayFragmentationTest
 */
public class ByteArrayFragmentationTest extends TestCase {
    static final int DATA_SIZE = 32 * 1024;
    byte[] testData;
    String testString = "";
    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        testString = "";
        for (int i = 0; testString.length() < DATA_SIZE; i++ ){
            testString += "abcdefg";
        }
        testData = testString.getBytes();
    }

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

    /**
     * Constructor for ByteArrayFragmentationTest.
     * @param arg0
     */
    public ByteArrayFragmentationTest(String arg0) {
        super(arg0);
    }

    public void testDoFragmentation() {
        ByteArray ba = new ByteArray(testData);
        ByteArrayFragmentation test = new ByteArrayFragmentation();
        test.setFragmentationLimit(DATA_SIZE/3);
        
        assertTrue(test.doFragmentation(ba));
        ByteArray[] fragments = test.fragment(ba);
        assertTrue(fragments.length > 1);
        ByteArray assembled = test.assemble(fragments);
        assertTrue(isSame(ba,assembled));
    }
    
    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;
    }
}
