1 /* 2 * Copyright (c) 2022, 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 8282819 26 * @summary Unit tests for Locale.of() method. Those tests check the equality 27 * of obtained objects with ones that are gotten from other means with both 28 * well-formed and ill-formed arguments. Also checks the possible NPEs 29 * for error cases. 30 * @run testng TestOf 31 */ 32 package test.java.util.Locale; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertThrows; 36 37 import java.util.Locale; 38 39 import org.testng.annotations.Test; 40 import org.testng.annotations.DataProvider; 41 42 @SuppressWarnings("deprecation") 43 @Test 44 public class TestOf { 45 46 @DataProvider data_1Arg()47 public Object[][] data_1Arg() { 48 return new Object[][]{ 49 // well-formed 50 {Locale.ENGLISH, "en"}, 51 {Locale.JAPANESE, "ja"}, 52 53 // ill-formed 54 {Locale.ROOT, ""}, 55 {new Locale("a"), "a"}, 56 {new Locale("xxxxxxxxxx"), "xxxxxxxxxx"}, 57 }; 58 } 59 60 @DataProvider data_2Args()61 public Object[][] data_2Args() { 62 return new Object[][]{ 63 // well-formed 64 {Locale.US, "en", "US"}, 65 {Locale.JAPAN, "ja", "JP"}, 66 67 // ill-formed 68 {new Locale("", "US"), "", "US"}, 69 {new Locale("a", "b"), "a", "b"}, 70 {new Locale("xxxxxxxxxx", "yyyyyyyyyy"), "xxxxxxxxxx", "yyyyyyyyyy"}, 71 }; 72 } 73 74 @DataProvider data_3Args()75 public Object[][] data_3Args() { 76 return new Object[][]{ 77 // well-formed 78 {Locale.forLanguageTag("en-US-POSIX"), "en", "US", "POSIX"}, 79 {Locale.forLanguageTag("ja-JP-POSIX"), "ja", "JP", "POSIX"}, 80 81 // ill-formed 82 {new Locale("", "", "POSIX"), "", "", "POSIX"}, 83 {new Locale("a", "b", "c"), "a", "b", "c"}, 84 {new Locale("xxxxxxxxxx", "yyyyyyyyyy", "zzzzzzzzzz"), 85 "xxxxxxxxxx", "yyyyyyyyyy", "zzzzzzzzzz"}, 86 {new Locale("ja", "JP", "JP"), "ja", "JP", "JP"}, 87 {new Locale("th", "TH", "TH"), "th", "TH", "TH"}, 88 {new Locale("no", "NO", "NY"), "no", "NO", "NY"}, 89 }; 90 } 91 92 @Test (dataProvider = "data_1Arg") test_1Arg(Locale expected, String lang)93 public void test_1Arg(Locale expected, String lang) { 94 assertEquals(Locale.of(lang), expected); 95 } 96 97 @Test (dataProvider = "data_2Args") test_2Args(Locale expected, String lang, String ctry)98 public void test_2Args(Locale expected, String lang, String ctry) { 99 assertEquals(Locale.of(lang, ctry), expected); 100 } 101 102 @Test (dataProvider = "data_3Args") test_3Args(Locale expected, String lang, String ctry, String vrnt)103 public void test_3Args(Locale expected, String lang, String ctry, String vrnt) { 104 assertEquals(Locale.of(lang, ctry, vrnt), expected); 105 } 106 107 @Test test_NPE()108 public void test_NPE() { 109 assertThrows(NullPointerException.class, () -> Locale.of(null)); 110 assertThrows(NullPointerException.class, () -> Locale.of("", null)); 111 assertThrows(NullPointerException.class, () -> Locale.of("", "", null)); 112 } 113 } 114