Path: blob/master/test/jdk/tools/launcher/modules/patch/basic/PatchTest.java
41159 views
/*1* Copyright (c) 2015, 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* @modules jdk.compiler27* jdk.naming.dns28* @build PatchTest29* jdk.test.lib.compiler.CompilerUtils30* jdk.test.lib.util.JarUtils31* @run testng PatchTest32* @summary Basic test for --patch-module33*/3435import java.io.File;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.stream.Collectors;40import java.util.stream.Stream;4142import jdk.test.lib.compiler.CompilerUtils;43import jdk.test.lib.util.JarUtils;44import static jdk.test.lib.process.ProcessTools.*;4546import org.testng.annotations.BeforeTest;47import org.testng.annotations.Test;48import static org.testng.Assert.*;4950/**51* Compiles and launches a test that uses --patch-module with two directories52* of classes to override existing classes and add new classes to modules in53* the boot layer.54*55* The classes overridden or added via --patch-module all define a public56* no-arg constructor and override toString to return "hi". This allows the57* launched test to check that the overridden classes are loaded.58*/5960@Test61public class PatchTest {6263// top-level source directory64private static final String TEST_SRC = System.getProperty("test.src");6566// source/destination tree for the test module67private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");68private static final Path MODS_DIR = Paths.get("mods");6970// source/destination tree for patch tree 171private static final Path SRC1_DIR = Paths.get(TEST_SRC, "src1");72private static final Path PATCHES1_DIR = Paths.get("patches1");7374// source/destination tree for patch tree 275private static final Path SRC2_DIR = Paths.get(TEST_SRC, "src2");76private static final Path PATCHES2_DIR = Paths.get("patches2");7778// destination directory for patches packaged as JAR files79private static final Path PATCHES_DIR = Paths.get("patches");808182// the classes overridden or added with --patch-module83private static final String[] CLASSES = {8485// java.base = boot loader86"java.base/java.text.Annotation", // override class87"java.base/java.text.AnnotationBuddy", // add class to package88"java.base/java.lang2.Object", // new package8990// jdk.naming.dns = platform class loader91"jdk.naming.dns/com.sun.jndi.dns.DnsClient",92"jdk.naming.dns/com.sun.jndi.dns.DnsClientBuddy",93"jdk.naming.dns/com.sun.jndi.dns2.Zone",9495// jdk.compiler = application class loaded96"jdk.compiler/com.sun.tools.javac.Main",97"jdk.compiler/com.sun.tools.javac.MainBuddy",98"jdk.compiler/com.sun.tools.javac2.Main",99100};101102103@BeforeTest104public void setup() throws Exception {105106// javac -d mods/test src/test/**107boolean compiled= CompilerUtils.compile(SRC_DIR.resolve("test"),108MODS_DIR.resolve("test"));109assertTrue(compiled, "classes did not compile");110111// javac --patch-module $MODULE=patches1/$MODULE -d patches1/$MODULE patches1/$MODULE/**112// jar cf patches/$MODULE-1.jar -C patches1/$MODULE .113for (Path src : Files.newDirectoryStream(SRC1_DIR)) {114Path output = PATCHES1_DIR.resolve(src.getFileName());115String mn = src.getFileName().toString();116compiled = CompilerUtils.compile(src, output,117"--patch-module", mn + "=" + src.toString());118assertTrue(compiled, "classes did not compile");119JarUtils.createJarFile(PATCHES_DIR.resolve(mn + "-1.jar"), output);120}121122// javac --patch-module $MODULE=patches2/$MODULE -d patches2/$MODULE patches2/$MODULE/**123// jar cf patches/$MODULE-2.jar -C patches2/$MODULE .124for (Path src : Files.newDirectoryStream(SRC2_DIR)) {125Path output = PATCHES2_DIR.resolve(src.getFileName());126String mn = src.getFileName().toString();127compiled = CompilerUtils.compile(src, output,128"--patch-module", mn + "=" + src.toString());129assertTrue(compiled, "classes did not compile");130JarUtils.createJarFile(PATCHES_DIR.resolve(mn + "-2.jar"), output);131}132133}134135/**136* Run test with patches to java.base, jdk.naming.dns and jdk.compiler137*/138void runTest(String basePatches, String dnsPatches, String compilerPatches)139throws Exception140{141// the argument to the test is the list of classes overridden or added142String arg = Stream.of(CLASSES).collect(Collectors.joining(","));143144int exitValue145= executeTestJava("--patch-module", "java.base=" + basePatches,146"--patch-module", "jdk.naming.dns=" + dnsPatches,147"--patch-module", "jdk.compiler=" + compilerPatches,148"--add-exports", "java.base/java.lang2=test",149"--add-exports", "jdk.naming.dns/com.sun.jndi.dns=test",150"--add-exports", "jdk.naming.dns/com.sun.jndi.dns2=test",151"--add-exports", "jdk.compiler/com.sun.tools.javac2=test",152"--add-modules", "jdk.naming.dns,jdk.compiler",153"--module-path", MODS_DIR.toString(),154"-m", "test/jdk.test.Main", arg)155.outputTo(System.out)156.errorTo(System.out)157.getExitValue();158159assertTrue(exitValue == 0);160}161162163/**164* Run test with ---patch-module and exploded patches165*/166public void testWithExplodedPatches() throws Exception {167168// patches1/java.base:patches2/java.base169String basePatches = PATCHES1_DIR.resolve("java.base")170+ File.pathSeparator + PATCHES2_DIR.resolve("java.base");171172String dnsPatches = PATCHES1_DIR.resolve("jdk.naming.dns")173+ File.pathSeparator + PATCHES2_DIR.resolve("jdk.naming.dns");174175String compilerPatches = PATCHES1_DIR.resolve("jdk.compiler")176+ File.pathSeparator + PATCHES2_DIR.resolve("jdk.compiler");177178runTest(basePatches, dnsPatches, compilerPatches);179}180181182/**183* Run test with ---patch-module and patches in JAR files184*/185public void testWithJarPatches() throws Exception {186187// patches/java.base-1.jar:patches/java-base-2.jar188String basePatches = PATCHES_DIR.resolve("java.base-1.jar")189+ File.pathSeparator + PATCHES_DIR.resolve("java.base-2.jar");190191String dnsPatches = PATCHES_DIR.resolve("jdk.naming.dns-1.jar")192+ File.pathSeparator + PATCHES_DIR.resolve("jdk.naming.dns-2.jar");193194String compilerPatches = PATCHES_DIR.resolve("jdk.compiler-1.jar")195+ File.pathSeparator + PATCHES_DIR.resolve("jdk.compiler-2.jar");196197runTest(basePatches, dnsPatches, compilerPatches);198199}200201202/**203* Run test with ---patch-module and patches in JAR files and exploded patches204*/205public void testWithJarAndExplodedPatches() throws Exception {206207// patches/java.base-1.jar:patches2/java.base208String basePatches = PATCHES_DIR.resolve("java.base-1.jar")209+ File.pathSeparator + PATCHES2_DIR.resolve("java.base");210211// patches1/jdk.naming.dns:patches/jdk.naming.dns-2.jar212String dnsPatches = PATCHES1_DIR.resolve("jdk.naming.dns")213+ File.pathSeparator + PATCHES_DIR.resolve("jdk.naming.dns-2.jar");214215String compilerPatches = PATCHES1_DIR.resolve("jdk.compiler")216+ File.pathSeparator + PATCHES_DIR.resolve("jdk.compiler-2.jar");217218runTest(basePatches, dnsPatches, compilerPatches);219220}221}222223224