test/jdk/java/net/SocketImpl/CompareSocketOptions.java
changeset 55102 59567035d279
equal deleted inserted replaced
55101:c41783eb76eb 55102:59567035d279
       
     1 /*
       
     2  * Copyright (c) 2019, 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  * @bug 8221481
       
    27  * @modules java.base/java.net:+open java.base/sun.nio.ch:+open
       
    28  * @run testng CompareSocketOptions
       
    29  * @summary Compare the set of socket options supported by the old and new SocketImpls
       
    30  */
       
    31 
       
    32 import java.io.IOException;
       
    33 import java.net.ServerSocket;
       
    34 import java.net.Socket;
       
    35 import java.net.SocketImpl;
       
    36 
       
    37 import org.testng.annotations.Test;
       
    38 import static org.testng.Assert.*;
       
    39 
       
    40 @Test
       
    41 public class CompareSocketOptions {
       
    42 
       
    43     /**
       
    44      * Test that the old and new platform SocketImpl support the same set of
       
    45      * client socket options.
       
    46      */
       
    47     public void testClientSocketSupportedOptions() throws IOException {
       
    48         Socket s1 = new Socket(createOldSocketImpl(false)) { };
       
    49         Socket s2 = new Socket(createNewSocketImpl(false)) { };
       
    50         assertEquals(s1.supportedOptions(), s2.supportedOptions());
       
    51     }
       
    52 
       
    53     /**
       
    54      * Test that the old and new platform SocketImpl support the same set of
       
    55      * server socket options.
       
    56      */
       
    57     public void testServerSocketSupportedOptions() throws IOException {
       
    58         ServerSocket ss1 = new ServerSocket(createOldSocketImpl(true)) { };
       
    59         ServerSocket ss2 = new ServerSocket(createNewSocketImpl(true)) { };
       
    60         assertEquals(ss1.supportedOptions(), ss2.supportedOptions());
       
    61     }
       
    62 
       
    63     private static SocketImpl createOldSocketImpl(boolean server) {
       
    64         return newPlatformSocketImpl("java.net.PlainSocketImpl", server);
       
    65     }
       
    66 
       
    67     private static SocketImpl createNewSocketImpl(boolean server) {
       
    68         return newPlatformSocketImpl("sun.nio.ch.NioSocketImpl", server);
       
    69     }
       
    70 
       
    71     private static SocketImpl newPlatformSocketImpl(String name, boolean server) {
       
    72         try {
       
    73             var ctor = Class.forName(name).getDeclaredConstructor(boolean.class);
       
    74             ctor.setAccessible(true);
       
    75             return (SocketImpl) ctor.newInstance(server);
       
    76         } catch (Exception e) {
       
    77             fail("Should not get here", e);
       
    78             return null;
       
    79         }
       
    80     }
       
    81 }