rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionAnnotationTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionAnnotationTest.java Sat Mar 19 10:31:13 2016 +0100
1.3 @@ -0,0 +1,117 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.tck;
1.22 +
1.23 +import java.lang.annotation.Retention;
1.24 +import java.lang.annotation.RetentionPolicy;
1.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
1.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
1.27 +import org.testng.annotations.Factory;
1.28 +
1.29 +/**
1.30 + *
1.31 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.32 + */
1.33 +public class ReflectionAnnotationTest {
1.34 + @Retention(RetentionPolicy.RUNTIME)
1.35 + @interface Ann {
1.36 + int integer() default 33;
1.37 + double dbl() default 33.3;
1.38 + String string() default "Ahoj";
1.39 + E enums() default E.B;
1.40 + Class<?> type() default String.class;
1.41 + }
1.42 +
1.43 + @interface AnnAnn {
1.44 + Ann ann() default @Ann(
1.45 + dbl = 44.4,
1.46 + enums = E.A,
1.47 + string = "Hi",
1.48 + integer = 44
1.49 + );
1.50 + }
1.51 +
1.52 + @Ann
1.53 + @AnnAnn
1.54 + class D {
1.55 + }
1.56 +
1.57 + @Ann(type = String.class)
1.58 + @AnnAnn(ann = @Ann(string = "Ciao"))
1.59 + enum E {
1.60 + A, B
1.61 + };
1.62 +
1.63 +
1.64 + @Compare public String annoClass() throws Exception {
1.65 + Retention r = Ann.class.getAnnotation(Retention.class);
1.66 + assert r != null : "Annotation is present";
1.67 + assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
1.68 + return r.annotationType().getName();
1.69 + }
1.70 +
1.71 + @Compare public boolean isAnnotation() {
1.72 + return Ann.class.isAnnotation();
1.73 + }
1.74 + @Compare public boolean isNotAnnotation() {
1.75 + return String.class.isAnnotation();
1.76 + }
1.77 + @Compare public boolean isNotAnnotationEnum() {
1.78 + return E.class.isAnnotation();
1.79 + }
1.80 +
1.81 + @Compare public int intDefaultAttrIsRead() {
1.82 + return D.class.getAnnotation(Ann.class).integer();
1.83 + }
1.84 +
1.85 + @Compare public double doubleDefaultAttrIsRead() {
1.86 + return D.class.getAnnotation(Ann.class).dbl();
1.87 + }
1.88 +
1.89 + @Compare public String stringDefaultAttrIsRead() {
1.90 + return D.class.getAnnotation(Ann.class).string();
1.91 + }
1.92 +
1.93 + @Compare public String enumDefaultAttrIsRead() {
1.94 + return D.class.getAnnotation(Ann.class).enums().toString();
1.95 + }
1.96 +
1.97 + @Compare public String classDefaultAttrIsRead() {
1.98 + return D.class.getAnnotation(Ann.class).type().getName();
1.99 + }
1.100 +
1.101 + @Compare public String classAttrIsRead() {
1.102 + return E.class.getAnnotation(Ann.class).type().getName();
1.103 + }
1.104 +
1.105 +// @Compare public String defaultAnnotationAttrIsRead() {
1.106 +// final Ann ann = D.class.getAnnotation(AnnAnn.class).ann();
1.107 +// return ann.string() + ann.dbl() + ann.enums() + ann.integer() + ann.type();
1.108 +// }
1.109 +//
1.110 +// @Compare public String annotationAttrIsRead() {
1.111 +// final Ann ann = E.class.getAnnotation(AnnAnn.class).ann();
1.112 +// return ann.string();
1.113 +// }
1.114 +
1.115 + @Factory
1.116 + public static Object[] create() {
1.117 + return VMTest.create(ReflectionAnnotationTest.class);
1.118 + }
1.119 +
1.120 +}