1 /*
2 * Copyright (c) 2007, 2019, 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 4640544 8044773
26 * @summary Unit test to check SocketChannel setOption/getOption/options
27 * methods.
28 * @modules java.base/sun.net.ext
29 * jdk.net
30 * @requires !vm.graal.enabled
31 * @run main SocketOptionTests
32 * @run main/othervm --limit-modules=java.base SocketOptionTests
33 */
34
35 import java.io.IOException;
36 import java.net.InetSocketAddress;
37 import java.net.SocketOption;
38 import java.nio.channels.ClosedChannelException;
39 import java.nio.channels.SocketChannel;
40 import java.util.Set;
41 import sun.net.ext.ExtendedSocketOptions;
42 import static java.net.StandardSocketOptions.*;
43 import static jdk.net.ExtendedSocketOptions.*;
44
45 public class SocketOptionTests {
46
47 static void checkOption(SocketChannel sc, SocketOption name, Object expectedValue)
48 throws IOException
49 {
50 Object value = sc.getOption(name);
145 // NullPointerException
146 try {
147 sc.setOption(null, "value");
148 throw new RuntimeException("NullPointerException not thrown");
149 } catch (NullPointerException x) {
150 }
151 try {
152 sc.getOption(null);
153 throw new RuntimeException("NullPointerException not thrown");
154 } catch (NullPointerException x) {
155 }
156
157 // ClosedChannelException
158 sc.close();
159 try {
160 sc.setOption(TCP_NODELAY, true);
161 throw new RuntimeException("ClosedChannelException not thrown");
162 } catch (ClosedChannelException x) {
163 }
164 }
165 }
|
1 /*
2 * Copyright (c) 2007, 2026, 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 4640544 8044773
26 * @summary Unit test to check SocketChannel setOption/getOption/options
27 * methods.
28 * @modules java.base/sun.net.ext
29 * jdk.net
30 * @run main SocketOptionTests
31 * @run main/othervm --limit-modules=java.base SocketOptionTests
32 */
33
34 import java.io.IOException;
35 import java.net.InetSocketAddress;
36 import java.net.SocketOption;
37 import java.nio.channels.ClosedChannelException;
38 import java.nio.channels.SocketChannel;
39 import java.util.Set;
40 import sun.net.ext.ExtendedSocketOptions;
41 import static java.net.StandardSocketOptions.*;
42 import static jdk.net.ExtendedSocketOptions.*;
43
44 public class SocketOptionTests {
45
46 static void checkOption(SocketChannel sc, SocketOption name, Object expectedValue)
47 throws IOException
48 {
49 Object value = sc.getOption(name);
144 // NullPointerException
145 try {
146 sc.setOption(null, "value");
147 throw new RuntimeException("NullPointerException not thrown");
148 } catch (NullPointerException x) {
149 }
150 try {
151 sc.getOption(null);
152 throw new RuntimeException("NullPointerException not thrown");
153 } catch (NullPointerException x) {
154 }
155
156 // ClosedChannelException
157 sc.close();
158 try {
159 sc.setOption(TCP_NODELAY, true);
160 throw new RuntimeException("ClosedChannelException not thrown");
161 } catch (ClosedChannelException x) {
162 }
163 }
164 }
|