Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/testlibrary_tests/OutputAnalyzerReportingTest.java
32278 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* @library /testlibrary30*/3132import java.io.ByteArrayOutputStream;33import java.io.PrintStream;3435import com.oracle.java.testlibrary.OutputAnalyzer;36import com.oracle.java.testlibrary.ProcessTools;373839public class OutputAnalyzerReportingTest {4041public static void main(String[] args) throws Exception {42// Create the output analyzer under test43String stdout = "aaaaaa";44String stderr = "bbbbbb";45OutputAnalyzer output = new OutputAnalyzer(stdout, stderr);4647// Expected summary values should be the same for all cases,48// since the outputAnalyzer object is the same49String expectedExitValue = "-1";50String expectedSummary =51" stdout: [" + stdout + "];\n" +52" stderr: [" + stderr + "]\n" +53" exitValue = " + expectedExitValue + "\n";545556DiagnosticSummaryTestRunner testRunner =57new DiagnosticSummaryTestRunner();5859// should have exit value60testRunner.init(expectedSummary);61int unexpectedExitValue = 2;62try {63output.shouldHaveExitValue(unexpectedExitValue);64} catch (RuntimeException e) { }65testRunner.closeAndCheckResults();6667// should not contain68testRunner.init(expectedSummary);69try {70output.shouldNotContain(stdout);71} catch (RuntimeException e) { }72testRunner.closeAndCheckResults();7374// should contain75testRunner.init(expectedSummary);76try {77output.shouldContain("unexpected-stuff");78} catch (RuntimeException e) { }79testRunner.closeAndCheckResults();8081// should not match82testRunner.init(expectedSummary);83try {84output.shouldNotMatch("[a]");85} catch (RuntimeException e) { }86testRunner.closeAndCheckResults();8788// should match89testRunner.init(expectedSummary);90try {91output.shouldMatch("[qwerty]");92} catch (RuntimeException e) { }93testRunner.closeAndCheckResults();9495}9697private static class DiagnosticSummaryTestRunner {98private ByteArrayOutputStream byteStream =99new ByteArrayOutputStream(10000);100101private String expectedSummary = "";102private PrintStream errStream;103104105public void init(String expectedSummary) {106this.expectedSummary = expectedSummary;107byteStream.reset();108errStream = new PrintStream(byteStream);109System.setErr(errStream);110}111112public void closeAndCheckResults() {113// check results114errStream.close();115String stdErrStr = byteStream.toString();116if (!stdErrStr.contains(expectedSummary)) {117throw new RuntimeException("The output does not contain "118+ "the diagnostic message, or the message is incorrect");119}120}121}122123}124125126