1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.gshell.io;
21  
22  import junit.framework.TestCase;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.PrintStream;
26  
27  /**
28   * Tests for the {@link SystemOutputHijacker} class.
29   *
30   * @version $Rev: 722797 $ $Date: 2008-12-03 08:18:16 +0100 (Wed, 03 Dec 2008) $
31   */
32  public class SystemOutputHijackerTest
33      extends TestCase
34  {
35      private ByteArrayOutputStream buff;
36  
37      private PrintStream out;
38      
39      protected void setUp() throws Exception {
40          buff = new ByteArrayOutputStream();
41          out = new PrintStream(buff);
42  
43          assertFalse(SystemOutputHijacker.isInstalled());
44      }
45  
46      protected void tearDown() throws Exception {
47          buff = null;
48          out = null;
49  
50          assertFalse(SystemOutputHijacker.isInstalled());
51      }
52  
53      private void installOut() throws Exception {
54          assertFalse(SystemOutputHijacker.isRegistered());
55  
56          SystemOutputHijacker.install(out);
57  
58          assertTrue(SystemOutputHijacker.isInstalled());
59          assertTrue(SystemOutputHijacker.isRegistered());
60      }
61  
62      private void deregisterAndUninstall() throws Exception {
63          SystemOutputHijacker.deregister();
64  
65          assertFalse(SystemOutputHijacker.isRegistered());
66  
67          SystemOutputHijacker.uninstall();
68  
69          assertFalse(SystemOutputHijacker.isInstalled());
70      }
71  
72      public void testInstallWithStream() throws Exception {
73          System.out.println("before");
74  
75          installOut();
76          
77          try {
78              System.out.print("hijacked");
79          }
80          finally {
81              deregisterAndUninstall();
82          }
83          
84          System.out.println("after");
85          
86          String msg = new String(buff.toByteArray());
87          
88          assertEquals("hijacked", msg);
89      }
90      
91      public void testInstallRegisterWithStream() throws Exception {
92          System.out.println("before");
93          
94          SystemOutputHijacker.install();
95          assertTrue(SystemOutputHijacker.isInstalled());
96  
97          assertFalse(SystemOutputHijacker.isRegistered());
98          SystemOutputHijacker.register(out);
99          assertTrue(SystemOutputHijacker.isRegistered());
100         
101         try {
102             System.out.print("hijacked");
103         }
104         finally {
105             deregisterAndUninstall();
106         }
107         
108         System.out.println("after");
109         
110         String msg = new String(buff.toByteArray());
111         
112         assertEquals("hijacked", msg);
113     }
114     
115     public void testDualStreams() throws Exception {
116         ByteArrayOutputStream errBuff = new ByteArrayOutputStream();
117         PrintStream err = new PrintStream(errBuff);
118         
119         System.out.println("before");
120         System.err.println("BEFORE");
121         
122         SystemOutputHijacker.install(out, err);
123         
124         assertTrue(SystemOutputHijacker.isInstalled());
125         assertTrue(SystemOutputHijacker.isRegistered());
126         
127         try {
128             System.out.print("hijacked");
129             System.err.print("HIJACKED");
130         }
131         finally {
132             deregisterAndUninstall();
133         }
134         
135         System.out.println("after");
136         System.err.println("AFTER");
137         
138         assertEquals("hijacked", new String(buff.toByteArray()));
139         assertEquals("HIJACKED", new String(errBuff.toByteArray()));
140     }
141     
142     public void testChildThreads() throws Exception {
143         System.out.println("before");
144         
145         installOut();
146         
147         Runnable task = new Runnable() {
148             public void run() {
149                 System.out.print("hijacked");
150             }
151         };
152         
153         try {
154             System.out.print("<");
155             
156             Thread t = new Thread(task);
157             t.start();
158             t.join();
159             
160             System.out.print(">");
161         }
162         finally {
163             deregisterAndUninstall();
164         }
165         
166         System.out.println("after");
167         
168         String msg = new String(buff.toByteArray());
169         
170         assertEquals("<hijacked>", msg);
171     }
172     
173     public void testNestedRegistration() throws Exception {
174         System.out.println("before");
175 
176         installOut();
177         
178         try {
179             System.out.print("hijacked");
180             
181             ByteArrayOutputStream childBuff = new ByteArrayOutputStream();
182             PrintStream childOut = new PrintStream(childBuff);
183             
184             System.out.print("!");
185             
186             SystemOutputHijacker.register(childOut);
187             
188             try {
189                 System.out.print("child");
190             }
191             finally {
192                 SystemOutputHijacker.deregister();
193             }
194 
195             System.out.print("!");
196 
197             assertEquals("child", new String(childBuff.toByteArray()));
198         }
199         finally {
200             deregisterAndUninstall();
201         }
202         
203         System.out.println("after");
204         
205         String msg = new String(buff.toByteArray());
206         
207         assertEquals("hijacked!!", msg);
208     }
209 }