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