1 /*
  2  * Copyright (c) 2014, 2020, 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 CommandLineFlagComboNegative
 27  * @summary Test command line flag combinations that differ between
 28  *          the dump and execute steps, in such way that they cause errors
 29  *          E.g. use compressed oops for creating and archive, but then
 30  *               execute w/o compressed oops
 31  * @requires vm.cds
 32  * @requires vm.bits == 64 & vm.opt.final.UseCompressedOops == true
 33  * @library /test/lib
 34  * @compile test-classes/Hello.java
 35  * @run driver CommandLineFlagComboNegative
 36  */
 37 
 38 import java.util.ArrayList;
 39 import jdk.test.lib.Platform;
 40 import jdk.test.lib.process.OutputAnalyzer;
 41 
 42 public class CommandLineFlagComboNegative {
 43 
 44     private class TestVector {
 45         public String testOptionForDumpStep;
 46         public String testOptionForExecuteStep;
 47         public String expectedErrorMsg;
 48         public int expectedErrorCode;
 49 
 50         public TestVector(String testOptionForDumpStep, String testOptionForExecuteStep,
 51                           String expectedErrorMsg, int expectedErrorCode) {
 52             this.testOptionForDumpStep=testOptionForDumpStep;
 53             this.testOptionForExecuteStep=testOptionForExecuteStep;
 54             this.expectedErrorMsg=expectedErrorMsg;
 55             this.expectedErrorCode=expectedErrorCode;
 56         }
 57     }
 58 
 59     private ArrayList<TestVector> testTable = new ArrayList<TestVector>();
 60 
 61     private void initTestTable() {
 62         testTable.add( new TestVector("-XX:ObjectAlignmentInBytes=8", "-XX:ObjectAlignmentInBytes=16",
 63             "An error has occurred while processing the shared archive file", 1) );
 64         if (!TestCommon.isDynamicArchive()) {
 65             testTable.add( new TestVector("-XX:ObjectAlignmentInBytes=64", "-XX:ObjectAlignmentInBytes=32",
 66                 "An error has occurred while processing the shared archive file", 1) );
 67         }
 68         testTable.add( new TestVector("-XX:+UseCompressedOops", "-XX:-UseCompressedOops",
 69             "The saved state of UseCompressedOops and UseCompressedClassPointers is different from runtime, CDS will be disabled.", 1) );
 70     }
 71 
 72     private void runTests() throws Exception
 73     {
 74         for (TestVector testEntry : testTable) {
 75             System.out.println("CommandLineFlagComboNegative: dump = " + testEntry.testOptionForDumpStep);
 76             System.out.println("CommandLineFlagComboNegative: execute = " + testEntry.testOptionForExecuteStep);
 77 
 78             String appJar = JarBuilder.getOrCreateHelloJar();
 79             OutputAnalyzer dumpOutput = TestCommon.dump(
 80                appJar, new String[] {"Hello"}, testEntry.testOptionForDumpStep);
 81 
 82             TestCommon.checkDump(dumpOutput, "Loading classes to share");
 83 
 84             TestCommon.run(
 85                 "-cp", appJar,
 86                 testEntry.testOptionForExecuteStep,
 87                 "-Xlog:cds", // for checking log message
 88                 "Hello")
 89                 .assertAbnormalExit(output -> {
 90                     output.shouldContain(testEntry.expectedErrorMsg)
 91                           .shouldHaveExitValue(testEntry.expectedErrorCode);
 92                     });
 93         }
 94     }
 95 
 96     public static void main(String[] args) throws Exception {
 97         CommandLineFlagComboNegative thisClass = new CommandLineFlagComboNegative();
 98         thisClass.initTestTable();
 99         thisClass.runTests();
100     }
101 }