/**
  * If the client has bound the connection but the factory is not running in auto-bind mode, the
  * connection must be invalidated.
  */
 public void testValidateObjectLowersActiveFlagIfConnectionHasBeenBoundButAutoBindIsNotSet() {
   mockConn.expects(once()).method("isBindAttempted").will(returnValue(true));
   // we assume autoBind defaults to false (we'd rather not go through the
   // whole process of mocking up the connection manager again)
   mockConn.expects(once()).method("setActive").with(eq(false));
   assertFalse(factory.validateObject(conn));
 }
  /**
   * Verifies that {@link PooledLDAPConnectionFactory#validateObject(Object)} does not adjust a
   * {@link PooledLDAPConnection}'s active flag if the connection appears to be "alive". Probably
   * overkill, but we've had problems that we think may be related to stale connections being
   * returned to the pool, so we want to be sure the validate operation is doing exactly what we
   * think it should be doing. Also verifies that a {@link PooledLDAPConnection}'s search method
   * returns a LDAPEntry.
   *
   * @throws LDAPException
   */
  public void testValidateObjectKeepsActiveFlagUpIfConnectionIsAlive() throws LDAPException {

    // will fail if the factory attempts to monkey with the active flag
    factory.setConnectionLivenessValidator(livenessValidator);
    mockConn.expects(once()).method("isBindAttempted").will(returnValue(false));
    mockLivenessValidator.expects(once()).method("isConnectionAlive").will(returnValue(true));

    assertTrue(factory.validateObject(conn));
  }
  /**
   * Verifies that {@link PooledLDAPConnectionFactory#validateObject(Object)} lowers {@link
   * PooledLDAPConnection}'s active flag if the current {@link LdapConnectionLivenessValidator}
   * reports that the connection is not live.
   *
   * @throws LDAPException test failure
   */
  public void testValidateObjectLowersActiveFlagIfConnectionIsNotAlive() throws LDAPException {

    factory.setConnectionLivenessValidator(livenessValidator);
    mockConn.expects(once()).method("isBindAttempted").will(returnValue(false));
    mockLivenessValidator.expects(once()).method("isConnectionAlive").will(returnValue(false));
    mockConn
        .expects(once())
        .method("setActive")
        .with(eq(false))
        .after(mockLivenessValidator, "isConnectionAlive");
    assertFalse(factory.validateObject(conn));
  }
  public void testValidateObjectLowersActiveFlagIfLivenessValidationThrowsException() {

    factory.setConnectionLivenessValidator(livenessValidator);
    mockConn.expects(once()).method("isBindAttempted").will(returnValue(false));
    mockLivenessValidator
        .expects(once())
        .method("isConnectionAlive")
        .will(throwException(new RuntimeException("catch me")));
    mockConn
        .expects(once())
        .method("setActive")
        .with(eq(false))
        .after(mockLivenessValidator, "isConnectionAlive");
    assertFalse(factory.validateObject(conn));
  }
  /**
   * Verifies that a failed rebind during connection validation marks the connection as "inactive".
   * This ensures that the connection is not returned to the pool by {@link
   * PooledLDAPConnection#finalize()}. Technically, a failed rebind does not mean the connection is
   * stale but failing to bind as the system user should indicate that something is very much wrong,
   * so reallocating a connection is not a bad idea. Certainly better than finding oneself caught in
   * an endless loop of validating stale connections.
   */
  public void testValidateObjectLowersActiveFlagIfRebindFails()
      throws UnsupportedEncodingException {

    setConnectionManagerConfigExpectations();
    factory.setConnectionManager(connMgr);
    LDAPException bindFailure = new LDAPException();

    mockConn.expects(once()).method("isBindAttempted").will(returnValue(true));
    mockConn
        .expects(once())
        .method("bind")
        .with(
            eq(LDAPConnection.LDAP_V3),
            eq(connMgrConfig.getLdapUser()),
            eq(connMgrConfig.getLdapPassword().getBytes("UTF-8")))
        .after("isBindAttempted")
        .will(throwException(bindFailure));
    mockConn.expects(once()).method("setActive").with(eq(false)).after("bind");

    assertFalse(factory.validateObject(conn));
  }
  /**
   * Verifies that the {@link PooledLdapConnection} to be validated is re-bound as the system user
   * prior to testing the connection for liveness. This is only relevant if the client has bound the
   * connection as another user and the autoBind behavior has been enabled.
   */
  public void testValidateObjectRebindsAsAutoBindUserIfNecessaryPriorToTestingConnectionLiveness()
      throws UnsupportedEncodingException {

    setConnectionManagerConfigExpectations();
    factory.setConnectionManager(connMgr);

    mockConn.expects(once()).method("isBindAttempted").will(returnValue(true));
    mockConn
        .expects(once())
        .method("bind")
        .with(
            eq(LDAPConnection.LDAP_V3),
            eq(connMgrConfig.getLdapUser()),
            eq(connMgrConfig.getLdapPassword().getBytes("UTF-8")))
        .after("isBindAttempted");
    mockConn.expects(once()).method("setBindAttempted").with(eq(false)).after("bind");

    mockLivenessValidator.expects(once()).method("isConnectionAlive").will(returnValue(true));
    factory.setConnectionLivenessValidator(livenessValidator);
    assertTrue(factory.validateObject(conn));
  }
  public void testInvalidatesNullObjects() {

    assertFalse(factory.validateObject(null));
  }