test/jdk/java/nio/channels/etc/PrintSupportedOptions.java
author alanb
Wed, 05 Sep 2018 11:52:08 +0100
changeset 51642 d4099c45f148
child 54770 62b6e7587b1f
permissions -rw-r--r--
8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51642
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     1
/*
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     4
 *
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     8
 *
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    13
 * accompanied this code).
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    14
 *
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    18
 *
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    21
 * questions.
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    22
 */
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    23
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    24
/**
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    25
 * @test
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    26
 * @requires (os.family == "linux" | os.family == "mac" | os.family == "windows")
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    27
 * @bug 8209152
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    28
 * @run main PrintSupportedOptions
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    29
 * @run main/othervm -Djava.net.preferIPv4Stack=true PrintSupportedOptions
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    30
 */
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    31
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    32
import java.io.IOException;
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    33
import java.net.SocketOption;
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    34
import java.nio.channels.*;
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    35
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    36
public class PrintSupportedOptions {
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    37
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    38
    @FunctionalInterface
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    39
    interface NetworkChannelSupplier<T extends NetworkChannel> {
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    40
        T get() throws IOException;
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    41
    }
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    42
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    43
    public static void main(String[] args) throws IOException {
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    44
        test(() -> SocketChannel.open());
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    45
        test(() -> ServerSocketChannel.open());
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    46
        test(() -> DatagramChannel.open());
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    47
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    48
        test(() -> AsynchronousSocketChannel.open());
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    49
        test(() -> AsynchronousServerSocketChannel.open());
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    50
    }
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    51
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    52
    @SuppressWarnings("unchecked")
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    53
    static <T extends NetworkChannel>
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    54
    void test(NetworkChannelSupplier<T> supplier) throws IOException {
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    55
        try (T ch = supplier.get()) {
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    56
            System.out.println(ch);
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    57
            for (SocketOption<?> opt : ch.supportedOptions()) {
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    58
                Object value = ch.getOption(opt);
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    59
                System.out.format(" %s -> %s%n", opt.name(), value);
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    60
                if (value != null) {
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    61
                    ch.setOption((SocketOption<Object>) opt, value);
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    62
                }
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    63
            }
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    64
        }
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    65
    }
d4099c45f148 8209152: (so) ServerSocketChannel::supportedOptions includes IP_TOS
alanb
parents:
diff changeset
    66
}