1 16 package org.apache.commons.attributes.test; 17 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Constructor ; 21 import java.io.File ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import org.apache.commons.attributes.Attributes; 27 import org.apache.commons.attributes.AttributeIndex; 28 import junit.framework.TestCase; 29 30 public class RuntimeAttributesTestCase extends TestCase { 31 32 protected void collectionsEquals (Collection a, Collection b) { 33 if (a.size () != b.size ()) { 34 fail ("A=" + a + " B=" + b); 35 } 36 37 Iterator iter = a.iterator (); 38 while (iter.hasNext ()) { 39 Object o = iter.next (); 40 if (!b.contains (o)) { 41 fail ("B does not contain " + o); 42 } 43 } 44 45 iter = b.iterator (); 46 while (iter.hasNext ()) { 47 Object o = iter.next (); 48 if (!a.contains (o)) { 49 fail ("A does not contain " + o); 50 } 51 } 52 } 53 54 public void testRuntimeAttributesEqual () throws Exception { 55 Class clazz1 = Sample.class; 56 Class clazz2 = RuntimeSample.class; 57 58 collectionsEquals (Attributes.getAttributes (clazz1), Attributes.getAttributes (clazz2)); 59 60 Method [] methods1 = clazz1.getDeclaredMethods (); 61 62 for (int i = 0; i < methods1.length; i++) { 63 Method m1 = methods1[i]; 64 Method m2 = clazz2.getDeclaredMethod (m1.getName (), m1.getParameterTypes ()); 65 66 collectionsEquals (Attributes.getAttributes (m1), Attributes.getAttributes (m2)); 67 68 int numParameters = m1.getParameterTypes().length; 69 for (int j = 0; j < numParameters; j++) { 70 collectionsEquals (Attributes.getParameterAttributes (m1, j), Attributes.getParameterAttributes (m2, j)); 71 } 72 73 collectionsEquals (Attributes.getReturnAttributes (m1), Attributes.getReturnAttributes (m2)); 74 } 75 76 Constructor [] ctors1 = clazz1.getDeclaredConstructors (); 77 for (int i = 0; i < ctors1.length; i++) { 78 Constructor c1 = ctors1[i]; 79 Constructor c2 = clazz2.getDeclaredConstructor (c1.getParameterTypes ()); 80 81 collectionsEquals (Attributes.getAttributes (c1), Attributes.getAttributes (c2)); 82 83 int numParameters = c1.getParameterTypes().length; 84 for (int j = 0; j < numParameters; j++) { 85 collectionsEquals (Attributes.getParameterAttributes (c1, j), Attributes.getParameterAttributes (c2, j)); 86 } 87 } 88 89 Field [] fields1 = clazz1.getDeclaredFields (); 90 for (int i = 0; i < fields1.length; i++) { 91 Field f1 = fields1[i]; 92 Field f2 = clazz2.getField (f1.getName ()); 93 collectionsEquals (Attributes.getAttributes (f1), Attributes.getAttributes (f2)); 94 } 95 } 96 } | Popular Tags |