|
1 /* |
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 |
|
25 /* |
|
26 * @test |
|
27 * @summary Test the OutputAnalyzer reporting functionality, |
|
28 * such as printing additional diagnostic info |
|
29 * (exit code, stdout, stderr, command line, etc.) |
|
30 * @library /testlibrary |
|
31 */ |
|
32 |
|
33 import java.io.ByteArrayOutputStream; |
|
34 import java.io.PrintStream; |
|
35 |
|
36 import jdk.testlibrary.OutputAnalyzer; |
|
37 |
|
38 public class OutputAnalyzerReportingTest { |
|
39 |
|
40 public static void main(String[] args) throws Exception { |
|
41 // Create the output analyzer under test |
|
42 String stdout = "aaaaaa"; |
|
43 String stderr = "bbbbbb"; |
|
44 OutputAnalyzer output = new OutputAnalyzer(stdout, stderr); |
|
45 |
|
46 // Expected summary values should be the same for all cases, |
|
47 // since the outputAnalyzer object is the same |
|
48 String expectedExitValue = "-1"; |
|
49 String expectedSummary = |
|
50 " stdout: [" + stdout + "];\n" + |
|
51 " stderr: [" + stderr + "]\n" + |
|
52 " exitValue = " + expectedExitValue + "\n"; |
|
53 |
|
54 |
|
55 DiagnosticSummaryTestRunner testRunner = |
|
56 new DiagnosticSummaryTestRunner(); |
|
57 |
|
58 // should have exit value |
|
59 testRunner.init(expectedSummary); |
|
60 int unexpectedExitValue = 2; |
|
61 try { |
|
62 output.shouldHaveExitValue(unexpectedExitValue); |
|
63 } catch (RuntimeException e) { } |
|
64 testRunner.closeAndCheckResults(); |
|
65 |
|
66 // should not contain |
|
67 testRunner.init(expectedSummary); |
|
68 try { |
|
69 output.shouldNotContain(stdout); |
|
70 } catch (RuntimeException e) { } |
|
71 testRunner.closeAndCheckResults(); |
|
72 |
|
73 // should contain |
|
74 testRunner.init(expectedSummary); |
|
75 try { |
|
76 output.shouldContain("unexpected-stuff"); |
|
77 } catch (RuntimeException e) { } |
|
78 testRunner.closeAndCheckResults(); |
|
79 |
|
80 // should not match |
|
81 testRunner.init(expectedSummary); |
|
82 try { |
|
83 output.shouldNotMatch("[a]"); |
|
84 } catch (RuntimeException e) { } |
|
85 testRunner.closeAndCheckResults(); |
|
86 |
|
87 // should match |
|
88 testRunner.init(expectedSummary); |
|
89 try { |
|
90 output.shouldMatch("[qwerty]"); |
|
91 } catch (RuntimeException e) { } |
|
92 testRunner.closeAndCheckResults(); |
|
93 |
|
94 } |
|
95 |
|
96 private static class DiagnosticSummaryTestRunner { |
|
97 private ByteArrayOutputStream byteStream = |
|
98 new ByteArrayOutputStream(10000); |
|
99 |
|
100 private String expectedSummary = ""; |
|
101 private PrintStream errStream; |
|
102 |
|
103 |
|
104 public void init(String expectedSummary) { |
|
105 this.expectedSummary = expectedSummary; |
|
106 byteStream.reset(); |
|
107 errStream = new PrintStream(byteStream); |
|
108 System.setErr(errStream); |
|
109 } |
|
110 |
|
111 public void closeAndCheckResults() { |
|
112 // check results |
|
113 errStream.close(); |
|
114 String stdErrStr = byteStream.toString(); |
|
115 if (!stdErrStr.contains(expectedSummary)) { |
|
116 throw new RuntimeException("The output does not contain " |
|
117 + "the diagnostic message, or the message is incorrect"); |
|
118 } |
|
119 } |
|
120 } |
|
121 |
|
122 } |