1 /*
   2  * Copyright (c) 2015, 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 /*
  26  * @test
  27  * @summary Test combinations of jigsaw options that affect the use of AppCDS
  28  *
  29  * @requires vm.cds & !vm.graal.enabled
  30  * @library /test/lib ..
  31  * @modules java.base/jdk.internal.misc
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  35  * @compile ../test-classes/Hello.java ../test-classes/HelloMore.java
  36  * @run main JigsawOptionsCombo
  37  */
  38 import jdk.test.lib.compiler.InMemoryJavaCompiler;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 import java.util.ArrayList;
  41 
  42 
  43 // Remaining WORK: TODO:
  44 // 1. test with -m initial-module; waiting for changes from Chris will provide
  45 //    utils to build modules
  46 // 2. Loading classes from Jmod files - waiting on utils
  47 // 3. Loading classes from exploded module dir"
  48 
  49 public class JigsawOptionsCombo {
  50 
  51     public static void main(String[] args) throws Exception {
  52         String source = "package javax.naming.spi; "                +
  53                         "public class NamingManager { "             +
  54                         "    static { "                             +
  55                         "        System.out.println(\"I pass!\"); " +
  56                         "    } "                                    +
  57                         "}";
  58         ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
  59             InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source, "--patch-module=java.naming"),
  60             "mods/java.naming");
  61 
  62         JarBuilder.build("hello", "Hello");
  63         JarBuilder.build("hello_more", "HelloMore");
  64 
  65         (new JigsawOptionsCombo()).runTests();
  66     }
  67 
  68 
  69     private ArrayList<TestCase> testCaseTable = new ArrayList<TestCase>();
  70 
  71     public static String infoDuringDump(String option) {
  72         return "Info: the " + option +
  73             " option is ignored when dumping the shared archive";
  74     }
  75 
  76     public void runTests() throws Exception {
  77 
  78         testCaseTable.add(new TestCase(
  79             "basic: Basic dump and execute, to verify the test plumbing works",
  80             "", "", 0,
  81             "", "", 0) );
  82 
  83         String bcpArg = "-Xbootclasspath/a:" +
  84         TestCommon.getTestJar("hello_more.jar");
  85 
  86         testCaseTable.add(new TestCase(
  87             "Xbootclasspath/a: is OK for both dump and run time",
  88             bcpArg, "", 0,
  89             bcpArg, "", 0) );
  90 
  91         testCaseTable.add(new TestCase(
  92             "module-path-01: --module-path is ignored for dump time",
  93             "--module-path mods",
  94             infoDuringDump("--module-path"), 0,
  95             null, null, 0) );
  96 
  97         testCaseTable.add(new TestCase(
  98             "module-path-02: --module-path is ok for run time",
  99             "", "", 0,
 100             "--module-path mods", "", 0) );
 101 
 102         testCaseTable.add(new TestCase(
 103             "add-modules-01: --add-modules is ok at dump time",
 104             "--add-modules java.management",
 105             "", 0,
 106             null, null, 0) );
 107 
 108         testCaseTable.add(new TestCase(
 109             "add-modules-02: --add-modules is ok at run time",
 110             "", "", 0,
 111             "--add-modules java.management", "", 0) );
 112 
 113         testCaseTable.add(new TestCase(
 114             "limit-modules-01: --limit-modules is ignored at dump time",
 115             "--limit-modules java.base",
 116             infoDuringDump("--limit-modules"), 0,
 117             null, null, 0) );
 118 
 119         testCaseTable.add(new TestCase(
 120             "limit-modules-02: --limit-modules is ok at run time",
 121             "", "", 0,
 122             "--limit-modules java.base", "", 0) );
 123 
 124         testCaseTable.add(new TestCase(
 125             "upgrade-module-path-01: --upgrade-module-path is ignored at dump time",
 126             "--upgrade-module-path mods",
 127             infoDuringDump("--upgrade-module-path"), 0,
 128             null, null, 0) );
 129 
 130         testCaseTable.add(new TestCase(
 131             "-upgrade-module-path-module-path-02: --upgrade-module-path is ok at run time",
 132             "", "", 0,
 133             "--upgrade-module-path mods", "", 0) );
 134 
 135         for (TestCase tc : testCaseTable) tc.execute();
 136     }
 137 
 138 
 139     // class representing a singe test case
 140     public class TestCase {
 141         String description;
 142         String dumpTimeArgs;
 143         String dumpTimeExpectedOutput;
 144         int    dumpTimeExpectedExitValue;
 145         String runTimeArgs;
 146         String runTimeExpectedOutput;
 147         int    runTimeExpectedExitValue;
 148 
 149         private String appJar = TestCommon.getTestJar("hello.jar");
 150         private String appClasses[] = {"Hello"};
 151 
 152 
 153         public TestCase(String description,
 154             String dumpTimeArgs, String dumpTimeExpectedOutput, int dumpTimeExpectedExitValue,
 155             String runTimeArgs, String runTimeExpectedOutput, int runTimeExpectedExitValue) {
 156 
 157             this.description = description;
 158             this.dumpTimeArgs = dumpTimeArgs;
 159             this.dumpTimeExpectedOutput = dumpTimeExpectedOutput;
 160             this.dumpTimeExpectedExitValue = dumpTimeExpectedExitValue;
 161             this.runTimeArgs = runTimeArgs;
 162             this.runTimeExpectedOutput = runTimeExpectedOutput;
 163             this.runTimeExpectedExitValue = runTimeExpectedExitValue;
 164         }
 165 
 166 
 167         public void execute() throws Exception {
 168             System.out.println("Description: " + description);
 169 
 170             // ===== dump step - create the archive
 171             OutputAnalyzer dumpOutput = TestCommon.dump(
 172                 appJar, appClasses, getDumpOptions());
 173 
 174             if (dumpTimeExpectedExitValue == 0) {
 175                 TestCommon.checkDump(dumpOutput, dumpTimeExpectedOutput);
 176             } else {
 177                 dumpOutput.shouldMatch(dumpTimeExpectedOutput);
 178                 dumpOutput.shouldHaveExitValue(dumpTimeExpectedExitValue);
 179             }
 180 
 181             // ===== exec step - use the archive
 182             if (runTimeArgs != null) {
 183                 OutputAnalyzer execOutput = TestCommon.exec(appJar, getRunOptions());
 184 
 185                 if (runTimeExpectedExitValue == 0) {
 186                     TestCommon.checkExec(execOutput, runTimeExpectedOutput, "Hello World");
 187                 } else {
 188                     execOutput.shouldMatch(dumpTimeExpectedOutput);
 189                     execOutput.shouldHaveExitValue(dumpTimeExpectedExitValue);
 190                 }
 191             }
 192         }
 193 
 194 
 195         // dump command line options can be separated by a space
 196         private String[] getDumpOptions() {
 197             return dumpTimeArgs.split(" ");
 198         }
 199 
 200 
 201         // run command line options can be separated by a space
 202         private String[] getRunOptions() {
 203             ArrayList<String> result = new ArrayList<>();
 204 
 205             if (runTimeArgs != "") {
 206                 String splitArgs[] = runTimeArgs.split(" ");
 207                 for (String arg : splitArgs)
 208                     result.add(arg);
 209             }
 210 
 211             result.add("Hello");
 212             return result.toArray(new String[1]);
 213         }
 214     }
 215 }