// Tags: JDK1.4 // Copyright (C) 2006 Jeroen Frijters // This file is part of Mauve. // Mauve is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2, or (at your option) // any later version. // Mauve is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Mauve; see the file COPYING. If not, write to // the Free Software Foundation, 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. */ package gnu.testlet.java.nio.channels.SocketChannel; import gnu.testlet.Testlet; import gnu.testlet.TestHarness; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; public class tests implements Testlet { public void test(TestHarness harness) { try { harness.checkPoint("test states"); testStates(harness); } catch (Exception x) { harness.check(false); harness.debug(x); } } private void testStates(TestHarness harness) throws Exception { ServerSocket srv = new ServerSocket(0, 1); Socket consumeSocket = new Socket(InetAddress.getLocalHost(), srv.getLocalPort()); SocketChannel ch = SocketChannel.open(); harness.checkPoint("initial state"); harness.check(ch.isOpen()); harness.check(!ch.isConnected()); harness.check(!ch.isConnectionPending()); harness.checkPoint("initiate async connect"); ch.configureBlocking(false); harness.check(!ch.connect(new InetSocketAddress(InetAddress.getLocalHost(), srv.getLocalPort()))); harness.check(!ch.isConnected()); harness.check(ch.isConnectionPending()); harness.check(!ch.finishConnect()); harness.checkPoint("establish the connection"); // free up the backlog srv.accept().close(); ch.configureBlocking(true); harness.check(ch.finishConnect()); harness.check(ch.isOpen()); harness.check(ch.isConnected()); harness.check(!ch.isConnectionPending()); harness.checkPoint("receive some data"); ch.configureBlocking(false); ByteBuffer buf = ByteBuffer.allocate(4); harness.check(ch.read(buf) == 0); Socket otherSide = srv.accept(); otherSide.getOutputStream().write("TEST".getBytes(), 0, 4); ch.configureBlocking(true); harness.check(ch.read(buf) == 4); harness.check(new String(buf.array()).equals("TEST")); harness.checkPoint("close"); ch.close(); harness.check(!ch.isOpen()); harness.check(!ch.isConnected()); harness.check(!ch.isConnectionPending()); // clean up consumeSocket.close(); srv.close(); } }