directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szoer...@apache.org
Subject svn commit: r307188 [2/2] - in /directory/testsuite/trunk/ldaptests: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/ldap/ src/main/java/org/apache/ldap/testsuite/ src/main/java/org/apache/ldap/tes...
Date Fri, 07 Oct 2005 20:27:30 GMT
Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/BasicModifyReplaceTests.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/BasicModifyReplaceTests.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/BasicModifyReplaceTests.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/BasicModifyReplaceTests.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,134 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.jndi.ops.modify;
+
+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 org.apache.ldap.testsuite.ldaptests.jndi.BaseProtocolTest;
+
+/**
+ * Tests for the modify operations with JNDI, which only include replace
+ * modifications.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+public class BasicModifyReplaceTests extends BaseProtocolTest
+{
+
+    DirContext ctx;
+
+    DirContext target;
+
+    public static final String RDN = "cn=Tori Amos";
+
+    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;
+    }
+
+    public void setUp() throws NamingException
+    {
+        super.setUp();
+
+        ctx = this.createContext();
+        target = (DirContext) ctx.lookup(this.getTestContainerRdn());
+
+        // Create a person with description
+        Attributes attributes = this.getPersonAttributes("Amos", "Tori Amos");
+        attributes.put("description", "an American singer-songwriter");
+
+        target.createSubcontext(RDN, attributes);
+    }
+
+    public void tearDown() throws NamingException
+    {
+        target.unbind(RDN);
+        target.close();
+
+        ctx.close();
+        ctx = null;
+
+        super.tearDown();
+    }
+
+    /**
+     * Replace a not required attribute.
+     * 
+     * Expected result: After successful deletion, attribute is not present in
+     * entry
+     * 
+     * @throws NamingException
+     */
+    public void testReplaceNotRequiredAttribute() throws NamingException
+    {
+
+        // Change description attribute
+        String newValue = "A new description for this person";
+        Attribute attr = new BasicAttribute("description", newValue);
+        Attributes attrs = new BasicAttributes();
+        attrs.put(attr);
+        target.modifyAttributes(RDN, DirContext.REPLACE_ATTRIBUTE, attrs);
+
+        // Verify, that attribute value changed
+        attrs = target.getAttributes(RDN);
+        attr = attrs.get("description");
+        assertTrue(attr.contains(newValue));
+        assertEquals(1, attr.size());
+
+    }
+
+    /**
+     * Try to Replace RDN.
+     * 
+     * @throws NamingException
+     */
+    public void testTryToReplaceRdn() throws NamingException
+    {
+
+        // Change RDN attribute cn
+        String newValue = "New Value";
+        Attribute attr = new BasicAttribute("cn", newValue);
+        Attributes attrs = new BasicAttributes();
+        attrs.put(attr);
+        try {
+            target.modifyAttributes(RDN, DirContext.REPLACE_ATTRIBUTE, attrs);
+        } catch (NamingException e) {
+            // expected behaviour
+            // TODO: refine Exception
+        }
+
+        // Verify, that attribute value has not changed
+        attrs = target.getAttributes(RDN);
+        attr = attrs.get("cn");
+        assertEquals(1, attr.size());
+        assertFalse(attr.contains(newValue));
+    }
+}
\ No newline at end of file

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/MixedModifyFailureTests.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/MixedModifyFailureTests.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/MixedModifyFailureTests.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modify/MixedModifyFailureTests.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,181 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.jndi.ops.modify;
+
+import javax.naming.InvalidNameException;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.AttributeInUseException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
+import javax.naming.directory.SchemaViolationException;
+
+import org.apache.ldap.testsuite.ldaptests.jndi.BaseProtocolTest;
+
+/**
+ * Tests with several modification items within one modify operation, which
+ * cause an error. Goal is to chcek whether the modify operation is atomic.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+public class MixedModifyFailureTests extends BaseProtocolTest
+{
+
+    DirContext start;
+
+    DirContext ctx;
+
+    public static final String RDN = "cn=Tori Amos";
+
+    public static final String PERSON_DESCRIPTION = "an American singer-songwriter";
+
+    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;
+    }
+
+    public void setUp() throws NamingException
+    {
+        super.setUp();
+
+        start = this.createContext();
+        ctx = (DirContext) start.lookup(this.getTestContainerRdn());
+
+        // Create a person with description
+        Attributes attributes = this.getPersonAttributes("Amos", "Tori Amos");
+        attributes.put("description", PERSON_DESCRIPTION);
+
+        ctx.createSubcontext(RDN, attributes);
+    }
+
+    public void tearDown() throws NamingException
+    {
+        ctx.unbind(RDN);
+        ctx.close();
+
+        start.close();
+        start = null;
+
+        super.tearDown();
+    }
+
+    /**
+     * Try to add a new attribute (allowed) and to replace the RDN attribute
+     * (forbidden).
+     * 
+     * @throws NamingException
+     */
+    public void testAddAndIllegalReplace() throws NamingException
+    {
+
+        // Add telephoneNumber attribute
+        String newValue = "1234567890";
+        Attribute telephoneNumber = new BasicAttribute("telephoneNumber", newValue);
+        ModificationItem modAdd = new ModificationItem(DirContext.ADD_ATTRIBUTE, telephoneNumber);
+
+        // Try to replace a desciption value, which does not exist
+        Attribute cn = new BasicAttribute("cn", "XXX");
+        ModificationItem modReplace = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
cn);
+
+        // execute modify: 1. add, 2. replace
+        ModificationItem[] mods = new ModificationItem[] { modAdd, modReplace };
+        try {
+            ctx.modifyAttributes(RDN, mods);
+            fail("mofify operation should fail.");
+        } catch (SchemaViolationException e) {
+            // Expected behaviour
+        } catch (InvalidNameException e) {
+            // Expected behaviour
+        }
+
+        // Verify, that no telephone attribute was created, and cn has bot been
+        // changed
+        Attributes attrs = ctx.getAttributes(RDN);
+        assertNull(attrs.get("telephoneNumber"));
+        cn = attrs.get("cn");
+        assertNotNull(cn);
+        assertTrue(cn.contains("Tori Amos"));
+
+        // Mix the sequence of modification items: 1. replace, 2. add
+        mods = new ModificationItem[] { modReplace, modAdd };
+        try {
+            ctx.modifyAttributes(RDN, mods);
+            fail("modify operation should fail.");
+        } catch (SchemaViolationException e) {
+            // Expected behaviour
+        } catch (InvalidNameException e) {
+            // Expected behaviour
+        }
+
+        // Verify again, that no telephone attribute was created, and cn has bot
+        // been changed
+        attrs = ctx.getAttributes(RDN);
+        assertNull(attrs.get("telephoneNumber"));
+        cn = attrs.get("cn");
+        assertNotNull(cn);
+        assertTrue(cn.contains("Tori Amos"));
+    }
+
+    /**
+     * Try to remove an attribute (allowed) and add an attribute value twice
+     * (forbidden) within one modify op. Expected behaviour is that the modify
+     * op fails and the entry is not changed.
+     * 
+     * @throws NamingException
+     */
+    public void testRemoveAndDuplicateAdd() throws NamingException
+    {
+
+        // Remove description attribute
+        Attribute description = new BasicAttribute("description");
+        ModificationItem modRemove = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, description);
+
+        // Try to add a new desciption value
+        Attribute telephoneNumber = new BasicAttribute("telephoneNumber", "01234567890");
+        ModificationItem modAdd = new ModificationItem(DirContext.ADD_ATTRIBUTE, telephoneNumber);
+
+        // execute modify: 1. remove, 2. add, 3. same add
+        ModificationItem[] mods = new ModificationItem[] { modRemove, modAdd, modAdd };
+        try {
+            ctx.modifyAttributes(RDN, mods);
+            fail("modify operation should fail.");
+        } catch (AttributeInUseException e) {
+            // Expected behaviour
+        }
+
+        // Verify, that description has not been removed, and telephoneNumber
+        // has not been added
+        Attributes attrs = ctx.getAttributes(RDN);
+        System.err.println(attrs);
+        description = attrs.get("description");
+        assertNotNull(description);
+        assertTrue(description.contains(PERSON_DESCRIPTION));
+        assertNull(attrs.get("telephoneNumber"));
+    }
+}
\ No newline at end of file

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/AllTests.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/AllTests.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/AllTests.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/AllTests.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.jndi.ops.modifydn;
+
+import junit.framework.TestSuite;
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+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;
+    }
+}

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/BasicModifyDnTests.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/BasicModifyDnTests.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/BasicModifyDnTests.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/BasicModifyDnTests.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,188 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.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 org.apache.ldap.testsuite.ldaptests.jndi.BaseProtocolTest;
+
+/**
+ * Basic Tests for the Modify DN operation.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+public class BasicModifyDnTests extends BaseProtocolTest
+{
+
+    /**
+     * Creates attributes for a inetOrgPerson 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 getInetOrgPersonAttributes(String sn, String cn)
+    {
+        Attributes attributes = new BasicAttributes();
+        Attribute attribute = new BasicAttribute("objectClass");
+        attribute.add("top");
+        attribute.add("person");
+        attribute.add("organizationalPerson");
+        attribute.add("inetOrgPerson");
+        attributes.put(attribute);
+        attributes.put("cn", cn);
+        attributes.put("sn", sn);
+
+        return attributes;
+    }
+
+    /**
+     * Modify DN of an entry, changing RDN from cn to uid.
+     * 
+     * @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 uidVal = "tamos";
+        String oldRdn = "cn=" + cnVal;
+        Attributes attributes = this.getInetOrgPersonAttributes(snVal, cnVal);
+        attributes.put("uid", uidVal);
+        target.createSubcontext(oldRdn, attributes);
+
+        // modify Rdn
+        String newRdn = "uid=" + uidVal;
+        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 uid
+        Attribute cn = tori.getAttributes("").get("cn");
+        assertTrue(cn.contains(cnVal));
+        assertEquals(1, cn.size());
+        Attribute uid = tori.getAttributes("").get("uid");
+        assertTrue(uid.contains(uidVal));
+        assertEquals(1, uid.size());
+
+        ctx.close();
+    }
+
+    /**
+     * Modify Rdn of an entry, changing it from cn to uid, 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 uidVal = "tamos";
+        String oldRdn = "cn=" + cnVal;
+        Attributes attributes = this.getInetOrgPersonAttributes(snVal, cnVal);
+        target.createSubcontext(oldRdn, attributes);
+
+        // modify Rdn, but try delete old val
+        try {
+            String newRdn = "uid=" + uidVal;
+            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 uid. 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;
+        String uidVal = "tamos";
+        Attributes attributes = this.getInetOrgPersonAttributes(snVal, cnVal);
+        target.createSubcontext(oldRdn, attributes);
+
+        // create same person with sn as RDN
+        String newRdn = "uid=" + uidVal;
+        attributes.put("uid", uidVal);
+        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
+        }
+
+        // Check that old entry still exists
+        DirContext tori = (DirContext) target.lookup(oldRdn);
+        assertNotNull(tori);
+
+        ctx.close();
+    }
+}
\ No newline at end of file

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/modifydn/ModifyDnSameAttributeTests.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,203 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.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 org.apache.ldap.testsuite.ldaptests.jndi.BaseProtocolTest;
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+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;
+    }
+
+    protected Attributes getOrganizationalUnitAttributes(String ou)
+    {
+        Attributes attributes = new BasicAttributes();
+        Attribute attribute = new BasicAttribute("objectClass");
+        attribute.add("top");
+        attribute.add("organizationalUnit");
+        attributes.put(attribute);
+        attributes.put("ou", ou);
+
+        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 an organizational unit, ou value is rdn
+        String oldOu = "Judean People's Front";
+        String oldRdn = "ou=" + oldOu;
+        Attributes attributes = this.getOrganizationalUnitAttributes(oldOu);
+        target.createSubcontext(oldRdn, attributes);
+
+        // modify Rdn
+        String newOu = "People's Front of Judea";
+        String newRdn = "ou=" + newOu;
+        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 ouEntry = (DirContext) target.lookup(newRdn);
+        assertNotNull(ouEntry);
+
+        // Check values of ou
+        Attribute ou = ouEntry.getAttributes("").get("ou");
+        assertTrue(ou.contains(newOu));
+        assertTrue(!ou.contains(oldOu)); // old vaue is gone
+        assertEquals(1, ou.size());
+
+        ctx.close();
+    }
+
+    /**
+     * 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 an organizational unit, ou value is rdn
+        String oldOu = "Judean People's Front";
+        String oldRdn = "ou=" + oldOu;
+        Attributes attributes = this.getOrganizationalUnitAttributes(oldOu);
+        target.createSubcontext(oldRdn, attributes);
+
+        // modify Rdn
+        String newOu = "People's Front of Judea";
+        String newRdn = "ou=" + newOu;
+        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 ouEntry = (DirContext) target.lookup(newRdn);
+        assertNotNull(ouEntry);
+
+        // Check values of cn
+        Attribute ou = ouEntry.getAttributes("").get("ou");
+        assertTrue(ou.contains(newOu));
+        assertTrue(ou.contains(oldOu)); // old value is still present
+        assertEquals(2, ou.size());
+
+        ouEntry.close();
+        target.close();
+        ctx.close();
+    }
+
+    /**
+     * 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 an organizational unit, ou value is rdn
+        String oldOu = "Judean People's Front";
+        String oldRdn = "ou=" + oldOu;
+        Attributes attributes = this.getOrganizationalUnitAttributes(oldOu);
+
+        // add a second ou value
+        String alternateOu = "J.P.F.";
+        Attribute ou = attributes.get("ou");
+        ou.add(alternateOu);
+        assertEquals(2, ou.size());
+
+        target.createSubcontext(oldRdn, attributes);
+
+        // modify Rdn
+        String newOu = "People's Front of Judea";
+        String newRdn = "ou=" + newOu;
+        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 ouEntry = (DirContext) target.lookup(newRdn);
+        assertNotNull(ouEntry);
+
+        // Check values of cn
+        ou = ouEntry.getAttributes("").get("ou");
+        assertTrue(ou.contains(newOu));
+        assertTrue(!ou.contains(oldOu)); // old value is gone
+        assertTrue(ou.contains(alternateOu)); // alternate value is still
+        assertEquals(2, ou.size());
+    }
+}

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/AllTests.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/AllTests.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/AllTests.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/AllTests.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.jndi.ops.search;
+
+import junit.framework.TestSuite;
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+public class AllTests extends TestSuite
+{
+
+    public static TestSuite suite()
+    {
+        TestSuite suite = new TestSuite("Search");
+
+        suite.addTestSuite(SearchScopeTest.class);
+        suite.addTestSuite(IllegalSearchFilterTest.class);
+
+        return suite;
+    }
+}

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/IllegalSearchFilterTest.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/IllegalSearchFilterTest.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/IllegalSearchFilterTest.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/IllegalSearchFilterTest.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,94 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.jndi.ops.search;
+
+import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InvalidSearchFilterException;
+import javax.naming.directory.SearchControls;
+
+import org.apache.ldap.testsuite.ldaptests.jndi.BaseProtocolTest;
+
+/**
+ * Tests for the search operations with JNDI which use illegal search filters.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+public class IllegalSearchFilterTest extends BaseProtocolTest
+{
+
+    private DirContext ctx;
+
+    public void setUp() throws NamingException
+    {
+        super.setUp();
+
+        ctx = this.createContext();
+        ctx = (DirContext) ctx.lookup(this.getTestContainerRdn());
+    }
+
+    public void tearDown() throws NamingException
+    {
+        ctx.close();
+        ctx = null;
+
+        super.tearDown();
+    }
+
+    public void testFiltersWithSyntaxErrors() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+        String[] filters = { null, "", ",", "&", "1", "This is no filter" };
+
+        for (int i = 0; i < filters.length; i++) {
+            String filter = filters[i];
+
+            // Search
+            try {
+                ctx.search("", filter, ctls);
+                fail("Filter [" + filter + "] has syntax errors and should cause an error.");
+            } catch (InvalidSearchFilterException e) {
+                // Expected behaviour
+            }
+        }
+    }
+
+    public void testFilterWithUnbalancedParenthesis() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+        String[] filters = { "((sn=Amos)", "(&(sn=Amos)(cn=Tori Amos)", "(!(sn=Amos)"
};
+
+        for (int i = 0; i < filters.length; i++) {
+            String filter = filters[i];
+
+            // Search
+            try {
+                ctx.search("", filter, ctls);
+                fail("Filter [" + filter + "] has unbalanced parenthesis and should cause
an error.");
+            } catch (InvalidSearchFilterException e) {
+                // Expected behaviour
+            }
+        }
+    }
+}
\ No newline at end of file

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchFilterSimpleTest.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchFilterSimpleTest.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchFilterSimpleTest.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchFilterSimpleTest.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,174 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.jndi.ops.search;
+
+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.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+import org.apache.ldap.testsuite.ldaptests.jndi.BaseProtocolTest;
+
+/**
+ * Tests for the search operations with JNDI.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+public class SearchFilterSimpleTest extends BaseProtocolTest
+{
+
+    private DirContext ctx;
+
+    static final String[] firstNames = { "Tori", "Heather", "Alanis", "Kate", "Fiona" };
+
+    static final String[] lastNames = { "Amos", "Nova", "Morisette", "Bush", "Apple" };
+
+    protected Attributes getPersonAttributes(Person p)
+    {
+        Attributes attributes = new BasicAttributes();
+        Attribute attribute = new BasicAttribute("objectClass");
+        attribute.add("top");
+        attribute.add("person");
+        attributes.put(attribute);
+        attributes.put("sn", p.sn);
+        attributes.put("cn", p.cn);
+
+        return attributes;
+    }
+
+    public void setUp() throws NamingException
+    {
+        super.setUp();
+
+        ctx = this.createContext();
+        ctx = (DirContext) ctx.lookup(this.getTestContainerRdn());
+
+        for (int i = 0; i < lastNames.length; ++i) {
+            Person p = new Person(firstNames[i], lastNames[i]);
+            ctx.createSubcontext("cn=" + p.cn, getPersonAttributes(p));
+        }
+    }
+
+    public void tearDown() throws NamingException
+    {
+        ctx.close();
+        ctx = null;
+
+        super.tearDown();
+    }
+
+    public void testSimpleFilter() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+        NamingEnumeration enm = ctx.search("", "sn=Amos", ctls);
+        assertTrue(enm.hasMore());
+        int numberOfEntries = 0;
+        while (enm.hasMore()) {
+            SearchResult entry = (SearchResult) enm.next();
+            Attributes attrs = entry.getAttributes();
+            Attribute sn = attrs.get("sn");
+
+            assertEquals(1, sn.size());
+            assertTrue(sn.contains("Amos"));
+
+            numberOfEntries++;
+        }
+
+        assertEquals("number of entries in result", 1, numberOfEntries);
+    }
+
+    public void testFilterWithNot() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+        NamingEnumeration enm = ctx.search("", "(!(sn=Amos))", ctls);
+        assertTrue(enm.hasMore());
+        int numberOfEntries = 0;
+        while (enm.hasMore()) {
+            SearchResult entry = (SearchResult) enm.next();
+            Attributes attrs = entry.getAttributes();
+            Attribute sn = attrs.get("sn");
+
+            assertFalse(sn.contains("Amos"));
+
+            numberOfEntries++;
+        }
+        assertEquals("number of entries in result", firstNames.length - 1, numberOfEntries);
+    }
+
+    public void testFilterWithAnd() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+
+        NamingEnumeration enm = ctx.search("", "(&(objectClass=person)(sn=Amos))", ctls);
+        assertTrue(enm.hasMore());
+        int numberOfEntries = 0;
+        while (enm.hasMore()) {
+            SearchResult entry = (SearchResult) enm.next();
+            Attributes attrs = entry.getAttributes();
+            Attribute sn = attrs.get("sn");
+            assertTrue(sn.contains("Amos"));
+            numberOfEntries++;
+        }
+        assertEquals("number of entries in result", 1, numberOfEntries);
+    }
+
+    public void testFilterWithOr() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+
+        NamingEnumeration enm = ctx.search("", "(|(cn=Kate Bush)(sn=Amos))", ctls);
+        assertTrue(enm.hasMore());
+        int numberOfEntries = 0;
+        while (enm.hasMore()) {
+            SearchResult entry = (SearchResult) enm.next();
+            Attributes attrs = entry.getAttributes();
+            Attribute sn = attrs.get("sn");
+            assertTrue(sn.contains("Amos") || sn.contains("Bush"));
+            numberOfEntries++;
+        }
+        assertEquals("number of entries in result", 2, numberOfEntries);
+    }
+
+    class Person
+    {
+
+        Person(String firstName, String lastName) {
+            this.sn = lastName;
+            this.cn = firstName + " " + lastName;
+        }
+
+        String sn;
+
+        String cn;
+    }
+}
\ No newline at end of file

Added: directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchScopeTest.java
URL: http://svn.apache.org/viewcvs/directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchScopeTest.java?rev=307188&view=auto
==============================================================================
--- directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchScopeTest.java
(added)
+++ directory/testsuite/trunk/ldaptests/src/main/java/org/apache/ldap/testsuite/ldaptests/jndi/ops/search/SearchScopeTest.java
Fri Oct  7 13:27:08 2005
@@ -0,0 +1,163 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.testsuite.ldaptests.jndi.ops.search;
+
+import java.util.HashSet;
+import java.util.Set;
+
+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.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+import org.apache.ldap.testsuite.ldaptests.jndi.BaseProtocolTest;
+
+/**
+ * Tests for the search operations with JNDI.
+ * 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: $
+ */
+public class SearchScopeTest extends BaseProtocolTest
+{
+
+    private DirContext ctx;
+
+    private static final String MATCH_ALL_FILTER = "(objectClass=*)";
+
+    private static final String UNITS_NAME = "units";
+
+    private static final String UNITS_RDN = "ou=" + UNITS_NAME;
+
+    private static final int SUBTREE_LEVEL_DEPTH = 2;
+
+    private static final int ENTRIES_PER_LEVEL = 3;
+
+    protected Attributes getOrganizationalUnitAttributes(String ou)
+    {
+        Attributes attributes = new BasicAttributes();
+        Attribute attribute = new BasicAttribute("objectClass");
+        attribute.add("top");
+        attribute.add("organizationalUnit");
+        attributes.put(attribute);
+        attributes.put("ou", ou);
+
+        return attributes;
+    }
+
+    protected void createSubtrees(DirContext target, int depth, int maxDepth, int count)
throws NamingException
+    {
+        if (depth < maxDepth) {
+            for (int i = 0; i < count; ++i) {
+                String ou = "unit_" + depth + "_" + (i);
+                Attributes attr = this.getOrganizationalUnitAttributes(ou);
+                DirContext entry = target.createSubcontext("ou=" + ou, attr);
+                createSubtrees(entry, depth + 1, maxDepth, count);
+            }
+        }
+
+    }
+
+    public void setUp() throws NamingException
+    {
+        super.setUp();
+
+        ctx = this.createContext();
+        ctx = (DirContext) ctx.lookup(this.getTestContainerRdn());
+
+        DirContext units = ctx.createSubcontext(UNITS_RDN, getOrganizationalUnitAttributes(UNITS_NAME));
+        createSubtrees(units, 0, SUBTREE_LEVEL_DEPTH, ENTRIES_PER_LEVEL);
+
+    }
+
+    public void tearDown() throws NamingException
+    {
+        ctx.close();
+        ctx = null;
+
+        super.tearDown();
+    }
+
+    public void testBaseScopeExisting() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
+        ctls.setReturningAttributes(new String[] { "ou" });
+
+        // Search
+        NamingEnumeration enm = ctx.search(UNITS_RDN, MATCH_ALL_FILTER, ctls);
+
+        // Exactly one entry in result
+        assertTrue(enm.hasMore());
+        SearchResult sr = (SearchResult) enm.next();
+        assertFalse(enm.hasMore());
+
+        // The right entry in result
+        Attributes attrs = sr.getAttributes();
+        Attribute ou = attrs.get("ou");
+        assertTrue(ou.contains(UNITS_NAME));
+    }
+
+    public void testBaseScopeNotExistingBase() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
+        ctls.setReturningAttributes(new String[] { "ou" });
+
+        try {
+            ctx.search(UNITS_RDN + "_", MATCH_ALL_FILTER, ctls);
+            fail("Search on a not existing base should fail");
+        } catch (NameNotFoundException nnfe) {
+            // expected behaviour
+        }
+    }
+
+    public void testOneLevelScope() throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+        ctls.setReturningAttributes(new String[] { "ou" });
+
+        // Search op
+        //
+        NamingEnumeration enm = ctx.search(UNITS_RDN, MATCH_ALL_FILTER, ctls);
+
+        // Analyse result data
+        assertTrue(enm.hasMore());
+        Set entries = new HashSet();
+        int count = 0;
+        while (enm.hasMore()) {
+            count++;
+            SearchResult sr = (SearchResult) enm.next();
+            Attribute ou = sr.getAttributes().get("ou");
+            entries.add(ou.get());
+        }
+        assertEquals("number of entries in result", ENTRIES_PER_LEVEL, count);
+        assertEquals("number of different entries in result", ENTRIES_PER_LEVEL, entries.size());
+        assertFalse("base included in result", entries.contains(UNITS_NAME));
+    }
+}
\ No newline at end of file



Mime
View raw message