1 /*
   2  * Copyright (c) 2014, 2015, 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  * @bug 8066670
  27  * @summary Testing -XX:+PrintSharedArchiveAndExit option
  28  * @library /testlibrary
  29  */
  30 
  31 import com.oracle.java.testlibrary.*;
  32 
  33 public class PrintSharedArchiveAndExit {
  34   public static void main(String[] args) throws Exception {
  35     String filename = "./PrintSharedArchiveAndExit.jsa";
  36 
  37     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  38         "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=" + filename, "-Xshare:dump");
  39     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  40     try {
  41       output.shouldContain("Loading classes to share");
  42       output.shouldHaveExitValue(0);
  43 
  44       // (1) With a valid archive
  45       pb = ProcessTools.createJavaProcessBuilder(
  46           "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=" + filename,
  47           "-XX:+PrintSharedArchiveAndExit", "-version");
  48       output = new OutputAnalyzer(pb.start());
  49       output.shouldContain("archive is valid");
  50       output.shouldNotContain("java version");     // Should not print JVM version
  51       output.shouldHaveExitValue(0);               // Should report success in error code.
  52 
  53       pb = ProcessTools.createJavaProcessBuilder(
  54           "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=" + filename,
  55           "-XX:+PrintSharedArchiveAndExit");
  56       output = new OutputAnalyzer(pb.start());
  57       output.shouldContain("archive is valid");
  58       output.shouldNotContain("Usage:");           // Should not print JVM help message
  59       output.shouldHaveExitValue(0);               // Should report success in error code.
  60 
  61       // (2) With an invalid archive (boot class path has been prepended)
  62       pb = ProcessTools.createJavaProcessBuilder(
  63           "-Xbootclasspath/p:foo.jar",
  64           "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=" + filename,
  65           "-XX:+PrintSharedArchiveAndExit", "-version");
  66       output = new OutputAnalyzer(pb.start());
  67       output.shouldContain("archive is invalid");
  68       output.shouldNotContain("java version");     // Should not print JVM version
  69       output.shouldHaveExitValue(1);               // Should report failure in error code.
  70 
  71       pb = ProcessTools.createJavaProcessBuilder(
  72           "-Xbootclasspath/p:foo.jar",
  73           "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=" + filename,
  74           "-XX:+PrintSharedArchiveAndExit");
  75       output = new OutputAnalyzer(pb.start());
  76       output.shouldContain("archive is invalid");
  77       output.shouldNotContain("Usage:");           // Should not print JVM help message
  78       output.shouldHaveExitValue(1);               // Should report failure in error code.
  79     } catch (RuntimeException e) {
  80       e.printStackTrace();
  81       output.shouldContain("Unable to use shared archive");
  82       output.shouldHaveExitValue(1);
  83     }
  84   }
  85 }