Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 71359 invoked from network); 15 Aug 2005 20:42:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 15 Aug 2005 20:42:37 -0000 Received: (qmail 87892 invoked by uid 500); 15 Aug 2005 20:42:36 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 87821 invoked by uid 500); 15 Aug 2005 20:42:35 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 87808 invoked by uid 99); 15 Aug 2005 20:42:35 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Aug 2005 13:42:35 -0700 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 15 Aug 2005 13:42:55 -0700 Received: (qmail 71332 invoked by uid 65534); 15 Aug 2005 20:42:33 -0000 Message-ID: <20050815204233.71331.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r232870 - in /directory/testsuite: ./ trunk/ trunk/protocoltests/ trunk/protocoltests/jndi/ trunk/protocoltests/jndi/controls/ trunk/protocoltests/jndi/ops/ trunk/protocoltests/jndi/ops/add/ trunk/protocoltests/jndi/ops/delete/ trunk/protoc... Date: Mon, 15 Aug 2005 20:42:31 -0000 To: commits@directory.apache.org From: akarasulu@apache.org X-Mailer: svnmailer-1.0.3 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: akarasulu Date: Mon Aug 15 13:42:20 2005 New Revision: 232870 URL: http://svn.apache.org/viewcvs?rev=232870&view=rev Log: checking in stefans code from DIREVE-225 Added: directory/testsuite/ directory/testsuite/trunk/ directory/testsuite/trunk/jndi.properties (with props) directory/testsuite/trunk/protocoltests/ directory/testsuite/trunk/protocoltests/jndi/ directory/testsuite/trunk/protocoltests/jndi/BaseProtocolTest.java (with props) directory/testsuite/trunk/protocoltests/jndi/controls/ directory/testsuite/trunk/protocoltests/jndi/ops/ directory/testsuite/trunk/protocoltests/jndi/ops/AllTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/add/ directory/testsuite/trunk/protocoltests/jndi/ops/add/AllTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/add/BasicAddTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/delete/ directory/testsuite/trunk/protocoltests/jndi/ops/delete/AllTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/delete/BasicDeleteTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/delete/TreeDeleteControlTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/ directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/AllTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/BasicModifyDnTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java (with props) directory/testsuite/trunk/protocoltests/jndi/schema/ Added: directory/testsuite/trunk/jndi.properties URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/jndi.properties?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/jndi.properties (added) +++ directory/testsuite/trunk/jndi.properties Mon Aug 15 13:42:20 2005 @@ -0,0 +1,9 @@ +java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory + +# Apache Directory Server +# (magritte, Windows XP) +# +java.naming.provider.url=ldap://localhost:10389/ou=system +java.naming.security.principal=uid=admin,ou=system +java.naming.security.credentials=secret +java.naming.security.authentication=simple \ No newline at end of file Propchange: directory/testsuite/trunk/jndi.properties ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/BaseProtocolTest.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/BaseProtocolTest.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/BaseProtocolTest.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/BaseProtocolTest.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,89 @@ +package protocoltests.jndi; + +import javax.naming.Binding; +import javax.naming.NameNotFoundException; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; +import javax.naming.directory.DirContext; +import javax.naming.ldap.InitialLdapContext; +import javax.naming.ldap.LdapContext; + +import junit.framework.TestCase; + +public class BaseProtocolTest extends TestCase +{ + + public void setUp() throws NamingException + { + + // Check whether test container entry exists + // If yes: delete it + DirContext ctx = this.createContext(); + try { + LdapContext entry = (LdapContext) ctx.lookup(getTestContainerRdn()); + this.deleteSubtree(entry); + } catch (NameNotFoundException ignored) { + } + + // Create an entry + Attributes attributes = new BasicAttributes(); + Attribute attribute = new BasicAttribute("objectClass"); + attribute.add("top"); + attribute.add("organizationalUnit"); + attributes.put(attribute); + attributes.put("ou", "playground"); + + DirContext pg = ctx.createSubcontext(getTestContainerRdn(), attributes); + assertNotNull(pg); + + pg = (DirContext) ctx.lookup(getTestContainerRdn()); + assertNotNull(pg); + + ctx.close(); + ctx = null; + } + + public void tearDown() throws NamingException + { + LdapContext ctx = this.createContext(); + + LdapContext pg = (LdapContext) ctx.lookup(getTestContainerRdn()); + this.deleteSubtree(pg); + + try { + ctx.lookup(getTestContainerRdn()); + fail("test entry not deleted"); + } catch (NameNotFoundException ignored) { + } + + ctx.close(); + ctx = null; + } + + private void deleteSubtree(LdapContext entry) throws NamingException + { + + NamingEnumeration enumeration = entry.listBindings(""); + while (enumeration.hasMore()) { + Binding b = (Binding) enumeration.next(); + if (b.getObject() instanceof LdapContext) { + deleteSubtree((LdapContext) b.getObject()); + } + } + entry.unbind(""); + } + + protected LdapContext createContext() throws NamingException + { + return new InitialLdapContext(); + } + + protected String getTestContainerRdn() + { + return "ou=playground"; + } +} \ No newline at end of file Propchange: directory/testsuite/trunk/protocoltests/jndi/BaseProtocolTest.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/AllTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/AllTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/AllTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/AllTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,19 @@ +package protocoltests.jndi.ops; + +import junit.framework.TestSuite; + +/** + * @author szoerner + */ +public class AllTests extends TestSuite { + + public static TestSuite suite() { + TestSuite suite = new TestSuite("Operations"); + + suite.addTest(protocoltests.jndi.ops.add.AllTests.suite()); + suite.addTest(protocoltests.jndi.ops.delete.AllTests.suite()); + suite.addTest(protocoltests.jndi.ops.modifydn.AllTests.suite()); + + return suite; + } +} Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/AllTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/add/AllTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/add/AllTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/add/AllTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/add/AllTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,17 @@ +package protocoltests.jndi.ops.add; + +import junit.framework.TestSuite; + +/** + * @author szoerner + */ +public class AllTests extends TestSuite { + + public static TestSuite suite() { + TestSuite suite = new TestSuite("Add"); + + suite.addTestSuite(BasicAddTests.class); + + return suite; + } +} Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/add/AllTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/add/BasicAddTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/add/BasicAddTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/add/BasicAddTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/add/BasicAddTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,120 @@ +package protocoltests.jndi.ops.add; + +import javax.naming.NameAlreadyBoundException; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; +import javax.naming.directory.DirContext; + +import protocoltests.jndi.BaseProtocolTest; + + +public class BasicAddTests extends BaseProtocolTest { + + private static String PERSON_SN_VALUE = "Amos"; + private static String PERSON_CN_VALUE = "Tori Amos"; + private static String PERSON_CN_ALTERNATE = "Myra Ellen Amos"; + + private static String PERSON_RDN = "cn="+PERSON_CN_VALUE; + + private Attributes createPersonAttributes(String cn, String sn) { + Attributes person = new BasicAttributes(); + + Attribute ocls = new BasicAttribute("objectClass"); + ocls.add("top"); + ocls.add("person"); + person.put(ocls); + + person.put("cn", cn); + person.put("sn", sn); + + return person; + } + + /** + * Just checks whether creation of a simple entry succeeds. + * + * @throws NamingException + */ + public void testAddEntry() throws NamingException { + + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Add a person + Attributes attributes = createPersonAttributes(PERSON_CN_VALUE, PERSON_SN_VALUE); + DirContext person = target + .createSubcontext(PERSON_RDN, attributes); + + // Check wether person looks fine + person = (DirContext) target.lookup(PERSON_RDN); + assertNotNull(person); + attributes = person.getAttributes(""); + + // objectclasses + Attribute ocls = attributes.get("objectClass"); + String[] expectedOcls = { "top", "person" }; + for (int i = 0; i < expectedOcls.length; i++) { + String name = expectedOcls[i]; + assertTrue("object class " + name + " is not present", ocls + .contains(name)); + } + + // Other attributes + Attribute cn = attributes.get("cn"); + assertTrue(cn.contains(PERSON_CN_VALUE)); + Attribute sn = attributes.get("sn"); + assertTrue(sn.contains(PERSON_SN_VALUE)); + + ctx.close(); + } + + /** + * Checks whether a duplicate entry causes a NameAlreadyBoundException. + * + * @throws NamingException + */ + public void testAddDuplicateEntry() throws NamingException { + + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + Attributes attributes = createPersonAttributes(PERSON_CN_VALUE, PERSON_SN_VALUE); + target.createSubcontext(PERSON_RDN, attributes); + + try { + target.createSubcontext(PERSON_RDN, attributes); + fail("duplicate entry should fail"); + } catch (NameAlreadyBoundException e) { + // Excepted behaviour; + assertTrue(true); + } + ctx.close(); + } + + public void testAddEntryWithMultipleValues() throws NamingException { + + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Create a person, description has multiple attributes + Attributes attributes = createPersonAttributes(PERSON_CN_VALUE, PERSON_SN_VALUE); + Attribute descr = new BasicAttribute("description"); + descr.add("a description for the person"); + descr.add("another description for this person"); + attributes.put(descr); + int two = descr.size(); + + target.createSubcontext(PERSON_RDN, attributes); + + DirContext tori = (DirContext) target.lookup(PERSON_RDN); + attributes = tori.getAttributes(""); + descr = attributes.get("description"); + + assertEquals(two, descr.size()); + + ctx.close(); + } +} \ No newline at end of file Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/add/BasicAddTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/delete/AllTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/delete/AllTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/delete/AllTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/delete/AllTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,18 @@ +package protocoltests.jndi.ops.delete; + +import junit.framework.TestSuite; + +/** + * @author szoerner + */ +public class AllTests extends TestSuite { + + public static TestSuite suite() { + TestSuite suite = new TestSuite("Delete"); + + suite.addTestSuite(BasicDeleteTests.class); + suite.addTestSuite(TreeDeleteControlTests.class); + + return suite; + } +} Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/delete/AllTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/delete/BasicDeleteTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/delete/BasicDeleteTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/delete/BasicDeleteTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/delete/BasicDeleteTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,111 @@ +package protocoltests.jndi.ops.delete; + +import javax.naming.ContextNotEmptyException; +import javax.naming.NameNotFoundException; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; +import javax.naming.directory.DirContext; + +import protocoltests.jndi.BaseProtocolTest; + +public class BasicDeleteTests extends BaseProtocolTest +{ + + protected Attributes getOrganizationalUnitAttributes(String ou) + { + Attributes attributes = new BasicAttributes(); + + Attribute ocls = new BasicAttribute("objectClass"); + ocls.add("top"); + ocls.add("organizationalUnit"); + attributes.put(ocls); + + attributes.put("ou", ou); + attributes.put("description", ou + "is an organizationalUnit"); + + return attributes; + } + + /** + * Basic deletion test. Create an entry, delete it, and check whether it is + * gone. + * + * @throws NamingException + */ + public void testDeleteEntry() throws NamingException + { + + final String ou = "myUnit"; + final String rdn = "ou=myUnit"; + + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Add an entry + Attributes attributes = this.getOrganizationalUnitAttributes(ou); + DirContext orgUnit = target.createSubcontext(rdn, attributes); + + // Check wether entry is created + orgUnit = (DirContext) target.lookup(rdn); + assertNotNull(orgUnit); + + // Delete Entry + target.unbind(rdn); + + // Check whether it is gone + try { + orgUnit = (DirContext) target.lookup(rdn); + fail("Deletion of entry failed"); + } catch (NameNotFoundException nnfe) { + // expected behaviour + assertTrue(true); + } + + ctx.close(); + } + + /** + * Basic deletion test. Create an entry, delete it, and check whether it is + * gone. + * + * @throws NamingException + */ + public void testDeleteNonEmptyEntry() throws NamingException + { + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Add an entry + final String ou = "nonLeaf"; + final String rdn = "ou=" + ou; + Attributes attributes = this.getOrganizationalUnitAttributes(ou); + target.createSubcontext(rdn, attributes); + + // Check wether entry is created + DirContext nonLeafRdn = (DirContext) target.lookup(rdn); + assertNotNull(nonLeafRdn); + + // Add an entry below this + final String ouLeaf = "leaf"; + final String rdnLeaf = "ou=" + ouLeaf; + attributes = this.getOrganizationalUnitAttributes(ouLeaf); + nonLeafRdn.createSubcontext(rdnLeaf, attributes); + + // Try to delete subtree Entry + try { + target.unbind(rdn); + fail("deletion of entry should fail."); + } catch (ContextNotEmptyException e) { + // expected; + } + + // Verify that entry soes exist + nonLeafRdn = (DirContext) target.lookup(rdn); + assertNotNull(nonLeafRdn); + + ctx.close(); + } +} Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/delete/BasicDeleteTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/delete/TreeDeleteControlTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/delete/TreeDeleteControlTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/delete/TreeDeleteControlTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/delete/TreeDeleteControlTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,110 @@ +package protocoltests.jndi.ops.delete; + +import javax.naming.NameNotFoundException; +import javax.naming.NamingException; +import javax.naming.OperationNotSupportedException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; +import javax.naming.directory.DirContext; +import javax.naming.ldap.Control; +import javax.naming.ldap.LdapContext; + +import protocoltests.jndi.BaseProtocolTest; + +public class TreeDeleteControlTests extends BaseProtocolTest +{ + class TreeDeleteControl implements Control + { + private static final long serialVersionUID = 1L; + + private boolean critical; + + TreeDeleteControl() { + this(Control.CRITICAL); + } + + TreeDeleteControl(boolean criticalValue) { + this.critical = criticalValue; + } + + public String getID() + { + return "1.2.840.113556.1.4.805"; + } + + public boolean isCritical() + { + return this.critical; + } + + public byte[] getEncodedValue() + { + return null; + } + } + + protected Attributes getOrganizationalUnitAttributes(String ou) + { + Attributes attributes = new BasicAttributes(); + + Attribute ocls = new BasicAttribute("objectClass"); + ocls.add("top"); + ocls.add("organizationalUnit"); + attributes.put(ocls); + + attributes.put("ou", ou); + attributes.put("description", ou + "is an organizationalUnit"); + + return attributes; + } + + public void testDeleteSubtreeWithControl() throws NamingException + { + LdapContext ctx = this.createContext(); + LdapContext target = (LdapContext) ctx.lookup(this.getTestContainerRdn()); + + // Add an entry + final String ou = "nonLeaf"; + final String rdn = "ou=" + ou; + Attributes attributes = this.getOrganizationalUnitAttributes(ou); + target.createSubcontext(rdn, attributes); + + // Check wether entry is created + DirContext nonLeafRdn = (DirContext) target.lookup(rdn); + assertNotNull(nonLeafRdn); + + // Add an entry below this + final String ouLeaf = "leaf"; + final String rdnLeaf = "ou=" + ouLeaf; + attributes = this.getOrganizationalUnitAttributes(ouLeaf); + nonLeafRdn.createSubcontext(rdnLeaf, attributes); + + // Try to delete subtree Entry + boolean supported = true; + try { + target.setRequestControls(new Control[] { new TreeDeleteControl(Control.CRITICAL) }); + target.unbind(rdn); + } catch (OperationNotSupportedException e) { + // expected; + supported = false; + } + target.setRequestControls(null); + + // Check, whether entry is deleted + boolean deleted = false; + try { + target.lookup(rdn); + } catch (NameNotFoundException e) { + deleted = true; + } + + // Two option are valid here: + // 1. The control is not supported and the entry still exists + // 2. The control is supported and te entry is deleted + assertTrue((!supported & !deleted) || (supported & deleted)); + + ctx.close(); + } +} Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/delete/TreeDeleteControlTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/AllTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/AllTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/AllTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/AllTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,18 @@ +package protocoltests.jndi.ops.modifydn; + +import junit.framework.TestSuite; + +/** + * @author szoerner + */ +public class AllTests extends TestSuite { + + public static TestSuite suite() { + TestSuite suite = new TestSuite("Modify DN"); + + suite.addTestSuite(BasicModifyDnTests.class); + suite.addTestSuite(ModifyDnSameAttributeTests.class); + + return suite; + } +} Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/AllTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/BasicModifyDnTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/BasicModifyDnTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/BasicModifyDnTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/BasicModifyDnTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,156 @@ +package protocoltests.jndi.ops.modifydn; + +import javax.naming.NameAlreadyBoundException; +import javax.naming.NameNotFoundException; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; +import javax.naming.directory.DirContext; +import javax.naming.directory.SchemaViolationException; + +import protocoltests.jndi.BaseProtocolTest; + +/** + * Basic Tests for the Modify DN operation. + * + * @author szoerner + */ + +public class BasicModifyDnTests extends BaseProtocolTest { + + /** + * Creates attributes for a person with minimum required attributes. + * + * @param sn + * sn value of the person (surname) + * @param cn + * cn value of the person (common name) + * @return attributes object for this person + */ + protected Attributes getPersonAttributes(String sn, String cn) { + Attributes attributes = new BasicAttributes(); + Attribute attribute = new BasicAttribute("objectClass"); + attribute.add("top"); + attribute.add("person"); + attributes.put(attribute); + attributes.put("cn", cn); + attributes.put("sn", sn); + + return attributes; + } + + /** + * Modify DN of an entry, changing RDN from cn to sn. + * + * @throws NamingException + */ + public void testModifyRdnAndKeepOld() throws NamingException { + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Create a person, cn value is rdn + String cnVal = "Tori Amos"; + String snVal = "Amos"; + String oldRdn = "cn=" + cnVal; + Attributes attributes = this.getPersonAttributes(snVal, cnVal); + target.createSubcontext(oldRdn, attributes); + + // modify Rdn + String newRdn = "sn=" + snVal; + target.addToEnvironment("java.naming.ldap.deleteRDN", "false"); + target.rename(oldRdn, newRdn); + + // Check, whether old Entry does not exists + try { + target.lookup(oldRdn); + fail("Entry must not exist"); + } catch (NameNotFoundException ignored) { + // expected behaviour + } + + // Check, whether new Entry exists + DirContext tori = (DirContext) target.lookup(newRdn); + assertNotNull(tori); + + // Check values of cn and sn + Attribute cn = tori.getAttributes("").get("cn"); + assertTrue(cn.contains(cnVal)); + assertEquals(1, cn.size()); + Attribute sn = tori.getAttributes("").get("sn"); + assertTrue(sn.contains(snVal)); + assertEquals(1, sn.size()); + + ctx.close(); + } + + /** + * Modify Rdn of an entry, changing it from cn to sn, but try delete old + * rdn. This should cause a schema violation. + * + * @throws NamingException + */ + public void testModifyRdnAndTryToDeleteOld() throws NamingException { + + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Create a person, cn value is rdn + String cnVal = "Tori Amos"; + String snVal = "Amos"; + String oldRdn = "cn=" + cnVal; + Attributes attributes = this.getPersonAttributes(snVal, cnVal); + target.createSubcontext(oldRdn, attributes); + + // modify Rdn, but try delete old val + try { + String newRdn = "sn=" + snVal; + target.addToEnvironment("java.naming.ldap.deleteRDN", "true"); + target.rename(oldRdn, newRdn); + fail("modify DN should cause a schema violation"); + } catch (SchemaViolationException sve) { + // expected behaviour + } + + // Check that old entry still exists + DirContext tori = (DirContext) target.lookup(oldRdn); + assertNotNull(tori); + + ctx.close(); + } + + /** + * Modify Rdn of an entry, changing it from cn to sn. But an entry with + * corresponding RDN already exists. This should cause an exception. + * + * @throws NamingException + */ + public void testModifyRdnToExistingValue() throws NamingException { + + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Create a person, cn value is rdn + String cnVal = "Tori Amos"; + String snVal = "Amos"; + String oldRdn = "cn=" + cnVal; + Attributes attributes = this.getPersonAttributes(snVal, cnVal); + target.createSubcontext(oldRdn, attributes); + + // create same person with sn as RDN + String newRdn = "sn=" + snVal; + target.createSubcontext(newRdn, attributes); + + // modify DN, use existing value + target.addToEnvironment("java.naming.ldap.deleteRDN", "false"); + try { + target.rename(oldRdn, newRdn); + fail("modify DN should cause an exception"); + } catch (NameAlreadyBoundException nbe) { + // expected behaviour + } + + ctx.close(); + } +} \ No newline at end of file Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/BasicModifyDnTests.java ------------------------------------------------------------------------------ svn:executable = * Added: directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java?rev=232870&view=auto ============================================================================== --- directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java (added) +++ directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java Mon Aug 15 13:42:20 2005 @@ -0,0 +1,175 @@ +package protocoltests.jndi.ops.modifydn; + +import javax.naming.NameNotFoundException; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; +import javax.naming.directory.DirContext; + +import protocoltests.jndi.BaseProtocolTest; + +public class ModifyDnSameAttributeTests extends BaseProtocolTest +{ + + protected Attributes getPersonAttributes(String sn, String cn) + { + Attributes attributes = new BasicAttributes(); + Attribute attribute = new BasicAttribute("objectClass"); + attribute.add("top"); + attribute.add("person"); + attributes.put(attribute); + attributes.put("cn", cn); + attributes.put("sn", sn); + attributes.put("description", cn + " is a person."); + + return attributes; + } + + /** + * Modify Rdn of an entry, delete its old rdn value. + * + * @throws NamingException + */ + public void testModifyRdnAndDeleteOld() throws NamingException + { + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Create a person, cn value is rdn + String oldCn = "Myra Ellen Amos"; + String oldRdn = "cn=" + oldCn; + Attributes attributes = this.getPersonAttributes("Amos", oldCn); + target.createSubcontext(oldRdn, attributes); + + // modify Rdn + String newCn = "Tori Amos"; + String newRdn = "cn=" + newCn; + target.addToEnvironment("java.naming.ldap.deleteRDN", "true"); + target.rename(oldRdn, newRdn); + + // Check, whether old Entry does not exists + try { + target.lookup(oldRdn); + fail("Entry must not exist"); + } catch (NameNotFoundException ignored) { + // expected behaviour + assertTrue(true); + } + + // Check, whether new Entry exists + DirContext tori = (DirContext) target.lookup(newRdn); + assertNotNull(tori); + + // Check values of cn + Attribute cn = tori.getAttributes("").get("cn"); + assertTrue(cn.contains(newCn)); + assertTrue(!cn.contains(oldCn)); // old vaue is gone + assertEquals(1, cn.size()); + + // Remove entry (use new rdn) + target.unbind(newRdn); + } + + /** + * Modify Rdn of an entry, keep its old rdn value. + * + * @throws NamingException + */ + public void testModifyRdnAndKeepOld() throws NamingException + { + + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Create a person, cn value is rdn + String oldCn = "Myra Ellen Amos"; + String oldRdn = "cn=" + oldCn; + Attributes attributes = this.getPersonAttributes("Amos", oldCn); + target.createSubcontext(oldRdn, attributes); + + // modify Rdn + String newCn = "Tori Amos"; + String newRdn = "cn=" + newCn; + target.addToEnvironment("java.naming.ldap.deleteRDN", "false"); + target.rename(oldRdn, newRdn); + + // Check, whether old entry does not exist + try { + target.lookup(oldRdn); + fail("Entry must not exist"); + } catch (NameNotFoundException ignored) { + // expected behaviour + assertTrue(true); + } + + // Check, whether new entry exists + DirContext tori = (DirContext) target.lookup(newRdn); + assertNotNull(tori); + + // Check values of cn + Attribute cn = tori.getAttributes("").get("cn"); + assertTrue(cn.contains(newCn)); + assertTrue(cn.contains(oldCn)); // old vaue is still there + assertEquals(2, cn.size()); + + // Remove entry (use new rdn) + target.unbind(newRdn); + } + + /** + * Modify Rdn of an entry, delete its old rdn value. Here, the rdn attribute + * cn has another value as well. + * + * @throws NamingException + */ + public void testModifyRdnAndDeleteOldVariant() throws NamingException + { + DirContext ctx = this.createContext(); + DirContext target = (DirContext) ctx.lookup(this.getTestContainerRdn()); + + // Create a person, cn value is rdn + String oldCn = "Myra Ellen Amos"; + String oldRdn = "cn=" + oldCn; + Attributes attributes = this.getPersonAttributes("Amos", oldCn); + + // add a second cn value + String alternateCn = "Myra E. Amos"; + Attribute cn = attributes.get("cn"); + cn.add(alternateCn); + assertEquals(2, cn.size()); + + target.createSubcontext(oldRdn, attributes); + + // modify Rdn + String newCn = "Tori Amos"; + String newRdn = "cn=" + newCn; + target.addToEnvironment("java.naming.ldap.deleteRDN", "true"); + target.rename(oldRdn, newRdn); + + // Check, whether old Entry does not exist anymore + try { + target.lookup(oldRdn); + fail("Entry must not exist"); + } catch (NameNotFoundException ignored) { + // expected behaviour + assertTrue(true); + } + + // Check, whether new Entry exists + DirContext tori = (DirContext) target.lookup(newRdn); + assertNotNull(tori); + + // Check values of cn + cn = tori.getAttributes("").get("cn"); + assertTrue(cn.contains(newCn)); + assertTrue(!cn.contains(oldCn)); // old value is gone + assertTrue(cn.contains(alternateCn)); // alternate value is still + // available + assertEquals(2, cn.size()); + + // Remove entry (use new rdn) + target.unbind(newRdn); + } +} Propchange: directory/testsuite/trunk/protocoltests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java ------------------------------------------------------------------------------ svn:executable = *