< prev index next >

test/runtime/CommandLine/OptionsValidation/common/optionsvalidation/JVMOption.java

Print this page




  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 package optionsvalidation;
  25 
  26 import com.sun.tools.attach.VirtualMachine;
  27 import com.sun.tools.attach.AttachOperationFailedException;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import jdk.test.lib.DynamicVMOption;
  31 import jdk.test.lib.OutputAnalyzer;

  32 import jdk.test.lib.ProcessTools;
  33 import jdk.test.lib.dcmd.CommandExecutor;
  34 import jdk.test.lib.dcmd.JMXExecutor;
  35 import sun.tools.attach.HotSpotVirtualMachine;
  36 
  37 import static optionsvalidation.JVMOptionsUtils.failedMessage;
  38 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
  39 
  40 public abstract class JVMOption {
  41 
  42     /**
  43      * Executor for JCMD
  44      */
  45     private final static CommandExecutor executor = new JMXExecutor();
  46 


  47     /**
  48      * Name of the tested parameter
  49      */
  50     protected String name;
  51 
  52     /**
  53      * Range is defined for option inside VM
  54      */
  55     protected boolean withRange;
  56 
  57     /**
  58      * Prepend string which added before testing option to the command line
  59      */
  60     private final List<String> prepend;
  61     private final StringBuilder prependString;
  62 
  63     protected JVMOption() {
  64         this.prepend = new ArrayList<>();
  65         prependString = new StringBuilder();
  66         withRange = false;
  67     }
  68 
  69     /**
  70      * Create JVM Option with given type and name.
  71      *
  72      * @param type type: "intx", "size_t", "uintx", "uint64_t" or "double"
  73      * @param name name of the option
  74      * @return created JVMOption
  75      */
  76     static JVMOption createVMOption(String type, String name) {
  77         JVMOption parameter;
  78 
  79         switch (type) {

  80             case "intx":
  81             case "size_t":

  82             case "uintx":
  83             case "uint64_t":
  84                 parameter = new IntJVMOption(name, type);
  85                 break;
  86             case "double":
  87                 parameter = new DoubleJVMOption(name);
  88                 break;
  89             default:
  90                 throw new Error("Expected only \"intx\", \"size_t\", \"uintx\", \"uint64_t\","
  91                         + " or \"double\" option types! Got " + type + " type!");

  92         }
  93 
  94         return parameter;
  95     }
  96 
  97     /**
  98      * Add passed options to the prepend options of the option. Prepend options
  99      * will be added before testing option to the command line.
 100      *
 101      * @param options array of prepend options
 102      */
 103     public final void addPrepend(String... options) {
 104         String toAdd;
 105 
 106         for (String option : options) {
 107             if (option.startsWith("-")) {
 108                 toAdd = option;
 109             } else {
 110                 /* Add "-" before parameter name */
 111                 toAdd = "-" + option;


 314             }
 315 
 316             vm.detach();
 317 
 318             option.setValue(origValue);
 319         }
 320 
 321         return failedTests;
 322     }
 323 
 324     /**
 325      * Run java with passed parameter and check the result depending on the
 326      * 'valid' parameter
 327      *
 328      * @param param tested parameter passed to the JVM
 329      * @param valid indicates whether the JVM should fail or not
 330      * @return true - if test passed
 331      * @throws Exception if java process can not be started
 332      */
 333     private boolean runJavaWithParam(String optionValue, boolean valid) throws Exception {
 334         int returnCode;
 335         boolean result = true;
 336         String value = optionValue.substring(optionValue.lastIndexOf("=") + 1);
 337         String fullOptionString = prependString.toString() + optionValue;
 338         List<String> runJava = new ArrayList<>();
 339         OutputAnalyzer out;
 340 




 341         runJava.addAll(prepend);
 342         runJava.add(optionValue);
 343         runJava.add(JVMOptionsUtils.class.getName());
 344 
 345         out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(true, runJava.toArray(new String[0])).start());
 346 
 347         returnCode = out.getExitValue();
 348 
 349         if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
 350             /* Always consider "fatal error" in output as fail */
 351             failedMessage(name, fullOptionString, valid, "JVM output reports a fatal error. JVM exited with code " + returnCode + "!");
 352             printOutputContent(out);
 353             result = false;
 354         } else if (valid == true) {
 355             if ((returnCode != 0) && (returnCode != 1)) {
 356                 failedMessage(name, fullOptionString, valid, "JVM exited with unexpected error code = " + returnCode);
 357                 printOutputContent(out);
 358                 result = false;
 359             } else if ((returnCode == 1) && (out.getOutput().isEmpty() == true)) {
 360                 failedMessage(name, fullOptionString, valid, "JVM exited with error(exitcode == 1)"
 361                         + ", but with empty stdout and stderr. Description of error is needed!");
 362                 result = false;
 363             } else if (out.getOutput().contains("is outside the allowed range")) {
 364                 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\"");
 365                 printOutputContent(out);
 366                 result = false;
 367             }
 368         } else {
 369             // valid == false
 370             if (returnCode == 0) {
 371                 failedMessage(name, fullOptionString, valid, "JVM successfully exit");
 372                 result = false;
 373             } else if (returnCode != 1) {
 374                 failedMessage(name, fullOptionString, valid, "JVM exited with code "
 375                         + returnCode + " which not equal to 1");
 376                 result = false;
 377             } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
 378                 failedMessage(name, fullOptionString, valid, "JVM output does not contain "
 379                         + "expected output \"" + getErrorMessageCommandLine(value) + "\"");
 380                 printOutputContent(out);
 381                 result = false;
 382             }
 383         }
 384 
 385         System.out.println("");
 386 
 387         return result;
 388     }
 389 
 390     /**
 391      * Construct option string with passed value
 392      *
 393      * @param value parameter value
 394      * @return string containing option with passed value
 395      */




  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 package optionsvalidation;
  25 
  26 import com.sun.tools.attach.VirtualMachine;
  27 import com.sun.tools.attach.AttachOperationFailedException;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import jdk.test.lib.DynamicVMOption;
  31 import jdk.test.lib.OutputAnalyzer;
  32 import jdk.test.lib.Platform;
  33 import jdk.test.lib.ProcessTools;
  34 import jdk.test.lib.dcmd.CommandExecutor;
  35 import jdk.test.lib.dcmd.JMXExecutor;
  36 import sun.tools.attach.HotSpotVirtualMachine;
  37 
  38 import static optionsvalidation.JVMOptionsUtils.failedMessage;
  39 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
  40 
  41 public abstract class JVMOption {
  42 
  43     /**
  44      * Executor for JCMD
  45      */
  46     private final static CommandExecutor executor = new JMXExecutor();
  47     
  48     private final static boolean isClient = Platform.isClient();
  49 
  50     /**
  51      * Name of the tested parameter
  52      */
  53     protected String name;
  54 
  55     /**
  56      * Range is defined for option inside VM
  57      */
  58     protected boolean withRange;
  59 
  60     /**
  61      * Prepend string which added before testing option to the command line
  62      */
  63     private final List<String> prepend;
  64     private final StringBuilder prependString;
  65 
  66     protected JVMOption() {
  67         this.prepend = new ArrayList<>();
  68         prependString = new StringBuilder();
  69         withRange = false;
  70     }
  71 
  72     /**
  73      * Create JVM Option with given type and name.
  74      *
  75      * @param type type: "intx", "size_t", "uintx", "uint64_t" or "double"
  76      * @param name name of the option
  77      * @return created JVMOption
  78      */
  79     static JVMOption createVMOption(String type, String name) {
  80         JVMOption parameter;
  81 
  82         switch (type) {
  83             case "int":
  84             case "intx":
  85             case "size_t":
  86             case "uint":
  87             case "uintx":
  88             case "uint64_t":
  89                 parameter = new IntJVMOption(name, type);
  90                 break;
  91             case "double":
  92                 parameter = new DoubleJVMOption(name);
  93                 break;
  94             default:
  95                 throw new Error("Expected only \"int\", \"intx\", \"size_t\", "
  96                         + "\"uint\", \"uintx\", \"uint64_t\", or \"double\" "
  97                         + "option types! Got " + type + " type!");
  98         }
  99 
 100         return parameter;
 101     }
 102 
 103     /**
 104      * Add passed options to the prepend options of the option. Prepend options
 105      * will be added before testing option to the command line.
 106      *
 107      * @param options array of prepend options
 108      */
 109     public final void addPrepend(String... options) {
 110         String toAdd;
 111 
 112         for (String option : options) {
 113             if (option.startsWith("-")) {
 114                 toAdd = option;
 115             } else {
 116                 /* Add "-" before parameter name */
 117                 toAdd = "-" + option;


 320             }
 321 
 322             vm.detach();
 323 
 324             option.setValue(origValue);
 325         }
 326 
 327         return failedTests;
 328     }
 329 
 330     /**
 331      * Run java with passed parameter and check the result depending on the
 332      * 'valid' parameter
 333      *
 334      * @param param tested parameter passed to the JVM
 335      * @param valid indicates whether the JVM should fail or not
 336      * @return true - if test passed
 337      * @throws Exception if java process can not be started
 338      */
 339     private boolean runJavaWithParam(String optionValue, boolean valid) throws Exception {
 340         int exitCode;
 341         boolean result = true;
 342         String value = optionValue.substring(optionValue.lastIndexOf("=") + 1);
 343         String fullOptionString = prependString.toString() + optionValue;
 344         List<String> runJava = new ArrayList<>();
 345         OutputAnalyzer out;
 346 
 347         if (isClient) {
 348             runJava.add("-client");
 349         }
 350         
 351         runJava.addAll(prepend);
 352         runJava.add(optionValue);
 353         runJava.add(JVMOptionsUtils.class.getName());
 354         
 355         out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start());
 356 
 357         exitCode = out.getExitValue();
 358 
 359         if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
 360             /* Always consider "fatal error" in output as fail */
 361             failedMessage(name, fullOptionString, valid, "JVM output reports a fatal error. JVM exited with code " + exitCode + "!");
 362             printOutputContent(out);
 363             result = false;
 364         } else if (valid == true) {
 365             if ((exitCode != 0) && (exitCode != 1)) {
 366                 failedMessage(name, fullOptionString, valid, "JVM exited with unexpected error code = " + exitCode);
 367                 printOutputContent(out);
 368                 result = false;
 369             } else if ((exitCode == 1) && (out.getOutput().isEmpty() == true)) {
 370                 failedMessage(name, fullOptionString, valid, "JVM exited with error(exitcode == 1)"
 371                         + ", but with empty stdout and stderr. Description of error is needed!");
 372                 result = false;
 373             } else if (out.getOutput().contains("is outside the allowed range")) {
 374                 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\"");
 375                 printOutputContent(out);
 376                 result = false;
 377             }
 378         } else {
 379             // valid == false
 380             if (exitCode == 0) {
 381                 failedMessage(name, fullOptionString, valid, "JVM successfully exit");
 382                 result = false;
 383             } else if (exitCode != 1) {
 384                 failedMessage(name, fullOptionString, valid, "JVM exited with code "
 385                         + exitCode + " which not equal to 1");
 386                 result = false;
 387             } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
 388                 failedMessage(name, fullOptionString, valid, "JVM output does not contain "
 389                         + "expected output \"" + getErrorMessageCommandLine(value) + "\"");
 390                 printOutputContent(out);
 391                 result = false;
 392             }
 393         }
 394 
 395         System.out.println("");
 396 
 397         return result;
 398     }
 399 
 400     /**
 401      * Construct option string with passed value
 402      *
 403      * @param value parameter value
 404      * @return string containing option with passed value
 405      */


< prev index next >