package modify; import java.util.Hashtable; import javax.naming.Context; 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.InitialDirContext; import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import junit.framework.TestCase; /** * Testcase to demonstrate DIRSERVER-636 ("Performing two modifications on a * single attribute within a single modify operation fails."). */ public class ModifyOpsTest extends TestCase { DirContext ctx = null; protected Attributes getPersonAttributes(String sn, String cn) { Attributes attrs = new BasicAttributes(); Attribute ocls = new BasicAttribute("objectClass"); ocls.add("top"); ocls.add("person"); attrs.put(ocls); attrs.put("cn", cn); attrs.put("sn", sn); return attrs; } protected void setUp() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:10389/dc=example,dc=com"); env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); env.put(Context.SECURITY_CREDENTIALS, "secret"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); ctx = new InitialDirContext(env); } protected void tearDown() throws NamingException { ctx.close(); } /** * Create a person entry and perform a modify op. */ public void testMultiValuedRdn() throws NamingException { // Create a person entry Attributes attrs = getPersonAttributes("Bush", "Kate Bush"); String rdn = "cn=Kate Bush"; ctx.createSubcontext(rdn, attrs); // Add a decsription with two values String[] descriptions = { "Kate Bush is a British singer-songwriter.", "She has become one of the most influential female artists of the twentieth century." }; Attribute desc1 = new BasicAttribute("description"); desc1.add(descriptions[0]); desc1.add(descriptions[1]); ModificationItem addModOp = new ModificationItem( DirContext.ADD_ATTRIBUTE, desc1); Attribute desc2 = new BasicAttribute("description"); desc2.add(descriptions[1]); ModificationItem delModOp = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, desc2); ctx.modifyAttributes(rdn, new ModificationItem[] { addModOp, delModOp }); SearchControls sctls = new SearchControls(); sctls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(sn=Bush)"; String base = ""; // Check entry NamingEnumeration enm = ctx.search(base, filter, sctls); assertTrue(enm.hasMore()); while (enm.hasMore()) { SearchResult sr = (SearchResult) enm.next(); attrs = sr.getAttributes(); Attribute desc = sr.getAttributes().get("description"); assertNotNull(desc); assertEquals(1, desc.size()); assertTrue(desc.contains(descriptions[0])); } // Remove the person entry ctx.destroySubcontext(rdn); } }