rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionAnnotationTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Mar 2016 09:05:43 +0100
changeset 1899 d729cfa77fa7
parent 1889 e1953d8b8338
child 1901 e9be21ce2291
permissions -rw-r--r--
Make sure getAnnotations on Class and Method returns some values
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    20 import java.lang.annotation.Annotation;
    21 import java.lang.annotation.Retention;
    22 import java.lang.annotation.RetentionPolicy;
    23 import org.apidesign.bck2brwsr.vmtest.Compare;
    24 import org.apidesign.bck2brwsr.vmtest.VMTest;
    25 import org.testng.annotations.Factory;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    30  */
    31 public class ReflectionAnnotationTest {
    32     @Retention(RetentionPolicy.RUNTIME)
    33     @interface Ann {
    34         int integer() default 33;
    35         double dbl() default 33.3;
    36         String string() default "Ahoj";
    37         E enums() default E.B;
    38         Class<?> type() default String.class;
    39     }
    40 
    41     @interface AnnAnn {
    42         Ann ann() default @Ann(
    43             dbl = 44.4,
    44             enums = E.A,
    45             string = "Hi",
    46             integer = 44
    47         );
    48     }
    49 
    50     @Ann
    51     @AnnAnn
    52     class D {
    53         @Ann
    54         @AnnAnn
    55         public void method() {
    56         }
    57     }
    58 
    59     @Ann(type = String.class)
    60     @AnnAnn(ann = @Ann(string = "Ciao"))
    61     enum E {
    62         A, B
    63     };
    64 
    65     
    66     @Compare public String annoClass() throws Exception {
    67         Retention r = Ann.class.getAnnotation(Retention.class);
    68         assert r != null : "Annotation is present";
    69         assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
    70         return r.annotationType().getName();
    71     }
    72 
    73     @Compare public boolean isAnnotation() {
    74         return Ann.class.isAnnotation();
    75     }
    76     @Compare public boolean isNotAnnotation() {
    77         return String.class.isAnnotation();
    78     }
    79     @Compare public boolean isNotAnnotationEnum() {
    80         return E.class.isAnnotation();
    81     }
    82 
    83     @Compare public int intDefaultAttrIsRead() {
    84         return D.class.getAnnotation(Ann.class).integer();
    85     }
    86 
    87     @Compare public double doubleDefaultAttrIsRead() {
    88         return D.class.getAnnotation(Ann.class).dbl();
    89     }
    90 
    91     @Compare public String stringDefaultAttrIsRead() {
    92         return D.class.getAnnotation(Ann.class).string();
    93     }
    94 
    95     @Compare public String enumDefaultAttrIsRead() {
    96         return D.class.getAnnotation(Ann.class).enums().toString();
    97     }
    98 
    99     @Compare public String classDefaultAttrIsRead() {
   100         return D.class.getAnnotation(Ann.class).type().getName();
   101     }
   102 
   103     @Compare public String classAttrIsRead() {
   104         return E.class.getAnnotation(Ann.class).type().getName();
   105     }
   106 
   107 //    @Compare public String defaultAnnotationAttrIsRead() {
   108 //        final Ann ann = D.class.getAnnotation(AnnAnn.class).ann();
   109 //        return ann.string() + ann.dbl() + ann.enums() + ann.integer() + ann.type();
   110 //    }
   111 //
   112 //    @Compare public String annotationAttrIsRead() {
   113 //        final Ann ann = E.class.getAnnotation(AnnAnn.class).ann();
   114 //        return ann.string();
   115 //    }
   116 
   117     private static String toClassNames(Object[] arr) {
   118         StringBuilder sb = new StringBuilder();
   119         sb.append("\n");
   120         for (Object o : arr) {
   121             if (o == null) {
   122                 sb.append("null");
   123             } else if (o instanceof Ann) {
   124                 sb.append("Ann");
   125             } else if (o instanceof AnnAnn) {
   126                 sb.append("AnnAnn");
   127             }
   128             sb.append("\n");
   129         }
   130         return sb.toString();
   131     }
   132 
   133     @Compare
   134     public String twoAnnotations() {
   135         Annotation[] arr = D.class.getAnnotations();
   136         if (arr == null) {
   137             return "Null array";
   138         }
   139         return toClassNames(arr);
   140     }
   141 
   142     @Compare
   143     public String twoDeclaredAnnotations() {
   144         Annotation[] arr = D.class.getDeclaredAnnotations();
   145         if (arr == null) {
   146             return "Null array";
   147         }
   148         return toClassNames(arr);
   149     }
   150 
   151     @Compare
   152     public String twoAnnotationsOnMethod() throws Exception {
   153         Annotation[] arr = D.class.getMethod("method").getAnnotations();
   154         if (arr == null) {
   155             return "Null array";
   156         }
   157         return toClassNames(arr);
   158     }
   159 
   160     @Compare
   161     public String twoDeclaredAnnotationsOnMethod() throws Exception {
   162         Annotation[] arr = D.class.getMethod("method").getAnnotations();
   163         if (arr == null) {
   164             return "Null array";
   165         }
   166         return toClassNames(arr);
   167     }
   168 
   169     @Factory
   170     public static Object[] create() {
   171         return VMTest.create(ReflectionAnnotationTest.class);
   172     }
   173     
   174 }