From 1a5143ec25e64ce012c5bbc9c0a15597c0ffcbc2 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 11 Jul 2013 13:15:43 +0000
Subject: [PATCH] Decouple some tests from running the server.

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/AciBodyTest.java   |   11 +++++
 opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/EnumRightTest.java |    3 +
 opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java                          |   42 ++++++++++++++++++++-
 opends/tests/unit-tests-testng/src/server/org/opends/server/types/AttributeTypeConstants.java           |   50 +++++++++++++++++++++++++
 4 files changed, 102 insertions(+), 4 deletions(-)

diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
index 2bb2e77..e0c51d3 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -103,6 +103,8 @@
 import org.opends.server.protocols.ldap.LDAPReader;
 import org.opends.server.tools.LDAPModify;
 import org.opends.server.tools.dsconfig.DSConfig;
+import org.opends.server.types.AttributeType;
+import org.opends.server.types.AttributeTypeConstants;
 import org.opends.server.types.ByteString;
 import org.opends.server.types.DN;
 import org.opends.server.types.DirectoryEnvironmentConfig;
@@ -113,6 +115,7 @@
 import org.opends.server.types.LDIFImportConfig;
 import org.opends.server.types.OperatingSystem;
 import org.opends.server.types.ResultCode;
+import org.opends.server.types.Schema;
 import org.opends.server.util.BuildVersion;
 import org.opends.server.util.EmbeddedUtils;
 import org.opends.server.util.LDIFReader;
@@ -248,6 +251,35 @@
   private static File testConfigDir;
 
   /**
+   * Setup in-memory versions of everything needed to run unit tests with the
+   * {@link DirectoryServer} class.
+   * <p>
+   * This method is trying hard to provide sensible defaults and core data you
+   * would expect from a normal install, including AttributeTypes, etc.
+   */
+  public static void startFakeServer()
+  {
+    DirectoryServer.setSchema(initializeInMemory(new Schema()));
+  }
+
+  private static Schema initializeInMemory(final Schema schema)
+  {
+    try
+    {
+      for (AttributeType attributeType : AttributeTypeConstants.ALL)
+      {
+        schema.registerAttributeType(attributeType, true);
+      }
+      return schema;
+    }
+    catch (DirectoryException e)
+    {
+      // rethrow this to fail the current test
+      throw new RuntimeException(e);
+    }
+  }
+
+  /**
    * 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
@@ -414,8 +446,14 @@
             + "instance.loc");
         installLoc.deleteOnExit();
         FileWriter w = new FileWriter(installLoc);
-        w.write(testInstanceRoot.getAbsolutePath());
-        w.close();
+        try
+        {
+          w.write(testInstanceRoot.getAbsolutePath());
+        }
+        finally
+        {
+          w.close();
+        }
 
         if (opendmkJar.exists())
         {
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/AciBodyTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/AciBodyTest.java
index f8ba1c2..284fcb4 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/AciBodyTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/AciBodyTest.java
@@ -28,13 +28,22 @@
 
 import static org.assertj.core.api.Assertions.*;
 
+import org.opends.server.DirectoryServerTestCase;
+import org.opends.server.TestCaseUtils;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 @SuppressWarnings("javadoc")
-public class AciBodyTest extends AciTestCase
+public class AciBodyTest extends DirectoryServerTestCase
 {
 
+  @BeforeClass
+  public void setUp()
+  {
+    TestCaseUtils.startFakeServer();
+  }
+
   @DataProvider(name = "validAcis")
   public Object[][] getValidAcis()
   {
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/EnumRightTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/EnumRightTest.java
index c946114..2294797 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/EnumRightTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/authorization/dseecompat/EnumRightTest.java
@@ -33,11 +33,12 @@
 import java.util.EnumSet;
 import java.util.Set;
 
+import org.opends.server.DirectoryServerTestCase;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 @SuppressWarnings("javadoc")
-public class EnumRightTest extends AciTestCase
+public class EnumRightTest extends DirectoryServerTestCase
 {
 
   private static final int ALL_RIGHTS_MASK = ACI_READ | ACI_WRITE | ACI_ADD
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/types/AttributeTypeConstants.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/types/AttributeTypeConstants.java
new file mode 100644
index 0000000..3a4c440
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/types/AttributeTypeConstants.java
@@ -0,0 +1,50 @@
+/*
+ * 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
+ *
+ *
+ *      Copyright 2013 ForgeRock AS
+ */
+package org.opends.server.types;
+
+import java.util.Collections;
+
+import org.opends.server.schema.OIDSyntax;
+
+/**
+ * Holds in-memory constants representing the AttributeTypes. This allow not
+ * starting the whole server for simple unit tests.
+ */
+@SuppressWarnings("javadoc")
+public interface AttributeTypeConstants
+{
+
+  AttributeType OBJECT_CLASS = new AttributeType(
+      "( 2.5.4.0 NAME 'objectClass' EQUALITY objectIdentifierMatch "
+          + "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 X-ORIGIN 'RFC 2256' )",
+      "objectClass", Collections.singletonList("objectClass"), "2.5.4.0", null,
+      null, new OIDSyntax(), AttributeUsage.USER_APPLICATIONS, false, false,
+      false, false);
+
+  AttributeType[] ALL = { OBJECT_CLASS, };
+
+}

--
Gitblit v1.10.0