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