From 7332968a929e36a87366f8f155eeac7acc04cf55 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 31 May 2016 10:07:04 +0000
Subject: [PATCH] Fix bad test behaviour

---
 opendj-server-legacy/src/test/java/org/opends/server/schema/CollationMatchingRuleTest.java   |  219 ++++-----------
 opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java     |  478 ++++++++++++++++++++++--------------
 opendj-server-legacy/src/main/java/org/opends/server/plugins/ReferentialIntegrityPlugin.java |   76 +---
 3 files changed, 383 insertions(+), 390 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/plugins/ReferentialIntegrityPlugin.java b/opendj-server-legacy/src/main/java/org/opends/server/plugins/ReferentialIntegrityPlugin.java
index 8d473ed..66f3a4a 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/plugins/ReferentialIntegrityPlugin.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/plugins/ReferentialIntegrityPlugin.java
@@ -43,6 +43,7 @@
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.config.server.ConfigChangeResult;
 import org.forgerock.opendj.config.server.ConfigException;
+import org.forgerock.opendj.config.server.ConfigurationChangeListener;
 import org.forgerock.opendj.ldap.AttributeDescription;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.DN;
@@ -50,7 +51,6 @@
 import org.forgerock.opendj.ldap.ResultCode;
 import org.forgerock.opendj.ldap.SearchScope;
 import org.forgerock.opendj.ldap.schema.AttributeType;
-import org.forgerock.opendj.config.server.ConfigurationChangeListener;
 import org.forgerock.opendj.server.config.meta.PluginCfgDefn;
 import org.forgerock.opendj.server.config.meta.ReferentialIntegrityPluginCfgDefn.CheckReferencesScopeCriteria;
 import org.forgerock.opendj.server.config.server.PluginCfg;
@@ -150,15 +150,6 @@
    */
   public static final String DELETE_DNS="deleteDNs";
 
-  /** The buffered reader that is used to read the log file by the background thread. */
-  private BufferedReader reader;
-
-  /**
-   * The buffered writer that is used to write update records in the log
-   * when the plugin is in background processing mode.
-   */
-  private BufferedWriter writer;
-
   /**
    * Specifies the mapping between the attribute type (specified in the
    * attributeTypes list) and the filter which the plugin should use
@@ -741,7 +732,7 @@
 
   /**
    * Sets up the log file that the plugin can write update recored to and
-   * the background thread can use to read update records from. The specifed
+   * the background thread can use to read update records from. The specified
    * log file name is the name to use for the file. If the file exists from
    * a previous run, use it.
    *
@@ -770,24 +761,12 @@
   }
 
   /**
-   * Sets up a buffered writer that the plugin can use to write update records
-   * with.
+   * Returns a buffered writer that the plugin can use to write update records with.
    *
    * @throws IOException If a new file writer cannot be created.
    */
