1/*
2 * Copyright (c) 1997, 2016, 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 */
23package org.netbeans.jemmy;
24
25import java.io.BufferedReader;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.InputStreamReader;
29import java.io.PrintStream;
30import java.io.PrintWriter;
31
32/**
33 *
34 * Test output.
35 *
36 * @author Alexandre Iline (alexandre.iline@oracle.com)
37 */
38public class TestOut {
39
40    private InputStream input;
41    private PrintWriter output;
42    private PrintWriter errput;
43    private PrintWriter golden_output;
44    private BufferedReader buffInput;
45    private boolean autoFlushMode = true;
46
47    /**
48     * Constructor.
49     *
50     * @param in Input stream
51     * @param out Output stream
52     * @param err Errput stream
53     */
54    public TestOut(InputStream in, PrintStream out, PrintStream err) {
55        this(in, out, err, null);
56    }
57
58    /**
59     * Constructor.
60     *
61     * @param in Input stream
62     * @param out Output stream
63     * @param err Errput stream
64     * @param golden Golgen output stream
65     */
66    public TestOut(InputStream in, PrintStream out, PrintStream err, PrintStream golden) {
67        super();
68        PrintWriter tout = null;
69        if (out != null) {
70            tout = new PrintWriter(out);
71        }
72        PrintWriter terr = null;
73        if (err != null) {
74            terr = new PrintWriter(err);
75        }
76        PrintWriter tgolden = null;
77        if (golden != null) {
78            tgolden = new PrintWriter(golden);
79        }
80        initStreams(in, tout, terr, tgolden);
81    }
82
83    /**
84     * Constructor.
85     *
86     * @param in Input stream
87     * @param out Output stream
88     * @param err Errput stream
89     */
90    public TestOut(InputStream in, PrintWriter out, PrintWriter err) {
91        this(in, out, err, null);
92    }
93
94    /**
95     * Constructor.
96     *
97     * @param in Input stream
98     * @param out Output stream
99     * @param err Errput stream
100     * @param golden Golgen output stream
101     */
102    public TestOut(InputStream in, PrintWriter out, PrintWriter err, PrintWriter golden) {
103        super();
104        initStreams(in, out, err, golden);
105        autoFlushMode = true;
106    }
107
108    /**
109     * Creates unstance using System.in, System.out and System.err streams.
110     */
111    public TestOut() {
112        this(System.in,
113                new PrintWriter(System.out),
114                new PrintWriter(System.err),
115                null);
116    }
117
118    /**
119     * Creates output which does not print any message anywhere.
120     *
121     * @return a TestOut object which does not print any message anywhere.
122     */
123    public static TestOut getNullOutput() {
124        return new TestOut((InputStream) null, (PrintWriter) null, (PrintWriter) null);
125    }
126
127    /**
128     * Specifies either flush is invoked after each output.
129     *
130     * @param autoFlushMode If true flush is invoking after each output.
131     * @return Old value of the auto flush mode.
132     * @see #getAutoFlushMode
133     */
134    public boolean setAutoFlushMode(boolean autoFlushMode) {
135        boolean oldValue = getAutoFlushMode();
136        this.autoFlushMode = autoFlushMode;
137        return oldValue;
138    }
139
140    /**
141     * Says if flush is invoked after each output.
142     *
143     * @return Value of the auto flush mode.
144     * @see #setAutoFlushMode
145     */
146    public boolean getAutoFlushMode() {
147        return autoFlushMode;
148    }
149
150    /**
151     * Read one byte from input.
152     *
153     * @return an int from input stream.
154     * @exception IOException
155     */
156    public int read() throws IOException {
157        if (input != null) {
158            return input.read();
159        } else {
160            return -1;
161        }
162    }
163
164    /**
165     * Read a line from input.
166     *
167     * @return a line from input stream.
168     * @exception IOException
169     */
170    public String readLine() throws IOException {
171        if (buffInput != null) {
172            return buffInput.readLine();
173        } else {
174            return null;
175        }
176    }
177
178    /**
179     * Prints a line into output.
180     *
181     * @param line a string to print into output stream.
182     */
183    public void print(String line) {
184        if (output != null) {
185            output.print(line);
186        }
187    }
188
189    /**
190     * Prints a line and then terminate the line by writing the line separator
191     * string.
192     *
193     * @param line a string to print into output stream.
194     */
195    public void printLine(String line) {
196        if (output != null) {
197            output.println(line);
198            if (autoFlushMode) {
199                output.flush();
200            }
201        }
202    }
203
204    /**
205     * Prints a line into golden output.
206     *
207     * @param line a string to print into golden output stream.
208     */
209    public void printGolden(String line) {
210        if (golden_output != null) {
211            golden_output.println(line);
212            if (autoFlushMode) {
213                golden_output.flush();
214            }
215        }
216    }
217
218    /**
219     * Prints a line into error output.
220     *
221     * @param line a string to print into error output stream.
222     */
223    public void printErrLine(String line) {
224        if (errput != null) {
225            errput.println(line);
226            if (autoFlushMode) {
227                errput.flush();
228            }
229        }
230    }
231
232    /**
233     * Prints a line into either output or errput.
234     *
235     * @param toOut If true prints a line into output.
236     * @param line a string to print.
237     */
238    public void printLine(boolean toOut, String line) {
239        if (toOut) {
240            printLine(line);
241        } else {
242            printErrLine(line);
243        }
244    }
245
246    /**
247     * Prints a trace line.
248     *
249     * @param text a trace text.
250     */
251    public void printTrace(String text) {
252        printLine("Trace:");
253        printLine(text);
254    }
255
256    /**
257     * Prints a error line.
258     *
259     * @param text a error text.
260     */
261    public void printError(String text) {
262        printErrLine("Error:");
263        printErrLine(text);
264    }
265
266    /**
267     * Prints an exception stack trace into error stream.
268     *
269     * @param e exception
270     */
271    public void printStackTrace(Throwable e) {
272        if (errput != null) {
273            e.printStackTrace(errput);
274            if (autoFlushMode) {
275                errput.flush();
276            }
277        }
278    }
279
280    /**
281     * Returns input stream.
282     *
283     * @return an input stream
284     */
285    public InputStream getInput() {
286        return input;
287    }
288
289    /**
290     * Returns output writer.
291     *
292     * @return an output stream
293     */
294    public PrintWriter getOutput() {
295        return output;
296    }
297
298    /**
299     * Returns errput writer.
300     *
301     * @return a error stream
302     */
303    public PrintWriter getErrput() {
304        return errput;
305    }
306
307    /**
308     * Returns golden output writer.
309     *
310     * @return a golden output stream
311     */
312    public PrintWriter getGolden() {
313        return golden_output;
314    }
315
316    /**
317     * Creates an output which prints only error messages.
318     *
319     * @return a TestOut instance which has only error stream.
320     */
321    public TestOut createErrorOutput() {
322        return new TestOut(null, null, getErrput());
323    }
324
325    /**
326     * Flushes all output threads.
327     */
328    public void flush() {
329        if (output != null) {
330            output.flush();
331        }
332        if (errput != null) {
333            errput.flush();
334        }
335        if (golden_output != null) {
336            golden_output.flush();
337        }
338    }
339
340    private void initStreams(InputStream in, PrintWriter out, PrintWriter err, PrintWriter golden) {
341        input = in;
342        output = out;
343        errput = err;
344        golden_output = golden;
345        if (input != null) {
346            buffInput = new BufferedReader(new InputStreamReader(in));
347        } else {
348            buffInput = null;
349        }
350    }
351}
352