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 AttributeIndexTestCase extends TestCase { 31 32 private URLClassLoader cl = null; 33 private AttributeIndex index = null; 34 private Class TESTCLASS = null; 35 private Class TESTCLASS_INNER = null; 36 private Class TESTATTRIBUTE = null; 37 38 public void setUp () throws Exception { 39 cl = new URLClassLoader (new URL []{new File ("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 { 47 Collection 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 { 55 Collection 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 { 63 Collection 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 { 70 Collection 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 { 77 Collection 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 { 84 Collection 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 { 91 Collection 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 { 98 Collection 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 |