Package org.jboss.serial.objectmetamodel

Source Code of org.jboss.serial.objectmetamodel.DataContainerTestCase

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.serial.objectmetamodel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import org.jboss.serial.data.Child;
import org.jboss.serial.data.TestParent;
import org.jboss.serial.data.TestClassWithSQLDateOnly;
import org.jboss.serial.objectmetamodel.datatest.ClassWithPrivateWriteAndRead;
import org.jboss.serial.util.StringUtil;

import junit.framework.TestCase;

/**
* @author clebert suconic
*/
public class DataContainerTestCase extends TestCase
{
    public void testReadAndWriteObjects() throws Exception
    {
        DataContainer container = new DataContainer(true);

        TestParent myTest = new TestParent(0);
        myTest.setA(33);

        for (int i=0;i<100;i++)
        {
            myTest.map.put("key" + i,"value" + i);

        }

        Child child = new Child();
        myTest.setChild(child);
        child.setParent(myTest);

        System.out.println("MyTest(original) = " + myTest);

        TestParent myTest2 = new TestParent(0);
        myTest2.setA(34);
        myTest2.setChild(child);
        myTest2.value=myTest.value;
        myTest2.map = myTest.map;
        // This value shouldn't be serialized as the reference has already written


        ObjectOutput output = container.getOutput();
        output.writeObject(myTest);
        output.writeObject(myTest2);
        myTest2.setA(99);
        output.flush();

        ObjectInput input = container.getInput();

        TestParent obj = (TestParent)input.readObject();
        TestParent obj2 = (TestParent)input.readObject();
        System.out.println("MyTest(read) = " + obj);

        System.out.println(obj2);

        assertEquals(34,obj2.getA());

    }

    public void testReadSequence() throws Exception
    {
        DataContainer container = new DataContainer(true);
        ObjectOutput output = container.getOutput();

        for (int i = 0; i < 10; i++)
        {
            output.writeUTF("Test" + i);
        }

        output.writeChar(55);

        output.writeLong(5);

        for (int i = 0; i < 10; i++)
        {
            output.writeUTF("Test" + i);
        }

        byte[] values = new byte[5];
        for (byte i=0;i<values.length;i++)
        {
            values[i]=(byte)(i + 1);
        }

        output.write(values);

        for (int i = 0; i < 10; i++)
        {
            output.writeUTF("Test" + i);
        }
        output.flush();

        ObjectInput input = container.getInput();

        for (int i=0;i<10;i++)
        {
            String value = input.readUTF();
            assertEquals("Test" + i ,value);
        }

        assertEquals((char)55,input.readChar());

        assertEquals(5,input.readLong());

        for (int i=0;i<10;i++)
        {
            String value = input.readUTF();
            assertEquals("Test" + i ,value);
        }


        for (byte b=0;b<values.length;b++)
        {
            assertEquals((byte)(b+1),input.readByte());
        }

        for (int i=0;i<10;i++)
        {
            String value = input.readUTF();
            assertEquals("Test" + i ,value);
        }

    }


    public void testReadSequenceWithBytesOnly() throws Exception
    {
        DataContainer container = new DataContainer(true);
        ObjectOutput output = container.getOutput();

        output.writeUTF("hello");
        byte[] values = new byte[5];
        for (byte i=0;i<values.length;i++)
        {
            values[i]=(byte)(i + ((byte)1));
        }

        output.write(values);
        output.flush();

        ObjectInput input = container.getInput();

        assertEquals("hello",input.readUTF());

        for (byte b=0;b<values.length;b++)
        {
            assertEquals((byte)(b+1),input.readByte());
        }


    }


    public void testClassWithSQLDate() throws Exception
    {
        TestClassWithSQLDateOnly dateObj = new TestClassWithSQLDateOnly();

        DataContainer data = new DataContainer(true);
        ObjectOutput out = data.getOutput();
        out.writeObject(dateObj);
        out.flush();

        ObjectInput input = data.getInput();
        TestClassWithSQLDateOnly newDate = (TestClassWithSQLDateOnly)input.readObject();

        assertEquals(dateObj.newDate, newDate.newDate);
    }

    public void testWritePrimitivesByteArray() throws Exception
    {
        DataContainer data = new DataContainer(true);

        ObjectOutput output = data.getOutput();
        for (int i=0;i<10;i++)
        {
            output.writeInt(i);
        }

        for (int i=0;i<10;i++)
        {
            output.writeUTF("String" + i);
        }

        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOutput = new DataOutputStream(byteOut);

        data.saveData(dataOutput);

        dataOutput.flush();

        ByteArrayInputStream byteinp = new ByteArrayInputStream(byteOut.toByteArray());
        DataInputStream dataInput = new DataInputStream(byteinp);

        DataContainer data2 = new DataContainer(true);
        data2.loadData(dataInput);

        ObjectInput input = data.getInput();
        for (int i=0;i<10;i++)
        {
            assertEquals(i,input.readInt());
        }

        for (int i=0;i<10;i++)
        {
            assertEquals("String" + i,input.readUTF());
        }

    }

    public void testWithPrivateWriteAndRead() throws Exception
    {
        DataContainer container = new DataContainer(true);
        ObjectOutput out = container.getOutput();
        out.writeObject(new ClassWithPrivateWriteAndRead());
        ObjectInput input = container.getInput();
        Object newobj = input.readObject();
    }

}
TOP

Related Classes of org.jboss.serial.objectmetamodel.DataContainerTestCase

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.