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