1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.lang.annotation.*;
  25 import java.lang.reflect.*;
  26 
  27 /*
  28  * @test
  29  * @bug 8054307
  30  * @summary Tests the correct encoding of latin1/UTF16 Strings used in annotations.
  31  */
  32 public class TestAnnotatedStringEncoding {
  33     @Retention(RetentionPolicy.RUNTIME)
  34     @Target(ElementType.METHOD)
  35     @interface Test {
  36         String str();
  37         int index();
  38     }
  39 
  40     public static void main(String[] args) throws Exception {
  41         new TestAnnotatedStringEncoding().run();
  42     }
  43 
  44     public void run() {
  45         // Iterate over annotated methods and retrieve the string
  46         for (Method m : this.getClass().getMethods()) {
  47             if (m.isAnnotationPresent(Test.class)) {
  48                 // Check if string equals expected value
  49                 Test test = m.getAnnotation(Test.class);
  50                 String str = test.str();
  51                 int index = test.index();
  52                 if (!str.equals(strValue[index])) {
  53                     throw new RuntimeException(m.getName() + " failed: \"" + str + "\" (0x" + Integer.toHexString(str.charAt(0)) +
  54                             ") does not equal \"" + strValue[index] + "\" (0x" + Integer.toHexString(strValue[index].charAt(0)) + ") .");
  55                 }
  56             }
  57         }
  58         System.out.println("Test passed.");
  59     }
  60 
  61     public static String[] strValue = {
  62         "\u0000", "\u0020", "\u0021", "\u0080",
  63         "\u00FF", "\u0100", "\u017F", "\u01FF",
  64         "\u07FF", "\u0800", "\uC280", "\uC2BF",
  65         "\uC380", "\uC3BF", "\uC5BF", "\uFFFF",
  66         "\u10000", "\u1FFFFF", "\u200000",
  67         "\u3FFFFFF", "\u4000000", "\u7FFFFFFF",
  68         "ab\uff23\uff24ef\uff27", "\uff21\uff22cd\uff25g", "\u00FF\u00FF\u00FF", "\u00A1\u00A1\u00A1\u00A1", ""};
  69 
  70     @Test(str = "\u0000", index = 0)
  71     public static void check0() { }
  72 
  73     @Test(str = "\u0020", index = 1)
  74     public static void check1() { }
  75 
  76     @Test(str = "\u0021", index = 2)
  77     public static void check2() { }
  78 
  79     @Test(str = "\u0080", index = 3)
  80     public static void check3() { }
  81 
  82     @Test(str = "\u00FF", index = 4)
  83     public static void check4() { }
  84 
  85     @Test(str = "\u0100", index = 5)
  86     public static void check5() { }
  87 
  88     @Test(str = "\u017F", index = 6)
  89     public static void check6() { }
  90 
  91     @Test(str = "\u01FF", index = 7)
  92     public static void check7() { }
  93 
  94     @Test(str = "\u07FF", index = 8)
  95     public static void check8() { }
  96 
  97     @Test(str = "\u0800", index = 9)
  98     public static void check9() { }
  99 
 100     @Test(str = "\uC280", index = 10)
 101     public static void check10() { }
 102 
 103     @Test(str = "\uC2BF", index = 11)
 104     public static void check11() { }
 105 
 106     @Test(str = "\uC380", index = 12)
 107     public static void check12() { }
 108 
 109     @Test(str = "\uC3BF", index = 13)
 110     public static void check13() { }
 111 
 112     @Test(str = "\uC5BF", index = 14)
 113     public static void check14() { }
 114 
 115     @Test(str = "\uFFFF", index = 15)
 116     public static void check15() { }
 117 
 118     @Test(str = "\u10000", index = 16)
 119     public static void check16() { }
 120 
 121     @Test(str = "\u1FFFFF", index = 17)
 122     public static void check17() { }
 123 
 124     @Test(str = "\u200000", index = 18)
 125     public static void check18() { }
 126 
 127     @Test(str = "\u3FFFFFF", index = 19)
 128     public static void check19() { }
 129 
 130     @Test(str = "\u4000000", index = 20)
 131     public static void check20() { }
 132 
 133     @Test(str = "\u7FFFFFFF", index = 21)
 134     public static void check21() { }
 135 
 136     @Test(str = "ab\uff23\uff24ef\uff27", index = 22)
 137     public static void check22() { }
 138 
 139     @Test(str = "\uff21\uff22cd\uff25g", index = 23)
 140     public static void check23() { }
 141 
 142     @Test(str = "\u00FF\u00FF\u00FF", index = 24)
 143     public static void check24() { }
 144 
 145     @Test(str = "\u00A1\u00A1\u00A1\u00A1", index = 25)
 146     public static void check25() { }
 147 
 148     @Test(str = "", index = 26)
 149     public static void check26() { }
 150 }