From 87941270b9bf6e86db5ab72f0ac2826cdfef9ed2 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Mon, 04 Sep 2006 05:59:21 +0000
Subject: [PATCH] Update the Directory Server build process so that the test target will create a minimized installation that will allow the server to be started for those tests that need access to the configuration, schema, or any other aspect of the running server.  Also, update the test cases to make use of this capability when necessary.

---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/SubtreeSpecificationTestCase.java |   18 +---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestBackendImpl.java      |    6 -
 opendj-sdk/opends/build.xml                                                                                   |   57 +++++++++++++-
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java                     |   56 ++++++++++++++
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestEntryContainer.java   |    8 -
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestJebFormat.java        |    7 -
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestDN.java                       |   18 +---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestEntry.java                    |   18 +---
 8 files changed, 129 insertions(+), 59 deletions(-)

diff --git a/opendj-sdk/opends/build.xml b/opendj-sdk/opends/build.xml
index c466f48..e867500 100644
--- a/opendj-sdk/opends/build.xml
+++ b/opendj-sdk/opends/build.xml
@@ -53,6 +53,8 @@
 
   <property name="unittest.classes.dir"
        location="${build.dir}/unit-tests/classes" />
+  <property name="unittest.package.dir"
+       location="${build.dir}/unit-tests/package" />
   <property name="unittest.report.dir"
        location="${build.dir}/unit-tests/report"/>
 
@@ -577,9 +579,54 @@
 
 
 
+  <!-- Create a minimal install suitable for running unit tests. -->
+  <target name="testpackage" depends="testinit">
+    <mkdir dir="${unittest.package.dir}"                 />
+    <mkdir dir="${unittest.package.dir}/bak"             />
+    <mkdir dir="${unittest.package.dir}/classes"         />
+    <mkdir dir="${unittest.package.dir}/config"          />
+    <mkdir dir="${unittest.package.dir}/config/schema"   />
+    <mkdir dir="${unittest.package.dir}/config/messages" />
+    <mkdir dir="${unittest.package.dir}/config/MakeLDIF" />
+    <mkdir dir="${unittest.package.dir}/db"              />
+    <mkdir dir="${unittest.package.dir}/changelogDb"     />
+    <mkdir dir="${unittest.package.dir}/ldif"            />
+    <mkdir dir="${unittest.package.dir}/lib"             />
+    <mkdir dir="${unittest.package.dir}/locks"           />
+    <mkdir dir="${unittest.package.dir}/logs"            />
+
+    <jar jarfile="${unittest.package.dir}/lib/OpenDS.jar"
+         basedir="${classes.dir}" compress="true" index="true" />
+
+    <jar jarfile="${unittest.package.dir}/lib/OpenDS-tests.jar"
+         basedir="${unittest.classes.dir}" compress="true" index="true" />
+
+    <copy todir="${unittest.package.dir}/lib">
+      <fileset file="${lib.dir}/*.jar" />
+    </copy>
+
+    <copy todir="${unittest.package.dir}/config">
+      <fileset file="${config.dir}/*" />
+    </copy>
+
+    <copy todir="${unittest.package.dir}/config/schema">
+      <fileset dir="${resource.dir}/schema" />
+    </copy>
+
+    <copy todir="${unittest.package.dir}/config/messages">
+      <fileset dir="${resource.dir}/messages" />
+    </copy>
+
+    <copy todir="${unittest.package.dir}/config/MakeLDIF">
+      <fileset dir="${resource.dir}/MakeLDIF" />
+    </copy>
+  </target>
+
+
+
   <!-- Execute the Directory Server TestNG unit tests in text mode. -->
   <target name="test"
-          depends="testinit,runtests"
+          depends="testinit,testpackage,runtests"
           description="Execute the Directory Server TestNG unit tests in text mode.">
   </target>
 
@@ -609,18 +656,20 @@
         <path refid="run.classpath" />
         <path refid="emma.lib" />
 
-        <fileset dir="${lib.dir}">
+        <fileset dir="${unittest.package.dir}/lib">
           <include name="*.jar" />
         </fileset>
 
         <fileset dir="${testng.lib.dir}">
           <include name="*.jar" />
         </fileset>
-
-        <pathelement location="${unittest.classes.dir}" />
       </classpath>
       <jvmarg  value="-Demma.coverage.out.file=${coverage.data.dir}/coverage.emma" />
       <jvmarg value="-Demma.coverage.out.merge=false" />
