1 /*
 2  * Copyright (c) 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  * @test
25  * @summary Test the implementation
26  * of Locale.availableLocales()
27  * @bug 8282319
28  * @run junit StreamAvailableLocales
29  */
30 
31 import java.util.Arrays;
32 import java.util.Locale;
33 import java.util.stream.Stream;
34 import org.junit.Test;
35 import org.junit.jupiter.params.ParameterizedTest;
36 import org.junit.jupiter.params.provider.MethodSource;
37 import org.junit.jupiter.params.provider.Arguments;
38 
39 public class StreamAvailableLocales {
40 
41     /**
42      * Test to validate that the methods: Locale.getAvailableLocales()
43      * and Locale.availableLocales() contain the same underlying elements
44      */
45     @Test
46     public void testStreamEqualsArray() {
47         Locale[] arrayLocales = Locale.getAvailableLocales();
48         Stream<Locale> streamedLocales = Locale.availableLocales();
49         Locale[] convertedLocales = streamedLocales.toArray(Locale[]::new);
50         if (Arrays.equals(arrayLocales, convertedLocales)) {
51             System.out.println("$$$ Passed: The underlying elements" +
52                     " of getAvailableLocales() and availableLocales() are the same!");
53         } else {
54             throw new RuntimeException("$$$ Error: The underlying elements" +
55                     " of getAvailableLocales() and availableLocales()" +
56                     " are not the same.");
57         }
58     }
59 
60     /**
61      * Test to validate that the stream has the required
62      * Locale.ROOT and Locale.US.
63      */
64     @ParameterizedTest
65     @MethodSource("requiredLocaleProvider")
66     public void testStreamRequirements(Locale requiredLocale, String localeName) {
67         if (Locale.availableLocales().anyMatch(loc -> (loc.equals(requiredLocale)))) {
68             System.out.printf("$$$ Passed: Stream has %s!%n", localeName);
69         } else {
70             throw new RuntimeException(String.format("$$$ Error:" +
71                     " Stream is missing %s!", localeName));
72         }
73     }
74 
75     // Data provider for testStreamRequirements
76     private static Stream<Arguments> requiredLocaleProvider() {
77         return Stream.of(
78                 Arguments.of(Locale.ROOT, "Root locale"),
79                 Arguments.of(Locale.US, "US locale")
80         );
81     }
82 }