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;
  }
 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();
 }
 protected void tearDown() throws Exception {
   log.info("CHAN MGR STOP");
   chanMgr.stop();
   chanMgr = null;
   log.info("MOCK SERVER STOP");
   mockServer.stop();
   log.info("check for warns");
   HandlerForTests.checkForWarnings();
   log.info("done");
 }
  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();
  }
  /**
   * 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);
  }
  /**
   * 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);
  }
  protected void setUp() throws Exception {
    HandlerForTests.setupLogging();
    // here I keep using the same channel manager on purpose, just
    // so we get testing between tests that the channel manager shutdown
    // and started back up cleanly.....
    if (chanMgr == null) {
      chanMgr = getClientChanMgr();
    }
    if (mockServer == null) {
      ChannelService svcChanMgr = getServerChanMgr();
      mockServer = new MockNIOServer(svcChanMgr, getServerFactoryHolder());
    }
    chanMgr.start();
    svrAddr = mockServer.start();
    log.fine("server port =" + svrAddr);

    loopBack = InetAddress.getByName("127.0.0.1");
    loopBackAnyPort = new InetSocketAddress(loopBack, 0);

    mockHandler = MockObjectFactory.createMock(DataListener.class);
    mockHandler.setDefaultBehavior("incomingData", new CloneByteBuffer());
    mockConnect = MockObjectFactory.createMock(ConnectionCallback.class);
    client1 = chanMgr.createTCPChannel("ClientChannel", getClientFactoryHolder());
  }
 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);
 }