+      <jvmarg value="-Dorg.opends.server.ForceDaemonThreads=true" />
+      <jvmarg value="-Dorg.opends.server.ServerRoot=${unittest.package.dir}" />
+      <jvmarg value="-Dorg.opends.server.ConfigClass=org.opends.server.config.ConfigFileHandler" />
+      <jvmarg value="-Dorg.opends.server.ConfigFile=${unittest.package.dir}/config/config.ldif" />
       <xmlfileset dir="${testng.dir}" includes="testng.xml" />
     </testng>
 
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
index a1b2dca..104a0ab 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -33,11 +33,67 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import org.opends.server.config.ConfigException;
+import org.opends.server.core.DirectoryServer;
+import org.opends.server.core.InitializationException;
+import org.opends.server.loggers.Error;
+import org.opends.server.loggers.Debug;
+
 /**
  * This class defines some utility functions which can be used by test
  * cases.
  */
 public final class TestCaseUtils {
+  /**
+   * The name of the system property that can be used to set the fully-qualified
+   * name of the Java class to use as the config handler.
+   */
+  public static final String PROPERTY_CONFIG_CLASS =
+       "org.opends.server.ConfigClass";
+
+  /**
+   * The name of the system property that can be used to set the path to the
+   * Directory Server configuration file.
+   */
+  public static final String PROPERTY_CONFIG_FILE =
+       "org.opends.server.ConfigFile";
+
+  /**
+   * Indicates whether the server has already been started.
+   */
+  private static boolean serverStarted = false;
+
+  /**
+   * Starts the Directory Server so that it will be available for use while
+   * running the unit tests.  This will only actually start the server once, so
+   * subsequent attempts to start it will be ignored because it will already be
+   * available.
+   *
+   * @throws  InitializationException  If a problem occurs while starting the
+   *                                   server.
+   *
+   * @throws  ConfigException  If there is a problem with the server
+   *                           configuration.
+   */
+  public static void startServer()
+         throws InitializationException, ConfigException
+  {
+    if (serverStarted)
+    {
+      return;
+    }
+
+    String configClass = System.getProperty(PROPERTY_CONFIG_CLASS);
+    String configFile  = System.getProperty(PROPERTY_CONFIG_FILE);
+
+    DirectoryServer directoryServer = DirectoryServer.getInstance();
+    directoryServer.bootstrapServer();
+    directoryServer.initializeConfiguration(configClass, configFile);
+    Error.removeAllErrorLoggers(false);
+    Debug.removeAllDebugLoggers(false);
+    directoryServer.startServer();
+    serverStarted = true;
+  }
 
   /**
    * Create a temporary directory with the specified prefix.
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestBackendImpl.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestBackendImpl.java
index ccea5d5..938f53f 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestBackendImpl.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestBackendImpl.java
@@ -29,7 +29,6 @@
 import java.io.ByteArrayInputStream;
 import java.io.File;
 
-import org.opends.server.InitialDirectoryServerFixture;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.api.Backend;
 import org.opends.server.config.ConfigEntry;
@@ -71,7 +70,8 @@
    */
   @BeforeClass
   public void setUp() throws Exception {
-    InitialDirectoryServerFixture.FACTORY.setUp();
+    // Make sure that the server is up and running.
+    TestCaseUtils.startServer();
 
     tempDir = TestCaseUtils.createTemporaryDirectory("jebtest");
     homeDirName = tempDir.getAbsolutePath();
@@ -96,8 +96,6 @@
    */
   @AfterClass
   public void tearDown() throws Exception {
-    InitialDirectoryServerFixture.FACTORY.tearDown();
-
     TestCaseUtils.deleteDirectory(tempDir);
   }
 
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestEntryContainer.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestEntryContainer.java
index 125723a..9e0a429 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestEntryContainer.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestEntryContainer.java
@@ -34,7 +34,6 @@
 import java.io.OutputStreamWriter;
 import java.util.ArrayList;
 
-import org.opends.server.SchemaFixture;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.types.Entry;
 import org.opends.server.types.LDIFImportConfig;
@@ -131,8 +130,9 @@
    */
   @BeforeClass
   public void setUp() throws Exception {
-    // This test suite depends on having the schema available.
-    SchemaFixture.FACTORY.setUp();
+    // This test suite depends on having the schema available, so we'll make
+    // sure the server is started.
+    TestCaseUtils.startServer();
 
     tempDir = TestCaseUtils.createTemporaryDirectory("jebtest");
     homeDirName = tempDir.getAbsolutePath();
@@ -169,8 +169,6 @@
    */
   @AfterClass
   public void tearDown() throws Exception {
-    SchemaFixture.FACTORY.tearDown();
-
     TestCaseUtils.deleteDirectory(tempDir);
   }
 
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestJebFormat.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestJebFormat.java
index 428af7f..430a948 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestJebFormat.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestJebFormat.java
@@ -33,7 +33,7 @@
 import java.util.Arrays;
 import java.util.List;
 
-import org.opends.server.InitialDirectoryServerFixture;
+import org.opends.server.TestCaseUtils;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.types.Attribute;
 import org.opends.server.types.AttributeType;
@@ -191,7 +191,8 @@
    */
   @Test()
   public void testEntryToAndFromDatabase() throws Exception {
-    InitialDirectoryServerFixture.FACTORY.setUp();
+    // Make sure that the server is up and running.
+    TestCaseUtils.startServer();
 
     // Convert the test LDIF string to a byte array
     byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString);
@@ -254,7 +255,5 @@
       }
     }
     reader.close();
-
-    InitialDirectoryServerFixture.FACTORY.tearDown();
   }
 }
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/SubtreeSpecificationTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/SubtreeSpecificationTestCase.java
index 381675e..5541c30 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/SubtreeSpecificationTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/SubtreeSpecificationTestCase.java
@@ -31,7 +31,7 @@
 import java.util.HashSet;
 import java.util.Set;
 
