public void testMakeObjectConnectsAndOtherwiseInitializesConnections()
      throws LDAPException, UnsupportedEncodingException {

    setConnectionManagerConfigExpectations();
    factory.setConnectionManager(connMgr);
    mockConn.expects(once()).method("setConnectionManager").with(same(connMgr));
    mockConn.expects(once()).method("setConstraints").with(NOT_NULL); // TODO make more specific
    mockConn
        .expects(once())
        .method("connect")
        .with(eq(connMgrConfig.getLdapHost()), eq(connMgrConfig.getLdapPort()))
        .after("setConstraints");
    mockConn.expects(once()).method("startTLS").after("connect");
    mockConn
        .expects(once())
        .method("bind")
        .with(
            eq(LDAPConnection.LDAP_V3),
            eq(connMgrConfig.getLdapUser()),
            eq(connMgrConfig.getLdapPassword().getBytes("UTF-8")))
        .after("connect");
    mockConn.expects(once()).method("setBindAttempted").with(eq(false)).after("bind");

    // the actual code exercise
    assertSame(conn, factory.makeObject());

    // TODO how to test socket factory assignment (static call)
  }
 /**
  * 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));
  }
  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();
  }
 public void testDestroyObjectIgnoresNonPoolableLdapConnections() {
   try {
     factory.destroyObject(mock(LDAPConnection.class).proxy());
   } catch (Exception e) {
     fail("Should have ignored non-poolable connection");
   }
 }
 public void testDestroyObjectIgnoresNullReferences() {
   try {
     factory.destroyObject(null);
   } catch (Exception e) {
     fail("Should have ignored null reference");
   }
 }
 /**
  * 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#activateObject(Object)} passes constraints and
   * an active flag to the given {@link PooledLDAPConnection}. Does not actually verify constraint
   * values.
   *
   * @throws LDAPException test error
   */
  public void testActivateObject() throws LDAPException {

    // TODO validate constraint assignment
    mockConn.expects(once()).method("setConstraints").with(ANYTHING);
    mockConn.expects(once()).method("setActive").with(eq(true));
    factory.activateObject(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 testDestroyObjectInvokesDisconnect() throws Exception {
   mockConn.expects(once()).method("setActive").with(eq(false));
   mockConn.expects(once()).method("disconnect").after("setActive");
   factory.destroyObject(conn);
 }
  public void testInvalidatesNullObjects() {

    assertFalse(factory.validateObject(null));
  }