001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id: TestOut.java,v 1.6 2007/10/05 11:36:05 jskrivanek Exp $ $Revision: 1.6 $ $Date: 2007/10/05 11:36:05 $
044: *
045: */
046:
047: package org.netbeans.jemmy;
048:
049: import java.io.BufferedReader;
050: import java.io.InputStream;
051: import java.io.InputStreamReader;
052: import java.io.IOException;
053: import java.io.PrintStream;
054: import java.io.PrintWriter;
055:
056: /**
057: *
058: * Test output.
059: *
060: * @author Alexandre Iline (alexandre.iline@sun.com)
061: */
062:
063: public class TestOut {
064:
065: private InputStream input;
066: private PrintWriter output;
067: private PrintWriter errput;
068: private PrintWriter golden_output;
069: private BufferedReader buffInput;
070: private boolean autoFlushMode = true;
071:
072: /**
073: * Constructor.
074: * @param in Input stream
075: * @param out Output stream
076: * @param err Errput stream
077: */
078: public TestOut(InputStream in, PrintStream out, PrintStream err) {
079: this (in, out, err, null);
080: }
081:
082: /**
083: * Constructor.
084: * @param in Input stream
085: * @param out Output stream
086: * @param err Errput stream
087: * @param golden Golgen output stream
088: */
089: public TestOut(InputStream in, PrintStream out, PrintStream err,
090: PrintStream golden) {
091: super ();
092: PrintWriter tout = null;
093: if (out != null) {
094: tout = new PrintWriter(out);
095: }
096: PrintWriter terr = null;
097: if (err != null) {
098: terr = new PrintWriter(err);
099: }
100: PrintWriter tgolden = null;
101: if (golden != null) {
102: tgolden = new PrintWriter(golden);
103: }
104: initStreams(in, tout, terr, tgolden);
105: }
106:
107: /**
108: * Constructor.
109: * @param in Input stream
110: * @param out Output stream
111: * @param err Errput stream
112: */
113: public TestOut(InputStream in, PrintWriter out, PrintWriter err) {
114: this (in, out, err, null);
115: }
116:
117: /**
118: * Constructor.
119: * @param in Input stream
120: * @param out Output stream
121: * @param err Errput stream
122: * @param golden Golgen output stream
123: */
124: public TestOut(InputStream in, PrintWriter out, PrintWriter err,
125: PrintWriter golden) {
126: super ();
127: initStreams(in, out, err, golden);
128: autoFlushMode = true;
129: }
130:
131: /**
132: * Creates unstance using System.in, System.out and System.err streams.
133: */
134: public TestOut() {
135: this (System.in, new PrintWriter(System.out), new PrintWriter(
136: System.err), null);
137: }
138:
139: /**
140: * Creates output which does not print any message anywhere.
141: * @return a TestOut object which does not print any message anywhere.
142: */
143: public static TestOut getNullOutput() {
144: return (new TestOut((InputStream) null, (PrintWriter) null,
145: (PrintWriter) null));
146: }
147:
148: /**
149: * Specifies either flush is invoked after each output.
150: * @param autoFlushMode If true flush is invoking after each output.
151: * @return Old value of the auto flush mode.
152: * @see #getAutoFlushMode
153: */
154: public boolean setAutoFlushMode(boolean autoFlushMode) {
155: boolean oldValue = getAutoFlushMode();
156: this .autoFlushMode = autoFlushMode;
157: return (oldValue);
158: }
159:
160: /**
161: * Says if flush is invoked after each output.
162: * @return Value of the auto flush mode.
163: * @see #setAutoFlushMode
164: */
165: public boolean getAutoFlushMode() {
166: return (autoFlushMode);
167: }
168:
169: /**
170: * Read one byte from input.
171: * @return an int from input stream.
172: * @exception IOException
173: */
174: public int read() throws IOException {
175: if (input != null) {
176: return (input.read());
177: } else {
178: return (-1);
179: }
180: }
181:
182: /**
183: * Read a line from input.
184: * @return a line from input stream.
185: * @exception IOException
186: */
187: public String readLine() throws IOException {
188: if (buffInput != null) {
189: return (buffInput.readLine());
190: } else {
191: return (null);
192: }
193: }
194:
195: /**
196: * Prints a line into output.
197: * @param line a string to print into output stream.
198: */
199: public void print(String line) {
200: if (output != null) {
201: output.print(line);
202: }
203: }
204:
205: /**
206: * Prints a line and then terminate the line by writing the line separator string.
207: * @param line a string to print into output stream.
208: */
209: public void printLine(String line) {
210: if (output != null) {
211: output.println(line);
212: if (autoFlushMode) {
213: output.flush();
214: }
215: }
216: }
217:
218: /**
219: * Prints a line into golden output.
220: * @param line a string to print into golden output stream.
221: */
222: public void printGolden(String line) {
223: if (golden_output != null) {
224: golden_output.println(line);
225: if (autoFlushMode) {
226: golden_output.flush();
227: }
228: }
229: }
230:
231: /**
232: * Prints a line into error output.
233: * @param line a string to print into error output stream.
234: */
235: public void printErrLine(String line) {
236: if (errput != null) {
237: errput.println(line);
238: if (autoFlushMode) {
239: errput.flush();
240: }
241: }
242: }
243:
244: /**
245: * Prints a line into either output or errput.
246: * @param toOut If true prints a line into output.
247: * @param line a string to print.
248: */
249: public void printLine(boolean toOut, String line) {
250: if (toOut) {
251: printLine(line);
252: } else {
253: printErrLine(line);
254: }
255: }
256:
257: /**
258: * Prints a trace line.
259: * @param text a trace text.
260: */
261: public void printTrace(String text) {
262: printLine("Trace:");
263: printLine(text);
264: }
265:
266: /**
267: * Prints a error line.
268: * @param text a error text.
269: */
270: public void printError(String text) {
271: printErrLine("Error:");
272: printErrLine(text);
273: }
274:
275: /**
276: * Prints an exception stack trace into error stream.
277: * @param e exception
278: */
279: public void printStackTrace(Throwable e) {
280: if (errput != null) {
281: e.printStackTrace(errput);
282: if (autoFlushMode) {
283: errput.flush();
284: }
285: }
286: }
287:
288: /**
289: * Returns input stream.
290: * @return an input stream
291: */
292: public InputStream getInput() {
293: return (input);
294: }
295:
296: /**
297: * Returns output writer.
298: * @return an output stream
299: */
300: public PrintWriter getOutput() {
301: return (output);
302: }
303:
304: /**
305: * Returns errput writer.
306: * @return a error stream
307: */
308: public PrintWriter getErrput() {
309: return (errput);
310: }
311:
312: /**
313: * Returns golden output writer.
314: * @return a golden output stream
315: */
316: public PrintWriter getGolden() {
317: return (golden_output);
318: }
319:
320: /**
321: * Creates an output which prints only error messages.
322: * @return a TestOut instance which has only error stream.
323: */
324: public TestOut createErrorOutput() {
325: return (new TestOut(null, null, getErrput()));
326: }
327:
328: /**
329: * Flushes all output threads.
330: */
331: public void flush() {
332: if (output != null) {
333: output.flush();
334: }
335: if (errput != null) {
336: errput.flush();
337: }
338: if (golden_output != null) {
339: golden_output.flush();
340: }
341: }
342:
343: private void initStreams(InputStream in, PrintWriter out,
344: PrintWriter err, PrintWriter golden) {
345: input = in;
346: output = out;
347: errput = err;
348: golden_output = golden;
349: if (input != null) {
350: buffInput = new BufferedReader(new InputStreamReader(in));
351: } else {
352: buffInput = null;
353: }
354: }
355: }
|