-import org.opends.server.SchemaFixture;
+import org.opends.server.TestCaseUtils;
 import org.opends.server.types.DN;
 import org.opends.server.types.Entry;
 import org.opends.server.types.ObjectClass;
@@ -76,8 +76,9 @@
    */
   @BeforeClass
   public final void setUp() throws Exception {
-    // This test suite depends on having the schema available.
-    SchemaFixture.FACTORY.setUp();
+    // This test suite depends on having the schema available, so we'll start
+    // the server.
+    TestCaseUtils.startServer();
 
     // Retrieve required object classes.
     objectClasses = new HashSet<ObjectClass>();
@@ -96,17 +97,6 @@
   }
 
   /**
-   * Tears down the environment for performing the tests in this suite.
-   *
-   * @throws Exception
-   *           If the environment could not be finalized.
-   */
-  @AfterClass
-  public final void tearDown() throws Exception {
-    SchemaFixture.FACTORY.tearDown();
-  }
-
-  /**
    * Get the common object classes.
    *
    * @return Returns the object classes.
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestDN.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestDN.java
index 5e3942e..691c08e 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestDN.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestDN.java
@@ -28,7 +28,7 @@
 
 import static org.testng.Assert.*;
 
-import org.opends.server.SchemaFixture;
+import org.opends.server.TestCaseUtils;
 import org.opends.server.protocols.asn1.ASN1OctetString;
 import org.opends.server.types.DN;
 import org.opends.server.types.RDN;
@@ -168,19 +168,9 @@
    */
   @BeforeClass
   public void setUp() throws Exception {
-    // This test suite depends on having the schema available.
-    SchemaFixture.FACTORY.setUp();
-  }
-
-  /**
-   * Tears down the environment for performing the tests in this suite.
-   *
-   * @throws Exception
-   *           If the environment could not be finalized.
-   */
-  @AfterClass
-  public void tearDown() throws Exception {
-    SchemaFixture.FACTORY.tearDown();
+    // This test suite depends on having the schema available, so we'll start
+    // the server.
+    TestCaseUtils.startServer();
   }
 
   /**
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestEntry.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestEntry.java
index a7f12d4..4b57865 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestEntry.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestEntry.java
@@ -33,7 +33,7 @@
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 
-import org.opends.server.SchemaFixture;
+import org.opends.server.TestCaseUtils;
 import org.opends.server.api.SubtreeSpecificationSet;
 import org.opends.server.protocols.asn1.ASN1OctetString;
 import org.opends.server.schema.AttributeTypeSyntax;
@@ -132,19 +132,9 @@
    */
   @BeforeClass
   public void setUp() throws Exception {
-    // This test suite depends on having the schema available.
-    SchemaFixture.FACTORY.setUp();
-  }
-
-  /**
-   * Tears down the environment for performing the tests in this suite.
-   *
-   * @throws Exception
-   *           If the environment could not be finalized.
-   */
-  @AfterClass
-  public void tearDown() throws Exception {
-    SchemaFixture.FACTORY.tearDown();
+    // This test suite depends on having the schema available, so we'll start
+    // the server.
+    TestCaseUtils.startServer();
   }
 
   /**

--
Gitblit v1.10.0