1 /*
2  * Copyright (c) 2015, 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 8044767 8139067 8210408
27  * @summary Basic tests for ResourceBundle with modules:
28  *          1) Named module "test" contains resource bundles for root and en,
29  *          and separate named modules "eubundles" and "asiabundles" contain
30  *          other resource bundles.
31  *          2) ResourceBundle.getBundle caller is in named module "test",
32  *          resource bundles are grouped in main (module "mainbundles"),
33  *          EU (module "eubundles"), and Asia (module "asiabundles").
34  *          3) ResourceBundle.getBundle caller is in named module "test" and all
35  *          resource bundles are in single named module "bundles".
36  *          4) ResourceBundle.getBundle caller is in named module "test" and all
37  *          resource bundles in xml format are in single named module "bundles".
38  *          5) Resource bundles in a local named module with no ResourceBundleProviders.
39  * @library /test/lib
40  *          ..
41  * @build jdk.test.lib.JDKToolLauncher
42  *        jdk.test.lib.Utils
43  *        jdk.test.lib.compiler.CompilerUtils
44  *        jdk.test.lib.process.ProcessTools
45  *        ModuleTestUtil
46  * @run testng BasicTest
47  */
48 
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.List;
52 
53 import jdk.test.lib.JDKToolLauncher;
54 import jdk.test.lib.Utils;
55 import jdk.test.lib.compiler.CompilerUtils;
56 import jdk.test.lib.process.ProcessTools;
57 import org.testng.annotations.DataProvider;
58 import org.testng.annotations.Test;
59 
60 import static jdk.test.lib.Asserts.assertEquals;
61 import static org.testng.Assert.assertTrue;
62 
63 @Test
64 public class BasicTest {
65     private static final String SRC_DIR_APPBASIC = "srcAppbasic";
66     private static final String SRC_DIR_APPBASIC2 = "srcAppbasic2";
67     private static final String SRC_DIR_BASIC = "srcBasic";
68     private static final String SRC_DIR_SIMPLE = "srcSimple";
69     private static final String SRC_DIR_XML = "srcXml";
70     private static final String SRC_DIR_MODLOCAL = "srcModlocal";
71 
72     private static final String MODS_DIR_APPBASIC = "modsAppbasic";
73     private static final String MODS_DIR_APPBASIC2 = "modsAppbasic2";
74     private static final String MODS_DIR_BASIC = "modsBasic";
75     private static final String MODS_DIR_SIMPLE = "modsSimple";
76     private static final String MODS_DIR_XML = "modsXml";
77     private static final String MODS_DIR_MODLOCAL = "modsModlocal";
78 
79     private static final String EXTRA_JAR_BASIC = "extra_basic.jar";
80     private static final String EXTRA_JAR_MODLOCAL = "extra_modlocal.jar";
81 
82     private static final List<String> LOCALE_LIST = List.of("de", "fr", "ja",
83             "zh-tw", "en", "de");
84     private static final List<String> LOCALE_LIST_BASIC = List.of("de", "fr",
85             "ja", "ja-jp", "zh-tw", "en", "de", "ja-jp");
86 
87     private static final List<String> MODULE_LIST = List.of("asiabundles",
88             "eubundles", "test");
89     private static final List<String> MODULE_LIST_BASIC = List.of("mainbundles",
90             "asiabundles", "eubundles", "test");
91     private static final List<String> MODULE_LIST_SIMPLE = List.of("bundles", "test");
92 
93     private static final String MAIN = "test/jdk.test.Main";
94 
95     @DataProvider(name = "basicTestData")
basicTestData()96     Object[][] basicTestData() {
97         return new Object[][] {
98                 // Named module "test" contains resource bundles for root and en,
99                 // and separate named modules "eubundles" and "asiabundles"
100                 // contain other resource bundles.
101                 {SRC_DIR_APPBASIC, MODS_DIR_APPBASIC, MODULE_LIST, LOCALE_LIST,
102                         ".properties"},
103                 {SRC_DIR_APPBASIC2, MODS_DIR_APPBASIC2, MODULE_LIST, LOCALE_LIST,
104                         ".properties"},
105 
106                 // Resource bundles are grouped in main (module "mainbundles"),
107                 // EU (module "eubundles"), and Asia (module "asiabundles").
108                 {SRC_DIR_BASIC, MODS_DIR_BASIC, MODULE_LIST_BASIC, LOCALE_LIST_BASIC,
109                         ".properties"},
110 
111                 // All resource bundles are in single named module "bundles".
112                 {SRC_DIR_SIMPLE, MODS_DIR_SIMPLE, MODULE_LIST_SIMPLE, LOCALE_LIST,
113                         ".properties"},
114 
115                 // All resource bundles in xml format are in single named
116                 // module "bundles".
117                 {SRC_DIR_XML, MODS_DIR_XML, MODULE_LIST_SIMPLE, LOCALE_LIST, ".xml"},
118 
119                 // Resource bundles local in named module "test".
120                 {SRC_DIR_MODLOCAL, MODS_DIR_MODLOCAL, List.of("test"), LOCALE_LIST,
121                         ".properties"},
122         };
123     }
124 
125     @Test(dataProvider = "basicTestData")
runBasicTest(String src, String mod, List<String> moduleList, List<String> localeList, String resFormat)126     public void runBasicTest(String src, String mod, List<String> moduleList,
127             List<String> localeList, String resFormat) throws Throwable {
128         Path srcPath = Paths.get(Utils.TEST_SRC, src);
129         Path modPath = Paths.get(Utils.TEST_CLASSES, mod);
130         moduleList.forEach(mn -> ModuleTestUtil.prepareModule(srcPath, modPath,
131                 mn, resFormat));
132         ModuleTestUtil.runModule(modPath.toString(), MAIN, localeList);
133     }
134 
135     @Test
RunBasicTestWithCp()136     public void RunBasicTestWithCp() throws Throwable {
137         Path jarPath = Paths.get(Utils.TEST_CLASSES, EXTRA_JAR_BASIC);
138         Path srcPath = Paths.get(Utils.TEST_SRC, SRC_DIR_BASIC);
139         Path modPath = Paths.get(Utils.TEST_CLASSES, MODS_DIR_BASIC);
140         Path classPath = Paths.get(Utils.TEST_CLASSES).resolve("classes")
141                 .resolve("basic");
142 
143         jarBasic(srcPath, classPath, jarPath);
144         // jdk.test.Main should NOT load bundles from the jar file specified
145         // by the class-path.
146         ModuleTestUtil.runModuleWithCp(jarPath.toString(), modPath.toString(),
147                 MAIN, List.of("es", "vi"), false);
148     }
149 
150     @Test
runModLocalTestWithCp()151     public void runModLocalTestWithCp() throws Throwable {
152         Path jarPath = Paths.get(Utils.TEST_CLASSES, EXTRA_JAR_MODLOCAL);
153         Path srcPath = Paths.get(Utils.TEST_SRC, SRC_DIR_MODLOCAL);
154         Path modPath = Paths.get(Utils.TEST_CLASSES, MODS_DIR_MODLOCAL);
155 
156         jarModLocal(srcPath, jarPath);
157         // jdk.test.Main should load bundles from the jar file specified by
158         // the class-path.
159         ModuleTestUtil.runModuleWithCp(jarPath.toString(), modPath.toString(),
160                 MAIN, List.of("vi"), true);
161     }
162 
163     /**
164      * Create extra_basic.jar to be added to the class path. It contains .class
165      * and .properties resource bundles.
166      */
jarBasic(Path srcPath, Path classPath, Path jarPath)167     private static void jarBasic(Path srcPath, Path classPath, Path jarPath)
168             throws Throwable {
169         boolean compiled = CompilerUtils.compile(srcPath.resolve("extra"),
170                 classPath);
171         assertTrue(compiled, "Compile Java files for extra_basic.jar failed.");
172 
173         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jar");
174         launcher.addToolArg("-cf")
175                 .addToolArg(jarPath.toString())
176                 .addToolArg("-C")
177                 .addToolArg(classPath.toString())
178                 .addToolArg("jdk/test/resources/eu")
179                 .addToolArg("-C")
180                 .addToolArg(srcPath.resolve("extra").toString())
181                 .addToolArg("jdk/test/resources/asia");
182 
183         int exitCode = ProcessTools.executeCommand(launcher.getCommand())
184                 .getExitValue();
185         assertEquals(exitCode, 0, "Create extra_basic.jar failed. "
186                 + "Unexpected exit code: " + exitCode);
187     }
188 
189     /**
190      * Create extra_modlocal.jar to be added to the class path. Expected
191      * properties files are picked up from the class path.
192      */
jarModLocal(Path srcPath, Path jarPath)193     private static void jarModLocal(Path srcPath, Path jarPath) throws Throwable {
194         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jar");
195         launcher.addToolArg("-cf")
196                 .addToolArg(jarPath.toString())
197                 .addToolArg("-C")
198                 .addToolArg(srcPath.resolve("extra").toString())
199                 .addToolArg("jdk/test/resources");
200 
201         int exitCode = ProcessTools.executeCommand(launcher.getCommand())
202                 .getExitValue();
203         assertEquals(exitCode, 0, "Create extra_modlocal.jar failed. "
204                 + "Unexpected exit code: " + exitCode);
205     }
206 }