test/jdk/java/net/httpclient/websocket/SendTest.java
changeset 58289 3a79d4cccbcb
parent 49944 4690a2871b44
equal deleted inserted replaced
58288:48e480e56aad 58289:3a79d4cccbcb
     1 /*
     1 /*
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    27  * @run testng/othervm
    27  * @run testng/othervm
    28  *      -Djdk.internal.httpclient.websocket.debug=true
    28  *      -Djdk.internal.httpclient.websocket.debug=true
    29  *       SendTest
    29  *       SendTest
    30  */
    30  */
    31 
    31 
    32 import org.testng.annotations.AfterTest;
       
    33 import org.testng.annotations.Test;
    32 import org.testng.annotations.Test;
    34 
    33 
    35 import java.io.IOException;
    34 import java.io.IOException;
    36 import java.net.http.WebSocket;
    35 import java.net.http.WebSocket;
    37 
    36 
    44 
    43 
    45 public class SendTest {
    44 public class SendTest {
    46 
    45 
    47     private static final Class<NullPointerException> NPE = NullPointerException.class;
    46     private static final Class<NullPointerException> NPE = NullPointerException.class;
    48 
    47 
    49     private DummyWebSocketServer server;
       
    50     private WebSocket webSocket;
       
    51 
       
    52     @AfterTest
       
    53     public void cleanup() {
       
    54         server.close();
       
    55         webSocket.abort();
       
    56     }
       
    57 
       
    58     @Test
    48     @Test
    59     public void sendMethodsThrowNPE() throws IOException {
    49     public void sendMethodsThrowNPE() throws IOException {
    60         server = new DummyWebSocketServer();
    50         try (var server = new DummyWebSocketServer()) {
    61         server.open();
    51             server.open();
    62         webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()
    52             var webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()
    63                 .buildAsync(server.getURI(), new WebSocket.Listener() { })
    53                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
    64                 .join();
    54                     .join();
       
    55             try {
       
    56                 assertThrows(NPE, () -> webSocket.sendText(null, false));
       
    57                 assertThrows(NPE, () -> webSocket.sendText(null, true));
       
    58                 assertThrows(NPE, () -> webSocket.sendBinary(null, false));
       
    59                 assertThrows(NPE, () -> webSocket.sendBinary(null, true));
       
    60                 assertThrows(NPE, () -> webSocket.sendPing(null));
       
    61                 assertThrows(NPE, () -> webSocket.sendPong(null));
       
    62                 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));
    65 
    63 
    66         assertThrows(NPE, () -> webSocket.sendText(null, false));
    64                 webSocket.abort();
    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 
    65 
    74         webSocket.abort();
    66                 assertThrows(NPE, () -> webSocket.sendText(null, false));
    75 
    67                 assertThrows(NPE, () -> webSocket.sendText(null, true));
    76         assertThrows(NPE, () -> webSocket.sendText(null, false));
    68                 assertThrows(NPE, () -> webSocket.sendBinary(null, false));
    77         assertThrows(NPE, () -> webSocket.sendText(null, true));
    69                 assertThrows(NPE, () -> webSocket.sendBinary(null, true));
    78         assertThrows(NPE, () -> webSocket.sendBinary(null, false));
    70                 assertThrows(NPE, () -> webSocket.sendPing(null));
    79         assertThrows(NPE, () -> webSocket.sendBinary(null, true));
    71                 assertThrows(NPE, () -> webSocket.sendPong(null));
    80         assertThrows(NPE, () -> webSocket.sendPing(null));
    72                 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));
    81         assertThrows(NPE, () -> webSocket.sendPong(null));
    73             } finally {
    82         assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));
    74                 webSocket.abort();
       
    75             }
       
    76         }
    83     }
    77     }
    84 
    78 
    85     // TODO: request in onClose/onError
    79     // TODO: request in onClose/onError
    86     // TODO: throw exception in onClose/onError
    80     // TODO: throw exception in onClose/onError
    87     // TODO: exception is thrown from request()
    81     // TODO: exception is thrown from request()
    88 
    82 
    89     @Test
    83     @Test
    90     public void sendCloseCompleted() throws IOException {
    84     public void sendCloseCompleted() throws IOException {
    91         server = new DummyWebSocketServer();
    85         try (var server = new DummyWebSocketServer()) {
    92         server.open();
    86             server.open();
    93         webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()
    87             var webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()
    94                 .buildAsync(server.getURI(), new WebSocket.Listener() { })
    88                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
    95                 .join();
    89                     .join();
    96         webSocket.sendClose(NORMAL_CLOSURE, "").join();
    90             try {
    97         assertTrue(webSocket.isOutputClosed());
    91                 webSocket.sendClose(NORMAL_CLOSURE, "").join();
    98         assertEquals(webSocket.getSubprotocol(), "");
    92                 assertTrue(webSocket.isOutputClosed());
    99         webSocket.request(1); // No exceptions must be thrown
    93                 assertEquals(webSocket.getSubprotocol(), "");
       
    94                 webSocket.request(1); // No exceptions must be thrown
       
    95             } finally {
       
    96                 webSocket.abort();
       
    97             }
       
    98         }
   100     }
    99     }
   101 }
   100 }
       
   101