1 /*
  2  * Copyright (c) 2007, 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 4122700 8282319
 27  * @summary Verify implementation of getAvailableLocales() and availableLocales()
 28  * @run junit AvailableLocalesTest
 29  */
 30 
 31 import java.util.Arrays;
 32 import java.util.Locale;
 33 import java.util.stream.Stream;
 34 
 35 import org.junit.jupiter.api.Test;
 36 import org.junit.jupiter.params.ParameterizedTest;
 37 import org.junit.jupiter.params.provider.MethodSource;
 38 import org.junit.jupiter.params.provider.Arguments;
 39 
 40 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 41 
 42 public class AvailableLocalesTest {
 43 
 44     /**
 45      * Test that Locale.getAvailableLocales() is non-empty and prints out
 46      * the returned locales - 4122700.
 47      */
 48     @Test
 49     public void nonEmptyLocalesTest() {
 50         Locale[] systemLocales = Locale.getAvailableLocales();
 51         assertNotEquals(systemLocales.length, 0, "Available locale list is empty!");
 52         System.out.println("Found " + systemLocales.length + " locales:");
 53         printLocales(systemLocales);
 54     }
 55 
 56     /**
 57      * Test to validate that the methods: Locale.getAvailableLocales()
 58      * and Locale.availableLocales() contain the same underlying elements
 59      */
 60     @Test
 61     public void streamEqualsArrayTest() {
 62         Locale[] arrayLocales = Locale.getAvailableLocales();
 63         Stream<Locale> streamedLocales = Locale.availableLocales();
 64         Locale[] convertedLocales = streamedLocales.toArray(Locale[]::new);
 65         if (Arrays.equals(arrayLocales, convertedLocales)) {
 66             System.out.println("$$$ Passed: The underlying elements" +
 67                     " of getAvailableLocales() and availableLocales() are the same!");
 68         } else {
 69             throw new RuntimeException("$$$ Error: The underlying elements" +
 70                     " of getAvailableLocales() and availableLocales()" +
 71                     " are not the same.");
 72         }
 73     }
 74 
 75     /**
 76      * Test to validate that the stream has the required
 77      * Locale.ROOT and Locale.US.
 78      */
 79     @ParameterizedTest
 80     @MethodSource("requiredLocaleProvider")
 81     public void requiredLocalesTest(Locale requiredLocale, String localeName) {
 82         if (Locale.availableLocales().anyMatch(loc -> (loc.equals(requiredLocale)))) {
 83             System.out.printf("$$$ Passed: Stream has %s!%n", localeName);
 84         } else {
 85             throw new RuntimeException(String.format("$$$ Error:" +
 86                     " Stream is missing %s!", localeName));
 87         }
 88     }
 89 
 90     // Helper method to print out all the system locales
 91     private void printLocales(Locale[] systemLocales) {
 92         Locale[] locales = new Locale[systemLocales.length];
 93         for (int i = 0; i < locales.length; i++) {
 94             Locale lowest = null;
 95             for (Locale systemLocale : systemLocales) {
 96                 if (i > 0 && locales[i - 1].toString().compareTo(systemLocale.toString()) >= 0)
 97                     continue;
 98                 if (lowest == null || systemLocale.toString().compareTo(lowest.toString()) < 0)
 99                     lowest = systemLocale;
100             }
101             locales[i] = lowest;
102         }
103         for (Locale locale : locales) {
104             if (locale.getCountry().length() == 0)
105                 System.out.println("    " + locale.getDisplayLanguage() + ":");
106             else {
107                 if (locale.getVariant().length() == 0)
108                     System.out.println("        " + locale.getDisplayCountry());
109                 else
110                     System.out.println("        " + locale.getDisplayCountry() + ", "
111                             + locale.getDisplayVariant());
112             }
113         }
114     }
115 
116     // Data provider for testStreamRequirements
117     private static Stream<Arguments> requiredLocaleProvider() {
118         return Stream.of(
119                 Arguments.of(Locale.ROOT, "Root locale"),
120                 Arguments.of(Locale.US, "US locale")
121         );
122     }
123 }