vinayc 2004/03/18 10:49:34
Added: integrationtests/src/test/org/apache/altrmi/test/tools/generator
BCELProxyGeneratorTestCase.java
TestInvocationHandler.java TestRemoteInterface.java
Log:
migrating bcel proxy generator test case
Revision Changes Path
1.1
incubator-altrmi/integrationtests/src/test/org/apache/altrmi/test/tools/generator/BCELProxyGeneratorTestCase.java
Index: BCELProxyGeneratorTestCase.java
===================================================================
/* ====================================================================
* Copyright 2001 - 2004
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.altrmi.test.tools.generator;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.apache.altrmi.client.impl.DefaultProxyHelper;
import org.apache.altrmi.client.impl.direct.DirectHostContext;
import org.apache.altrmi.client.impl.ClientSideClassFactory;
import org.apache.altrmi.client.impl.DumbClientMonitor;
import org.apache.altrmi.client.impl.DefaultConnectionPinger;
import org.apache.altrmi.server.ProxyGenerator;
import org.apache.altrmi.common.DefaultThreadPool;
import org.apache.altrmi.tools.generator.BCELProxyGeneratorImpl;
import org.apache.altrmi.server.PublicationDescriptionItem;
/**
* Class BCELProxyGeneratorTest
* Unit testing of BCELProxyGeneratorImpl
* @version 1.0
* @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandrasekharan</a>
*/
public class BCELProxyGeneratorTestCase extends TestCase
{
private ProxyGenerator m_proxyGenerator;
private Class m_generatedProxyClass;
private Object m_generatedProxyObject;
private ClientSideClassFactory m_factory;
/************************ TestInterface *******************/
public static final Class m_testInterfaceClass=TestRemoteInterface.class;
public BCELProxyGeneratorTestCase(String testName)
{
super(testName);
}
private Class createNewClass()
{
if(m_generatedProxyClass!=null)
return m_generatedProxyClass;
m_proxyGenerator.setGenName("Something");
m_proxyGenerator.setInterfacesToExpose(new
PublicationDescriptionItem[]{
new PublicationDescriptionItem(m_testInterfaceClass)});
m_proxyGenerator.setClassGenDir(".");
m_proxyGenerator.verbose(true);
m_proxyGenerator.generateClass(null);
m_generatedProxyClass=((BCELProxyGeneratorImpl)m_proxyGenerator).getGeneratedClass("AltrmiGeneratedSomething_Main");
return m_generatedProxyClass;
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception
{
m_proxyGenerator =
(ProxyGenerator) Class
.forName("org.apache.altrmi.tools.generator.BCELProxyGeneratorImpl")
.newInstance();
//create the Proxy Class using the BCEL Generator
createNewClass();
m_proxyGenerator.verbose(true);
}
/**
* Method testGeneratedClassName.
* Checks whether 'Class' is created properly
*/
public void testGeneratedClassNameOfProxy()
{
assertNotNull(m_proxyGenerator);
assertNotNull(m_generatedProxyClass);
assertEquals(m_generatedProxyClass.getName().equals("AltrmiGeneratedSomething_Main"),true);
}
/**
* Method testConstructorOfProxy.
* Test if the instance is created properly using the lone
* Constructor embedded within the Proxy implementation
* @throws Exception
*/
public void testConstructorOfProxy() throws Exception
{
if(m_generatedProxyClass==null)
testGeneratedClassNameOfProxy();
TestInvocationHandler invocationHandler =new
TestInvocationHandler(new DefaultThreadPool(),
new DumbClientMonitor(), new DefaultConnectionPinger());
//create the m_factory;
m_factory=new ClientSideClassFactory(new
DirectHostContext.WithSimpleDefaults(invocationHandler), false);
DefaultProxyHelper defaultProxyHelper =
new DefaultProxyHelper(m_factory,
invocationHandler,
"PublishedName",
"ObjectName",
new Long(1010),
new Long(3030));
Constructor[] _constructors= m_generatedProxyClass.getConstructors();
//there shld be only 1 constructor for the generated proxy
// one that takes BaseServedObject as the argument
assertEquals(_constructors.length,1);
m_generatedProxyObject = _constructors[0].newInstance(new
Object[]{defaultProxyHelper});
assertNotNull(m_generatedProxyObject);
}
/**
* Method testGetReferenceIDMethodOfProxy.
* Testing
* =================================
* public Long altrmiGetReferenceID(Object factoryThatIsAsking) {
* return mBaseServedObject.getReferenceID(factoryThatIsAsking);
* }
* =================================
* @throws Exception
*/
public void testGetReferenceIDMethodOfProxy() throws Exception
{
if(m_generatedProxyObject==null)
testConstructorOfProxy();
Method _getReferenceIDMethod =
m_generatedProxyClass.getMethod("altrmiGetReferenceID",new
Class[]{Object.class});
assertNotNull(_getReferenceIDMethod);
Object _ret = _getReferenceIDMethod.invoke(m_generatedProxyObject,new
Object[]{m_factory});
assertEquals(new Long(1010),_ret);
}
/**
* Method testGeneratedMethodsPassOne.
* Testing
* This test involves the crux of the stub-generation
* routine.
* 1. Pass an test interface for stub-generation
* 2. Test the created stub
* @throws Exception
*/
public void testGeneratedMethodsPassOne() throws Exception
{
if(m_generatedProxyObject==null)
testConstructorOfProxy();
Method[] methods = m_generatedProxyClass.getMethods();
for(int i=0;i<methods.length;i++)
{
if(methods[i].getName().indexOf("test")==-1)
{
continue;
}
//System.out.println("Testing method["+methods[i].getName()+"]");
Object[] _arguments= new
Object[methods[i].getParameterTypes().length];
for(int j=0;j<_arguments.length;j++)
{
_arguments[j]=m_testInterfaceClass.getField(methods[i].getName()+"_arg"+j).get(null);
//System.out.println("argType["+methods[i].getParameterTypes()[j]+"]arg["+j+"]"+_arguments[j]);
}
if(methods[i].getParameterTypes().length==0)
_arguments=null;
Object _ret=methods[i].invoke(m_generatedProxyObject,_arguments);
if(methods[i].getReturnType()!=Void.TYPE)
{
assertEquals(m_testInterfaceClass.getField(methods[i].getName()+"_retValue").get(null),_ret);
}
}
}
}
1.1
incubator-altrmi/integrationtests/src/test/org/apache/altrmi/test/tools/generator/TestInvocationHandler.java
Index: TestInvocationHandler.java
===================================================================
/* ====================================================================
* Copyright 2001 - 2004
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.altrmi.test.tools.generator;
import java.lang.reflect.Method;
import org.apache.altrmi.client.impl.AbstractClientInvocationHandler;
import org.apache.altrmi.client.ClientMonitor;
import org.apache.altrmi.client.ConnectionPinger;
import org.apache.altrmi.common.Reply;
import org.apache.altrmi.common.Request;
import org.apache.altrmi.common.ExceptionReply;
import org.apache.altrmi.common.MethodReply;
import org.apache.altrmi.common.MethodRequest;
import org.apache.altrmi.common.OpenConnectionReply;
import org.apache.altrmi.common.OpenConnectionRequest;
import org.apache.altrmi.common.ThreadPool;
import org.apache.altrmi.server.ServerInvocationHandler;
/**
* TestInvocationHandler
* @version 1.0
* @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandrasekharan</a>
*/
public class TestInvocationHandler extends AbstractClientInvocationHandler
implements ServerInvocationHandler{
public TestInvocationHandler(ThreadPool threadPool, ClientMonitor
clientMonitor, ConnectionPinger connectionPinger)
{
super(threadPool, clientMonitor, connectionPinger);
}
public Reply handleInvocation(Request request)
{
return handleInvocation(request, "test");
}
public Reply handleInvocation(Request request, Object connectionDetails)
{
if(request instanceof OpenConnectionRequest)
{
return new OpenConnectionReply();
}
else if(request instanceof MethodRequest)
{
MethodRequest methodRequest = (MethodRequest) request;
//System.out.println("methodRequest[" +
methodRequest.getMethodSignature() + "]");
Method[] methods = TestRemoteInterface.class.getMethods();
for (int i = 0; i < methods.length; i++)
{
try
{
if
(methodRequest.getMethodSignature().indexOf(methods[i].getName()) != -1)
{
Object[] _arguments=methodRequest.getArgs();
for(int j=0;j<_arguments.length;j++)
{
if(!TestRemoteInterface.class.getField(methods[i].getName() +
"_arg"+j).get(null).equals(_arguments[j]))
{
return new ExceptionReply(new
Exception(methodRequest.getMethodSignature()+": arguments not marshalled
correctly \n expected["+TestRemoteInterface.class.getField(methods[i].getName()
+ "_arg"+j).get(null)+"] received["+_arguments[j]+"]"));
}
}
MethodReply methodReply =null;
if(methods[i].getReturnType()!=Void.TYPE)
methodReply =
new MethodReply(
TestRemoteInterface.class.getField(methods[i].getName() +
"_retValue").get(null));
else
methodReply =new MethodReply();
return methodReply;
}
}
catch(Exception e)
{
e.printStackTrace();
return new ExceptionReply(e);
}
}
}
return null;
}
/*
* @see AbstractClientInvocationHandler#tryReconnect()
*/
protected boolean tryReconnect()
{
return true;
}
/*
* @see ClientInvocationHandler#getLastRealRequest()
*/
public long getLastRealRequest()
{
return 0;
}
}
1.1
incubator-altrmi/integrationtests/src/test/org/apache/altrmi/test/tools/generator/TestRemoteInterface.java
Index: TestRemoteInterface.java
===================================================================
/* ====================================================================
* Copyright 2001 - 2004
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.altrmi.test.tools.generator;
/**
* Interface TestRemoteInterface
* Test Interface for which the stub would be generated
* The test is automated in the sense given the input args and expected
* return of the function and the testcase will test for the same .
* The method is invoked on the BCEL generated proxy and then
* the invocationhandler associated with the proxy will
* be used to complete the test cycle for the client side stub
* @version 1.0
* @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandrasekharan</a>
*/
public interface TestRemoteInterface
{
String test0(String name);
String test0_arg0="a for altrmi";
String test0_retValue="b for bat";
String test1(int me);
String test1_retValue="c for cat";
Integer test1_arg0=new Integer(1);
int test2(int you,String str);
Integer test2_retValue=new Integer(7654);
Integer test2_arg0=new Integer(1);
String test2_arg1=new String("d for dog");
Integer test3();
Integer test3_retValue=new Integer(1010);
float test4();
Float test4_retValue=new Float(100.10);
byte test6(byte[] lotsofdata);
byte[] test6_arg0=new byte[100];
byte test6_retValue=(byte)10;
double test5(int[] ia , String[] sa);
int[] test5_arg0={1,2,3};
String[] test5_arg1={"a","b","z"};
Double test5_retValue=new Double(1010.4545);
void test7();
Float test7_arg0=new Float(10);
StringBuffer test8(String something);
String test8_arg0="";
StringBuffer test8_retValue=new StringBuffer("I am here , Where are
you?");
StringBuffer test9( float f, double d1,Long d2 ,double s3,String s);
Float test9_arg0=new Float(10);
Double test9_arg1=new Double(1000);
Long test9_arg2=new Long(1000);
Double test9_arg3=new Double(1000);
String test9_arg4=new String("s for so long, are you a fool");
StringBuffer test9_retValue=new StringBuffer("e for elephant");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]