Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java
38833 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @summary Test the OutputAnalyzer reporting functionality,27* such as printing additional diagnostic info28* (exit code, stdout, stderr, command line, etc.)29*/3031import java.io.ByteArrayOutputStream;32import java.io.PrintStream;3334import jdk.testlibrary.OutputAnalyzer;3536public class OutputAnalyzerReportingTest {3738public static void main(String[] args) throws Exception {39// Create the output analyzer under test40String stdout = "aaaaaa";41String stderr = "bbbbbb";42OutputAnalyzer output = new OutputAnalyzer(stdout, stderr);4344// Expected summary values should be the same for all cases,45// since the outputAnalyzer object is the same46String expectedExitValue = "-1";47String expectedSummary =48" stdout: [" + stdout + "];\n" +49" stderr: [" + stderr + "]\n" +50" exitValue = " + expectedExitValue + "\n";515253DiagnosticSummaryTestRunner testRunner =54new DiagnosticSummaryTestRunner();5556// should have exit value57testRunner.init(expectedSummary);58int unexpectedExitValue = 2;59try {60output.shouldHaveExitValue(unexpectedExitValue);61} catch (RuntimeException e) { }62testRunner.closeAndCheckResults();6364// should not contain65testRunner.init(expectedSummary);66try {67output.shouldNotContain(stdout);68} catch (RuntimeException e) { }69testRunner.closeAndCheckResults();7071// should contain72testRunner.init(expectedSummary);73try {74output.shouldContain("unexpected-stuff");75} catch (RuntimeException e) { }76testRunner.closeAndCheckResults();7778// should not match79testRunner.init(expectedSummary);80try {81output.shouldNotMatch("[a]");82} catch (RuntimeException e) { }83testRunner.closeAndCheckResults();8485// should match86testRunner.init(expectedSummary);87try {88output.shouldMatch("[qwerty]");89} catch (RuntimeException e) { }90testRunner.closeAndCheckResults();9192}9394private static class DiagnosticSummaryTestRunner {95private ByteArrayOutputStream byteStream =96new ByteArrayOutputStream(10000);9798private String expectedSummary = "";99private PrintStream errStream;100101102public void init(String expectedSummary) {103this.expectedSummary = expectedSummary;104byteStream.reset();105errStream = new PrintStream(byteStream);106System.setErr(errStream);107}108109public void closeAndCheckResults() {110// check results111errStream.close();112String stdErrStr = byteStream.toString();113if (!stdErrStr.contains(expectedSummary)) {114throw new RuntimeException("The output does not contain "115+ "the diagnostic message, or the message is incorrect");116}117}118}119120}121122123