0N/A/*
2362N/A * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary Unit test for server-socket-channel adaptors
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.charset.*;
0N/A
0N/A
0N/Apublic class AdaptServerSocket {
0N/A
0N/A static java.io.PrintStream out = System.out;
0N/A
0N/A static volatile Exception clientException = null;
0N/A static volatile Thread client = null;
0N/A
0N/A static void startClient(final int port, final int dally)
0N/A throws Exception
0N/A {
0N/A Thread t = new Thread() {
0N/A public void run() {
0N/A try {
0N/A Socket so = new Socket();
0N/A out.println("client: " + so);
0N/A if (dally > 0)
0N/A Thread.sleep(dally);
0N/A so.connect(new InetSocketAddress(port));
0N/A if (Thread.interrupted()) {
0N/A out.println("client interrupted");
0N/A so.close();
0N/A return;
0N/A }
0N/A out.println("client: " + so);
0N/A int a = so.getInputStream().read();
0N/A out.println("client: read " + a);
0N/A a += 1;
0N/A so.getOutputStream().write(a);
0N/A out.println("client: wrote " + a);
0N/A so.close();
0N/A } catch (Exception x) {
0N/A if (x instanceof InterruptedException)
0N/A return;
0N/A clientException = x;
0N/A x.printStackTrace();
0N/A }
0N/A }
0N/A };
0N/A t.setDaemon(true);
0N/A t.start();
0N/A client = t;
0N/A }
0N/A
0N/A static void test(int clientDally, int timeout, boolean shouldTimeout)
0N/A throws Exception
0N/A {
0N/A out.println();
0N/A
0N/A ServerSocketChannel ssc = ServerSocketChannel.open();
0N/A ServerSocket sso = ssc.socket();
0N/A out.println("created: " + ssc);
0N/A out.println(" " + sso);
0N/A if (timeout != 0)
0N/A sso.setSoTimeout(timeout);
0N/A out.println("timeout: " + sso.getSoTimeout());
0N/A sso.bind(null);
0N/A out.println("bound: " + ssc);
0N/A out.println(" " + sso);
0N/A startClient(sso.getLocalPort(), clientDally);
0N/A Thread.sleep(10);
0N/A
0N/A Socket so = null;
0N/A try {
0N/A so = sso.accept();
0N/A } catch (SocketTimeoutException x) {
0N/A if (shouldTimeout)
0N/A out.println("Accept timed out, as expected");
0N/A else
0N/A throw x;
0N/A }
0N/A if (shouldTimeout && (so != null))
0N/A throw new Exception("Accept did not time out");
0N/A
0N/A if (so != null) {
0N/A int a = 42;
0N/A so.getOutputStream().write(a);
0N/A int b = so.getInputStream().read();
0N/A if (b != a + 1)
0N/A throw new Exception("Read incorrect data");
0N/A out.println("server: read " + b);
0N/A sso.close();
0N/A }
0N/A
0N/A client.interrupt();
0N/A client.join();
0N/A if (clientException != null)
0N/A throw clientException;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A test(0, 0, false);
0N/A test(50, 500, false);
0N/A test(500, 50, true);
0N/A }
0N/A
0N/A}