Search in sources :

Example 16 with LDAPSDKUsageException

use of com.unboundid.util.LDAPSDKUsageException in project ldapsdk by pingidentity.

the class UpdateConnectionPoolBindRequestAndServerSetTestCase method testSetServerSetForLDAPThreadLocalConnectionPool.

/**
 * Tests the behavior of the
 * {@link LDAPThreadLocalConnectionPool#setServerSet} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSetServerSetForLDAPThreadLocalConnectionPool() throws Exception {
    // Create three different in-memory directory server instances to use for
    // testing.
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    cfg.addAdditionalBindCredentials("cn=Directory Manager", "password");
    final InMemoryDirectoryServer ds1 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds1.startListening();
    final InMemoryDirectoryServer ds2 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds2.startListening();
    final InMemoryDirectoryServer ds3 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds3.startListening();
    try (LDAPThreadLocalConnectionPool pool = new LDAPThreadLocalConnectionPool(new SingleServerSet("127.0.0.1", ds1.getListenPort()), new SimpleBindRequest("cn=Directory Manager", "password"))) {
        // Make sure that the connection is initially established to ds1.
        LDAPConnection conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        pool.releaseConnection(conn);
        // Update the server so that new connections will be established to ds2.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds2.getListenPort()));
        // Make sure that the existing connection is still connected to ds1.
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        // Release the connection as defunct and make sure that the new connection
        // is connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Try to set a null server set.  That should be rejected.
        try {
            pool.setServerSet(null);
            fail("Expected an exception when setting a null server set.");
        } catch (final LDAPSDKUsageException e) {
        // This was expected.
        }
        // It should still be possible to get new connections, and they should
        // still be connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Set the server set so that new connections will be established to ds3.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds3.getListenPort()));
        // Make sure that new connections are established to ds3.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds3.getListenPort());
        pool.releaseConnection(conn);
    } finally {
        ds1.shutDown(true);
        ds2.shutDown(true);
        ds3.shutDown(true);
    }
}
Also used : InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) InMemoryDirectoryServerConfig(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) Test(org.testng.annotations.Test)

Example 17 with LDAPSDKUsageException

use of com.unboundid.util.LDAPSDKUsageException in project ldapsdk by pingidentity.

the class UpdateConnectionPoolBindRequestAndServerSetTestCase method testSetServerSetForLDAPConnectionPoolFromConn.

/**
 * Tests the behavior of the {@link LDAPConnectionPool#setServerSet} method
 * for a connection pool created with an already-established connection rather
 * than a server set and a bind request.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSetServerSetForLDAPConnectionPoolFromConn() throws Exception {
    // Create three different in-memory directory server instances to use for
    // testing.
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    cfg.addAdditionalBindCredentials("cn=Directory Manager", "password");
    final InMemoryDirectoryServer ds1 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds1.startListening();
    final InMemoryDirectoryServer ds2 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds2.startListening();
    final InMemoryDirectoryServer ds3 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds3.startListening();
    try (LDAPConnection connection = new LDAPConnection("127.0.0.1", ds1.getListenPort(), "cn=Directory Manager", "password");
        LDAPConnectionPool pool = new LDAPConnectionPool(connection, 1, 1)) {
        // Make sure that the connection is initially established to ds1.
        LDAPConnection conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        pool.releaseConnection(conn);
        // Update the server so that new connections will be established to ds2.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds2.getListenPort()));
        // Make sure that the existing connection is still connected to ds1.
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        // Release the connection as defunct and make sure that the new connection
        // is connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Try to set a null server set.  That should be rejected.
        try {
            pool.setServerSet(null);
            fail("Expected an exception when setting a null server set.");
        } catch (final LDAPSDKUsageException e) {
        // This was expected.
        }
        // It should still be possible to get new connections, and they should
        // still be connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Set the server set so that new connections will be established to ds3.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds3.getListenPort()));
        // Make sure that new connections are established to ds3.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds3.getListenPort());
        pool.releaseConnection(conn);
    } finally {
        ds1.shutDown(true);
        ds2.shutDown(true);
        ds3.shutDown(true);
    }
}
Also used : InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) InMemoryDirectoryServerConfig(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) Test(org.testng.annotations.Test)

Aggregations

LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)17 Test (org.testng.annotations.Test)17 JSONString (com.unboundid.util.json.JSONString)12 JSONField (com.unboundid.util.json.JSONField)8 JSONObject (com.unboundid.util.json.JSONObject)8 JSONArray (com.unboundid.util.json.JSONArray)5 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)4 InMemoryDirectoryServerConfig (com.unboundid.ldap.listener.InMemoryDirectoryServerConfig)4 JSONNumber (com.unboundid.util.json.JSONNumber)4 JSONException (com.unboundid.util.json.JSONException)1