KickJava   Java API By Example, From Geeks To Geeks.

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


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 AttributeIndexTestCase extends TestCase {
31     
32     private URLClassLoader JavaDoc cl = null;
33     private AttributeIndex index = null;
34     private Class JavaDoc TESTCLASS = null;
35     private Class JavaDoc TESTCLASS_INNER = null;
36     private Class JavaDoc TESTATTRIBUTE = null;
37     
38     public void setUp () throws Exception JavaDoc {
39         cl = new URLClassLoader JavaDoc (new URL JavaDoc[]{new File JavaDoc ("target/cl2/cl2.jar").toURL ()}, getClass().getClassLoader ());
40         index = new AttributeIndex (cl);
41         TESTCLASS = cl.loadClass ("TestClass");
42         TESTCLASS_INNER = cl.loadClass ("TestClass$Inner");
43         TESTATTRIBUTE = cl.loadClass ("TestAttribute");
44     }
45     
46     public void testAttributeIndexCompatible () throws Exception JavaDoc {
47         Collection JavaDoc classes = index.getClassesWithAttribute ("TestAttribute");
48         System.out.println (classes);
49         assertEquals (2, classes.size ());
50         assertTrue (classes.contains ("TestClass"));
51         assertTrue (classes.contains ("TestClass.Inner"));
52     }
53     
54     public void testClasses () throws Exception JavaDoc {
55         Collection JavaDoc classes = index.getClasses (TESTATTRIBUTE);
56         System.out.println (classes);
57         assertEquals (2, classes.size ());
58         assertTrue (classes.contains (TESTCLASS));
59         assertTrue (classes.contains (TESTCLASS_INNER));
60     }
61     
62     public void testMethods () throws Exception JavaDoc {
63         Collection JavaDoc methods = index.getMethods (TESTATTRIBUTE);
64         System.out.println (methods);
65         assertEquals (1, methods.size ());
66         assertTrue (methods.contains (TESTCLASS.getDeclaredMethods()[0]));
67     }
68     
69     public void testConstructors () throws Exception JavaDoc {
70         Collection JavaDoc ctors = index.getConstructors (TESTATTRIBUTE);
71         System.out.println (ctors);
72         assertEquals (1, ctors.size ());
73         assertTrue (ctors.contains (TESTCLASS.getDeclaredConstructors()[0]));
74     }
75     
76     public void testConstructorParameters () throws Exception JavaDoc {
77         Collection JavaDoc ctors = index.getConstructorParameters (TESTATTRIBUTE);
78         System.out.println (ctors);
79         assertEquals (1, ctors.size ());
80         assertTrue (ctors.contains (new AttributeIndex.ConstructorParameter (TESTCLASS.getDeclaredConstructors()[0], 0)));
81     }
82     
83     public void testMethodParameters () throws Exception JavaDoc {
84         Collection JavaDoc methods = index.getMethodParameters (TESTATTRIBUTE);
85         System.out.println (methods);
86         assertEquals (1, methods.size ());
87         assertTrue (methods.contains (new AttributeIndex.MethodParameter (TESTCLASS.getDeclaredMethods()[0], 0)));
88     }
89     
90     public void testMethodsReturning () throws Exception JavaDoc {
91         Collection JavaDoc methods = index.getMethodsReturning (TESTATTRIBUTE);
92         System.out.println (methods);
93         assertEquals (1, methods.size ());
94         assertTrue (methods.contains (TESTCLASS.getDeclaredMethods()[0]));
95     }
96     
97     public void testFields () throws Exception JavaDoc {
98         Collection JavaDoc fields = index.getFields (TESTATTRIBUTE);
99         System.out.println (fields);
100         assertEquals (1, fields.size ());
101         assertTrue (fields.contains (TESTCLASS.getDeclaredFields()[0]));
102     }
103 }
Popular Tags