From cc08b5573ab5fb3b88ef64a48d3edeae475b852a Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Tue, 04 Sep 2007 19:27:20 +0000
Subject: [PATCH] Move the DNBuilder implementation into the core ManagedObjectPath class and add the following methods:

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/admin/ManagedObjectPathTest.java |   84 ++++++++++++++
 opends/src/server/org/opends/server/admin/server/DNBuilder.java                              |  116 ++-----------------
 opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java  |   12 -
 opends/src/server/org/opends/server/admin/ManagedObjectPath.java                             |  130 +++++++++++++++++++++
 4 files changed, 230 insertions(+), 112 deletions(-)

diff --git a/opends/src/server/org/opends/server/admin/ManagedObjectPath.java b/opends/src/server/org/opends/server/admin/ManagedObjectPath.java
index d37816c..6f788e8 100644
--- a/opends/src/server/org/opends/server/admin/ManagedObjectPath.java
+++ b/opends/src/server/org/opends/server/admin/ManagedObjectPath.java
@@ -38,6 +38,12 @@
 import org.opends.server.admin.std.client.RootCfgClient;
 import org.opends.server.admin.std.meta.RootCfgDefn;
 import org.opends.server.admin.std.server.RootCfg;
+import org.opends.server.core.DirectoryServer;
+import org.opends.server.types.AttributeType;
+import org.opends.server.types.AttributeValue;
+import org.opends.server.types.DN;
+import org.opends.server.types.DirectoryException;
+import org.opends.server.types.RDN;
 
 
 
