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     private static final String SYSTEM_SETTINGS = "Operating System Metrics:";
  69 
  70     static void containsAllOptions(TestResult tr) {
  71         checkContains(tr, VM_SETTINGS);
  72         checkContains(tr, PROP_SETTINGS);
  73         checkContains(tr, LOCALE_SETTINGS);
  74         if (System.getProperty("os.name").contains("Linux")) {
  75             checkContains(tr, SYSTEM_SETTINGS);
  76         }
  77     }
  78 
  79     static void runTestOptionDefault() throws IOException {
  80         String stackSize = "256"; // in kb
  81         if (getArch().equals("ppc64") || getArch().equals("ppc64le")) {
  82             stackSize = "800";










  83         }
  84         TestResult tr = null;
  85         tr = doExec(javaCmd, "-Xms64m", "-Xmx512m",
  86                 "-Xss" + stackSize + "k", "-XshowSettings", "-jar", testJar.getAbsolutePath());


  87         containsAllOptions(tr);
  88         if (!tr.isOK()) {
  89             System.out.println(tr.status);
  90             throw new RuntimeException("test fails");
  91         }
  92         tr = doExec(javaCmd, "-Xms65536k", "-Xmx712m",
  93                 "-Xss" + stackSize + "000", "-XshowSettings", "-jar", testJar.getAbsolutePath());

  94         containsAllOptions(tr);
  95         if (!tr.isOK()) {
  96             System.out.println(tr.status);
  97             throw new RuntimeException("test fails");
  98         }
  99     }
 100 
 101     static void runTestOptionSystem() throws IOException {
 102         TestResult tr = doExec(javaCmd, "-XshowSettings:system");
 103         if (System.getProperty("os.name").contains("Linux")) {
 104             checkNoContains(tr, VM_SETTINGS);
 105             checkNoContains(tr, PROP_SETTINGS);
 106             checkNoContains(tr, LOCALE_SETTINGS);
 107             checkContains(tr, SYSTEM_SETTINGS);
 108         } else {
 109             // -XshowSettings prints all available settings when
 110             // settings argument is not recognized.
 111             containsAllOptions(tr);
 112         }
 113     }
 114 
 115     static void runTestOptionAll() throws IOException {
 116         init();
 117         TestResult tr = null;
 118         tr = doExec(javaCmd, "-XshowSettings:all");
 119         containsAllOptions(tr);
 120     }
 121 
 122     static void runTestOptionVM() throws IOException {
 123         TestResult tr = null;
 124         tr = doExec(javaCmd, "-XshowSettings:vm");
 125         checkContains(tr, VM_SETTINGS);
 126         checkNoContains(tr, PROP_SETTINGS);
 127         checkNoContains(tr, LOCALE_SETTINGS);
 128     }
 129 
 130     static void runTestOptionProperty() throws IOException {
 131         TestResult tr = null;
 132         tr = doExec(javaCmd, "-XshowSettings:properties");
 133         checkNoContains(tr, VM_SETTINGS);
 134         checkContains(tr, PROP_SETTINGS);
 135         checkNoContains(tr, LOCALE_SETTINGS);
 136     }
 137 
 138     static void runTestOptionLocale() throws IOException {
 139         TestResult tr = null;
 140         tr = doExec(javaCmd, "-XshowSettings:locale");
 141         checkNoContains(tr, VM_SETTINGS);
 142         checkNoContains(tr, PROP_SETTINGS);
 143         checkContains(tr, LOCALE_SETTINGS);
 144     }
 145 
 146     static void runTestBadOptions() throws IOException {
 147         TestResult tr = null;
 148         tr = doExec(javaCmd, "-XshowSettingsBadOption");
 149         checkNoContains(tr, VM_SETTINGS);
 150         checkNoContains(tr, PROP_SETTINGS);
 151         checkNoContains(tr, LOCALE_SETTINGS);
 152         checkContains(tr, "Unrecognized option: -XshowSettingsBadOption");
 153     }
 154 
 155     static void runTest7123582() throws IOException {
 156         TestResult tr = null;
 157         tr = doExec(javaCmd, "-XshowSettings", "-version");
 158         if (!tr.isOK()) {
 159             System.out.println(tr.status);
 160             throw new RuntimeException("test fails");
 161         }
 162         containsAllOptions(tr);
 163     }
 164 
 165     public static void main(String... args) {
 166         try {
 167             runTestOptionAll();
 168             runTestOptionDefault();
 169             runTestOptionVM();
 170             runTestOptionProperty();
 171             runTestOptionLocale();
 172             runTestOptionSystem();
 173             runTestBadOptions();
 174             runTest7123582();
 175         } catch (IOException ioe) {
 176             throw new RuntimeException(ioe);
 177         }
 178     }
 179 }
--- EOF ---