Path: blob/master/test/jdk/java/lang/ClassLoader/getResource/automaticmodules/Driver.java
41159 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @library /test/lib26* @build Driver Main27* jdk.test.lib.util.JarUtils28* @run main Driver29* @summary Test ClassLoader.getResourceXXX to locate resources in an automatic30* module31*/3233import java.lang.module.ModuleFinder;34import java.lang.module.ModuleReference;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.jar.Attributes;39import java.util.jar.Manifest;4041import jdk.test.lib.process.ProcessTools;42import jdk.test.lib.util.JarUtils;4344/**45* The driver creates a JAR file containing p/Foo.class, p/foo.properties,46* and p/resources/bar.properties. This ensures there are is a resource in47* a module package and a resource that is not in the module package. The48* test is then launched to locate every resource in the JAR file.49*/5051public class Driver {5253private static final String TEST_CLASSES = System.getProperty("test.classes");5455public static void main(String[] args) throws Exception {56// create content for JAR file57Path dir = Files.createTempDirectory("classes");58Path p = Files.createDirectory(dir.resolve("p"));59Files.createFile(p.resolve("Foo.class"));60Files.createFile(p.resolve("foo.properties"));61Path resources = Files.createDirectory(p.resolve("resources"));62Files.createFile(resources.resolve("bar.properties"));6364// create the JAR file, including a manifest65Path jarFile = Paths.get("library-1.0.jar");66Manifest man = new Manifest();67Attributes attrs = man.getMainAttributes();68attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");69JarUtils.createJarFile(jarFile, man, dir, p);7071// get the module name72ModuleFinder finder = ModuleFinder.of(jarFile);73ModuleReference mref = finder.findAll().stream().findAny().orElse(null);74if (mref == null)75throw new RuntimeException("Module not found!!!");76String name = mref.descriptor().name();7778// launch the test with the JAR file on the module path79if (ProcessTools.executeTestJava("-p", jarFile.toString(),80"--add-modules", name,81"-cp", TEST_CLASSES,82"Main", name)83.outputTo(System.out)84.errorTo(System.out)85.getExitValue() != 0)86throw new RuntimeException("Test failed - see output");87}8889}909192