LDAP-248: DirContextAdapter now handles javax.naming.Name instances properly with regards to equality

LDAP-274: ODM now uses standard Spring converter (if present).
LDAP-275: Support for different Collection types in ODM
This commit is contained in:
Mattias Hellborg Arthursson
2013-10-24 10:25:22 +02:00
parent 27593ac973
commit c2abc9660e
25 changed files with 1422 additions and 133 deletions

View File

@@ -42,7 +42,7 @@ import static org.junit.Assert.fail;
/**
* Tests the DirContextAdapter class.
*
*
* @author Andreas Ronge
* @author Mattias Hellborg Arthursson
* @author Ulrik Sandberg
@@ -1114,7 +1114,7 @@ public class DirContextAdapterTest {
/**
* Test for LDAP-15: DirContextAdapter.setAttribute(). Verifies that setting
* an Attribute should modify updatedAttrs if in update mode.
*
*
* @throws NamingException
*/
@Test
@@ -1223,4 +1223,133 @@ public class DirContextAdapterTest {
DirContextAdapter tested = new DirContextAdapter("cn=john doe, ou=company");
assertEquals(LdapUtils.newLdapName("cn=john doe, ou=company"), tested.getDn());
}
@Test
public void testAddDnAttributeValueIdentical() {
BasicAttributes attributes = new BasicAttributes();
attributes.put("uniqueMember", "cn=john doe, ou=company");
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.addAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(0, modificationItems.length);
}
@Test
public void testAddDnAttributeSyntacticallyEqual() {
BasicAttributes attributes = new BasicAttributes();
attributes.put("uniqueMember", "cn=john doe,OU=company");
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.addAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(0, modificationItems.length);
}
@Test
public void testRemoveDnAttributeSyntacticallyEqual() throws NamingException {
BasicAttributes attributes = new BasicAttributes();
attributes.put("uniqueMember", "cn=john doe,OU=company");
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.removeAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(1, modificationItems.length);
ModificationItem modificationItem = modificationItems[0];
assertEquals(DirContext.REMOVE_ATTRIBUTE, modificationItem.getModificationOp());
assertEquals("uniqueMember", modificationItem.getAttribute().getID());
}
@Test
public void testRemoveOneOfSeveralDnAttributeSyntacticallyEqual() throws NamingException {
BasicAttributes attributes = new BasicAttributes();
BasicAttribute attribute = new BasicAttribute("uniqueMember", "cn=john doe,OU=company");
attribute.add("cn=jane doe, ou=company");
attributes.put(attribute);
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.removeAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(1, modificationItems.length);
ModificationItem modificationItem = modificationItems[0];
assertEquals(DirContext.REMOVE_ATTRIBUTE, modificationItem.getModificationOp());
assertEquals("uniqueMember", modificationItem.getAttribute().getID());
assertEquals("cn=john doe,OU=company", modificationItem.getAttribute().get());
}
@Test
public void testAddDnAttributeNewValue() throws NamingException {
BasicAttributes attributes = new BasicAttributes();
attributes.put("uniqueMember", "cn=john doe, ou=company");
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.addAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=jane doe, ou=company"));
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(1, modificationItems.length);
ModificationItem modificationItem = modificationItems[0];
assertEquals(DirContext.ADD_ATTRIBUTE, modificationItem.getModificationOp());
assertEquals("uniqueMember", modificationItem.getAttribute().getID());
assertEquals("cn=jane doe, ou=company", modificationItem.getAttribute().get());
}
@Test
public void testSetDnAttributeValueIdentical() {
BasicAttributes attributes = new BasicAttributes();
attributes.put("uniqueMember", "cn=john doe, ou=company");
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.setAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(0, modificationItems.length);
}
@Test
public void testSetDnAttributesValueIdentical() {
BasicAttributes attributes = new BasicAttributes();
attributes.put("uniqueMember", "cn=john doe, ou=company");
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.setAttributeValues("uniqueMember", new Object[]{LdapUtils.newLdapName("cn=john doe, ou=company")});
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(0, modificationItems.length);
}
@Test
public void testSetDnAttributesValuesOneNewEntry() throws NamingException {
BasicAttributes attributes = new BasicAttributes();
attributes.put("uniqueMember", "cn=john doe, ou=company");
DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
tested.setUpdateMode(true);
tested.setAttributeValues("uniqueMember", new Object[]{
LdapUtils.newLdapName("cn=john doe, ou=company"),
LdapUtils.newLdapName("cn=jane doe, ou=company")
});
ModificationItem[] modificationItems = tested.getModificationItems();
assertEquals(1, modificationItems.length);
ModificationItem modificationItem = modificationItems[0];
assertEquals(DirContext.ADD_ATTRIBUTE, modificationItem.getModificationOp());
assertEquals("uniqueMember", modificationItem.getAttribute().getID());
assertEquals("cn=jane doe, ou=company", modificationItem.getAttribute().get());
}
}

View File

