Package org.serial.testshared

Source Code of org.serial.testshared.SharedObjectsTestCase

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.Collections;

import junit.framework.TestCase;

import org.jboss.serial.data.TestCircularReferences;
import org.jboss.serial.io.JBossObjectInputStreamSharedTree;
import org.jboss.serial.io.JBossObjectOutputStreamSharedTree;

/** Verifies if two objects written to the same Streaming will share the same object tree */
public class SharedObjectsTestCase extends TestCase
{
  public void testJava() throws Exception
  {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream objOut = new ObjectOutputStream(byteOut);

    TestCircularReferences circular = TestCircularReferences.createTestInstance();
    objOut.writeObject(circular.getReferences());
    objOut.writeObject(circular);
    objOut.reset();
    objOut.writeObject(circular.getReferences());
   
    ObjectInputStream objInput = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
   
    Object first = objInput.readObject();
    TestCircularReferences second = (TestCircularReferences)objInput.readObject();
    Object third = objInput.readObject();
    assertSame((Object)second.getReferences(),first);
    assertEquals(circular.getReferences().size(),((Collection)third).size());
    assertNotSame(third,second);
  }
 
  public void testJBoss() throws Exception
  {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream objOut = new JBossObjectOutputStreamSharedTree(byteOut);

    TestCircularReferences circular = TestCircularReferences.createTestInstance();
    objOut.writeObject(circular.getReferences());
    objOut.writeObject(circular);
    objOut.reset();
    objOut.writeObject(circular.getReferences());
   
    ObjectInputStream objInput = new JBossObjectInputStreamSharedTree(new ByteArrayInputStream(byteOut.toByteArray()));
   
    Object first = objInput.readObject();
    TestCircularReferences second = (TestCircularReferences)objInput.readObject();
    Object third = objInput.readObject();
    assertSame((Object)second.getReferences(),first);
    assertEquals(circular.getReferences().size(),((Collection)third).size());
    assertNotSame(third,second);
  }
}
TOP

Related Classes of org.serial.testshared.SharedObjectsTestCase

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.