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 8166884
26  * @summary Checks the subsequent call to parse the same language ranges
27  *          which must generate the same list of language ranges
28  *          i.e. the priority list containing equivalents, as in the
29  *          first call
30  */
31 
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.stream.Collectors;
36 
37 public class Bug8166994 {
38 
main(String[] args)39     public static void main(String[] args) {
40         List<String> list = Arrays.asList("ccq-aa", "ybd-aa", "rki-aa");
41         String ranges = "ccq-aa";
42         testParseConsistency(list, ranges);
43 
44         // consecutive call to check the language range parse consistency
45         testParseConsistency(list, ranges);
46 
47         // another case with ranges consisting of multiple equivalents and
48         // single equivalents
49         list = Arrays.asList("gfx-xz", "oun-xz", "mwj-xz", "vaj-xz",
50                 "taj-xy", "tsf-xy");
51         ranges = "gfx-xz, taj-xy";
52         testParseConsistency(list, ranges);
53         // consecutive call to check the language range parse consistency
54         testParseConsistency(list, ranges);
55 
56     }
57 
testParseConsistency(List<String> list, String ranges)58     private static void testParseConsistency(List<String> list, String ranges) {
59         List<String> priorityList = parseRanges(ranges);
60         if (!list.equals(priorityList)) {
61             throw new RuntimeException("Failed to parse the language range ["
62                     + ranges + "], Expected: " + list + " Found: "
63                     + priorityList);
64         }
65     }
66 
parseRanges(String s)67     private static List<String> parseRanges(String s) {
68         return Locale.LanguageRange.parse(s).stream()
69                 .map(Locale.LanguageRange::getRange)
70                 .collect(Collectors.toList());
71     }
72 
73 }
74 
75