1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.annotation.tests.java.lang.annotation; 19 20 import junit.framework.TestCase; 21 22 import java.lang.annotation.Annotation; 23 import java.lang.reflect.Method; 24 import java.util.ArrayList; 25 import java.util.Arrays; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * Test case of java.lang.annotation.Annotation 32 */ 33 public class AnnotationTest extends TestCase { 34 35 public void test_annotationType() { 36 Annotation [] annotations = AnnotatedClass.class.getDeclaredAnnotations(); 37 assertEquals(1, annotations.length); 38 Annotation anno = annotations[0]; 39 assertEquals(TestAnnotation1.class, anno.annotationType()); 40 } 41 42 public void test_equals() throws Exception { 43 // test type 44 Method m1 = AnnotatedClass2.class 45 .getDeclaredMethod("a", new Class[] {}); 46 Method m2 = AnnotatedClass2.class 47 .getDeclaredMethod("b", new Class[] {}); 48 assertFalse("other annotation class type", 49 m1.getDeclaredAnnotations()[0].equals(m2 50 .getDeclaredAnnotations()[0])); 51 52 // test equality / non equality for base types and compound types 53 List<Method> methods = Arrays.asList(AnnotatedClass.class.getDeclaredMethods()); 54 Map<String, List<Method>> eqs = new HashMap<String, List<Method>>(); 55 Map<String, List<Method>> neqs = new HashMap<String, List<Method>>(); 56 for (Method m : methods) { 57 String name = m.getName(); 58 //System.out.println("name "+name); 59 Map<String, List<Method>> curT = name.charAt(0) == 'e'? eqs : neqs; 60 String testNum = name.substring(1,3); // 01 61 List<Method> mlist = curT.get(testNum); 62 if (mlist == null) { 63 mlist = new ArrayList<Method>(); 64 curT.put(testNum, mlist); 65 } 66 mlist.add(AnnotatedClass.class.getDeclaredMethod(name, new Class[] {})); 67 } 68 69 for (List<Method> eqList : eqs.values()) { 70 for (int i = 0; i < eqList.size() -1; i++) { 71 for (int j = i+1; j < eqList.size(); j++) { 72 Method me1 = eqList.get(i); 73 Method me2 = eqList.get(j); 74 //System.out.println("eq test for "+me1.getName()+", "+me2.getName()); 75 Annotation a1 = me1.getDeclaredAnnotations()[0]; 76 Annotation a2 = me2.getDeclaredAnnotations()[0]; 77 assertEquals("must be equal : method1:"+me1.getName()+", method2: "+me2.getName(), a1, a2); 78 assertEquals("same hashcode", a1.hashCode(), a2.hashCode()); 79 } 80 } 81 } 82 83 for (List<Method> eqList : neqs.values()) { 84 for (int i = 0; i < eqList.size() -1; i++) { 85 for (int j = i+1; j < eqList.size(); j++) { 86 Method me1 = eqList.get(i); 87 Method me2 = eqList.get(j); 88 Annotation a1 = me1.getDeclaredAnnotations()[0]; 89 Annotation a2 = me2.getDeclaredAnnotations()[0]; 90 //System.out.println("ne test for "+me1.getName()+", "+me2.getName()); 91 assertFalse("must not be equal : method1:"+me1.getName()+", method2: "+me2.getName(), 92 a1.equals(a2)); 93 if (a1.hashCode() != a2.hashCode()) { 94 assertFalse("not same hashcode -> not equals", a1.equals(a2)); 95 } 96 } 97 98 } 99 } 100 } 101 102 public void test_hashCode() throws SecurityException, NoSuchMethodException { 103 Annotation a1 = AnnotatedClass.class.getDeclaredAnnotations()[0]; 104 assertEquals(a1.hashCode(), (127 * "value".hashCode() ^ "foobar".hashCode())); 105 // i+= 127 *(key.hashCode() ^ memberValHashCode(value); 106 107 Method m1 = AnnotatedClass.class.getDeclaredMethod("e34c", new Class[] {}); 108 int arrHc = Arrays.hashCode(new Object[]{}); 109 /* 110 TestAnnotation3[] arrAnno() default {}; 111 String[] arrString() default {}; 112 Class[] arrClass() default {}; 113 TestEnum1[] arrEnum() default {}; 114 */ 115 assertEquals( 116 (127 * "arrAnno".hashCode() ^ arrHc) + 117 (127 * "arrString".hashCode() ^ arrHc)+ 118 (127 * "arrClass".hashCode() ^ arrHc) + 119 (127 * "arrEnum".hashCode() ^ arrHc) 120 , 121 m1.getDeclaredAnnotations()[0].hashCode()); 122 123 Method m2 = AnnotatedClass3.class.getDeclaredMethod("a", new Class[] {}); 124 assertEquals( 125 (127 * "i".hashCode() ^ 12345), 126 m2.getDeclaredAnnotations()[0].hashCode()); 127 128 } 129 130 public static void test35304() throws Exception { 131 Class c = AnnotationTest.class; 132 Class[] parameterTypes = new Class[] { String.class, String.class }; 133 Annotation[][] annotations = c.getDeclaredMethod("test35304_method", parameterTypes).getParameterAnnotations(); 134 assertEquals(2, annotations.length); // Two parameters. 135 assertEquals(0, annotations[0].length); // No annotations on the first. 136 assertEquals(1, annotations[1].length); // One annotation on the second. 137 } 138 139 private static String test35304_method(String s1, @Deprecated String s2) { return null; } 140 } 141 142 class AnnotatedClass2 { 143 @TestAnnotation3() 144 void a() {} 145 @TestAnnotation3b() 146 void b() {} 147 } 148 149 class AnnotatedClass3 { 150 @TestAnnotation4(i = 12345) 151 void a() {} 152 } 153 154 @TestAnnotation1("foobar") 155 class AnnotatedClass { 156 157 // ----- boolean ----- 158 @TestAnnotation3(z = false) 159 void e01a() {} 160 @TestAnnotation3(z = false) 161 void e01b() {} 162 @TestAnnotation3() 163 void e01c() {} 164 165 @TestAnnotation3(z = true) 166 void e02a() {} 167 @TestAnnotation3(z = true) 168 void e02b() {} 169 170 @TestAnnotation3(z = false) 171 void n03a() {} 172 @TestAnnotation3(z = true) 173 void n03b() {} 174 175 176 // ----- byte ----- 177 @TestAnnotation3(b = 0) 178 void e04a() {} 179 @TestAnnotation3(b = 0) 180 void e04b() {} 181 @TestAnnotation3() 182 void e04c() {} 183 184 @TestAnnotation3(b= 127) 185 void e05a() {} 186 @TestAnnotation3(b = 127) 187 void e05b() {} 188 189 @TestAnnotation3(b = -128) 190 void n06a() {} 191 @TestAnnotation3(b = 127) 192 void n06b() {} 193 194 195 // ----- short ----- 196 @TestAnnotation3(s = 0) 197 void e07a() {} 198 @TestAnnotation3(s = 0) 199 void e07b() {} 200 @TestAnnotation3() 201 void e07c() {} 202 203 @TestAnnotation3(s= 32767) 204 void e08a() {} 205 @TestAnnotation3(s = 32767) 206 void e08b() {} 207 208 @TestAnnotation3(s = -32768) 209 void n09a() {} 210 @TestAnnotation3(s = 32767) 211 void n09b() {} 212 213 214 // ----- int ----- 215 @TestAnnotation3(i = 100) 216 void e10a() {} 217 @TestAnnotation3(i = 100) 218 void e10b() {} 219 @TestAnnotation3() 220 void e10c() {} 221 222 @TestAnnotation3(i = Integer.MAX_VALUE) 223 void e11a() {} 224 @TestAnnotation3(i = Integer.MAX_VALUE) 225 void e11b() {} 226 227 @TestAnnotation3(i = Integer.MAX_VALUE) 228 void n12a() {} 229 @TestAnnotation3(i = Integer.MIN_VALUE) 230 void n12b() {} 231 232 233 // ----- long ----- 234 @TestAnnotation3(j = 0) 235 void e13a() {} 236 @TestAnnotation3(j = 0) 237 void e13b() {} 238 @TestAnnotation3() 239 void e13c() {} 240 241 @TestAnnotation3(j = Long.MAX_VALUE) 242 void e14a() {} 243 @TestAnnotation3(j = Long.MAX_VALUE) 244 void e14b() {} 245 246 @TestAnnotation3(j = Long.MAX_VALUE) 247 void n15a() {} 248 @TestAnnotation3(j = Long.MIN_VALUE) 249 void n15b() {} 250 251 252 // ----- float ----- 253 @TestAnnotation3(f = 0.0f) 254 void e16a() {} 255 @TestAnnotation3(f = 0.0f) 256 void e16b() {} 257 @TestAnnotation3() 258 void e16c() {} 259 260 @TestAnnotation3(f = Float.MAX_VALUE) 261 void e17a() {} 262 @TestAnnotation3(f = Float.MAX_VALUE) 263 void e17b() {} 264 265 @TestAnnotation3(f = Float.NaN) 266 void e18a() {} 267 @TestAnnotation3(f = Float.NaN) 268 void e18b() {} 269 270 @TestAnnotation3(f = Long.MAX_VALUE) 271 void n19a() {} 272 @TestAnnotation3(f = Long.MIN_VALUE) 273 void n19b() {} 274 275 @TestAnnotation3(f = 0.0f) 276 void n20a() {} 277 @TestAnnotation3(f = -0.0f) 278 void n20b() {} 279 280 281 // ----- double ----- 282 @TestAnnotation3(d = 0.0d) 283 void e21a() {} 284 @TestAnnotation3(d = 0.0d) 285 void e21b() {} 286 @TestAnnotation3() 287 void e21c() {} 288 289 @TestAnnotation3(d = Double.MAX_VALUE) 290 void e22a() {} 291 @TestAnnotation3(d = Double.MAX_VALUE) 292 void e22b() {} 293 294 @TestAnnotation3(d = Double.NaN) 295 void e23a() {} 296 @TestAnnotation3(d = Double.NaN) 297 void e23b() {} 298 299 300 @TestAnnotation3(d = Double.MAX_VALUE) 301 void n24a() {} 302 @TestAnnotation3(d = Double.MIN_VALUE) 303 void n24b() {} 304 305 @TestAnnotation3(d = 0.0d) 306 void n25a() {} 307 @TestAnnotation3(d = -0.0d) 308 void n25b() {} 309 310 311 // ----- String ----- 312 @TestAnnotation3(aString = "") 313 void e26a() {} 314 @TestAnnotation3(aString = "") 315 void e26b() {} 316 @TestAnnotation3() 317 void e26c() {} 318 319 @TestAnnotation3(aString = "asjdfk jkls dfjklsd fklsd jklds kflds jfkldsfjd"+"d") 320 void e27a() {} 321 @TestAnnotation3(aString = "asjdfk jkls dfjklsd fklsd jklds kflds jfkldsfj"+"dd") 322 void e27b() {} 323 324 @TestAnnotation3(aString = "a") 325 void n28a() {} 326 @TestAnnotation3(aString = "b") 327 void n28b() {} 328 329 330 // ----- Class----- 331 @TestAnnotation3(aClazz = Void.class) 332 void e29a() {} 333 @TestAnnotation3(aClazz = Void.class) 334 void e29b() {} 335 @TestAnnotation3() 336 void e29c() {} 337 338 @TestAnnotation3(aClazz = Integer.class) 339 void n30a() {} 340 @TestAnnotation3(aClazz = int.class) 341 void n30b() {} 342 343 344 // ----- Enum----- 345 @TestAnnotation3(aEnum = TestEnum1.F) 346 void e31a() {} 347 @TestAnnotation3(aEnum = TestEnum1.F) 348 void e31b() {} 349 @TestAnnotation3() 350 void e31c() {} 351 352 @TestAnnotation3(aEnum = TestEnum1.F) 353 void n32a() {} 354 @TestAnnotation3(aEnum = TestEnum1.A) 355 void n32b() {} 356 357 @TestAnnotation3(aEnum = TestEnum1.F) 358 void n33a() {} 359 @TestAnnotation3(aEnum = TestEnum1.L) 360 void n33b() {} 361 362 363 // ----- String arr----- 364 @TestAnnotation2(arrString = {}) 365 void e34a() {} 366 @TestAnnotation2(arrString = {}) 367 void e34b() {} 368 @TestAnnotation2(arrString = {}) 369 void e34c() {} 370 371 @TestAnnotation2(arrString = { "a", "b"}) 372 void e35a() {} 373 @TestAnnotation2(arrString = { "a", "b" }) 374 void e35b() {} 375 376 @TestAnnotation2(arrString = { "a", "b"}) 377 void n36a() {} 378 @TestAnnotation2(arrString = { "a", "c" }) 379 void n36b() {} 380 381 382 // ----- Class arr----- 383 @TestAnnotation2(arrClass= {}) 384 void e37a() {} 385 @TestAnnotation2(arrClass = {}) 386 void e37b() {} 387 @TestAnnotation2(arrClass = {}) 388 void e37c() {} 389 390 @TestAnnotation2(arrClass = { Void.class, Integer.class}) 391 void e38a() {} 392 @TestAnnotation2(arrClass = { Void.class, Integer.class}) 393 void e38b() {} 394 395 @TestAnnotation2(arrClass = { Void.class, Integer.class}) 396 void n39a() {} 397 @TestAnnotation2(arrClass = { Void.class, int.class}) 398 void n39b() {} 399 400 // ----- Enum arr----- 401 @TestAnnotation2(arrEnum= {}) 402 void e40a() {} 403 @TestAnnotation2(arrEnum = {}) 404 void e40b() {} 405 @TestAnnotation2(arrEnum = {}) 406 void e40c() {} 407 408 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F }) 409 void e41a() {} 410 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F }) 411 void e41b() {} 412 413 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F }) 414 void n42a() {} 415 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.L }) 416 void n42b() {} 417 418 419 // ----- Annotation arr----- 420 @TestAnnotation2(arrAnno= {}) 421 void e43a() {} 422 @TestAnnotation2(arrAnno = {}) 423 void e43b() {} 424 @TestAnnotation2() 425 void e43c() {} 426 427 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)}) 428 void e44a() {} 429 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)}) 430 void e44b() {} 431 432 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)}) 433 void n45a() {} 434 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 124)}) 435 void n45b() {} 436 437 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)}) 438 void n46a() {} 439 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = -20), @TestAnnotation3(j = 123)}) 440 void n46b() {} 441 442 } 443