User: user57  
  Date: 02/04/05 20:47:26

  Modified:    src/main/org/jboss/test/proxycompiler/test
                        ProxyCompilerUnitTestCase.java
  Added:       src/main/org/jboss/test/proxycompiler/test
                        BeanProxyUnitTestCase.java
  Log:
   o Moved ProxyCompilerUnitTestCase to BeanProxyUnitTestCase, since it tested
     that a deployed CMP2 bean proxy worked
   o New ProxyCompilerUnitTestCase tests that ProxyCompiler handles genration
     of proxies for interfaces and abstract classes with various signatures
     and returns.  It also tests that the invokation works too, though it
     could probably be improved some, right now we just assume that if it can
     call the method and process the return that it worked.
  
  Revision  Changes    Path
  1.2       +322 -97   
jbosstest/src/main/org/jboss/test/proxycompiler/test/ProxyCompilerUnitTestCase.java
  
  Index: ProxyCompilerUnitTestCase.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jbosstest/src/main/org/jboss/test/proxycompiler/test/ProxyCompilerUnitTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ProxyCompilerUnitTestCase.java    7 Mar 2002 17:03:55 -0000       1.1
  +++ ProxyCompilerUnitTestCase.java    6 Apr 2002 04:47:26 -0000       1.2
  @@ -1,123 +1,348 @@
  -
   /*
    * JBoss, the OpenSource J2EE webOS
    *
    * Distributable under LGPL license.
    * See terms of license at gnu.org.
  - *
    */
   
  -package org.jboss.test.proxycompiler.test; // Generated package name
  +package org.jboss.test.proxycompiler.test;
  +
  +import java.lang.reflect.Method;
   
   import junit.framework.*;
  -import org.jboss.test.JBossTestCase;
  -import org.jboss.test.proxycompiler.Util;
  -import org.jboss.test.proxycompiler.beans.interfaces.ProxyCompilerTest;
  -import org.jboss.test.proxycompiler.beans.interfaces.ProxyCompilerTestHome;
  -import java.util.Iterator;
  -import java.util.Collection;
   
  +import org.apache.log4j.Category;
  +
  +import org.jboss.proxy.compiler.*;
   
   /**
  - * ProxyCompilerTestUnitTestCase.java
  - *
  + * Test the ability of the proxy compiler to create proxies for
  + * simple/complex interfaces and classes.
    *
  - * Created: Wed Jan 30 00:16:57 2002
  - *
  - * @author <a href="mailto:[EMAIL PROTECTED]";>David Jencks</a>
  - * @version
  + * @version <tt>$Revision: 1.2 $</tt>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
    */
  -
  -public class ProxyCompilerUnitTestCase extends JBossTestCase 
  +public class ProxyCompilerUnitTestCase
  +   extends TestCase 
   {
  -   private ProxyCompilerTestHome home;
  -   
  -   public ProxyCompilerUnitTestCase (String name)
  +   private static final Category log = 
Category.getInstance(ProxyCompilerUnitTestCase.class);
  +
  +   protected InvocationHandler handler;
  +
  +   public ProxyCompilerUnitTestCase(String name)
      {
         super(name);
      }
   
  -   public static Test suite() throws Exception
  +   protected void setUp() throws Exception
      {
  -      return getDeploySetup(ProxyCompilerUnitTestCase.class, 
"proxycompiler-test.jar");
  +      super.setUp();
  +
  +      handler = new LoggingInvocationHandler(log);
  +   }
  +
  +   /**
  +    * Create a value for the given type.  If non-void primitive, then
  +    * return a wrapper, else return null.
  +    */
  +   protected static Object createValue(Class type)
  +   {
  +      Object rv = null;
  +
  +      if (type.isPrimitive()) {
  +         if (type == Boolean.TYPE)
  +            rv = new Boolean(false);
  +         
  +         else if (type == Byte.TYPE)
  +            rv = new Byte((byte)0);
  +         
  +         else if (type == Character.TYPE)
  +            rv = new Character((char)0);
  +         
  +         else if (type == Short.TYPE)
  +            rv = new Short((short)0);
  +         
  +         else if (type == Integer.TYPE)
  +            rv = new Integer(0);
  +         
  +         else if (type == Long.TYPE)
  +            rv = new Long(0);
  +         
  +         else if (type == Float.TYPE)
  +            rv = new Float(0);
  +         
  +         else if (type == Double.TYPE)
  +            rv = new Double(0);
  +         
  +         else if (type == Void.TYPE)
  +            rv = null;
  +         
  +         else 
  +            throw new Error("unreachable");
  +      }
  +
  +      return rv;
      }
   
  -   public void testProxyCompilerTest() throws Exception
  +   /**
  +    * An InvocationHandler which simplly logs all calls
  +    */
  +   public static class LoggingInvocationHandler
  +      implements InvocationHandler
      {
  -      
  -      Integer pk = new Integer(1);
  -      ProxyCompilerTest bean = home.create(pk);
  -      
  -      assertEquals("Object argument error", pk, bean.getPk());
  -      
  -      bean.setBool(true);
  -      assertTrue("boolean argument error", bean.getBool());
  -      
  -      byte byteArg = (byte)123;
  -      bean.setByte(byteArg);
  -      assertEquals("byte argument error", byteArg, bean.getByte());
  -      
  -      char charArg = 'N';
  -      bean.setChar(charArg);
  -      assertEquals("char argument error", charArg, bean.getChar());
  -      
  -      double doubleArg = 1.5;
  -      bean.setDouble(doubleArg);
  -      assertEquals("double argument error", doubleArg, bean.getDouble(), 0.01);
  -
  -      float floatArg = 1.5f;
  -      bean.setFloat(floatArg);
  -      assertEquals("float argument error", floatArg, bean.getFloat(), 0.01f);
  -      
  -      int intArg = 234;
  -      bean.setInt(intArg);
  -      assertEquals("int argument error", intArg, bean.getInt());
  -      
  -      long longArg = 23456L;
  -      bean.setLong(longArg);
  -      assertEquals("long argument error", longArg, bean.getLong());
  -      
  -      short shortArg = (short)7;
  -      bean.setShort(shortArg);
  -      assertEquals("short argument error", shortArg, bean.getShort());
  -      
  -      Object[] objectArrayArg = new Object[]{new Integer(4), "Hello Mum", new 
Float(123.0)};
  -      bean.setObjectArray(objectArrayArg);
  -      Object[] objectReturnArg = bean.getObjectArray();
  -      for ( int i = 0;  i < objectArrayArg.length;  i++ ) {
  -         assertEquals("Object[] argument error", objectArrayArg[i], 
objectReturnArg[i]);
  -      }
  -      
  -      int[] intArrayArg = new int[]{2, 4, 6, 8};
  -      bean.setIntArray(intArrayArg);
  -      int[] intReturnArg = bean.getIntArray();
  -      for ( int i = 0;  i < intArrayArg.length;  i++ ) {
  -         assertEquals("int[] argument error", intArrayArg[i], intReturnArg[i]);
  -      }
  -
  -      assertTrue("noArgs argument error", bean.noArgsMethod());
  -      
  -      String stringRep = Util.getStringRepresentation(intArg, pk, intArrayArg, 
objectArrayArg);
  -
  -      String returnArg = bean.complexSignatureMethod(intArg, pk, intArrayArg, 
objectArrayArg);
  -
  -      assertEquals("complex argument error", stringRep, returnArg);
  -
  -   }
  -
  -   protected void setUp()
  -      throws Exception
  -   {
  -      home = (ProxyCompilerTestHome)getInitialContext().lookup("ProxyCompilerTest");
  -      getLog().debug("Remove ProxyCompilerTest bean instances");
  -
  -      Collection beansColl = home.findAll();
  -      
  -      for(Iterator beans = beansColl.iterator(); beans.hasNext();) {
  -         ProxyCompilerTest bean = (ProxyCompilerTest)beans.next();
  -         getLog().debug("Removing " + bean.getPrimaryKey());
  -         bean.remove();
  +      private Category log;
  +
  +      public LoggingInvocationHandler(final Category log) {
  +         this.log = log;
  +      }
  +
  +      public Object invoke(Object dummy, Method method, Object[] args) throws 
Throwable {
  +         log.debug("invoked: " + dummy + "," + method + "," + args);
  +
  +         log.debug("arguments: ");
  +         for (int i=0; i<args.length; i++) {
  +            String msg = "   arg" + i + ": " + args[i];
  +            if (args[i] != null) msg += ", type=" + args[i].getClass();
  +            log.debug(msg);
  +         }
  +
  +         Object value = createValue(method.getReturnType());
  +         log.debug("return value: " + value);
  +         return value;
  +      }
  +   }
  +
  +   protected Object createProxy(Class type) throws Exception
  +   {
  +      Object proxy = Proxy.newProxyInstance(type.getClass().getClassLoader(),
  +                                            new Class[] { type },
  +                                            handler);
  +
  +      log.debug("new proxy: " + proxy);
  +
  +      return proxy;
  +   }
  +
  +   protected void invokeDeclaredMethods(Object obj, Class type) throws Exception
  +   {
  +      Method[] methods = type.getDeclaredMethods();
  +
  +      for (int i=0; i<methods.length; i++) {
  +         log.debug("Invoking method: "  + methods[i]);
  +
  +         Class[] pTypes = methods[i].getParameterTypes();
  +         Object[] args = new Object[pTypes.length];
  +
  +         // create some dummy arg values
  +         for (int j=0; j<args.length; j++) {
  +            args[j] = createValue(pTypes[j]);
  +         }
  +
  +         Object rv = methods[i].invoke(obj, args);
  +
  +         log.debug("Method returned: " + rv);
         }
      }
   
  -}// ProxyCompilerTestUnitTestCase
  +   public static interface EmptyInterface
  +   {
  +      // empty
  +   }
  +
  +   public void testEmptyInterface() throws Exception
  +   {
  +      createProxy(EmptyInterface.class);
  +   }
  +
  +   public static interface SimpleInterface
  +   {
  +      void simple();
  +   }
  +
  +   public void testSimpleInterface() throws Exception
  +   {
  +      Object obj = createProxy(SimpleInterface.class);
  +      invokeDeclaredMethods(obj, SimpleInterface.class);
  +   }
  +
  +   public static interface ReturnValues
  +   {
  +      // returns, no args
  +
  +      void noargs();
  +
  +      Object Object_noargs();
  +
  +      byte byte__noargs();
  +
  +      boolean boolean_noargs();
  +
  +      char char_noargs();
  +
  +      int int_noargs();
  +
  +      short short_noargs();
  +
  +      long long_noargs();
  +
  +      float float_noargs();
  +
  +      double double_noargs();
  +   }
  +
  +   public void testReturnValues() throws Exception
  +   {
  +      Object obj = createProxy(ReturnValues.class);
  +      invokeDeclaredMethods(obj, ReturnValues.class);
  +   }
  +
  +   public static interface CommonMethodParameters
  +   {
  +      // no returns, different args
  +
  +      void boolean1(boolean a);
  +
  +      void boolean2(boolean a, boolean b);
  +
  +      void boolean3(boolean a, boolean b, boolean c);
  +
  +      void boolean4(boolean a, boolean b, boolean c, boolean d);
  +
  +      void byte1(byte a);
  +
  +      void byte2(byte a, byte b);
  +
  +      void byte3(byte a, byte b, byte c);
  +
  +      void byte4(byte a, byte b, byte c, byte d);
  +
  +      void char1(char a);
  +
  +      void char2(char a, char b);
  +
  +      void char3(char a, char b, char c);
  +
  +      void char4(char a, char b, char c, char d);
  +
  +      void short1(short a);
  +
  +      void short2(short a, short b);
  +
  +      void short3(short a, short b, short c);
  +
  +      void short4(short a, short b, short c, short d);
  +
  +      void int1(int a);
  +
  +      void int2(int a, int b);
  +
  +      void int3(int a, int b, int c);
  +
  +      void int4(int a, int b, int c, int d);
  +
  +      void long1(long a);
  +
  +      void long2(long a, long b);
  +
  +      void long3(long a, long b, long c);
  +
  +      void long4(long a, long b, long c, long d);
  +
  +      void long5(long a, long b, long c, long d, long e);
  +
  +      void long6(long a, long b, long c, long d, long e, long f);
  +
  +      void float1(float a);
  +
  +      void float2(float a, float b);
  +
  +      void float3(float a, float b, float c);
  +
  +      void float4(float a, float b, float c, float d);
  +
  +      void double1(double a);
  +
  +      void double2(double a, double b);
  +
  +      void double3(double a, double b, double c);
  +
  +      void double4(double a, double b, double c, double d);
  +
  +      void double5(double a, double b, double c, double d, double e);
  +
  +      void double6(double a, double b, double c, double d, double e, double f);
  +
  +      void Object1(Object a);
  +
  +      void Object2(Object a, Object b);
  +
  +      void Object3(Object a, Object b, Object c);
  +
  +      void Object4(Object a, Object b, Object c, Object d);
  +   }
  +
  +   public void testCommonMethodParameters() throws Exception
  +   {
  +      Object obj = createProxy(CommonMethodParameters.class);
  +      invokeDeclaredMethods(obj, CommonMethodParameters.class);
  +   }
  +
  +   public static abstract class SimpleAbstractClass
  +   {
  +      public abstract void test1();
  +
  +      public abstract Object test2();
  +
  +      public abstract Object test3(Object obj);
  +
  +      public abstract Object test4(Object obj) throws Exception;
  +   }
  +
  +   public void testSimpleAbstractClass() throws Exception
  +   {
  +      Object obj = createProxy(SimpleAbstractClass.class);
  +      invokeDeclaredMethods(obj, SimpleAbstractClass.class);
  +   }
  +
  +   public static interface ComplexInterface
  +      extends EmptyInterface, SimpleInterface, CommonMethodParameters
  +   {
  +      interface NestedInterface
  +         extends CommonMethodParameters
  +      {
  +         // blah
  +      }
  +
  +      abstract class NestedAbstractClass
  +         extends SimpleAbstractClass
  +      {
  +         // blah
  +      }
  +
  +      class ConcreteClass
  +      {
  +         // blah
  +      }
  +
  +      long complex1(boolean a, byte b, char c, short d, int e, long f, float g, 
double h, Object i)
  +         throws Exception, Error, Throwable;
  +
  +      Object[] complex2(boolean[] a, byte[] b, char[] c, short[] d, int[] e, long[] 
f, float[] g, double[] h, Object[] i)
  +         throws Exception, Error, Throwable;
  +
  +      Object[][] complex3(boolean[][] a, byte[][] b, char[][] c, short[][] d, 
int[][] e, long[][] f, float[][] g, double[][] h, Object[][] i)
  +         throws Exception, Error, Throwable;
  +
  +      Object[][][] complex4(boolean[] a, byte[][] b, char[][][] c, short[][][][] d, 
int[][][][][] e, long[][][][][][] f, float[][][][][][][] g, double[][][][][][][][] h, 
Object[][][][][][][][][] i)
  +         throws Exception, Error, Throwable;
  +   }
  +
  +   public void testComplexInterface() throws Exception
  +   {
  +      Object obj = createProxy(ComplexInterface.class);
  +      invokeDeclaredMethods(obj, EmptyInterface.class);
  +      invokeDeclaredMethods(obj, SimpleInterface.class);
  +      invokeDeclaredMethods(obj, CommonMethodParameters.class);
  +      invokeDeclaredMethods(obj, ComplexInterface.class);
  +   }
  +}
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/proxycompiler/test/BeanProxyUnitTestCase.java
  
  Index: BeanProxyUnitTestCase.java
  ===================================================================
  
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   *
   */
  
  package org.jboss.test.proxycompiler.test; // Generated package name
  
  import junit.framework.*;
  
  import org.jboss.test.JBossTestCase;
  
  import org.jboss.test.proxycompiler.Util;
  import org.jboss.test.proxycompiler.beans.interfaces.ProxyCompilerTest;
  import org.jboss.test.proxycompiler.beans.interfaces.ProxyCompilerTestHome;
  
  import java.util.Iterator;
  import java.util.Collection;
  
  /**
   * Tests the proxy generation for beans.
   * 
   * <p>Currently only tests CMP2 beans.
   *
   * Created: Wed Jan 30 00:16:57 2002
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author <a href="mailto:[EMAIL PROTECTED]";>David Jencks</a>
   */
  public class BeanProxyUnitTestCase 
     extends JBossTestCase 
  {
     private ProxyCompilerTestHome home;
     
     public BeanProxyUnitTestCase(final String name)
     {
        super(name);
     }
  
     public static Test suite() throws Exception
     {
        return getDeploySetup(ProxyCompilerUnitTestCase.class, 
"proxycompiler-test.jar");
     }
  
     public void testProxyCompilerTest() throws Exception
     {
        
        Integer pk = new Integer(1);
        ProxyCompilerTest bean = home.create(pk);
        
        assertEquals("Object argument error", pk, bean.getPk());
        
        bean.setBool(true);
        assertTrue("boolean argument error", bean.getBool());
        
        byte byteArg = (byte)123;
        bean.setByte(byteArg);
        assertEquals("byte argument error", byteArg, bean.getByte());
        
        char charArg = 'N';
        bean.setChar(charArg);
        assertEquals("char argument error", charArg, bean.getChar());
        
        double doubleArg = 1.5;
        bean.setDouble(doubleArg);
        assertEquals("double argument error", doubleArg, bean.getDouble(), 0.01);
  
        float floatArg = 1.5f;
        bean.setFloat(floatArg);
        assertEquals("float argument error", floatArg, bean.getFloat(), 0.01f);
        
        int intArg = 234;
        bean.setInt(intArg);
        assertEquals("int argument error", intArg, bean.getInt());
        
        long longArg = 23456L;
        bean.setLong(longArg);
        assertEquals("long argument error", longArg, bean.getLong());
        
        short shortArg = (short)7;
        bean.setShort(shortArg);
        assertEquals("short argument error", shortArg, bean.getShort());
        
        Object[] objectArrayArg = new Object[]{new Integer(4), "Hello Mum", new 
Float(123.0)};
        bean.setObjectArray(objectArrayArg);
        Object[] objectReturnArg = bean.getObjectArray();
        for ( int i = 0;  i < objectArrayArg.length;  i++ ) {
           assertEquals("Object[] argument error", objectArrayArg[i], 
objectReturnArg[i]);
        }
        
        int[] intArrayArg = new int[]{2, 4, 6, 8};
        bean.setIntArray(intArrayArg);
        int[] intReturnArg = bean.getIntArray();
        for ( int i = 0;  i < intArrayArg.length;  i++ ) {
           assertEquals("int[] argument error", intArrayArg[i], intReturnArg[i]);
        }
  
        assertTrue("noArgs argument error", bean.noArgsMethod());
        
        String stringRep = Util.getStringRepresentation(intArg, pk, intArrayArg, 
objectArrayArg);
  
        String returnArg = bean.complexSignatureMethod(intArg, pk, intArrayArg, 
objectArrayArg);
  
        assertEquals("complex argument error", stringRep, returnArg);
  
     }
  
     protected void setUp()
        throws Exception
     {
        home = (ProxyCompilerTestHome)getInitialContext().lookup("ProxyCompilerTest");
        getLog().debug("Remove ProxyCompilerTest bean instances");
  
        Collection beansColl = home.findAll();
        
        for(Iterator beans = beansColl.iterator(); beans.hasNext();) {
           ProxyCompilerTest bean = (ProxyCompilerTest)beans.next();
           getLog().debug("Removing " + bean.getPrimaryKey());
           bean.remove();
        }
     }
  
  }// ProxyCompilerTestUnitTestCase
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to