1 /*
  2  * Copyright (c) 2017, 2023, 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 /*
 25  * @test
 26  * @bug 8032842 8175539
 27  * @summary Checks that the filterTags() and lookup() methods
 28  *          preserve the case of matching language tag(s).
 29  *          Before 8032842 fix these methods return the matching
 30  *          language tag(s) in lowercase.
 31  *          Also, checks the filterTags() to return only unique
 32  *          (ignoring case considerations) matching tags.
 33  * @run junit PreserveTagCase
 34  */
 35 
 36 import java.util.List;
 37 import java.util.Locale;
 38 import java.util.Locale.FilteringMode;
 39 import java.util.Locale.LanguageRange;
 40 import java.util.stream.Stream;
 41 
 42 import org.junit.jupiter.params.ParameterizedTest;
 43 import org.junit.jupiter.params.provider.Arguments;
 44 import org.junit.jupiter.params.provider.MethodSource;
 45 
 46 import static org.junit.jupiter.api.Assertions.assertEquals;
 47 
 48 public class PreserveTagCase {
 49 
 50     /**
 51      * This test ensures that Locale.filterTags() preserves the case of matching
 52      * language tag(s).
 53      */
 54     @ParameterizedTest
 55     @MethodSource("filterProvider")
 56     public static void testFilterTags(String ranges, List<String> tags,
 57                                   List<String> expected, FilteringMode mode) {
 58         List<LanguageRange> priorityList = LanguageRange.parse(ranges);
 59         List<String> actual = Locale.filterTags(priorityList, tags, mode);
 60         assertEquals(actual, expected, String.format("[filterTags() failed for " +
 61                 "the language range: %s, Expected: %s, Found: %s]", ranges, expected, actual));
 62     }
 63 
 64     /**
 65      * This test ensures that Locale.lookupTag() preserves the case of matching
 66      * language tag(s).
 67      */
 68     @ParameterizedTest
 69     @MethodSource("lookupProvider")
 70     public static void testLookupTag(String ranges, List<String> tags,
 71                                   String expected) {
 72         List<LanguageRange> priorityList = LanguageRange.parse(ranges);
 73         String actual = Locale.lookupTag(priorityList, tags);
 74         assertEquals(actual, expected, String.format("[lookupTags() failed for " +
 75                 "the language range: %s, Expected: %s, Found: %s]", ranges, expected, actual));
 76     }
 77 
 78     private static Stream<Arguments> filterProvider() {
 79         return Stream.of(
 80                 // test filterBasic() for preserving the case of matching tags for
 81                 // the language range '*', with no duplicates in the matching tags
 82                 Arguments.of("*",
 83                         List.of("de-CH", "hi-in", "En-GB", "ja-Latn-JP", "JA-JP", "en-GB"),
 84                         List.of("de-CH", "hi-in", "En-GB", "ja-Latn-JP", "JA-JP"),
 85                         FilteringMode.AUTOSELECT_FILTERING),
 86                 // test filterBasic() for preserving the case of matching tags for
 87                 // basic ranges other than *, with no duplicates in the matching tags
 88                 Arguments.of("mtm-RU, en-GB",
 89                         List.of("En-Gb", "mTm-RU", "en-US", "en-latn", "en-GB"),
 90                         List.of("mTm-RU", "En-Gb"),
 91                         FilteringMode.AUTOSELECT_FILTERING),
 92                 // test filterExtended() for preserving the case of matching tags for
 93                 // the language range '*', with no duplicates in the matching tags
 94                 Arguments.of("*",
 95                         List.of("de-CH", "hi-in", "En-GB", "hi-IN", "ja-Latn-JP", "JA-JP"),
 96                         List.of("de-CH", "hi-in", "En-GB", "ja-Latn-JP", "JA-JP"),
 97                         FilteringMode.EXTENDED_FILTERING),
 98                 // test filterExtended() for preserving the case of matching tags for
 99                 // extended ranges other than *, with no duplicates in the matching tags
100                 Arguments.of("*-ch;q=0.5, *-Latn;q=0.4",
101                         List.of("fr-CH", "de-Ch", "en-latn", "en-US", "en-Latn"),
102                         List.of("fr-CH", "de-Ch", "en-latn"),
103                         FilteringMode.EXTENDED_FILTERING)
104         );
105     }
106 
107     private static Stream<Arguments> lookupProvider() {
108         return Stream.of(
109                 // test lookupTag() for preserving the case of matching tag
110                 Arguments.of("*-ch;q=0.5", List.of("en", "fR-cH"), "fR-cH"),
111                 Arguments.of("*-Latn;q=0.4", List.of("en", "fR-LATn"), "fR-LATn")
112         );
113     }
114 }