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");
 }
  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());
  }
  /**
   * 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);
  }