hotspot/test/testlibrary_tests/OutputAnalyzerReportingTest.java
author ctornqvi
Fri, 19 Aug 2016 10:06:30 -0400
changeset 40631 ed82623d7831
parent 36851 03e2f4d0a421
permissions -rw-r--r--
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder Reviewed-by: coleenp, gtriantafill, mseledtsov, iignatyev, dholmes, dsamersoff
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
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36851
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
19141
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.)
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36851
diff changeset
    30
 * @library /test/lib
36851
03e2f4d0a421 8153737: Unsupported Module
chegar
parents: 30604
diff changeset
    31
 * @modules java.base/jdk.internal.misc
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 20280
diff changeset
    32
 *          java.management
19141
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    33
 */
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    34
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    35
import java.io.ByteArrayOutputStream;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    36
import java.io.PrintStream;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    37
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36851
diff changeset
    38
import jdk.test.lib.process.OutputAnalyzer;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36851
diff changeset
    39
import jdk.test.lib.process.ProcessTools;
19141
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    40
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 class OutputAnalyzerReportingTest {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    43
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    45
        // Create the output analyzer under test
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    46
        String stdout = "aaaaaa";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    47
        String stderr = "bbbbbb";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    48
        OutputAnalyzer output = new OutputAnalyzer(stdout, stderr);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    49
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    50
        // Expected summary values should be the same for all cases,
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    51
        // since the outputAnalyzer object is the same
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    52
        String expectedExitValue = "-1";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    53
        String expectedSummary =
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    54
                " stdout: [" + stdout + "];\n" +
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    55
                " stderr: [" + stderr + "]\n" +
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    56
                " exitValue = " + expectedExitValue + "\n";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    57
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    58
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    59
        DiagnosticSummaryTestRunner testRunner =
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    60
                new DiagnosticSummaryTestRunner();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    61
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    62
        // should have exit value
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    63
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    64
        int unexpectedExitValue = 2;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    65
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    66
            output.shouldHaveExitValue(unexpectedExitValue);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    67
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    68
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    69
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    70
        // should not contain
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    71
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    72
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    73
            output.shouldNotContain(stdout);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    74
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    75
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    76
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    77
        // should contain
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    78
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    79
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    80
            output.shouldContain("unexpected-stuff");
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    81
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    82
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    83
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    84
        // should not match
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    85
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    86
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    87
            output.shouldNotMatch("[a]");
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    88
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    89
        testRunner.closeAndCheckResults();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    90
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    91
        // should match
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    92
        testRunner.init(expectedSummary);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    93
        try {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    94
            output.shouldMatch("[qwerty]");
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    95
        } catch (RuntimeException e) { }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    96
        testRunner.closeAndCheckResults();
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
    }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
    99
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   100
    private static class DiagnosticSummaryTestRunner {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   101
        private ByteArrayOutputStream byteStream =
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   102
                new ByteArrayOutputStream(10000);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   103
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   104
        private String expectedSummary = "";
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   105
        private PrintStream errStream;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   106
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   107
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   108
        public void init(String expectedSummary) {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   109
            this.expectedSummary = expectedSummary;
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   110
            byteStream.reset();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   111
            errStream = new PrintStream(byteStream);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   112
            System.setErr(errStream);
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   113
        }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   114
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   115
        public void closeAndCheckResults() {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   116
            // check results
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   117
            errStream.close();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   118
            String stdErrStr = byteStream.toString();
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   119
            if (!stdErrStr.contains(expectedSummary)) {
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   120
                throw new RuntimeException("The output does not contain "
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   121
                    + "the diagnostic message, or the message is incorrect");
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
    }
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   125
1e8184e44a8c 8020614: OutputAnalyzer.shouldHaveExitValue() should print stdout/stderr output
mseledtsov
parents:
diff changeset
   126
}