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