Path: blob/master/test/jdk/java/net/SocketImpl/CompareSocketOptions.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 822148126* @modules java.base/java.net:+open java.base/sun.nio.ch:+open27* @run testng CompareSocketOptions28* @summary Compare the set of socket options supported by the old and new SocketImpls29*/3031import java.io.IOException;32import java.net.ServerSocket;33import java.net.Socket;34import java.net.SocketImpl;3536import org.testng.annotations.Test;37import static org.testng.Assert.*;3839@Test40public class CompareSocketOptions {4142/**43* Test that the old and new platform SocketImpl support the same set of44* client socket options.45*/46public void testClientSocketSupportedOptions() throws IOException {47Socket s1 = new Socket(createOldSocketImpl(false)) { };48Socket s2 = new Socket(createNewSocketImpl(false)) { };49assertEquals(s1.supportedOptions(), s2.supportedOptions());50}5152/**53* Test that the old and new platform SocketImpl support the same set of54* server socket options.55*/56public void testServerSocketSupportedOptions() throws IOException {57ServerSocket ss1 = new ServerSocket(createOldSocketImpl(true)) { };58ServerSocket ss2 = new ServerSocket(createNewSocketImpl(true)) { };59assertEquals(ss1.supportedOptions(), ss2.supportedOptions());60}6162private static SocketImpl createOldSocketImpl(boolean server) {63return newPlatformSocketImpl("java.net.PlainSocketImpl", server);64}6566private static SocketImpl createNewSocketImpl(boolean server) {67return newPlatformSocketImpl("sun.nio.ch.NioSocketImpl", server);68}6970private static SocketImpl newPlatformSocketImpl(String name, boolean server) {71try {72var ctor = Class.forName(name).getDeclaredConstructor(boolean.class);73ctor.setAccessible(true);74return (SocketImpl) ctor.newInstance(server);75} catch (Exception e) {76fail("Should not get here", e);77return null;78}79}80}8182