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