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 8137317 8139238 8210408 27 * @summary Visibility tests for ResourceBundle.getBundle with and without 28 * an unnamed module argument. 29 * @library /test/lib 30 * .. 31 * @build jdk.test.lib.JDKToolLauncher 32 * jdk.test.lib.Utils 33 * jdk.test.lib.compiler.CompilerUtils 34 * jdk.test.lib.process.ProcessTools 35 * ModuleTestUtil 36 * @run testng VisibilityTest 37 */ 38 39 import java.nio.file.Path; 40 import java.nio.file.Paths; 41 import java.util.List; 42 43 import jdk.test.lib.JDKToolLauncher; 44 import jdk.test.lib.Utils; 45 import jdk.test.lib.process.ProcessTools; 46 import org.testng.annotations.BeforeTest; 47 import org.testng.annotations.DataProvider; 48 import org.testng.annotations.Test; 49 50 import static org.testng.Assert.assertEquals; 51 52 @Test 53 public class VisibilityTest { 54 private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "src"); 55 private static final Path MODS_DIR = Paths.get(Utils.TEST_CLASSES, "mods"); 56 private static final Path CLASSES_DIR = Paths.get(Utils.TEST_CLASSES, "classes"); 57 private static final Path NAMED_BUNDLES_DIR = MODS_DIR.resolve("named.bundles"); 58 private static final Path EXPORTED_NAMED_BUNDLES_DIR = MODS_DIR.resolve("exported.named.bundles"); 59 60 private static final List<String> MODULE_LIST = List.of("embargo", 61 "exported.named.bundles", "named.bundles", "test"); 62 63 @BeforeTest 64 public void prepareTestEnv() throws Throwable { 65 MODULE_LIST.forEach(mn -> ModuleTestUtil.prepareModule(SRC_DIR, 66 MODS_DIR, mn, ".properties")); 67 68 // Prepare resource bundles in an unnamed module 69 ModuleTestUtil.compilePkg(SRC_DIR, CLASSES_DIR, "pkg"); 70 ModuleTestUtil.copyResFiles(SRC_DIR, CLASSES_DIR, "pkg", ".properties"); 71 72 } 73 74 /** 75 * Package jdk.test is in named module "test". 76 * Package jdk.embargo is in named module "embargo". 77 * 78 * jdk.{test,embargo}.TestWithUnnamedModuleArg call: 79 * ResourceBundle.getBundle(basename, classloader.getUnnamedModule()) 80 * where classloader is the TCCL or system class loader. 81 * jdk.{test,embargo}.TestWithNoModuleArg call: 82 * ResourceBundle.getBundle(basename) 83 * 84 * jdk.test.resources[.exported].classes.* are class-based resource bundles. 85 * jdk.test.resources[.exported].props.* are properties file-based resource bundles. 86 * 87 * Packages jdk.test.resources.{classes,props} in named module "named.bundles" 88 * are exported only to named module "test". 89 * Packages jdk.test.resources.exported.{classes,props} in named module 90 * "exported.named.bundle" are exported to unnamed modules. 91 */ 92 93 @DataProvider(name = "RunWithTestResData") 94 Object[][] RunWithTestResData() { 95 return new Object[][] { 96 // Tests using jdk.test.TestWithNoModuleArg and jdk.embargo.TestWithNoModuleArg. 97 // Neither of which specifies an unnamed module with ResourceBundle.getBundle(). 98 99 // jdk.test.resources.{classes,props}.* are available only to 100 // named module "test" by ResourceBundleProvider. 101 {List.of("-p", MODS_DIR.toString(), 102 "-m", "test/jdk.test.TestWithNoModuleArg", 103 "jdk.test.resources.classes.MyResources", "true")}, 104 {List.of("-p", MODS_DIR.toString(), 105 "-m", "test/jdk.test.TestWithNoModuleArg", 106 "jdk.test.resources.props.MyResources", "true")}, 107 {List.of("-p", MODS_DIR.toString(), 108 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 109 "jdk.test.resources.classes.MyResources", "false")}, 110 {List.of("-p", MODS_DIR.toString(), 111 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 112 "jdk.test.resources.props.MyResources", "false")}, 113 114 // Add mods/named.bundles to the class path. 115 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 116 "-p", MODS_DIR.toString(), 117 "-m", "test/jdk.test.TestWithNoModuleArg", 118 "jdk.test.resources.classes.MyResources", "true")}, 119 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 120 "-p", MODS_DIR.toString(), 121 "-m", "test/jdk.test.TestWithNoModuleArg", 122 "jdk.test.resources.props.MyResources", "true")}, 123 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 124 "-p", MODS_DIR.toString(), 125 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 126 "jdk.test.resources.classes.MyResources", "true")}, 127 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 128 "-p", MODS_DIR.toString(), 129 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 130 "jdk.test.resources.props.MyResources", "true")}, 131 132 // Tests using jdk.test.TestWithUnnamedModuleArg and 133 // jdk.embargo.TestWithUnnamedModuleArg. 134 // Both of which specify an unnamed module with ResourceBundle.getBundle. 135 136 // jdk.test.resources.classes is exported to named module "test". 137 // IllegalAccessException is thrown in ResourceBundle.Control.newBundle(). 138 {List.of("-p", MODS_DIR.toString(), 139 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 140 "jdk.test.resources.classes.MyResources", "false")}, 141 142 // jdk.test.resources.props is exported to named module "test". 143 // loader.getResource() doesn't find jdk.test.resources.props.MyResources. 144 {List.of("-p", MODS_DIR.toString(), 145 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 146 "jdk.test.resources.props.MyResources", "false")}, 147 148 // IllegalAccessException is thrown in ResourceBundle.Control.newBundle(). 149 {List.of("-p", MODS_DIR.toString(), 150 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 151 "jdk.test.resources.classes.MyResources", "false")}, 152 153 // jdk.test.resources.props is exported to named module "test". 154 // loader.getResource() doesn't find jdk.test.resources.props.MyResources. 155 {List.of("-p", MODS_DIR.toString(), 156 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 157 "jdk.test.resources.props.MyResources", "false")}, 158 159 // Add mods/named.bundles to the class path. 160 161 // IllegalAccessException is thrown in ResourceBundle.Control.newBundle(). 162 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 163 "-p", MODS_DIR.toString(), 164 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 165 "jdk.test.resources.classes.MyResources", "false")}, 166 167 // loader.getResource() finds jdk.test.resources.exported.props.MyResources. 168 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 169 "-p", MODS_DIR.toString(), 170 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 171 "jdk.test.resources.props.MyResources", "true")}, 172 173 // jdk.test.resources.exported.classes.MyResources is treated 174 // as if the class is in an unnamed module. 175 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 176 "-p", MODS_DIR.toString(), 177 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 178 "jdk.test.resources.classes.MyResources", "true")}, 179 180 // loader.getResource() finds jdk.test.resources.exported.props.MyResources. 181 {List.of("-cp", NAMED_BUNDLES_DIR.toString(), 182 "-p", MODS_DIR.toString(), 183 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 184 "jdk.test.resources.props.MyResources", "true")}, 185 }; 186 } 187 188 @DataProvider(name = "RunWithExportedResData") 189 Object[][] RunWithExportedResData() { 190 return new Object[][] { 191 // Tests using jdk.test.TestWithNoModuleArg and jdk.embargo.TestWithNoModuleArg 192 // neither of which specifies an unnamed module with ResourceBundle.getBundle. 193 194 // None of jdk.test.resources.exported.** is available to the named modules. 195 {List.of("-p", MODS_DIR.toString(), 196 "-m", "test/jdk.test.TestWithNoModuleArg", 197 "jdk.test.resources.exported.classes.MyResources", "false")}, 198 {List.of("-p", MODS_DIR.toString(), 199 "-m", "test/jdk.test.TestWithNoModuleArg", 200 "jdk.test.resources.exported.props.MyResources", "false")}, 201 {List.of("-p", MODS_DIR.toString(), 202 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 203 "jdk.test.resources.exported.classes.MyResources", "false")}, 204 {List.of("-p", MODS_DIR.toString(), 205 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 206 "jdk.test.resources.exported.props.MyResources", "false")}, 207 208 // Add mods/exported.named.bundles to the class path. 209 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 210 "-p", MODS_DIR.toString(), 211 "-m", "test/jdk.test.TestWithNoModuleArg", 212 "jdk.test.resources.exported.classes.MyResources", "true")}, 213 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 214 "-p", MODS_DIR.toString(), 215 "-m", "test/jdk.test.TestWithNoModuleArg", 216 "jdk.test.resources.exported.props.MyResources", "true")}, 217 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 218 "-p", MODS_DIR.toString(), 219 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 220 "jdk.test.resources.exported.classes.MyResources", "true")}, 221 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 222 "-p", MODS_DIR.toString(), 223 "-m", "embargo/jdk.embargo.TestWithNoModuleArg", 224 "jdk.test.resources.exported.props.MyResources", "true")}, 225 226 // Tests using jdk.test.TestWithUnnamedModuleArg and 227 // jdk.embargo.TestWithUnnamedModuleArg which specify 228 // an unnamed module with ResourceBundle.getBundle. 229 230 // loader.loadClass() doesn't find jdk.test.resources.exported.classes.MyResources 231 // and throws a ClassNotFoundException. 232 {List.of("-p", MODS_DIR.toString(), 233 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 234 "jdk.test.resources.exported.classes.MyResources", "false")}, 235 236 // The properties files in jdk.test.resources.exported.props 237 // are not found with loader.getResource(). 238 {List.of("-p", MODS_DIR.toString(), 239 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 240 "jdk.test.resources.exported.props.MyResources", "false")}, 241 242 // loader.loadClass() doesn't find jdk.test.resources.exported.classes.MyResources 243 // and throws a ClassNotFoundException. 244 {List.of("-p", MODS_DIR.toString(), 245 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 246 "jdk.test.resources.exported.classes.MyResources", "false")}, 247 248 // The properties files in jdk.test.resources.exported.props are not found 249 // with loader.getResource(). 250 {List.of("-p", MODS_DIR.toString(), 251 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 252 "jdk.test.resources.exported.props.MyResources", "false")}, 253 254 // Add mods/exported.named.bundles to the class path. 255 256 // jdk.test.resources.exported.classes.MyResources.getModule().isNamed() 257 // returns false. 258 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 259 "-p", MODS_DIR.toString(), 260 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 261 "jdk.test.resources.exported.classes.MyResources", "true")}, 262 263 // loader.getResource() finds jdk.test.resources.exported.props.MyResources. 264 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 265 "-p", MODS_DIR.toString(), 266 "-m", "test/jdk.test.TestWithUnnamedModuleArg", 267 "jdk.test.resources.exported.props.MyResources", "true")}, 268 269 // jdk.test.resources.exported.classes.MyResources.getModule().isNamed() 270 // returns false. 271 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 272 "-p", MODS_DIR.toString(), 273 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 274 "jdk.test.resources.exported.classes.MyResources", "true")}, 275 276 // loader.getResource() finds jdk.test.resources.exported.props.MyResources. 277 {List.of("-cp", EXPORTED_NAMED_BUNDLES_DIR.toString(), 278 "-p", MODS_DIR.toString(), 279 "-m", "embargo/jdk.embargo.TestWithUnnamedModuleArg", 280 "jdk.test.resources.exported.props.MyResources", "true")}, 281 282 }; 283 } 284 285 @DataProvider(name = "RunWithPkgResData") 286 Object[][] RunWithPkgResData() { 287 return new Object[][] { 288 // jdk.pkg.resources.* are in an unnamed module. 289 // jdk.pkg.test.Main calls ResourceBundle.getBundle with an unnamed module. 290 { List.of("-cp", CLASSES_DIR.resolve("pkg").toString(), "jdk.pkg.test.Main", 291 "jdk.pkg.resources.classes.MyResources", "true")}, 292 { List.of("-cp", CLASSES_DIR.resolve("pkg").toString(), "jdk.pkg.test.Main", 293 "jdk.pkg.resources.props.MyResources", "true")}, 294 }; 295 } 296 297 /** 298 * Test cases with jdk.test.resources.* 299 */ 300 @Test(dataProvider = "RunWithTestResData") 301 public void RunWithTestRes(List<String> argsList) throws Throwable { 302 int exitCode = runCmd(argsList); 303 assertEquals(exitCode, 0, "Execution of the tests with " 304 + "jdk.test.resources.* failed. " 305 + "Unexpected exit code: " + exitCode); 306 } 307 308 /** 309 * Test cases with jdk.test.resources.exported.* 310 */ 311 @Test(dataProvider = "RunWithExportedResData") 312 public void RunWithExportedRes(List<String> argsList) throws Throwable { 313 int exitCode = runCmd(argsList); 314 assertEquals(exitCode, 0, "Execution of the tests with " 315 + "jdk.test.resources.exported.* failed. " 316 + "Unexpected exit code: " + exitCode); 317 } 318 319 /** 320 * Test cases with jdk.pkg.resources.* 321 */ 322 @Test(dataProvider = "RunWithPkgResData") 323 public void RunWithPkgRes(List<String> argsList) throws Throwable { 324 int exitCode = runCmd(argsList); 325 assertEquals(exitCode, 0, "Execution of the tests with " 326 + "jdk.pkg.resources.* failed. " 327 + "Unexpected exit code: " + exitCode); 328 } 329 330 private int runCmd(List<String> argsList) throws Throwable { 331 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("java"); 332 argsList.forEach(launcher::addToolArg); 333 334 return ProcessTools.executeCommand(launcher.getCommand()).getExitValue(); 335 } 336 }