1 /* |
|
2 * Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 |
|
25 /* @test |
|
26 * @summary Test chat server chatserver test |
|
27 * |
|
28 * @library /src/sample/share/nio/chatserver |
|
29 * @build ChatTest ChatServer Client ClientReader DataReader MessageReader NameReader |
|
30 * @run testng ChatTest |
|
31 */ |
|
32 |
|
33 import java.io.*; |
|
34 import java.net.InetSocketAddress; |
|
35 import java.net.Socket; |
|
36 import java.util.ArrayList; |
|
37 import java.util.Collections; |
|
38 import java.util.List; |
|
39 import java.util.concurrent.CyclicBarrier; |
|
40 |
|
41 import org.testng.annotations.Test; |
|
42 |
|
43 public class ChatTest { |
|
44 public static int listeningPort = 0; |
|
45 |
|
46 @Test |
|
47 public static void doTest() throws Throwable { |
|
48 testStartStop(); |
|
49 testPortOpen(); |
|
50 testAsksForName(); |
|
51 testUseName(); |
|
52 testConnectDisconnectConnect(); |
|
53 testUsernameAndMessage(); |
|
54 testDontReceiveMessageInNameState(); |
|
55 } |
|
56 |
|
57 private static ChatServer startServer() throws IOException { |
|
58 ChatServer server = new ChatServer(0); |
|
59 InetSocketAddress address = (InetSocketAddress) server.getSocketAddress(); |
|
60 listeningPort = address.getPort(); |
|
61 server.run(); |
|
62 return server; |
|
63 } |
|
64 |
|
65 public static void testStartStop() throws Exception { |
|
66 ChatServer server = startServer(); |
|
67 server.shutdown(); |
|
68 } |
|
69 |
|
70 public static void testPortOpen() throws Exception { |
|
71 ChatServer server = startServer(); |
|
72 try { |
|
73 Socket socket = new Socket("localhost", listeningPort); |
|
74 if (!socket.isConnected()) { |
|
75 throw new RuntimeException("Failed to connect to server: port not open"); |
|
76 } |
|
77 } finally { |
|
78 server.shutdown(); |
|
79 } |
|
80 } |
|
81 |
|
82 public static void testAsksForName() throws Exception { |
|
83 ChatServer server = startServer(); |
|
84 try { |
|
85 Socket socket = new Socket("localhost", listeningPort); |
|
86 |
|
87 Reader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
|
88 String string = readAvailableString(reader); |
|
89 if (!string.equals("Name: ")) { |
|
90 throw new RuntimeException("Server doesn't send Name: "); |
|
91 } |
|
92 } finally { |
|
93 server.shutdown(); |
|
94 } |
|
95 } |
|
96 |
|
97 public static void testUseName() throws Throwable { |
|
98 ChatServer server = startServer(); |
|
99 try { |
|
100 performTestUseName(); |
|
101 } finally { |
|
102 server.shutdown(); |
|
103 } |
|
104 } |
|
105 |
|
106 public static void testConnectDisconnectConnect() throws Exception { |
|
107 ChatServer server = startServer(); |
|
108 try { |
|
109 performTestConnectDisconnectConnect(); |
|
110 } finally { |
|
111 server.shutdown(); |
|
112 } |
|
113 } |
|
114 |
|
115 public static void testUsernameAndMessage() throws Exception { |
|
116 ChatServer server = startServer(); |
|
117 try { |
|
118 performTestUsernameAndMessage(); |
|
119 } finally { |
|
120 server.shutdown(); |
|
121 } |
|
122 } |
|
123 |
|
124 public static void testDontReceiveMessageInNameState() throws Exception { |
|
125 ChatServer server = startServer(); |
|
126 try { |
|
127 performDontReceiveMessageInNameState(); |
|
128 } finally { |
|
129 server.shutdown(); |
|
130 } |
|
131 } |
|
132 |
|
133 private static void assertEqual(List<Exception> exception, Object value, Object expected) { |
|
134 if (expected == value) { |
|
135 return; |
|
136 } |
|
137 if (expected == null) { |
|
138 exception.add(new RuntimeException("Expected null, but was: " + value)); |
|
139 return; |
|
140 } |
|
141 if (!expected.equals(value)) { |
|
142 exception.add(new RuntimeException("Expected: " + expected + " but was: " + value)); |
|
143 return; |
|
144 } |
|
145 } |
|
146 |
|
147 private static void performDontReceiveMessageInNameState() throws Exception { |
|
148 final CyclicBarrier barrier1 = new CyclicBarrier(2); |
|
149 final CyclicBarrier barrier2 = new CyclicBarrier(2); |
|
150 final CyclicBarrier barrier3 = new CyclicBarrier(2); |
|
151 final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>()); |
|
152 |
|
153 ChatConnection chatConnection = new ChatConnection() { |
|
154 @Override |
|
155 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
156 String string = readAvailableString(reader); |
|
157 assertEqual(exceptions, string, "Name: "); |
|
158 writer.write("testClient1\n"); |
|
159 waitForJoin(reader, "testClient1"); |
|
160 barrier1.await(); |
|
161 writer.write("Ignore this!\n"); |
|
162 barrier2.await(); |
|
163 barrier3.await(); |
|
164 } |
|
165 }; |
|
166 |
|
167 Thread client2 = new Thread(new ChatConnection() { |
|
168 @Override |
|
169 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
170 barrier1.await(); |
|
171 barrier2.await(); |
|
172 String string = readAvailableString(reader); |
|
173 assertEqual(exceptions, string, "Name: "); |
|
174 string = readAvailableString(reader, true); |
|
175 assertEqual(exceptions, string, null); |
|
176 writer.write("testClient2\n"); |
|
177 barrier3.await(); |
|
178 } |
|
179 }); |
|
180 |
|
181 client2.start(); |
|
182 chatConnection.run(); |
|
183 if (!exceptions.isEmpty()) { |
|
184 throw exceptions.get(0); |
|
185 } |
|
186 |
|
187 } |
|
188 |
|
189 private static void waitForJoin(BufferedReader reader, String s) throws IOException { |
|
190 String joined; |
|
191 do { |
|
192 joined = readAvailableString(reader); |
|
193 } while (!(joined != null && joined.contains("Welcome " + s))); |
|
194 } |
|
195 |
|
196 private static void performTestUsernameAndMessage() throws Exception { |
|
197 final CyclicBarrier barrier1 = new CyclicBarrier(2); |
|
198 final CyclicBarrier barrier2 = new CyclicBarrier(2); |
|
199 final CyclicBarrier barrier3 = new CyclicBarrier(2); |
|
200 final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>()); |
|
201 |
|
202 ChatConnection chatConnection = new ChatConnection() { |
|
203 @Override |
|
204 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
205 String string = readAvailableString(reader); |
|
206 assertEqual(exceptions, string, "Name: "); |
|
207 writer.write("testClient1\n"); |
|
208 waitForJoin(reader, "testClient1"); |
|
209 barrier1.await(); |
|
210 barrier2.await(); |
|
211 string = readAvailableString(reader); |
|
212 assertEqual(exceptions, string, "testClient2: Hello world!\n"); |
|
213 barrier3.await(); |
|
214 } |
|
215 }; |
|
216 |
|
217 Thread client2 = new Thread(new ChatConnection() { |
|
218 @Override |
|
219 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
220 String string = readAvailableString(reader); |
|
221 assertEqual(exceptions, string, "Name: "); |
|
222 barrier1.await(); |
|
223 writer.write("testClient2\nHello world!\n"); |
|
224 barrier2.await(); |
|
225 barrier3.await(); |
|
226 } |
|
227 }); |
|
228 |
|
229 client2.start(); |
|
230 chatConnection.run(); |
|
231 if (!exceptions.isEmpty()) { |
|
232 throw exceptions.get(0); |
|
233 } |
|
234 } |
|
235 |
|
236 private static void performTestConnectDisconnectConnect() throws Exception { |
|
237 final CyclicBarrier barrier1 = new CyclicBarrier(2); |
|
238 final CyclicBarrier barrier2 = new CyclicBarrier(2); |
|
239 final CyclicBarrier barrier3 = new CyclicBarrier(2); |
|
240 final List<Exception> exceptions = new ArrayList<Exception>(); |
|
241 |
|
242 ChatConnection chatConnection = new ChatConnection() { |
|
243 @Override |
|
244 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
245 String string = readAvailableString(reader); |
|
246 assertEqual(exceptions, string, "Name: "); |
|
247 writer.write("testClient1\n"); |
|
248 } |
|
249 }; |
|
250 |
|
251 ChatConnection chatConnection2 = new ChatConnection() { |
|
252 @Override |
|
253 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
254 readAvailableString(reader); |
|
255 writer.write("testClient1\n"); |
|
256 waitForJoin(reader, "testClient1"); |
|
257 barrier1.await(); |
|
258 writer.write("Good morning!\n"); |
|
259 barrier2.await(); |
|
260 String string = readAvailableString(reader); |
|
261 assertEqual(exceptions, string, "testClient2: Hello world!\n"); |
|
262 barrier3.await(); |
|
263 } |
|
264 }; |
|
265 |
|
266 Thread client2 = new Thread(new ChatConnection() { |
|
267 @Override |
|
268 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
269 readAvailableString(reader); |
|
270 writer.write("testClient2\n"); |
|
271 waitForJoin(reader, "testClient2"); |
|
272 barrier1.await(); |
|
273 writer.write("Hello world!\n"); |
|
274 barrier2.await(); |
|
275 String string = readAvailableString(reader); |
|
276 assertEqual(exceptions, string, "testClient1: Good morning!\n"); |
|
277 barrier3.await(); |
|
278 } |
|
279 }); |
|
280 |
|
281 client2.start(); |
|
282 chatConnection.run(); |
|
283 chatConnection2.run(); |
|
284 if (!exceptions.isEmpty()) { |
|
285 throw exceptions.get(0); |
|
286 } |
|
287 } |
|
288 |
|
289 private static void performTestUseName() throws Exception { |
|
290 final CyclicBarrier barrier1 = new CyclicBarrier(2); |
|
291 final CyclicBarrier barrier2 = new CyclicBarrier(2); |
|
292 final CyclicBarrier barrier3 = new CyclicBarrier(2); |
|
293 final List<Exception> exceptions = new ArrayList<Exception>(); |
|
294 |
|
295 ChatConnection chatConnection = new ChatConnection() { |
|
296 @Override |
|
297 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
298 String string = readAvailableString(reader); |
|
299 if (!"Name: ".equals(string)) { |
|
300 exceptions.add(new RuntimeException("Expected Name: ")); |
|
301 } |
|
302 writer.write("testClient1\n"); |
|
303 waitForJoin(reader, "testClient1"); |
|
304 barrier1.await(); |
|
305 barrier2.await(); |
|
306 string = readAvailableString(reader); |
|
307 if (!"testClient2: Hello world!\n".equals(string)) { |
|
308 exceptions.add(new RuntimeException("testClient2: Hello world!\n")); |
|
309 } |
|
310 barrier3.await(); |
|
311 } |
|
312 }; |
|
313 |
|
314 Thread client2 = new Thread(new ChatConnection() { |
|
315 @Override |
|
316 public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception { |
|
317 String string = readAvailableString(reader); |
|
318 if (!"Name: ".equals(string)) { |
|
319 exceptions.add(new RuntimeException("Expected Name: ")); |
|
320 } |
|
321 writer.write("testClient2\n"); |
|
322 waitForJoin(reader, "testClient2"); |
|
323 barrier1.await(); |
|
324 writer.write("Hello world!\n"); |
|
325 barrier2.await(); |
|
326 barrier3.await(); |
|
327 } |
|
328 }); |
|
329 |
|
330 client2.start(); |
|
331 chatConnection.run(); |
|
332 if (!exceptions.isEmpty()) { |
|
333 throw exceptions.get(0); |
|
334 } |
|
335 } |
|
336 |
|
337 private static String readAvailableString(Reader reader) throws IOException { |
|
338 return readAvailableString(reader, false); |
|
339 } |
|
340 |
|
341 private static String readAvailableString(Reader reader, boolean now) throws IOException { |
|
342 StringBuilder builder = new StringBuilder(); |
|
343 int bytes; |
|
344 if (now && !reader.ready()) { |
|
345 return null; |
|
346 } |
|
347 do { |
|
348 char[] buf = new char[256]; |
|
349 bytes = reader.read(buf); |
|
350 builder.append(buf, 0, bytes); |
|
351 } while (bytes == 256); |
|
352 return builder.toString(); |
|
353 } |
|
354 |
|
355 private abstract static class ChatConnection implements Runnable { |
|
356 public Exception exception; |
|
357 |
|
358 @Override |
|
359 public void run() { |
|
360 try (Socket socket = new Socket("localhost", listeningPort); |
|
361 BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
|
362 Writer writer = new FlushingWriter(new OutputStreamWriter(socket.getOutputStream()))) { |
|
363 socket.setTcpNoDelay(true); |
|
364 |
|
365 run(socket, reader, writer); |
|
366 } catch (Exception e) { |
|
367 exception = e; |
|
368 } |
|
369 } |
|
370 |
|
371 public abstract void run(Socket socket, BufferedReader reader, Writer writer) throws Exception; |
|
372 } |
|
373 |
|
374 private static class FlushingWriter extends Writer { |
|
375 public final Writer delegate; |
|
376 |
|
377 private FlushingWriter(Writer delegate) { |
|
378 this.delegate = delegate; |
|
379 } |
|
380 |
|
381 @Override |
|
382 public void write(char[] cbuf, int off, int len) throws IOException { |
|
383 delegate.write(cbuf, off, len); |
|
384 } |
|
385 |
|
386 @Override |
|
387 public void flush() throws IOException { |
|
388 delegate.flush(); |
|
389 } |
|
390 |
|
391 @Override |
|
392 public void close() throws IOException { |
|
393 delegate.close(); |
|
394 } |
|
395 |
|
396 @Override |
|
397 public void write(String str) throws IOException { |
|
398 super.write(str); |
|
399 flush(); |
|
400 } |
|
401 } |
|
402 } |
|