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