Path: blob/master/test/jdk/java/lang/String/UnicodeCasingTest.java
41149 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* @test27* @bug 4397357 6565620 6959267 7070436 7198195 8032446 8072600 822143128* @summary Confirm normal case mappings are handled correctly.29* @library /lib/testlibrary/java/lang30* @run main/timeout=200 UnicodeCasingTest31*/3233import java.io.BufferedReader;34import java.io.IOException;35import java.nio.file.Files;36import java.nio.file.Paths;37import java.util.ArrayList;38import java.util.Locale;39import java.util.HashMap;40import java.util.List;41import java.util.Map;4243public class UnicodeCasingTest {4445private static boolean err = false;4647// Locales which are used for testing48private static List<Locale> locales = new ArrayList<>();49static {50locales.add(new Locale("az", ""));51locales.addAll(java.util.Arrays.asList(Locale.getAvailableLocales()));52}5354// Default locale55private static String defaultLang;5657// List for Unicode characters whose mappings are included in58// SpecialCasing.txt and mappings in UnicodeData.txt isn't applicable.59private static Map<String, String> excludeList = new HashMap<>();6061public static void main(String[] args) {62UnicodeCasingTest specialCasingTest = new UnicodeCasingTest();63specialCasingTest.test();64}6566private void test() {67Locale defaultLocale = Locale.getDefault();68BufferedReader in = null;69try {70// First, we create exlude lists of characters whose mappings exist71// in SpecialCasing.txt and mapping rules in UnicodeData.txt aren't72// applicable.73in = Files.newBufferedReader(UCDFiles.SPECIAL_CASING.toRealPath());74String line;75while ((line = in.readLine()) != null) {76if (line.length() == 0 || line.charAt(0) == '#') {77continue;78}79updateExcludeList(line);80}81in.close();82in = null;83int locale_num = locales.size();84for (int l = 0; l < locale_num; l++) {85Locale locale = locales.get(l);86Locale.setDefault(locale);87defaultLang = locale.getLanguage();88// System.out.println("Testing on " + locale + " locale....");89System.err.println("Testing on " + locale + " locale....");90in = Files.newBufferedReader(UCDFiles.UNICODE_DATA.toRealPath());91while ((line = in.readLine()) != null) {92if (line.length() == 0 || line.charAt(0) == '#') {93continue;94}95test(line);96}97in.close();98in = null;99}100}101catch (IOException e) {102err = true;103e.printStackTrace();104}105finally {106if (in != null) {107try {108in.close();109}110catch (IOException e) {111}112}113114Locale.setDefault(defaultLocale);115116if (err) {117throw new RuntimeException("UnicodeCasingTest failed.");118} else {119System.out.println("*** UnicodeCasingTest passed.");120}121}122}123124private void updateExcludeList(String line) {125int index = line.indexOf('#');126if (index != -1) {127line = line.substring(0, index);128}129130String lang = null;131String condition = null;132String[] fields = line.split("; ");133134// If the given character is mapped to multiple characters under the135// normal condition, add it to the exclude list.136if (fields.length == 4) {137excludeList.put(fields[0], "all");138} else if (fields.length == 5) {139if (fields[4].length() == 2) { /// locale140if (excludeList.get(fields[0]) == null) {141excludeList.put(fields[0], fields[4]);142}143}144}145}146147private void test(String line) {148String[] fields = line.split(";", 15);149String orig = convert(fields[0]);150151String lang = excludeList.get(fields[0]);152if (!"all".equals(lang) && !defaultLang.equals(lang)) {153if (fields[12].length() == 0) {154testUpperCase(orig, convert(fields[0]));155} else {156testUpperCase(orig, convert(fields[12]));157}158159if (fields[13].length() == 0) {160testLowerCase(orig, convert(fields[0]));161} else {162testLowerCase(orig, convert(fields[13]));163}164}165}166167private void testUpperCase(String orig, String expected) {168String got = orig.toUpperCase();169170// Ugly workaround for special mappings for az and tr locales....171if (orig.equals("\u0069") &&172(defaultLang.equals("az") || defaultLang.equals("tr"))) {173expected = "\u0130";174}175176if (!expected.equals(got)) {177err = true;178System.err.println("toUpperCase(" +179") failed.\n\tOriginal: " + toString(orig) +180"\n\tGot: " + toString(got) +181"\n\tExpected: " + toString(expected));182}183}184185private void testLowerCase(String orig, String expected) {186String got = orig.toLowerCase();187// Ugly workaround for special mappings for az and tr locales....188if (orig.equals("\u0049") &&189(defaultLang.equals("az") || defaultLang.equals("tr"))) {190expected = "\u0131";191}192193if (!expected.equals(got)) {194err = true;195System.err.println("toLowerCase(" +196") failed.\n\tOriginal: " + toString(orig) +197"\n\tGot: " + toString(got) +198"\n\tExpected: " + toString(expected));199}200}201202StringBuilder sb = new StringBuilder();203204private String convert(String str) {205sb.setLength(0);206207String[] tokens = str.split(" ");208for (String token : tokens) {209int j = Integer.parseInt(token, 16);210if (j < Character.MIN_SUPPLEMENTARY_CODE_POINT) {211sb.append((char)j);212} else {213sb.append(Character.toChars(j));214}215}216217return sb.toString();218}219220private String toString(String str) {221sb.setLength(0);222223int len = str.length();224for (int i = 0; i < len; i++) {225sb.append("0x").append(Integer.toHexString(str.charAt(i)).toUpperCase()).append(" ");226}227228return sb.toString();229}230231}232233234