/*
* 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.data.tests;
import junit.framework.TestCase;
import java.lang.reflect.Proxy;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.data.proxy.TestHandler;
import org.jboss.serial.data.proxy.ProxiedClass;
import org.jboss.serial.data.proxy.InterfaceForProxy;
/**
* $Id: ProxyTestCase.java 217 2006-04-18 18:42:42Z csuconic $
*
* @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
*/
public class ProxyTestCase extends TestCase {
public void testJavaSerialization() throws Exception
{
InterfaceForProxy proxy = createProxy();
assertEquals(6,proxy.doSomething());
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream (byteout);
out.writeObject(proxy);
out.flush();
ObjectInputStream inp = new ObjectInputStream (new ByteArrayInputStream(byteout.toByteArray()));
InterfaceForProxy proxy2 = (InterfaceForProxy)inp.readObject();
assertEquals(6,proxy2.doSomething());
}
public void testJBossSerialization() throws Exception
{
InterfaceForProxy proxy = createProxy();
assertEquals(6,proxy.doSomething());
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
JBossObjectOutputStream out = new JBossObjectOutputStream (byteout);
out.writeObject(proxy);
out.flush();
JBossObjectInputStream inp = new JBossObjectInputStream (new ByteArrayInputStream(byteout.toByteArray()));
InterfaceForProxy proxy2 = (InterfaceForProxy)inp.readObject();
assertEquals(6,proxy2.doSomething());
}
private InterfaceForProxy createProxy() {
ProxiedClass proxyClass = new ProxiedClass(2);
assertEquals(2,proxyClass.doSomething());
Class proxyClazz = Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(),new Class[]{InterfaceForProxy.class});
System.out.println(proxyClazz);
InterfaceForProxy proxy = (InterfaceForProxy) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{InterfaceForProxy.class},new TestHandler(3,proxyClass));
return proxy;
}
}