From f94601c43a60f861c6ef4ee7b76697e3987d207b Mon Sep 17 00:00:00 2001
From: gbellato <gbellato@localhost>
Date: Fri, 24 Nov 2006 08:27:51 +0000
Subject: [PATCH] Add a basic test for the synchronization changelog. This test allows to check that the changelog code is working without involving the server side code.

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/UpdateComparatorTest.java |    3 
 opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/UpdateOperationTest.java            |    9 +
 /dev/null                                                                                                       |   55 -------------
 opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/ProtocolWindowTest.java             |    2 
 opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/StressTest.java                     |    3 
 opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/ChangelogTest.java        |  103 +++++++++++++++++++++++++
 opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/SynchronizationTestCase.java        |    8 +-
 7 files changed, 118 insertions(+), 65 deletions(-)

diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/ProtocolWindowTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/ProtocolWindowTest.java
index be2f00b..7a2ac23 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/ProtocolWindowTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/ProtocolWindowTest.java
@@ -99,7 +99,7 @@
     final DN baseDn = DN.decode("ou=People,dc=example,dc=com");
 
     ChangelogBroker broker = openChangelogSession(baseDn, (short) 13,
-        WINDOW_SIZE);
+        WINDOW_SIZE, 8989, 1000);
 
     try {
       
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/StressTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/StressTest.java
index 8cfdec3..a0d621f 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/StressTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/StressTest.java
@@ -111,7 +111,8 @@
     final int TOTAL_MESSAGES = 1000;
     cleanEntries();
 
-    ChangelogBroker broker = openChangelogSession(baseDn, (short) 18, 100);
+    ChangelogBroker broker =
+      openChangelogSession(baseDn, (short) 18, 100, 8989, 5000);
     Monitor monitor = new Monitor("stress test monitor");
     DirectoryServer.registerMonitorProvider(monitor);
 
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/SynchronizationTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/SynchronizationTestCase.java
index d06ab1f..b9536f6 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/SynchronizationTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/SynchronizationTestCase.java
@@ -107,11 +107,10 @@
   
   /**
    * Open a changelog session to the local Changelog server.
-   * @param window_size 
    *
    */
   protected ChangelogBroker openChangelogSession(
-      final DN baseDn, short serverId, int window_size)
+      final DN baseDn, short serverId, int window_size, int port, int timeout)
           throws Exception, SocketException
   {
     PersistentServerState state = new PersistentServerState(baseDn);
@@ -119,9 +118,10 @@
     ChangelogBroker broker = new ChangelogBroker(
         state, baseDn, serverId, 0, 0, 0, 0, window_size);
     ArrayList<String> servers = new ArrayList<String>(1);
-    servers.add("localhost:8989");
+    servers.add("localhost:" + port);
     broker.start(servers);
-    broker.setSoTimeout(1000);
+    if (timeout != 0)
+      broker.setSoTimeout(timeout);
     /*
      * loop receiving update until there is nothing left
      * to make sure that message from previous tests have been consumed.
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/UpdateOperationTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/UpdateOperationTest.java
index 31dc4cd..3381157 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/UpdateOperationTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/UpdateOperationTest.java
@@ -248,7 +248,8 @@
      * Open a session to the changelog server using the Changelog broker API.
      * This must use a serverId different from the LDAP server ID
      */
-    ChangelogBroker broker = openChangelogSession(baseDn, (short) 2, 100);
+    ChangelogBroker broker =
+      openChangelogSession(baseDn, (short) 2, 100, 8989, 1000);
 
     /*
      * Create a Change number generator to generate new changenumbers
@@ -560,7 +561,8 @@
 
     cleanEntries();
 
-    ChangelogBroker broker = openChangelogSession(baseDn, (short) 27, 100);
+    ChangelogBroker broker =
+      openChangelogSession(baseDn, (short) 27, 100, 8989, 1000);
     try {
       ChangeNumberGenerator gen = new ChangeNumberGenerator((short) 27, 0);
 
@@ -956,7 +958,8 @@
     final DN baseDn = DN.decode("ou=People,dc=example,dc=com");
 
     Thread.sleep(2000);
-    ChangelogBroker broker = openChangelogSession(baseDn, (short) 11, 100);
+    ChangelogBroker broker =
+      openChangelogSession(baseDn, (short) 11, 100, 8989, 1000);
     try
     {
       ChangeNumberGenerator gen = new ChangeNumberGenerator((short) 11, 0);
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/ChangeLogTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/ChangeLogTestCase.java
deleted file mode 100644
index 186343e..0000000
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/ChangeLogTestCase.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * CDDL HEADER START
- *
- * The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License").  You may not use this file except in compliance
- * with the License.
- *
- * You can obtain a copy of the license at
- * trunk/opends/resource/legal-notices/OpenDS.LICENSE
- * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
- * See the License for the specific language governing permissions
- * and limitations under the License.
- *
- * When distributing Covered Code, include this CDDL HEADER in each
- * file and include the License file at
- * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
- * add the following below this CDDL HEADER, with the fields enclosed
- * by brackets "[]" replaced with your own identifying * information:
- *      Portions Copyright [yyyy] [name of copyright owner]
- *
- * CDDL HEADER END
- *
- *
- *      Portions Copyright 2006 Sun Microsystems, Inc.
- */
-package org.opends.server.synchronization.changelog;
-
-import org.opends.server.DirectoryServerTestCase;
-import org.opends.server.TestCaseUtils;
-import org.testng.annotations.Test;
-
-import org.testng.annotations.BeforeClass;
-
-/**
- * An abstract class that all synchronization unit test should extend. 
- */
-@Test(groups = { "precommit", "synchronization" })
-public abstract class ChangeLogTestCase extends DirectoryServerTestCase
-{
-
-  /**
-   * Set up the environment for performing the tests in this suite.
-   * 
-   * @throws Exception
-   *         If the environment could not be set up.
-   */
-  @BeforeClass
-  public void setUp() throws Exception
-  {
-    // This test suite depends on having the schema available.
-    TestCaseUtils.startServer();
-  }
-
-}
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/ChangelogTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/ChangelogTest.java
new file mode 100644
index 0000000..02a2521
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/ChangelogTest.java
@@ -0,0 +1,103 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying * information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Portions Copyright 2006 Sun Microsystems, Inc.
+ */
+package org.opends.server.synchronization.changelog;
+
+import java.net.ServerSocket;
+
+import org.opends.server.TestCaseUtils;
+import org.opends.server.config.ConfigEntry;
+import org.opends.server.synchronization.SynchronizationTestCase;
+import org.opends.server.synchronization.common.ChangeNumber;
+import org.opends.server.synchronization.plugin.ChangelogBroker;
+import org.opends.server.synchronization.protocol.DeleteMsg;
+import org.opends.server.synchronization.protocol.SynchronizationMessage;
+import org.opends.server.types.DN;
+import org.opends.server.types.Entry;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+/**
+ * Tests for the chngelog service code.
+ */
+
+public class ChangelogTest extends SynchronizationTestCase
+{
+  /**
+   * Basic test of the changelog code.
+   * Create a changelog server, connect 2 clients and exchange
+   * messages between the clients.
+   */
+  @Test()
+  public void changelogBasic() throws Exception
+  {
+    // find  a free port
+    ServerSocket socket = TestCaseUtils.bindFreePort();
+    int changelogPort = socket.getLocalPort();
+    socket.close();
+
+    String changelogLdif =
+      "dn: cn=Changelog Server\n"
+        + "objectClass: top\n"
+        + "objectClass: ds-cfg-synchronization-changelog-server-config\n"
+        + "cn: Changelog Server\n"
+        + "ds-cfg-changelog-port: "+ changelogPort + "\n"
+        + "ds-cfg-changelog-server-id: 1\n";
+    Entry tmp = TestCaseUtils.entryFromLdifString(changelogLdif);
+    ConfigEntry changelogConfig = new ConfigEntry(tmp, null);
+    Changelog changelog = new Changelog(changelogConfig);
+
+    ChangelogBroker broker1 = null;
+    ChangelogBroker broker2 = null;
+    
+    try {
+      broker1 = openChangelogSession(
+          DN.decode("dc=example,dc=com"), (short) 1, 100, changelogPort, 1000);
+      broker2 = openChangelogSession(
+          DN.decode("dc=example,dc=com"), (short) 2, 100, changelogPort, 1000);
+
+      ChangeNumber cn = new ChangeNumber((long) 1, 1, (short)1);
+      DeleteMsg msg = new DeleteMsg("o=test,dc=example,dc=com", cn, "uid");
+      broker1.publish(msg);
+      SynchronizationMessage msg2 = broker2.receive();
+      if (msg2 instanceof DeleteMsg)
+      {
+        DeleteMsg del = (DeleteMsg) msg2;
+        assertTrue(del.toString().equals(msg2.toString()));
+      }
+      else
+        fail("Changelog transmission failed");
+    }
+    finally
+    {
+      changelog.shutdown();
+      if (broker1 != null)
+        broker1.stop();
+      if (broker2 != null)
+        broker2.stop();
+    }
+  }
+}
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/UpdateComparatorTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/UpdateComparatorTest.java
index e753523..1faa317 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/UpdateComparatorTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/synchronization/changelog/UpdateComparatorTest.java
@@ -35,6 +35,7 @@
 
 import org.opends.server.core.DeleteOperation;
 import org.opends.server.protocols.internal.InternalClientConnection;
+import org.opends.server.synchronization.SynchronizationTestCase;
 import org.opends.server.synchronization.changelog.UpdateComparator;
 import org.opends.server.synchronization.common.ChangeNumber;
 import org.opends.server.synchronization.protocol.DeleteContext;
@@ -49,7 +50,7 @@
 /**
  * Test ChangeNumber and ChangeNumberGenerator
  */
-public class UpdateComparatorTest extends ChangeLogTestCase
+public class UpdateComparatorTest extends SynchronizationTestCase
 {
 
   /**

--
Gitblit v1.10.0