1 /*
2  * Copyright (c) 2012, 2018, 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 6336885 7196799 7197573 7198834 8000245 8000615 8001440 8008577
27  *      8010666 8013086 8013233 8013903 8015960 8028771 8054482 8062006
28  *      8150432
29  * @summary tests for "java.locale.providers" system property
30  * @library /test/lib
31  * @build LocaleProviders
32  *        providersrc.spi.src.tznp
33  *        providersrc.spi.src.tznp8013086
34  * @modules java.base/sun.util.locale
35  *          java.base/sun.util.locale.provider
36  * @run main LocaleProvidersRun
37  */
38 
39 import java.util.Locale;
40 
41 import jdk.test.lib.JDKToolLauncher;
42 import jdk.test.lib.process.ProcessTools;
43 import jdk.test.lib.Utils;
44 
45 public class LocaleProvidersRun {
main(String[] args)46     public static void main(String[] args) throws Throwable {
47         //get the platform default locales
48         Locale platDefLoc = Locale.getDefault(Locale.Category.DISPLAY);
49         String defLang = platDefLoc.getLanguage();
50         String defCtry = platDefLoc.getCountry();
51         System.out.println("DEFLANG = " + defLang);
52         System.out.println("DEFCTRY = " + defCtry);
53 
54         Locale platDefFormat = Locale.getDefault(Locale.Category.FORMAT);
55         String defFmtLang = platDefFormat.getLanguage();
56         String defFmtCtry = platDefFormat.getCountry();
57         System.out.println("DEFFMTLANG = " + defFmtLang);
58         System.out.println("DEFFMTCTRY = " + defFmtCtry);
59 
60         //Run Test
61         //testing HOST is selected for the default locale,
62         // if specified on Windows or MacOSX
63         String osName = System.getProperty("os.name");
64         String param1 = "JRE";
65         if(osName.startsWith("Windows") || osName.startsWith("Mac")) {
66             param1 = "HOST";
67         }
68         testRun("HOST,JRE", "adapterTest", param1, defLang, defCtry);
69 
70         //testing HOST is NOT selected for the non-default locale, if specified
71         //Try to find the locale JRE supports which is not the platform default
72         // (HOST supports that one)
73         String param2;
74         String param3;
75         if (!defLang.equals("en") && !defFmtLang.equals("en")){
76             param2 = "en";
77             param3 = "US";
78         } else if(!defLang.equals("ja") && !defFmtLang.equals("ja")){
79             param2 = "ja";
80             param3 = "JP";
81         } else {
82             param2 = "zh";
83             param3 = "CN";
84         }
85         testRun("HOST,JRE", "adapterTest", "JRE", param2, param3);
86 
87         //testing SPI is NOT selected, as there is none.
88         testRun("SPI,JRE", "adapterTest", "JRE", "en", "US");
89         testRun("SPI,COMPAT", "adapterTest", "JRE", "en", "US");
90 
91         //testing the order, variant #1. This assumes en_GB DateFormat data are
92         // available both in JRE & CLDR
93         testRun("CLDR,JRE", "adapterTest", "CLDR", "en", "GB");
94         testRun("CLDR,COMPAT", "adapterTest", "CLDR", "en", "GB");
95 
96         //testing the order, variant #2. This assumes en_GB DateFormat data are
97         // available both in JRE & CLDR
98         testRun("JRE,CLDR", "adapterTest", "JRE", "en", "GB");
99         testRun("COMPAT,CLDR", "adapterTest", "JRE", "en", "GB");
100 
101         //testing the order, variant #3 for non-existent locale in JRE
102         // assuming "haw" is not in JRE.
103         testRun("JRE,CLDR", "adapterTest", "CLDR", "haw", "");
104         testRun("COMPAT,CLDR", "adapterTest", "CLDR", "haw", "");
105 
106         //testing the order, variant #4 for the bug 7196799. CLDR's "zh" data
107         // should be used in "zh_CN"
108         testRun("CLDR", "adapterTest", "CLDR", "zh", "CN");
109 
110         //testing FALLBACK provider. SPI and invalid one cases.
111         testRun("SPI", "adapterTest", "FALLBACK", "en", "US");
112         testRun("FOO", "adapterTest", "CLDR", "en", "US");
113         testRun("BAR,SPI", "adapterTest", "FALLBACK", "en", "US");
114 
115         //testing 7198834 fix.
116         testRun("HOST", "bug7198834Test", "", "", "");
117 
118         //testing 8000245 fix.
119         testRun("JRE", "tzNameTest", "Europe/Moscow", "", "");
120         testRun("COMPAT", "tzNameTest", "Europe/Moscow", "", "");
121 
122         //testing 8000615 fix.
123         testRun("JRE", "tzNameTest", "America/Los_Angeles", "", "");
124         testRun("COMPAT", "tzNameTest", "America/Los_Angeles", "", "");
125 
126         //testing 8001440 fix.
127         testRun("CLDR", "bug8001440Test", "", "", "");
128 
129         //testing 8010666 fix.
130         if (defLang.equals("en")) {
131             testRun("HOST", "bug8010666Test", "", "", "");
132         }
133 
134         //testing 8013086 fix.
135         testRun("JRE,SPI", "bug8013086Test", "ja", "JP", "");
136         testRun("COMPAT,SPI", "bug8013086Test", "ja", "JP", "");
137 
138         //testing 8013903 fix. (Windows only)
139         testRun("HOST,JRE", "bug8013903Test", "", "", "");
140         testRun("HOST", "bug8013903Test", "", "", "");
141         testRun("HOST,COMPAT", "bug8013903Test", "", "", "");
142 
143         //testing 8027289 fix, if the platform format default is zh_CN
144         // this assumes Windows' currency symbol for zh_CN is \u00A5, the yen
145         // (yuan) sign.
146         if (!defLang.equals("en") && !defCtry.equals("CN")){
147             testRun("JRE,HOST", "bug8027289Test", "FFE5", "", "");
148             testRun("COMPAT,HOST", "bug8027289Test", "FFE5", "", "");
149             testRun("HOST", "bug8027289Test", "00A5", "", "");
150         }
151     }
152 
testRun(String prefList, String methodName, String param1, String param2, String param3)153     private static void testRun(String prefList, String methodName,
154             String param1, String param2, String param3) throws Throwable{
155         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("java");
156         launcher.addToolArg("-cp")
157                 .addToolArg(Utils.TEST_CLASS_PATH)
158                 .addToolArg("-Djava.locale.providers=" + prefList)
159                 .addToolArg("LocaleProviders")
160                 .addToolArg(methodName)
161                 .addToolArg(param1)
162                 .addToolArg(param2)
163                 .addToolArg(param3);
164         int exitCode = ProcessTools.executeCommand(launcher.getCommand())
165                 .getExitValue();
166         if (exitCode != 0) {
167             throw new RuntimeException("Unexpected exit code: " + exitCode);
168         }
169     }
170 }
171