Skip to content

Commit fdc8455

Browse files
author
Roger Riggs
committed
8288495: [test] Make OutputAnalyzer exception more informative
Reviewed-by: lmesnik, naoto, jpai, dholmes
1 parent 925084c commit fdc8455

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

test/lib-test/jdk/test/lib/process/OutputAnalyzerTest.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,12 +24,12 @@
2424
/*
2525
* @test
2626
* @summary Test the OutputAnalyzer utility class
27-
* @modules java.management
2827
* @library /test/lib
2928
* @run main OutputAnalyzerTest
3029
*/
3130

3231
import jdk.test.lib.process.OutputAnalyzer;
32+
import jdk.test.lib.process.ProcessTools;
3333

3434
public class OutputAnalyzerTest {
3535

@@ -216,6 +216,32 @@ public static void main(String args[]) throws Exception {
216216
throw new Exception("firstMatch(String, int) failed to match. Expected: " + aa + " got: " + result);
217217
}
218218
}
219+
220+
{
221+
try {
222+
// Verify the exception message
223+
OutputAnalyzer out = ProcessTools.executeProcess("true");
224+
out.shouldHaveExitValue(1);
225+
throw new RuntimeException("'shouldHaveExitValue' should have thrown an exception");
226+
} catch (Throwable ex) {
227+
if (!ex.getMessage().equals("Expected to get exit value of [1], exit value is: [0]")) {
228+
throw new RuntimeException("Unexpected message: " + ex.getMessage());
229+
}
230+
}
231+
}
232+
233+
{
234+
try {
235+
// Verify the exception message
236+
OutputAnalyzer out = ProcessTools.executeProcess("true");
237+
out.shouldNotHaveExitValue(0);
238+
throw new RuntimeException("'shouldNotHaveExitValue' should have thrown an exception");
239+
} catch (Throwable ex) {
240+
if (!ex.getMessage().equals("Unexpected to get exit value of [0]")) {
241+
throw new RuntimeException("Unexpected message: " + ex.getMessage());
242+
}
243+
}
244+
}
219245
}
220246

221247
}

test/lib/jdk/test/lib/process/OutputAnalyzer.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -218,7 +218,7 @@ public OutputAnalyzer shouldContain(String expectedString) {
218218
String stderr = getStderr();
219219
if (!stdout.contains(expectedString) && !stderr.contains(expectedString)) {
220220
reportDiagnosticSummary();
221-
throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr \n");
221+
throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr");
222222
}
223223
return this;
224224
}
@@ -233,7 +233,7 @@ public OutputAnalyzer stdoutShouldContain(String expectedString) {
233233
String stdout = getStdout();
234234
if (!stdout.contains(expectedString)) {
235235
reportDiagnosticSummary();
236-
throw new RuntimeException("'" + expectedString + "' missing from stdout \n");
236+
throw new RuntimeException("'" + expectedString + "' missing from stdout");
237237
}
238238
return this;
239239
}
@@ -248,7 +248,7 @@ public OutputAnalyzer stderrShouldContain(String expectedString) {
248248
String stderr = getStderr();
249249
if (!stderr.contains(expectedString)) {
250250
reportDiagnosticSummary();
251-
throw new RuntimeException("'" + expectedString + "' missing from stderr \n");
251+
throw new RuntimeException("'" + expectedString + "' missing from stderr");
252252
}
253253
return this;
254254
}
@@ -264,11 +264,11 @@ public OutputAnalyzer shouldNotContain(String notExpectedString) {
264264
String stderr = getStderr();
265265
if (stdout.contains(notExpectedString)) {
266266
reportDiagnosticSummary();
267-
throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
267+
throw new RuntimeException("'" + notExpectedString + "' found in stdout");
268268
}
269269
if (stderr.contains(notExpectedString)) {
270270
reportDiagnosticSummary();
271-
throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
271+
throw new RuntimeException("'" + notExpectedString + "' found in stderr");
272272
}
273273
return this;
274274
}
@@ -302,7 +302,7 @@ public OutputAnalyzer stdoutShouldNotContain(String notExpectedString) {
302302
String stdout = getStdout();
303303
if (stdout.contains(notExpectedString)) {
304304
reportDiagnosticSummary();
305-
throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
305+
throw new RuntimeException("'" + notExpectedString + "' found in stdout");
306306
}
307307
return this;
308308
}
@@ -317,7 +317,7 @@ public OutputAnalyzer stderrShouldNotContain(String notExpectedString) {
317317
String stderr = getStderr();
318318
if (stderr.contains(notExpectedString)) {
319319
reportDiagnosticSummary();
320-
throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
320+
throw new RuntimeException("'" + notExpectedString + "' found in stderr");
321321
}
322322
return this;
323323
}
@@ -338,7 +338,7 @@ public OutputAnalyzer shouldMatch(String regexp) {
338338
if (!stdoutMatcher.find() && !stderrMatcher.find()) {
339339
reportDiagnosticSummary();
340340
throw new RuntimeException("'" + regexp
341-
+ "' missing from stdout/stderr \n");
341+
+ "' missing from stdout/stderr");
342342
}
343343
return this;
344344
}
@@ -356,7 +356,7 @@ public OutputAnalyzer stdoutShouldMatch(String regexp) {
356356
if (!matcher.find()) {
357357
reportDiagnosticSummary();
358358
throw new RuntimeException("'" + regexp
359-
+ "' missing from stdout \n");
359+
+ "' missing from stdout");
360360
}
361361
return this;
362362
}
@@ -374,7 +374,7 @@ public OutputAnalyzer stderrShouldMatch(String pattern) {
374374
if (!matcher.find()) {
375375
reportDiagnosticSummary();
376376
throw new RuntimeException("'" + pattern
377-
+ "' missing from stderr \n");
377+
+ "' missing from stderr");
378378
}
379379
return this;
380380
}
@@ -393,15 +393,15 @@ public OutputAnalyzer shouldNotMatch(String regexp) {
393393
if (matcher.find()) {
394394
reportDiagnosticSummary();
395395
throw new RuntimeException("'" + regexp
396-
+ "' found in stdout: '" + matcher.group() + "' \n");
396+
+ "' found in stdout: '" + matcher.group() + "'");
397397
}
398398

