package forjira; 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 junit.framework.TestCase; /** * Demonstration of DIREVE-230 (Deletion of RDN and objectClass attribute) * * @author szoerner */ public class ObjectClassesRemovalTest extends TestCase { DirContext ctx; static String RDN_PERSON = "cn=Charly Brown"; public void testRemoveObjectClasses() throws NamingException { // 1. create person entry Attributes personAttrs = this.getPersonAttributes("Brown", "Charly Brown"); ctx.createSubcontext(RDN_PERSON, personAttrs); // 2. Try to remove attribute objectClass Attribute attr = new BasicAttribute("objectclass"); Attributes attrs = new BasicAttributes(); attrs.put(attr); try { ctx.modifyAttributes(RDN_PERSON, DirContext.REMOVE_ATTRIBUTE, attrs); System.err.println("Deletion of attribute objectClass should fail."); } catch (NamingException e) { // expected behaviour } // 3. Try to lookup entry DirContext cbrown = (DirContext) ctx.lookup(RDN_PERSON); assertNotNull(cbrown); System.err.println(cbrown.getNameInNamespace()); System.err.println(cbrown.getAttributes("")); // 4. Remove entry ctx.unbind(RDN_PERSON); } protected void setUp() throws Exception { super.setUp(); ctx = new InitialDirContext(); } protected void tearDown() throws Exception { ctx.close(); super.tearDown(); } 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; } }