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 * 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()]));
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
|
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 option
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()]));
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
|