/*
* 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.aopproxy;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.jboss.aop.InstanceAdvisor;
import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.aop.joinpoint.MethodInvocation;
import org.jboss.aop.proxy.ClassProxy;
import org.jboss.aop.proxy.ClassProxyFactory;
import org.jboss.serial.benchmarks.SizeBenchmarkTestCase;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;
public class AOPTestCase extends TestCase
{
static class LocalInterceptor implements Interceptor, Serializable
{
BaseClass baseClass;
public LocalInterceptor(BaseClass baseClass)
{
this.baseClass=baseClass;
}
public String getName() {
return null;
}
public Object invoke(Invocation invocation) throws Throwable {
return baseClass.doSomething(2)*2;
}
}
static class LocalInterceptorArrayList implements Interceptor, Serializable
{
ArrayList baseClass;
public LocalInterceptorArrayList(ArrayList baseClass)
{
this.baseClass=baseClass;
}
public String getName() {
return null;
}
public Object invoke(Invocation invocation) throws Throwable {
System.out.println("Called invocation on ArrayList");
if (invocation instanceof MethodInvocation)
{
MethodInvocation methodInvoke = (MethodInvocation)invocation;
return methodInvoke.getMethod().invoke(baseClass,methodInvoke.getArguments());
}
else
{
return invocation.invokeNext();
}
}
}
public void testSimpleAOPSerialization() throws Exception
{
ClassProxy proxy = createPOJOProxy();
proxy = createPOJOProxy();
BaseClass base = (BaseClass)proxy;
System.out.println("Result = "+ base.doSomething(1));
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
JBossObjectOutputStream objout = new JBossObjectOutputStream(byteout);
objout.writeObject(proxy);
objout.flush();
SizeBenchmarkTestCase.saveFileNoCheck("/tmp/check.bin",byteout.toByteArray());
JBossObjectInputStream objectInput = new JBossObjectInputStream(new ByteArrayInputStream(byteout.toByteArray()));
BaseClass baseNew = (BaseClass)objectInput.readObject();
assertEquals(4,baseNew.doSomething(5));
}
public void testSimpleAOPSerializationOnClass() throws Exception
{
ClassProxy proxy = createPOJOProxy();
proxy = createPOJOProxy();
BaseClass base = (BaseClass)proxy;
System.out.println("Result = "+ base.doSomething(1));
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
JBossObjectOutputStream objout = new JBossObjectOutputStream(byteout);
objout.writeObject(proxy.getClass());
objout.flush();
SizeBenchmarkTestCase.saveFileNoCheck("/tmp/check.bin",byteout.toByteArray());
JBossObjectInputStream objectInput = new JBossObjectInputStream(new ByteArrayInputStream(byteout.toByteArray()));
Class baseNew = (Class)objectInput.readObject();
assertEquals(proxy.getClass(),baseNew);
//assertEquals(4,baseNew.doSomething(5));
}
public void testSimpleAOPSerializationCollection() throws Exception
{
ClassProxy proxy = createArrayListProxy();
proxy = createArrayListProxy();
ArrayList base = (ArrayList)proxy;
//base.add("Test");
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
JBossObjectOutputStream objout = new JBossObjectOutputStream(byteout);
objout.writeObject(proxy);
objout.flush();
JBossObjectInputStream objectInput = new JBossObjectInputStream(new ByteArrayInputStream(byteout.toByteArray()));
ArrayList baseNew = (ArrayList)objectInput.readObject();
//assertEquals(1,baseNew.size());
}
private ClassProxy createPOJOProxy() throws Exception {
ClassProxy proxy = ClassProxyFactory.newInstance(BaseClass.class);
InstanceAdvisor advisor = proxy._getInstanceAdvisor();
advisor.appendInterceptor(new LocalInterceptor(new BaseClass()));
return proxy;
}
private ClassProxy createArrayListProxy() throws Exception {
ClassProxy proxy = ClassProxyFactory.newInstance(ArrayList.class);
InstanceAdvisor advisor = proxy._getInstanceAdvisor();
advisor.appendInterceptor(new LocalInterceptorArrayList(new ArrayList()));
return proxy;
}
}