/*
* 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.hibernatesession;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.util.HashSet;
import junit.framework.TestCase;
import org.hibernate.proxy.CGLIBProxyFactory;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.ProxyFactory;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;
public class ProxySerializationTestCase extends TestCase
{
public void testWriteTestOnRegularSerialization() throws Exception
{
DataPoint proxyPoint = createProxy();
Object obj = ((HibernateProxy)proxyPoint).writeReplace();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
objOut.writeObject(proxyPoint);
ByteArrayInputStream byteInp = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream objInput = new ObjectInputStream(byteInp);
DataPoint proxyPoint2 = (DataPoint)objInput.readObject();
try
{
proxyPoint2.exception();
fail("An exception should be generated");
}
catch (Exception e)
{
}
}
public void testWriteTestOnJBossSerialization() throws Exception
{
try
{
DataPoint proxyPoint = createProxy();
Object obj = ((HibernateProxy)proxyPoint).writeReplace();
//System.out.println("ProxyPoint= " + proxyPoint);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
JBossObjectOutputStream objOut = new JBossObjectOutputStream(byteOut);
objOut.writeObject(proxyPoint);
ByteArrayInputStream byteInp = new ByteArrayInputStream(byteOut.toByteArray());
JBossObjectInputStream objInput = new JBossObjectInputStream(byteInp);
DataPoint proxyPoint2 = (DataPoint)objInput.readObject();
try
{
proxyPoint2.exception();
fail("An exception should be generated");
}
catch (Exception e)
{
}
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
}
public static DataPoint createProxy() throws NoSuchMethodException {
MockHibernateSession.isOpened=true;
ProxyFactory pf = new CGLIBProxyFactory();
HashSet proxyInterfaces = new HashSet();
proxyInterfaces.add(HibernateProxy.class);
Class mappedClass = DataPoint.class;
Method proxyGetIdentifierMethod = mappedClass.getMethod("getId",new Class[]{});
Method proxySetIdentifierMethod = mappedClass.getMethod("setId",new Class[]{Long.TYPE});
pf.postInstantiate(
"Test" ,
mappedClass,
proxyInterfaces,
proxyGetIdentifierMethod,
proxySetIdentifierMethod,
null
);
DataPoint proxyPoint = (DataPoint)pf.getProxy(new Long(0),new MockHibernateSession());
MockHibernateSession.isOpened=false;
return proxyPoint;
}
}