1 /*
   2  * Copyright (c) 2016, 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  * @summary Make sure --patch-module works when a jar file and a directory is specified for a module
  27  * @library /testlibrary
  28  * @modules java.base/jdk.internal.misc
  29  *          jdk.jartool/sun.tools.jar
  30  * @build BasicJarBuilder
  31  * @compile PatchModule2DirsMain.java
  32  * @run main PatchModuleTestJarDir
  33  */
  34 
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import jdk.test.lib.*;
  38 
  39 public class PatchModuleTestJarDir {
  40     private static String moduleJar;
  41 
  42     public static void main(String[] args) throws Exception {
  43 
  44         // Create a class file in the module java.naming. This class file
  45         // will be put in the javanaming.jar file.
  46         String source = "package javax.naming.spi; "                    +
  47                         "public class NamingManager1 { "                +
  48                         "    static { "                                 +
  49                         "        System.out.println(\"I pass one!\"); " +
  50                         "    } "                                        +
  51                         "}";
  52 
  53         ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager1",
  54              InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager1", source, "-Xmodule:java.naming"),
  55              System.getProperty("test.classes"));
  56 
  57         // Build the jar file that will be used for the module "java.naming".
  58         BasicJarBuilder.build("javanaming", "javax/naming/spi/NamingManager1");
  59         moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");
  60 
  61         // Just to make sure we are not fooled by the class file being on the
  62         // class path where all the test classes are stored, write the NamingManager.class
  63         // file out again with output that does not contain what OutputAnalyzer
  64         // expects. This will provide confidence that the contents of the class
  65         // is truly coming from the jar file and not the class file.
  66         source = "package javax.naming.spi; "                +
  67                  "public class NamingManager1 { "            +
  68                  "    static { "                             +
  69                  "        System.out.println(\"Fail!\"); "   +
  70                  "    } "                                    +
  71                  "}";
  72 
  73         ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager1",
  74              InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager1", source, "-Xmodule:java.naming"),
  75              System.getProperty("test.classes"));
  76 
  77         // Create a second class file in the module java.naming. This class file
  78         // will be put in the mods/java.naming directory.
  79         source = "package javax.naming.spi; "                    +
  80                  "public class NamingManager2 { "                +
  81                  "    static { "                                 +
  82                  "        System.out.println(\"I pass two!\"); " +
  83                  "    } "                                        +
  84                  "}";
  85 
  86         ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager2",
  87              InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager2", source, "-Xmodule:java.naming"),
  88              (System.getProperty("test.classes") + "/mods/java.naming"));
  89 
  90 
  91         // Supply --patch-module with the name of the jar file for the module java.naming.
  92         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("--patch-module=java.naming=" +
  93                                                                            moduleJar +
  94                                                                            File.pathSeparator +
  95                                                                            System.getProperty("test.classes") + "/mods/java.naming",
  96                                                                   "PatchModule2DirsMain",
  97                                                                   "javax.naming.spi.NamingManager1",
  98                                                                   "javax.naming.spi.NamingManager2");
  99 
 100         new OutputAnalyzer(pb.start())
 101             .shouldContain("I pass one!")
 102             .shouldContain("I pass two!")
 103             .shouldHaveExitValue(0);
 104     }
 105 }