Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

StreamsHandler.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.util;
  14. import java.io.PrintStream;
  15. /**
  16. * Manage system err and system out streams.
  17. * Clients can suppress stream output during StreamsHandler lifecycle
  18. * and intermittantly listen to both streams if signalled on construction.
  19. * To print to the underlying streams (without hiding or listening),
  20. * use either the log methods (which manage lineation)
  21. * or the out and err fields.
  22. * <pre>
  23. * boolean hideStreams = true;
  24. * boolean listen = true;
  25. * StreamsHander streams = new StreamsHander(hideStreams, listen);
  26. * streams.startListening();
  27. * ...
  28. * streams.out.println("this goes out to without listening"); XXX verify
  29. * StreamsHandler.Result result = streams.stopListening();
  30. * streams.restoreStreams();
  31. * System.out.println("Suppressed output stream follows");
  32. * System.out.print(result.out);
  33. * System.out.println("Suppressed error stream follows");
  34. * System.out.print(result.err);
  35. * </pre>
  36. * Warning: does not distinguish streams from different threads.
  37. */
  38. public class StreamsHandler {
  39. /** real output stream and sink for log if logToOut */
  40. public final PrintStream out;
  41. /** real error stream and sink for log if !logToOut */
  42. public final PrintStream err;
  43. /** if true, then can listen using startListening() */
  44. protected final boolean listening;
  45. /** if logToOut, then out, else err */
  46. private final PrintStream log;
  47. /** true if the last logged item was a newline */
  48. private boolean loggedLine;
  49. /** sniffs stream to gather test output to System.out */
  50. protected StreamSniffer outSniffer;
  51. /** sniffs stream to gather test output to System.err */
  52. protected StreamSniffer errSniffer;
  53. /** permits us to hide output stream (after sniffing by outSniffer */
  54. protected ProxyPrintStream outDelegate;
  55. /** permits us to hide error stream (after sniffing by errSniffer */
  56. protected ProxyPrintStream errDelegate;
  57. /** when sniffing, this has sniffed contents of output stream */
  58. protected StringBuffer outListener;
  59. /** when sniffing, this has sniffed contents of error stream */
  60. protected StringBuffer errListener;
  61. /** @param hide if true, then suppress stream output (can still listen) */
  62. public StreamsHandler(boolean listen) {
  63. this(listen, false);
  64. }
  65. /**
  66. * @param listen possible to sniff streams only if true
  67. * @param logToOut if true, then log methods go to System.out -- otherwise, System.err.
  68. */
  69. public StreamsHandler(
  70. boolean listen,
  71. boolean logToOut) {
  72. this.err = System.err;
  73. this.out = System.out;
  74. outDelegate = new ProxyPrintStream(System.out);
  75. errDelegate = new ProxyPrintStream(System.err);
  76. this.listening = listen;
  77. // final PrintStream HIDE = NullPrintStream.NULL_PrintStream;
  78. outSniffer = new StreamSniffer(outDelegate);
  79. System.setOut(new PrintStream(outSniffer));
  80. errSniffer = new StreamSniffer(errDelegate);
  81. System.setErr(new PrintStream(errSniffer));
  82. log = (logToOut ? this.out : this.err);
  83. loggedLine = true;
  84. }
  85. /** render output and error streams (after sniffing) */
  86. public void show() {
  87. outDelegate.show();
  88. errDelegate.show();
  89. }
  90. /** suppress output and error streams (after sniffing) */
  91. public void hide() {
  92. outDelegate.hide();
  93. errDelegate.hide();
  94. }
  95. /** restore streams. Do not use this after restoring. */
  96. public void restoreStreams() {
  97. if (null != outSniffer) {
  98. outSniffer = null;
  99. errSniffer = null;
  100. System.setOut(out);
  101. System.setErr(err);
  102. }
  103. }
  104. /** @return PrintStream used for direct logging */
  105. public PrintStream getLogStream() {
  106. return log;
  107. }
  108. /** log item without newline. */
  109. public void log(String s) {
  110. log.print(s);
  111. if (loggedLine) {
  112. loggedLine = false;
  113. }
  114. }
  115. /**
  116. * Log item with newline.
  117. * If previous log did not have a newline,
  118. * then this prepends a newline.
  119. */
  120. public void lnlog(String s) {
  121. if (!loggedLine) {
  122. log.println("");
  123. }
  124. log.println(s);
  125. }
  126. /**
  127. * Start listening to both streams.
  128. * Tosses any old data captured.
  129. * (Has no effect if not listening.)
  130. * @throws IllegalStateException if called after restoreStreams()
  131. * @see endListening()
  132. */
  133. public void startListening() {
  134. if (null == outSniffer) {
  135. throw new IllegalStateException("no listening after restore");
  136. }
  137. if (listening) {
  138. if (null != outListener) {
  139. outListener.setLength(0);
  140. errListener.setLength(0);
  141. } else {
  142. outListener = new StringBuffer();
  143. outSniffer.setBuffer(outListener);
  144. errListener = new StringBuffer();
  145. errSniffer.setBuffer(errListener);
  146. }
  147. }
  148. }
  149. /**
  150. * End listening to both streams and return data captured.
  151. * Must call startListening() first.
  152. * @throws IllegalStateException if called when not listening
  153. * @return Result with sniffed output and error String
  154. * @see startListening()
  155. */
  156. public Result endListening() {
  157. return endListening(true);
  158. }
  159. /**
  160. * End listening to both streams and return data captured.
  161. * Must call startListening() first.
  162. * @param getResult if false, return Result.EMPTY
  163. * and avoid converting buffer to String.
  164. * @throws IllegalStateException if called when not listening
  165. * @return Result with sniffed output and error String
  166. * @see startListening()
  167. */
  168. public Result endListening(boolean getResult) {
  169. if (!listening) {
  170. return Result.EMPTY;
  171. }
  172. if (null == outListener) {
  173. throw new IllegalStateException("listening not started");
  174. }
  175. Result result = (!getResult ? Result.EMPTY
  176. : new Result(outListener.toString(), errListener.toString()));
  177. errListener = null;
  178. outListener = null;
  179. return result;
  180. }
  181. /** output and error String */
  182. public static class Result {
  183. static final Result EMPTY = new Result(null, null);
  184. public final String out;
  185. public final String err;
  186. private Result(String out, String err) {
  187. this.out = out;
  188. this.err = err;
  189. }
  190. }
  191. }