Path: blob/master/test/jdk/java/lang/Character/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 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.File;35import java.io.FileReader;36import java.util.ArrayList;37import java.util.List;38import java.util.Locale;3940public class UnicodeCasingTest {4142private static boolean err = false;4344// Locales which are used for testing45private static List<Locale> locales = new ArrayList<>();46static {47locales.add(new Locale("az", ""));48locales.addAll(java.util.Arrays.asList(Locale.getAvailableLocales()));49}505152public static void main(String[] args) {53UnicodeCasingTest specialCasingTest = new UnicodeCasingTest();54specialCasingTest.test();55}5657private void test() {58Locale defaultLocale = Locale.getDefault();5960BufferedReader in = null;6162try {63File file = UCDFiles.UNICODE_DATA.toFile();6465int locale_num = locales.size();66for (int l = 0; l < locale_num; l++) {67Locale locale = locales.get(l);68Locale.setDefault(locale);69System.out.println("Testing on " + locale + " locale....");7071in = new BufferedReader(new FileReader(file));7273String line;74while ((line = in.readLine()) != null) {75if (line.length() == 0 || line.charAt(0) == '#') {76continue;77}7879test(line);80}8182in.close();83in = null;84}85}86catch (Exception e) {87err = true;88e.printStackTrace();89}90finally {91if (in != null) {92try {93in.close();94}95catch (Exception e) {96}97}9899Locale.setDefault(defaultLocale);100101if (err) {102throw new RuntimeException("UnicodeCasingTest failed.");103} else {104System.out.println("UnicodeCasingTest passed.");105}106}107}108109private void test(String line) {110String[] fields = line.split(";", 15);111int orig = convert(fields[0]);112113if (fields[12].length() != 0) {114testUpperCase(orig, convert(fields[12]));115} else {116testUpperCase(orig, orig);117}118119if (fields[13].length() != 0) {120testLowerCase(orig, convert(fields[13]));121} else {122testLowerCase(orig, orig);123}124125if (fields[14].length() != 0) {126testTitleCase(orig, convert(fields[14]));127} else {128testTitleCase(orig, orig);129}130}131132private void testUpperCase(int orig, int expected) {133int got = Character.toUpperCase(orig);134135if (expected != got) {136err = true;137System.err.println("toUpperCase(" +138") failed.\n\tOriginal: " + toString(orig) +139"\n\tGot: " + toString(got) +140"\n\tExpected: " + toString(expected));141}142}143144private void testLowerCase(int orig, int expected) {145int got = Character.toLowerCase(orig);146147if (expected != got) {148err = true;149System.err.println("toLowerCase(" +150") failed.\n\tOriginal: " + toString(orig) +151"\n\tGot: " + toString(got) +152"\n\tExpected: " + toString(expected));153}154}155156private void testTitleCase(int orig, int expected) {157int got = Character.toTitleCase(orig);158159if (expected != got) {160err = true;161System.err.println("toTitleCase(" +162") failed.\n\tOriginal: " + toString(orig) +163"\n\tGot: " + toString(got) +164"\n\tExpected: " + toString(expected));165}166}167168private int convert(String str) {169return Integer.parseInt(str, 16);170}171172private String toString(int i) {173return Integer.toHexString(i).toUpperCase();174}175176}177178179