1 /*
   2  * Copyright (c) 2015, 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 /lib/testlibrary
  27  * @build PatchTest CompilerUtils jdk.testlibrary.*
  28  * @run testng PatchTest
  29  * @summary Basic test for -Xpatch
  30  */
  31 
  32 import java.io.File;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 import java.util.stream.Collectors;
  37 import java.util.stream.Stream;
  38 
  39 import static jdk.testlibrary.ProcessTools.*;
  40 
  41 import org.testng.annotations.BeforeTest;
  42 import org.testng.annotations.Test;
  43 import static org.testng.Assert.*;
  44 
  45 
  46 /**
  47  * Compiles and launches a test that uses -Xpatch with two directories of
  48  * classes to override existing and add new classes to modules in the
  49  * boot layer.
  50  *
  51  * The classes overridden or added via -Xpatch all define a public no-arg
  52  * constructor and override toString to return "hi". This allows the launched
  53  * test to check that the overridden classes are loaded.
  54  */
  55 
  56 @Test
  57 public class PatchTest {
  58 
  59     // top-level source directory
  60     private static final String TEST_SRC = System.getProperty("test.src");
  61 
  62     // source/destination tree for the test module
  63     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  64     private static final Path MODS_DIR = Paths.get("mods");
  65 
  66     // source/destination tree for patch tree 1
  67     private static final Path SRC1_DIR = Paths.get(TEST_SRC, "src1");
  68     private static final Path PATCHES1_DIR = Paths.get("patches1");
  69 
  70     // source/destination tree for patch tree 2
  71     private static final Path SRC2_DIR = Paths.get(TEST_SRC, "src2");
  72     private static final Path PATCHES2_DIR = Paths.get("patches2");
  73 
  74 
  75     // the classes overridden or added with -Xpatch
  76     private static final String[] CLASSES = {
  77 
  78         // java.base = boot loader
  79         "java.base/java.text.Annotation",           // override class
  80         "java.base/java.text.AnnotationBuddy",      // add class to package
  81         "java.base/java.lang2.Object",              // new package
  82 
  83         // jdk.naming.dns = extension class loader
  84         "jdk.naming.dns/com.sun.jndi.dns.DnsClient",
  85         "jdk.naming.dns/com.sun.jndi.dns.DnsClientBuddy",
  86         "jdk.naming.dns/com.sun.jndi.dns2.Zone",
  87 
  88         // jdk.compiler = application class loaded
  89         "jdk.compiler/com.sun.tools.javac.Main",
  90         "jdk.compiler/com.sun.tools.javac.MainBuddy",
  91         "jdk.compiler/com.sun.tools.javac2.Main",
  92 
  93     };
  94 
  95 
  96     @BeforeTest
  97     public void compile() throws Exception {
  98 
  99         // javac -d mods/test src/test/**
 100         boolean compiled= CompilerUtils.compile(SRC_DIR.resolve("test"),
 101                                                 MODS_DIR.resolve("test"));
 102         assertTrue(compiled, "classes did not compile");
 103 
 104         // javac -Xmodule:$MODULE -d patches1/$MODULE patches1/$MODULE/**
 105         for (Path src : Files.newDirectoryStream(SRC1_DIR)) {
 106             Path output = PATCHES1_DIR.resolve(src.getFileName());
 107             String mn = src.getFileName().toString();
 108             compiled  = CompilerUtils.compile(src, output, "-Xmodule:" + mn);
 109             assertTrue(compiled, "classes did not compile");
 110         }
 111 
 112         // javac -Xmodule:$MODULE -d patches2/$MODULE patches2/$MODULE/**
 113         for (Path src : Files.newDirectoryStream(SRC2_DIR)) {
 114             Path output = PATCHES2_DIR.resolve(src.getFileName());
 115             String mn = src.getFileName().toString();
 116             compiled  = CompilerUtils.compile(src, output, "-Xmodule:" + mn);
 117             assertTrue(compiled, "classes did not compile");
 118         }
 119 
 120     }
 121 
 122     /**
 123      * Run the test with -Xpatch
 124      */
 125     public void testRunWithXPatch() throws Exception {
 126 
 127         // value for -Xpatch
 128         String patchPath = PATCHES1_DIR + File.pathSeparator + PATCHES2_DIR;
 129 
 130         // value for -XaddExports
 131         String addExportsValue = "java.base/java.lang2=test"
 132                 + ",jdk.naming.dns/com.sun.jndi.dns=test"
 133                 + ",jdk.naming.dns/com.sun.jndi.dns2=test"
 134                 + ",jdk.compiler/com.sun.tools.javac2=test";
 135 
 136         // the argument to the test is the list of classes overridden or added
 137         String arg = Stream.of(CLASSES).collect(Collectors.joining(","));
 138 
 139         int exitValue
 140             =  executeTestJava("-Xpatch:" + patchPath,
 141                                "-XaddExports:" + addExportsValue,
 142                                "-addmods", "jdk.naming.dns,jdk.compiler",
 143                                "-mp", MODS_DIR.toString(),
 144                                "-m", "test/jdk.test.Main", arg)
 145                 .outputTo(System.out)
 146                 .errorTo(System.out)
 147                 .getExitValue();
 148 
 149         assertTrue(exitValue == 0);
 150 
 151     }
 152 
 153 }