65 * 2: Mapping Failure - this happens when the OS (intermittently) fails to map the 66 * CDS archive, normally caused by Address Space Layout Randomization. 67 * We usually treat this as "pass". 68 * 3: Normal Exit - the JVM process has finished without crashing, and the exit code is 0. 69 * 4: Abnormal Exit - the JVM process has finished without crashing, and the exit code is not 0. 70 * 71 * In most test cases, we need to check the JVM process's output in cases 3 and 4. However, we need 72 * to make sure that our test code is not confused by case 2. 73 * 74 * For example, a JVM process is expected to print the string "Hi" and exit with 0. With the old 75 * CDSTestUtils.runWithArchive API, the test may be written as this: 76 * 77 * OutputAnalyzer out = CDSTestUtils.runWithArchive(args); 78 * out.shouldContain("Hi"); 79 * 80 * However, if the JVM process fails with mapping failure, the string "Hi" will not be in the output, 81 * and your test case will fail intermittently. 82 * 83 * Instead, the test case should be written as 84 * 85 * CCDSTestUtils.run(args).assertNormalExit("Hi"); 86 * 87 * EXAMPLES/HOWTO 88 * 89 * 1. For simple substring matching: 90 * 91 * CCDSTestUtils.run(args).assertNormalExit("Hi"); 92 * CCDSTestUtils.run(args).assertNormalExit("a", "b", "x"); 93 * CCDSTestUtils.run(args).assertAbnormalExit("failure 1", "failure2"); 94 * 95 * 2. For more complex output matching: using Lambda expressions 96 * 97 * CCDSTestUtils.run(args) 98 * .assertNormalExit(output -> output.shouldNotContain("this should not be printed"); 99 * CCDSTestUtils.run(args) 100 * .assertAbnormalExit(output -> { 101 * output.shouldNotContain("this should not be printed"); 102 * output.shouldHaveExitValue(123); 103 * }); 104 * 105 * 3. Chaining several checks: 106 * 107 * CCDSTestUtils.run(args) 108 * .assertNormalExit(output -> output.shouldNotContain("this should not be printed") 109 * .assertNormalExit("should have this", "should have that"); 110 * 111 * 4. [Rare use case] if a test sometimes exit normally, and sometimes abnormally: 112 * 113 * CCDSTestUtils.run(args) 114 * .ifNormalExit("ths string is printed when exiting with 0") 115 * .ifAbNormalExit("ths string is printed when exiting with 1"); 116 * 117 * NOTE: you usually don't want to write your test case like this -- it should always 118 * exit with the same exit code. (But I kept this API because some existing test cases 119 * behave this way -- need to revisit). 120 */ 121 public static class Result { 122 private final OutputAnalyzer output; 123 private final CDSOptions options; 124 private final boolean hasNormalExit; 125 private final String CDS_DISABLED = "warning: CDS is disabled when the"; 126 127 public Result(CDSOptions opts, OutputAnalyzer out) throws Exception { 128 checkMappingFailure(out); 129 this.options = opts; 130 this.output = out; 131 hasNormalExit = (output.getExitValue() == 0); 132 133 if (hasNormalExit) { 371 public static OutputAnalyzer runWithArchive(String... cliPrefix) 372 throws Exception { 373 374 return runWithArchive( (new CDSOptions()) 375 .setArchiveName(getDefaultArchiveName()) 376 .addPrefix(cliPrefix) ); 377 } 378 379 380 // Execute JVM with CDS archive, specify CDSOptions 381 public static OutputAnalyzer runWithArchive(CDSOptions opts) 382 throws Exception { 383 384 ArrayList<String> cmd = new ArrayList<String>(); 385 386 for (String p : opts.prefix) cmd.add(p); 387 388 cmd.add("-Xshare:" + opts.xShareMode); 389 cmd.add("-Dtest.timeout.factor=" + TestTimeoutFactor); 390 391 if (opts.archiveName == null) 392 opts.archiveName = getDefaultArchiveName(); 393 cmd.add("-XX:SharedArchiveFile=" + opts.archiveName); 394 395 if (opts.useVersion) 396 cmd.add("-version"); 397 398 for (String s : opts.suffix) cmd.add(s); 399 400 String[] cmdLine = cmd.toArray(new String[cmd.size()]); 401 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, cmdLine); 402 return executeAndLog(pb, "exec"); 403 } 404 405 406 // A commonly used convenience methods to create an archive and check the results 407 // Creates an archive and checks for errors 408 public static OutputAnalyzer runWithArchiveAndCheck(CDSOptions opts) throws Exception { 409 return checkExec(runWithArchive(opts)); 410 } 411 412 413 public static OutputAnalyzer runWithArchiveAndCheck(String... cliPrefix) throws Exception { | 65 * 2: Mapping Failure - this happens when the OS (intermittently) fails to map the 66 * CDS archive, normally caused by Address Space Layout Randomization. 67 * We usually treat this as "pass". 68 * 3: Normal Exit - the JVM process has finished without crashing, and the exit code is 0. 69 * 4: Abnormal Exit - the JVM process has finished without crashing, and the exit code is not 0. 70 * 71 * In most test cases, we need to check the JVM process's output in cases 3 and 4. However, we need 72 * to make sure that our test code is not confused by case 2. 73 * 74 * For example, a JVM process is expected to print the string "Hi" and exit with 0. With the old 75 * CDSTestUtils.runWithArchive API, the test may be written as this: 76 * 77 * OutputAnalyzer out = CDSTestUtils.runWithArchive(args); 78 * out.shouldContain("Hi"); 79 * 80 * However, if the JVM process fails with mapping failure, the string "Hi" will not be in the output, 81 * and your test case will fail intermittently. 82 * 83 * Instead, the test case should be written as 84 * 85 * CDSTestUtils.run(args).assertNormalExit("Hi"); 86 * 87 * EXAMPLES/HOWTO 88 * 89 * 1. For simple substring matching: 90 * 91 * CDSTestUtils.run(args).assertNormalExit("Hi"); 92 * CDSTestUtils.run(args).assertNormalExit("a", "b", "x"); 93 * CDSTestUtils.run(args).assertAbnormalExit("failure 1", "failure2"); 94 * 95 * 2. For more complex output matching: using Lambda expressions 96 * 97 * CDSTestUtils.run(args) 98 * .assertNormalExit(output -> output.shouldNotContain("this should not be printed"); 99 * CDSTestUtils.run(args) 100 * .assertAbnormalExit(output -> { 101 * output.shouldNotContain("this should not be printed"); 102 * output.shouldHaveExitValue(123); 103 * }); 104 * 105 * 3. Chaining several checks: 106 * 107 * CDSTestUtils.run(args) 108 * .assertNormalExit(output -> output.shouldNotContain("this should not be printed") 109 * .assertNormalExit("should have this", "should have that"); 110 * 111 * 4. [Rare use case] if a test sometimes exit normally, and sometimes abnormally: 112 * 113 * CDSTestUtils.run(args) 114 * .ifNormalExit("ths string is printed when exiting with 0") 115 * .ifAbNormalExit("ths string is printed when exiting with 1"); 116 * 117 * NOTE: you usually don't want to write your test case like this -- it should always 118 * exit with the same exit code. (But I kept this API because some existing test cases 119 * behave this way -- need to revisit). 120 */ 121 public static class Result { 122 private final OutputAnalyzer output; 123 private final CDSOptions options; 124 private final boolean hasNormalExit; 125 private final String CDS_DISABLED = "warning: CDS is disabled when the"; 126 127 public Result(CDSOptions opts, OutputAnalyzer out) throws Exception { 128 checkMappingFailure(out); 129 this.options = opts; 130 this.output = out; 131 hasNormalExit = (output.getExitValue() == 0); 132 133 if (hasNormalExit) { 371 public static OutputAnalyzer runWithArchive(String... cliPrefix) 372 throws Exception { 373 374 return runWithArchive( (new CDSOptions()) 375 .setArchiveName(getDefaultArchiveName()) 376 .addPrefix(cliPrefix) ); 377 } 378 379 380 // Execute JVM with CDS archive, specify CDSOptions 381 public static OutputAnalyzer runWithArchive(CDSOptions opts) 382 throws Exception { 383 384 ArrayList<String> cmd = new ArrayList<String>(); 385 386 for (String p : opts.prefix) cmd.add(p); 387 388 cmd.add("-Xshare:" + opts.xShareMode); 389 cmd.add("-Dtest.timeout.factor=" + TestTimeoutFactor); 390 391 if (!opts.useSystemArchive) { 392 if (opts.archiveName == null) 393 opts.archiveName = getDefaultArchiveName(); 394 cmd.add("-XX:SharedArchiveFile=" + opts.archiveName); 395 } 396 397 if (opts.useVersion) 398 cmd.add("-version"); 399 400 for (String s : opts.suffix) cmd.add(s); 401 402 String[] cmdLine = cmd.toArray(new String[cmd.size()]); 403 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, cmdLine); 404 return executeAndLog(pb, "exec"); 405 } 406 407 408 // A commonly used convenience methods to create an archive and check the results 409 // Creates an archive and checks for errors 410 public static OutputAnalyzer runWithArchiveAndCheck(CDSOptions opts) throws Exception { 411 return checkExec(runWithArchive(opts)); 412 } 413 414 415 public static OutputAnalyzer runWithArchiveAndCheck(String... cliPrefix) throws Exception { |