1 /*
   2  * Copyright (c) 2010, 2012, 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 import java.io.File;
  24 import java.io.IOException;
  25 
  26 /*
  27  * @test
  28  * @bug 6994753 7123582
  29  * @summary tests -XshowSettings options
  30  * @compile -XDignore.symbol.file Settings.java
  31  * @run main Settings
  32  * @author ksrini
  33  */
  34 public class Settings extends TestHelper {
  35     private static File testJar = null;
  36 
  37     static void init() throws IOException {
  38         if  (testJar != null) {
  39             return;
  40         }
  41         testJar = new File("test.jar");
  42         StringBuilder tsrc = new StringBuilder();
  43         tsrc.append("public static void main(String... args) {\n");
  44         tsrc.append("   for (String x : args) {\n");
  45         tsrc.append("        System.out.println(x);\n");
  46         tsrc.append("   }\n");
  47         tsrc.append("}\n");
  48         createJar(testJar, tsrc.toString());
  49     }
  50 
  51     static void checkContains(TestResult tr, String str) {
  52         if (!tr.contains(str)) {
  53             System.out.println(tr);
  54             throw new RuntimeException(str + " not found");
  55         }
  56     }
  57 
  58     static void checkNoContains(TestResult tr, String str) {
  59         if (tr.contains(str)) {
  60             System.out.println(tr.status);
  61             throw new RuntimeException(str + " found");
  62         }
  63     }
  64 
  65     private static final String VM_SETTINGS = "VM settings:";
  66     private static final String PROP_SETTINGS = "Property settings:";
  67     private static final String LOCALE_SETTINGS = "Locale settings:";
  68 
  69     static void containsAllOptions(TestResult tr) {
  70         checkContains(tr, VM_SETTINGS);
  71         checkContains(tr, PROP_SETTINGS);
  72         checkContains(tr, LOCALE_SETTINGS);
  73     }
  74 
  75     static void runTestOptionDefault() throws IOException {
  76         String stackSize = "256"; // in kb
  77         if (getArch().equals("ppc64")) {
  78             stackSize = "800";
  79         }
  80         TestResult tr = null;
  81         tr = doExec(javaCmd, "-Xms64m", "-Xmx512m",
  82                 "-Xss" + stackSize + "k", "-XshowSettings", "-jar", testJar.getAbsolutePath());
  83         containsAllOptions(tr);
  84         if (!tr.isOK()) {
  85             System.out.println(tr.status);
  86             throw new RuntimeException("test fails");
  87         }
  88         tr = doExec(javaCmd, "-Xms65536k", "-Xmx712m",
  89                 "-Xss" + stackSize + "000", "-XshowSettings", "-jar", testJar.getAbsolutePath());
  90         containsAllOptions(tr);
  91         if (!tr.isOK()) {
  92             System.out.println(tr.status);
  93             throw new RuntimeException("test fails");
  94         }
  95     }
  96 
  97     static void runTestOptionAll() throws IOException {
  98         init();
  99         TestResult tr = null;
 100         tr = doExec(javaCmd, "-XshowSettings:all");
 101         containsAllOptions(tr);
 102     }
 103 
 104     static void runTestOptionVM() throws IOException {
 105         TestResult tr = null;
 106         tr = doExec(javaCmd, "-XshowSettings:vm");
 107         checkContains(tr, VM_SETTINGS);
 108         checkNoContains(tr, PROP_SETTINGS);
 109         checkNoContains(tr, LOCALE_SETTINGS);
 110     }
 111 
 112     static void runTestOptionProperty() throws IOException {
 113         TestResult tr = null;
 114         tr = doExec(javaCmd, "-XshowSettings:properties");
 115         checkNoContains(tr, VM_SETTINGS);
 116         checkContains(tr, PROP_SETTINGS);
 117         checkNoContains(tr, LOCALE_SETTINGS);
 118     }
 119 
 120     static void runTestOptionLocale() throws IOException {
 121         TestResult tr = null;
 122         tr = doExec(javaCmd, "-XshowSettings:locale");
 123         checkNoContains(tr, VM_SETTINGS);
 124         checkNoContains(tr, PROP_SETTINGS);
 125         checkContains(tr, LOCALE_SETTINGS);
 126     }
 127 
 128     static void runTestBadOptions() throws IOException {
 129         TestResult tr = null;
 130         tr = doExec(javaCmd, "-XshowSettingsBadOption");
 131         checkNoContains(tr, VM_SETTINGS);
 132         checkNoContains(tr, PROP_SETTINGS);
 133         checkNoContains(tr, LOCALE_SETTINGS);
 134         checkContains(tr, "Unrecognized option: -XshowSettingsBadOption");
 135     }
 136 
 137     static void runTest7123582() throws IOException {
 138         TestResult tr = null;
 139         tr = doExec(javaCmd, "-XshowSettings", "-version");
 140         if (!tr.isOK()) {
 141             System.out.println(tr.status);
 142             throw new RuntimeException("test fails");
 143         }
 144         containsAllOptions(tr);
 145     }
 146 
 147     public static void main(String... args) {
 148         try {
 149             runTestOptionAll();
 150             runTestOptionDefault();
 151             runTestOptionVM();
 152             runTestOptionProperty();
 153             runTestOptionLocale();
 154             runTestBadOptions();
 155             runTest7123582();
 156         } catch (IOException ioe) {
 157             throw new RuntimeException(ioe);
 158         }
 159     }
 160 }