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 AttributesTestCase extends TestCase { 31 32 public void testClassAttributes () throws Exception { 33 34 37 Class c = SuperSample.class; 38 assertEquals (1, Attributes.getAttributes (c).size ()); 39 assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ()); 40 assertTrue (Attributes.hasAttributeType (c, Dependency.class)); 41 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "super-sample" ))); 42 } 43 44 public void testMethodAttributes () throws Exception { 45 49 Method m = SuperSample.class.getMethod ("someMethod", new Class []{ Integer.TYPE }); 50 assertEquals (2, Attributes.getAttributes (m).size ()); 51 assertEquals (1, Attributes.getAttributes (m, Dependency.class).size ()); 52 assertEquals (1, Attributes.getAttributes (m, ThreadSafe.class).size ()); 53 assertTrue (Attributes.hasAttributeType (m, Dependency.class)); 54 assertTrue (Attributes.hasAttributeType (m, ThreadSafe.class)); 55 assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "super-some-method-sample" ))); 56 assertTrue (Attributes.hasAttribute (m, new ThreadSafe ())); 57 } 58 59 public void testFieldAttributes () throws Exception { 60 64 Field f = SuperSample.class.getField ("field"); 65 assertEquals (2, Attributes.getAttributes (f).size ()); 66 assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ()); 67 assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ()); 68 assertTrue (Attributes.hasAttribute (f, new ThreadSafe ())); 69 assertTrue (Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-field" ) )); 70 assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class)); 71 assertTrue (Attributes.hasAttributeType (f, Dependency.class)); 72 } 73 74 public void testDefaultConstructorAttributes () throws Exception { 75 78 Constructor c = SuperSample.class.getDeclaredConstructor (new Class [0]); 79 assertEquals (1, Attributes.getAttributes (c).size ()); 80 assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ()); 81 assertTrue (Attributes.hasAttributeType (c, Dependency.class)); 82 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-ctor1" ))); 83 } 84 85 public void testConstructorAttributes () throws Exception { 86 89 Constructor c = SuperSample.class.getDeclaredConstructor (new Class []{ String .class, (new String [0][0]).getClass () } ); 90 assertEquals (1, Attributes.getAttributes (c).size ()); 91 assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ()); 92 assertTrue (Attributes.hasAttributeType (c, Dependency.class)); 93 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-ctor2" ))); 94 95 assertEquals (1, Attributes.getParameterAttributes (c, 1).size ()); 96 assertEquals (1, Attributes.getParameterAttributes (c, 1, ThreadSafe.class).size ()); 97 assertTrue (Attributes.hasParameterAttributeType (c, 1, ThreadSafe.class)); 98 assertTrue (Attributes.hasParameterAttribute (c, 1, new ThreadSafe ())); 99 } 100 101 public void testClassInheritance () throws Exception { 102 Class c = Sample.class; 103 assertEquals (5, Attributes.getAttributes (c).size ()); 104 assertEquals (4, Attributes.getAttributes (c, Dependency.class).size ()); 105 assertTrue (Attributes.hasAttributeType (c, Dependency.class)); 106 assertTrue (Attributes.hasAttributeType (c, ThreadSafe.class)); 107 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample" ))); 108 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "super-sample" ))); 109 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-if-1-c" ))); 110 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-if-2-c" ))); 111 assertTrue (Attributes.hasAttribute (c, new ThreadSafe ())); 112 } 113 114 public void testMethodInheritance () throws Exception { 115 Method m = Sample.class.getMethod ("someMethod", new Class []{ Integer.TYPE }); 116 assertEquals (4, Attributes.getAttributes (m).size ()); 117 assertEquals (4, Attributes.getAttributes (m, Dependency.class).size ()); 118 assertTrue (Attributes.hasAttributeType (m, Dependency.class)); 119 assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "super-some-method-sample" ))); 120 assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-some-method2" ) )); 121 assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-if-1" ) )); 122 assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-if-2" ) )); 123 } 124 125 public void testPrivateMethodNonInheritance () throws Exception { 126 Method m = Sample.class.getDeclaredMethod ("privateMethod", new Class []{}); 127 assertEquals (1, Attributes.getAttributes (m).size ()); 128 assertEquals (1, Attributes.getAttributes (m, Dependency.class).size ()); 129 assertTrue (Attributes.hasAttributeType (m, Dependency.class)); 130 assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-privateMethod" ) )); 131 132 m = SuperSample.class.getDeclaredMethod ("privateMethod", new Class []{}); 133 assertEquals (1, Attributes.getAttributes (m).size ()); 134 assertEquals (1, Attributes.getAttributes (m, Dependency.class).size ()); 135 assertTrue (Attributes.hasAttributeType (m, Dependency.class)); 136 assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "super-privateMethod" ) )); 137 } 138 139 public void testParameterAndReturnAttributes () throws Exception { 140 Method m = Sample.class.getMethod ("methodWithAttributes", new Class []{ Integer.TYPE, Integer.TYPE }); 141 assertEquals (0, Attributes.getAttributes (m).size ()); 142 assertEquals (2, Attributes.getReturnAttributes (m).size ()); 143 assertTrue (Attributes.hasReturnAttribute (m, new Dependency ( SampleService.class, "sample-return" ) )); 144 assertTrue (Attributes.hasReturnAttribute (m, new Dependency ( SampleService.class, "sample-if-return" ) )); 145 146 assertEquals (0, Attributes.getParameterAttributes (m, 0).size ()); 147 assertEquals (2, Attributes.getParameterAttributes (m, 1).size ()); 148 assertTrue (Attributes.hasParameterAttribute (m, 1, new Dependency ( SampleService.class, "sample-if-param-2" ) )); 149 assertTrue (Attributes.hasParameterAttribute (m, 1, new ThreadSafe () )); 150 } 151 152 public void testFieldNonInheritance () throws Exception { 153 Field f = SuperSample.class.getField ("field"); 154 assertEquals (2, Attributes.getAttributes (f).size ()); 155 assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ()); 156 assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ()); 157 assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class)); 158 assertTrue (Attributes.hasAttributeType (f, Dependency.class)); 159 assertTrue (Attributes.hasAttribute (f, new ThreadSafe ())); 160 assertTrue (Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-field" ))); 161 162 f = Sample.class.getField ("field"); 163 assertEquals (1, Attributes.getAttributes (f).size ()); 164 assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ()); 165 assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class)); 166 assertTrue (Attributes.hasAttribute (f, new ThreadSafe ())); 167 168 f = SuperSample.class.getField ("noAttributesInSubClass"); 169 assertEquals (1, Attributes.getAttributes (f).size ()); 170 assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ()); 171 assertTrue (Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-noattrs" ))); 172 assertTrue (Attributes.hasAttributeType (f, Dependency.class)); 173 174 f = Sample.class.getField ("noAttributesInSubClass"); 175 assertEquals (0, Attributes.getAttributes (f).size ()); 176 assertEquals (0, Attributes.getAttributes (f, Dependency.class).size ()); 177 assertTrue (!Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-noattrs" ))); 178 assertTrue (!Attributes.hasAttributeType (f, Dependency.class)); 179 } 180 181 public void testNoAttributes () throws Exception { 182 Method m = Sample.class.getMethod ("methodWithNoAttributes", new Class [0]); 183 assertEquals (0, Attributes.getAttributes (m).size ()); 184 } 185 186 190 public void testClassLoaderKeying () throws Exception { 191 URLClassLoader cl1 = new URLClassLoader (new URL []{new File ("target/cl1/").toURL ()}, getClass().getClassLoader ()); 192 URLClassLoader cl2 = new URLClassLoader (new URL []{new File ("target/cl2/").toURL ()}, getClass().getClassLoader ()); 193 194 Class cl1Class = cl1.loadClass ("TestClass"); 195 Class cl2Class = cl2.loadClass ("TestClass"); 196 197 assertEquals ("[[TestAttribute 1]]", Attributes.getAttributes (cl1Class).toString ()); 198 assertEquals ("[[TestAttribute TestClass]]", Attributes.getAttributes (cl2Class).toString ()); 199 } 200 201 public void testInnerClasses () throws Exception { 202 Class c = Sample.InnerSample.class; 203 assertEquals (1, Attributes.getAttributes (c).size ()); 204 assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ()); 205 assertTrue (Attributes.hasAttributeType (c, Dependency.class)); 206 assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "inner-sample" ))); 207 } 208 209 public void testNamedParameters () throws Exception { 210 Method m = Sample.class.getMethod ("methodWithNamedParameters", new Class []{ }); 211 assertEquals (1, Attributes.getAttributes (m).size ()); 212 assertEquals (1, Attributes.getAttributes (m, BeanAttribute.class).size ()); 213 assertTrue (Attributes.hasAttributeType (m, BeanAttribute.class)); 214 BeanAttribute ba = new BeanAttribute (1, "a"); 215 ba.setName ("Smith, John \"Agent\""); 216 ba.setAnotherNumber (42); 217 assertTrue (Attributes.hasAttribute (m, ba)); 218 } 219 } | Popular Tags |