opends/src/server/org/opends/server/admin/LDAPProfile.java
@@ -38,15 +38,25 @@ /** * This class is used to map configuration elements to their LDAP schema names. * This class is used to map configuration elements to their LDAP * schema names. */ public final class LDAPProfile { public abstract class LDAPProfile { // This class is abstract so that we can derive a mock LDAP profile // for testing. // The singleton instance. private static final LDAPProfile INSTANCE = new LDAPProfile(); private static final LDAPProfile INSTANCE = new MyLDAPProfile(); // The LDAP profile property table. private final ManagedObjectDefinitionResource resource; /** * Protected default constructor. */ protected LDAPProfile() { // No implementation required. } @@ -61,43 +71,143 @@ // Private constructor. private LDAPProfile() { this.resource = ManagedObjectDefinitionResource.createForProfile("ldap"); /** * Concrete implementation. */ private static class MyLDAPProfile extends LDAPProfile { // The LDAP profile property table. private final ManagedObjectDefinitionResource resource; // Private constructor. private MyLDAPProfile() { this.resource = ManagedObjectDefinitionResource .createForProfile("ldap"); } /** * {@inheritDoc} */ public String getInstantiableRelationChildRDNType( InstantiableRelationDefinition<?, ?> r) { return resource.getString(r.getParentDefinition(), "naming-attribute." + r.getName()); } /** * {@inheritDoc} */ public List<String> getInstantiableRelationObjectClasses( InstantiableRelationDefinition<?, ?> r) { return Arrays.asList(new String[] { "top", "ds-cfg-branch" }); } /** * {@inheritDoc} */ public String getRelationRDNSequence(RelationDefinition<?, ?> r) { return resource.getString(r.getParentDefinition(), "rdn." + r.getName()); } /** * {@inheritDoc} */ public String getFilter(AbstractManagedObjectDefinition<?, ?> d) { StringBuilder builder = new StringBuilder(); builder.append("(ObjectClass="); builder.append(getObjectClass(d)); builder.append(')'); return builder.toString(); } /** * {@inheritDoc} */ public String getObjectClass( AbstractManagedObjectDefinition<?, ?> d) { return resource.getString(d, "objectclass"); } /** * {@inheritDoc} */ public List<String> getObjectClasses( AbstractManagedObjectDefinition<?, ?> d) { LinkedList<String> objectClasses = new LinkedList<String>(); Set<String> s = new HashSet<String>(); // Add the object classes from the parent hierarchy. while (d != null) { String oc = getObjectClass(d); if (!s.contains(oc)) { objectClasses.addFirst(oc); s.add(oc); } d = d.getParent(); } // Make sure that we have top. if (!s.contains("top")) { objectClasses.addFirst("top"); } return objectClasses; } /** * {@inheritDoc} */ public String getAttributeName(ManagedObjectDefinition<?, ?> d, PropertyDefinition<?> pd) { return resource.getString(d, "attribute." + pd.getName()); } } /** * Gets the LDAP RDN attribute type for child entries of an instantiable * relation. * Gets the LDAP RDN attribute type for child entries of an * instantiable relation. * * @param r * The instantiable relation. * @return Returns the LDAP RDN attribute type for child entries of an * instantiable relation. * @return Returns the LDAP RDN attribute type for child entries of * an instantiable relation. */ public String getInstantiableRelationChildRDNType( InstantiableRelationDefinition<?, ?> r) { return resource.getString(r.getParentDefinition(), "naming-attribute." + r.getName()); } public abstract String getInstantiableRelationChildRDNType( InstantiableRelationDefinition<?, ?> r); /** * Gets the LDAP object classes associated with an instantiable relation * branch. The branch is the parent entry of child managed objects. * Gets the LDAP object classes associated with an instantiable * relation branch. The branch is the parent entry of child managed * objects. * * @param r * The instantiable relation. * @return Returns the LDAP object classes associated with an instantiable * relation branch. * @return Returns the LDAP object classes associated with an * instantiable relation branch. */ public List<String> getInstantiableRelationObjectClasses( InstantiableRelationDefinition<?, ?> r) { return Arrays.asList(new String[] { "top", "ds-cfg-branch" }); } public abstract List<String> getInstantiableRelationObjectClasses( InstantiableRelationDefinition<?, ?> r); @@ -106,95 +216,69 @@ * * @param r * The relation. * @return Returns the LDAP RDN sequence associatied with a relation. * @return Returns the LDAP RDN sequence associatied with a * relation. */ public String getRelationRDNSequence(RelationDefinition<?, ?> r) { return resource.getString(r.getParentDefinition(), "rdn." + r.getName()); } public abstract String getRelationRDNSequence( RelationDefinition<?, ?> r); /** * Get an LDAP filter string which can be used to search for entries matching * the specified definition. * Get an LDAP filter string which can be used to search for entries * matching the specified definition. * * @param d * The managed object definition. * @return Returns the LDAP filter. */ public String getFilter(AbstractManagedObjectDefinition<?, ?> d) { StringBuilder builder = new StringBuilder(); builder.append("(ObjectClass="); builder.append(getObjectClass(d)); builder.append(')'); return builder.toString(); } public abstract String getFilter( AbstractManagedObjectDefinition<?, ?> d); /** * Get the principle object class associated with the specified definition. * * @param d * The managed object definition. * @return Returns the principle object class associated with the specified * definition. */ public String getObjectClass(AbstractManagedObjectDefinition<?, ?> d) { return resource.getString(d, "objectclass"); } /** * Get all the object classes associated with the specified definition. * <p> * The returned list is ordered such that the uppermost object classes appear * first (e.g. top). * * @param d * The managed object definition. * @return Returns all the object classes associated with the specified * definition. */ public List<String> getObjectClasses( AbstractManagedObjectDefinition<?, ?> d) { LinkedList<String> objectClasses = new LinkedList<String>(); Set<String> s = new HashSet<String>(); // Add the object classes from the parent hierarchy. while (d != null) { String oc = getObjectClass(d); if (!s.contains(oc)) { objectClasses.addFirst(oc); s.add(oc); } d = d.getParent(); } // Make sure that we have top. if (!s.contains("top")) { objectClasses.addFirst("top"); } return objectClasses; } /** * Get the name of the LDAP attribute associated with the specified property * Get the principle object class associated with the specified * definition. * * @param d * The managed object definition. * @return Returns the principle object class associated with the * specified definition. */ public abstract String getObjectClass( AbstractManagedObjectDefinition<?, ?> d); /** * Get all the object classes associated with the specified * definition. * <p> * The returned list is ordered such that the uppermost object * classes appear first (e.g. top). * * @param d * The managed object definition. * @return Returns all the object classes associated with the * specified definition. */ public abstract List<String> getObjectClasses( AbstractManagedObjectDefinition<?, ?> d); /** * Get the name of the LDAP attribute associated with the specified * property definition. * * @param d * The managed object definition. * @param pd * The property definition. * @return Returns the name of the LDAP attribute associated with the * specified property definition. * @return Returns the name of the LDAP attribute associated with * the specified property definition. */ public String getAttributeName(ManagedObjectDefinition<?, ?> d, PropertyDefinition<?> pd) { return resource.getString(d, "attribute." + pd.getName()); } public abstract String getAttributeName( ManagedObjectDefinition<?, ?> d, PropertyDefinition<?> pd); } opends/src/server/org/opends/server/admin/server/DNBuilder.java
@@ -96,8 +96,23 @@ * Create a new DN builder. */ public DNBuilder() { this(LDAPProfile.getInstance()); } /** * Create a new DN builder with the provided LDAP profile. * <p> * This constructor is package private and only intended for testing * purposes against a mock LDAP profile. * * @param profile * The LDAP profile to use when building the DN. */ DNBuilder(LDAPProfile profile) { this.dn = DN.nullDN(); this.profile = LDAPProfile.getInstance(); this.profile = profile; } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java
New file @@ -0,0 +1,169 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import static org.testng.Assert.assertEquals; import org.opends.server.TestCaseUtils; import org.opends.server.admin.AdminTestCase; import org.opends.server.admin.InstantiableRelationDefinition; import org.opends.server.admin.ManagedObjectPath; import org.opends.server.admin.OptionalRelationDefinition; import org.opends.server.admin.SingletonRelationDefinition; import org.opends.server.admin.std.meta.RootCfgDefn; import org.opends.server.types.DN; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; /** * Test cases for the server DNBuilder class. */ public final class DNBuilderTest extends AdminTestCase { /** * Sets up tests * * @throws Exception * If the server could not be initialized. */ @BeforeClass public void setUp() throws Exception { // This test suite depends on having the schema available, so // we'll start the server. TestCaseUtils.startServer(); } /** * Tests construction of a DN from a managed object path containing * a subordinate one-to-many relationship. * * @throws Exception * If an unexpected exception occurred. */ @Test public void testCreateOneToMany() throws Exception { // First create the path. ManagedObjectPath path = ManagedObjectPath.emptyPath(); InstantiableRelationDefinition<TestParentCfgClient, TestParentCfg> r1 = new InstantiableRelationDefinition<TestParentCfgClient, TestParentCfg>( RootCfgDefn.getInstance(), "test-parent", "test-parents", TestParentCfgDefn.getInstance()); InstantiableRelationDefinition<TestChildCfgClient, TestChildCfg> r2 = new InstantiableRelationDefinition<TestChildCfgClient, TestChildCfg>( TestParentCfgDefn.getInstance(), "test-child", "test-children", TestChildCfgDefn.getInstance()); path = path.child(r1, "test-parent-1"); path = path.child(r2, "test-child-1"); // Now serialize it. DNBuilder builder = new DNBuilder(new MockLDAPProfile()); path.serialize(builder); DN actual = builder.getInstance(); DN expected = DN .decode("cn=test-child-1,cn=test-children,cn=test-parent-1,cn=test-parents"); assertEquals(actual, expected); } /** * Tests construction of a DN from a managed object path containing * a subordinate one-to-one relationship. * * @throws Exception * If an unexpected exception occurred. */ @Test public void testCreateOneToOne() throws Exception { // First create the path. ManagedObjectPath path = ManagedObjectPath.emptyPath(); InstantiableRelationDefinition<TestParentCfgClient, TestParentCfg> r1 = new InstantiableRelationDefinition<TestParentCfgClient, TestParentCfg>( RootCfgDefn.getInstance(), "test-parent", "test-parents", TestParentCfgDefn.getInstance()); SingletonRelationDefinition<TestChildCfgClient, TestChildCfg> r2 = new SingletonRelationDefinition<TestChildCfgClient, TestChildCfg>( TestParentCfgDefn.getInstance(), "singleton-test-child", TestChildCfgDefn.getInstance()); path = path.child(r1, "test-parent-1"); path = path.child(r2); // Now serialize it. DNBuilder builder = new DNBuilder(new MockLDAPProfile()); path.serialize(builder); DN actual = builder.getInstance(); DN expected = DN .decode("cn=singleton-test-child,cn=test-parent-1,cn=test-parents"); assertEquals(actual, expected); } /** * Tests construction of a DN from a managed object path containing * a subordinate one-to-zero-or-one relationship. * * @throws Exception * If an unexpected exception occurred. */ @Test public void testCreateOneToZeroOrOne() throws Exception { // First create the path. ManagedObjectPath path = ManagedObjectPath.emptyPath(); InstantiableRelationDefinition<TestParentCfgClient, TestParentCfg> r1 = new InstantiableRelationDefinition<TestParentCfgClient, TestParentCfg>( RootCfgDefn.getInstance(), "test-parent", "test-parents", TestParentCfgDefn.getInstance()); OptionalRelationDefinition<TestChildCfgClient, TestChildCfg> r2 = new OptionalRelationDefinition<TestChildCfgClient, TestChildCfg>( TestParentCfgDefn.getInstance(), "optional-test-child", TestChildCfgDefn.getInstance()); path = path.child(r1, "test-parent-1"); path = path.child(r2); // Now serialize it. DNBuilder builder = new DNBuilder(new MockLDAPProfile()); path.serialize(builder); DN actual = builder.getInstance(); DN expected = DN .decode("cn=optional-test-child,cn=test-parent-1,cn=test-parents"); assertEquals(actual, expected); } } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/MockLDAPProfile.java
New file @@ -0,0 +1,137 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import java.util.List; import org.opends.server.admin.AbstractManagedObjectDefinition; import org.opends.server.admin.InstantiableRelationDefinition; import org.opends.server.admin.LDAPProfile; import org.opends.server.admin.ManagedObjectDefinition; import org.opends.server.admin.PropertyDefinition; import org.opends.server.admin.RelationDefinition; /** * A mock LDAP profile for testing purposes. */ public final class MockLDAPProfile extends LDAPProfile { /** * Creates a new mock LDAP profile. */ public MockLDAPProfile() { // No implementation required. } /** * {@inheritDoc} */ @Override public String getAttributeName(ManagedObjectDefinition<?, ?> d, PropertyDefinition<?> pd) { return "ds-cfg-" + pd.getName(); } /** * {@inheritDoc} */ @Override public String getFilter(AbstractManagedObjectDefinition<?, ?> d) { // Not implemented yet. throw new UnsupportedOperationException(); } /** * {@inheritDoc} */ @Override public String getInstantiableRelationChildRDNType( InstantiableRelationDefinition<?, ?> r) { return "cn"; } /** * {@inheritDoc} */ @Override public List<String> getInstantiableRelationObjectClasses( InstantiableRelationDefinition<?, ?> r) { // Not implemented yet. throw new UnsupportedOperationException(); } /** * {@inheritDoc} */ @Override public String getObjectClass(AbstractManagedObjectDefinition<?, ?> d) { // Not implemented yet. throw new UnsupportedOperationException(); } /** * {@inheritDoc} */ @Override public List<String> getObjectClasses( AbstractManagedObjectDefinition<?, ?> d) { // Not implemented yet. throw new UnsupportedOperationException(); } /** * {@inheritDoc} */ @Override public String getRelationRDNSequence(RelationDefinition<?, ?> r) { if (r instanceof InstantiableRelationDefinition) { InstantiableRelationDefinition<?, ?> i = (InstantiableRelationDefinition<?, ?>) r; return "cn=" + i.getPluralName(); } else { return "cn=" + r.getName(); } } } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/TestChildCfg.java
New file @@ -0,0 +1,46 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import org.opends.server.admin.Configuration; import org.opends.server.admin.ManagedObjectDefinition; /** * A sample server-side configuration interface for testing. */ public interface TestChildCfg extends Configuration { /** * {@inheritDoc} */ ManagedObjectDefinition<? extends TestChildCfgClient, ? extends TestChildCfg> definition(); } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/TestChildCfgClient.java
New file @@ -0,0 +1,46 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import org.opends.server.admin.ConfigurationClient; import org.opends.server.admin.ManagedObjectDefinition; /** * A sample client-side configuration interface for testing. */ public interface TestChildCfgClient extends ConfigurationClient { /** * {@inheritDoc} */ ManagedObjectDefinition<? extends TestChildCfgClient, ? extends TestChildCfg> definition(); } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/TestChildCfgDefn.java
New file @@ -0,0 +1,64 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import org.opends.server.admin.AbstractManagedObjectDefinition; /** * A sample configuration definition class for testing. */ public final class TestChildCfgDefn extends AbstractManagedObjectDefinition<TestChildCfgClient, TestChildCfg> { // The singleton configuration definition instance. private static final TestChildCfgDefn INSTANCE = new TestChildCfgDefn(); /** * Get the definition singleton. * * @return Returns the definition singleton. */ public static TestChildCfgDefn getInstance() { return INSTANCE; } /** * Private constructor. */ private TestChildCfgDefn() { super("test-child", null); } } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/TestParentCfg.java
New file @@ -0,0 +1,46 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import org.opends.server.admin.Configuration; import org.opends.server.admin.ManagedObjectDefinition; /** * A sample server-side configuration interface for testing. */ public interface TestParentCfg extends Configuration { /** * {@inheritDoc} */ ManagedObjectDefinition<? extends TestParentCfgClient, ? extends TestParentCfg> definition(); } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/TestParentCfgClient.java
New file @@ -0,0 +1,46 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import org.opends.server.admin.ConfigurationClient; import org.opends.server.admin.ManagedObjectDefinition; /** * A sample client-side configuration interface for testing. */ public interface TestParentCfgClient extends ConfigurationClient { /** * {@inheritDoc} */ ManagedObjectDefinition<? extends TestParentCfgClient, ? extends TestParentCfg> definition(); } opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/TestParentCfgDefn.java
New file @@ -0,0 +1,65 @@ /* * 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 * * * Portions Copyright 2007 Sun Microsystems, Inc. */ package org.opends.server.admin.server; import org.opends.server.admin.AbstractManagedObjectDefinition; /** * A sample configuration definition class for testing. */ public final class TestParentCfgDefn extends AbstractManagedObjectDefinition<TestParentCfgClient, TestParentCfg> { // The singleton configuration definition instance. private static final TestParentCfgDefn INSTANCE = new TestParentCfgDefn(); /** * Get the definition singleton. * * @return Returns the definition singleton. */ public static TestParentCfgDefn getInstance() { return INSTANCE; } /** * Private constructor. */ private TestParentCfgDefn() { super("test-parent", null); } }