Path: blob/master/test/jdk/java/net/SocketImpl/TestDefaultBehavior.java
41152 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 822447726* @summary Basic test for java.net.SocketImpl default behavior27* @run testng TestDefaultBehavior28*/2930import java.io.IOException;31import java.io.InputStream;32import java.io.OutputStream;33import java.net.InetAddress;34import java.net.SocketAddress;35import java.net.SocketImpl;36import java.net.SocketOption;37import java.util.Set;38import org.testng.annotations.Test;39import static java.lang.Boolean.*;40import static java.net.StandardSocketOptions.*;41import static org.testng.Assert.assertEquals;42import static org.testng.Assert.expectThrows;4344public class TestDefaultBehavior {4546static final Class<NullPointerException> NPE = NullPointerException.class;47static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;4849@Test50public void socketImpl() {51CustomSocketImpl csi = new CustomSocketImpl();5253assertEquals(csi.supportedOptions().size(), 0);5455expectThrows(NPE, () -> csi.setOption(null, null));56expectThrows(NPE, () -> csi.setOption(null, 1));57expectThrows(UOE, () -> csi.setOption(SO_RCVBUF, 100));58expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));59expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, FALSE));60expectThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, TRUE));61expectThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, FALSE));62expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));6364expectThrows(NPE, () -> csi.getOption(null));65expectThrows(UOE, () -> csi.getOption(SO_RCVBUF));66expectThrows(UOE, () -> csi.getOption(SO_KEEPALIVE));67expectThrows(UOE, () -> csi.getOption(FAKE_SOCK_OPT));68}6970static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() {71@Override public String name() { return "FAKE_SOCK_OPT"; }72@Override public Class<Boolean> type() { return Boolean.class; }73};7475// A SocketImpl that delegates the three new-style socket option76// methods to the default java.net.SocketImpl implementation.77static class CustomSocketImpl extends SocketImpl {7879@Override80public <T> void setOption(SocketOption<T> name, T value) throws IOException {81super.setOption(name, value);82}8384@Override85public Set<SocketOption<?>> supportedOptions() {86return super.supportedOptions();87}8889@Override90public <T> T getOption(SocketOption<T> name) throws IOException {91return super.getOption(name);92}9394// --9596@Override protected void create(boolean stream) { }97@Override protected void connect(String host, int port) { }98@Override protected void connect(InetAddress address, int port) { }99@Override protected void connect(SocketAddress address, int timeout) { }100@Override protected void bind(InetAddress host, int port) { }101@Override protected void listen(int backlog) { }102@Override protected void accept(SocketImpl s) { }103@Override protected InputStream getInputStream() { return null; }104@Override protected OutputStream getOutputStream() { return null; }105@Override protected int available() { return 0; }106@Override protected void close() { }107@Override protected void sendUrgentData(int data) { }108@Override public void setOption(int optID, Object value) { }109@Override public Object getOption(int optID) { return null; }110}111}112113114