test/jdk/java/net/SocketImpl/TestDefaultBehavior.java
author chegar
Wed, 29 May 2019 13:58:05 +0100
changeset 55081 dd321e3596c0
permissions -rw-r--r--
8224477: java.net socket types new-style socket option methods - spec and impl mismatch Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     1
/*
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     4
 *
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     8
 *
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    13
 * accompanied this code).
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    14
 *
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    18
 *
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    21
 * questions.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    22
 */
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    23
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    24
/*
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    25
 * @test
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    26
 * @bug 8224477
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    27
 * @summary Basic test for java.net.SocketImpl default behavior
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    28
 * @run testng TestDefaultBehavior
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    29
 */
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    30
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    31
import java.io.IOException;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    32
import java.io.InputStream;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    33
import java.io.OutputStream;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    34
import java.net.InetAddress;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    35
import java.net.SocketAddress;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    36
import java.net.SocketImpl;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    37
import java.net.SocketOption;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    38
import java.util.Set;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    39
import org.testng.annotations.Test;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    40
import static java.lang.Boolean.*;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    41
import static java.net.StandardSocketOptions.*;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    42
import static org.testng.Assert.assertEquals;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    43
import static org.testng.Assert.expectThrows;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    44
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    45
public class TestDefaultBehavior {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    46
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    47
    static final Class<NullPointerException> NPE = NullPointerException.class;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    48
    static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    49
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    50
    @Test
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    51
    public void socketImpl() {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    52
        CustomSocketImpl csi = new CustomSocketImpl();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    53
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    54
        assertEquals(csi.supportedOptions().size(), 0);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    55
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    56
        expectThrows(NPE, () -> csi.setOption(null, null));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    57
        expectThrows(NPE, () -> csi.setOption(null, 1));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    58
        expectThrows(UOE, () -> csi.setOption(SO_RCVBUF, 100));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    59
        expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    60
        expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, FALSE));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    61
        expectThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, TRUE));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    62
        expectThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, FALSE));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    63
        expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    64
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    65
        expectThrows(NPE, () -> csi.getOption(null));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    66
        expectThrows(UOE, () -> csi.getOption(SO_RCVBUF));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    67
        expectThrows(UOE, () -> csi.getOption(SO_KEEPALIVE));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    68
        expectThrows(UOE, () -> csi.getOption(FAKE_SOCK_OPT));
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    69
    }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    70
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    71
    static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    72
        @Override public String name() { return "FAKE_SOCK_OPT"; }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    73
        @Override public Class<Boolean> type() { return Boolean.class; }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    74
    };
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    75
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    76
    // A SocketImpl that delegates the three new-style socket option
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    77
    // methods to the default java.net.SocketImpl implementation.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    78
    static class CustomSocketImpl extends SocketImpl {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    79
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    80
        @Override
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    81
        public <T> void setOption(SocketOption<T> name, T value) throws IOException {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    82
            super.setOption(name, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    83
        }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    84
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    85
        @Override
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    86
        public Set<SocketOption<?>> supportedOptions() {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    87
            return super.supportedOptions();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    88
        }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    89
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    90
        @Override
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    91
        public <T> T getOption(SocketOption<T> name) throws IOException {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    92
            return super.getOption(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    93
        }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    94
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    95
        // --
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    96
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    97
        @Override protected void create(boolean stream) { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    98
        @Override protected void connect(String host, int port) { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
    99
        @Override protected void connect(InetAddress address, int port) { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   100
        @Override protected void connect(SocketAddress address, int timeout)  { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   101
        @Override protected void bind(InetAddress host, int port) { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   102
        @Override protected void listen(int backlog) { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   103
        @Override protected void accept(SocketImpl s)  { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   104
        @Override protected InputStream getInputStream() { return null; }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   105
        @Override protected OutputStream getOutputStream() { return null; }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   106
        @Override protected int available() { return 0; }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   107
        @Override protected void close() { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   108
        @Override protected void sendUrgentData(int data)  { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   109
        @Override public void setOption(int optID, Object value) { }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   110
        @Override public Object getOption(int optID) { return null; }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   111
    }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
diff changeset
   112
}