leosutic 2003/12/28 12:40:53
Modified: attributes/unittest/src/test/org/apache/commons/attributes/test
AttributesTestCase.java
Log:
Added ability to programmatically define attributes
for a class.
Revision Changes Path
1.5 +66 -0
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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- AttributesTestCase.java 29 Sep 2003 21:17:08 -0000 1.4
+++ AttributesTestCase.java 28 Dec 2003 20:40:53 -0000 1.5
@@ -7,6 +7,7 @@
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;
@@ -151,6 +152,71 @@
public void testNoAttributes () throws Exception {
Method m = Sample.class.getMethod ("methodWithNoAttributes", new Class[0]);
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));
+ }
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]