1 /* 2 * Copyright (c) 2017, 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 * @library /test/lib 27 * @build Driver Main 28 * jdk.test.lib.util.JarUtils 29 * @run main Driver 30 * @summary Test ClassLoader.getResourceXXX to locate resources in an automatic 31 * module 32 */ 33 34 import java.lang.module.ModuleFinder; 35 import java.lang.module.ModuleReference; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.util.jar.Attributes; 40 import java.util.jar.Manifest; 41 42 import jdk.test.lib.process.ProcessTools; 43 import jdk.test.lib.util.JarUtils; 44 45 /** 46 * The driver creates a JAR file containing p/Foo.class, p/foo.properties, 47 * and p/resources/bar.properties. This ensures there are is a resource in 48 * a module package and a resource that is not in the module package. The 49 * test is then launched to locate every resource in the JAR file. 50 */ 51 52 public class Driver { 53 54 private static final String TEST_CLASSES = System.getProperty("test.classes"); 55 56 public static void main(String[] args) throws Exception { 57 // create content for JAR file 58 Path dir = Files.createTempDirectory("classes"); 59 Path p = Files.createDirectory(dir.resolve("p")); 60 Files.createFile(p.resolve("Foo.class")); 61 Files.createFile(p.resolve("foo.properties")); 62 Path resources = Files.createDirectory(p.resolve("resources")); 63 Files.createFile(resources.resolve("bar.properties")); 64 65 // create the JAR file, including a manifest 66 Path jarFile = Paths.get("library-1.0.jar"); 67 Manifest man = new Manifest(); 68 Attributes attrs = man.getMainAttributes(); 69 attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); 70 JarUtils.createJarFile(jarFile, man, dir, p); 71 72 // get the module name 73 ModuleFinder finder = ModuleFinder.of(jarFile); 74 ModuleReference mref = finder.findAll().stream().findAny().orElse(null); 75 if (mref == null) 76 throw new RuntimeException("Module not found!!!"); 77 String name = mref.descriptor().name(); 78 79 // launch the test with the JAR file on the module path 80 if (ProcessTools.executeTestJava("-p", jarFile.toString(), 81 "--add-modules", name, 82 "-cp", TEST_CLASSES, 83 "Main", name) 84 .outputTo(System.out) 85 .errorTo(System.out) 86 .getExitValue() != 0) 87 throw new RuntimeException("Test failed - see output"); 88 } 89 90 }