1 37 package net.sourceforge.cruisecontrol.util; 38 39 import junit.framework.TestCase; 40 41 import java.io.ByteArrayInputStream ; 42 import java.io.ByteArrayOutputStream ; 43 import java.io.InputStream ; 44 import java.io.PrintStream ; 45 import java.util.ArrayList ; 46 import java.util.List ; 47 48 52 public class StreamPumperTest extends TestCase { 53 54 public void testPumping() { 55 String line1 = "line1"; 56 String line2 = "line2"; 57 String lines = line1 + "\n" + line2; 58 ByteArrayInputStream inputStream = 59 new ByteArrayInputStream (lines.getBytes()); 60 61 TestConsumer consumer = new TestConsumer(); 62 StreamPumper pumper = new StreamPumper(inputStream, consumer); 63 new Thread (pumper).run(); 64 65 assertTrue(consumer.wasLineConsumed(line1, 1000)); 67 assertTrue(consumer.wasLineConsumed(line2, 1000)); 68 } 69 70 public void testNoSystemOut() { 71 PrintStream oldOut = System.out; 72 ByteArrayOutputStream newOut = new ByteArrayOutputStream (); 73 try { 74 System.setOut(new PrintStream (newOut)); 75 InputStream input = new ByteArrayInputStream ( 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 88 class TestConsumer implements StreamConsumer { 89 90 private List lines = new ArrayList (); 91 92 101 public boolean wasLineConsumed(String 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 try { 113 Thread.sleep(10); 114 } catch (InterruptedException e) { 115 } 117 118 trialTime = System.currentTimeMillis() - start; 120 121 } while (trialTime < timeout); 122 123 return false; 125 } 126 127 public void consumeLine(String line) { 128 lines.add(line); 129 } 130 } 131 | Popular Tags |