private ByteBuffer verifyDataPassing(TCPChannel svrChan) throws Exception {
    ByteBuffer b = ByteBuffer.allocate(10);
    helper.putString(b, "de");
    helper.doneFillingBuffer(b);
    int expectedWrote = b.remaining();
    log.fine("***********************************************");
    int actualWrite = client1.oldWrite(b);
    assertEquals(expectedWrote, actualWrite);

    CalledMethod m = mockServer.expect(MockNIOServer.INCOMING_DATA);
    TCPChannel actualChannel = (TCPChannel) m.getAllParams()[0];
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be correct type of channel", c, actualChannel.getClass());

    ByteBuffer actualBuf = (ByteBuffer) m.getAllParams()[1];
    String result = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals("de", result);

    b.rewind();
    svrChan.oldWrite(b);

    m = mockHandler.expect(MockDataHandler.INCOMING_DATA);
    actualBuf = (ByteBuffer) m.getAllParams()[1];
    result = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals("de", result);
    return b;
  }
  /**
   * Order between TCPChannel.connect and TCPChannel.registerForRead results in different code
   * paths...this is one of the tests.
   *
   * @throws Exception
   */
  public void testRegisterForReadsAfterConnect() throws Exception {
    // make sure we are testing the right one....
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be instance of secure channel", c, client1.getClass());

    // no bind, just do connect to test port is not zero
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");
    log.info("connected");

    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);
    InetSocketAddress localAddr = client1.getLocalAddress();
    assertTrue("Port should not be 0", localAddr.getPort() != 0);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);

    client1.registerForReads((DataListener) mockHandler);

    log.info("data passing");
    verifyDataPassing(svrChan);
    log.info("teardown");
    verifyTearDown();
    log.info("done");
  }
 public void testBindThroughConnect() throws Exception {
   client1.oldConnect(svrAddr);
   int port = client1.getLocalAddress().getPort();
   assertTrue("port is zero, this is bad", port != 0);
   mockServer.expect("connected");
   // verifyTearDown();
 }
 /**
  * the first 1.5.0 jdk threw Errors on this test case instead of BindException or SocketException.
  * Our ChannelManager converts the Error back if it is a SocketException.
  *
  * @throws Exception
  */
 public void testAlreadyBound150Jdk() throws Exception {
   client1.bind(loopBackAnyPort);
   try {
     client1.bind(loopBackAnyPort);
     fail("Should have thrown SocketException");
   } catch (NioException e) {
   }
 }
  public void testUnregisterReregisterForReads() throws Exception {
    Class c = Class.forName(getChannelImplName());

    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    client1.registerForReads((DataListener) mockHandler);

    ByteBuffer b = verifyDataPassing(svrChan);

    client1.unregisterForReads();
    b.rewind();
    svrChan.oldWrite(b);
    Thread.sleep(5000);
    mockHandler.expect(MockObject.NONE);

    client1.registerForReads((DataListener) mockHandler);
    CalledMethod m = mockHandler.expect(MockNIOServer.INCOMING_DATA);
    ByteBuffer actualBuf = (ByteBuffer) m.getAllParams()[1];
    String result = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals("de", result);

    verifyTearDown();
  }
  /**
   * This cannot pass on linux right now as warnings end up in the log from reads firing even though
   * there should be none if not connected. We can fix this later if it is needed.
   *
   * <p>Order between TCPChannel.connect and TCPChannel.registerForRead results in different code
   * paths...this is one of the tests.
   *
   * @throws Exception
   */
  public void xtestRegisterForReadsBeforeConnect() throws Exception {
    // make sure we are testing the right one....
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be instance of correct channel type", c, client1.getClass());

    client1.bind(loopBackAnyPort);
    client1.registerForReads((DataListener) mockHandler);
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");

    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);

    verifyDataPassing(svrChan);
    verifyTearDown();
  }
  /**
   * There was a bug where calling ChannelManager.shutdown before closing any sockets registered
   * with that ChannelManager cannot be closed...well, at least this test proves when we close the
   * test, the other side should receive that -1 indicating the far end closed the socket.
   *
   * @throws Exception
   */
  public void testCloseSocketAfterChannelMgrShutdown() throws Exception {
    Class c = Class.forName(getChannelImplName());

    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);
    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);

    client1.registerForReads((DataListener) mockHandler);

    verifyDataPassing(svrChan);

    // shutdown channel manager first....should all sockets be closed?  Right now
    // someone has to manually close all accepted sockets...ie. client responsibility.
    svrChan.oldClose();

    mockServer.stop();

    // notice the Channelmanager on the client side has not shut down so we should
    // see a close event....
    mockHandler.expect(MockNIOServer.FAR_END_CLOSED);
  }
  public void testConnectClose() throws Exception {
    // make sure we are testing the right one....
    //        Class c = Class.forName(getChannelImplName());
    //        assertEquals("should be instance of secure channel", c, client1.getClass());

    // no bind, just do connect to test port is not zero
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");

    mockServer.expect(MockNIOServer.CONNECTED);

    verifyTearDown();
  }
  /**
   * This fixes the bug in the jdk where SocketChannel.getLocalPort return 0 instead of the port
   * that was bound.
   *
   * @throws Exception
   */
  public void testTwoBindsOnPortZero() throws Exception {
    TCPChannel chan1 = chanMgr.createTCPChannel("chan1", getClientFactoryHolder());
    TCPChannel chan2 = chanMgr.createTCPChannel("chan2", getClientFactoryHolder());

    InetSocketAddress addr = new InetSocketAddress(loopBack, 0);
    chan1.bind(addr);
    chan2.bind(addr);

    int port1 = chan1.getLocalAddress().getPort();
    int port2 = chan2.getLocalAddress().getPort();

    assertTrue("port1 is zero, this is bad", port1 != 0);
    assertTrue("port2 is zero, this is bad", port2 != 0);
    assertTrue("port1==port2, this is bad port1=" + port1, port1 != port2);
  }
  /**
   * Test closing socket before ChannelManager shutdown works.
   *
   * @throws Exception
   */
  public void testCloseSvrSocketBeforeChannelMgrShutdown() throws Exception {
    Class c = Class.forName(getChannelImplName());
    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);

    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    client1.registerForReads((DataListener) mockHandler);

    verifyDataPassing(svrChan);

    svrChan.oldClose();

    // shutdown channel manager first
    mockServer.stop();

    mockHandler.expect(MockNIOServer.FAR_END_CLOSED);
  }
 private void verifyTearDown() throws IOException {
   log.info("local=" + client1.getLocalAddress() + " remote=" + client1.getRemoteAddress());
   log.info("CLIENT1 CLOSE");
   client1.oldClose();
   mockServer.expect(MockNIOServer.FAR_END_CLOSED);
 }