@@ -0,0 +1,247 @@
package org.springframework.ldap.core;
import org.junit.Test;
import org.springframework.ldap.support.LdapUtils;
import javax.naming.NamingException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Mattias Hellborg Arthursson
*/
public class NameAwareAttributeTest {
@Test
public void testEqualsWithIdNotSame() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
NameAwareAttribute attr2 = new NameAwareAttribute("someOtherAttribute");
assertFalse(attr1.equals(attr2));
}
@Test
public void testEqualsWithSameIdNoValues() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
assertTrue(attr1.equals(attr2));
assertEquals(attr1.hashCode(), attr2.hashCode());
}
@Test
public void testEqualsUnorderedWithIdenticalAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add("value1");
attr1.add("value2");
attr1.add("value3");
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add("value1");
attr2.add("value2");
attr2.add("value3");
assertTrue(attr1.equals(attr2));
assertEquals(attr1.hashCode(), attr2.hashCode());
}
@Test
public void testEqualsUnorderedWithIdenticalArrayAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(new byte[]{1, 2, 3});
attr1.add(new byte[]{3, 2, 1});
attr1.add(new byte[]{1});
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(new byte[]{1, 2, 3});
attr2.add(new byte[]{3, 2, 1});
attr2.add(new byte[]{1});
assertTrue(attr1.equals(attr2));
assertEquals(attr1.hashCode(), attr2.hashCode());
}
@Test
public void testEqualsUnorderedWithDifferentOrderArrayAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(new byte[]{1, 2, 3});
attr1.add(new byte[]{3, 2, 1});
attr1.add(new byte[]{1});
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(new byte[]{3, 2, 1});
attr2.add(new byte[]{1});
attr2.add(new byte[]{1, 2, 3});
assertTrue(attr1.equals(attr2));
assertEquals(attr1.hashCode(), attr2.hashCode());
}
@Test
public void testEqualsUnorderedWithDifferentArrayAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(new byte[]{1, 2, 3});
attr1.add(new byte[]{3, 2, 2});
attr1.add(new byte[]{1});
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(new byte[]{1, 2, 3});
attr2.add(new byte[]{3, 2, 1});
attr2.add(new byte[]{1});
assertFalse(attr1.equals(attr2));
}
@Test
public void testEqualsUnorderedWithDifferentNumberOfArrayAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(new byte[]{1, 2, 3});
attr1.add(new byte[]{3, 2, 1});
attr1.add(new byte[]{1});
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(new byte[]{1, 2, 3});
attr2.add(new byte[]{1});
assertFalse(attr1.equals(attr2));
}
@Test
public void testEqualsOrderedWithIdenticalArrayAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute", true);
attr1.add(new byte[]{1, 2, 3});
attr1.add(new byte[]{3, 2, 1});
attr1.add(new byte[]{1});
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute", true);
attr2.add(new byte[]{1, 2, 3});
attr2.add(new byte[]{3, 2, 1});
attr2.add(new byte[]{1});
assertTrue(attr1.equals(attr2));
assertEquals(attr1.hashCode(), attr2.hashCode());
}
@Test
public void testEqualsOrderedWithArrayAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute", true);
attr1.add(new byte[]{1, 2, 3});
attr1.add(new byte[]{3, 2, 1});
attr1.add(new byte[]{1});
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute", true);
attr2.add(new byte[]{1, 2, 3});
attr2.add(new byte[]{3, 2, 1});
attr2.add(new byte[]{1});
assertTrue(attr1.equals(attr2));
assertEquals(attr1.hashCode(), attr2.hashCode());
}
@Test
public void testEqualsOrderedWithDifferentOrderArrayAttributes() {
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute", true);
attr1.add(new byte[]{1, 2, 3});
attr1.add(new byte[]{3, 2, 1});
attr1.add(new byte[]{1});
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute", true);
attr2.add(new byte[]{3, 2, 1});
attr2.add(new byte[]{1});
attr2.add(new byte[]{1, 2, 3});
assertFalse(attr1.equals(attr2));
}
@Test
public void testSameDistinguishedNameValue() throws NamingException {
String expectedName = "cn=John Doe,ou=People";
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(LdapUtils.newLdapName(expectedName));
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(LdapUtils.newLdapName(expectedName));
assertEquals(attr1, attr2);
assertEquals(attr1.hashCode(), attr2.hashCode());
assertEquals(expectedName, attr1.get());
assertEquals(expectedName, attr2.get());
}
@Test
public void testEqualDistinguishedNameValue() throws NamingException {
// The names here are syntactically equal, but differ in exact string representation
String expectedName1 = "cn=John Doe, OU=People";
String expectedName2 = "cn=John Doe,ou=People";
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(LdapUtils.newLdapName(expectedName1));
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(LdapUtils.newLdapName(expectedName2));
assertEquals(attr1, attr2);
assertEquals(attr1.hashCode(), attr2.hashCode());
assertEquals(expectedName1, attr1.get());
assertEquals(expectedName2, attr2.get());
}
@Test
public void testEqualDistinguishedNameValueUninitialized() throws NamingException {
// The names here are syntactically equal, but differ in exact string representation
String expectedName1 = "cn=John Doe, OU=People";
String expectedName2 = "cn=John Doe,ou=People";
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(expectedName1);
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(LdapUtils.newLdapName(expectedName2));
assertFalse(attr1.equals(attr2));
assertEquals(expectedName1, attr1.get());
assertEquals(expectedName2, attr2.get());
}
@Test
public void testEqualDistinguishedNameValueManuallyInitialized() throws NamingException {
// The names here are syntactically equal, but differ in exact string representation
String expectedName1 = "cn=John Doe, OU=People";
String expectedName2 = "cn=John Doe,ou=People";
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(expectedName1);
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(LdapUtils.newLdapName(expectedName2));
attr1.initValuesAsNames();
assertTrue(attr1.equals(attr2));
assertEquals(attr1.hashCode(), attr2.hashCode());
assertEquals(expectedName1, attr1.get());
assertEquals(expectedName2, attr2.get());
}
@Test
public void testUnequalDistinguishedNameValue() throws NamingException {
// The names here are syntactically equal, but differ in exact string representation
String expectedName1 = "cn=Jane Doe,ou=People";
String expectedName2 = "cn=John Doe,ou=People";
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(LdapUtils.newLdapName(expectedName1));
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(LdapUtils.newLdapName(expectedName2));
assertFalse(attr1.equals(attr2));
assertEquals(expectedName1, attr1.get());
assertEquals(expectedName2, attr2.get());
}
@Test
public void testComparingWDistinguishedNameValueWithInvalidName() throws NamingException {
// The names here are syntactically equal, but differ in exact string representation
String expectedName1 = "cn=Jane Doe,ou=People";
String expectedValue2 = "thisisnotavaliddn";
NameAwareAttribute attr1 = new NameAwareAttribute("someAttribute");
attr1.add(LdapUtils.newLdapName(expectedName1));
NameAwareAttribute attr2 = new NameAwareAttribute("someAttribute");
attr2.add(expectedValue2);
assertFalse(attr1.equals(attr2));
assertEquals(expectedName1, attr1.get());
assertEquals(expectedValue2, attr2.get());
}
}

