Package org.jboss.serial.readresolve

Source Code of org.jboss.serial.readresolve.ReadResolveTestCase

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectStreamClass;
import java.util.Arrays;

import org.jboss.serial.DataSerializationTest;
import org.jboss.serial.classmetamodel.ClassMetamodelFactory;
import org.jboss.serial.data.TestNonSerializableSDF;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectInputStreamSharedTree;
import org.jboss.serial.io.JBossObjectOutputStream;

public class ReadResolveTestCase extends DataSerializationTest
{
  int calledResolve=0;
 
  public ReadResolveTestCase()
  {
  }

  public void executTest(Object domain) throws Throwable
  {
    if (domain instanceof String)
    {
      return;
    }
    try
    {
      System.out.println("Testing readResolve for " + domain.getClass().getName());
          ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
          JBossObjectOutputStream os = new JBossObjectOutputStream(byteOut );
          os.writeObject(domain);
          os.flush();
          os.close();
         
          ClassMetamodelFactory.clear(false);
 
          calledResolve=0;
          Object domain2 = null;
          for (int i=0;i<20;i++)
          {
              ByteArrayInputStream byteInpt = new ByteArrayInputStream(byteOut.toByteArray());
              JBossObjectInputStream is = new JBossObjectInputStream(byteInpt)
              {
            protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
              try
              {
                calledResolve++;
                return super.resolveClass(desc);
              }
              catch (Throwable e)
              {
                e.printStackTrace(System.out);
                return null;
              }
            }
               
              };
            domain2= is.readObject();
          }
          assertNotSame(domain,domain2);
          if (domain instanceof String[])
          {
            assertTrue(Arrays.equals((String[])domain, (String[])domain2));
          }
          else
          {
            assertEquals(domain,domain2);
          }
          assertTrue("ReadResolve wasn't called",calledResolve>0);

          if (domain instanceof TestNonSerializableSDF )
          {
            assertTrue("ReadResolve was called too often",calledResolve<20);
          }
          else
          {
            assertTrue("ReadResolve was called too often",calledResolve<10);
           
          }
 
          calledResolve=0;
          for (int i=0;i<20;i++)
          {
              ClassMetamodelFactory.clear(false);
              ByteArrayInputStream byteInpt = new ByteArrayInputStream(byteOut.toByteArray());
              JBossObjectInputStream is = new JBossObjectInputStream(byteInpt)
              {
            protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
              calledResolve++;
              return super.resolveClass(desc);
            }
               
              };
            domain2= is.readObject();
          }
          assertTrue("ReadResolve wasn't called as often as it should be",calledResolve>10);
 
          calledResolve=0;
          ClassMetamodelFactory.clear(false);
          for (int i=0;i<20;i++)
          {
              ByteArrayInputStream byteInpt = new ByteArrayInputStream(byteOut.toByteArray());
              JBossObjectInputStream is = new JBossObjectInputStreamSharedTree(byteInpt)
              {
            protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
              calledResolve++;
              return super.resolveClass(desc);
            }
               
              };
            domain2= is.readObject();
          }
          assertTrue("ReadResolve wasn't called for shared tree",calledResolve>0);

          calledResolve=0;
          for (int i=0;i<20;i++)
          {
              ByteArrayInputStream byteInpt = new ByteArrayInputStream(byteOut.toByteArray());
              JBossObjectInputStream is = new JBossObjectInputStreamSharedTree(byteInpt)
              {
            protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
              calledResolve++;
              return super.resolveClass(desc);
            }
               
              };
            domain2= is.readObject();
          }
          assertTrue("ReadResolve wasn't supposed to be called here",calledResolve==0);
   
    }
    catch (Throwable e)
    {
      e.printStackTrace(System.out);
      throw e;
    }
       
  }
}
TOP

Related Classes of org.jboss.serial.readresolve.ReadResolveTestCase

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.