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 4316602
27  * @author joconner
28  * @summary Verify all Locale constructors and of() methods
29  * @run junit LocaleConstructors
30  */
31 
32 import java.util.Locale;
33 
34 import org.junit.jupiter.api.Test;
35 
36 import static org.junit.jupiter.api.Assertions.assertEquals;
37 
38 /**
39  * This class tests to ensure that the language, language/country, and
40  * language/country/variant Locale constructors + of() method are all allowed.
41  */
42 public class LocaleConstructors {
43 
44     static final String LANG = "en";
45     static final String COUNTRY = "US";
46     static final String VAR = "socal";
47 
48     // Test Locale constructor and .of() allow (language) argument(s)
49     @Test
50     public void langTest() {
51         Locale aLocale = Locale.of(LANG);
52         Locale otherLocale = new Locale(LANG);
53         assertEquals(aLocale.toString(), LANG);
54         assertEquals(otherLocale.toString(), LANG);
55     }
56 
57     // Test Locale constructor and .of() allow (language, constructor) argument(s)
58     @Test
59     public void langCountryTest() {
60         Locale aLocale = Locale.of(LANG, COUNTRY);
61         Locale otherLocale = new Locale(LANG, COUNTRY);
62         assertEquals(aLocale.toString(), String.format("%s_%s",
63                 LANG, COUNTRY));
64         assertEquals(otherLocale.toString(), String.format("%s_%s",
65                 LANG, COUNTRY));
66     }
67 
68     // Test Locale constructor and .of() allow
69     // (language, constructor, variant) argument(s)
70     @Test
71     public void langCountryVariantTest() {
72         Locale aLocale = Locale.of(LANG, COUNTRY, VAR);
73         Locale otherLocale = new Locale(LANG, COUNTRY, VAR);
74         assertEquals(aLocale.toString(), String.format("%s_%s_%s",
75                 LANG, COUNTRY, VAR));
76         assertEquals(otherLocale.toString(), String.format("%s_%s_%s",
77                 LANG, COUNTRY, VAR));
78     }
79 }