test/jdk/java/nio/channels/DatagramChannel/ConnectExceptions.java
author bpb
Thu, 22 Mar 2018 12:29:52 -0700
changeset 49284 a51ca91c2cde
permissions -rw-r--r--
8198753: (dc) DatagramChannel throws unspecified exceptions Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     1
/*
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     4
 *
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     8
 *
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    13
 * accompanied this code).
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    14
 *
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    18
 *
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    21
 * questions.
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    22
 */
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    23
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    24
/* @test
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    25
 * @bug 8198753
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    26
 * @summary Test DatagramChannel connect exceptions
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    27
 * @library ..
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    28
 * @run testng ConnectExceptions
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    29
 */
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    30
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    31
import java.io.*;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    32
import java.net.*;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    33
import java.nio.*;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    34
import java.nio.channels.*;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    35
import org.testng.annotations.AfterTest;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    36
import org.testng.annotations.BeforeTest;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    37
import org.testng.annotations.Test;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    38
import static org.testng.Assert.*;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    39
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    40
public class ConnectExceptions {
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    41
    static DatagramChannel sndChannel;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    42
    static DatagramChannel rcvChannel;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    43
    static InetSocketAddress sender;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    44
    static InetSocketAddress receiver;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    45
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    46
    @BeforeTest
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    47
    public static void setup() throws Exception {
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    48
        sndChannel = DatagramChannel.open();
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    49
        sndChannel.bind(null);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    50
        InetAddress address = InetAddress.getLocalHost();
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    51
        if (address.isLoopbackAddress()) {
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    52
            address = InetAddress.getLoopbackAddress();
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    53
        }
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    54
        sender = new InetSocketAddress(address,
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    55
            sndChannel.socket().getLocalPort());
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    56
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    57
        rcvChannel = DatagramChannel.open();
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    58
        rcvChannel.bind(null);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    59
        receiver = new InetSocketAddress(address,
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    60
            rcvChannel.socket().getLocalPort());
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    61
    }
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    62
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    63
    @Test(expectedExceptions = UnsupportedAddressTypeException.class)
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    64
    public static void unsupportedAddressTypeException() throws Exception {
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    65
        rcvChannel.connect(sender);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    66
        sndChannel.connect(new SocketAddress() {});
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    67
    }
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    68
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    69
    @Test(expectedExceptions = UnresolvedAddressException.class)
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    70
    public static void unresolvedAddressException() throws Exception {
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    71
        String host = TestUtil.UNRESOLVABLE_HOST;
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    72
        InetSocketAddress unresolvable = new InetSocketAddress (host, 37);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    73
        sndChannel.connect(unresolvable);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    74
    }
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    75
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    76
    @Test(expectedExceptions = AlreadyConnectedException.class)
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    77
    public static void alreadyConnectedException() throws Exception {
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    78
        sndChannel.connect(receiver);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    79
        InetSocketAddress random = new InetSocketAddress(0);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    80
        sndChannel.connect(random);
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    81
    }
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    82
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    83
    @AfterTest
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    84
    public static void cleanup() throws Exception {
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    85
        rcvChannel.close();
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    86
        sndChannel.close();
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    87
    }
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
diff changeset
    88
}