244 } catch (RuntimeException e) { 245 String errorMessage = String.format( 246 "JVM should start with option '%s' without errors.", 247 optionName); 248 throw new AssertionError(errorMessage, e); 249 } 250 try { 251 outputAnalyzer.shouldMatch(String.format( 252 CommandLineOptionTest.PRINT_FLAGS_FINAL_FORMAT, 253 optionName, expectedValue)); 254 } catch (RuntimeException e) { 255 String errorMessage = String.format( 256 "Option '%s' is expected to have '%s' value%n%s", 257 optionName, expectedValue, 258 optionErrorString); 259 throw new AssertionError(errorMessage, e); 260 } 261 } 262 263 /** 264 * Verifies that value of specified JVM when type of newly started VM 265 * is the same as the type of current. 266 * This method filter out option with {@code optionName} 267 * name from test java options. 268 * Only mode flag will be passed to VM in addition to 269 * {@code additionalVMOpts} 270 * 271 * @param optionName name of tested option. 272 * @param expectedValue expected value of tested option. 273 * @param optionErrorString message to show if option has another value 274 * @param additionalVMOpts additional options that should be 275 * passed to JVM. 276 * @throws Throwable if verification fails or some other issues occur. 277 */ 278 public static void verifyOptionValueForSameVM(String optionName, 279 String expectedValue, String optionErrorString, 280 String... additionalVMOpts) throws Throwable { 281 List<String> finalOptions = new ArrayList<>(); 282 finalOptions.add(CommandLineOptionTest.getVMTypeOption()); 283 Collections.addAll(finalOptions, additionalVMOpts); 292 * to it's {@code value}. 293 * 294 * @param name the name of option to be prepared 295 * @param value the value of option 296 * @return prepared command line flag 297 */ 298 public static String prepareBooleanFlag(String name, boolean value) { 299 return String.format("-XX:%c%s", (value ? '+' : '-'), name); 300 } 301 302 /** 303 * Prepares numeric command line flag with name {@code name} by setting 304 * it's value to {@code value}. 305 * 306 * @param name the name of option to be prepared 307 * @param value the value of option 308 * @return prepared command line flag 309 */ 310 public static String prepareNumericFlag(String name, Number value) { 311 return String.format("-XX:%s=%s", name, value.toString()); 312 } 313 314 /** 315 * Returns message that should occur in VM output if option 316 * {@code optionName} if unrecognized. 317 * 318 * @param optionName the name of option for which message should be returned 319 * @return message saying that option {@code optionName} is unrecognized 320 */ 321 public static String getUnrecognizedOptionErrorMessage(String optionName) { 322 return String.format( 323 CommandLineOptionTest.UNRECOGNIZED_OPTION_ERROR_FORMAT, 324 optionName); 325 } 326 327 /** 328 * Returns message that should occur in VM output if option 329 * {@code optionName} is experimental and 330 * -XX:+UnlockExperimentalVMOptions was not passed to VM. 331 * | 244 } catch (RuntimeException e) { 245 String errorMessage = String.format( 246 "JVM should start with option '%s' without errors.", 247 optionName); 248 throw new AssertionError(errorMessage, e); 249 } 250 try { 251 outputAnalyzer.shouldMatch(String.format( 252 CommandLineOptionTest.PRINT_FLAGS_FINAL_FORMAT, 253 optionName, expectedValue)); 254 } catch (RuntimeException e) { 255 String errorMessage = String.format( 256 "Option '%s' is expected to have '%s' value%n%s", 257 optionName, expectedValue, 258 optionErrorString); 259 throw new AssertionError(errorMessage, e); 260 } 261 } 262 263 /** 264 * Start VM with given options and values. 265 * Generates command line option flags from 266 * {@code optionNames} and {@code optionValues}. 267 * 268 * @param optionNames names of options to pass in 269 * @param optionValues values of options 270 * @param additionalVMOpts additional options that should be 271 * passed to JVM. 272 * @return output from vm process 273 */ 274 public static OutputAnalyzer startVMWithOptions(String[] optionNames, 275 String[] optionValues, 276 String... additionalVMOpts) throws Throwable { 277 List<String> vmOpts = new ArrayList<>(); 278 if (optionNames == null || optionValues == null || optionNames.length != optionValues.length) { 279 throw new IllegalArgumentException("optionNames and/or optionValues"); 280 } 281 282 for (int i = 0; i < optionNames.length; i++) { 283 vmOpts.add(prepareFlag(optionNames[i], optionValues[i])); 284 } 285 Collections.addAll(vmOpts, additionalVMOpts); 286 Collections.addAll(vmOpts, "-version"); 287 288 ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder( 289 vmOpts.toArray(new String[vmOpts.size()])); 290 291 return new OutputAnalyzer(processBuilder.start()); 292 } 293 294 /** 295 * Verifies from the output that values of specified JVM options were the same as 296 * expected values. 297 * 298 * @param outputAnalyzer search output for expect options and values. 299 * @param optionNames names of tested options. 300 * @param expectedValues expected values of tested options. 301 * @throws Throwable if verification fails or some other issues occur. 302 */ 303 public static void verifyOptionValuesFromOutput(OutputAnalyzer outputAnalyzer, 304 String[] optionNames, 305 String[] expectedValues) throws Throwable { 306 outputAnalyzer.shouldHaveExitValue(0); 307 for (int i = 0; i < optionNames.length; i++) { 308 outputAnalyzer.shouldMatch(String.format( 309 CommandLineOptionTest.PRINT_FLAGS_FINAL_FORMAT, 310 optionNames[i], expectedValues[i])); 311 } 312 } 313 314 /** 315 * Verifies that value of specified JVM options are the same as 316 * expected values. 317 * Generates command line option flags from 318 * {@code optionNames} and {@code expectedValues}. 319 * 320 * @param optionNames names of tested options. 321 * @param expectedValues expected values of tested options. 322 * @throws Throwable if verification fails or some other issues occur. 323 */ 324 public static void verifyOptionValues(String[] optionNames, 325 String[] expectedValues) throws Throwable { 326 OutputAnalyzer outputAnalyzer = startVMWithOptions(optionNames, expectedValues, "-XX:+PrintFlagsFinal"); 327 verifyOptionValuesFromOutput(outputAnalyzer, optionNames, expectedValues); 328 } 329 330 /** 331 * Verifies that value of specified JVM when type of newly started VM 332 * is the same as the type of current. 333 * This method filter out option with {@code optionName} 334 * name from test java options. 335 * Only mode flag will be passed to VM in addition to 336 * {@code additionalVMOpts} 337 * 338 * @param optionName name of tested option. 339 * @param expectedValue expected value of tested option. 340 * @param optionErrorString message to show if option has another value 341 * @param additionalVMOpts additional options that should be 342 * passed to JVM. 343 * @throws Throwable if verification fails or some other issues occur. 344 */ 345 public static void verifyOptionValueForSameVM(String optionName, 346 String expectedValue, String optionErrorString, 347 String... additionalVMOpts) throws Throwable { 348 List<String> finalOptions = new ArrayList<>(); 349 finalOptions.add(CommandLineOptionTest.getVMTypeOption()); 350 Collections.addAll(finalOptions, additionalVMOpts); 359 * to it's {@code value}. 360 * 361 * @param name the name of option to be prepared 362 * @param value the value of option 363 * @return prepared command line flag 364 */ 365 public static String prepareBooleanFlag(String name, boolean value) { 366 return String.format("-XX:%c%s", (value ? '+' : '-'), name); 367 } 368 369 /** 370 * Prepares numeric command line flag with name {@code name} by setting 371 * it's value to {@code value}. 372 * 373 * @param name the name of option to be prepared 374 * @param value the value of option 375 * @return prepared command line flag 376 */ 377 public static String prepareNumericFlag(String name, Number value) { 378 return String.format("-XX:%s=%s", name, value.toString()); 379 } 380 381 /** 382 * Prepares generic command line flag with name {@code name} by setting 383 * it's value to {@code value}. 384 * 385 * @param name the name of option to be prepared 386 * @param value the value of option ("+" or "-" can be used instead of "true" or "false") 387 * @return prepared command line flag 388 */ 389 public static String prepareFlag(String name, String value) { 390 if (value.equals("+") || value.equalsIgnoreCase("true")) { 391 return "-XX:+" + name; 392 } else if (value.equals("-") || value.equalsIgnoreCase("false")) { 393 return "-XX:-" + name; 394 } else { 395 return "-XX:" + name + "=" + value; 396 } 397 } 398 399 /** 400 * Returns message that should occur in VM output if option 401 * {@code optionName} if unrecognized. 402 * 403 * @param optionName the name of option for which message should be returned 404 * @return message saying that option {@code optionName} is unrecognized 405 */ 406 public static String getUnrecognizedOptionErrorMessage(String optionName) { 407 return String.format( 408 CommandLineOptionTest.UNRECOGNIZED_OPTION_ERROR_FORMAT, 409 optionName); 410 } 411 412 /** 413 * Returns message that should occur in VM output if option 414 * {@code optionName} is experimental and 415 * -XX:+UnlockExperimentalVMOptions was not passed to VM. 416 * |