commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosu...@apache.org
Subject cvs commit: jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test AttributeIndexTestCase.java RuntimeAttributesTestCase.java SealableTestCase.java AttributesTestCase.java
Date Sun, 21 Mar 2004 00:02:37 GMT
leosutic    2004/03/20 16:02:37

  Modified:    attributes/unittest/src/test/org/apache/commons/attributes/test
                        AttributesTestCase.java
  Added:       attributes/unittest/src/test/org/apache/commons/attributes/test
                        AttributeIndexTestCase.java
                        RuntimeAttributesTestCase.java
                        SealableTestCase.java
  Log:
  Broke up the test cases into several test classes.
  
  Revision  Changes    Path
  1.11      +1 -88     jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/AttributesTestCase.java
  
  Index: AttributesTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/AttributesTestCase.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- AttributesTestCase.java	20 Mar 2004 00:04:03 -0000	1.10
  +++ AttributesTestCase.java	21 Mar 2004 00:02:37 -0000	1.11
  @@ -169,71 +169,6 @@
           assertEquals (0, Attributes.getAttributes (m).size ());
       }
       
  -    protected void collectionsEquals (Collection a, Collection b) {
  -        if (a.size () != b.size ()) {
  -            fail ("A=" + a + " B=" + b);
  -        }
  -        
  -        Iterator iter = a.iterator ();
  -        while (iter.hasNext ()) {
  -            Object o = iter.next ();
  -            if (!b.contains (o)) {
  -                fail ("B does not contain " + o);
  -            }
  -        }
  -        
  -        iter = b.iterator ();
  -        while (iter.hasNext ()) {
  -            Object o = iter.next ();
  -            if (!a.contains (o)) {
  -                fail ("A does not contain " + o);
  -            }
  -        }
  -    }
  -    
  -    public void testAttributesEqual () throws Exception {
  -        Class clazz1 = Sample.class;
  -        Class clazz2 = RuntimeSample.class;
  -        
  -        collectionsEquals (Attributes.getAttributes (clazz1), Attributes.getAttributes
(clazz2));
  -        
  -        Method[] methods1 = clazz1.getDeclaredMethods ();
  -        
  -        for (int i = 0; i < methods1.length; i++) {
  -            Method m1 = methods1[i];
  -            Method m2 = clazz2.getDeclaredMethod (m1.getName (), m1.getParameterTypes ());
  -            
  -            collectionsEquals (Attributes.getAttributes (m1), Attributes.getAttributes
(m2));
  -            
  -            int numParameters = m1.getParameterTypes().length;
  -            for (int j = 0; j < numParameters; j++) {
  -                collectionsEquals (Attributes.getParameterAttributes (m1, j), Attributes.getParameterAttributes
(m2, j));
  -            }
  -            
  -            collectionsEquals (Attributes.getReturnAttributes (m1), Attributes.getReturnAttributes
(m2));
  -        }
  -        
  -        Constructor[] ctors1 = clazz1.getDeclaredConstructors ();
  -        for (int i = 0; i < ctors1.length; i++) {
  -            Constructor c1 = ctors1[i];
  -            Constructor c2 = clazz2.getDeclaredConstructor (c1.getParameterTypes ());
  -            
  -            collectionsEquals (Attributes.getAttributes (c1), Attributes.getAttributes
(c2));
  -            
  -            int numParameters = c1.getParameterTypes().length;
  -            for (int j = 0; j < numParameters; j++) {
  -                collectionsEquals (Attributes.getParameterAttributes (c1, j), Attributes.getParameterAttributes
(c2, j));
  -            }
  -        }
  -        
  -        Field[] fields1 = clazz1.getDeclaredFields ();
  -        for (int i = 0; i < fields1.length; i++) {
  -            Field f1 = fields1[i];
  -            Field f2 = clazz2.getField (f1.getName ());
  -            collectionsEquals (Attributes.getAttributes (f1), Attributes.getAttributes
(f2));
  -        }
  -    }
  -    
       /**
        * Ensure that loading a class with the same name from two different class loaders
        * won't mess up the attribute cache.
  @@ -246,17 +181,7 @@
           Class cl2Class = cl2.loadClass ("TestClass");
           
           assertEquals ("[[TestAttribute 1]]", Attributes.getAttributes (cl1Class).toString
());
  -        assertEquals ("[[TestAttribute 2]]", Attributes.getAttributes (cl2Class).toString
());
  -    }
  -    
  -    public void testAttributeIndex () throws Exception {
  -        URLClassLoader cl2 = new URLClassLoader (new URL[]{new File ("target/cl2/cl2.jar").toURL
()}, getClass().getClassLoader ());
  -        AttributeIndex index = new AttributeIndex (cl2);
  -        Collection classes = index.getClassesWithAttribute ("TestAttribute");
  -        System.out.println (classes);
  -        assertEquals (2, classes.size ());
  -        assertTrue (classes.contains ("TestClass"));
  -        assertTrue (classes.contains ("TestClass.Inner"));        
  +        assertEquals ("[[TestAttribute TestClass]]", Attributes.getAttributes (cl2Class).toString
());
       }
       
       public void testInnerClasses () throws Exception {
  @@ -276,17 +201,5 @@
           ba.setName ("Smith, John \"Agent\"");
           ba.setAnotherNumber (42);
           assertTrue (Attributes.hasAttribute (m, ba));
  -    }
  -    
  -    public void testSealable () throws Exception {
  -        Method m = Sample.class.getMethod ("methodWithNamedParameters", new Class[]{ });
  -        BeanAttribute attribute = (BeanAttribute) Attributes.getAttributes (m, BeanAttribute.class).iterator
().next ();
  -        
  -        try {
  -            attribute.setName ("Joe Doe");
  -            fail ("Attribute should be sealed!");
  -        } catch (IllegalStateException ise) {
  -            // -- OK, attribute should be sealed.
  -        }
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/AttributeIndexTestCase.java
  
  Index: AttributeIndexTestCase.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation
   * 
   * 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.commons.attributes.test;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.lang.reflect.Constructor;
  import java.io.File;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.Collection;
  import java.util.Iterator;
  import org.apache.commons.attributes.Attributes;
  import org.apache.commons.attributes.AttributeIndex;
  import junit.framework.TestCase;
  
  public class AttributeIndexTestCase extends TestCase {
      
      private URLClassLoader cl = null;
      private AttributeIndex index = null;
      private Class TESTCLASS = null;
      private Class TESTCLASS_INNER = null;
      private Class TESTATTRIBUTE = null;
      
      public void setUp () throws Exception {
          cl = new URLClassLoader (new URL[]{new File ("target/cl2/cl2.jar").toURL ()}, getClass().getClassLoader
());
          index = new AttributeIndex (cl);
          TESTCLASS = cl.loadClass ("TestClass");
          TESTCLASS_INNER = cl.loadClass ("TestClass$Inner");
          TESTATTRIBUTE = cl.loadClass ("TestAttribute");
      }
      
      public void testAttributeIndexCompatible () throws Exception {
          Collection classes = index.getClassesWithAttribute ("TestAttribute");
          System.out.println (classes);
          assertEquals (2, classes.size ());
          assertTrue (classes.contains ("TestClass"));
          assertTrue (classes.contains ("TestClass.Inner"));        
      }
      
      public void testClasses () throws Exception {
          Collection classes = index.getClasses (TESTATTRIBUTE);
          System.out.println (classes);
          assertEquals (2, classes.size ());
          assertTrue (classes.contains (TESTCLASS));
          assertTrue (classes.contains (TESTCLASS_INNER));
      }
      
      public void testMethods () throws Exception {
          Collection methods = index.getMethods (TESTATTRIBUTE);
          System.out.println (methods);
          assertEquals (1, methods.size ());
          assertTrue (methods.contains (TESTCLASS.getDeclaredMethods()[0]));
      }
      
      public void testConstructors () throws Exception {
          Collection ctors = index.getConstructors (TESTATTRIBUTE);
          System.out.println (ctors);
          assertEquals (1, ctors.size ());
          assertTrue (ctors.contains (TESTCLASS.getDeclaredConstructors()[0]));
      }
      
      public void testConstructorParameters () throws Exception {
          Collection ctors = index.getConstructorParameters (TESTATTRIBUTE);
          System.out.println (ctors);
          assertEquals (1, ctors.size ());
          assertTrue (ctors.contains (new AttributeIndex.ConstructorParameter (TESTCLASS.getDeclaredConstructors()[0],
0)));
      }
      
      public void testMethodParameters () throws Exception {
          Collection methods = index.getMethodParameters (TESTATTRIBUTE);
          System.out.println (methods);
          assertEquals (1, methods.size ());
          assertTrue (methods.contains (new AttributeIndex.MethodParameter (TESTCLASS.getDeclaredMethods()[0],
0)));
      }
      
      public void testMethodsReturning () throws Exception {
          Collection methods = index.getMethodsReturning (TESTATTRIBUTE);
          System.out.println (methods);
          assertEquals (1, methods.size ());
          assertTrue (methods.contains (TESTCLASS.getDeclaredMethods()[0]));
      }
      
      public void testFields () throws Exception {
          Collection fields = index.getFields (TESTATTRIBUTE);
          System.out.println (fields);
          assertEquals (1, fields.size ());
          assertTrue (fields.contains (TESTCLASS.getDeclaredFields()[0]));
      }
  }
  
  
  1.1                  jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/RuntimeAttributesTestCase.java
  
  Index: RuntimeAttributesTestCase.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation
   * 
   * 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.commons.attributes.test;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.lang.reflect.Constructor;
  import java.io.File;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.Collection;
  import java.util.Iterator;
  import org.apache.commons.attributes.Attributes;
  import org.apache.commons.attributes.AttributeIndex;
  import junit.framework.TestCase;
  
  public class RuntimeAttributesTestCase extends TestCase {
     
      protected void collectionsEquals (Collection a, Collection b) {
          if (a.size () != b.size ()) {
              fail ("A=" + a + " B=" + b);
          }
          
          Iterator iter = a.iterator ();
          while (iter.hasNext ()) {
              Object o = iter.next ();
              if (!b.contains (o)) {
                  fail ("B does not contain " + o);
              }
          }
          
          iter = b.iterator ();
          while (iter.hasNext ()) {
              Object o = iter.next ();
              if (!a.contains (o)) {
                  fail ("A does not contain " + o);
              }
          }
      }
      
      public void testRuntimeAttributesEqual () throws Exception {
          Class clazz1 = Sample.class;
          Class clazz2 = RuntimeSample.class;
          
          collectionsEquals (Attributes.getAttributes (clazz1), Attributes.getAttributes (clazz2));
          
          Method[] methods1 = clazz1.getDeclaredMethods ();
          
          for (int i = 0; i < methods1.length; i++) {
              Method m1 = methods1[i];
              Method m2 = clazz2.getDeclaredMethod (m1.getName (), m1.getParameterTypes ());
              
              collectionsEquals (Attributes.getAttributes (m1), Attributes.getAttributes (m2));
              
              int numParameters = m1.getParameterTypes().length;
              for (int j = 0; j < numParameters; j++) {
                  collectionsEquals (Attributes.getParameterAttributes (m1, j), Attributes.getParameterAttributes
(m2, j));
              }
              
              collectionsEquals (Attributes.getReturnAttributes (m1), Attributes.getReturnAttributes
(m2));
          }
          
          Constructor[] ctors1 = clazz1.getDeclaredConstructors ();
          for (int i = 0; i < ctors1.length; i++) {
              Constructor c1 = ctors1[i];
              Constructor c2 = clazz2.getDeclaredConstructor (c1.getParameterTypes ());
              
              collectionsEquals (Attributes.getAttributes (c1), Attributes.getAttributes (c2));
              
              int numParameters = c1.getParameterTypes().length;
              for (int j = 0; j < numParameters; j++) {
                  collectionsEquals (Attributes.getParameterAttributes (c1, j), Attributes.getParameterAttributes
(c2, j));
              }
          }
          
          Field[] fields1 = clazz1.getDeclaredFields ();
          for (int i = 0; i < fields1.length; i++) {
              Field f1 = fields1[i];
              Field f2 = clazz2.getField (f1.getName ());
              collectionsEquals (Attributes.getAttributes (f1), Attributes.getAttributes (f2));
          }
      }
  }
  
  
  1.1                  jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/SealableTestCase.java
  
  Index: SealableTestCase.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation
   * 
   * 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.commons.attributes.test;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.lang.reflect.Constructor;
  import java.io.File;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.Collection;
  import java.util.Iterator;
  import org.apache.commons.attributes.Attributes;
  import org.apache.commons.attributes.AttributeIndex;
  import junit.framework.TestCase;
  
  public class SealableTestCase extends TestCase {
      
      public void testSealable () throws Exception {
          Method m = Sample.class.getMethod ("methodWithNamedParameters", new Class[]{ });
          BeanAttribute attribute = (BeanAttribute) Attributes.getAttributes (m, BeanAttribute.class).iterator
().next ();
          
          try {
              attribute.setName ("Joe Doe");
              fail ("Attribute should be sealed!");
          } catch (IllegalStateException ise) {
              // -- OK, attribute should be sealed.
          }
      }
  }
  
  

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


Mime
View raw message