You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by le...@apache.org on 2004/09/11 21:07:10 UTC

cvs commit: jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test AttributesTestCase.java RuntimeAttributesTestCase.java SealableTestCase.java TargetTestCase.java

leosutic    2004/09/11 12:07:10

  Modified:    attributes/unittest/src/test/org/apache/commons/attributes/test
                        AttributesTestCase.java
                        RuntimeAttributesTestCase.java
                        SealableTestCase.java TargetTestCase.java
  Log:
  Refactored tests.
  
  Revision  Changes    Path
  1.3       +1 -0      jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/AttributesTestCase.java
  
  Index: AttributesTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/AttributesTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AttributesTestCase.java	10 Sep 2004 20:49:38 -0000	1.2
  +++ AttributesTestCase.java	11 Sep 2004 19:07:09 -0000	1.3
  @@ -25,6 +25,7 @@
   import java.util.Iterator;
   import org.apache.commons.attributes.Attributes;
   import org.apache.commons.attributes.AttributeIndex;
  +import org.apache.commons.attributes.test.samples.*;
   import junit.framework.TestCase;
   
   public class AttributesTestCase extends TestCase {
  
  
  
  1.2       +2 -0      jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/RuntimeAttributesTestCase.java
  
  Index: RuntimeAttributesTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/RuntimeAttributesTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RuntimeAttributesTestCase.java	6 Jul 2004 20:43:39 -0000	1.1
  +++ RuntimeAttributesTestCase.java	11 Sep 2004 19:07:09 -0000	1.2
  @@ -25,6 +25,8 @@
   import java.util.Iterator;
   import org.apache.commons.attributes.Attributes;
   import org.apache.commons.attributes.AttributeIndex;
  +import org.apache.commons.attributes.test.samples.RuntimeSample;
  +import org.apache.commons.attributes.test.samples.Sample;
   import junit.framework.TestCase;
   
   public class RuntimeAttributesTestCase extends TestCase {
  
  
  
  1.3       +31 -6     jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/SealableTestCase.java
  
  Index: SealableTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/SealableTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SealableTestCase.java	28 Aug 2004 21:12:19 -0000	1.2
  +++ SealableTestCase.java	11 Sep 2004 19:07:09 -0000	1.3
  @@ -25,29 +25,54 @@
   import java.util.Iterator;
   import org.apache.commons.attributes.Attributes;
   import org.apache.commons.attributes.AttributeIndex;
  +import org.apache.commons.attributes.DefaultSealable;
   import org.apache.commons.attributes.SealedAttributeException;
   import junit.framework.TestCase;
   
   public class SealableTestCase extends TestCase {
       
  +    /**
  +     * A sample attribute with bean-like properties. Used to test the 
  +     * named parameter syntax.
  +     */
  +    public static class SampleSealableAttribute extends DefaultSealable {
  +        
  +        private int number;
  +        
  +        public SampleSealableAttribute () {
  +        }
  +        
  +        public int getNumber () {
  +            return number;
  +        }
  +        
  +        public void setNumber (int number) {
  +            checkSealed ();
  +            this.number = number;
  +        }
  +    }
  +    
  +    /** @@SealableTestCase.SampleSealableAttribute (number=1) */
  +    public static class ClassWithSealable {}
  +    
       public void testSealable () throws Exception {
  -        Method m = Sample.class.getMethod ("methodWithNamedParameters", new Class[]{ });
  -        BeanAttribute attribute = (BeanAttribute) Attributes.getAttributes (m, BeanAttribute.class).iterator ().next ();
  +        SampleSealableAttribute attribute = (SampleSealableAttribute) Attributes.getAttribute (ClassWithSealable.class, SampleSealableAttribute.class);
           
           try {
  -            attribute.setName ("Joe Doe");
  +            attribute.setNumber (11);
               fail ("Attribute should be sealed!");
           } catch (IllegalStateException ise) {
               // -- OK, attribute should be sealed.
           }
  +        
  +        assertEquals (1, attribute.getNumber ());
       }
       
       public void testSealableExceptionType () throws Exception {
  -        Method m = Sample.class.getMethod ("methodWithNamedParameters", new Class[]{ });
  -        BeanAttribute attribute = (BeanAttribute) Attributes.getAttributes (m, BeanAttribute.class).iterator ().next ();
  +        SampleSealableAttribute attribute = (SampleSealableAttribute) Attributes.getAttribute (ClassWithSealable.class, SampleSealableAttribute.class);
           
           try {
  -            attribute.setName ("Joe Doe");
  +            attribute.setNumber (11);
               fail ("Attribute should be sealed!");
           } catch (SealedAttributeException ise) {
               // -- OK, attribute should be sealed and throw a SealedAttributeException.
  
  
  
  1.4       +345 -6    jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/TargetTestCase.java
  
  Index: TargetTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/attributes/unittest/src/test/org/apache/commons/attributes/test/TargetTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TargetTestCase.java	10 Sep 2004 20:49:38 -0000	1.3
  +++ TargetTestCase.java	11 Sep 2004 19:07:09 -0000	1.4
  @@ -24,16 +24,74 @@
   public class TargetTestCase extends TestCase {
       
       /**
  -     * @@TargetTestCase.AttributeWithTarget()
  +     * @@Target(Target.METHOD)
        */
  -    public static class ClassWithInvalidDeclarations {}
  +    public static class AttributeWithTargetMethod {}
       
       /**
  -     * @@Target(Target.METHOD)
  +     * @@Target(Target.CLASS)
        */
  -    public static class AttributeWithTarget {}
  +    public static class AttributeWithTargetClass {}
       
  -    public void testMethodAttributeOnClass () throws Exception {
  +    /**
  +     * @@Target(Target.FIELD)
  +     */
  +    public static class AttributeWithTargetField {}
  +    
  +    /**
  +     * @@Target(Target.CONSTRUCTOR)
  +     */
  +    public static class AttributeWithTargetConstructor {}
  +    
  +    /**
  +     * @@Target(Target.METHOD_PARAMETER)
  +     */
  +    public static class AttributeWithTargetMethodParameter {}
  +    
  +    /**
  +     * @@Target(Target.CONSTRUCTOR_PARAMETER)
  +     */
  +    public static class AttributeWithTargetConstructorParameter {}
  +    
  +    /**
  +     * @@Target(Target.RETURN)
  +     */
  +    public static class AttributeWithTargetReturn {}
  +    
  +    /*
  +     *
  +    -------------------------------------------------------------------
  +    *
  +    */
  +    private void expectSuccess (Class[] classes) {
  +        for (int i = 0; i < classes.length; i++) {
  +            Attributes.getAttributes (classes[i]);
  +        }
  +    }
  +    
  +    private void expectFail (Class[] classes) {
  +        for (int i = 0; i < classes.length; i++) {
  +            try {
  +                Attributes.getAttributes (classes[i]);
  +                fail (classes[i].getName () + " should throw an InvalidAttributeTargetError");
  +            } catch (InvalidAttributeTargetError iate) {
  +                // OK.
  +            }
  +        }
  +    }
  +    
  +    /*
  +     *
  +    -------------------------------------------------------------------
  +    *
  +    */
  +    
  +    /**
  +     * @@TargetTestCase.AttributeWithTargetMethod()
  +     */
  +    public static class ClassWithInvalidDeclarations {}
  +    
  +    public void testErrorMessage () throws Exception {
           Class clazz1 = ClassWithInvalidDeclarations.class;
           
           try {
  @@ -41,11 +99,292 @@
               
               fail ();
           } catch (InvalidAttributeTargetError iate) {
  -            assertEquals ("Attributes of type org.apache.commons.attributes.test.TargetTestCase$AttributeWithTarget " + 
  +            assertEquals ("Attributes of type org.apache.commons.attributes.test.TargetTestCase$AttributeWithTargetMethod " + 
                   "can't be applied to org.apache.commons.attributes.test.TargetTestCase$ClassWithInvalidDeclarations. " + 
                   "They can only be applied to: METHOD", 
                   iate.getMessage ());
               // OK.
           }
  +    }
  +    
  +    /** @@TargetTestCase.AttributeWithTargetClass() */
  +    public static class ClassAttributes0 {}
  +    /** @@TargetTestCase.AttributeWithTargetMethod() */
  +    public static class ClassAttributes1 {}
  +    /** @@TargetTestCase.AttributeWithTargetField() */
  +    public static class ClassAttributes2 {}
  +    /** @@TargetTestCase.AttributeWithTargetMethodParameter() */
  +    public static class ClassAttributes3 {}
  +    /** @@TargetTestCase.AttributeWithTargetConstructorParameter() */
  +    public static class ClassAttributes4 {}
  +    /** @@TargetTestCase.AttributeWithTargetReturn() */
  +    public static class ClassAttributes5 {}
  +    /** @@TargetTestCase.AttributeWithTargetConstructor() */
  +    public static class ClassAttributes6 {}
  +    
  +    public void testClassAttributes () throws Exception {
  +        expectSuccess (new Class[]{ ClassAttributes0.class });
  +        
  +        expectFail (new Class[]{ 
  +                ClassAttributes1.class,
  +                ClassAttributes2.class,
  +                ClassAttributes3.class,
  +                ClassAttributes4.class,
  +                ClassAttributes5.class,
  +                ClassAttributes6.class
  +                });
  +    }
  +    
  +    
  +    public static class FieldAttributes0 {
  +        /** @@TargetTestCase.AttributeWithTargetClass() */
  +        private Object o;
  +    }
  +    public static class FieldAttributes1 {
  +        /** @@TargetTestCase.AttributeWithTargetMethod() */
  +        private Object o;
  +    }
  +    public static class FieldAttributes2 {
  +        /** @@TargetTestCase.AttributeWithTargetField() */
  +        private Object o;
  +    }
  +    public static class FieldAttributes3 {
  +        /** @@TargetTestCase.AttributeWithTargetMethodParameter() */
  +        private Object o;
  +    }
  +    public static class FieldAttributes4 {
  +        /** @@TargetTestCase.AttributeWithTargetConstructorParameter() */
  +        private Object o;
  +    }
  +    public static class FieldAttributes5 {
  +        /** @@TargetTestCase.AttributeWithTargetReturn() */
  +        private Object o;
  +    }
  +    public static class FieldAttributes6 {
  +        /** @@TargetTestCase.AttributeWithTargetConstructor() */
  +        private Object o;
  +    }
  +    
  +    public void testFieldAttributes () throws Exception {
  +        expectSuccess (new Class[]{ FieldAttributes2.class });
  +        
  +        expectFail (new Class[]{ 
  +                FieldAttributes0.class,
  +                FieldAttributes1.class,
  +                FieldAttributes3.class,
  +                FieldAttributes4.class,
  +                FieldAttributes5.class,
  +                FieldAttributes6.class
  +                });
  +    }
  +    
  +    public static class MethodAttributes0 {
  +        /** @@TargetTestCase.AttributeWithTargetClass() */
  +        private void method () {};
  +    }
  +    public static class MethodAttributes1 {
  +        /** @@TargetTestCase.AttributeWithTargetMethod() */
  +        private void method () {};
  +    }
  +    public static class MethodAttributes2 {
  +        /** @@TargetTestCase.AttributeWithTargetField() */
  +        private void method () {};
  +    }
  +    public static class MethodAttributes3 {
  +        /** @@TargetTestCase.AttributeWithTargetMethodParameter() */
  +        private void method () {};
  +    }
  +    public static class MethodAttributes4 {
  +        /** @@TargetTestCase.AttributeWithTargetConstructorParameter() */
  +        private void method () {};
  +    }
  +    public static class MethodAttributes5 {
  +        /** @@TargetTestCase.AttributeWithTargetReturn() */
  +        private void method () {};
  +    }
  +    public static class MethodAttributes6 {
  +        /** @@TargetTestCase.AttributeWithTargetConstructor() */
  +        private void method () {};
  +    }
  +    
  +    public void testMethodAttributes () throws Exception {
  +        expectSuccess (new Class[]{ MethodAttributes1.class });
  +        
  +        expectFail (new Class[]{ 
  +                MethodAttributes0.class,
  +                MethodAttributes2.class,
  +                MethodAttributes3.class,
  +                MethodAttributes4.class,
  +                MethodAttributes5.class,
  +                MethodAttributes6.class
  +                });
  +    }
  +    
  +    public static class ConstructorAttributes0 {
  +        /** @@TargetTestCase.AttributeWithTargetClass() */
  +        public ConstructorAttributes0 () {};
  +    }
  +    public static class ConstructorAttributes1 {
  +        /** @@TargetTestCase.AttributeWithTargetMethod() */
  +        public ConstructorAttributes1 () {};
  +    }
  +    public static class ConstructorAttributes2 {
  +        /** @@TargetTestCase.AttributeWithTargetField() */
  +        public ConstructorAttributes2 () {};
  +    }
  +    public static class ConstructorAttributes3 {
  +        /** @@TargetTestCase.AttributeWithTargetMethodParameter() */
  +        public ConstructorAttributes3 () {};
  +    }
  +    public static class ConstructorAttributes4 {
  +        /** @@TargetTestCase.AttributeWithTargetConstructorParameter() */
  +        public ConstructorAttributes4 () {};
  +    }
  +    public static class ConstructorAttributes5 {
  +        /** @@TargetTestCase.AttributeWithTargetReturn() */
  +        public ConstructorAttributes5 () {};
  +    }
  +    public static class ConstructorAttributes6 {
  +        /** @@TargetTestCase.AttributeWithTargetConstructor() */
  +        public ConstructorAttributes6 () {};
  +    }
  +    
  +    public void testConstructorAttributes () throws Exception {
  +        expectSuccess (new Class[]{ ConstructorAttributes6.class });
  +        
  +        expectFail (new Class[]{ 
  +                ConstructorAttributes0.class,
  +                ConstructorAttributes1.class,
  +                ConstructorAttributes2.class,
  +                ConstructorAttributes3.class,
  +                ConstructorAttributes4.class,
  +                ConstructorAttributes5.class
  +                });
  +    }
  +    
  +    public static class ConstructorParameterAttributes0 {
  +        /** @@.param TargetTestCase.AttributeWithTargetClass() */
  +        public ConstructorParameterAttributes0 (int param) {};
  +    }
  +    public static class ConstructorParameterAttributes1 {
  +        /** @@.param TargetTestCase.AttributeWithTargetMethod() */
  +        public ConstructorParameterAttributes1 (int param) {};
  +    }
  +    public static class ConstructorParameterAttributes2 {
  +        /** @@.param TargetTestCase.AttributeWithTargetField() */
  +        public ConstructorParameterAttributes2 (int param) {};
  +    }
  +    public static class ConstructorParameterAttributes3 {
  +        /** @@.param TargetTestCase.AttributeWithTargetMethodParameter() */
  +        public ConstructorParameterAttributes3 (int param) {};
  +    }
  +    public static class ConstructorParameterAttributes4 {
  +        /** @@.param TargetTestCase.AttributeWithTargetConstructorParameter() */
  +        public ConstructorParameterAttributes4 (int param) {};
  +    }
  +    public static class ConstructorParameterAttributes5 {
  +        /** @@.param TargetTestCase.AttributeWithTargetReturn() */
  +        public ConstructorParameterAttributes5 (int param) {};
  +    }
  +    public static class ConstructorParameterAttributes6 {
  +        /** @@.param TargetTestCase.AttributeWithTargetConstructor() */
  +        public ConstructorParameterAttributes6 (int param) {};
  +    }
  +    
  +    public void testConstructorParameterAttributes () throws Exception {
  +        expectSuccess (new Class[]{ ConstructorParameterAttributes4.class });
  +        
  +        expectFail (new Class[]{ 
  +                ConstructorParameterAttributes0.class,
  +                ConstructorParameterAttributes1.class,
  +                ConstructorParameterAttributes2.class,
  +                ConstructorParameterAttributes3.class,
  +                ConstructorParameterAttributes5.class,
  +                ConstructorParameterAttributes6.class
  +                });
  +    }
  +    
  +    public static class MethodParameterAttributes0 {
  +        /** @@.param TargetTestCase.AttributeWithTargetClass() */
  +        private void method (int param) {};
  +    }
  +    public static class MethodParameterAttributes1 {
  +        /** @@.param TargetTestCase.AttributeWithTargetMethod() */
  +        private void method (int param) {};
  +    }
  +    public static class MethodParameterAttributes2 {
  +        /** @@.param TargetTestCase.AttributeWithTargetField() */
  +        private void method (int param) {};
  +    }
  +    public static class MethodParameterAttributes3 {
  +        /** @@.param TargetTestCase.AttributeWithTargetMethodParameter() */
  +        private void method (int param) {};
  +    }
  +    public static class MethodParameterAttributes4 {
  +        /** @@.param TargetTestCase.AttributeWithTargetConstructorParameter() */
  +        private void method (int param) {};
  +    }
  +    public static class MethodParameterAttributes5 {
  +        /** @@.param TargetTestCase.AttributeWithTargetReturn() */
  +        private void method (int param) {};
  +    }
  +    public static class MethodParameterAttributes6 {
  +        /** @@.param TargetTestCase.AttributeWithTargetConstructor() */
  +        private void method (int param) {};
  +    }
  +    
  +    public void testMethodParameterAttributes () throws Exception {
  +        expectSuccess (new Class[]{ MethodParameterAttributes3.class });
  +        
  +        expectFail (new Class[]{ 
  +                MethodParameterAttributes0.class,
  +                MethodParameterAttributes1.class,
  +                MethodParameterAttributes2.class,
  +                MethodParameterAttributes4.class,
  +                MethodParameterAttributes5.class,
  +                MethodParameterAttributes6.class
  +                });
  +    }
  +    
  +    public static class ReturnAttributes0 {
  +        /** @@.return TargetTestCase.AttributeWithTargetClass() */
  +        private int method (int param) { return 0; };
  +    }
  +    public static class ReturnAttributes1 {
  +        /** @@.return TargetTestCase.AttributeWithTargetMethod() */
  +        private int method (int param) { return 0; };
  +    }
  +    public static class ReturnAttributes2 {
  +        /** @@.return TargetTestCase.AttributeWithTargetField() */
  +        private int method (int param) { return 0; };
  +    }
  +    public static class ReturnAttributes3 {
  +        /** @@.return TargetTestCase.AttributeWithTargetMethodParameter() */
  +        private int method (int param) { return 0; };
  +    }
  +    public static class ReturnAttributes4 {
  +        /** @@.return TargetTestCase.AttributeWithTargetConstructorParameter() */
  +        private int method (int param) { return 0; };
  +    }
  +    public static class ReturnAttributes5 {
  +        /** @@.return TargetTestCase.AttributeWithTargetReturn() */
  +        private int method (int param) { return 0; };
  +    }
  +    public static class ReturnAttributes6 {
  +        /** @@.return TargetTestCase.AttributeWithTargetConstructor() */
  +        private int method (int param) { return 0; };
  +    }
  +    
  +    public void testReturnAttributes () throws Exception {
  +        expectSuccess (new Class[]{ ReturnAttributes5.class });
  +        
  +        expectFail (new Class[]{ 
  +                ReturnAttributes0.class,
  +                ReturnAttributes1.class,
  +                ReturnAttributes2.class,
  +                ReturnAttributes3.class,
  +                ReturnAttributes4.class,
  +                ReturnAttributes6.class
  +                });
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org