From 249f365384d6e9927fc8cd3e161a789bbaf0ed47 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 13 Dec 2013 22:40:35 +0000
Subject: [PATCH] Backport fix for OPENDJ-1252: ReadSchema example throws NPE

---
 opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java |   34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)

diff --git a/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java b/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
index add66e4..b50e04c 100644
--- a/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
+++ b/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
@@ -35,7 +35,9 @@
 import java.util.Set;
 
 import org.forgerock.i18n.LocalizedIllegalArgumentException;
+import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.DecodeException;
 import org.forgerock.opendj.ldap.Entry;
 import org.forgerock.opendj.ldap.EntryNotFoundException;
 import org.forgerock.opendj.ldap.LinkedHashMapEntry;
@@ -43,6 +45,7 @@
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+
 import org.testng.annotations.Test;
 import org.forgerock.opendj.ldap.Connection;
 import org.forgerock.opendj.ldap.requests.SearchRequest;
@@ -2000,9 +2003,38 @@
         final Schema schema =
                 new SchemaBuilder(Schema.getCoreSchema()).defaultMatchingRule(
                         CoreSchema.getCaseIgnoreMatchingRule()).toSchema().asNonStrictSchema();
-        assertThat(schema.getDefaultMatchingRule()).isEqualTo(CoreSchema.getCaseIgnoreMatchingRule());
+        assertThat(schema.getDefaultMatchingRule()).isEqualTo(
+                CoreSchema.getCaseIgnoreMatchingRule());
         assertThat(schema.getAttributeType("dummy").getEqualityMatchingRule()).isEqualTo(
                 CoreSchema.getCaseIgnoreMatchingRule());
     }
 
+    @Test
+    public void testDefaultSyntaxDefinedInSchema() {
+        // The next line was triggering a NPE with OPENDJ-1252.
+        final Schema schema =
+                new SchemaBuilder().addSyntax("( 9.9.9 DESC 'Test Syntax' )", false).addSyntax(
+                        CoreSchema.getOctetStringSyntax().toString(), false).toSchema();
+
+        // Ensure that the substituted syntax is usable.
+        assertThat(schema.getSyntax("9.9.9").valueIsAcceptable(ByteString.valueOf("test"), null))
+                .isTrue();
+    }
+
+    @Test
+    public void testDefaultMatchingRuleDefinedInSchema() throws DecodeException {
+        final Schema schema =
+                new SchemaBuilder().addSyntax(CoreSchema.getOctetStringSyntax().toString(), false)
+                        .addMatchingRule(
+                                "( 9.9.9 NAME 'testRule' SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
+                                false).addMatchingRule(
+                                CoreSchema.getOctetStringMatchingRule().toString(), false)
+                        .toSchema();
+
+        // Ensure that the substituted rule is usable: was triggering a NPE with OPENDJ-1252.
+        assertThat(
+                schema.getMatchingRule("9.9.9").normalizeAttributeValue(ByteString.valueOf("test")))
+                .isEqualTo(ByteString.valueOf("test"));
+    }
+
 }

--
Gitblit v1.10.0