public void testConnector() throws Exception {
    SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX40, "TW", "ISLD");
    SessionSettings settings = setUpSessionSettings(sessionID);
    DefaultSessionFactory sessionFactory =
        new DefaultSessionFactory(
            new UnitTestApplication(),
            new MemoryStoreFactory(),
            new ScreenLogFactory(true, true, true));

    SessionConnector connector = new SessionConnectorUnderTest(settings, sessionFactory);

    connector.addPropertyChangeListener(new SessionConnectorListener());

    Session session = connector.createSession(sessionID);
    assertNotNull(session);

    Map<SessionID, Session> sessions = Collections.singletonMap(session.getSessionID(), session);
    connector.setSessions(sessions);

    assertEquals(1, propertyChangeEvents.size());

    assertEquals(1, connector.getManagedSessions().size());
    assertEquals(session, connector.getManagedSessions().get(0));

    assertFalse(connector.isLoggedOn());

    Field stateField = session.getClass().getDeclaredField("state");
    stateField.setAccessible(true);
    SessionState state = (SessionState) stateField.get(session);

    state.setLogonSent(true);
    state.setLogonReceived(true);
    assertTrue(connector.isLoggedOn());

    assertTrue(session.isEnabled());
    connector.logoutAllSessions(true);
    assertFalse(session.isEnabled());

    assertEquals(9999, connector.getIntSetting(Acceptor.SETTING_SOCKET_ACCEPT_PORT));

    assertNotNull(connector.getScheduledExecutorService());
    assertEquals(settings, connector.getSettings());
  }
 @Override
 public void exceptionCaught(IoSession ioSession, Throwable cause) throws Exception {
   boolean disconnectNeeded = false;
   Session quickFixSession = findQFSession(ioSession);
   Throwable realCause = cause;
   if (cause instanceof ProtocolDecoderException && cause.getCause() != null) {
     realCause = cause.getCause();
   } else {
     Throwable chain = cause;
     while (chain != null && chain.getCause() != null) {
       chain = chain.getCause();
       if (chain instanceof IOException) {
         realCause = chain;
         break;
       }
     }
   }
   String reason;
   if (realCause instanceof IOException) {
     if (quickFixSession != null && quickFixSession.isEnabled()) {
       reason = "Socket exception (" + ioSession.getRemoteAddress() + "): " + cause;
     } else {
       reason = "Socket (" + ioSession.getRemoteAddress() + "): " + cause;
     }
     disconnectNeeded = true;
   } else if (realCause instanceof CriticalProtocolCodecException) {
     reason = "Critical protocol codec error: " + cause;
     disconnectNeeded = true;
   } else if (realCause instanceof ProtocolCodecException) {
     reason = "Protocol handler exception: " + cause;
   } else {
     reason = cause.toString();
   }
   if (disconnectNeeded) {
     try {
       if (quickFixSession != null) {
         quickFixSession.disconnect(reason, true);
       } else {
         log.error(reason, cause);
         ioSession.closeNow();
       }
     } finally {
       ioSession.setAttribute("QFJ_RESET_IO_CONNECTOR", Boolean.TRUE);
     }
   } else {
     log.error(reason, cause);
   }
 }