LDAP-138: DistinguishedName.toString now defaults to compact format - toString consults new System property to use the old
format.
This commit is contained in:
@@ -65,12 +65,29 @@ import org.springframework.ldap.support.ListComparator;
|
||||
* String dn = path.toString();
|
||||
* </pre>
|
||||
*
|
||||
* will render <code>uid=adam.skogman, ou=People, dc=jayway, dc=se</code>
|
||||
* will render <code>uid=adam.skogman,ou=People,dc=jayway,dc=se</code>.
|
||||
*
|
||||
* <p>
|
||||
* <b>NB:</b>As of version 1.3 the default toString representation of
|
||||
* DistinguishedName now defaults to a compact one, without spaces between the
|
||||
* respective RDNs. For backward compatibility, set the
|
||||
* {@link #SPACED_DN_FORMAT_PROPERTY} ({@value #SPACED_DN_FORMAT_PROPERTY}) to
|
||||
* <code>true</code>.
|
||||
* @author Adam Skogman
|
||||
* @author Mattias Arthursson
|
||||
*/
|
||||
public class DistinguishedName implements Name {
|
||||
/**
|
||||
* System property that will be inspected to determine whether toString will
|
||||
* format the DN with spaces after each comma or use a more compact
|
||||
* representation, i.e.:
|
||||
* <code>uid=adam.skogman, ou=People, dc=jayway, dc=se</code> rather than
|
||||
* <code>uid=adam.skogman,ou=People,dc=jayway,dc=se</code>. Default is
|
||||
* compact representation.
|
||||
* @since 1.3
|
||||
*/
|
||||
public static final String SPACED_DN_FORMAT_PROPERTY = "org.springframework.ldap.core.spacedDnFormat";
|
||||
|
||||
private static final boolean COMPACT = true;
|
||||
|
||||
private static final boolean NON_COMPACT = false;
|
||||
@@ -227,19 +244,29 @@ public class DistinguishedName implements Name {
|
||||
|
||||
/**
|
||||
* Get the String representation of this <code>DistinguishedName</code>.
|
||||
* Add a space after each comma, to make it readable.
|
||||
* Depending on the setting of property
|
||||
* <code>org.springframework.ldap.core.spacedDnFormat</code> a space will be
|
||||
* added after each comma, to make the result more readable. Default is
|
||||
* compact representation, i.e. without any spaces.
|
||||
*
|
||||
* @return a syntactically correct, properly escaped, nicely formatted
|
||||
* String representation of the <code>DistinguishedName</code>.
|
||||
* @return a syntactically correct, properly escaped String representation
|
||||
* of the <code>DistinguishedName</code>.
|
||||
* @see #SPACED_DN_FORMAT_PROPERTY
|
||||
*/
|
||||
public String toString() {
|
||||
return format(NON_COMPACT);
|
||||
String spacedFormatting = System.getProperty(SPACED_DN_FORMAT_PROPERTY);
|
||||
if (StringUtils.isBlank(spacedFormatting)) {
|
||||
return format(COMPACT);
|
||||
}
|
||||
else {
|
||||
return format(NON_COMPACT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the compact String representation of this
|
||||
* <code>DistinguishedName</code>. Add no space after each comma, to make
|
||||
* it compact.
|
||||
* <code>DistinguishedName</code>. Add no space after each comma, to make it
|
||||
* compact.
|
||||
*
|
||||
* @return a syntactically correct, properly escaped String representation
|
||||
* of the <code>DistinguishedName</code>.
|
||||
|
||||
@@ -57,7 +57,7 @@ public class DistinguishedNameEditorTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testGetAsText() throws Exception {
|
||||
String expectedDn = "dc=jayway, dc=se";
|
||||
String expectedDn = "dc=jayway,dc=se";
|
||||
tested.setValue(new DistinguishedName(expectedDn));
|
||||
String text = tested.getAsText();
|
||||
assertEquals(expectedDn, text);
|
||||
|
||||
@@ -91,7 +91,7 @@ public class DistinguishedNameTest extends TestCase {
|
||||
path.remove(1);
|
||||
path.remove(3);
|
||||
|
||||
assertEquals("cn=john.doe, ou=Some Company, ou=G, ou=M", path.toString());
|
||||
assertEquals("cn=john.doe,ou=Some Company,ou=G,ou=M", path.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,7 +123,7 @@ public class DistinguishedNameTest extends TestCase {
|
||||
|
||||
path1.append(path2);
|
||||
|
||||
assertEquals("Append failed", "ou=baz, ou=foo, ou=bar", path1.toString());
|
||||
assertEquals("Append failed", "ou=baz,ou=foo,ou=bar", path1.toString());
|
||||
}
|
||||
|
||||
public void testPrepend() {
|
||||
@@ -132,7 +132,7 @@ public class DistinguishedNameTest extends TestCase {
|
||||
|
||||
path1.prepend(path2);
|
||||
|
||||
assertEquals("Append failed", "ou=foo, ou=bar, cn=fie, ou=baz", path1.toString());
|
||||
assertEquals("Append failed", "ou=foo,ou=bar,cn=fie,ou=baz", path1.toString());
|
||||
}
|
||||
|
||||
public void testEquals() throws Exception {
|
||||
@@ -315,7 +315,7 @@ public class DistinguishedNameTest extends TestCase {
|
||||
|
||||
path1.addAll(path2);
|
||||
|
||||
assertEquals("AddAll failed", "ou=baz, ou=foo, ou=bar", path1.toString());
|
||||
assertEquals("AddAll failed", "ou=baz,ou=foo,ou=bar", path1.toString());
|
||||
}
|
||||
|
||||
public void testAddAll_Index() throws InvalidNameException {
|
||||
@@ -324,21 +324,21 @@ public class DistinguishedNameTest extends TestCase {
|
||||
|
||||
path1.addAll(1, path2);
|
||||
|
||||
assertEquals("AddAll failed", "ou=foo, ou=baz, ou=bar", path1.toString());
|
||||
assertEquals("AddAll failed", "ou=foo,ou=baz,ou=bar", path1.toString());
|
||||
}
|
||||
|
||||
public void testAdd() throws InvalidNameException {
|
||||
DistinguishedName path1 = new DistinguishedName("ou=foo, ou=bar");
|
||||
path1.add("ou=baz");
|
||||
|
||||
assertEquals("Add failed", "ou=baz, ou=foo, ou=bar", path1.toString());
|
||||
assertEquals("Add failed", "ou=baz,ou=foo,ou=bar", path1.toString());
|
||||
}
|
||||
|
||||
public void testAdd_Index() throws InvalidNameException {
|
||||
DistinguishedName path1 = new DistinguishedName("ou=foo, ou=bar");
|
||||
path1.add(1, "ou=baz");
|
||||
|
||||
assertEquals("Add failed", "ou=foo, ou=baz, ou=bar", path1.toString());
|
||||
assertEquals("Add failed", "ou=foo,ou=baz,ou=bar", path1.toString());
|
||||
}
|
||||
|
||||
public void testToUrl() {
|
||||
@@ -482,7 +482,7 @@ public class DistinguishedNameTest extends TestCase {
|
||||
DistinguishedName tested = new DistinguishedName("dc=mycompany,dc=com");
|
||||
tested.append("ou", "company1").append("cn", "john doe");
|
||||
|
||||
assertEquals("cn=john doe, ou=company1, dc=mycompany, dc=com", tested.toString());
|
||||
assertEquals("cn=john doe,ou=company1,dc=mycompany,dc=com", tested.toString());
|
||||
}
|
||||
|
||||
public void testUnmodifiableDistinguishedName() throws Exception {
|
||||
@@ -506,4 +506,18 @@ public class DistinguishedNameTest extends TestCase {
|
||||
DistinguishedName name = new DistinguishedName("cn=foo \r bar");
|
||||
assertNotNull(name);
|
||||
}
|
||||
|
||||
public void testToStringCompact() {
|
||||
try {
|
||||
DistinguishedName name = new DistinguishedName("cn=john doe, ou=company");
|
||||
// First check the default
|
||||
assertEquals("cn=john doe,ou=company", name.toString());
|
||||
System.setProperty(DistinguishedName.SPACED_DN_FORMAT_PROPERTY, "true");
|
||||
assertEquals("cn=john doe, ou=company", name.toString());
|
||||
}
|
||||
finally {
|
||||
// Always restore the system setting
|
||||
System.setProperty(DistinguishedName.SPACED_DN_FORMAT_PROPERTY, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ public class DefaultDirObjectFactoryTest extends TestCase {
|
||||
verify();
|
||||
|
||||
assertEquals("ou=some unit", adapter.getDn().toString());
|
||||
assertEquals("ou=some unit, dc=jayway, dc=se", adapter.getNameInNamespace());
|
||||
assertEquals("ou=some unit,dc=jayway,dc=se", adapter.getNameInNamespace());
|
||||
assertEquals(expectedAttributes, adapter.getAttributes());
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ public class DefaultDirObjectFactoryTest extends TestCase {
|
||||
DefaultDirObjectFactory tested = new DefaultDirObjectFactory();
|
||||
DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, "");
|
||||
|
||||
assertEquals("ou=People, o=JNDITutorial", result.getDn().toString());
|
||||
assertEquals("ou=People,o=JNDITutorial", result.getDn().toString());
|
||||
assertEquals("ldap://localhost:389", result.getReferralUrl().toString());
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ public class DefaultDirObjectFactoryTest extends TestCase {
|
||||
DefaultDirObjectFactory tested = new DefaultDirObjectFactory();
|
||||
DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, "");
|
||||
|
||||
assertEquals("ou=People, o=JNDITutorial", result.getDn().toString());
|
||||
assertEquals("ou=People,o=JNDITutorial", result.getDn().toString());
|
||||
assertEquals("ldaps://localhost:389", result.getReferralUrl().toString());
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public class DefaultTempEntryRenamingStrategyTest extends TestCase {
|
||||
DefaultTempEntryRenamingStrategy tested = new DefaultTempEntryRenamingStrategy();
|
||||
|
||||
Name result = tested.getTemporaryName(expectedOriginalName);
|
||||
assertEquals("cn=john doe_temp, ou=somecompany, c=SE", result
|
||||
assertEquals("cn=john doe_temp,ou=somecompany,c=SE", result
|
||||
.toString());
|
||||
assertNotSame(expectedOriginalName, result);
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class DefaultTempEntryRenamingStrategyTest extends TestCase {
|
||||
DefaultTempEntryRenamingStrategy tested = new DefaultTempEntryRenamingStrategy();
|
||||
|
||||
Name result = tested.getTemporaryName(expectedOriginalName);
|
||||
assertEquals("cn=john doe_temp+sn=doe, ou=somecompany, c=SE", result
|
||||
assertEquals("cn=john doe_temp+sn=doe,ou=somecompany,c=SE", result
|
||||
.toString());
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class DifferentSubtreeTempEntryRenamingStrategyTest extends TestCase {
|
||||
Name result = tested.getTemporaryName(originalName);
|
||||
|
||||
// Verify result
|
||||
assertEquals("cn=john doe" + nextSequenceNo + ", ou=tempEntries",
|
||||
assertEquals("cn=john doe" + nextSequenceNo + ",ou=tempEntries",
|
||||
result.toString());
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ public class InvalidBackslashITest extends AbstractLdapTemplateIntegrationTest {
|
||||
@Override
|
||||
protected Object doMapFromContext(DirContextOperations ctx) {
|
||||
DistinguishedName dn = (DistinguishedName) ctx.getDn();
|
||||
assertEquals("cn=Some\\\\Person6, ou=company1, c=Sweden", dn.toString());
|
||||
assertEquals("cn=Some\\\\Person6,ou=company1,c=Sweden", dn.toString());
|
||||
assertEquals("Some\\Person6", dn.getLdapRdn("cn").getValue());
|
||||
return new Object();
|
||||
}
|
||||
|
||||
@@ -195,16 +195,16 @@ public class LdapTemplateLookupITest extends AbstractLdapTemplateIntegrationTest
|
||||
public void testLookup_GetNameInNamespace_Plain() {
|
||||
DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person2, ou=company1,c=Sweden");
|
||||
|
||||
assertEquals("cn=Some Person2, ou=company1, c=Sweden", result.getDn().toString());
|
||||
assertEquals("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", result.getNameInNamespace());
|
||||
assertEquals("cn=Some Person2,ou=company1,c=Sweden", result.getDn().toString());
|
||||
assertEquals("cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se", result.getNameInNamespace());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookup_GetNameInNamespace_MultiRdn() {
|
||||
DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person+sn=Person, ou=company1,c=Norway");
|
||||
|
||||
assertEquals("cn=Some Person+sn=Person, ou=company1, c=Norway", result.getDn().toString());
|
||||
assertEquals("cn=Some Person+sn=Person, ou=company1, c=Norway, dc=jayway, dc=se", result.getNameInNamespace());
|
||||
assertEquals("cn=Some Person+sn=Person,ou=company1,c=Norway", result.getDn().toString());
|
||||
assertEquals("cn=Some Person+sn=Person,ou=company1,c=Norway,dc=jayway,dc=se", result.getNameInNamespace());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -59,8 +59,8 @@ public class LdapTemplateNoBaseSuffixITest extends AbstractLdapTemplateIntegrati
|
||||
assertEquals("Some Person2", result.getStringAttribute("cn"));
|
||||
assertEquals("Person2", result.getStringAttribute("sn"));
|
||||
assertEquals("Sweden, Company1, Some Person2", result.getStringAttribute("description"));
|
||||
assertEquals("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", result.getDn().toString());
|
||||
assertEquals("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", result.getNameInNamespace());
|
||||
assertEquals("cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se", result.getDn().toString());
|
||||
assertEquals("cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se", result.getNameInNamespace());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,9 +84,9 @@ public class LdapTemplateNoBaseSuffixITest extends AbstractLdapTemplateIntegrati
|
||||
|
||||
assertEquals("Some Person4", result.getStringAttribute("cn"));
|
||||
assertEquals("Person4", result.getStringAttribute("sn"));
|
||||
assertEquals("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se", result.getDn().toString());
|
||||
assertEquals("cn=Some Person4,ou=company1,c=Sweden,dc=jayway,dc=se", result.getDn().toString());
|
||||
|
||||
tested.unbind("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se");
|
||||
tested.unbind("cn=Some Person4,ou=company1,c=Sweden,dc=jayway,dc=se");
|
||||
try {
|
||||
tested.lookup("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se");
|
||||
fail("NameNotFoundException expected");
|
||||
|
||||
Reference in New Issue
Block a user