From 20a6e252d40e11c46ed06a1529be80a2188cc880 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 19 May 2011 15:39:08 +0000
Subject: [PATCH] Fix OPENDJ-159 - LDAP connections use stale default schema if it is changed after factory creation

---
 opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/SchemaResolver.java            |   15 +++++++
 opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/LDAPServer.java                |    6 ++
 opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/DecodeOptions.java             |    3 +
 opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/ConnectionFactoryTestCase.java |   65 ++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 3 deletions(-)

diff --git a/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/DecodeOptions.java b/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/DecodeOptions.java
index 886191e..8d744d4 100644
--- a/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/DecodeOptions.java
+++ b/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/DecodeOptions.java
@@ -23,6 +23,7 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
+ *      Portions copyright 2011 ForgeRock AS
  */
 
 package org.opends.sdk;
@@ -92,7 +93,7 @@
   {
     this.attributeFactory = LinkedAttribute.FACTORY;
     this.entryFactory = LinkedHashMapEntry.FACTORY;
-    this.schemaResolver = new FixedSchemaResolver(Schema.getDefaultSchema());
+    this.schemaResolver = SchemaResolver.DEFAULT;
   }
 
 
diff --git a/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/SchemaResolver.java b/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/SchemaResolver.java
index f7243c4..44aed09 100644
--- a/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/SchemaResolver.java
+++ b/opendj-sdk/opendj3/opendj-sdk/src/main/java/org/opends/sdk/SchemaResolver.java
@@ -23,6 +23,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
+ *      Portions copyright 2011 ForgeRock AS
  */
 
 package org.opends.sdk;
@@ -47,6 +48,20 @@
  */
 public interface SchemaResolver
 {
+  /**
+   * A schema resolver which always returns the current default schema as
+   * returned by {@link Schema#getDefaultSchema()}.
+   */
+  public static final SchemaResolver DEFAULT = new SchemaResolver()
+  {
+
+    public Schema resolveSchema(String dn)
+    {
+      return Schema.getDefaultSchema();
+    }
+  };
+
+
 
   /**
    * Finds the appropriate schema for use with the provided distinguished name.
diff --git a/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/ConnectionFactoryTestCase.java b/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/ConnectionFactoryTestCase.java
index b559513..d170ac5 100644
--- a/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/ConnectionFactoryTestCase.java
+++ b/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/ConnectionFactoryTestCase.java
@@ -23,11 +23,13 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
+ *      Portions copyright 2011 ForgeRock AS
  */
 
 package org.opends.sdk;
 
 
+import static org.fest.assertions.Assertions.assertThat;
 
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
@@ -43,6 +45,9 @@
 import org.opends.sdk.requests.DigestMD5SASLBindRequest;
 import org.opends.sdk.requests.Requests;
 import org.opends.sdk.requests.SearchRequest;
+import org.opends.sdk.responses.SearchResultEntry;
+import org.opends.sdk.schema.Schema;
+import org.opends.sdk.schema.SchemaBuilder;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
@@ -301,8 +306,66 @@
   {
     final Connection con = factory.getConnection();
     assertNotNull(con);
-    // quickly check if iit is a valid connection.
+    // quickly check if it is a valid connection.
     assertTrue(con.readRootDSE().getEntry().getName().isRootDN());
     con.close();
   }
+
+
+
+  /**
+   * Verifies that LDAP connections take into consideration changes to the
+   * default schema post creation. See OPENDJ-159.
+   * <p>
+   * This test is disabled because it cannot be run in parallel with rest of the
+   * test suite, because it modifies the global default schema.
+   *
+   * @throws Exception
+   *           If an unexpected error occurred.
+   */
+  @Test(enabled = false)
+  public void testSchemaUsage() throws Exception
+  {
+    // Create a connection factory: this should always use the default schema,
+    // even if it is updated.
+    final ConnectionFactory factory = new LDAPConnectionFactory(
+        "localhost", TestCaseUtils.getLdapPort());
+    final Schema defaultSchema = Schema.getDefaultSchema();
+
+    final Connection connection = factory.getConnection();
+    try
+    {
+      // Simulate a client which reads the schema from the server and then
+      // sets it as the application default. We then want subsequent
+      // operations to use this schema, not the original default.
+      final SchemaBuilder builder = new SchemaBuilder(
+          Schema.getCoreSchema());
+      builder
+          .addAttributeType(
+              "( 0.9.2342.19200300.100.1.3 NAME 'mail' EQUALITY "
+                  + "caseIgnoreIA5Match SUBSTR caseIgnoreIA5SubstringsMatch "
+                  + "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26{256} )",
+              false);
+      final Schema testSchema = builder.toSchema().nonStrict();
+      assertThat(testSchema.getWarnings()).isEmpty();
+      Schema.setDefaultSchema(testSchema);
+
+      // Read an entry containing the mail attribute.
+      final SearchResultEntry e = connection
+          .readEntry("uid=user.0,ou=people,o=test");
+
+      assertThat(e.getAttribute("mail")).isNotNull();
+      assertThat(
+          e.getAttribute(AttributeDescription.valueOf("mail",
+              testSchema))).isNotNull();
+    }
+    finally
+    {
+      // Restore original schema.
+      Schema.setDefaultSchema(defaultSchema);
+
+      // Close connection.
+      connection.close();
+    }
+  }
 }
diff --git a/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/LDAPServer.java b/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/LDAPServer.java
index d977648..5f7a8f6 100644
--- a/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/LDAPServer.java
+++ b/opendj-sdk/opendj3/opendj-sdk/src/test/java/org/opends/sdk/LDAPServer.java
@@ -23,6 +23,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
+ *      Portions copyright 2011 ForgeRock AS
  */
 
 package org.opends.sdk;
@@ -708,10 +709,13 @@
       final String sn = String.format("sn: %d", i);
       final String uid = String.format("uid: user.%d", i);
 
+      // See org.opends.sdk.ConnectionFactoryTestCase.testSchemaUsage().
+      final String mail = String.format("mail: user.%d@example.com", i);
+
       final DN d = DN.valueOf(dn);
       final Entry e = new LinkedHashMapEntry("dn: " + dn,
           "objectclass: person", "objectclass: inetorgperson",
-          "objectclass: top", cn, sn, uid);
+          "objectclass: top", cn, sn, uid, mail);
       entryMap.put(d, Entries.unmodifiableEntry(e));
     }
   }

--
Gitblit v1.10.0