1 /*
 2  * Copyright (c) 2017, 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 8032842 8175539
26  * @summary Checks that the filterTags() and lookup() methods
27  *          preserve the case of matching language tag(s).
28  *          Before 8032842 fix these methods return the matching
29  *          language tag(s) in lowercase.
30  *          Also, checks the filterTags() to return only unique
31  *          (ignoring case considerations) matching tags.
32  *
33  */
34 
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.Locale.FilteringMode;
38 import java.util.Locale.LanguageRange;
39 
40 public class Bug8032842 {
41 
42     public static void main(String[] args) {
43 
44         // test filterBasic() for preserving the case of matching tags for
45         // the language range '*', with no duplicates in the matching tags
46         testFilter("*", List.of("de-CH", "hi-in", "En-GB", "ja-Latn-JP",
47                 "JA-JP", "en-GB"),
48                 List.of("de-CH", "hi-in", "En-GB", "ja-Latn-JP", "JA-JP"),
49                 FilteringMode.AUTOSELECT_FILTERING);
50 
51         // test filterBasic() for preserving the case of matching tags for
52         // basic ranges other than *, with no duplicates in the matching tags
53         testFilter("mtm-RU, en-GB", List.of("En-Gb", "mTm-RU", "en-US",
54                 "en-latn", "en-GB"),
55                 List.of("mTm-RU", "En-Gb"), FilteringMode.AUTOSELECT_FILTERING);
56 
57         // test filterExtended() for preserving the case of matching tags for
58         // the language range '*', with no duplicates in the matching tags
59         testFilter("*", List.of("de-CH", "hi-in", "En-GB", "hi-IN",
60                 "ja-Latn-JP", "JA-JP"),
61                 List.of("de-CH", "hi-in", "En-GB", "ja-Latn-JP", "JA-JP"),
62                 FilteringMode.EXTENDED_FILTERING);
63 
64         // test filterExtended() for preserving the case of matching tags for
65         // extended ranges other than *, with no duplicates in the matching tags
66         testFilter("*-ch;q=0.5, *-Latn;q=0.4", List.of("fr-CH", "de-Ch",
67                 "en-latn", "en-US", "en-Latn"),
68                 List.of("fr-CH", "de-Ch", "en-latn"),
69                 FilteringMode.EXTENDED_FILTERING);
70 
71         // test lookupTag() for preserving the case of matching tag
72         testLookup("*-ch;q=0.5", List.of("en", "fR-cH"), "fR-cH");
73 
74     }
75 
76     public static void testFilter(String ranges, List<String> tags,
77             List<String> expected, FilteringMode mode) {
78         List<LanguageRange> priorityList = LanguageRange.parse(ranges);
79         List<String> actual = Locale.filterTags(priorityList, tags, mode);
80         if (!actual.equals(expected)) {
81             throw new RuntimeException("[filterTags() failed for the language"
82                     + " range: " + ranges + ", Expected: " + expected
83                     + ", Found: " + actual + "]");
84         }
85     }
86 
87     public static void testLookup(String ranges, List<String> tags,
88             String expected) {
89         List<LanguageRange> priorityList = LanguageRange.parse(ranges);
90         String actual = Locale.lookupTag(priorityList, tags);
91         if (!actual.equals(expected)) {
92             throw new RuntimeException("[lookupTag() failed for the language"
93                     + " range: " + ranges + ", Expected: " + expected
94                     + ", Found: " + actual + "]");
95         }
96     }
97 
98 }
99