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