From 72650d4cc41c64136d064967d7fec3726d850fee Mon Sep 17 00:00:00 2001
From: Ludovic Poitou <ludovic.poitou@forgerock.com>
Date: Thu, 14 Oct 2010 11:52:28 +0000
Subject: [PATCH] Multiple enhancements and bug fixes to the SDK (update from OpenDS by matthew_swift):

---
 sdk/tests/unit-tests-testng/src/org/opends/sdk/ConnectionFactoryTestCase.java |  148 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 105 insertions(+), 43 deletions(-)

diff --git a/sdk/tests/unit-tests-testng/src/org/opends/sdk/ConnectionFactoryTestCase.java b/sdk/tests/unit-tests-testng/src/org/opends/sdk/ConnectionFactoryTestCase.java
index a3a876f..3984c96 100644
--- a/sdk/tests/unit-tests-testng/src/org/opends/sdk/ConnectionFactoryTestCase.java
+++ b/sdk/tests/unit-tests-testng/src/org/opends/sdk/ConnectionFactoryTestCase.java
@@ -32,12 +32,15 @@
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
+import java.net.InetSocketAddress;
+import java.util.Arrays;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
 import org.opends.sdk.requests.DigestMD5SASLBindRequest;
 import org.opends.sdk.requests.Requests;
 import org.opends.sdk.requests.SearchRequest;
+import org.testng.Assert;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 import org.testng.annotations.DataProvider;
@@ -45,6 +48,7 @@
 import javax.net.ssl.SSLContext;
 
 
+
 /**
  * Tests the connectionfactory classes.
  */
@@ -55,7 +59,7 @@
     // latch.
     private final CountDownLatch latch;
     // invalid flag.
-    private volatile boolean invalid;
+    private volatile ErrorResultException error;
 
 
 
@@ -69,7 +73,7 @@
     public void handleErrorResult(final ErrorResultException error)
     {
       // came here.
-      invalid = true;
+      this.error = error;
       latch.countDown();
     }
 
@@ -96,66 +100,125 @@
     TestCaseUtils.startServer();
   }
 
+
+
   @DataProvider(name = "connectionFactories")
-  public Object[][] getModifyDNRequests() throws Exception
+  public Object[][] getConnectyionFactories() throws Exception
   {
-    Object[][] factories = new Object[7][1];
+    Object[][] factories = new Object[21][1];
 
     // HeartBeatConnectionFactory
     // Use custom search request.
     SearchRequest request = Requests.newSearchRequest(
-        "uid=user.0,ou=people,o=test", SearchScope.BASE_OBJECT, "objectclass=*",
-        "cn");
+        "uid=user.0,ou=people,o=test", SearchScope.BASE_OBJECT,
+        "objectclass=*", "cn");
 
-    factories[0][0] = new HeartBeatConnectionFactory(
-        new LDAPConnectionFactory("localhost", TestCaseUtils.getLdapPort()),
-        1000, TimeUnit.MILLISECONDS, request);
+    factories[0][0] = new HeartBeatConnectionFactory(new LDAPConnectionFactory(
+        "localhost", TestCaseUtils.getLdapPort()), 1000, TimeUnit.MILLISECONDS,
+        request);
 
     // InternalConnectionFactory
