jdk/test/java/util/Locale/Bug8035133.java
changeset 40114 0194b0ba95c6
equal deleted inserted replaced
40113:7ba318a6b751 40114:0194b0ba95c6
       
     1 /*
       
     2  * Copyright (c) 2016, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 /*
       
    24  * @test
       
    25  * @bug 8035133
       
    26  * @summary Checks that the tags matching the range with quality weight q=0
       
    27  *          e.g. en;q=0 must be elimited and must not be the part of output
       
    28  */
       
    29 
       
    30 import java.util.ArrayList;
       
    31 import java.util.Iterator;
       
    32 import java.util.List;
       
    33 import java.util.Locale;
       
    34 
       
    35 
       
    36 public class Bug8035133 {
       
    37 
       
    38     private static boolean err = false;
       
    39 
       
    40     public static void main(String[] args) {
       
    41 
       
    42         // checking Locale.lookup with de-ch;q=0
       
    43         checkLookup("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
       
    44                 "de-ch, en, fr-ch", "fr-CH");
       
    45 
       
    46         /* checking Locale.lookup with *;q=0 '*' should be ignored
       
    47          * in lookup
       
    48          */
       
    49         checkLookup("en;q=0.1, *-ch;q=0.5, *;q=0",
       
    50                 "de-ch, en, fr-ch", "de-CH");
       
    51 
       
    52         // checking Locale.filter with fr-ch;q=0 in BASIC_FILTERING
       
    53         checkFilter("en;q=0.1, fr-ch;q=0.0, de-ch;q=0.5",
       
    54                 "de-ch, en, fr-ch", "de-CH, en");
       
    55 
       
    56         // checking Locale.filter with *;q=0 in BASIC_FILTERING
       
    57         checkFilter("de-ch;q=0.6, *;q=0", "de-ch, fr-ch", "");
       
    58 
       
    59         // checking Locale.filter with *;q=0 in BASIC_FILTERING
       
    60         checkFilter("de-ch;q=0.6, de;q=0", "de-ch", "");
       
    61 
       
    62         // checking Locale.filter with *;q=0.6, en;q=0 in BASIC_FILTERING
       
    63         checkFilter("*;q=0.6, en;q=0", "de-ch, hi-in, en", "de-CH, hi-IN");
       
    64 
       
    65         // checking Locale.filter with de-ch;q=0 in EXTENDED_FILTERING
       
    66         checkFilter("en;q=0.1, *-ch;q=0.5, de-ch;q=0",
       
    67                 "de-ch, en, fr-ch", "fr-CH, en");
       
    68 
       
    69         /* checking Locale.filter with *-ch;q=0 in EXTENDED_FILTERING which
       
    70          * must make filter to return "" empty or no match
       
    71          */
       
    72         checkFilter("de-ch;q=0.5, *-ch;q=0", "de-ch, fr-ch", "");
       
    73 
       
    74         /* checking Locale.filter with *;q=0 in EXTENDED_FILTERING which
       
    75          * must make filter to return "" empty or no match
       
    76          */
       
    77         checkFilter("*-ch;q=0.5, *;q=0", "de-ch, fr-ch", "");
       
    78 
       
    79         /* checking Locale.filter with *;q=0.6, *-Latn;q=0 in
       
    80          * EXTENDED_FILTERING
       
    81          */
       
    82         checkFilter("*;q=0.6, *-Latn;q=0", "de-ch, hi-in, en-Latn",
       
    83                 "de-CH, hi-IN");
       
    84 
       
    85         if (err) {
       
    86             throw new RuntimeException("[LocaleMatcher method(s) failed]");
       
    87         }
       
    88 
       
    89     }
       
    90 
       
    91     private static void checkLookup(String ranges, String tags,
       
    92             String expectedLocale) {
       
    93 
       
    94         List<Locale.LanguageRange> priorityList = Locale.LanguageRange
       
    95                 .parse(ranges);
       
    96         List<Locale> localeList = generateLocales(tags);
       
    97         Locale loc = Locale.lookup(priorityList, localeList);
       
    98         String actualLocale
       
    99                 = loc.toLanguageTag();
       
   100 
       
   101         if (!actualLocale.equals(expectedLocale)) {
       
   102             System.err.println("Locale.lookup failed with ranges: " + ranges
       
   103                     + " Expected: " + expectedLocale
       
   104                     + " Actual: " + actualLocale);
       
   105             err = true;
       
   106         }
       
   107 
       
   108     }
       
   109 
       
   110     private static void checkFilter(String ranges, String tags,
       
   111             String expectedLocales) {
       
   112 
       
   113         List<Locale.LanguageRange> priorityList = Locale.LanguageRange
       
   114                 .parse(ranges);
       
   115         List<Locale> localeList = generateLocales(tags);
       
   116         String actualLocales = getLocalesAsString(
       
   117                 Locale.filter(priorityList, localeList));
       
   118 
       
   119         if (!actualLocales.equals(expectedLocales)) {
       
   120             System.err.println("Locale.filter failed with ranges: " + ranges
       
   121                     + " Expected: " + expectedLocales
       
   122                     + " Actual: " + actualLocales);
       
   123             err = true;
       
   124         }
       
   125 
       
   126     }
       
   127 
       
   128     private static List<Locale> generateLocales(String tags) {
       
   129         if (tags == null) {
       
   130             return null;
       
   131         }
       
   132 
       
   133         List<Locale> localeList = new ArrayList<>();
       
   134         if (tags.equals("")) {
       
   135             return localeList;
       
   136         }
       
   137         String[] t = tags.split(", ");
       
   138         for (String tag : t) {
       
   139             localeList.add(Locale.forLanguageTag(tag));
       
   140         }
       
   141         return localeList;
       
   142     }
       
   143 
       
   144     private static String getLocalesAsString(List<Locale> locales) {
       
   145         StringBuilder sb = new StringBuilder();
       
   146 
       
   147         Iterator<Locale> itr = locales.iterator();
       
   148         if (itr.hasNext()) {
       
   149             sb.append(itr.next().toLanguageTag());
       
   150         }
       
   151         while (itr.hasNext()) {
       
   152             sb.append(", ");
       
   153             sb.append(itr.next().toLanguageTag());
       
   154         }
       
   155 
       
   156         return sb.toString().trim();
       
   157     }
       
   158 
       
   159 }