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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.harmony.annotation.tests.java.lang.annotation;
20 import junit.framework.TestCase;
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;
31 * Test case of java.lang.annotation.Annotation
33 public class AnnotationTest extends TestCase {
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());
42 public void test_equals() throws Exception {
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]));
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);
63 mlist = new ArrayList<Method>();
64 curT.put(testNum, mlist);
66 mlist.add(AnnotatedClass.class.getDeclaredMethod(name, new Class[] {}));
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());
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(),
93 if (a1.hashCode() != a2.hashCode()) {
94 assertFalse("not same hashcode -> not equals", a1.equals(a2));
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);
107 Method m1 = AnnotatedClass.class.getDeclaredMethod("e34c", new Class[] {});
108 int arrHc = Arrays.hashCode(new Object[]{});
110 TestAnnotation3[] arrAnno() default {};
111 String[] arrString() default {};
112 Class[] arrClass() default {};
113 TestEnum1[] arrEnum() default {};
116 (127 * "arrAnno".hashCode() ^ arrHc) +
117 (127 * "arrString".hashCode() ^ arrHc)+
118 (127 * "arrClass".hashCode() ^ arrHc) +
119 (127 * "arrEnum".hashCode() ^ arrHc)
121 m1.getDeclaredAnnotations()[0].hashCode());
123 Method m2 = AnnotatedClass3.class.getDeclaredMethod("a", new Class[] {});
125 (127 * "i".hashCode() ^ 12345),
126 m2.getDeclaredAnnotations()[0].hashCode());
131 class AnnotatedClass2 {
138 class AnnotatedClass3 {
139 @TestAnnotation4(i = 12345)
143 @TestAnnotation1("foobar")
144 class AnnotatedClass {
146 // ----- boolean -----
147 @TestAnnotation3(z = false)
149 @TestAnnotation3(z = false)
154 @TestAnnotation3(z = true)
156 @TestAnnotation3(z = true)
159 @TestAnnotation3(z = false)
161 @TestAnnotation3(z = true)
166 @TestAnnotation3(b = 0)
168 @TestAnnotation3(b = 0)
173 @TestAnnotation3(b= 127)
175 @TestAnnotation3(b = 127)
178 @TestAnnotation3(b = -128)
180 @TestAnnotation3(b = 127)
185 @TestAnnotation3(s = 0)
187 @TestAnnotation3(s = 0)
192 @TestAnnotation3(s= 32767)
194 @TestAnnotation3(s = 32767)
197 @TestAnnotation3(s = -32768)
199 @TestAnnotation3(s = 32767)
204 @TestAnnotation3(i = 100)
206 @TestAnnotation3(i = 100)
211 @TestAnnotation3(i = Integer.MAX_VALUE)
213 @TestAnnotation3(i = Integer.MAX_VALUE)
216 @TestAnnotation3(i = Integer.MAX_VALUE)
218 @TestAnnotation3(i = Integer.MIN_VALUE)
223 @TestAnnotation3(j = 0)
225 @TestAnnotation3(j = 0)
230 @TestAnnotation3(j = Long.MAX_VALUE)
232 @TestAnnotation3(j = Long.MAX_VALUE)
235 @TestAnnotation3(j = Long.MAX_VALUE)
237 @TestAnnotation3(j = Long.MIN_VALUE)
242 @TestAnnotation3(f = 0.0f)
244 @TestAnnotation3(f = 0.0f)
249 @TestAnnotation3(f = Float.MAX_VALUE)
251 @TestAnnotation3(f = Float.MAX_VALUE)
254 @TestAnnotation3(f = Float.NaN)
256 @TestAnnotation3(f = Float.NaN)
259 @TestAnnotation3(f = Long.MAX_VALUE)
261 @TestAnnotation3(f = Long.MIN_VALUE)
264 @TestAnnotation3(f = 0.0f)
266 @TestAnnotation3(f = -0.0f)
270 // ----- double -----
271 @TestAnnotation3(d = 0.0d)
273 @TestAnnotation3(d = 0.0d)
278 @TestAnnotation3(d = Double.MAX_VALUE)
280 @TestAnnotation3(d = Double.MAX_VALUE)
283 @TestAnnotation3(d = Double.NaN)
285 @TestAnnotation3(d = Double.NaN)
289 @TestAnnotation3(d = Double.MAX_VALUE)
291 @TestAnnotation3(d = Double.MIN_VALUE)
294 @TestAnnotation3(d = 0.0d)
296 @TestAnnotation3(d = -0.0d)
300 // ----- String -----
301 @TestAnnotation3(aString = "")
303 @TestAnnotation3(aString = "")
308 @TestAnnotation3(aString = "asjdfk jkls dfjklsd fklsd jklds kflds jfkldsfjd"+"d")
310 @TestAnnotation3(aString = "asjdfk jkls dfjklsd fklsd jklds kflds jfkldsfj"+"dd")
313 @TestAnnotation3(aString = "a")
315 @TestAnnotation3(aString = "b")
320 @TestAnnotation3(aClazz = Void.class)
322 @TestAnnotation3(aClazz = Void.class)
327 @TestAnnotation3(aClazz = Integer.class)
329 @TestAnnotation3(aClazz = int.class)
334 @TestAnnotation3(aEnum = TestEnum1.F)
336 @TestAnnotation3(aEnum = TestEnum1.F)
341 @TestAnnotation3(aEnum = TestEnum1.F)
343 @TestAnnotation3(aEnum = TestEnum1.A)
346 @TestAnnotation3(aEnum = TestEnum1.F)
348 @TestAnnotation3(aEnum = TestEnum1.L)
352 // ----- String arr-----
353 @TestAnnotation2(arrString = {})
355 @TestAnnotation2(arrString = {})
357 @TestAnnotation2(arrString = {})
360 @TestAnnotation2(arrString = { "a", "b"})
362 @TestAnnotation2(arrString = { "a", "b" })
365 @TestAnnotation2(arrString = { "a", "b"})
367 @TestAnnotation2(arrString = { "a", "c" })
371 // ----- Class arr-----
372 @TestAnnotation2(arrClass= {})
374 @TestAnnotation2(arrClass = {})
376 @TestAnnotation2(arrClass = {})
379 @TestAnnotation2(arrClass = { Void.class, Integer.class})
381 @TestAnnotation2(arrClass = { Void.class, Integer.class})
384 @TestAnnotation2(arrClass = { Void.class, Integer.class})
386 @TestAnnotation2(arrClass = { Void.class, int.class})
389 // ----- Enum arr-----
390 @TestAnnotation2(arrEnum= {})
392 @TestAnnotation2(arrEnum = {})
394 @TestAnnotation2(arrEnum = {})
397 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F })
399 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F })
402 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F })
404 @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.L })
408 // ----- Annotation arr-----
409 @TestAnnotation2(arrAnno= {})
411 @TestAnnotation2(arrAnno = {})
416 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
418 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
421 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
423 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 124)})
426 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
428 @TestAnnotation2(arrAnno = { @TestAnnotation3(i = -20), @TestAnnotation3(j = 123)})