@@ -114,6 +120,95 @@
     S extends Configuration> {
 
   /**
+   * A serialize which is used to generate the toDN representation.
+   */
+  private static final class DNSerializer implements
+      ManagedObjectPathSerializer {
+
+    // The current DN.
+    private DN dn;
+
+    // The LDAP profile.
+    private final LDAPProfile profile;
+
+
+
+    // Create a new DN builder.
+    private DNSerializer() {
+      this.dn = DN.nullDN();
+      this.profile = LDAPProfile.getInstance();
+    }
+
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public <C extends ConfigurationClient, S extends Configuration>
+    void appendManagedObjectPathElement(
+        InstantiableRelationDefinition<? super C, ? super S> r,
+        AbstractManagedObjectDefinition<C, S> d, String name) {
+      // Add the RDN sequence representing the relation.
+      appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
+
+      // Now add the single RDN representing the named instance.
+      String type = profile.getInstantiableRelationChildRDNType(r);
+      AttributeType atype = DirectoryServer.getAttributeType(
+          type.toLowerCase(), true);
+      AttributeValue avalue = new AttributeValue(atype, name);
+      dn = dn.concat(RDN.create(atype, avalue));
+    }
+
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public <C extends ConfigurationClient, S extends Configuration>
+    void appendManagedObjectPathElement(
+        OptionalRelationDefinition<? super C, ? super S> r,
+        AbstractManagedObjectDefinition<C, S> d) {
+      // Add the RDN sequence representing the relation.
+      appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
+    }
+
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public <C extends ConfigurationClient, S extends Configuration>
+    void appendManagedObjectPathElement(
+        SingletonRelationDefinition<? super C, ? super S> r,
+        AbstractManagedObjectDefinition<C, S> d) {
+      // Add the RDN sequence representing the relation.
+      appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
+    }
+
+
+
+    // Appends the RDN sequence representing the provided relation.
+    private void appendManagedObjectPathElement(RelationDefinition<?, ?> r) {
+      // Add the RDN sequence representing the relation.
+      try {
+        DN localName = DN.decode(profile.getRelationRDNSequence(r));
+        dn = dn.concat(localName);
+      } catch (DirectoryException e) {
+        throw new RuntimeException(e);
+      }
+    }
+
+
+
+    // Gets the serialized DN value.
+    private DN toDN() {
+      return dn;
+    }
+  }
+
+
+
+  /**
    * Abstract path element.
    */
   private static abstract class Element<C extends ConfigurationClient,
@@ -943,6 +1038,27 @@
 
 
   /**
+   * Determines whether this managed object path references the same
+   * location as the provided managed object path.
+   * <p>
+   * This method differs from <code>equals</code> in that it ignores
+   * sub-type definitions.
+   *
+   * @param other
+   *          The managed object path to be compared.
+   * @return Returns <code>true</code> if this managed object path
+   *         references the same location as the provided managed
+   *         object path.
+   */
+  public boolean matches(ManagedObjectPath<?, ?> other) {
+    DN thisDN = toDN();
+    DN otherDN = other.toDN();
+    return thisDN.equals(otherDN);
+  }
+
+
+
+  /**
    * Creates a new parent managed object path representing the
    * immediate parent of this path. This method is a short-hand for
    * <code>parent(1)</code>.
@@ -1065,6 +1181,20 @@
 
 
   /**
+   * Creates a DN representation of this managed object path.
+   *
+   * @return Returns a DN representation of this managed object path.
+   */
+  public DN toDN() {
+    // Use a simple serializer to create the contents.
+    DNSerializer serializer = new DNSerializer();
+    serialize(serializer);
+    return serializer.toDN();
+  }
+
+
+
+  /**
    * {@inheritDoc}
    */
   @Override
diff --git a/opends/src/server/org/opends/server/admin/server/DNBuilder.java b/opends/src/server/org/opends/server/admin/server/DNBuilder.java
index 35c62cd..7f2d764 100644
--- a/opends/src/server/org/opends/server/admin/server/DNBuilder.java
+++ b/opends/src/server/org/opends/server/admin/server/DNBuilder.java
@@ -29,41 +29,30 @@
 
 
 
-import org.opends.server.admin.AbstractManagedObjectDefinition;
-import org.opends.server.admin.Configuration;
-import org.opends.server.admin.ConfigurationClient;
-import org.opends.server.admin.InstantiableRelationDefinition;
 import org.opends.server.admin.LDAPProfile;
 import org.opends.server.admin.ManagedObjectPath;
-import org.opends.server.admin.ManagedObjectPathSerializer;
-import org.opends.server.admin.OptionalRelationDefinition;
 import org.opends.server.admin.RelationDefinition;
-import org.opends.server.admin.SingletonRelationDefinition;
-import org.opends.server.core.DirectoryServer;
-import org.opends.server.types.AttributeType;
-import org.opends.server.types.AttributeValue;
 import org.opends.server.types.DN;
 import org.opends.server.types.DirectoryException;
-import org.opends.server.types.RDN;
 
 
 
 /**
- * A strategy for creating <code>DN</code>s from managed object paths.
+ * A factory class for creating <code>DN</code>s from managed
+ * object paths.
  */
-final class DNBuilder implements ManagedObjectPathSerializer {
+final class DNBuilder {
 
   /**
    * Creates a new DN representing the specified managed object path.
    *
    * @param path
    *          The managed object path.
-   * @return Returns a new DN representing the specified managed object path.
+   * @return Returns a new DN representing the specified managed
+   *         object path.
    */
   public static DN create(ManagedObjectPath<?, ?> path) {
-    DNBuilder builder = new DNBuilder();
-    path.serialize(builder);
-    return builder.getInstance();
+    return path.toDN();
   }
 
 
@@ -81,61 +70,12 @@
    */
   public static DN create(ManagedObjectPath<?, ?> path,
       RelationDefinition<?, ?> relation) {
-    DNBuilder builder = new DNBuilder();
-    path.serialize(builder);
-    builder.appendManagedObjectPathElement(relation);
-    return builder.getInstance();
-  }
+    DN dn = path.toDN();
 
-  // The current DN.
-  private DN dn;
-
-  // The LDAP profile.
-  private final LDAPProfile profile;
-
-
-
-  /**
-   * Create a new DN builder.
-   */
-  public DNBuilder() {
-    this.dn = DN.nullDN();
-    this.profile = LDAPProfile.getInstance();
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public <C extends ConfigurationClient, S extends Configuration>
-      void appendManagedObjectPathElement(
-          InstantiableRelationDefinition<? super C, ? super S> r,
-          AbstractManagedObjectDefinition<C, S> d, String name) {
-    // Add the RDN sequence representing the relation.
-    appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
-
-    // Now add the single RDN representing the named instance.
-    String type = profile.getInstantiableRelationChildRDNType(r);
-    AttributeType atype = DirectoryServer.getAttributeType(type.toLowerCase(),
-                                                           true);
-    AttributeValue avalue = new AttributeValue(atype, name);
-    dn = dn.concat(RDN.create(atype, avalue));
-  }
-
-
-
-  /**
-   * Appends the RDN sequence representing the provided relation.
-   *
-   * @param r
-   *          The relation definition.
-   */
-  public void appendManagedObjectPathElement(RelationDefinition<?, ?> r) {
-    // Add the RDN sequence representing the relation.
     try {
-      DN localName = DN.decode(profile.getRelationRDNSequence(r));
-      dn = dn.concat(localName);
+      LDAPProfile profile = LDAPProfile.getInstance();
+      DN localName = DN.decode(profile.getRelationRDNSequence(relation));
+      return dn.concat(localName);
     } catch (DirectoryException e) {
       throw new RuntimeException(e);
     }
@@ -143,38 +83,8 @@
 
 
 
-  /**
-   * {@inheritDoc}
-   */
-  public <C extends ConfigurationClient, S extends Configuration>
-      void appendManagedObjectPathElement(
-          OptionalRelationDefinition<? super C, ? super S> r,
-          AbstractManagedObjectDefinition<C, S> d) {
-    // Add the RDN sequence representing the relation.
-    appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public <C extends ConfigurationClient, S extends Configuration>
-      void appendManagedObjectPathElement(
-          SingletonRelationDefinition<? super C, ? super S> r,
-          AbstractManagedObjectDefinition<C, S> d) {
-    // Add the RDN sequence representing the relation.
-    appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
-  }
-
-
-
-  /**
-   * Create a new DN using the current state of this DN builder.
-   *
-   * @return Returns the new DN instance.
-   */
-  public DN getInstance() {
-    return dn;
+  // Prevent instantiation.
+  private DNBuilder() {
+    // No implementation required.
   }
 }
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/ManagedObjectPathTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/ManagedObjectPathTest.java
index ff3f06d..ea8a1f1 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/ManagedObjectPathTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/ManagedObjectPathTest.java
@@ -47,6 +47,7 @@
 import org.opends.server.admin.std.server.ConnectionHandlerCfg;
 import org.opends.server.admin.std.server.GlobalCfg;
 import org.opends.server.admin.std.server.LDAPConnectionHandlerCfg;
+import org.opends.server.types.DN;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -258,4 +259,87 @@
         ManagedObjectPath
             .valueOf("/relation=synchronization-provider+type=multimaster-synchronization-provider+name=MMR/relation=multimaster-domain+name=Domain"));
   }
+
+  /**
+   * Tests matches and equals methods behave differently.
+   */
+  @Test
+  public void testMatches() {
+    ManagedObjectPath<?, ?> path = ManagedObjectPath.emptyPath();
+
+    ManagedObjectPath<ConnectionHandlerCfgClient, ConnectionHandlerCfg> child1 = path
+        .child(RootCfgDefn.getInstance()
+            .getConnectionHandlersRelationDefinition(),
+            "LDAP connection handler");
+
+    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child2 = path
+        .child(RootCfgDefn.getInstance()
+            .getConnectionHandlersRelationDefinition(),
+            LDAPConnectionHandlerCfgDefn.getInstance(),
+            "LDAP connection handler");
+
+    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child3 = path
+        .child(RootCfgDefn.getInstance()
+            .getConnectionHandlersRelationDefinition(),
+            LDAPConnectionHandlerCfgDefn.getInstance(),
+            "Another LDAP connection handler");
+
+    assertTrue(child1.matches(child1));
+    assertTrue(child2.matches(child2));
+    assertTrue(child1.matches(child2));
+    assertTrue(child2.matches(child1));
+    
+    assertTrue(child1.equals(child1));
+    assertTrue(child2.equals(child2));
+    assertFalse(child1.equals(child2));
+    assertFalse(child2.equals(child1));
+    
+    assertFalse(child1.matches(child3));
+    assertFalse(child2.matches(child3));
+    assertFalse(child3.matches(child1));
+    assertFalse(child3.matches(child2));
+    
+    assertFalse(child1.equals(child3));
+    assertFalse(child2.equals(child3));
+    assertFalse(child3.equals(child1));
+    assertFalse(child3.equals(child2));
+  }
+  
+  /**
+   * Tests toDN method.
+   * 
+   * @throws Exception
+   *           If an unexpected error occurred.
+   */
+  @Test
+  public void testToDN() throws Exception {
+    ManagedObjectPath<?, ?> path = ManagedObjectPath.emptyPath();
+
+    ManagedObjectPath<ConnectionHandlerCfgClient, ConnectionHandlerCfg> child1 = path
+        .child(RootCfgDefn.getInstance()
+            .getConnectionHandlersRelationDefinition(),
+            "LDAP connection handler");
+
+    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child2 = path
+        .child(RootCfgDefn.getInstance()
+            .getConnectionHandlersRelationDefinition(),
+            LDAPConnectionHandlerCfgDefn.getInstance(),
+            "LDAP connection handler");
+
+    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child3 = path
+        .child(RootCfgDefn.getInstance()
+            .getConnectionHandlersRelationDefinition(),
+            LDAPConnectionHandlerCfgDefn.getInstance(),
+            "Another LDAP connection handler");
+    
+    DN expectedEmpty = DN.nullDN();
+    DN expectedChild1 = DN.decode("cn=LDAP connection handler,cn=connection handlers,cn=config");
+    DN expectedChild2 = DN.decode("cn=LDAP connection handler,cn=connection handlers,cn=config");
+    DN expectedChild3 = DN.decode("cn=Another LDAP connection handler,cn=connection handlers,cn=config");
+    
+    assertEquals(path.toDN(), expectedEmpty);
+    assertEquals(child1.toDN(), expectedChild1);
+    assertEquals(child2.toDN(), expectedChild2);
+    assertEquals(child3.toDN(), expectedChild3);
+  }
 }
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java
index 545c6f4..a79be3a 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java
@@ -101,9 +101,7 @@
         .getTestChildrenRelationDefinition(), "test-child-1");
 
     // Now serialize it.
-    DNBuilder builder = new DNBuilder();
-    path.serialize(builder);
-    DN actual = builder.getInstance();
+    DN actual = DNBuilder.create(path);
     DN expected = DN
         .decode("cn=test-child-1,cn=test children,cn=test-parent-1,cn=test parents,cn=config");
 
@@ -152,9 +150,7 @@
     // Now serialize it.
     LDAPProfile.getInstance().pushWrapper(wrapper);
     try {
-      DNBuilder builder = new DNBuilder();
-      path.serialize(builder);
-      DN actual = builder.getInstance();
+      DN actual = DNBuilder.create(path);
       DN expected = DN
           .decode("cn=singleton-test-child,cn=test-parent-1,cn=test parents,cn=config");
 
@@ -184,9 +180,7 @@
         .getOptionalTestChildRelationDefinition());
 
     // Now serialize it.
-    DNBuilder builder = new DNBuilder();
-    path.serialize(builder);
-    DN actual = builder.getInstance();
+    DN actual = DNBuilder.create(path);
     DN expected = DN
         .decode("cn=optional test child,cn=test-parent-1,cn=test parents,cn=config");
 

--
Gitblit v1.10.0