ykantser | 2c9d44f | 2013-11-13 11:46:05 +0100 | [diff] [blame] | 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.) |
ykantser | 2c9d44f | 2013-11-13 11:46:05 +0100 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | import java.io.ByteArrayOutputStream; |
| 33 | import java.io.PrintStream; |
| 34 | |
| 35 | import jdk.testlibrary.OutputAnalyzer; |
| 36 | |
| 37 | public class OutputAnalyzerReportingTest { |
| 38 | |
| 39 | public static void main(String[] args) throws Exception { |
| 40 | // Create the output analyzer under test |
| 41 | String stdout = "aaaaaa"; |
| 42 | String stderr = "bbbbbb"; |
| 43 | OutputAnalyzer output = new OutputAnalyzer(stdout, stderr); |
| 44 | |
| 45 | // Expected summary values should be the same for all cases, |
| 46 | // since the outputAnalyzer object is the same |
| 47 | String expectedExitValue = "-1"; |
| 48 | String expectedSummary = |
| 49 | " stdout: [" + stdout + "];\n" + |
| 50 | " stderr: [" + stderr + "]\n" + |
| 51 | " exitValue = " + expectedExitValue + "\n"; |
| 52 | |
| 53 | |
| 54 | DiagnosticSummaryTestRunner testRunner = |
| 55 | new DiagnosticSummaryTestRunner(); |
| 56 | |
| 57 | // should have exit value |
| 58 | testRunner.init(expectedSummary); |
| 59 | int unexpectedExitValue = 2; |
| 60 | try { |
| 61 | output.shouldHaveExitValue(unexpectedExitValue); |
| 62 | } catch (RuntimeException e) { } |
| 63 | testRunner.closeAndCheckResults(); |
| 64 | |
| 65 | // should not contain |
| 66 | testRunner.init(expectedSummary); |
| 67 | try { |
| 68 | output.shouldNotContain(stdout); |
| 69 | } catch (RuntimeException e) { } |
| 70 | testRunner.closeAndCheckResults(); |
| 71 | |
| 72 | // should contain |
| 73 | testRunner.init(expectedSummary); |
| 74 | try { |
| 75 | output.shouldContain("unexpected-stuff"); |
| 76 | } catch (RuntimeException e) { } |
| 77 | testRunner.closeAndCheckResults(); |
| 78 | |
| 79 | // should not match |
| 80 | testRunner.init(expectedSummary); |
| 81 | try { |
| 82 | output.shouldNotMatch("[a]"); |
| 83 | } catch (RuntimeException e) { } |
| 84 | testRunner.closeAndCheckResults(); |
| 85 | |
| 86 | // should match |
| 87 | testRunner.init(expectedSummary); |
| 88 | try { |
| 89 | output.shouldMatch("[qwerty]"); |
| 90 | } catch (RuntimeException e) { } |
| 91 | testRunner.closeAndCheckResults(); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | private static class DiagnosticSummaryTestRunner { |
| 96 | private ByteArrayOutputStream byteStream = |
| 97 | new ByteArrayOutputStream(10000); |
| 98 | |
| 99 | private String expectedSummary = ""; |
| 100 | private PrintStream errStream; |
| 101 | |
| 102 | |
| 103 | public void init(String expectedSummary) { |
| 104 | this.expectedSummary = expectedSummary; |
| 105 | byteStream.reset(); |
| 106 | errStream = new PrintStream(byteStream); |
| 107 | System.setErr(errStream); |
| 108 | } |
| 109 | |
| 110 | public void closeAndCheckResults() { |
| 111 | // check results |
| 112 | errStream.close(); |
| 113 | String stdErrStr = byteStream.toString(); |
| 114 | if (!stdErrStr.contains(expectedSummary)) { |
| 115 | throw new RuntimeException("The output does not contain " |
| 116 | + "the diagnostic message, or the message is incorrect"); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } |