vinayc      02/05/14 14:41:22

  Added:       altrmi/src/test/org/apache/excalibur/altrmi/test/generator
                        BCELProxyGeneratorTestCase.java
                        TestInvocationHandler.java TestRemoteInterface.java
  Log:
  Unit Tests for BCEL ProxyGenerator
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-excalibur/altrmi/src/test/org/apache/excalibur/altrmi/test/generator/BCELProxyGeneratorTestCase.java
  
  Index: BCELProxyGeneratorTestCase.java
  ===================================================================
  /*
   * (c) Copyright 2001 MyCorporation.
   * All Rights Reserved.
   */
  package org.apache.excalibur.altrmi.test.generator;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  
  import junit.framework.TestCase;
  
  import org.apache.excalibur.altrmi.client.impl.DefaultProxyHelper;
  import org.apache.excalibur.altrmi.common.ProxyGenerator;
  import org.apache.excalibur.altrmi.generator.BCELProxyGeneratorImpl;
  
  
  
  /**
   * Class BCELProxyGeneratorTest
   *    Unit testing of BCELProxyGeneratorImpl
   * @version   1.0
   * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   */
  public class BCELProxyGeneratorTestCase  extends TestCase
  { 
        private ProxyGenerator mProxyGenerator;
        private Class mGeneratedProxyClass;
        private Object mGeneratedProxyObject; 
        /************************ TestInterface *******************/
        public  static final Class 
mTestInterfaceClass=//org.apache.excalibur.altrmi.test.TestInterface.class;
                                                                                
                         TestRemoteInterface.class;
        public BCELProxyGeneratorTestCase(String testName)
        {
                super(testName);
        }
   
        private Class _createNewClass()
        {
                if(mGeneratedProxyClass!=null)
                        return mGeneratedProxyClass;
                mProxyGenerator.setGenName("Something");
                mProxyGenerator.setInterfacesToExpose(new 
Class[]{mTestInterfaceClass});
                mProxyGenerator.setClassGenDir(".");
                mProxyGenerator.verbose(true);
                mProxyGenerator.generateClass(null);
                
                
                
mGeneratedProxyClass=((BCELProxyGeneratorImpl)mProxyGenerator).getGeneratedClass("AltrmiGeneratedSomething_Main");
                return mGeneratedProxyClass;
        }
  
  
  
        /*
         * @see TestCase#setUp()
         */
        protected void setUp() throws Exception
        {
          try
          {
              mProxyGenerator =
                  (ProxyGenerator) Class
                      
.forName("org.apache.excalibur.altrmi.generator.BCELProxyGeneratorImpl")
                          .newInstance();
          }
          catch (Exception e)
          {
              e.printStackTrace();
  
              throw new RuntimeException("PrimaryGenerator Impl jar not in 
classpath");
          }
          //create the Proxy Class using the BCEL Generator
                _createNewClass();      
                mProxyGenerator.verbose(true);
        }
        
        
        /**
         * Method testGeneratedClassName.
         *      Checks whether 'Class' is created properly
         */
        public void testGeneratedClassNameOfProxy()
        {
                assertNotNull(mProxyGenerator);
                assertNotNull(mGeneratedProxyClass);
                
assertEquals(mGeneratedProxyClass.getName().equals("AltrmiGeneratedSomething_Main"),true);
        }
  
        /**
         * Method testConstructorOfProxy.
         *      Test if the instance is created properly using the lone 
         *              Constructor embedded within the AltrmiProxy 
implementation
         * @throws Exception
         */
        public void testConstructorOfProxy() throws Exception
        {
                if(mGeneratedProxyClass==null)
                        testGeneratedClassNameOfProxy();
                
                DefaultProxyHelper defaultProxyHelper = 
                                                        new 
DefaultProxyHelper(null,
                                                                                
                  new TestInvocationHandler(),
                                                                                
                  "PublishedName",
                                                                                
                  "ObjectName",
                                                                                
                  new Long(1010),
                                                                                
                  new Long(3030));
        
                Constructor[] _constructors=  
mGeneratedProxyClass.getConstructors();
                //there shld be only 1 constructor for the generated proxy
                // one that takes BaseServedObject as the argument
                assertEquals(_constructors.length,1);
        
                mGeneratedProxyObject = _constructors[0].newInstance(new 
Object[]{defaultProxyHelper});
                assertNotNull(mGeneratedProxyObject);
                        
        }
        
        
        /**
         * Method testGetReferenceIDMethodOfProxy.
         *      Testing 
         * =================================
         * public Long altrmiGetReferenceID(Object factoryThatIsAsking) {
       *   return mBaseServedObject.getReferenceID(factoryThatIsAsking);
       *        }
         * =================================
         * @throws Exception
         */
        public void testGetReferenceIDMethodOfProxy() throws Exception
        {
                if(mGeneratedProxyObject==null)
                        testConstructorOfProxy();
                
                Method _getReferenceIDMethod = 
mGeneratedProxyClass.getMethod("altrmiGetReferenceID",new 
Class[]{Object.class});
                assertNotNull(_getReferenceIDMethod);
                Object _ret = 
_getReferenceIDMethod.invoke(mGeneratedProxyObject,new Object[]{null});
                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(mGeneratedProxyObject==null)
                        testConstructorOfProxy();
                
                
                Method[] __fooMethods = mGeneratedProxyClass.getMethods();
                for(int i=0;i<__fooMethods.length;i++)
                {
                        if(__fooMethods[i].getName().indexOf("test")==-1)
                        {
                                continue;
                        }
                        //System.out.println("Testing 
method["+__fooMethods[i].getName()+"]");
                        Object[] _arguments= new 
Object[__fooMethods[i].getParameterTypes().length];
                        for(int j=0;j<_arguments.length;j++)
                        {
                                
                                
_arguments[j]=mTestInterfaceClass.getField(__fooMethods[i].getName()+"_arg"+j).get(null);
                                
//System.out.println("argType["+__fooMethods[i].getParameterTypes()[j]+"]arg["+j+"]"+_arguments[j]);
                        }
                        if(__fooMethods[i].getParameterTypes().length==0)
                                _arguments=null;
                        Object 
_ret=__fooMethods[i].invoke(mGeneratedProxyObject,_arguments);
                        
                        if(__fooMethods[i].getReturnType()!=Void.TYPE)
                        {
                                
assertEquals(mTestInterfaceClass.getField(__fooMethods[i].getName()+"_retValue").get(null),_ret);
                        }
                }
        }
        
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/test/org/apache/excalibur/altrmi/test/generator/TestInvocationHandler.java
  
  Index: TestInvocationHandler.java
  ===================================================================
  /*
   * (c) Copyright 2001 MyCorporation.
   * All Rights Reserved.
   */
  package org.apache.excalibur.altrmi.test.generator;
  
  import java.lang.reflect.Method;
  
  import org.apache.excalibur.altrmi.common.AltrmiInvocationHandler;
  import org.apache.excalibur.altrmi.common.AltrmiReply;
  import org.apache.excalibur.altrmi.common.AltrmiRequest;
  import org.apache.excalibur.altrmi.common.ExceptionReply;
  import org.apache.excalibur.altrmi.common.MethodReply;
  import org.apache.excalibur.altrmi.common.MethodRequest;
  
  
  /**
   * TestInvocationHandler 
   * @version   1.0
   * @author
   */
  public class TestInvocationHandler implements AltrmiInvocationHandler {
  
        /*
         * @see AltrmiInvocationHandler#handleInvocation(AltrmiRequest)
         */
        public AltrmiReply handleInvocation(AltrmiRequest request) {
                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;
        }
  
  }
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/test/org/apache/excalibur/altrmi/test/generator/TestRemoteInterface.java
  
  Index: TestRemoteInterface.java
  ===================================================================
  /*
   * (c) Copyright 2001 MyCorporation.
   * All Rights Reserved.
   */
  package org.apache.excalibur.altrmi.test.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
   */
  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:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to