1111
1212import static org .junit .Assert .*;
1313
14- @ RunWith (HierarchicalContextRunner .class )
1514public class SocketServerTest {
16- private ClosingSocketService service ;
1715 private SocketServer server ;
16+ private EchoSocketService echoService ;
1817 private int port ;
1918
2019 @ Before
21- public void setup () {
20+ public void setup () throws Exception {
2221 port = 8042 ;
22+ echoService = new EchoSocketService ();
23+ server = new SocketServer (port , echoService );
24+ }
25+
26+ @ After
27+ public void tearDown () throws Exception {
28+ server .stop ();
2329 }
2430
2531 public static abstract class TestSocketService implements SocketService {
2632 public void serve (Socket s ) {
2733 try {
2834 doService (s );
29- synchronized (this ) { notify (); }
35+ synchronized (this ) {
36+ notify ();
37+ }
3038 s .close ();
3139 } catch (IOException e ) {
3240 e .printStackTrace ();
@@ -36,112 +44,6 @@ public void serve(Socket s) {
3644 protected abstract void doService (Socket s ) throws IOException ;
3745 }
3846
39- public static class ClosingSocketService extends TestSocketService {
40- public int connections ;
41-
42- protected void doService (Socket s ) throws IOException {
43- connections ++;
44- }
45- }
46-
47- public class WithClosingSocketService {
48- @ Before
49- public void setUp () throws Exception {
50- service = new ClosingSocketService ();
51- server = new SocketServer (port , service );
52- }
53-
54- @ After
55- public void tearDown () throws Exception {
56- server .stop ();
57- }
58-
59- @ Test
60- public void instantiate () throws Exception {
61- assertEquals (port , server .getPort ());
62- assertEquals (service , server .getService ());
63- }
64-
65- @ Test
66- public void canStartAndStopServer () throws Exception {
67- server .start ();
68- assertTrue (server .isRunning ());
69- server .stop ();
70- assertFalse (server .isRunning ());
71- }
72-
73- @ Test
74- public void acceptsAnIncomingConnection () throws Exception {
75- // TODO - MDM - Possible Race Condition? Have seen hanging test.
76- server .start ();
77- new Socket ("localhost" , port );
78- synchronized (service ) {
79- service .wait ();
80- }
81- server .stop ();
82-
83- assertEquals (1 , service .connections );
84- }
85-
86- @ Test
87- public void acceptsMultipleIncomingConnections () throws Exception {
88- // TODO - MDM - Possible Race Condition? Have seen hanging test.
89- server .start ();
90- new Socket ("localhost" , port );
91- synchronized (service ) {
92- service .wait ();
93- }
94- new Socket ("localhost" , port );
95- synchronized (service ) {
96- service .wait ();
97- }
98- server .stop ();
99-
100- assertEquals (2 , service .connections );
101- }
102- }
103-
104- public static class ReadingSocketService extends TestSocketService {
105- public String message ;
106-
107- protected void doService (Socket s ) throws IOException {
108- InputStream is = s .getInputStream ();
109- InputStreamReader isr = new InputStreamReader (is );
110- BufferedReader br = new BufferedReader (isr );
111- message = br .readLine ();
112- }
113- }
114-
115- public class WithReadingSocketService {
116- private ReadingSocketService readingService ;
117-
118- @ Before
119- public void setup () throws Exception {
120- readingService = new ReadingSocketService ();
121- server = new SocketServer (port , readingService );
122- }
123-
124- @ After
125- public void tearDown () throws Exception {
126- server .stop ();
127- }
128-
129- @ Test
130- public void canSendAndReceiveData () throws Exception {
131- // TODO - MDM - Possible Race Condition? Have seen hanging test.
132- server .start ();
133- Socket s = new Socket ("localhost" , port );
134- OutputStream os = s .getOutputStream ();
135- os .write ("hello\n " .getBytes ());
136- synchronized (readingService ) {
137- readingService .wait ();
138- }
139- server .stop ();
140-
141- assertEquals ("hello" , readingService .message );
142- }
143- }
144-
14547 public static class EchoSocketService extends TestSocketService {
14648
14749 protected void doService (Socket s ) throws IOException {
@@ -155,37 +57,25 @@ protected void doService(Socket s) throws IOException {
15557 }
15658 }
15759
158- public class WithEchoSocketService {
159- private EchoSocketService echoService ;
160-
161- @ Before
162- public void setup () throws Exception {
163- echoService = new EchoSocketService ();
164- server = new SocketServer ( port , echoService );
165- }
166-
167- @ After
168- public void tearDown () throws Exception {
169- server . stop ( );
170- }
60+ @ Test
61+ public void canEcho () throws Exception {
62+ server . start ();
63+ Thread . yield ();
64+ Socket s = new Socket ( "localhost" , port );
65+ OutputStream os = s . getOutputStream ();
66+ os . write ( "echo \n " . getBytes () );
67+ InputStream is = s . getInputStream ();
68+ InputStreamReader isr = new InputStreamReader ( is );
69+ BufferedReader br = new BufferedReader ( isr );
70+ String response = br . readLine ();
71+ assertEquals ( "echo" , response );
72+ }
17173
172- @ Test
173- public void canEcho () throws Exception {
174- // TODO - MDM - Possible Race Condition? Have seen hanging test.
175- server .start ();
176- Socket s = new Socket ("localhost" , port );
177- OutputStream os = s .getOutputStream ();
178- os .write ("echo\n " .getBytes ());
179- synchronized (echoService ) {
180- echoService .wait ();
181- }
182- InputStream is = s .getInputStream ();
183- InputStreamReader isr = new InputStreamReader (is );
184- BufferedReader br = new BufferedReader (isr );
185- String response = br .readLine ();
186- assertEquals ("echo" , response );
187- }
74+ @ Test
75+ public void multipleEchos () throws Exception
76+ {
77+
18878 }
79+ }
18980
19081
191- }
0 commit comments