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