You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jl...@apache.org on 2004/02/24 23:36:01 UTC

cvs commit: incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/gbean GBeanInfoFactoryTest.java

jlaskowski    2004/02/24 14:36:01

  Modified:    modules/kernel/src/java/org/apache/geronimo/gbean
                        GBeanInfoFactory.java
               modules/kernel/src/test/org/apache/geronimo/gbean
                        GBeanInfoFactoryTest.java
  Log:
    o more unit tests (coverage level at 100%!)
    o introduce sanity check method to prevent NullPoinerExceptions
  
  Revision  Changes    Path
  1.13      +38 -26    incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/gbean/GBeanInfoFactory.java
  
  Index: GBeanInfoFactory.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/gbean/GBeanInfoFactory.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- GBeanInfoFactory.java	24 Feb 2004 18:41:45 -0000	1.12
  +++ GBeanInfoFactory.java	24 Feb 2004 22:36:01 -0000	1.13
  @@ -91,15 +91,11 @@
       }
   
       public GBeanInfoFactory(Class clazz) {
  -        if (clazz == null) {
  -            throw new IllegalArgumentException("argument is null");
  -        }
  -        this.name = this.className = clazz.getName();
  +        this(((Class) checkNotNull(clazz)).getName(), clazz.getName(), null);
       }
   
       public GBeanInfoFactory(String name, String className) {
  -        this.name = name;
  -        this.className = className;
  +        this(name, className, null);
       }
   
       public GBeanInfoFactory(String className, GBeanInfo source) {
  @@ -107,34 +103,50 @@
       }
   
       public GBeanInfoFactory(Class clazz, GBeanInfo source) {
  -        this(clazz.getName(), clazz.getName(), source);
  +        this(((Class) checkNotNull(clazz)).getName(), clazz.getName(), source);
       }
   
       public GBeanInfoFactory(String name, String className, GBeanInfo source) {
  -        if (name == null || className == null || source == null) {
  -            throw new IllegalArgumentException("null argument(s) supplied");
  -        }
  +        checkNotNull(name);
  +        checkNotNull(className);
  +
           this.name = name;
           this.className = className;
  -        Set sourceAttrs = source.getAttributes();
  -        if (sourceAttrs != null && !sourceAttrs.isEmpty()) {
  -            for (Iterator it = sourceAttrs.iterator(); it.hasNext();) {
  -                GAttributeInfo gattrInfo = (GAttributeInfo) it.next();
  -                attributes.put(gattrInfo.getName(), gattrInfo);
  +        if (source != null) {
  +            Set sourceAttrs = source.getAttributes();
  +            if (sourceAttrs != null && !sourceAttrs.isEmpty()) {
  +                for (Iterator it = sourceAttrs.iterator(); it.hasNext();) {
  +                    GAttributeInfo gattrInfo = (GAttributeInfo) it.next();
  +                    attributes.put(gattrInfo.getName(), gattrInfo);
  +                }
               }
  -        }
   
  -        Set sourceOps = source.getOperations();
  -        if (sourceOps != null && !sourceOps.isEmpty()) {
  -            for (Iterator it = sourceOps.iterator(); it.hasNext();) {
  -                GOperationInfo gopInfo = (GOperationInfo) it.next();
  -                operations.put(gopInfo.getName(), gopInfo);
  +            Set sourceOps = source.getOperations();
  +            if (sourceOps != null && !sourceOps.isEmpty()) {
  +                for (Iterator it = sourceOps.iterator(); it.hasNext();) {
  +                    GOperationInfo gopInfo = (GOperationInfo) it.next();
  +                    operations.put(gopInfo.getName(), gopInfo);
  +                }
               }
  +            references.addAll(source.getReferences());
  +            notifications.addAll(source.getNotifications());
  +            //in case subclass constructor has same parameters as superclass.
  +            constructor = source.getConstructor();
  +        }
  +    }
  +
  +    /**
  +     * Checks whether or not the input argument is null; otherwise it throws
  +     * {@link IllegalArgumentException}.
  +     * 
  +     * @param obj
  +     *            the input argument to validate
  +     */
  +    private static final Object checkNotNull(final Object obj) {
  +        if (obj == null) {
  +            throw new IllegalArgumentException("null argument supplied");
           }
  -        references.addAll(source.getReferences());
  -        notifications.addAll(source.getNotifications());
  -        //in case subclass constructor has same parameters as superclass.
  -        constructor = source.getConstructor();
  +        return obj;
       }
   
       public void addInterface(Class intf) {
  
  
  
  1.2       +61 -61    incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/gbean/GBeanInfoFactoryTest.java
  
  Index: GBeanInfoFactoryTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/gbean/GBeanInfoFactoryTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- GBeanInfoFactoryTest.java	24 Feb 2004 18:41:45 -0000	1.1
  +++ GBeanInfoFactoryTest.java	24 Feb 2004 22:36:01 -0000	1.2
  @@ -56,6 +56,7 @@
   package org.apache.geronimo.gbean;
   
   import java.io.Serializable;
  +import java.util.Collections;
   import java.util.Set;
   
   import junit.framework.TestCase;
  @@ -70,7 +71,15 @@
        */
       public void testGBeanInfoFactoryString() {
           assertNotNull(new GBeanInfoFactory(""));
  -        assertNotNull(new GBeanInfoFactory((String) null));
  +        try {
  +            new GBeanInfoFactory((String) null);
  +            fail("IllegalArgumentException expected");
  +        } catch (IllegalArgumentException expected) {}
  +
  +        final String gbeanName = "gbeanName";
  +        GBeanInfoFactory gbeanInfoFactory = new GBeanInfoFactory(gbeanName);
  +        assertEquals(gbeanName, gbeanInfoFactory.getBeanInfo().getName());
  +        assertEquals(gbeanName, gbeanInfoFactory.getBeanInfo().getClassName());
       }
   
       /*
  @@ -82,12 +91,38 @@
               new GBeanInfoFactory((Class) null);
               fail("IllegalArgumentException expected");
           } catch (IllegalArgumentException expected) {}
  +
  +        final Class className = String.class;
  +        GBeanInfoFactory gbeanInfoFactory = new GBeanInfoFactory(className);
  +        assertEquals(className.getName(), gbeanInfoFactory.getBeanInfo().getName());
  +        assertEquals(className.getName(), gbeanInfoFactory.getBeanInfo().getClassName());
  +    }
  +
  +    /*
  +     * test for void GBeanInfoFactory(Class, String)
  +     */
  +    public void testGBeanInfoFactoryClassString() {
  +        try {
  +            new GBeanInfoFactory((Class) null, null);
  +            fail("IllegalArgumentException expected");
  +        } catch (IllegalArgumentException expected) {}
       }
   
       /*
  -     * Class to test for void GBeanInfoFactory(String, String)
  +     * test for void GBeanInfoFactory(Class, GBeanInfo)
        */
  -    public void testGBeanInfoFactoryStringString() {
  +    public void testGBeanInfoFactoryClassGBeanInfo() {
  +        try {
  +            new GBeanInfoFactory((Class) null, (GBeanInfo) null);
  +            fail("IllegalArgumentException expected");
  +        } catch (IllegalArgumentException expected) {}
  +
  +        final Class className = String.class;
  +        GBeanInfoFactory gbeanInfoFactory = new GBeanInfoFactory(className, MockGBean.getGBeanInfo());
  +        assertEquals(className.getName(), gbeanInfoFactory.getBeanInfo().getName());
  +        assertEquals(className.getName(), gbeanInfoFactory.getBeanInfo().getClassName());
  +        assertTrue(gbeanInfoFactory.getBeanInfo().getAttributes().isEmpty());
  +        assertTrue(gbeanInfoFactory.getBeanInfo().getOperations().isEmpty());
       }
   
       /*
  @@ -105,11 +140,6 @@
       }
   
       /*
  -     * void GBeanInfoFactory(String, String, GBeanInfo)
  -     */
  -    public void testGBeanInfoFactoryStringStringGBeanInfo() {}
  -
  -    /*
        * Class to test for void addInterface(Class)
        */
       public void testAddInterfaceClass() {
  @@ -153,66 +183,36 @@
           assertEquals("setInt", gattrInfo.getSetterName());
       }
   
  -    /*
  -     * Class to test for void addInterface(Class, String[])
  -     */
  -    public void testAddInterfaceClassStringArray() {}
  -
  -    /*
  -     * Class to test for void addAttribute(String, boolean)
  -     */
  -    public void testAddAttributeStringboolean() {}
  -
  -    /*
  -     * Class to test for void addAttribute(GAttributeInfo)
  -     */
  -    public void testAddAttributeGAttributeInfo() {}
  -
  -    /*
  -     * Class to test for void setConstructor(GConstructorInfo)
  -     */
  -    public void testSetConstructorGConstructorInfo() {}
  -
  -    /*
  -     * Class to test for void setConstructor(String[], Class[])
  -     */
  -    public void testSetConstructorStringArrayClassArray() {}
  -
  -    /*
  -     * Class to test for void addOperation(GOperationInfo)
  -     */
  -    public void testAddOperationGOperationInfo() {}
  -
  -    /*
  -     * Class to test for void addOperation(String, Class[])
  -     */
  -    public void testAddOperationStringClassArray() {}
  +    private static interface SetterOnlyInterface {
   
  -    /*
  -     * Class to test for void addReference(GReferenceInfo)
  -     */
  -    public void testAddReferenceGReferenceInfo() {}
  +        public void setInt(int i);
  +    }
   
  -    /*
  -     * Class to test for void addReference(String, Class)
  -     */
  -    public void testAddReferenceStringClass() {}
  +    private static interface GetterOnlyInterface {
   
  -    public void testAddNotification() {}
  +        public int getInt();
  +    }
   
  -    public void testGetBeanInfo() {}
  +    final static GNotificationInfo notificationInfo = new GNotificationInfo("notification", Collections.singleton(null));
   
  -    protected void setUp() {}
  +    final static GReferenceInfo refInfo = new GReferenceInfo("reference", String.class);
   
  -    protected void tearDown() {}
  +    public static final class MockGBean {
   
  -    private static interface SetterOnlyInterface {
  +        public static final GBeanInfo GBEAN_INFO;
   
  -        public void setInt(int i);
  +        static {
  +            GBeanInfoFactory infoFactory = new GBeanInfoFactory(MockGBean.class);
  +            infoFactory.setConstructor(new GConstructorInfo(new String[] { String.class.getName(),
  +                    Integer.class.getName()}, new Class[] { String.class, Integer.class}));
  +            infoFactory.addNotification(notificationInfo);
  +            infoFactory.addReference(refInfo);
  +            GBEAN_INFO = infoFactory.getBeanInfo();
  +        }
  +
  +        public static GBeanInfo getGBeanInfo() {
  +            return GBEAN_INFO;
  +        }
       }
   
  -    private static interface GetterOnlyInterface {
  -
  -        public int getInt();
  -    }
   }