test/jdk/java/net/SocketImpl/CompareSocketOptions.java
author phedlin
Fri, 15 Nov 2019 16:39:08 +0100
changeset 59291 54cf02d52c1b
parent 55102 59567035d279
permissions -rw-r--r--
8220376: C2: Int >0 not recognized as !=0 for div by 0 check Reviewed-by: neliasso, vlivanov, mdoerr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     1
/*
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     4
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     8
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    13
 * accompanied this code).
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    14
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    18
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    21
 * questions.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    22
 */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    23
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    24
/*
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    25
 * @test
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    26
 * @bug 8221481
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    27
 * @modules java.base/java.net:+open java.base/sun.nio.ch:+open
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    28
 * @run testng CompareSocketOptions
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    29
 * @summary Compare the set of socket options supported by the old and new SocketImpls
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    30
 */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    31
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    32
import java.io.IOException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    33
import java.net.ServerSocket;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    34
import java.net.Socket;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    35
import java.net.SocketImpl;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    36
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    37
import org.testng.annotations.Test;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    38
import static org.testng.Assert.*;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    39
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    40
@Test
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    41
public class CompareSocketOptions {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    42
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    43
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    44
     * Test that the old and new platform SocketImpl support the same set of
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    45
     * client socket options.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    46
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    47
    public void testClientSocketSupportedOptions() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    48
        Socket s1 = new Socket(createOldSocketImpl(false)) { };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    49
        Socket s2 = new Socket(createNewSocketImpl(false)) { };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    50
        assertEquals(s1.supportedOptions(), s2.supportedOptions());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    51
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    52
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    53
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    54
     * Test that the old and new platform SocketImpl support the same set of
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    55
     * server socket options.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    56
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    57
    public void testServerSocketSupportedOptions() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    58
        ServerSocket ss1 = new ServerSocket(createOldSocketImpl(true)) { };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    59
        ServerSocket ss2 = new ServerSocket(createNewSocketImpl(true)) { };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    60
        assertEquals(ss1.supportedOptions(), ss2.supportedOptions());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    61
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    62
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    63
    private static SocketImpl createOldSocketImpl(boolean server) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    64
        return newPlatformSocketImpl("java.net.PlainSocketImpl", server);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    65
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    66
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    67
    private static SocketImpl createNewSocketImpl(boolean server) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    68
        return newPlatformSocketImpl("sun.nio.ch.NioSocketImpl", server);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    69
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    70
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    71
    private static SocketImpl newPlatformSocketImpl(String name, boolean server) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    72
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    73
            var ctor = Class.forName(name).getDeclaredConstructor(boolean.class);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    74
            ctor.setAccessible(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    75
            return (SocketImpl) ctor.newInstance(server);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    76
        } catch (Exception e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    77
            fail("Should not get here", e);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    78
            return null;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    79
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    80
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    81
}