package tests; 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; /** * Test case to verify DIREVE-216. An appropriate jndi.properties is needed to * run it. * * @author szoerner */ public class AddObjectClassesToEntryTest extends TestCase { public static final String RDN = "cn=The Person"; DirContext ctx = null; /** * Create an entry for a person. */ public void setUp() throws NamingException { ctx = new InitialDirContext(); // Create a person Attributes attributes = new BasicAttributes(); Attribute attribute = new BasicAttribute("objectClass"); attribute.add("top"); attribute.add("person"); attributes.put(attribute); attributes.put("cn", "The Person"); attributes.put("sn", "Person"); attributes.put("description", "this is a person"); DirContext person = ctx.createSubcontext(RDN, attributes); assertNotNull(person); } /** * Remove the person. */ public void tearDown() throws NamingException { ctx.unbind(RDN); } /** * Just a little test to check wether the person is created correctly after * setup. * * @throws NamingException */ public void testSetUpTearDown() throws NamingException { DirContext person = (DirContext) ctx.lookup(RDN); assertNotNull(person); // Check object classes Attributes attributes = person.getAttributes(""); Attribute ocls = attributes.get("objectClass"); String[] expectedOcls = { "top", "person" }; for (int i = 0; i < expectedOcls.length; i++) { String name = expectedOcls[i]; assertTrue("object class " + name + " is present", ocls .contains(name)); } } /** * This is the original defect as in JIRA DIREVE-216. * * @throws NamingException */ public void testAddObjectClasses() throws NamingException { // modify object classes, add two more Attributes attributes = new BasicAttributes(); Attribute ocls = new BasicAttribute("objectClass"); ocls.add("organizationalPerson"); ocls.add("inetOrgPerson"); attributes.put(ocls); DirContext person = (DirContext) ctx.lookup(RDN); person.modifyAttributes("", DirContext.ADD_ATTRIBUTE, attributes); // Read again from directory person = (DirContext) ctx.lookup(RDN); attributes = person.getAttributes(""); Attribute newOcls = attributes.get("objectClass"); String[] expectedOcls = { "top", "person", "organizationalPerson", "inetOrgPerson" }; for (int i = 0; i < expectedOcls.length; i++) { String name = expectedOcls[i]; assertTrue("object class " + name + " is present", newOcls .contains(name)); } } /** * This changes a single attribute value. Just as a reference. * * @throws NamingException */ public void testModifyDescription() throws NamingException { String newDescription = "More info on the user ..."; // modify object classes, add two more Attributes attributes = new BasicAttributes(); Attribute desc = new BasicAttribute("description", newDescription); attributes.put(desc); DirContext person = (DirContext) ctx.lookup(RDN); person.modifyAttributes("", DirContext.REPLACE_ATTRIBUTE, attributes); // Read again from directory person = (DirContext) ctx.lookup(RDN); attributes = person.getAttributes(""); Attribute newDesc = attributes.get("description"); assertTrue("new Description", newDesc.contains(newDescription)); } }