< prev index next >

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

Print this page




   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 package optionsvalidation;
  24 
  25 import com.sun.tools.attach.VirtualMachine;
  26 import com.sun.tools.attach.AttachOperationFailedException;
  27 import java.util.ArrayList;


  28 import java.util.List;

  29 import jdk.test.lib.DynamicVMOption;
  30 import jdk.test.lib.OutputAnalyzer;
  31 import jdk.test.lib.ProcessTools;
  32 import jdk.test.lib.dcmd.CommandExecutor;
  33 import jdk.test.lib.dcmd.JMXExecutor;
  34 import sun.tools.attach.HotSpotVirtualMachine;
  35 
  36 import static optionsvalidation.JVMOptionsUtils.failedMessage;
  37 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
  38 import static optionsvalidation.JVMOptionsUtils.VMType;
  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      * Test valid min range value and additional small values
  59      */
  60     protected boolean testMinRange;
  61 
  62     /**
  63      * Test valid max range value and additional big values
  64      */
  65     protected boolean testMaxRange;
  66 


  67     /**
  68      * Prepend string which added before testing option to the command line
  69      */
  70     private final List<String> prepend;
  71     private final StringBuilder prependString;
  72 
  73     protected JVMOption() {
  74         this.prepend = new ArrayList<>();
  75         prependString = new StringBuilder();



  76         withRange = false;
  77         testMinRange = true;
  78         testMaxRange = true;
  79     }
  80 
  81     /**
  82      * Create JVM Option with given type and name.
  83      *
  84      * @param type type: "intx", "size_t", "uintx", "uint64_t" or "double"
  85      * @param name name of the option
  86      * @return created JVMOption
  87      */
  88     static JVMOption createVMOption(String type, String name) {
  89         JVMOption parameter;
  90 
  91         switch (type) {
  92             case "int":
  93             case "intx":
  94             case "size_t":
  95             case "uint":


 144      * Mark this option as option which range is defined inside VM
 145      */
 146     final void optionWithRange() {
 147         withRange = true;
 148     }
 149 
 150     /**
 151      * Exclude testing of min range value for this option
 152      */
 153     public final void excludeTestMinRange() {
 154         testMinRange = false;
 155     }
 156 
 157     /**
 158      * Exclude testing of max range value for this option
 159      */
 160     public final void excludeTestMaxRange() {
 161         testMaxRange = false;
 162     }
 163 




 164     /**
 165      * Set new minimum option value
 166      *
 167      * @param min new minimum value
 168      */
 169     abstract void setMin(String min);
 170 
 171     /**
 172      * Get string with minimum value of the option
 173      *
 174      * @return string with minimum value of the option
 175      */
 176     abstract String getMin();
 177 
 178     /**
 179      * Set new maximum option value
 180      *
 181      * @param max new maximum value
 182      */
 183     abstract void setMax(String min);


 367         List<String> runJava = new ArrayList<>();
 368         OutputAnalyzer out;
 369 
 370         if (VMType != null) {
 371             runJava.add(VMType);
 372         }
 373         runJava.addAll(prepend);
 374         runJava.add(optionValue);
 375         runJava.add(JVMOptionsUtils.class.getName());
 376 
 377         out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start());
 378 
 379         exitCode = out.getExitValue();
 380 
 381         if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
 382             /* Always consider "fatal error" in output as fail */
 383             failedMessage(name, fullOptionString, valid, "JVM output reports a fatal error. JVM exited with code " + exitCode + "!");
 384             printOutputContent(out);
 385             result = false;
 386         } else if (valid == true) {
 387             if ((exitCode != 0) && (exitCode != 1)) {
 388                 failedMessage(name, fullOptionString, valid, "JVM exited with unexpected error code = " + exitCode);
 389                 printOutputContent(out);
 390                 result = false;
 391             } else if ((exitCode == 1) && (out.getOutput().isEmpty() == true)) {
 392                 failedMessage(name, fullOptionString, valid, "JVM exited with error(exitcode == 1)"
 393                         + ", but with empty stdout and stderr. Description of error is needed!");
 394                 result = false;
 395             } else if (out.getOutput().contains("is outside the allowed range")) {
 396                 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\"");
 397                 printOutputContent(out);
 398                 result = false;
 399             }
 400         } else {
 401             // valid == false
 402             if (exitCode == 0) {
 403                 failedMessage(name, fullOptionString, valid, "JVM successfully exit");
 404                 result = false;
 405             } else if (exitCode != 1) {
 406                 failedMessage(name, fullOptionString, valid, "JVM exited with code "
 407                         + exitCode + " which not equal to 1");
 408                 result = false;
 409             } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
 410                 failedMessage(name, fullOptionString, valid, "JVM output does not contain "
 411                         + "expected output \"" + getErrorMessageCommandLine(value) + "\"");
 412                 printOutputContent(out);
 413                 result = false;




   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 package optionsvalidation;
  24 
  25 import com.sun.tools.attach.VirtualMachine;
  26 import com.sun.tools.attach.AttachOperationFailedException;
  27 import java.util.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.HashSet;
  30 import java.util.List;
  31 import java.util.Set;
  32 import jdk.test.lib.DynamicVMOption;
  33 import jdk.test.lib.OutputAnalyzer;
  34 import jdk.test.lib.ProcessTools;
  35 import jdk.test.lib.dcmd.CommandExecutor;
  36 import jdk.test.lib.dcmd.JMXExecutor;
  37 import sun.tools.attach.HotSpotVirtualMachine;
  38 
  39 import static optionsvalidation.JVMOptionsUtils.failedMessage;
  40 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
  41 import static optionsvalidation.JVMOptionsUtils.VMType;
  42 
  43 public abstract class JVMOption {
  44 
  45     /**
  46      * Executor for JCMD
  47      */
  48     private final static CommandExecutor executor = new JMXExecutor();
  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      * Test valid min range value and additional small values
  62      */
  63     protected boolean testMinRange;
  64 
  65     /**
  66      * Test valid max range value and additional big values
  67      */
  68     protected boolean testMaxRange;
  69 
  70     private Set<Integer> allowedExitCodes;
  71 
  72     /**
  73      * Prepend string which added before testing option to the command line
  74      */
  75     private final List<String> prepend;
  76     private final StringBuilder prependString;
  77 
  78     protected JVMOption() {
  79         this.prepend = new ArrayList<>();
  80         prependString = new StringBuilder();
  81         allowedExitCodes = new HashSet<>();
  82         allowedExitCodes.add(0);
  83         allowedExitCodes.add(1);
  84         withRange = false;
  85         testMinRange = true;
  86         testMaxRange = true;
  87     }
  88 
  89     /**
  90      * Create JVM Option with given type and name.
  91      *
  92      * @param type type: "intx", "size_t", "uintx", "uint64_t" or "double"
  93      * @param name name of the option
  94      * @return created JVMOption
  95      */
  96     static JVMOption createVMOption(String type, String name) {
  97         JVMOption parameter;
  98 
  99         switch (type) {
 100             case "int":
 101             case "intx":
 102             case "size_t":
 103             case "uint":


 152      * Mark this option as option which range is defined inside VM
 153      */
 154     final void optionWithRange() {
 155         withRange = true;
 156     }
 157 
 158     /**
 159      * Exclude testing of min range value for this option
 160      */
 161     public final void excludeTestMinRange() {
 162         testMinRange = false;
 163     }
 164 
 165     /**
 166      * Exclude testing of max range value for this option
 167      */
 168     public final void excludeTestMaxRange() {
 169         testMaxRange = false;
 170     }
 171 
 172     public final void setAllowedExitCodes(Integer... allowedExitCodes) {
 173         this.allowedExitCodes.addAll(Arrays.asList(allowedExitCodes));
 174     }
 175 
 176     /**
 177      * Set new minimum option value
 178      *
 179      * @param min new minimum value
 180      */
 181     abstract void setMin(String min);
 182 
 183     /**
 184      * Get string with minimum value of the option
 185      *
 186      * @return string with minimum value of the option
 187      */
 188     abstract String getMin();
 189 
 190     /**
 191      * Set new maximum option value
 192      *
 193      * @param max new maximum value
 194      */
 195     abstract void setMax(String min);


 379         List<String> runJava = new ArrayList<>();
 380         OutputAnalyzer out;
 381 
 382         if (VMType != null) {
 383             runJava.add(VMType);
 384         }
 385         runJava.addAll(prepend);
 386         runJava.add(optionValue);
 387         runJava.add(JVMOptionsUtils.class.getName());
 388 
 389         out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start());
 390 
 391         exitCode = out.getExitValue();
 392 
 393         if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
 394             /* Always consider "fatal error" in output as fail */
 395             failedMessage(name, fullOptionString, valid, "JVM output reports a fatal error. JVM exited with code " + exitCode + "!");
 396             printOutputContent(out);
 397             result = false;
 398         } else if (valid == true) {
 399             if (!allowedExitCodes.contains(exitCode)) {
 400                 failedMessage(name, fullOptionString, valid, "JVM exited with unexpected error code = " + exitCode);
 401                 printOutputContent(out);
 402                 result = false;
 403             } else if ((exitCode != 0) && (out.getOutput().isEmpty() == true)) {
 404                 failedMessage(name, fullOptionString, valid, "JVM exited with error(exitcode == " + exitCode +
 405                         + "), but with empty stdout and stderr. Description of error is needed!");
 406                 result = false;
 407             } else if (out.getOutput().contains("is outside the allowed range")) {
 408                 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\"");
 409                 printOutputContent(out);
 410                 result = false;
 411             }
 412         } else {
 413             // valid == false
 414             if (exitCode == 0) {
 415                 failedMessage(name, fullOptionString, valid, "JVM successfully exit");
 416                 result = false;
 417             } else if (exitCode != 1) {
 418                 failedMessage(name, fullOptionString, valid, "JVM exited with code "
 419                         + exitCode + " which not equal to 1");
 420                 result = false;
 421             } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
 422                 failedMessage(name, fullOptionString, valid, "JVM output does not contain "
 423                         + "expected output \"" + getErrorMessageCommandLine(value) + "\"");
 424                 printOutputContent(out);
 425                 result = false;


< prev index next >