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