• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  
test_annotationType()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  
test_equals()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  
test_hashCode()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  
test35304()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  
test35304_method(String s1, @Deprecated String s2)139      private static String test35304_method(String s1, @Deprecated String s2) { return null; }
140  }
141  
142  class AnnotatedClass2 {
143      @TestAnnotation3()
a()144      void a() {}
145      @TestAnnotation3b()
b()146      void b() {}
147  }
148  
149  class AnnotatedClass3 {
150      @TestAnnotation4(i = 12345)
a()151      void a() {}
152  }
153  
154  @TestAnnotation1("foobar")
155  class AnnotatedClass {
156  
157      // ----- boolean -----
158      @TestAnnotation3(z = false)
e01a()159      void e01a() {}
160      @TestAnnotation3(z = false)
e01b()161      void e01b() {}
162      @TestAnnotation3()
e01c()163      void e01c() {}
164  
165      @TestAnnotation3(z = true)
e02a()166      void e02a() {}
167      @TestAnnotation3(z = true)
e02b()168      void e02b() {}
169  
170      @TestAnnotation3(z = false)
n03a()171      void n03a() {}
172      @TestAnnotation3(z = true)
n03b()173      void n03b() {}
174  
175  
176      // ----- byte -----
177      @TestAnnotation3(b = 0)
e04a()178      void e04a() {}
179      @TestAnnotation3(b = 0)
e04b()180      void e04b() {}
181      @TestAnnotation3()
e04c()182      void e04c() {}
183  
184      @TestAnnotation3(b= 127)
e05a()185      void e05a() {}
186      @TestAnnotation3(b = 127)
e05b()187      void e05b() {}
188  
189      @TestAnnotation3(b = -128)
n06a()190      void n06a() {}
191      @TestAnnotation3(b = 127)
n06b()192      void n06b() {}
193  
194  
195      // ----- short -----
196      @TestAnnotation3(s = 0)
e07a()197      void e07a() {}
198      @TestAnnotation3(s = 0)
e07b()199      void e07b() {}
200      @TestAnnotation3()
e07c()201      void e07c() {}
202  
203      @TestAnnotation3(s= 32767)
e08a()204      void e08a() {}
205      @TestAnnotation3(s = 32767)
e08b()206      void e08b() {}
207  
208      @TestAnnotation3(s = -32768)
n09a()209      void n09a() {}
210      @TestAnnotation3(s = 32767)
n09b()211      void n09b() {}
212  
213  
214      // ----- int -----
215      @TestAnnotation3(i = 100)
e10a()216      void e10a() {}
217      @TestAnnotation3(i = 100)
e10b()218      void e10b() {}
219      @TestAnnotation3()
e10c()220      void e10c() {}
221  
222      @TestAnnotation3(i = Integer.MAX_VALUE)
e11a()223      void e11a() {}
224      @TestAnnotation3(i = Integer.MAX_VALUE)
e11b()225      void e11b() {}
226  
227      @TestAnnotation3(i = Integer.MAX_VALUE)
n12a()228      void n12a() {}
229      @TestAnnotation3(i = Integer.MIN_VALUE)
n12b()230      void n12b() {}
231  
232  
233      // ----- long -----
234      @TestAnnotation3(j = 0)
e13a()235      void e13a() {}
236      @TestAnnotation3(j = 0)
e13b()237      void e13b() {}
238      @TestAnnotation3()
e13c()239      void e13c() {}
240  
241      @TestAnnotation3(j = Long.MAX_VALUE)
e14a()242      void e14a() {}
243      @TestAnnotation3(j = Long.MAX_VALUE)
e14b()244      void e14b() {}
245  
246      @TestAnnotation3(j = Long.MAX_VALUE)
n15a()247      void n15a() {}
248      @TestAnnotation3(j = Long.MIN_VALUE)
n15b()249      void n15b() {}
250  
251  
252      // ----- float -----
253      @TestAnnotation3(f = 0.0f)
e16a()254      void e16a() {}
255      @TestAnnotation3(f = 0.0f)
e16b()256      void e16b() {}
257      @TestAnnotation3()
e16c()258      void e16c() {}
259  
260      @TestAnnotation3(f = Float.MAX_VALUE)
e17a()261      void e17a() {}
262      @TestAnnotation3(f = Float.MAX_VALUE)
e17b()263      void e17b() {}
264  
265      @TestAnnotation3(f = Float.NaN)
e18a()266      void e18a() {}
267      @TestAnnotation3(f = Float.NaN)
e18b()268      void e18b() {}
269  
270      @TestAnnotation3(f = Long.MAX_VALUE)
n19a()271      void n19a() {}
272      @TestAnnotation3(f = Long.MIN_VALUE)
n19b()273      void n19b() {}
274  
275      @TestAnnotation3(f = 0.0f)
n20a()276      void n20a() {}
277      @TestAnnotation3(f = -0.0f)
n20b()278      void n20b() {}
279  
280  
281      // ----- double -----
282      @TestAnnotation3(d = 0.0d)
e21a()283      void e21a() {}
284      @TestAnnotation3(d = 0.0d)
e21b()285      void e21b() {}
286      @TestAnnotation3()
e21c()287      void e21c() {}
288  
289      @TestAnnotation3(d = Double.MAX_VALUE)
e22a()290      void e22a() {}
291      @TestAnnotation3(d = Double.MAX_VALUE)
e22b()292      void e22b() {}
293  
294      @TestAnnotation3(d = Double.NaN)
e23a()295      void e23a() {}
296      @TestAnnotation3(d = Double.NaN)
e23b()297      void e23b() {}
298  
299  
300      @TestAnnotation3(d = Double.MAX_VALUE)
n24a()301      void n24a() {}
302      @TestAnnotation3(d = Double.MIN_VALUE)
n24b()303      void n24b() {}
304  
305      @TestAnnotation3(d = 0.0d)
n25a()306      void n25a() {}
307      @TestAnnotation3(d = -0.0d)
n25b()308      void n25b() {}
309  
310  
311   // ----- String -----
312      @TestAnnotation3(aString = "")
e26a()313      void e26a() {}
314      @TestAnnotation3(aString = "")
e26b()315      void e26b() {}
316      @TestAnnotation3()
e26c()317      void e26c() {}
318  
319      @TestAnnotation3(aString = "asjdfk jkls dfjklsd fklsd jklds kflds jfkldsfjd"+"d")
e27a()320      void e27a() {}
321      @TestAnnotation3(aString = "asjdfk jkls dfjklsd fklsd jklds kflds jfkldsfj"+"dd")
e27b()322      void e27b() {}
323  
324      @TestAnnotation3(aString = "a")
n28a()325      void n28a() {}
326      @TestAnnotation3(aString = "b")
n28b()327      void n28b() {}
328  
329  
330      // ----- Class-----
331      @TestAnnotation3(aClazz = Void.class)
e29a()332      void e29a() {}
333      @TestAnnotation3(aClazz = Void.class)
e29b()334      void e29b() {}
335      @TestAnnotation3()
e29c()336      void e29c() {}
337  
338      @TestAnnotation3(aClazz = Integer.class)
n30a()339      void n30a() {}
340      @TestAnnotation3(aClazz = int.class)
n30b()341      void n30b() {}
342  
343  
344      // ----- Enum-----
345      @TestAnnotation3(aEnum = TestEnum1.F)
e31a()346      void e31a() {}
347      @TestAnnotation3(aEnum = TestEnum1.F)
e31b()348      void e31b() {}
349      @TestAnnotation3()
e31c()350      void e31c() {}
351  
352      @TestAnnotation3(aEnum = TestEnum1.F)
n32a()353      void n32a() {}
354      @TestAnnotation3(aEnum = TestEnum1.A)
n32b()355      void n32b() {}
356  
357      @TestAnnotation3(aEnum = TestEnum1.F)
n33a()358      void n33a() {}
359      @TestAnnotation3(aEnum = TestEnum1.L)
n33b()360      void n33b() {}
361  
362  
363      // ----- String arr-----
364      @TestAnnotation2(arrString = {})
e34a()365      void e34a() {}
366      @TestAnnotation2(arrString = {})
e34b()367      void e34b() {}
368      @TestAnnotation2(arrString = {})
e34c()369      void e34c() {}
370  
371      @TestAnnotation2(arrString = { "a", "b"})
e35a()372      void e35a() {}
373      @TestAnnotation2(arrString = { "a", "b" })
e35b()374      void e35b() {}
375  
376      @TestAnnotation2(arrString = { "a", "b"})
n36a()377      void n36a() {}
378      @TestAnnotation2(arrString = { "a", "c" })
n36b()379      void n36b() {}
380  
381  
382      // ----- Class arr-----
383      @TestAnnotation2(arrClass= {})
e37a()384      void e37a() {}
385      @TestAnnotation2(arrClass = {})
e37b()386      void e37b() {}
387      @TestAnnotation2(arrClass = {})
e37c()388      void e37c() {}
389  
390      @TestAnnotation2(arrClass = { Void.class, Integer.class})
e38a()391      void e38a() {}
392      @TestAnnotation2(arrClass = { Void.class, Integer.class})
e38b()393      void e38b() {}
394  
395      @TestAnnotation2(arrClass = { Void.class, Integer.class})
n39a()396      void n39a() {}
397      @TestAnnotation2(arrClass = { Void.class, int.class})
n39b()398      void n39b() {}
399  
400      // ----- Enum arr-----
401      @TestAnnotation2(arrEnum= {})
e40a()402      void e40a() {}
403      @TestAnnotation2(arrEnum = {})
e40b()404      void e40b() {}
405      @TestAnnotation2(arrEnum = {})
e40c()406      void e40c() {}
407  
408      @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F })
e41a()409      void e41a() {}
410      @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F })
e41b()411      void e41b() {}
412  
413      @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.F })
n42a()414      void n42a() {}
415      @TestAnnotation2(arrEnum = { TestEnum1.A, TestEnum1.L })
n42b()416      void n42b() {}
417  
418  
419      // ----- Annotation arr-----
420      @TestAnnotation2(arrAnno= {})
e43a()421      void e43a() {}
422      @TestAnnotation2(arrAnno = {})
e43b()423      void e43b() {}
424      @TestAnnotation2()
e43c()425      void e43c() {}
426  
427      @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
e44a()428      void e44a() {}
429      @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
e44b()430      void e44b() {}
431  
432      @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
n45a()433      void n45a() {}
434      @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 124)})
n45b()435      void n45b() {}
436  
437      @TestAnnotation2(arrAnno = { @TestAnnotation3(i = 20), @TestAnnotation3(j = 123)})
n46a()438      void n46a() {}
439      @TestAnnotation2(arrAnno = { @TestAnnotation3(i = -20), @TestAnnotation3(j = 123)})
n46b()440      void n46b() {}
441  
442  }
443