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