View File

@@ -18,12 +18,14 @@ package org.springframework.ldap.core.support;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.NameAwareAttributes;
import org.springframework.ldap.support.LdapUtils;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InvalidNameException;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import java.util.Hashtable;
@@ -54,7 +56,7 @@ public class DefaultDirObjectFactoryTest {
@Test
public void testGetObjectInstance() throws Exception {
BasicAttributes expectedAttributes = new BasicAttributes();
Attributes expectedAttributes = new NameAwareAttributes();
expectedAttributes.put("someAttribute", "someValue");
DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, DN, null,
@@ -68,7 +70,7 @@ public class DefaultDirObjectFactoryTest {
@Test
public void testGetObjectInstance_CompositeName() throws Exception {
BasicAttributes expectedAttributes = new BasicAttributes();
Attributes expectedAttributes = new NameAwareAttributes();
expectedAttributes.put("someAttribute", "someValue");
CompositeName name = new CompositeName();
@@ -85,7 +87,7 @@ public class DefaultDirObjectFactoryTest {
@Test
public void testGetObjectInstance_nullObject() throws Exception {
BasicAttributes expectedAttributes = new BasicAttributes();
Attributes expectedAttributes = new NameAwareAttributes();
expectedAttributes.put("someAttribute", "someValue");
DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(null, DN, null, new Hashtable(),
@@ -97,7 +99,7 @@ public class DefaultDirObjectFactoryTest {
@Test
public void testGetObjectInstance_ObjectNotContext() throws Exception {
BasicAttributes expectedAttributes = new BasicAttributes();
Attributes expectedAttributes = new NameAwareAttributes();
expectedAttributes.put("someAttribute", "someValue");
DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(new Object(), DN, null,
@@ -114,7 +116,7 @@ public class DefaultDirObjectFactoryTest {
*/
@Test
public void testGetObjectInstance_BaseSet() throws Exception {
BasicAttributes expectedAttributes = new BasicAttributes();
Attributes expectedAttributes = new NameAwareAttributes();
expectedAttributes.put("someAttribute", "someValue");
when(contextMock2.getNameInNamespace()).thenReturn("dc=jayway, dc=se");

View File

@@ -51,7 +51,7 @@ public class DefaultObjectDirectoryMapperTest {
assertFalse(idAttribute.isBinary());
assertFalse(idAttribute.isDnAttribute());
assertFalse(idAttribute.isTransient());
assertFalse(idAttribute.isList());
assertFalse(idAttribute.isCollection());
assertField(entityData, "fullName", "cn", "cn", false, false, false);
assertField(entityData, "lastName", "sn", null, false, false, false);
@@ -115,7 +115,7 @@ public class DefaultObjectDirectoryMapperTest {
assertEquals(expectedBinary, attribute.isBinary());
assertEquals(expectedTransient, attribute.isTransient());
assertEquals(expectedList, attribute.isList());
assertEquals(expectedList, attribute.isCollection());
}
}
}