/* * 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.server; import java.util.Hashtable; 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.ldap.InitialLdapContext; import javax.naming.ldap.LdapContext; /** * Testcase with different modify operations on a person entry. Each includes a * single add op only. Created to demonstrate DIREVE-241 ("Adding an already * existing attribute value with a modify operation does not cause an error."). * * @author Apache Directory Project * @version $Rev$ */ public class ModifyAddTest extends AbstractServerTest { private LdapContext ctx = null; public static final String RDN = "cn=Tori Amos"; public static final String PERSON_DESCRIPTION = "an American singer-songwriter"; /** * Creation of required attributes of a person entry. */ 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; } /** * Create context and a person entry. */ public void setUp() throws Exception { super.setUp(); Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); env.put("java.naming.provider.url", "ldap://localhost:" + port + "/ou=system"); env.put("java.naming.security.principal", "uid=admin,ou=system"); env.put("java.naming.security.credentials", "secret"); env.put("java.naming.security.authentication", "simple"); ctx = new InitialLdapContext(env, null); assertNotNull(ctx); // Create a person with description Attributes attributes = this.getPersonAttributes("Amos", "Tori Amos"); attributes.put("description", "an American singer-songwriter"); ctx.createSubcontext(RDN, attributes); } /** * Remove person entry and close context. */ public void tearDown() throws Exception { ctx.unbind(RDN); ctx.close(); ctx.close(); ctx = null; super.tearDown(); } /** * Add a new attribute to a person entry. * * @throws NamingException */ public void testAddNewAttributeValue() throws NamingException { // Add telephoneNumber attribute String newValue = "1234567890"; Attributes attrs = new BasicAttributes("telephoneNumber", newValue); ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs); // Verify, that attribute value is added attrs = ctx.getAttributes(RDN); Attribute attr = attrs.get("telephoneNumber"); assertNotNull(attr); assertTrue(attr.contains(newValue)); assertEquals(1, attr.size()); } /** * Add a new attribute with two values. * * @throws NamingException */ public void testAddNewAttributeValues() throws NamingException { // Add telephoneNumber attribute String[] newValues = { "1234567890", "999999999" }; Attribute attr = new BasicAttribute("telephoneNumber"); attr.add(newValues[0]); attr.add(newValues[1]); Attributes attrs = new BasicAttributes(); attrs.put(attr); ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs); // Verify, that attribute values are present attrs = ctx.getAttributes(RDN); attr = attrs.get("telephoneNumber"); assertNotNull(attr); assertTrue(attr.contains(newValues[0])); assertTrue(attr.contains(newValues[1])); assertEquals(newValues.length, attr.size()); } /** * Add an additional value. * * @throws NamingException */ public void testAddAdditionalAttributeValue() throws NamingException { // A new description attribute value String newValue = "A new description for this person"; assertFalse(newValue.equals(PERSON_DESCRIPTION)); Attributes attrs = new BasicAttributes("description", newValue); ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs); // Verify, that attribute value is added attrs = ctx.getAttributes(RDN); Attribute attr = attrs.get("description"); assertNotNull(attr); assertTrue(attr.contains(newValue)); assertTrue(attr.contains(PERSON_DESCRIPTION)); assertEquals(2, attr.size()); } /** * Try to add an already existing attribute value. * * Expected behaviour: Modify operation fails with an * AttributeInUseException. Original LDAP Error code: 20 (Indicates that the * attribute value specified in a modify or add operation already exists as * a value for that attribute). * * @throws NamingException */ public void testAddExistingAttributeValue() throws NamingException { // Change description attribute Attributes attrs = new BasicAttributes("description", PERSON_DESCRIPTION); try { ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs); fail("Adding an already existing atribute value should fail."); } catch (AttributeInUseException e) { // expected behaviour } // Verify, that attribute is still there, and is the only one attrs = ctx.getAttributes(RDN); Attribute attr = attrs.get("description"); assertNotNull(attr); assertTrue(attr.contains(PERSON_DESCRIPTION)); assertEquals(1, attr.size()); } }