399399
String stderr = getStderr();
400400
matcher = pattern.matcher(stderr);
401401
if (matcher.find()) {
402402
reportDiagnosticSummary();
403403
throw new RuntimeException("'" + regexp
404-
+ "' found in stderr: '" + matcher.group() + "' \n");
404+
+ "' found in stderr: '" + matcher.group() + "'");
405405
}
406406

407407
return this;
@@ -420,7 +420,7 @@ public OutputAnalyzer stdoutShouldNotMatch(String regexp) {
420420
if (matcher.find()) {
421421
reportDiagnosticSummary();
422422
throw new RuntimeException("'" + regexp
423-
+ "' found in stdout \n");
423+
+ "' found in stdout");
424424
}
425425
return this;
426426
}
@@ -438,7 +438,7 @@ public OutputAnalyzer stderrShouldNotMatch(String regexp) {
438438
if (matcher.find()) {
439439
reportDiagnosticSummary();
440440
throw new RuntimeException("'" + regexp
441-
+ "' found in stderr \n");
441+
+ "' found in stderr");
442442
}
443443
return this;
444444
}
@@ -487,7 +487,7 @@ public OutputAnalyzer shouldHaveExitValue(int expectedExitValue) {
487487
if (getExitValue() != expectedExitValue) {
488488
reportDiagnosticSummary();
489489
throw new RuntimeException("Expected to get exit value of ["
490-
+ expectedExitValue + "]\n");
490+
+ expectedExitValue + "], exit value is: [" + getExitValue() + "]");
491491
}
492492
return this;
493493
}
@@ -502,7 +502,7 @@ public OutputAnalyzer shouldNotHaveExitValue(int notExpectedExitValue) {
502502
if (getExitValue() == notExpectedExitValue) {
503503
reportDiagnosticSummary();
504504
throw new RuntimeException("Unexpected to get exit value of ["
505-
+ notExpectedExitValue + "]\n");
505+
+ notExpectedExitValue + "]");
506506
}
507507
return this;
508508
}
@@ -637,7 +637,7 @@ public OutputAnalyzer stderrShouldMatchIgnoreVMWarnings(String pattern) {
637637
if (!matcher.find()) {
638638
reportDiagnosticSummary();
639639
throw new RuntimeException("'" + pattern
640-
+ "' missing from stderr \n");
640+
+ "' missing from stderr");
641641
}
642642
return this;
643643
}

0 commit comments

Comments
 (0)