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