-    factories[1][0] = Connections
-      .newInternalConnectionFactory(LDAPServer.getInstance(), null);
+    factories[1][0] = Connections.newInternalConnectionFactory(
+        LDAPServer.getInstance(), null);
 
     // AuthenticatedConnectionFactory
     factories[2][0] = new AuthenticatedConnectionFactory(
-      new LDAPConnectionFactory("localhost", TestCaseUtils.getLdapPort()),
-      Requests.newSimpleBindRequest("", ""));
+        new LDAPConnectionFactory("localhost", TestCaseUtils.getLdapPort()),
+        Requests.newSimpleBindRequest("", ""));
 
     // AuthenticatedConnectionFactory with multi-stage SASL
     factories[3][0] = new AuthenticatedConnectionFactory(
-      new LDAPConnectionFactory("localhost", TestCaseUtils.getLdapPort()),
-      Requests.newCRAMMD5SASLBindRequest("id:user",
+        new LDAPConnectionFactory("localhost", TestCaseUtils.getLdapPort()),
+        Requests.newCRAMMD5SASLBindRequest("id:user",
             ByteString.valueOf("password")));
 
     // LDAPConnectionFactory with default options
-    factories[4][0] = new LDAPConnectionFactory(
-      "localhost", TestCaseUtils.getLdapPort());
+    factories[4][0] = new LDAPConnectionFactory("localhost",
+        TestCaseUtils.getLdapPort());
 
     // LDAPConnectionFactory with startTLS
-    SSLContext sslContext = new SSLContextBuilder().
-        setTrustManager(TrustManagers.trustAll()).getSSLContext();
-    LDAPOptions options = new LDAPOptions().setSSLContext(sslContext).
-        setUseStartTLS(true).setEnabledCipherSuites(
-        new String[]{"SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
-                         "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
-                         "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
-                         "SSL_DH_anon_WITH_DES_CBC_SHA",
-                         "SSL_DH_anon_WITH_RC4_128_MD5",
-                         "TLS_DH_anon_WITH_AES_128_CBC_SHA",
-                         "TLS_DH_anon_WITH_AES_256_CBC_SHA"});
-    factories[5][0] = new LDAPConnectionFactory(
-        "localhost", TestCaseUtils.getLdapPort(), options);
+    SSLContext sslContext = new SSLContextBuilder().setTrustManager(
+        TrustManagers.trustAll()).getSSLContext();
+    LDAPOptions options = new LDAPOptions()
+        .setSSLContext(sslContext)
+        .setUseStartTLS(true)
+        .setEnabledCipherSuites(
+            new String[] { "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
+                "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
+                "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
+                "SSL_DH_anon_WITH_DES_CBC_SHA", "SSL_DH_anon_WITH_RC4_128_MD5",
+                "TLS_DH_anon_WITH_AES_128_CBC_SHA",
+                "TLS_DH_anon_WITH_AES_256_CBC_SHA" });
+    factories[5][0] = new LDAPConnectionFactory("localhost",
+        TestCaseUtils.getLdapPort(), options);
 
     // startTLS + SASL confidentiality
+    // Use IP address here so that DIGEST-MD5 host verification works if local
+    // host name is not localhost (e.g. on some machines it might be
+    // localhost.localdomain).
     factories[6][0] = new AuthenticatedConnectionFactory(
-        new LDAPConnectionFactory("localhost", TestCaseUtils.getLdapPort(),
-            options), Requests.newDigestMD5SASLBindRequest("id:user",
-            ByteString.valueOf("password")).setQOP(
-            DigestMD5SASLBindRequest.QOPOption.AUTH_CONF).setCipher(
-            DigestMD5SASLBindRequest.CipherOption.TRIPLE_DES_RC4));
+        new LDAPConnectionFactory(new InetSocketAddress("127.0.0.1",
+            TestCaseUtils.getLdapPort()), options), Requests
+            .newDigestMD5SASLBindRequest("id:user",
+                ByteString.valueOf("password"))
+            .addQOP(DigestMD5SASLBindRequest.QOP_AUTH_CONF)
+            .setCipher(DigestMD5SASLBindRequest.CIPHER_LOW));
+
+    // Connection pool and load balancing tests.
+    ConnectionFactory offlineServer1 = Connections.newNamedConnectionFactory(
+        new LDAPConnectionFactory("localhost", TestCaseUtils.findFreePort()),
+        "offline1");
+    ConnectionFactory offlineServer2 = Connections.newNamedConnectionFactory(
+        new LDAPConnectionFactory("localhost", TestCaseUtils.findFreePort()),
+        "offline2");
+    ConnectionFactory onlineServer = Connections.newNamedConnectionFactory(
+        new LDAPConnectionFactory("localhost", TestCaseUtils.getLdapPort()),
+        "online");
+
+    // Connection pools.
+    factories[7][0] = Connections.newConnectionPool(onlineServer, 10);
+
+    // Round robin.
+    factories[8][0] = Connections
+        .newLoadBalancer(new RoundRobinLoadBalancingAlgorithm(Arrays.asList(
+            onlineServer, offlineServer1)));
+    factories[9][0] = factories[8][0];
+    factories[10][0] = factories[8][0];
+    factories[11][0] = Connections
+        .newLoadBalancer(new RoundRobinLoadBalancingAlgorithm(Arrays.asList(
+            offlineServer1, onlineServer)));
+    factories[12][0] = Connections
+        .newLoadBalancer(new RoundRobinLoadBalancingAlgorithm(Arrays.asList(
+            offlineServer1, offlineServer2, onlineServer)));
+    factories[13][0] = Connections
+        .newLoadBalancer(new RoundRobinLoadBalancingAlgorithm(Arrays.asList(
+            Connections.newConnectionPool(offlineServer1, 10),
+            Connections.newConnectionPool(onlineServer, 10))));
+
+    // Fail-over.
+    factories[14][0] = Connections
+        .newLoadBalancer(new FailoverLoadBalancingAlgorithm(Arrays.asList(
+            onlineServer, offlineServer1)));
+    factories[15][0] = factories[14][0];
+    factories[16][0] = factories[14][0];
+    factories[17][0] = Connections
+        .newLoadBalancer(new FailoverLoadBalancingAlgorithm(Arrays.asList(
+            offlineServer1, onlineServer)));
+    factories[18][0] = Connections
+        .newLoadBalancer(new FailoverLoadBalancingAlgorithm(Arrays.asList(
+            offlineServer1, offlineServer2, onlineServer)));
+    factories[19][0] = Connections
+        .newLoadBalancer(new FailoverLoadBalancingAlgorithm(Arrays.asList(
+            Connections.newConnectionPool(offlineServer1, 10),
+            Connections.newConnectionPool(onlineServer, 10))));
+
+    factories[20][0] = Connections.newConnectionPool(onlineServer, 10);
 
     return factories;
   }
 
+
+
   /**
    * Tests the async connection in the blocking mode. This is not fully async as
    * it blocks on the future.
@@ -166,10 +229,10 @@
   public void testBlockingFutureNoHandler(ConnectionFactory factory)
       throws Exception
   {
-    final FutureResult<AsynchronousConnection> future =
-        factory.getAsynchronousConnection(null);
+    final FutureResult<AsynchronousConnection> future = factory
+        .getAsynchronousConnection(null);
     final AsynchronousConnection con = future.get();
-    // quickly check if iit is a valid connection.
+    // quickly check if it is a valid connection.
     // Don't use a result handler.
     assertNotNull(con.readRootDSE(null).get());
     con.close();
@@ -189,15 +252,14 @@
     // Use the handler to get the result asynchronously.
     final CountDownLatch latch = new CountDownLatch(1);
     final MyResultHandler handler = new MyResultHandler(latch);
-    final FutureResult<AsynchronousConnection> future =
-        factory.getAsynchronousConnection(handler);
+    final FutureResult<AsynchronousConnection> future = factory
+        .getAsynchronousConnection(handler);
     // Since we don't have anything to do, we would rather
     // be notified by the latch when the other thread calls our handler.
     latch.await(); // should do a timed wait rather?
-    if (handler.invalid)
+    if (handler.error != null)
     {
-      // There was an error.
-      throw new Exception();
+      throw handler.error;
     }
   }
 

--
Gitblit v1.10.0