KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > test > RuntimeAttributesTestCase


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes.test;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.io.File JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
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 JavaDoc a, Collection JavaDoc b) {
33         if (a.size () != b.size ()) {
34             fail ("A=" + a + " B=" + b);
35         }
36         
37         Iterator JavaDoc iter = a.iterator ();
38         while (iter.hasNext ()) {
39             Object JavaDoc 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 JavaDoc 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 JavaDoc {
55         Class JavaDoc clazz1 = Sample.class;
56         Class JavaDoc clazz2 = RuntimeSample.class;
57         
58         collectionsEquals (Attributes.getAttributes (clazz1), Attributes.getAttributes (clazz2));
59         
60         Method JavaDoc[] methods1 = clazz1.getDeclaredMethods ();
61         
62         for (int i = 0; i < methods1.length; i++) {
63             Method JavaDoc m1 = methods1[i];
64             Method JavaDoc 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 JavaDoc[] ctors1 = clazz1.getDeclaredConstructors ();
77         for (int i = 0; i < ctors1.length; i++) {
78             Constructor JavaDoc c1 = ctors1[i];
79             Constructor JavaDoc 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 JavaDoc[] fields1 = clazz1.getDeclaredFields ();
90         for (int i = 0; i < fields1.length; i++) {
91             Field JavaDoc f1 = fields1[i];
92             Field JavaDoc f2 = clazz2.getField (f1.getName ());
93             collectionsEquals (Attributes.getAttributes (f1), Attributes.getAttributes (f2));
94         }
95     }
96 }
Popular Tags