-  private void setupWriter() throws IOException {
-    writer=new BufferedWriter(new FileWriter(logFile, true));
-  }
-
-
-  /**
-   * Sets up a buffered reader that the background thread can use to read
-   * update records with.
-   *
-   * @throws IOException If a new file reader cannot be created.
-   */
-  private void setupReader() throws IOException {
-    reader=new BufferedReader(new FileReader(logFile));
+  private BufferedWriter setupWriter() throws IOException {
+    return new BufferedWriter(new FileWriter(logFile, true));
   }
 
   /**
@@ -802,16 +781,13 @@
   private void writeLog(Map<DN,DN> modDNmap) {
     synchronized(logFile)
     {
-      try
+      try (BufferedWriter writer = setupWriter())
       {
-        setupWriter();
         for(Map.Entry<DN,DN> mapEntry : modDNmap.entrySet())
         {
           writer.write(mapEntry.getKey() + "\t" + mapEntry.getValue());
           writer.newLine();
         }
-        writer.flush();
-        writer.close();
       }
       catch (IOException io)
       {
@@ -829,16 +805,13 @@
   private void writeLog(Set<DN> deleteDNset) {
     synchronized(logFile)
     {
-      try
+      try (BufferedWriter writer = setupWriter())
       {
-        setupWriter();
         for (DN deletedEntryDN : deleteDNset)
         {
           writer.write(deletedEntryDN.toString());
           writer.newLine();
         }
-        writer.flush();
-        writer.close();
       }
       catch (IOException io)
       {
@@ -864,26 +837,27 @@
           return;
         }
 
-        setupReader();
-        String line;
-        while((line=reader.readLine()) != null) {
-          try {
-            String[] a=line.split("[\t]");
-            DN origDn = DN.valueOf(a[0]);
-            //If there is only a single DN string than it must be a delete.
-            if(a.length == 1) {
-              processDelete(Collections.singleton(origDn), false);
-            } else {
-              DN movedDN=DN.valueOf(a[1]);
-              processModifyDN(origDn, movedDN);
+        try (BufferedReader reader = new BufferedReader(new FileReader(logFile)))
+        {
+          String line;
+          while((line=reader.readLine()) != null) {
+            try {
+              String[] a=line.split("[\t]");
+              DN origDn = DN.valueOf(a[0]);
+              //If there is only a single DN string than it must be a delete.
+              if(a.length == 1) {
+                processDelete(Collections.singleton(origDn), false);
+              } else {
+                DN movedDN=DN.valueOf(a[1]);
+                processModifyDN(origDn, movedDN);
+              }
+            } catch (LocalizedIllegalArgumentException e) {
+              //This exception should rarely happen since the plugin wrote the DN
+              //strings originally.
+              logger.error(ERR_PLUGIN_REFERENT_CANNOT_DECODE_STRING_AS_DN, e.getMessage());
             }
-          } catch (LocalizedIllegalArgumentException e) {
-            //This exception should rarely happen since the plugin wrote the DN
-            //strings originally.
-            logger.error(ERR_PLUGIN_REFERENT_CANNOT_DECODE_STRING_AS_DN, e.getMessage());
           }
         }
-        reader.close();
         logFile.delete();
         logFile.createNewFile();
       } catch (IOException io) {
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
index b60489f..4f5483c 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
@@ -30,6 +30,7 @@
 import java.io.File;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
@@ -67,6 +68,7 @@
 import org.opends.server.types.SearchFilter;
 import org.opends.server.util.CollectionUtils;
 import org.opends.server.util.ServerConstants;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -87,6 +89,25 @@
     assertNotNull(schemaBackend);
   }
 
+  @AfterClass
+  public void removeFilesCreatedInTests()
+  {
+    final File schemaDir = DirectoryServer.getEnvironmentConfig().getSchemaDirectory();
+
+    final List<String> filesCreatedInTests = Arrays.asList(
+        "98-schema-test-attrtype.ldif",
+        "98-schema-test-replaceattrtype.ldif",
+        "98-schema-test-oc.ldif",
+        "98-schema-test-nameform.ldif",
+        "98-schema-test-dcr.ldif",
+        "98-schema-test-dsr.ldif",
+        "98-schema-test-mru.ldif");
+    for (String fileCreatedInTests : filesCreatedInTests)
+    {
+      new File(schemaDir, fileCreatedInTests).delete();
+    }
+  }
+
   /**
    * Tests the {@code initializeBackend} method by providing a null
    * configuration entry.
@@ -827,23 +848,30 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddAttributeTypeObsoleteEMR()
-         throws Exception
+  public void testAddAttributeTypeObsoleteEMR() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testAddATObsoleteEMRMatch", "1.3.6.1.4.1.26027.1.999.20", true);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: attributeTypes",
-         "attributeTypes: ( testaddatobsoleteemr-oid " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: attributeTypes",
+          "attributeTypes: ( testaddatobsoleteemr-oid " +
               "NAME 'testAddATObsoleteEMR' " +
               "EQUALITY testAddATObsoleteEMRMatch " +
               "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE " +
-              "X-ORGIN 'SchemaBackendTestCase' )");
+          "X-ORGIN 'SchemaBackendTestCase' )");
 
-    runModify(argsNotPermissive(), ldif, System.err, CONSTRAINT_VIOLATION);
+      runModify(argsNotPermissive(), ldif, System.err, CONSTRAINT_VIOLATION);
+    }
+    finally
+    {
+      deregisterAttributeType("testaddatobsoleteemr-oid");
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
@@ -1163,47 +1191,71 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testRemoveAttributeTypeReferencedByMRU()
-         throws Exception
+  public void testRemoveAttributeTypeReferencedByMRU() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testRemoveATRefByMRUMatch", "1.3.6.1.4.1.26027.1.999.17", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: attributeTypes",
-         "attributeTypes: ( testremoveatrefbymruat-oid " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: attributeTypes",
+          "attributeTypes: ( testremoveatrefbymruat-oid " +
               "NAME 'testRemoveATRefByMRUAT' " +
               "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE " +
               "X-ORIGIN 'SchemaBackendTestCase' )",
-         "-",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.17 " +
+          "-",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.17 " +
               "NAME 'testRemoveATRefByMRUMRU' APPLIES testRemoveATRefByMRUAT " +
               "X-ORIGIN 'SchemaBackendTestCase' )",
-         "",
-         "dn: cn=schema",
-         "changetype: modify",
-         "delete: attributeTypes",
-         "attributeTypes: ( testremoveatrefbymruat-oid " +
+              "",
+          "dn: cn=schema",
+          "changetype: modify",
+          "delete: attributeTypes",
+          "attributeTypes: ( testremoveatrefbymruat-oid " +
               "NAME 'testRemoveATRefByMRUAT' " +
               "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    String attrName = "testremoveatrefbymruat";
-    assertFalse(DirectoryServer.getSchema().hasAttributeType(attrName));
+      String attrName = "testremoveatrefbymruat";
+      assertFalse(DirectoryServer.getSchema().hasAttributeType(attrName));
 
-    runModify(argsNotPermissive(), ldif, UNWILLING_TO_PERFORM);
+      runModify(argsNotPermissive(), ldif, UNWILLING_TO_PERFORM);
 
-    MatchingRuleUse mru =
-         DirectoryServer.getSchema().getMatchingRuleUse(matchingRule);
-    assertNotNull(mru);
-    assertTrue(mru.hasName("testremoveatrefbymrumru"));
+      assertMatchingRuleUseExistsWithName(matchingRule, "testremoveatrefbymrumru");
 
-    assertTrue(DirectoryServer.getSchema().hasAttributeType(attrName));
+      assertTrue(DirectoryServer.getSchema().hasAttributeType(attrName));
+    }
+    finally
+    {
+      DirectoryServer.getSchema();
+      deregisterMatchingRuleUse(matchingRule);
+      deregisterAttributeType("testremoveatrefbymruat-oid");
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
+  private void deregisterAttributeType(String nameOrOid) throws DirectoryException
+  {
+    org.opends.server.types.Schema schema = DirectoryServer.getSchema();
+    schema.deregisterAttributeType(schema.getAttributeType(nameOrOid));
+  }
+
+  private void deregisterMatchingRuleUse(MatchingRule matchingRule) throws DirectoryException
+  {
+    org.opends.server.types.Schema schema = DirectoryServer.getSchema();
+    schema.deregisterMatchingRuleUse(schema.getMatchingRuleUse(matchingRule));
+  }
+
+  private void deregisterMatchingRule(MatchingRule matchingRule) throws DirectoryException
+  {
+    DirectoryServer.getSchema().deregisterMatchingRule(matchingRule);
+  }
+
+
   /**
    * Tests the behavior of the schema backend when attempting to add a new
    * objectclass that doesn't already exist, that has a valid superior class,
@@ -3587,28 +3639,32 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddMatchingRuleUseSuccessful()
-         throws Exception
+  public void testAddMatchingRuleUseSuccessful() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testAddMRUSuccessfulMatch", "1.3.6.1.4.1.26027.1.999.10", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.10 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.10 " +
               "NAME 'testAddMRUSuccessful' APPLIES cn " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
+      runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
 
-    MatchingRuleUse mru =
-         DirectoryServer.getSchema().getMatchingRuleUse(matchingRule);
-    assertNotNull(mru);
-    assertTrue(mru.hasName("testaddmrusuccessful"));
+      assertMatchingRuleUseExistsWithName(matchingRule, "testaddmrusuccessful");
+    }
+    finally
+    {
+      deregisterMatchingRuleUse(matchingRule);
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
@@ -3618,35 +3674,38 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddMatchingRuleUseToAltSchemaFile()
-         throws Exception
+  public void testAddMatchingRuleUseToAltSchemaFile() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testAddMRUToAltSchemaFileMatch", "1.3.6.1.4.1.26027.1.999.18", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.18 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.18 " +
               "NAME 'testAddMRUToAltSchemaFile' APPLIES cn " +
               "X-SCHEMA-FILE '98-schema-test-mru.ldif' " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    File schemaFile = new File(SchemaConfigManager.getSchemaDirectoryPath(),
-                               "98-schema-test-mru.ldif");
-    assertFalse(schemaFile.exists());
+      File schemaFile = new File(SchemaConfigManager.getSchemaDirectoryPath(), "98-schema-test-mru.ldif");
+      assertFalse(schemaFile.exists());
 
-    runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
+      runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
 
-    MatchingRuleUse mru =
-         DirectoryServer.getSchema().getMatchingRuleUse(matchingRule);
-    assertNotNull(mru);
-    assertTrue(mru.hasName("testaddmrutoaltschemafile"));
+      assertMatchingRuleUseExistsWithName(matchingRule, "testaddmrutoaltschemafile");
 
-    assertTrue(schemaFile.exists());
+      assertTrue(schemaFile.exists());
+    }
+    finally
+    {
+      deregisterMatchingRuleUse(matchingRule);
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
@@ -3656,34 +3715,39 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testReplaceMatchingRuleUseSuccessful()
-         throws Exception
+  public void testReplaceMatchingRuleUseSuccessful() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testReplaceMRUSuccessfulMatch", "1.3.6.1.4.1.26027.1.999.11", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.11 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.11 " +
               "NAME 'testReplaceMRUSuccessful' APPLIES cn " +
               "X-ORIGIN 'SchemaBackendTestCase' )",
-         "",
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.11 " +
+          "",
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.11 " +
               "NAME 'testReplaceMRUSuccessful' APPLIES ( cn $ sn ) " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsPermissive(), ldif, System.err, SUCCESS);
+      runModify(argsPermissive(), ldif, System.err, SUCCESS);
 
-    MatchingRuleUse mru =         DirectoryServer.getSchema().getMatchingRuleUse(matchingRule);
-    assertNotNull(mru);
-    assertTrue(mru.hasName("testreplacemrusuccessful"));
+      assertMatchingRuleUseExistsWithName(matchingRule, "testreplacemrusuccessful");
+    }
+    finally
+    {
+      deregisterMatchingRuleUse(matchingRule);
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
@@ -3693,40 +3757,44 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testRemoveAndAddMatchingRuleUse()
-         throws Exception
+  public void testRemoveAndAddMatchingRuleUse() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testRemoveAndAddMRUMatch", "1.3.6.1.4.1.26027.1.999.12", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.12 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.12 " +
               "NAME 'testRemoveAndAddMRU' APPLIES cn " +
               "X-ORIGIN 'SchemaBackendTestCase' )",
-         "",
-         "dn: cn=schema",
-         "changetype: modify",
-         "delete: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.12 " +
+          "",
+          "dn: cn=schema",
+          "changetype: modify",
+          "delete: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.12 " +
               "NAME 'testRemoveAndAddMRU' APPLIES cn " +
               "X-ORIGIN 'SchemaBackendTestCase' )",
-         "-",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.12 " +
+          "-",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.12 " +
               "NAME 'testRemoveAndAddMRU' APPLIES ( cn $ sn ) " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
+      runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
 
-    MatchingRuleUse mru =
-         DirectoryServer.getSchema().getMatchingRuleUse(matchingRule);
-    assertNotNull(mru);
-    assertTrue(mru.hasName("testremoveandaddmru"));
+      assertMatchingRuleUseExistsWithName(matchingRule, "testremoveandaddmru");
+    }
+    finally
+    {
+      deregisterMatchingRuleUse(matchingRule);
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
@@ -3737,35 +3805,46 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddMatchingRuleUseMRConflict()
-         throws Exception
+  public void testAddMatchingRuleUseMRConflict() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testAddMRUMRConflictMatch", "1.3.6.1.4.1.26027.1.999.14", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.14 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.14 " +
               "NAME 'testAddMRUMRConflict' APPLIES cn " +
               "X-ORIGIN 'SchemaBackendTestCase' )",
-         "",
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.14 " +
+          "",
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.14 " +
               "NAME 'testAddMRUMRConflict2' APPLIES sn " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsNotPermissive(), ldif, ATTRIBUTE_OR_VALUE_EXISTS);
+      runModify(argsNotPermissive(), ldif, ATTRIBUTE_OR_VALUE_EXISTS);
 
-    MatchingRuleUse mru =
-         DirectoryServer.getSchema().getMatchingRuleUse(matchingRule);
+      assertMatchingRuleUseExistsWithName(matchingRule, "testaddmrumrconflict");
+    }
+    finally
+    {
+      deregisterMatchingRuleUse(matchingRule);
+      deregisterMatchingRule(matchingRule);
+    }
+  }
+
+  private void assertMatchingRuleUseExistsWithName(MatchingRule matchingRule, String mruName)
+  {
+    MatchingRuleUse mru = DirectoryServer.getSchema().getMatchingRuleUse(matchingRule);
     assertNotNull(mru);
-    assertTrue(mru.hasName("testaddmrumrconflict"));
+    assertTrue(mru.hasName(mruName));
   }
 
   /**
@@ -3796,24 +3875,30 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddMatchingRuleUseAttributeTypeUndefined()
-         throws Exception
+  public void testAddMatchingRuleUseAttributeTypeUndefined() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testAddMRUATUndefinedMatch", "1.3.6.1.4.1.26027.1.999.16", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.16 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.16 " +
               "NAME 'testAddMatchingRuleUseATUndefined' " +
               "APPLIES xxxundefinedxxx " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+      runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+    }
+    finally
+    {
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
@@ -3823,25 +3908,31 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddMatchingRuleUseAttributeTypeMultipleUndefined()
-         throws Exception
+  public void testAddMatchingRuleUseAttributeTypeMultipleUndefined() throws Exception
   {
     MatchingRule matchingRule =
         getMatchingRule("testAddMRUATMultipleUndefinedMatch", "1.3.6.1.4.1.26027.1.999.19", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.19 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.19 " +
               "NAME 'testAddMatchingRuleUseATMultipleUndefined' " +
               "APPLIES ( cn $ xxxundefinedxxx ) " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+      runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+    }
+    finally
+    {
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   private void assertSchemaDoesNotHaveMatchingRuleUse(MatchingRule matchingRule)
@@ -3859,23 +3950,29 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddMatchingRuleUseObsoleteMatchingRule()
-         throws Exception
+  public void testAddMatchingRuleUseObsoleteMatchingRule() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testAddMRUObsoleteMRMatch", "1.3.6.1.4.1.26027.1.999.21", true);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.21 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.21 " +
               "NAME 'testAddMatchingRuleUseObsoleteMatchingRule' " +
-              "APPLIES cn X-ORIGIN 'SchemaBackendTestCase' )");
+          "APPLIES cn X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+      runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+    }
+    finally
+    {
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
@@ -3885,28 +3982,35 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testAddMatchingRuleUseObsoleteAttributeType()
-         throws Exception
+  public void testAddMatchingRuleUseObsoleteAttributeType() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testAddMRUObsoleteATMatch", "1.3.6.1.4.1.26027.1.999.22", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: attributeTypes",
-         "attributeTypes: ( testaddmruobsoleteat-oid " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: attributeTypes",
+          "attributeTypes: ( testaddmruobsoleteat-oid " +
               "NAME 'testAddMRUObsoleteAT' OBSOLETE )",
-         "-",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.22 " +
+          "-",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.22 " +
               "NAME 'testAddMatchingRuleUseObsoleteAttributeType' " +
               "APPLIES testAddMRUObsoleteAT " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
 
-    runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+      runModify(argsNotPermissive(), ldif, CONSTRAINT_VIOLATION);
+    }
+    finally
+    {
+      deregisterAttributeType("testaddmruobsoleteat-oid");
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   private void runModify(String[] args, String ldifContent, ResultCode expectedRC)
@@ -3947,30 +4051,36 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testRemoveMatchingRuleUseSuccessful()
-         throws Exception
+  public void testRemoveMatchingRuleUseSuccessful() throws Exception
   {
     MatchingRule matchingRule = getMatchingRule("testRemoveMRUSuccessfulMatch", "1.3.6.1.4.1.26027.1.999.13", false);
-    DirectoryServer.registerMatchingRule(matchingRule, false);
+    DirectoryServer.getSchema().registerMatchingRule(matchingRule, false);
 
-    String ldif = toLdif(
-         "dn: cn=schema",
-         "changetype: modify",
-         "add: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.13 " +
+    try
+    {
+      String ldif = toLdif(
+          "dn: cn=schema",
+          "changetype: modify",
+          "add: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.13 " +
               "NAME 'testRemoveMRUSuccessful' APPLIES cn " +
               "X-ORIGIN 'SchemaBackendTestCase' )",
-         "",
-         "dn: cn=schema",
-         "changetype: modify",
-         "delete: matchingRuleUse",
-         "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.13 " +
+          "",
+          "dn: cn=schema",
+          "changetype: modify",
+          "delete: matchingRuleUse",
+          "matchingRuleUse: ( 1.3.6.1.4.1.26027.1.999.13 " +
               "NAME 'testRemoveMRUSuccessful' APPLIES cn " +
               "X-ORIGIN 'SchemaBackendTestCase' )");
 
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
-    runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
-    assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+      runModify(argsNotPermissive(), ldif, System.err, SUCCESS);
+      assertSchemaDoesNotHaveMatchingRuleUse(matchingRule);
+    }
+    finally
+    {
+      deregisterMatchingRule(matchingRule);
+    }
   }
 
   /**
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/schema/CollationMatchingRuleTest.java b/opendj-server-legacy/src/test/java/org/opends/server/schema/CollationMatchingRuleTest.java
index 24e0648..38548d9 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/schema/CollationMatchingRuleTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/schema/CollationMatchingRuleTest.java
@@ -17,10 +17,11 @@
 package org.opends.server.schema;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
+import org.forgerock.opendj.ldap.DN;
 import org.forgerock.opendj.ldap.ResultCode;
-import org.forgerock.opendj.ldap.SearchScope;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.controls.ServerSideSortRequestControl;
 import org.opends.server.controls.VLVRequestControl;
@@ -28,27 +29,27 @@
 import org.opends.server.protocols.internal.SearchRequest;
 import org.opends.server.tools.LDAPModify;
 import org.opends.server.types.Control;
-import org.forgerock.opendj.ldap.DN;
 import org.opends.server.types.Entry;
+import org.opends.server.types.LDAPException;
 import org.opends.server.types.SearchResultEntry;
+import org.opends.server.util.CollectionUtils;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import static java.util.Arrays.*;
+
+import static org.forgerock.opendj.ldap.SearchScope.*;
 import static org.opends.server.protocols.internal.InternalClientConnection.*;
 import static org.opends.server.protocols.internal.Requests.*;
 import static org.testng.Assert.*;
 
-/**
- * Integration tests for collation matching rules.
- */
+/** Integration tests for collation matching rules. */
 @SuppressWarnings("javadoc")
 public final class CollationMatchingRuleTest
         extends SchemaTestCase
 {
-
   /**
-   * Ensures that the Directory Server is running before executing the
-   * testcases.
+   * Ensures that the Directory Server is running before executing the testcases.
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
@@ -64,7 +65,6 @@
     user4 = DN.valueOf("cn=user4,dc=example,dc=com");
   }
 
-
   /**
    * Test to verify an ADD of an entry containing international characters.
    * @throws  Exception  If an unexpected problem occurs.
@@ -96,148 +96,83 @@
     assertEquals(err,0);
   }
 
-
-
-  /**
-   * Test to search the collation equality matching rule using OID.
-   */
-  @Test(dependsOnMethods = {"org.opends.server.schema."+
-                "CollationMatchingRuleTest.addEntry"})
+  /** Test to search the collation equality matching rule using OID. */
+  @Test(dependsOnMethods = "addEntry")
   public void searchCollationEqualityUsingOID() throws Exception
   {
     //Search the collation rule with OID of en and no suffix in the filter.
     SearchRequest request =
-        newSearchRequest("uid=user,o=test", SearchScope.WHOLE_SUBTREE, "cn:1.3.6.1.4.1.42.2.27.9.4.34.1:=sanchez");
-    InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
-    assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
-    List<SearchResultEntry> entries = searchOperation.getSearchEntries();
-    SearchResultEntry e = entries.get(0);
-    //An entry must be returned.
-    assertNotNull(e);
+        newSearchRequest("uid=user,o=test", WHOLE_SUBTREE, "cn:1.3.6.1.4.1.42.2.27.9.4.34.1:=sanchez");
+    searchCollationAtLeastOneEntryReturned(request);
   }
 
-
-
-  /**
-   * Test to search the collation equality matching rule using language Tag.
-   */
-  @Test(dependsOnMethods = {"org.opends.server.schema."+
-                "CollationMatchingRuleTest.addEntry"})
+  /** Test to search the collation equality matching rule using language Tag. */
+  @Test(dependsOnMethods = "addEntry")
   public void searchCollationEqualityUsingLanguageTag() throws Exception
   {
     //Search the collation rule with language tag of en and no suffix
     //in the filter.
-    SearchRequest request = newSearchRequest("uid=user,o=test", SearchScope.WHOLE_SUBTREE, "cn:en:=sanchez");
-    InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
-    assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
-    List<SearchResultEntry> entries = searchOperation.getSearchEntries();
-    SearchResultEntry e = entries.get(0);
-    //An entry must be returned.
-    assertNotNull(e);
+    SearchRequest request = newSearchRequest("uid=user,o=test", WHOLE_SUBTREE, "cn:en:=sanchez");
+    searchCollationAtLeastOneEntryReturned(request);
   }
 
-
-  /**
-   * Test to search the collation Less than matching rule using OID and suffix.
-   */
-  @Test(dependsOnMethods = {"org.opends.server.schema."+
-                "CollationMatchingRuleTest.addEntry"})
+  /** Test to search the collation Less than matching rule using OID and suffix. */
+  @Test(dependsOnMethods = "addEntry")
   public void searchCollationLTUsingOIDSuffix() throws Exception
   {
     //Search the collation rule with OID of es and suffix in the filter.
     String filter = "departmentnumber:1.3.6.1.4.1.42.2.27.9.4.49.1.1:=abc120";
-    SearchRequest request = newSearchRequest("uid=user,o=test", SearchScope.WHOLE_SUBTREE, filter);
-    InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
-    assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
-    List<SearchResultEntry> entries = searchOperation.getSearchEntries();
-    SearchResultEntry e = entries.get(0);
-    //An entry must be returned.
-    assertNotNull(e);
+    SearchRequest request = newSearchRequest("uid=user,o=test", WHOLE_SUBTREE, filter);
+    searchCollationAtLeastOneEntryReturned(request);
   }
 
-
-
   /**
-   * Test to search the collation Less than Equal to matching rule using
-   * Language Tag and suffix.
+   * Test to search the collation Less than Equal to matching rule using Language Tag and suffix.
    */
-  @Test(dependsOnMethods = {"org.opends.server.schema."+
-                "CollationMatchingRuleTest.addEntry"})
+  @Test(dependsOnMethods = "addEntry")
   public void searchCollationLTEUsingLanguageSuffix() throws Exception
   {
     //Search the collation rule with tag of fr and suffix in the filter.
-    SearchRequest request = newSearchRequest("uid=user,o=test", SearchScope.WHOLE_SUBTREE, "carLicense:fr.2:=ebe2");
-    InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
-    assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
-    List<SearchResultEntry> entries = searchOperation.getSearchEntries();
-    SearchResultEntry e = entries.get(0);
-    //An entry must be returned.
-    assertNotNull(e);
+    SearchRequest request = newSearchRequest("uid=user,o=test", WHOLE_SUBTREE, "carLicense:fr.2:=ebe2");
+    searchCollationAtLeastOneEntryReturned(request);
   }
 
-
-
-  /**
-   * Test to search the collation Greater than matching rule using language
-   * tag.
-   */
-  @Test(dependsOnMethods = {"org.opends.server.schema."+
-                "CollationMatchingRuleTest.addEntry"})
+  /** Test to search the collation Greater than matching rule using language tag. */
+  @Test(dependsOnMethods = "addEntry")
   public void searchCollationGTUsingLanguage() throws Exception
   {
     //Search the collation rule with tag of fr in the filter.
-    SearchRequest request = newSearchRequest("uid=user,o=test", SearchScope.WHOLE_SUBTREE, "carLicense:fr.5:=ebe1");
-    InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
-    assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
-    List<SearchResultEntry> entries = searchOperation.getSearchEntries();
-    SearchResultEntry e = entries.get(0);
-    //An entry must be returned.
-    assertNotNull(e);
+    SearchRequest request = newSearchRequest("uid=user,o=test", WHOLE_SUBTREE, "carLicense:fr.5:=ebe1");
+    searchCollationAtLeastOneEntryReturned(request);
   }
 
-
-
-  /**
-   * Test to search the collation Greater than Equal to matching rule using
-   * language tag.
-   */
-  @Test(dependsOnMethods = {"org.opends.server.schema."+
-                "CollationMatchingRuleTest.addEntry"})
+  /** Test to search the collation Greater than Equal to matching rule using language tag. */
+  @Test(dependsOnMethods = "addEntry")
   public void searchCollationGTEUsingLanguage() throws Exception
   {
     //Search the collation rule with tag of es and suffix in the filter.
-    SearchRequest request =
-        newSearchRequest("uid=user,o=test", SearchScope.WHOLE_SUBTREE, "departmentnumber:es.4:=abc111");
-    InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
-    assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
-    List<SearchResultEntry> entries = searchOperation.getSearchEntries();
-    SearchResultEntry e = entries.get(0);
-    //An entry must be returned.
-    assertNotNull(e);
+    SearchRequest request = newSearchRequest("uid=user,o=test", WHOLE_SUBTREE, "departmentnumber:es.4:=abc111");
+    searchCollationAtLeastOneEntryReturned(request);
   }
 
-
-
-  /**
-   * Test to search the collation substring matching rule using
-   * language tag and suffix.
-   */
-  @Test(dependsOnMethods = {"org.opends.server.schema."+
-                "CollationMatchingRuleTest.addEntry"})
+  /** Test to search the collation substring matching rule using language tag and suffix. */
+  @Test(dependsOnMethods = "addEntry")
   public void searchCollationSubstring() throws Exception
   {
     /*Search the collation rule with tag of en and suffix in the filter.
      *It searches for string quebec against the value of sn which is
      * Qu\u00e9bec.
      */
-    SearchRequest request = newSearchRequest("uid=user,o=test", SearchScope.WHOLE_SUBTREE, "sn:en.6:=*u*bec");
+    SearchRequest request = newSearchRequest("uid=user,o=test", WHOLE_SUBTREE, "sn:en.6:=*u*bec");
+    searchCollationAtLeastOneEntryReturned(request);
+  }
+
+  private void searchCollationAtLeastOneEntryReturned(SearchRequest request)
+  {
     InternalSearchOperation searchOperation = getRootConnection().processSearch(request);
-    searchOperation.run();
     assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
     List<SearchResultEntry> entries = searchOperation.getSearchEntries();
-    SearchResultEntry e = entries.get(0);
-    //An entry must be returned for sn=quebec.
-    assertNotNull(e);
+    assertNotNull(entries.get(0));
   }
 
   private DN user1;
@@ -245,7 +180,6 @@
   private DN user3;
   private DN user4;
 
-
   /**
    * Test to verify the Sort control works well with the Collation
    * Less-than-equal-to matching rule and French Locale.
@@ -253,17 +187,11 @@
   @Test
   public void testSortControlLTERule() throws Exception
   {
-    ArrayList<DN> expectedDNOrder = new ArrayList<>();
-    expectedDNOrder.add(user4);
-    expectedDNOrder.add(user3);
-    expectedDNOrder.add(user2);
-    expectedDNOrder.add(user1);
-    ArrayList<Control> requestControls = new ArrayList<>();
-    requestControls.add(new ServerSideSortRequestControl("displayname:fr"));
-    ValidateSortControl("displayname:fr-FR.6:=A*", expectedDNOrder, requestControls);
+    List<DN> expectedDNOrder = asList(user4, user3, user2, user1);
+    List<Control> requestControls = serverSideSortControl("displayname:fr");
+    validateSortControl("displayname:fr-FR.6:=A*", expectedDNOrder, requestControls);
   }
 
-
   /**
    * Test to verify the Sort control works with Collation equality
    * matching rule and Spanish locale.
@@ -271,17 +199,11 @@
   @Test
   public void testSortControlEQRule() throws Exception
   {
-    ArrayList<DN> expectedDNOrder = new ArrayList<>();
-    expectedDNOrder.add(user4);
-    expectedDNOrder.add(user3);
-    expectedDNOrder.add(user2);
-    expectedDNOrder.add(user1);
-    ArrayList<Control> requestControls = new ArrayList<>();
-    requestControls.add(new ServerSideSortRequestControl("displayname:es"));
-    ValidateSortControl("displayname:es.6:=A*", expectedDNOrder, requestControls);
+    List<DN> expectedDNOrder = asList(user4, user3, user2, user1);
+    List<Control> requestControls = serverSideSortControl("displayname:es");
+    validateSortControl("displayname:es.6:=A*", expectedDNOrder, requestControls);
   }
 
-
   /**
    * Test to verify the Sort control works with Collation greater
    * than matching rule and English locale in a descending order.
@@ -289,47 +211,35 @@
   @Test
   public void testSortControlGTRule() throws Exception
   {
-    ArrayList<DN> expectedDNOrder = new ArrayList<>();
-    expectedDNOrder.add(user1);
-    expectedDNOrder.add(user2);
-    expectedDNOrder.add(user3);
-    expectedDNOrder.add(user4);
-    ArrayList<Control> requestControls = new ArrayList<>();
-    requestControls.add(new ServerSideSortRequestControl("-displayname:en"));
-    ValidateSortControl("displayname:en-US.6:=A*", expectedDNOrder, requestControls);
+    List<DN> expectedDNOrder = asList(user1, user2, user3, user4);
+    List<Control> requestControls = serverSideSortControl("-displayname:en");
+    validateSortControl("displayname:en-US.6:=A*", expectedDNOrder, requestControls);
   }
 
-
-
-  /**
-   * Tests the Sort control with the VLV control using a collation equality
-   * matching rule.
-   */
+  /** Tests the Sort control with the VLV control using a collation equality matching rule. */
   @Test
   public void testVLVSortControl() throws Exception
   {
-    ArrayList<DN> expectedDNOrder = new ArrayList<>();
-    expectedDNOrder.add(user4);
-    expectedDNOrder.add(user3);
-    expectedDNOrder.add(user2);
-    expectedDNOrder.add(user1);
-    ArrayList<Control> requestControls = new ArrayList<>();
-    requestControls.add(new ServerSideSortRequestControl("displayname:fr"));
-    requestControls.add(new VLVRequestControl(0, 4, 1, 0));
-    ValidateSortControl("objectclass=inetOrgPerson", expectedDNOrder, requestControls);
+    List<DN> expectedDNOrder = asList(user4, user3, user2, user1);
+    List<Control> requestControls = CollectionUtils.<Control> newArrayList(
+        new ServerSideSortRequestControl("displayname:fr"),
+        new VLVRequestControl(0, 4, 1, 0));
+    validateSortControl("objectclass=inetOrgPerson", expectedDNOrder, requestControls);
   }
 
+  private List<Control> serverSideSortControl(String sortOrderString) throws LDAPException
+  {
+    return Arrays.<Control> asList(new ServerSideSortRequestControl(sortOrderString));
+  }
 
-
-  private void ValidateSortControl(String searchFilter,
-          ArrayList<DN> expectedDNOrder,
-          ArrayList<Control> requestControls) throws Exception
+  private void validateSortControl(String searchFilter, List<DN> expectedDNOrder, List<Control> requestControls)
+      throws Exception
   {
     try
     {
       populateEntriesForControl();
 
-      SearchRequest request = newSearchRequest(DN.valueOf("dc=example,dc=com"), SearchScope.WHOLE_SUBTREE, searchFilter)
+      SearchRequest request = newSearchRequest(DN.valueOf("dc=example,dc=com"), WHOLE_SUBTREE, searchFilter)
           .addControl(requestControls);
       InternalSearchOperation internalSearch = getRootConnection().processSearch(request);
       assertEquals(internalSearch.getResultCode(), ResultCode.SUCCESS);
@@ -351,7 +261,6 @@
     return results;
   }
 
-
   private void populateEntriesForControl() throws Exception
   {
     TestCaseUtils.clearBackend("userRoot", "dc=example,dc=com");

--
Gitblit v1.10.0