1 /* |
|
2 * Copyright (c) 2017, 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 package java.net.http.internal.websocket; |
|
25 |
|
26 import java.net.http.WebSocket; |
|
27 import java.net.http.WebSocket.MessagePart; |
|
28 |
|
29 import java.nio.ByteBuffer; |
|
30 import java.util.ArrayList; |
|
31 import java.util.List; |
|
32 import java.util.Objects; |
|
33 import java.util.concurrent.CompletableFuture; |
|
34 import java.util.concurrent.CompletionStage; |
|
35 |
|
36 import static java.net.http.internal.websocket.TestSupport.fullCopy; |
|
37 |
|
38 public class MockListener implements WebSocket.Listener { |
|
39 |
|
40 private final long bufferSize; |
|
41 private long count; |
|
42 private final List<Invocation> invocations = new ArrayList<>(); |
|
43 private final CompletableFuture<?> lastCall = new CompletableFuture<>(); |
|
44 |
|
45 /* |
|
46 * Typical buffer sizes: 1, n, Long.MAX_VALUE |
|
47 */ |
|
48 public MockListener(long bufferSize) { |
|
49 if (bufferSize < 1) { |
|
50 throw new IllegalArgumentException(); |
|
51 } |
|
52 this.bufferSize = bufferSize; |
|
53 } |
|
54 |
|
55 @Override |
|
56 public void onOpen(WebSocket webSocket) { |
|
57 System.out.printf("onOpen(%s)%n", webSocket); |
|
58 invocations.add(new OnOpen(webSocket)); |
|
59 onOpen0(webSocket); |
|
60 } |
|
61 |
|
62 protected void onOpen0(WebSocket webSocket) { |
|
63 replenish(webSocket); |
|
64 } |
|
65 |
|
66 @Override |
|
67 public CompletionStage<?> onText(WebSocket webSocket, |
|
68 CharSequence message, |
|
69 MessagePart part) { |
|
70 System.out.printf("onText(%s, %s, %s)%n", webSocket, message, part); |
|
71 invocations.add(new OnText(webSocket, message.toString(), part)); |
|
72 return onText0(webSocket, message, part); |
|
73 } |
|
74 |
|
75 protected CompletionStage<?> onText0(WebSocket webSocket, |
|
76 CharSequence message, |
|
77 MessagePart part) { |
|
78 replenish(webSocket); |
|
79 return null; |
|
80 } |
|
81 |
|
82 @Override |
|
83 public CompletionStage<?> onBinary(WebSocket webSocket, |
|
84 ByteBuffer message, |
|
85 MessagePart part) { |
|
86 System.out.printf("onBinary(%s, %s, %s)%n", webSocket, message, part); |
|
87 invocations.add(new OnBinary(webSocket, fullCopy(message), part)); |
|
88 return onBinary0(webSocket, message, part); |
|
89 } |
|
90 |
|
91 protected CompletionStage<?> onBinary0(WebSocket webSocket, |
|
92 ByteBuffer message, |
|
93 MessagePart part) { |
|
94 replenish(webSocket); |
|
95 return null; |
|
96 } |
|
97 |
|
98 @Override |
|
99 public CompletionStage<?> onPing(WebSocket webSocket, ByteBuffer message) { |
|
100 System.out.printf("onPing(%s, %s)%n", webSocket, message); |
|
101 invocations.add(new OnPing(webSocket, fullCopy(message))); |
|
102 return onPing0(webSocket, message); |
|
103 } |
|
104 |
|
105 protected CompletionStage<?> onPing0(WebSocket webSocket, ByteBuffer message) { |
|
106 replenish(webSocket); |
|
107 return null; |
|
108 } |
|
109 |
|
110 @Override |
|
111 public CompletionStage<?> onPong(WebSocket webSocket, ByteBuffer message) { |
|
112 System.out.printf("onPong(%s, %s)%n", webSocket, message); |
|
113 invocations.add(new OnPong(webSocket, fullCopy(message))); |
|
114 return onPong0(webSocket, message); |
|
115 } |
|
116 |
|
117 protected CompletionStage<?> onPong0(WebSocket webSocket, ByteBuffer message) { |
|
118 replenish(webSocket); |
|
119 return null; |
|
120 } |
|
121 |
|
122 @Override |
|
123 public CompletionStage<?> onClose(WebSocket webSocket, |
|
124 int statusCode, |
|
125 String reason) { |
|
126 System.out.printf("onClose(%s, %s, %s)%n", webSocket, statusCode, reason); |
|
127 invocations.add(new OnClose(webSocket, statusCode, reason)); |
|
128 lastCall.complete(null); |
|
129 return null; |
|
130 } |
|
131 |
|
132 @Override |
|
133 public void onError(WebSocket webSocket, Throwable error) { |
|
134 System.out.printf("onError(%s, %s)%n", webSocket, error); |
|
135 invocations.add(new OnError(webSocket, error == null ? null : error.getClass())); |
|
136 lastCall.complete(null); |
|
137 } |
|
138 |
|
139 public CompletableFuture<?> onCloseOrOnErrorCalled() { |
|
140 return lastCall.copy(); |
|
141 } |
|
142 |
|
143 protected void replenish(WebSocket webSocket) { |
|
144 if (--count <= 0) { |
|
145 count = bufferSize - bufferSize / 2; |
|
146 } |
|
147 webSocket.request(count); |
|
148 } |
|
149 |
|
150 public List<Invocation> invocations() { |
|
151 return new ArrayList<>(invocations); |
|
152 } |
|
153 |
|
154 public abstract static class Invocation { |
|
155 |
|
156 public static OnOpen onOpen(WebSocket webSocket) { |
|
157 return new OnOpen(webSocket); |
|
158 } |
|
159 |
|
160 public static OnText onText(WebSocket webSocket, |
|
161 String text, |
|
162 MessagePart part) { |
|
163 return new OnText(webSocket, text, part); |
|
164 } |
|
165 |
|
166 public static OnBinary onBinary(WebSocket webSocket, |
|
167 ByteBuffer data, |
|
168 MessagePart part) { |
|
169 return new OnBinary(webSocket, data, part); |
|
170 } |
|
171 |
|
172 public static OnPing onPing(WebSocket webSocket, |
|
173 ByteBuffer data) { |
|
174 return new OnPing(webSocket, data); |
|
175 } |
|
176 |
|
177 public static OnPong onPong(WebSocket webSocket, |
|
178 ByteBuffer data) { |
|
179 return new OnPong(webSocket, data); |
|
180 } |
|
181 |
|
182 public static OnClose onClose(WebSocket webSocket, |
|
183 int statusCode, |
|
184 String reason) { |
|
185 return new OnClose(webSocket, statusCode, reason); |
|
186 } |
|
187 |
|
188 public static OnError onError(WebSocket webSocket, |
|
189 Class<? extends Throwable> clazz) { |
|
190 return new OnError(webSocket, clazz); |
|
191 } |
|
192 |
|
193 final WebSocket webSocket; |
|
194 |
|
195 private Invocation(WebSocket webSocket) { |
|
196 this.webSocket = webSocket; |
|
197 } |
|
198 } |
|
199 |
|
200 public static final class OnOpen extends Invocation { |
|
201 |
|
202 public OnOpen(WebSocket webSocket) { |
|
203 super(webSocket); |
|
204 } |
|
205 |
|
206 @Override |
|
207 public boolean equals(Object o) { |
|
208 if (this == o) return true; |
|
209 if (o == null || getClass() != o.getClass()) return false; |
|
210 Invocation that = (Invocation) o; |
|
211 return Objects.equals(webSocket, that.webSocket); |
|
212 } |
|
213 |
|
214 @Override |
|
215 public int hashCode() { |
|
216 return Objects.hashCode(webSocket); |
|
217 } |
|
218 } |
|
219 |
|
220 public static final class OnText extends Invocation { |
|
221 |
|
222 final String text; |
|
223 final MessagePart part; |
|
224 |
|
225 public OnText(WebSocket webSocket, String text, MessagePart part) { |
|
226 super(webSocket); |
|
227 this.text = text; |
|
228 this.part = part; |
|
229 } |
|
230 |
|
231 @Override |
|
232 public boolean equals(Object o) { |
|
233 if (this == o) return true; |
|
234 if (o == null || getClass() != o.getClass()) return false; |
|
235 OnText onText = (OnText) o; |
|
236 return Objects.equals(text, onText.text) && |
|
237 part == onText.part && |
|
238 Objects.equals(webSocket, onText.webSocket); |
|
239 } |
|
240 |
|
241 @Override |
|
242 public int hashCode() { |
|
243 return Objects.hash(text, part, webSocket); |
|
244 } |
|
245 |
|
246 @Override |
|
247 public String toString() { |
|
248 return String.format("onText(%s, %s, %s)", webSocket, text, part); |
|
249 } |
|
250 } |
|
251 |
|
252 public static final class OnBinary extends Invocation { |
|
253 |
|
254 final ByteBuffer data; |
|
255 final MessagePart part; |
|
256 |
|
257 public OnBinary(WebSocket webSocket, ByteBuffer data, MessagePart part) { |
|
258 super(webSocket); |
|
259 this.data = data; |
|
260 this.part = part; |
|
261 } |
|
262 |
|
263 @Override |
|
264 public boolean equals(Object o) { |
|
265 if (this == o) return true; |
|
266 if (o == null || getClass() != o.getClass()) return false; |
|
267 OnBinary onBinary = (OnBinary) o; |
|
268 return Objects.equals(data, onBinary.data) && |
|
269 part == onBinary.part && |
|
270 Objects.equals(webSocket, onBinary.webSocket); |
|
271 } |
|
272 |
|
273 @Override |
|
274 public int hashCode() { |
|
275 return Objects.hash(data, part, webSocket); |
|
276 } |
|
277 |
|
278 @Override |
|
279 public String toString() { |
|
280 return String.format("onBinary(%s, %s, %s)", webSocket, data, part); |
|
281 } |
|
282 } |
|
283 |
|
284 public static final class OnPing extends Invocation { |
|
285 |
|
286 final ByteBuffer data; |
|
287 |
|
288 public OnPing(WebSocket webSocket, ByteBuffer data) { |
|
289 super(webSocket); |
|
290 this.data = data; |
|
291 } |
|
292 |
|
293 @Override |
|
294 public boolean equals(Object o) { |
|
295 if (this == o) return true; |
|
296 if (o == null || getClass() != o.getClass()) return false; |
|
297 OnPing onPing = (OnPing) o; |
|
298 return Objects.equals(data, onPing.data) && |
|
299 Objects.equals(webSocket, onPing.webSocket); |
|
300 } |
|
301 |
|
302 @Override |
|
303 public int hashCode() { |
|
304 return Objects.hash(data, webSocket); |
|
305 } |
|
306 |
|
307 @Override |
|
308 public String toString() { |
|
309 return String.format("onPing(%s, %s)", webSocket, data); |
|
310 } |
|
311 } |
|
312 |
|
313 public static final class OnPong extends Invocation { |
|
314 |
|
315 final ByteBuffer data; |
|
316 |
|
317 public OnPong(WebSocket webSocket, ByteBuffer data) { |
|
318 super(webSocket); |
|
319 this.data = data; |
|
320 } |
|
321 |
|
322 @Override |
|
323 public boolean equals(Object o) { |
|
324 if (this == o) return true; |
|
325 if (o == null || getClass() != o.getClass()) return false; |
|
326 OnPong onPong = (OnPong) o; |
|
327 return Objects.equals(data, onPong.data) && |
|
328 Objects.equals(webSocket, onPong.webSocket); |
|
329 } |
|
330 |
|
331 @Override |
|
332 public int hashCode() { |
|
333 return Objects.hash(data, webSocket); |
|
334 } |
|
335 |
|
336 @Override |
|
337 public String toString() { |
|
338 return String.format("onPong(%s, %s)", webSocket, data); |
|
339 } |
|
340 } |
|
341 |
|
342 public static final class OnClose extends Invocation { |
|
343 |
|
344 final int statusCode; |
|
345 final String reason; |
|
346 |
|
347 public OnClose(WebSocket webSocket, int statusCode, String reason) { |
|
348 super(webSocket); |
|
349 this.statusCode = statusCode; |
|
350 this.reason = reason; |
|
351 } |
|
352 |
|
353 @Override |
|
354 public boolean equals(Object o) { |
|
355 if (this == o) return true; |
|
356 if (o == null || getClass() != o.getClass()) return false; |
|
357 OnClose onClose = (OnClose) o; |
|
358 return statusCode == onClose.statusCode && |
|
359 Objects.equals(reason, onClose.reason) && |
|
360 Objects.equals(webSocket, onClose.webSocket); |
|
361 } |
|
362 |
|
363 @Override |
|
364 public int hashCode() { |
|
365 return Objects.hash(statusCode, reason, webSocket); |
|
366 } |
|
367 |
|
368 @Override |
|
369 public String toString() { |
|
370 return String.format("onClose(%s, %s, %s)", webSocket, statusCode, reason); |
|
371 } |
|
372 } |
|
373 |
|
374 public static final class OnError extends Invocation { |
|
375 |
|
376 final Class<? extends Throwable> clazz; |
|
377 |
|
378 public OnError(WebSocket webSocket, Class<? extends Throwable> clazz) { |
|
379 super(webSocket); |
|
380 this.clazz = clazz; |
|
381 } |
|
382 |
|
383 @Override |
|
384 public boolean equals(Object o) { |
|
385 if (this == o) return true; |
|
386 if (o == null || getClass() != o.getClass()) return false; |
|
387 OnError onError = (OnError) o; |
|
388 return Objects.equals(clazz, onError.clazz) && |
|
389 Objects.equals(webSocket, onError.webSocket); |
|
390 } |
|
391 |
|
392 @Override |
|
393 public int hashCode() { |
|
394 return Objects.hash(clazz, webSocket); |
|
395 } |
|
396 |
|
397 @Override |
|
398 public String toString() { |
|
399 return String.format("onError(%s, %s)", webSocket, clazz); |
|
400 } |
|
401 } |
|
402 } |
|