/* * 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.directory.ModificationItem; import javax.naming.ldap.InitialLdapContext; import javax.naming.ldap.LdapContext; /** * Add duplicate attribute values to an entry via modify operation (two single * adds within one op). The test case chcks whether the appropriate error * occurrs (error code 20, "Attribute or value exists"). * * Created to demonstrate DIREVE-268 ("Adding a duplicate attribute value should * cause an error."). * * @author Apache Directory Project * @version $Rev$ */ public class ModifyAddDuplicateValueTest extends AbstractServerTest { private LdapContext ctx = null; public static final String CN_VALUE = "Heather Nova"; public static final String SN_VALUE = "Nova"; public static final String RDN = "cn=" + CN_VALUE; /** * Create context. */ 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); ctx.createSubcontext(RDN, getPersonAttributes(SN_VALUE, CN_VALUE)); } /** * Close context. */ public void tearDown() throws Exception { ctx.unbind(RDN); ctx.close(); ctx = null; super.tearDown(); } /** * 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; } /** * Try to add a duplicate attribute value to an entry, where this attribute * is already present (objectclass in this case). Expected behaviour is that * the modify operation causes an error (error code 20, "Attribute or value * exists"). * * @throws NamingException */ public void testAddDuplicateValueToExistingAttribute() throws NamingException { // modify object classes, add a new value twice Attribute ocls = new BasicAttribute("objectClass", "organizationalPerson"); ModificationItem[] modItems = new ModificationItem[2]; modItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, ocls); modItems[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, ocls); try { ctx.modifyAttributes(RDN, modItems); fail("Adding a duplicate attribute value should cause an error."); } catch (AttributeInUseException ex) { // Expected behaviour } // Check, whether attribute objectClass is unchanged Attributes attrs = ctx.getAttributes(RDN); ocls = attrs.get("objectClass"); assertEquals(ocls.size(), 2); assertTrue(ocls.contains("top")); assertTrue(ocls.contains("person")); } /** * Try to add a duplicate attribute value to an entry, where this attribute * is not present. Expected behaviour is that the modify operation causes an * error (error code 20, "Attribute or value exists"). * * @throws NamingException */ public void testAddDuplicateValueToNewAttribute() throws NamingException { // add the same description value twice Attribute desc = new BasicAttribute("description", "an singer-songwriter"); ModificationItem[] modItems = new ModificationItem[2]; modItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, desc); modItems[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, desc); try { ctx.modifyAttributes(RDN, modItems); fail("Adding a duplicate attribute value should cause an error."); } catch (AttributeInUseException ex) { // Expected behaviour ex.printStackTrace(); } // Check, whether attribute description is still not present Attributes attrs = ctx.getAttributes(RDN); assertNull(attrs.get("description")); } }