1 /* 2 * Copyright (c) 2018, 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 * @build DummyWebSocketServer 27 * @run testng/othervm 28 * -Djdk.internal.httpclient.websocket.debug=true 29 * SendTest 30 */ 31 32 import org.testng.annotations.AfterTest; 33 import org.testng.annotations.Test; 34 35 import java.io.IOException; 36 import java.net.http.WebSocket; 37 38 import static java.net.http.HttpClient.Builder.NO_PROXY; 39 import static java.net.http.HttpClient.newBuilder; 40 import static java.net.http.WebSocket.NORMAL_CLOSURE; 41 import static org.testng.Assert.assertEquals; 42 import static org.testng.Assert.assertThrows; 43 import static org.testng.Assert.assertTrue; 44 45 public class SendTest { 46 47 private static final Class<NullPointerException> NPE = NullPointerException.class; 48 49 private DummyWebSocketServer server; 50 private WebSocket webSocket; 51 52 @AfterTest cleanup()53 public void cleanup() { 54 server.close(); 55 webSocket.abort(); 56 } 57 58 @Test sendMethodsThrowNPE()59 public void sendMethodsThrowNPE() throws IOException { 60 server = new DummyWebSocketServer(); 61 server.open(); 62 webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() 63 .buildAsync(server.getURI(), new WebSocket.Listener() { }) 64 .join(); 65 66 assertThrows(NPE, () -> webSocket.sendText(null, false)); 67 assertThrows(NPE, () -> webSocket.sendText(null, true)); 68 assertThrows(NPE, () -> webSocket.sendBinary(null, false)); 69 assertThrows(NPE, () -> webSocket.sendBinary(null, true)); 70 assertThrows(NPE, () -> webSocket.sendPing(null)); 71 assertThrows(NPE, () -> webSocket.sendPong(null)); 72 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null)); 73 74 webSocket.abort(); 75 76 assertThrows(NPE, () -> webSocket.sendText(null, false)); 77 assertThrows(NPE, () -> webSocket.sendText(null, true)); 78 assertThrows(NPE, () -> webSocket.sendBinary(null, false)); 79 assertThrows(NPE, () -> webSocket.sendBinary(null, true)); 80 assertThrows(NPE, () -> webSocket.sendPing(null)); 81 assertThrows(NPE, () -> webSocket.sendPong(null)); 82 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null)); 83 } 84 85 // TODO: request in onClose/onError 86 // TODO: throw exception in onClose/onError 87 // TODO: exception is thrown from request() 88 89 @Test sendCloseCompleted()90 public void sendCloseCompleted() throws IOException { 91 server = new DummyWebSocketServer(); 92 server.open(); 93 webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() 94 .buildAsync(server.getURI(), new WebSocket.Listener() { }) 95 .join(); 96 webSocket.sendClose(NORMAL_CLOSURE, "").join(); 97 assertTrue(webSocket.isOutputClosed()); 98 assertEquals(webSocket.getSubprotocol(), ""); 99 webSocket.request(1); // No exceptions must be thrown 100 } 101 } 102