Path: blob/master/test/jdk/java/util/Locale/Bug8159420.java
41149 views
/*1* Copyright (c) 2016, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 815942026* @summary Checks the proper execution of LanguageRange.parse() and27* other LocaleMatcher methods when used in the locales like28* Turkish, because the toLowerCase() method is invoked in the29* parse() and other LocaleMatcher methods.30* e.g. "HI-Deva".toLowerCase() in the Turkish locale returns31* "hı-deva", where 'ı' is the LATIN SMALL LETTER DOTLESS I character32* which is not allowed in the language ranges/tags.33* @compile -encoding utf-8 Bug8159420.java34* @run main Bug815942035*/3637import java.util.List;38import java.util.Locale;39import java.util.Locale.LanguageRange;40import java.util.Locale.FilteringMode;41import java.util.LinkedHashMap;42import java.util.HashMap;43import java.util.Iterator;44import java.util.ArrayList;45import static java.util.Locale.FilteringMode.EXTENDED_FILTERING;46import static java.util.Locale.FilteringMode.AUTOSELECT_FILTERING;4748public class Bug8159420 {4950static boolean err = false;5152public static void main(String[] args) {5354Locale origLocale = null;55try {5657origLocale = Locale.getDefault();58Locale.setDefault(new Locale("tr", "TR"));59testParse();60testFilter(EXTENDED_FILTERING);61testFilter(AUTOSELECT_FILTERING);62testLookup();63testMapEquivalents();6465if (err) {66throw new RuntimeException("[LocaleMatcher method(s) in turkish"67+ " locale failed]");68}6970} finally {71Locale.setDefault(origLocale);72}7374}7576/* Before the fix, the testParse() method was throwing77* IllegalArgumentException in Turkish Locale78*/79private static void testParse() {80String ranges = "HI-Deva, ja-hIrA-JP, RKI";81try {82LanguageRange.parse(ranges);83} catch (Exception ex) {84System.err.println("[testParse() failed on range string: "85+ ranges + "] due to "+ex);86err = true;87}88}8990/* Before the fix, the testFilter() method was returning empty list in91* Turkish Locale92*/93private static void testFilter(FilteringMode mode) {9495String ranges = "hi-IN, itc-Ital";96String tags = "hi-IN, itc-Ital";97List<LanguageRange> priorityList = LanguageRange.parse(ranges);98List<Locale> tagList = generateLocales(tags);99String actualLocales = showLocales(Locale.filter(priorityList, tagList, mode));100String expectedLocales = "hi-IN, itc-Ital";101102if (!expectedLocales.equals(actualLocales)) {103System.err.println("testFilter(" + mode + ") failed on language ranges:"104+ " [" + ranges + "] and language tags: [" + tags + "]");105err = true;106}107}108109/* Before the fix, the testLookup() method was returning null in Turkish110* Locale111*/112private static void testLookup() {113boolean error = false;114String ranges = "hi-IN, itc-Ital";115String tags = "hi-IN, itc-Ital";116List<LanguageRange> priorityList = LanguageRange.parse(ranges);117List<Locale> localeList = generateLocales(tags);118Locale actualLocale119= Locale.lookup(priorityList, localeList);120String actualLocaleString = "";121122if (actualLocale != null) {123actualLocaleString = actualLocale.toLanguageTag();124} else {125error = true;126}127128String expectedLocale = "hi-IN";129130if (!expectedLocale.equals(actualLocaleString)) {131error = true;132}133134if (error) {135System.err.println("testLookup() failed on language ranges:"136+ " [" + ranges + "] and language tags: [" + tags + "]");137err = true;138}139140}141142/* Before the fix, testMapEquivalents() method was returning only "hi-in"143* in Turkish Locale144*/145private static void testMapEquivalents() {146147String ranges = "HI-IN";148List<LanguageRange> priorityList = LanguageRange.parse(ranges);149HashMap<String, List<String>> map = new LinkedHashMap<>();150List<String> equivalentList = new ArrayList<>();151equivalentList.add("HI");152equivalentList.add("HI-Deva");153map.put("HI", equivalentList);154155List<LanguageRange> expected = new ArrayList<>();156expected.add(new LanguageRange("hi-in"));157expected.add(new LanguageRange("hi-deva-in"));158List<LanguageRange> got159= LanguageRange.mapEquivalents(priorityList, map);160161if (!areEqual(expected, got)) {162System.err.println("testMapEquivalents() failed");163err = true;164}165166}167168private static boolean areEqual(List<LanguageRange> expected,169List<LanguageRange> got) {170171boolean error = false;172if (expected.equals(got)) {173return !error;174}175176List<LanguageRange> cloneExpected = new ArrayList<>(expected);177cloneExpected.removeAll(got);178if (!cloneExpected.isEmpty()) {179error = true;180System.err.println("Found missing range(s): " + cloneExpected);181}182183// not creating the 'got' clone as the list will not be used after this184got.removeAll(expected);185if (!got.isEmpty()) {186error = true;187System.err.println("Found extra range(s): " + got);188}189return !error;190}191192private static List<Locale> generateLocales(String tags) {193if (tags == null) {194return null;195}196197List<Locale> localeList = new ArrayList<>();198if (tags.equals("")) {199return localeList;200}201String[] t = tags.split(", ");202for (String tag : t) {203localeList.add(Locale.forLanguageTag(tag));204}205return localeList;206}207208private static String showLocales(List<Locale> locales) {209StringBuilder sb = new StringBuilder();210211Iterator<Locale> itr = locales.iterator();212if (itr.hasNext()) {213sb.append(itr.next().toLanguageTag());214}215while (itr.hasNext()) {216sb.append(", ");217sb.append(itr.next().toLanguageTag());218}219220return sb.toString().trim();221}222223}224225226