mseledtsov@5471: /* mseledtsov@5471: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. mseledtsov@5471: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mseledtsov@5471: * mseledtsov@5471: * This code is free software; you can redistribute it and/or modify it mseledtsov@5471: * under the terms of the GNU General Public License version 2 only, as mseledtsov@5471: * published by the Free Software Foundation. mseledtsov@5471: * mseledtsov@5471: * This code is distributed in the hope that it will be useful, but WITHOUT mseledtsov@5471: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mseledtsov@5471: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mseledtsov@5471: * version 2 for more details (a copy is included in the LICENSE file that mseledtsov@5471: * accompanied this code). mseledtsov@5471: * mseledtsov@5471: * You should have received a copy of the GNU General Public License version mseledtsov@5471: * 2 along with this work; if not, write to the Free Software Foundation, mseledtsov@5471: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mseledtsov@5471: * mseledtsov@5471: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mseledtsov@5471: * or visit www.oracle.com if you need additional information or have any mseledtsov@5471: * questions. mseledtsov@5471: */ mseledtsov@5471: mseledtsov@5471: mseledtsov@5471: /* mseledtsov@5471: * @test mseledtsov@5471: * @summary Test the OutputAnalyzer reporting functionality, mseledtsov@5471: * such as printing additional diagnostic info mseledtsov@5471: * (exit code, stdout, stderr, command line, etc.) mseledtsov@5471: * @library /testlibrary mseledtsov@5471: */ mseledtsov@5471: mseledtsov@5471: import java.io.ByteArrayOutputStream; mseledtsov@5471: import java.io.PrintStream; mseledtsov@5471: mseledtsov@5471: import com.oracle.java.testlibrary.OutputAnalyzer; mseledtsov@5471: import com.oracle.java.testlibrary.ProcessTools; mseledtsov@5471: mseledtsov@5471: mseledtsov@5471: public class OutputAnalyzerReportingTest { mseledtsov@5471: mseledtsov@5471: public static void main(String[] args) throws Exception { mseledtsov@5471: // Create the output analyzer under test mseledtsov@5471: String stdout = "aaaaaa"; mseledtsov@5471: String stderr = "bbbbbb"; mseledtsov@5471: OutputAnalyzer output = new OutputAnalyzer(stdout, stderr); mseledtsov@5471: mseledtsov@5471: // Expected summary values should be the same for all cases, mseledtsov@5471: // since the outputAnalyzer object is the same mseledtsov@5471: String expectedExitValue = "-1"; mseledtsov@5471: String expectedSummary = mseledtsov@5471: " stdout: [" + stdout + "];\n" + mseledtsov@5471: " stderr: [" + stderr + "]\n" + mseledtsov@5471: " exitValue = " + expectedExitValue + "\n"; mseledtsov@5471: mseledtsov@5471: mseledtsov@5471: DiagnosticSummaryTestRunner testRunner = mseledtsov@5471: new DiagnosticSummaryTestRunner(); mseledtsov@5471: mseledtsov@5471: // should have exit value mseledtsov@5471: testRunner.init(expectedSummary); mseledtsov@5471: int unexpectedExitValue = 2; mseledtsov@5471: try { mseledtsov@5471: output.shouldHaveExitValue(unexpectedExitValue); mseledtsov@5471: } catch (RuntimeException e) { } mseledtsov@5471: testRunner.closeAndCheckResults(); mseledtsov@5471: mseledtsov@5471: // should not contain mseledtsov@5471: testRunner.init(expectedSummary); mseledtsov@5471: try { mseledtsov@5471: output.shouldNotContain(stdout); mseledtsov@5471: } catch (RuntimeException e) { } mseledtsov@5471: testRunner.closeAndCheckResults(); mseledtsov@5471: mseledtsov@5471: // should contain mseledtsov@5471: testRunner.init(expectedSummary); mseledtsov@5471: try { mseledtsov@5471: output.shouldContain("unexpected-stuff"); mseledtsov@5471: } catch (RuntimeException e) { } mseledtsov@5471: testRunner.closeAndCheckResults(); mseledtsov@5471: mseledtsov@5471: // should not match mseledtsov@5471: testRunner.init(expectedSummary); mseledtsov@5471: try { mseledtsov@5471: output.shouldNotMatch("[a]"); mseledtsov@5471: } catch (RuntimeException e) { } mseledtsov@5471: testRunner.closeAndCheckResults(); mseledtsov@5471: mseledtsov@5471: // should match mseledtsov@5471: testRunner.init(expectedSummary); mseledtsov@5471: try { mseledtsov@5471: output.shouldMatch("[qwerty]"); mseledtsov@5471: } catch (RuntimeException e) { } mseledtsov@5471: testRunner.closeAndCheckResults(); mseledtsov@5471: mseledtsov@5471: } mseledtsov@5471: mseledtsov@5471: private static class DiagnosticSummaryTestRunner { mseledtsov@5471: private ByteArrayOutputStream byteStream = mseledtsov@5471: new ByteArrayOutputStream(10000); mseledtsov@5471: mseledtsov@5471: private String expectedSummary = ""; mseledtsov@5471: private PrintStream errStream; mseledtsov@5471: mseledtsov@5471: mseledtsov@5471: public void init(String expectedSummary) { mseledtsov@5471: this.expectedSummary = expectedSummary; mseledtsov@5471: byteStream.reset(); mseledtsov@5471: errStream = new PrintStream(byteStream); mseledtsov@5471: System.setErr(errStream); mseledtsov@5471: } mseledtsov@5471: mseledtsov@5471: public void closeAndCheckResults() { mseledtsov@5471: // check results mseledtsov@5471: errStream.close(); mseledtsov@5471: String stdErrStr = byteStream.toString(); mseledtsov@5471: if (!stdErrStr.contains(expectedSummary)) { mseledtsov@5471: throw new RuntimeException("The output does not contain " mseledtsov@5471: + "the diagnostic message, or the message is incorrect"); mseledtsov@5471: } mseledtsov@5471: } mseledtsov@5471: } mseledtsov@5471: mseledtsov@5471: }