KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > StreamPumperTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.util;
38
39 import junit.framework.TestCase;
40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.io.PrintStream JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.List JavaDoc;
47
48 /**
49  *
50  * @author <a HREF="mailto:pj@thoughtworks.com">Paul Julius</a>
51  */

52 public class StreamPumperTest extends TestCase {
53
54     public void testPumping() {
55         String JavaDoc line1 = "line1";
56         String JavaDoc line2 = "line2";
57         String JavaDoc lines = line1 + "\n" + line2;
58         ByteArrayInputStream JavaDoc inputStream =
59                 new ByteArrayInputStream JavaDoc(lines.getBytes());
60
61         TestConsumer consumer = new TestConsumer();
62         StreamPumper pumper = new StreamPumper(inputStream, consumer);
63         new Thread JavaDoc(pumper).run();
64
65         //Check the consumer to see if it got both lines.
66
assertTrue(consumer.wasLineConsumed(line1, 1000));
67         assertTrue(consumer.wasLineConsumed(line2, 1000));
68     }
69
70     public void testNoSystemOut() {
71         PrintStream JavaDoc oldOut = System.out;
72         ByteArrayOutputStream JavaDoc newOut = new ByteArrayOutputStream JavaDoc();
73         try {
74             System.setOut(new PrintStream JavaDoc(newOut));
75             InputStream JavaDoc input = new ByteArrayInputStream JavaDoc(
76                     "some input".getBytes());
77             new StreamPumper(input, null, null).run();
78             assertEquals(0, newOut.toByteArray().length);
79         } finally {
80             System.setOut(oldOut);
81         }
82     }
83 }
84
85 /**
86  * Used by the test to track whether a line actually got consumed or not.
87  */

88 class TestConsumer implements StreamConsumer {
89
90     private List JavaDoc lines = new ArrayList JavaDoc();
91
92     /**
93      * Checks to see if this consumer consumed a particular line. This method
94      * will wait up to timeout number of milliseconds for the line to get
95      * consumed.
96      *
97      * @param testLine Line to test for.
98      * @param timeout Number of milliseconds to wait for the line.
99      * @return true if the line gets consumed, else false.
100      */

101     public boolean wasLineConsumed(String JavaDoc testLine, long timeout) {
102
103         long start = System.currentTimeMillis();
104         long trialTime = 0;
105
106         do {
107             if (lines.contains(testLine)) {
108                 return true;
109             }
110
111             //Sleep a bit.
112
try {
113                 Thread.sleep(10);
114             } catch (InterruptedException JavaDoc e) {
115                 //ignoring...
116
}
117
118             //How long have been waiting for the line?
119
trialTime = System.currentTimeMillis() - start;
120
121         } while (trialTime < timeout);
122
123         //If we got here, then the line wasn't consume within the timeout
124
return false;
125     }
126
127     public void consumeLine(String JavaDoc line) {
128         lines.add(line);
129     }
130 }
131
Popular Tags