/**
 *
 * 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.message;

import junit.framework.TestCase;

import java.io.*;

/**
 * @version $Revision: 1.1.1.1 $
 */
public class PackageFacadeTest extends TestCase {

    public static void main(String[] args) {
        junit.textui.TestRunner.run(PackageFacadeTest.class);
    }

    public void testTransactionInfo() throws Exception {

        TransactionInfo info = new TransactionInfo();
        info.setId((short)544);

        TransactionInfo info2 = (TransactionInfo) writeAndReadPacket(info);

        assertEquals("id", info.getId(), info2.getId());
    }

    public void testProducerInfo() throws Exception {

        ProducerInfo info = new ProducerInfo();
        info.setId((short)544);
        info.setClientId("SomeClientID");

        ProducerInfo info2 = (ProducerInfo) writeAndReadPacket(info);

        assertEquals("id", info.getId(), info2.getId());
        assertEquals("clientID", info.getClientId(), info2.getClientId());

    }

    protected Packet writeAndReadPacket(Packet info) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(buffer);
        out.writeObject(new PacketFacade(info));
        out.close();

        byte[] bytes = buffer.toByteArray();
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Object value = in.readObject();
        in.close();

        assertTrue("value is PacketFacade: " + value, value instanceof PacketFacade);
        PacketFacade facade = (PacketFacade) value;

        Packet packet = facade.getPacket();
        assertTrue("Should have a non-null packet: " + packet, packet != null);
        return packet;
    }

}
