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
|
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 * Start VM with given options and values.
332 * Generates command line option flags from
333 * {@code optionNames} and {@code optionValues}.
334 *
335 * @param optionNames names of options to pass in
336 * @param optionValues values of options
337 * @param additionalVMOpts additional options that should be
338 * passed to JVM.
339 * @return output from vm process
340 */
341 public static OutputAnalyzer startVMWithOptions(String[] optionNames,
342 String[] optionValues,
343 String... additionalVMOpts) throws Throwable {
344 List<String> vmOpts = new ArrayList<>();
345 if (optionNames == null || optionValues == null || optionNames.length != optionValues.length) {
346 throw new IllegalArgumentException("optionNames and/or optionValues");
347 }
348
349 for (int i = 0; i < optionNames.length; i++) {
350 vmOpts.add(prepareFlag(optionNames[i], optionValues[i]));
351 }
352 Collections.addAll(vmOpts, additionalVMOpts);
353 Collections.addAll(vmOpts, "-version");
354
355 ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(
356 vmOpts.toArray(new String[vmOpts.size()]));
357
358 return new OutputAnalyzer(processBuilder.start());
359 }
360
361 /**
362 * Verifies from the output that values of specified JVM options were the same as
363 * expected values.
364 *
365 * @param outputAnalyzer search output for expect options and values.
366 * @param optionNames names of tested options.
367 * @param expectedValues expected values of tested options.
368 * @throws Throwable if verification fails or some other issues occur.
369 */
370 public static void verifyOptionValuesFromOutput(OutputAnalyzer outputAnalyzer,
371 String[] optionNames,
372 String[] expectedValues) throws Throwable {
373 outputAnalyzer.shouldHaveExitValue(0);
374 for (int i = 0; i < optionNames.length; i++) {
375 outputAnalyzer.shouldMatch(String.format(
376 CommandLineOptionTest.PRINT_FLAGS_FINAL_FORMAT,
377 optionNames[i], expectedValues[i]));
378 }
379 }
380
381 /**
382 * Verifies that value of specified JVM options are the same as
383 * expected values.
384 * Generates command line option flags from
385 * {@code optionNames} and {@code expectedValues}.
386 *
387 * @param optionNames names of tested options.
388 * @param expectedValues expected values of tested options.
389 * @throws Throwable if verification fails or some other issues occur.
390 */
391 public static void verifyOptionValues(String[] optionNames,
392 String[] expectedValues) throws Throwable {
393 OutputAnalyzer outputAnalyzer = startVMWithOptions(optionNames, expectedValues, "-XX:+PrintFlagsFinal");
394 verifyOptionValuesFromOutput(outputAnalyzer, optionNames, expectedValues);
395 }
396
397 /**
398 * Start VM with given options and values.
399 * Generates command line option flags from
400 * {@code optionNames} and {@code optionValues}.
401 *
402 * @param optionNames names of options to pass in
403 * @param optionValues values of options
404 * @param additionalVMOpts additional options that should be
405 * passed to JVM.
406 * @return output from vm process
407 */
408 public static OutputAnalyzer startVMWithOptions(String[] optionNames,
409 String[] optionValues,
410 String... additionalVMOpts) throws Throwable {
411 List<String> vmOpts = new ArrayList<>();
412 if (optionNames == null || optionValues == null || optionNames.length != optionValues.length) {
413 throw new IllegalArgumentException("optionNames and/or optionValues");
414 }
415
416 for (int i = 0; i < optionNames.length; i++) {
417 vmOpts.add(prepareFlag(optionNames[i], optionValues[i]));
418 }
419 Collections.addAll(vmOpts, additionalVMOpts);
420 Collections.addAll(vmOpts, "-version");
421
422 ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(
423 vmOpts.toArray(new String[vmOpts.size()]));
424
425 return new OutputAnalyzer(processBuilder.start());
426 }
427
428 /**
429 * Verifies from the output that values of specified JVM options were the same as
430 * expected values.
431 *
432 * @param outputAnalyzer search output for expect options and values.
433 * @param optionNames names of tested options.
434 * @param expectedValues expected values of tested options.
435 * @throws Throwable if verification fails or some other issues occur.
436 */
437 public static void verifyOptionValuesFromOutput(OutputAnalyzer outputAnalyzer,
438 String[] optionNames,
439 String[] expectedValues) throws Throwable {
440 outputAnalyzer.shouldHaveExitValue(0);
441 for (int i = 0; i < optionNames.length; i++) {
442 outputAnalyzer.shouldMatch(String.format(
443 CommandLineOptionTest.PRINT_FLAGS_FINAL_FORMAT,
444 optionNames[i], expectedValues[i]));
445 }
446 }
447
448 /**
449 * Verifies that value of specified JVM options are the same as
450 * expected values.
451 * Generates command line option flags from
452 * {@code optionNames} and {@code expectedValues}.
453 *
454 * @param optionNames names of tested options.
455 * @param expectedValues expected values of tested options.
456 * @throws Throwable if verification fails or some other issues occur.
457 */
458 public static void verifyOptionValues(String[] optionNames,
459 String[] expectedValues) throws Throwable {
460 OutputAnalyzer outputAnalyzer = startVMWithOptions(optionNames, expectedValues, "-XX:+PrintFlagsFinal");
461 verifyOptionValuesFromOutput(outputAnalyzer, optionNames, expectedValues);
462 }
463
464 /**
465 * Verifies that value of specified JVM when type of newly started VM
466 * is the same as the type of current.
467 * This method filter out option with {@code optionName}
468 * name from test java options.
469 * Only mode flag will be passed to VM in addition to
470 * {@code additionalVMOpts}
471 *
472 * @param optionName name of tested option.
473 * @param expectedValue expected value of tested option.
474 * @param optionErrorString message to show if option has another value
475 * @param additionalVMOpts additional options that should be
476 * passed to JVM.
477 * @throws Throwable if verification fails or some other issues occur.
478 */
479 public static void verifyOptionValueForSameVM(String optionName,
480 String expectedValue, String optionErrorString,
481 String... additionalVMOpts) throws Throwable {
482 List<String> finalOptions = new ArrayList<>();
483 finalOptions.add(CommandLineOptionTest.getVMTypeOption());
484 Collections.addAll(finalOptions, additionalVMOpts);
493 * to it's {@code value}.
494 *
495 * @param name the name of option to be prepared
496 * @param value the value of option
497 * @return prepared command line flag
498 */
499 public static String prepareBooleanFlag(String name, boolean value) {
500 return String.format("-XX:%c%s", (value ? '+' : '-'), name);
501 }
502
503 /**
504 * Prepares numeric command line flag with name {@code name} by setting
505 * it's value to {@code value}.
506 *
507 * @param name the name of option to be prepared
508 * @param value the value of option
509 * @return prepared command line flag
510 */
511 public static String prepareNumericFlag(String name, Number value) {
512 return String.format("-XX:%s=%s", name, value.toString());
513 }
514
515 /**
516 * Prepares generic command line flag with name {@code name} by setting
517 * it's value to {@code value}.
518 *
519 * @param name the name of option to be prepared
520 * @param value the value of option ("+" or "-" can be used instead of "true" or "false")
521 * @return prepared command line flag
522 */
523 public static String prepareFlag(String name, String value) {
524 if (value.equals("+") || value.equalsIgnoreCase("true")) {
525 return "-XX:+" + name;
526 } else if (value.equals("-") || value.equalsIgnoreCase("false")) {
527 return "-XX:-" + name;
528 } else {
529 return "-XX:" + name + "=" + value;
530 }
531 }
532
533 /**
534 * Prepares generic command line flag with name {@code name} by setting
535 * it's value to {@code value}.
536 *
537 * @param name the name of option to be prepared
538 * @param value the value of option ("+" or "-" can be used instead of "true" or "false")
539 * @return prepared command line flag
540 */
541 public static String prepareFlag(String name, String value) {
542 if (value.equals("+") || value.equalsIgnoreCase("true")) {
543 return "-XX:+" + name;
544 } else if (value.equals("-") || value.equalsIgnoreCase("false")) {
545 return "-XX:-" + name;
546 } else {
547 return "-XX:" + name + "=" + value;
548 }
549 }
550
551 /**
552 * Prepares generic command line flag with name {@code name} by setting
553 * it's value to {@code value}.
554 *
555 * @param name the name of option to be prepared
556 * @param value the value of option ("+" or "-" can be used instead of "true" or "false")
557 * @return prepared command line flag
558 */
559 public static String prepareFlag(String name, String value) {
560 if (value.equals("+") || value.equalsIgnoreCase("true")) {
561 return "-XX:+" + name;
562 } else if (value.equals("-") || value.equalsIgnoreCase("false")) {
563 return "-XX:-" + name;
564 } else {
565 return "-XX:" + name + "=" + value;
566 }
567 }
568
|