protected void setUp() throws Exception {

    mockConn = mock(PooledLDAPConnection.class, "mockConn");
    conn = (PooledLDAPConnection) mockConn.proxy();

    factory =
        new PooledLDAPConnectionFactory() {
          @Override
          protected PooledLDAPConnection newConnection() {
            return conn;
          }
        };

    mockLDAPSearchResults = mock(LDAPSearchResults.class, "mockLDAPSearchResults");
    ldapSearchResults = (LDAPSearchResults) mockLDAPSearchResults.proxy();
    mockLDAPEntry = mock(LDAPEntry.class, "mockLDAPEntry");
    ldapEntry = (LDAPEntry) mockLDAPEntry.proxy();
    mockLivenessValidator = new Mock(LdapConnectionLivenessValidator.class);
    livenessValidator = (LdapConnectionLivenessValidator) mockLivenessValidator.proxy();
    factory.setConnectionLivenessValidator(livenessValidator);
    mockConnMgr = new Mock(LdapConnectionManager.class);
    connMgr = (LdapConnectionManager) mockConnMgr.proxy();
    mockConnMgrConfig = new Mock(LdapConnectionManagerConfig.class);
    connMgrConfig = (LdapConnectionManagerConfig) mockConnMgrConfig.proxy();
    // don't call setConnectionManager() b/c we don't know what expectations to set
    super.setUp();
  }
 /**
  * Verifies that {@link PooledLDAPConnectionFactory} is never without a {@link
  * LdapConnectionLivenessValidator}.
  */
 public void testDefaultsToNativeLdapConnectionLivenessValidatorIfThatPropertySetToNull() {
   factory.setConnectionLivenessValidator(null);
   Object livenessValidator = factory.getConnectionLivenessValidator();
   assertTrue(
       "Expected a NativeLdapConnectionLivenessValidator but was [" + livenessValidator + "]",
       livenessValidator instanceof NativeLdapConnectionLivenessValidator);
 }
  /**
   * 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 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));
  }