1 /*
   2  * Copyright (c) 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 4397357 6565620 6959267 8032446 8072600
  29  * @summary Confirm normal case mappings are handled correctly.
  30  * @run main/timeout=200 UnicodeCasingTest
  31  */
  32 
  33 import java.io.BufferedReader;
  34 import java.io.File;
  35 import java.io.FileReader;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.Locale;
  39 
  40 public class UnicodeCasingTest {
  41 
  42     private static boolean err = false;
  43 
  44     // Locales which are used for testing
  45     private static List<Locale> locales = new ArrayList<>();
  46     static {
  47         locales.add(new Locale("az", ""));
  48         locales.addAll(java.util.Arrays.asList(Locale.getAvailableLocales()));
  49     }
  50 
  51 
  52     public static void main(String[] args) {
  53         UnicodeCasingTest specialCasingTest = new UnicodeCasingTest();
  54         specialCasingTest.test();
  55     }
  56 
  57     private void test() {
  58         Locale defaultLocale = Locale.getDefault();
  59 
  60         BufferedReader in = null;
  61 
  62         try {
  63             File file = new File(System.getProperty("test.src", "."),
  64                                  "UnicodeData.txt");
  65 
  66             int locale_num = locales.size();
  67             for (int l = 0; l < locale_num; l++) {
  68                 Locale locale = locales.get(l);
  69                 Locale.setDefault(locale);
  70                 System.out.println("Testing on " + locale + " locale....");
  71 
  72                 in = new BufferedReader(new FileReader(file));
  73 
  74                 String line;
  75                 while ((line = in.readLine()) != null) {
  76                     if (line.length() == 0 || line.charAt(0) == '#') {
  77                         continue;
  78                     }
  79 
  80                     test(line);
  81                 }
  82 
  83                 in.close();
  84                 in = null;
  85             }
  86         }
  87         catch (Exception e) {
  88             err = true;
  89             e.printStackTrace();
  90         }
  91         finally {
  92             if (in != null) {
  93                 try {
  94                     in.close();
  95                 }
  96                 catch (Exception e) {
  97                 }
  98             }
  99 
 100             Locale.setDefault(defaultLocale);
 101 
 102             if (err) {
 103                 throw new RuntimeException("UnicodeCasingTest failed.");
 104             } else {
 105                 System.out.println("UnicodeCasingTest passed.");
 106             }
 107         }
 108     }
 109 
 110     private void test(String line) {
 111         String[] fields = line.split(";", 15);
 112         int orig = convert(fields[0]);
 113 
 114         if (fields[12].length() != 0) {
 115             testUpperCase(orig, convert(fields[12]));
 116         } else {
 117             testUpperCase(orig, orig);
 118         }
 119 
 120         if (fields[13].length() != 0) {
 121             testLowerCase(orig, convert(fields[13]));
 122         } else {
 123             testLowerCase(orig, orig);
 124         }
 125 
 126         if (fields[14].length() != 0) {
 127             testTitleCase(orig, convert(fields[14]));
 128         } else {
 129             testTitleCase(orig, orig);
 130         }
 131     }
 132 
 133     private void testUpperCase(int orig, int expected) {
 134         int got = Character.toUpperCase(orig);
 135 
 136         if (expected != got) {
 137             err = true;
 138             System.err.println("toUpperCase(" +
 139                 ") failed.\n\tOriginal: " + toString(orig) +
 140                 "\n\tGot:      " + toString(got) +
 141                 "\n\tExpected: " + toString(expected));
 142         }
 143     }
 144 
 145     private void testLowerCase(int orig, int expected) {
 146         int got = Character.toLowerCase(orig);
 147 
 148         if (expected != got) {
 149             err = true;
 150             System.err.println("toLowerCase(" +
 151                 ") failed.\n\tOriginal: " + toString(orig) +
 152                 "\n\tGot:      " + toString(got) +
 153                 "\n\tExpected: " + toString(expected));
 154         }
 155     }
 156 
 157     private void testTitleCase(int orig, int expected) {
 158         int got = Character.toTitleCase(orig);
 159 
 160         if (expected != got) {
 161             err = true;
 162             System.err.println("toTitleCase(" +
 163                 ") failed.\n\tOriginal: " + toString(orig) +
 164                 "\n\tGot:      " + toString(got) +
 165                 "\n\tExpected: " + toString(expected));
 166         }
 167     }
 168 
 169     private int convert(String str) {
 170         return Integer.parseInt(str, 16);
 171     }
 172 
 173     private String toString(int i) {
 174         return Integer.toHexString(i).toUpperCase();
 175     }
 176 
 177 }