1 package net.sourceforge.cruisecontrol.publishers; 2 3 import junit.framework.TestCase; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.OutputStream ; 7 import java.net.Socket ; 8 9 public class SocketPublisherTest extends TestCase { 10 11 public void testSocketPublisherSendsSuccess() throws Exception { 12 final MockSocket mockSocket = new MockSocket(); 13 SocketFactory factory = new SocketFactory() { 14 public Socket createSocket(String server, int port) { 15 return mockSocket; 16 } 17 }; 18 19 SocketPublisher socketPublisher = new SocketPublisher(factory); 20 socketPublisher.setSocketServer("localhost"); 21 socketPublisher.setPort("1555"); 22 socketPublisher.writeToSocket("Success"); 23 24 assertEquals("Success", mockSocket.toString()); 25 } 26 27 private static class MockSocket extends Socket { 28 29 private final ByteArrayOutputStream out = new ByteArrayOutputStream (); 30 31 public OutputStream getOutputStream() throws IOException { 32 return out; 33 } 34 35 public synchronized void close() throws IOException { 36 } 37 38 public String toString() { 39 return new String (out.toByteArray()); 40 } 41 } 42 43 44 45 } 46 | Popular Tags |