Path: blob/master/test/jdk/java/util/Locale/Bug8035133.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*/22/*23* @test24* @bug 803513325* @summary Checks that the tags matching the range with quality weight q=026* e.g. en;q=0 must be elimited and must not be the part of output27*/2829import java.util.ArrayList;30import java.util.Iterator;31import java.util.List;32import java.util.Locale;333435public class Bug8035133 {3637private static boolean err = false;3839public static void main(String[] args) {4041// checking Locale.lookup with de-ch;q=042checkLookup("en;q=0.1, *-ch;q=0.5, de-ch;q=0",43"de-ch, en, fr-ch", "fr-CH");4445/* checking Locale.lookup with *;q=0 '*' should be ignored46* in lookup47*/48checkLookup("en;q=0.1, *-ch;q=0.5, *;q=0",49"de-ch, en, fr-ch", "de-CH");5051// checking Locale.filter with fr-ch;q=0 in BASIC_FILTERING52checkFilter("en;q=0.1, fr-ch;q=0.0, de-ch;q=0.5",53"de-ch, en, fr-ch", "de-CH, en");5455// checking Locale.filter with *;q=0 in BASIC_FILTERING56checkFilter("de-ch;q=0.6, *;q=0", "de-ch, fr-ch", "");5758// checking Locale.filter with *;q=0 in BASIC_FILTERING59checkFilter("de-ch;q=0.6, de;q=0", "de-ch", "");6061// checking Locale.filter with *;q=0.6, en;q=0 in BASIC_FILTERING62checkFilter("*;q=0.6, en;q=0", "de-ch, hi-in, en", "de-CH, hi-IN");6364// checking Locale.filter with de-ch;q=0 in EXTENDED_FILTERING65checkFilter("en;q=0.1, *-ch;q=0.5, de-ch;q=0",66"de-ch, en, fr-ch", "fr-CH, en");6768/* checking Locale.filter with *-ch;q=0 in EXTENDED_FILTERING which69* must make filter to return "" empty or no match70*/71checkFilter("de-ch;q=0.5, *-ch;q=0", "de-ch, fr-ch", "");7273/* checking Locale.filter with *;q=0 in EXTENDED_FILTERING which74* must make filter to return "" empty or no match75*/76checkFilter("*-ch;q=0.5, *;q=0", "de-ch, fr-ch", "");7778/* checking Locale.filter with *;q=0.6, *-Latn;q=0 in79* EXTENDED_FILTERING80*/81checkFilter("*;q=0.6, *-Latn;q=0", "de-ch, hi-in, en-Latn",82"de-CH, hi-IN");8384if (err) {85throw new RuntimeException("[LocaleMatcher method(s) failed]");86}8788}8990private static void checkLookup(String ranges, String tags,91String expectedLocale) {9293List<Locale.LanguageRange> priorityList = Locale.LanguageRange94.parse(ranges);95List<Locale> localeList = generateLocales(tags);96Locale loc = Locale.lookup(priorityList, localeList);97String actualLocale98= loc.toLanguageTag();99100if (!actualLocale.equals(expectedLocale)) {101System.err.println("Locale.lookup failed with ranges: " + ranges102+ " Expected: " + expectedLocale103+ " Actual: " + actualLocale);104err = true;105}106107}108109private static void checkFilter(String ranges, String tags,110String expectedLocales) {111112List<Locale.LanguageRange> priorityList = Locale.LanguageRange113.parse(ranges);114List<Locale> localeList = generateLocales(tags);115String actualLocales = getLocalesAsString(116Locale.filter(priorityList, localeList));117118if (!actualLocales.equals(expectedLocales)) {119System.err.println("Locale.filter failed with ranges: " + ranges120+ " Expected: " + expectedLocales121+ " Actual: " + actualLocales);122err = true;123}124125}126127private static List<Locale> generateLocales(String tags) {128if (tags == null) {129return null;130}131132List<Locale> localeList = new ArrayList<>();133if (tags.equals("")) {134return localeList;135}136String[] t = tags.split(", ");137for (String tag : t) {138localeList.add(Locale.forLanguageTag(tag));139}140return localeList;141}142143private static String getLocalesAsString(List<Locale> locales) {144StringBuilder sb = new StringBuilder();145146Iterator<Locale> itr = locales.iterator();147if (itr.hasNext()) {148sb.append(itr.next().toLanguageTag());149}150while (itr.hasNext()) {151sb.append(", ");152sb.append(itr.next().toLanguageTag());153}154155return sb.toString().trim();156}157158}159160161