Package org.activemq.transport.openwire

Source Code of org.activemq.transport.openwire.PacketMarshallerTestSupport

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

import junit.framework.TestCase;
import org.activemq.io.impl.AbstractPacketMarshaller;
import org.activemq.message.Packet;
import org.activemq.transport.openwire.io.OpenWireFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.DataOutput;
import java.io.FileOutputStream;
import java.io.File;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.EOFException;

/**
* @version $Revision: 1.1 $
*/
public abstract class PacketMarshallerTestSupport extends TestCase {
    private static final transient Log log = LogFactory.getLog(PacketMarshallerTestSupport.class);

    protected AbstractPacketMarshaller marshaller;
    protected OpenWireFormat wireformat = new OpenWireFormat();
    protected File directory = new File("target/sample-data");

    public void testCreateThenWriteToFileThenReadBack() throws Exception {
        Packet packet = marshaller.createPacket();
        populatePacket(packet);
        assertPacketValid(packet);

        // now lets write it to a file
        File file = new File(directory, marshaller.getClass().getName() + ".sample");
        DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
        wireformat.writePacket(packet, out);
        wireformat.writePacket(packet, out);
        out.close();

        // now lets read them back again
        DataInputStream in = new DataInputStream(new FileInputStream(file));
        packet = wireformat.readPacket(in);
        assertPacketValid(packet);

        packet = wireformat.readPacket(in);
        assertPacketValid(packet);

        // lets assert that we cannot load any more data from the file
        try {
            byte b = in.readByte();
            fail("Should have thrown EOFException. Must be data left in the file that has not been parsed. Read byte: "
                    + b + "after then end of the file: " + file);
        }
        catch (EOFException e) {
            System.out.println("Caught expected EOF exception: " + e);
        }
        finally {
            try {
                in.close();
            }
            catch (IOException e) {
                log.warn("Caught exception while closing file: " + e, e);
            }
        }
    }
   
    protected void setUp() throws Exception {
        super.setUp();

        directory.mkdirs();

        marshaller = createMarshaller();
        assertNotNull("Marshaller should not be null", marshaller);
    }

    protected void populatePacket(Packet packet) throws Exception {
    }

    protected void assertPacketValid(Packet packet) throws Exception {
        assertNotNull("packet should not be null", packet);
    }

    /**
     * @return a newly created marshaller
     */
    protected abstract AbstractPacketMarshaller createMarshaller();
}
TOP

Related Classes of org.activemq.transport.openwire.PacketMarshallerTestSupport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.