|
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 4675045 8198753 |
|
26 * @summary Test DatagramChannel send exceptions |
|
27 * @library .. |
|
28 * @run testng SendExceptions |
|
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 SendExceptions { |
|
41 static DatagramChannel sndChannel; |
|
42 static DatagramChannel rcvChannel; |
|
43 static InetSocketAddress sender; |
|
44 static InetSocketAddress receiver; |
|
45 static ByteBuffer buf = ByteBuffer.allocate(17); |
|
46 |
|
47 @BeforeTest |
|
48 public static void setup() throws Exception { |
|
49 sndChannel = DatagramChannel.open(); |
|
50 sndChannel.bind(null); |
|
51 InetAddress address = InetAddress.getLocalHost(); |
|
52 if (address.isLoopbackAddress()) { |
|
53 address = InetAddress.getLoopbackAddress(); |
|
54 } |
|
55 sender = new InetSocketAddress(address, |
|
56 sndChannel.socket().getLocalPort()); |
|
57 |
|
58 rcvChannel = DatagramChannel.open(); |
|
59 rcvChannel.bind(null); |
|
60 receiver = new InetSocketAddress(address, |
|
61 rcvChannel.socket().getLocalPort()); |
|
62 } |
|
63 |
|
64 @Test(expectedExceptions = UnsupportedAddressTypeException.class) |
|
65 public static void unsupportedAddressTypeException() throws Exception { |
|
66 rcvChannel.connect(sender); |
|
67 sndChannel.send(buf, new SocketAddress() {}); |
|
68 } |
|
69 |
|
70 @Test(expectedExceptions = UnresolvedAddressException.class) |
|
71 public static void unresolvedAddressException() throws Exception { |
|
72 String host = TestUtil.UNRESOLVABLE_HOST; |
|
73 InetSocketAddress unresolvable = new InetSocketAddress (host, 37); |
|
74 sndChannel.send(buf, unresolvable); |
|
75 } |
|
76 |
|
77 @Test(expectedExceptions = AlreadyConnectedException.class) |
|
78 public static void alreadyConnectedException() throws Exception { |
|
79 sndChannel.connect(receiver); |
|
80 InetSocketAddress random = new InetSocketAddress(0); |
|
81 sndChannel.send(buf, random); |
|
82 } |
|
83 |
|
84 @AfterTest |
|
85 public static void cleanup() throws Exception { |
|
86 rcvChannel.close(); |
|
87 sndChannel.close(); |
|
88 } |
|
89 } |