|
1 | 1 | /* |
2 | | - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
25 | 25 | * @bug 8035133 |
26 | 26 | * @summary Checks that the tags matching the range with quality weight q=0 |
27 | 27 | * e.g. en;q=0 must be elimited and must not be the part of output |
| 28 | + * @run junit Bug8035133 |
28 | 29 | */ |
29 | 30 |
|
30 | 31 | import java.util.ArrayList; |
31 | 32 | import java.util.Iterator; |
32 | 33 | import java.util.List; |
33 | 34 | import java.util.Locale; |
| 35 | +import java.util.stream.Stream; |
34 | 36 |
|
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.Arguments; |
| 39 | +import org.junit.jupiter.params.provider.MethodSource; |
35 | 40 |
|
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", ""); |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
78 | 42 |
|
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 | | - } |
| 43 | +public class Bug8035133 { |
90 | 44 |
|
91 | | - private static void checkLookup(String ranges, String tags, |
| 45 | + // Ensure weights with 'q=0' work as expected during lookup |
| 46 | + @ParameterizedTest |
| 47 | + @MethodSource("lookupProvider") |
| 48 | + public void lookupTest(String ranges, String tags, |
92 | 49 | String expectedLocale) { |
93 | 50 |
|
94 | 51 | List<Locale.LanguageRange> priorityList = Locale.LanguageRange |
95 | 52 | .parse(ranges); |
96 | 53 | List<Locale> localeList = generateLocales(tags); |
97 | 54 | 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 | | - } |
| 55 | + String actualLocale = loc.toLanguageTag(); |
| 56 | + assertEquals(expectedLocale, actualLocale); |
| 57 | + } |
107 | 58 |
|
| 59 | + private static Stream<Arguments> lookupProvider() { |
| 60 | + return Stream.of( |
| 61 | + // checking Locale.lookup with de-ch;q=0 |
| 62 | + Arguments.of("en;q=0.1, *-ch;q=0.5, de-ch;q=0", |
| 63 | + "de-ch, en, fr-ch", "fr-CH"), |
| 64 | + // checking Locale.lookup with *;q=0 '*' should be ignored in lookup |
| 65 | + Arguments.of("en;q=0.1, *-ch;q=0.5, *;q=0", |
| 66 | + "de-ch, en, fr-ch", "de-CH") |
| 67 | + ); |
108 | 68 | } |
109 | 69 |
|
110 | | - private static void checkFilter(String ranges, String tags, |
| 70 | + // Ensure weights with 'q=0' work as expected during filtering |
| 71 | + @ParameterizedTest |
| 72 | + @MethodSource("filterProvider") |
| 73 | + public void filterTest(String ranges, String tags, |
111 | 74 | String expectedLocales) { |
112 | 75 |
|
113 | 76 | List<Locale.LanguageRange> priorityList = Locale.LanguageRange |
114 | 77 | .parse(ranges); |
115 | 78 | List<Locale> localeList = generateLocales(tags); |
116 | 79 | String actualLocales = getLocalesAsString( |
117 | 80 | Locale.filter(priorityList, localeList)); |
| 81 | + assertEquals(expectedLocales, actualLocales); |
| 82 | + } |
118 | 83 |
|
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 | | - |
| 84 | + private static Stream<Arguments> filterProvider() { |
| 85 | + return Stream.of( |
| 86 | + // checking Locale.filter with fr-ch;q=0 in BASIC_FILTERING |
| 87 | + Arguments.of("en;q=0.1, fr-ch;q=0.0, de-ch;q=0.5", |
| 88 | + "de-ch, en, fr-ch", "de-CH, en"), |
| 89 | + // checking Locale.filter with *;q=0 in BASIC_FILTERING |
| 90 | + Arguments.of("de-ch;q=0.6, *;q=0", "de-ch, fr-ch", ""), |
| 91 | + // checking Locale.filter with *;q=0 in BASIC_FILTERING |
| 92 | + Arguments.of("de-ch;q=0.6, de;q=0", "de-ch", ""), |
| 93 | + // checking Locale.filter with *;q=0.6, en;q=0 in BASIC_FILTERING |
| 94 | + Arguments.of("*;q=0.6, en;q=0", "de-ch, hi-in, en", "de-CH, hi-IN"), |
| 95 | + // checking Locale.filter with de-ch;q=0 in EXTENDED_FILTERING |
| 96 | + Arguments.of("en;q=0.1, *-ch;q=0.5, de-ch;q=0", |
| 97 | + "de-ch, en, fr-ch", "fr-CH, en"), |
| 98 | + /* checking Locale.filter with *-ch;q=0 in EXTENDED_FILTERING which |
| 99 | + * must make filter to return "" empty or no match |
| 100 | + */ |
| 101 | + Arguments.of("de-ch;q=0.5, *-ch;q=0", "de-ch, fr-ch", ""), |
| 102 | + /* checking Locale.filter with *;q=0 in EXTENDED_FILTERING which |
| 103 | + * must make filter to return "" empty or no match |
| 104 | + */ |
| 105 | + Arguments.of("*-ch;q=0.5, *;q=0", "de-ch, fr-ch", ""), |
| 106 | + /* checking Locale.filter with *;q=0.6, *-Latn;q=0 in |
| 107 | + * EXTENDED_FILTERING |
| 108 | + */ |
| 109 | + Arguments.of("*;q=0.6, *-Latn;q=0", "de-ch, hi-in, en-Latn", |
| 110 | + "de-CH, hi-IN") |
| 111 | + ); |
126 | 112 | } |
127 | 113 |
|
128 | 114 | private static List<Locale> generateLocales(String tags) { |
@@ -155,5 +141,4 @@ private static String getLocalesAsString(List<Locale> locales) { |
155 | 141 |
|
156 | 142 | return sb.toString().trim(); |
157 | 143 | } |
158 | | - |
159 | 144 | } |
0 commit comments