1/*
2 * Copyright (c) 2007, 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
24package test.java.awt.regtesthelpers.process;
25
26import java.io.PrintStream;
27
28/**
29 * The calss incapsulates process related information and methods to
30 * handle this information.
31 *
32 */
33
34public class ProcessResults {
35    private int exitValue;
36    private StringBuilder stdout;
37    private StringBuilder stderr;
38
39    public ProcessResults() {
40        exitValue = -1;
41        stdout = new StringBuilder();
42        stderr = new StringBuilder();
43    }
44
45    public synchronized int getExitValue () {
46        return exitValue;
47    }
48
49    public synchronized String getStdOut() {
50        return stdout.toString();
51    }
52
53   public synchronized String getStdErr() {
54        return stderr.toString();
55    }
56
57    /** Prints child process standart output into the desired PrintStream.
58     *
59     * @param printTo PrintStraem where output have to be redirected
60     */
61    public synchronized void printProcessStandartOutput(PrintStream printTo) {
62        if (stdout != null && stdout.length() > 0) {
63            printTo.println("========= Child VM System.out ========");
64            printTo.print(stdout);
65            printTo.println("======================================");
66        }
67    }
68
69    /** Prints child process error output into the desired PrintStream.
70     *
71     * @param printTo PrintStraem where output have to be redirected
72     */
73    public synchronized void printProcessErrorOutput(PrintStream printTo) {
74        if (stderr != null && stderr.length() > 0) {
75            printTo.println("========= Child VM System.err ========");
76            printTo.print(stderr);
77            printTo.println("======================================");
78        }
79    }
80
81    /**  Prints child process error output into the desired {@code PrintStream},
82     *   if child JVM error output contains any of the next words: "error",
83     *   "exception". We cannot be sure that the test is failed when stderr is
84     *   not empty, because in error stream could be written some debug information.
85     *
86     * @param err PrintStraem where output have to be redirected
87     */
88    public synchronized void verifyStdErr(PrintStream err) {
89        if (stderr != null && ((stderr.toString().toLowerCase().indexOf("error") != -1)
90                || (stderr.toString().toLowerCase().indexOf("exception") != -1)))
91        {
92            printProcessErrorOutput(err);
93            throw new RuntimeException("WARNING: Child process  error stream " +
94                    "is not empty!");
95        }
96    }
97
98    /**  Throws new RuntimeException if the child JVM returns not 0 value.
99     *
100     * @param err PrintStraem where output have to be redirected
101     */
102    public synchronized void verifyProcessExitValue(PrintStream err) {
103        if (exitValue != 0) {
104            throw new RuntimeException("Child process returns not 0 value!" +
105                    "Returned value is " + exitValue);
106        }
107    }
108
109    public void verifyProcessExecutionResults(PrintStream err) {
110        // the next functions are synchronized
111        verifyStdErr(err);
112        verifyProcessExitValue(err);
113    }
114
115    synchronized void appendToStdOut(char c) {
116        stdout.append(c);
117    }
118
119    synchronized void appendToStdErr(char c) {
120        stderr.append(c);
121    }
122
123    synchronized void setExitValue(int exitValue) {
124        this.exitValue = exitValue;
125    }
126}
127