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