test/testlibrary/com/oracle/java/testlibrary/cli/CommandLineOptionTest.java

Print this page

        

*** 47,94 **** = " VM option '%s' is diagnostic and must be enabled via " + "-XX:\\+UnlockDiagnosticVMOptions."; private static final String PRINT_FLAGS_FINAL_FORMAT = "%s\\s*:?=\\s*%s"; /** ! * Verifies that JVM startup behaviour matches our expectations. * * @param option an option that should be passed to JVM * @param expectedMessages an array of patterns that should occur * in JVM output. If {@code null} then * JVM output could be empty. * @param unexpectedMessages an array of patterns that should not * occur in JVM output. If {@code null} then * JVM output could be empty. * @param exitCode expected exit code. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyJVMStartup(String option, String expectedMessages[], String unexpectedMessages[], ! ExitCode exitCode) throws Throwable { CommandLineOptionTest.verifyJVMStartup(expectedMessages, ! unexpectedMessages, exitCode, false, option); } /** ! * Verifies that JVM startup behaviour matches our expectations. * * @param expectedMessages an array of patterns that should occur * in JVM output. If {@code null} then * JVM output could be empty. * @param unexpectedMessages an array of patterns that should not * occur in JVM output. If {@code null} then * JVM output could be empty. * @param exitCode expected exit code. * @param addTestVMOptions if {@code true} then test VM options will be * passed to VM. * @param options options that should be passed to VM in addition to mode * flag. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyJVMStartup(String expectedMessages[], String unexpectedMessages[], ExitCode exitCode, ! boolean addTestVMOptions, String... options) throws Throwable { List<String> finalOptions = new ArrayList<>(); if (addTestVMOptions) { Collections.addAll(finalOptions, Utils.getTestJavaOpts()); } Collections.addAll(finalOptions, options); --- 47,107 ---- = " VM option '%s' is diagnostic and must be enabled via " + "-XX:\\+UnlockDiagnosticVMOptions."; private static final String PRINT_FLAGS_FINAL_FORMAT = "%s\\s*:?=\\s*%s"; /** ! * Verifies that JVM startup behavior matches our expectations. * * @param option an option that should be passed to JVM * @param expectedMessages an array of patterns that should occur * in JVM output. If {@code null} then * JVM output could be empty. * @param unexpectedMessages an array of patterns that should not * occur in JVM output. If {@code null} then * JVM output could be empty. * @param exitCode expected exit code. + * @param exitErrorMessage message that will be shown if exit code is not + * as expected. + * @param wrongWarningMessage message that will be shown if warning + * messages are not as expected. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyJVMStartup(String option, String expectedMessages[], String unexpectedMessages[], ! ExitCode exitCode, String exitErrorMessage, ! String wrongWarningMessage) ! throws Throwable { CommandLineOptionTest.verifyJVMStartup(expectedMessages, ! unexpectedMessages, exitCode, exitErrorMessage, ! wrongWarningMessage, false, option); } /** ! * Verifies that JVM startup behavior matches our expectations. * * @param expectedMessages an array of patterns that should occur * in JVM output. If {@code null} then * JVM output could be empty. * @param unexpectedMessages an array of patterns that should not * occur in JVM output. If {@code null} then * JVM output could be empty. * @param exitCode expected exit code. + * @param exitErrorMessage message that will be shown if exit code is not + * as expected. + * @param wrongWarningMessage message that will be shown if warning + * messages are not as expected. * @param addTestVMOptions if {@code true} then test VM options will be * passed to VM. * @param options options that should be passed to VM in addition to mode * flag. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyJVMStartup(String expectedMessages[], String unexpectedMessages[], ExitCode exitCode, ! String exitErrorMessage, String wrongWarningMessage, ! boolean addTestVMOptions, String... options) ! throws Throwable { List<String> finalOptions = new ArrayList<>(); if (addTestVMOptions) { Collections.addAll(finalOptions, Utils.getTestJavaOpts()); } Collections.addAll(finalOptions, options);
*** 97,145 **** ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(finalOptions.toArray( new String[finalOptions.size()])); OutputAnalyzer outputAnalyzer = new OutputAnalyzer(processBuilder.start()); outputAnalyzer.shouldHaveExitValue(exitCode.value); if (expectedMessages != null) { for (String expectedMessage : expectedMessages) { outputAnalyzer.shouldMatch(expectedMessage); } } if (unexpectedMessages != null) { for (String unexpectedMessage : unexpectedMessages) { outputAnalyzer.shouldNotMatch(unexpectedMessage); } } } /** ! * Verifies that JVM startup behaviour matches our expectations when type * of newly started VM is the same as the type of current. * * @param expectedMessages an array of patterns that should occur * in JVM output. If {@code null} then * JVM output could be empty. * @param unexpectedMessages an array of patterns that should not * occur in JVM output. If {@code null} then * JVM output could be empty. * @param exitCode expected exit code. * @param options options that should be passed to VM in addition to mode * flag. * @throws Throwable if verification fails or some other issues occur. */ public static void verifySameJVMStartup(String expectedMessages[], ! String unexpectedMessages[], ExitCode exitCode, String... options) throws Throwable { List<String> finalOptions = new ArrayList<>(); finalOptions.add(CommandLineOptionTest.getVMTypeOption()); Collections.addAll(finalOptions, options); CommandLineOptionTest.verifyJVMStartup(expectedMessages, ! unexpectedMessages, exitCode, false, finalOptions.toArray(new String[finalOptions.size()])); } /** * Verifies that value of specified JVM option is the same as --- 110,187 ---- ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(finalOptions.toArray( new String[finalOptions.size()])); OutputAnalyzer outputAnalyzer = new OutputAnalyzer(processBuilder.start()); + + try { outputAnalyzer.shouldHaveExitValue(exitCode.value); + } catch (RuntimeException e) { + String errorMessage = String.format( + "JVMStartup should have exit value '%d'.%n%s", + exitCode.value, exitErrorMessage); + throw new AssertionError(errorMessage, e); + } + if (expectedMessages != null) { for (String expectedMessage : expectedMessages) { + try { outputAnalyzer.shouldMatch(expectedMessage); + } catch (RuntimeException e) { + String errorMessage = String.format( + "Expected message not found: '%s'.%n%s", + expectedMessage, wrongWarningMessage); + throw new AssertionError(errorMessage, e); + } } } if (unexpectedMessages != null) { for (String unexpectedMessage : unexpectedMessages) { + try { outputAnalyzer.shouldNotMatch(unexpectedMessage); + } catch (RuntimeException e) { + String errorMessage = String.format( + "Unexpected message found: '%s'.%n%s", + unexpectedMessage, wrongWarningMessage); + throw new AssertionError(errorMessage, e); + } } } } /** ! * Verifies that JVM startup behavior matches our expectations when type * of newly started VM is the same as the type of current. * * @param expectedMessages an array of patterns that should occur * in JVM output. If {@code null} then * JVM output could be empty. * @param unexpectedMessages an array of patterns that should not * occur in JVM output. If {@code null} then * JVM output could be empty. * @param exitCode expected exit code. + * @param exitErrorMessage Message that will be shown if exit value is not + * as expected. + * @param wrongWarningMessage message that will be shown if warning + * messages are not as expected. * @param options options that should be passed to VM in addition to mode * flag. * @throws Throwable if verification fails or some other issues occur. */ public static void verifySameJVMStartup(String expectedMessages[], ! String unexpectedMessages[], String exitErrorMessage, ! String wrongWarningMessage, ExitCode exitCode, String... options) throws Throwable { List<String> finalOptions = new ArrayList<>(); finalOptions.add(CommandLineOptionTest.getVMTypeOption()); Collections.addAll(finalOptions, options); CommandLineOptionTest.verifyJVMStartup(expectedMessages, ! unexpectedMessages, exitCode, exitErrorMessage, ! wrongWarningMessage, false, finalOptions.toArray(new String[finalOptions.size()])); } /** * Verifies that value of specified JVM option is the same as
*** 147,163 **** * This method filter out option with {@code optionName} * name from test java options. * * @param optionName a name of tested option. * @param expectedValue expected value of tested option. * @param additionalVMOpts additional options that should be * passed to JVM. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyOptionValue(String optionName, ! String expectedValue, String... additionalVMOpts) throws Throwable { ! verifyOptionValue(optionName, expectedValue, true, additionalVMOpts); } /** * Verifies that value of specified JVM option is the same as * expected value. --- 189,209 ---- * This method filter out option with {@code optionName} * name from test java options. * * @param optionName a name of tested option. * @param expectedValue expected value of tested option. + * @param optionErrorString message will be shown if option value is not as + * expected. * @param additionalVMOpts additional options that should be * passed to JVM. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyOptionValue(String optionName, ! String expectedValue, String optionErrorString, ! String... additionalVMOpts) throws Throwable { ! verifyOptionValue(optionName, expectedValue, optionErrorString, ! true, additionalVMOpts); } /** * Verifies that value of specified JVM option is the same as * expected value.
*** 166,183 **** * * @param optionName a name of tested option. * @param expectedValue expected value of tested option. * @param addTestVmOptions if {@code true}, then test VM options * will be used. * @param additionalVMOpts additional options that should be * passed to JVM. * @throws Throwable if verification fails or some other issues * occur. */ public static void verifyOptionValue(String optionName, ! String expectedValue, boolean addTestVmOptions, ! String... additionalVMOpts) throws Throwable { List<String> vmOpts = new ArrayList<>(); if (addTestVmOptions) { Collections.addAll(vmOpts, Utils.getFilteredTestJavaOpts(optionName)); --- 212,232 ---- * * @param optionName a name of tested option. * @param expectedValue expected value of tested option. * @param addTestVmOptions if {@code true}, then test VM options * will be used. + * @param optionErrorString message will be shown if option value is not as + * expected. * @param additionalVMOpts additional options that should be * passed to JVM. * @throws Throwable if verification fails or some other issues * occur. */ public static void verifyOptionValue(String optionName, ! String expectedValue, String optionErrorString, ! boolean addTestVmOptions, String... additionalVMOpts) ! throws Throwable { List<String> vmOpts = new ArrayList<>(); if (addTestVmOptions) { Collections.addAll(vmOpts, Utils.getFilteredTestJavaOpts(optionName));
*** 189,202 **** --- 238,266 ---- vmOpts.toArray(new String[vmOpts.size()])); OutputAnalyzer outputAnalyzer = new OutputAnalyzer(processBuilder.start()); + try { outputAnalyzer.shouldHaveExitValue(0); + } catch (RuntimeException e) { + String errorMessage = String.format( + "JVMStartup should be successful with option '%s'", + optionName); + throw new AssertionError(errorMessage, e); + } + try { outputAnalyzer.shouldMatch(String.format( CommandLineOptionTest.PRINT_FLAGS_FINAL_FORMAT, optionName, expectedValue)); + } catch (RuntimeException e) { + String errorMessage = String.format( + "Option '%s' is expected to have '%s' value%n%s", + optionName, expectedValue, + optionErrorString); + throw new AssertionError(errorMessage, e); + } } /** * Verifies that value of specified JVM when type of newly started VM * is the same as the type of current.
*** 205,226 **** * Only mode flag will be passed to VM in addition to * {@code additionalVMOpts} * * @param optionName name of tested option. * @param expectedValue expected value of tested option. * @param additionalVMOpts additional options that should be * passed to JVM. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyOptionValueForSameVM(String optionName, ! String expectedValue, String... additionalVMOpts) throws Throwable { List<String> finalOptions = new ArrayList<>(); finalOptions.add(CommandLineOptionTest.getVMTypeOption()); Collections.addAll(finalOptions, additionalVMOpts); CommandLineOptionTest.verifyOptionValue(optionName, expectedValue, ! false, finalOptions.toArray(new String[finalOptions.size()])); } /** * Prepares boolean command line flag with name {@code name} according * to it's {@code value}. --- 269,293 ---- * Only mode flag will be passed to VM in addition to * {@code additionalVMOpts} * * @param optionName name of tested option. * @param expectedValue expected value of tested option. + * @param optionErrorString message to show if option has another value * @param additionalVMOpts additional options that should be * passed to JVM. * @throws Throwable if verification fails or some other issues occur. */ public static void verifyOptionValueForSameVM(String optionName, ! String expectedValue, String optionErrorString, ! String... additionalVMOpts) throws Throwable { List<String> finalOptions = new ArrayList<>(); finalOptions.add(CommandLineOptionTest.getVMTypeOption()); Collections.addAll(finalOptions, additionalVMOpts); CommandLineOptionTest.verifyOptionValue(optionName, expectedValue, ! optionErrorString, false, ! finalOptions.toArray(new String[finalOptions.size()])); } /** * Prepares boolean command line flag with name {@code name} according * to it's {@code value}.