KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > SocketPublisherTest


1 package net.sourceforge.cruisecontrol.publishers;
2
3 import junit.framework.TestCase;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.net.Socket JavaDoc;
8
9 public class SocketPublisherTest extends TestCase {
10
11     public void testSocketPublisherSendsSuccess() throws Exception JavaDoc {
12         final MockSocket mockSocket = new MockSocket();
13         SocketFactory factory = new SocketFactory() {
14             public Socket JavaDoc createSocket(String JavaDoc 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 JavaDoc {
28
29         private final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
30
31         public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
32             return out;
33         }
34
35         public synchronized void close() throws IOException JavaDoc {
36         }
37
38         public String JavaDoc toString() {
39             return new String JavaDoc(out.toByteArray());
40         }
41     }
42
43
44
45 }
46
Popular Tags