hotspot/test/testlibrary_tests/OutputAnalyzerReportingTest.java
author coffeys
Sat, 11 Jan 2014 11:22:35 +0000
changeset 22263 9328773ff3c5
parent 20280 0305ea27e15b
child 29678 dd2f3932c21e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19141
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     1
/*
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     4
 *
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     8
 *
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    13
 * accompanied this code).
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    14
 *
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    18
 *
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    21
 * questions.
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    22
 */
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    23
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    24
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    25
/*
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    26
 * @test
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    27
 * @summary Test the OutputAnalyzer reporting functionality,
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    28
 *     such as printing additional diagnostic info
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    29
 *     (exit code, stdout, stderr, command line, etc.)
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    30
 * @library /testlibrary
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    31
 */
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    32
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    33
import java.io.ByteArrayOutputStream;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    34
import java.io.PrintStream;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    35
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    36
import com.oracle.java.testlibrary.OutputAnalyzer;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    37
import com.oracle.java.testlibrary.ProcessTools;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    38
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    39
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    40
public class OutputAnalyzerReportingTest {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    41
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    43
        // Create the output analyzer under test
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    44
        String stdout = "aaaaaa";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    45
        String stderr = "bbbbbb";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    46
        OutputAnalyzer output = new OutputAnalyzer(stdout, stderr);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    47
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    48
        // Expected summary values should be the same for all cases,
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    49
        // since the outputAnalyzer object is the same
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    50
        String expectedExitValue = "-1";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    51
        String expectedSummary =
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    52
                " stdout: [" + stdout + "];\n" +
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    53
                " stderr: [" + stderr + "]\n" +
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    54
                " exitValue = " + expectedExitValue + "\n";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    55
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    56
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    57
        DiagnosticSummaryTestRunner testRunner =
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    58
                new DiagnosticSummaryTestRunner();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    59
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    60
        // should have exit value
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    61
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    62
        int unexpectedExitValue = 2;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    63
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    64
            output.shouldHaveExitValue(unexpectedExitValue);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    65
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    66
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    67
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    68
        // should not contain
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    69
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    70
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    71
            output.shouldNotContain(stdout);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    72
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    73
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    74
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    75
        // should contain
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    76
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    77
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    78
            output.shouldContain("unexpected-stuff");
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    79
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    80
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    81
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    82
        // should not match
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    83
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    84
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    85
            output.shouldNotMatch("[a]");
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    86
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    87
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    88
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    89
        // should match
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    90
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    91
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    92
            output.shouldMatch("[qwerty]");
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    93
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    94
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    95
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    96
    }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    97
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    98
    private static class DiagnosticSummaryTestRunner {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    99
        private ByteArrayOutputStream byteStream =
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   100
                new ByteArrayOutputStream(10000);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   101
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   102
        private String expectedSummary = "";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   103
        private PrintStream errStream;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   104
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   105
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   106
        public void init(String expectedSummary) {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   107
            this.expectedSummary = expectedSummary;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   108
            byteStream.reset();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   109
            errStream = new PrintStream(byteStream);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   110
            System.setErr(errStream);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   111
        }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   112
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   113
        public void closeAndCheckResults() {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   114
            // check results
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   115
            errStream.close();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   116
            String stdErrStr = byteStream.toString();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   117
            if (!stdErrStr.contains(expectedSummary)) {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   118
                throw new RuntimeException("The output does not contain "
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   119
                    + "the diagnostic message, or the message is incorrect");
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   120
            }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   121
        }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   122
    }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   123
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   124
}