diff --git a/core/src/main/java/org/springframework/ldap/core/DistinguishedName.java b/core/src/main/java/org/springframework/ldap/core/DistinguishedName.java
index 94bbff8e..17d3bfe4 100644
--- a/core/src/main/java/org/springframework/ldap/core/DistinguishedName.java
+++ b/core/src/main/java/org/springframework/ldap/core/DistinguishedName.java
@@ -65,12 +65,29 @@ import org.springframework.ldap.support.ListComparator;
* String dn = path.toString();
*
*
- * will render uid=adam.skogman, ou=People, dc=jayway, dc=se
+ * will render uid=adam.skogman,ou=People,dc=jayway,dc=se.
*
+ *
+ * NB: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
+ * true.
* @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.:
+ * uid=adam.skogman, ou=People, dc=jayway, dc=se rather than
+ * uid=adam.skogman,ou=People,dc=jayway,dc=se. 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 DistinguishedName.
- * Add a space after each comma, to make it readable.
+ * Depending on the setting of property
+ * org.springframework.ldap.core.spacedDnFormat 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 DistinguishedName.
+ * @return a syntactically correct, properly escaped String representation
+ * of the DistinguishedName.
+ * @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
- * DistinguishedName. Add no space after each comma, to make
- * it compact.
+ * DistinguishedName. Add no space after each comma, to make it
+ * compact.
*
* @return a syntactically correct, properly escaped String representation
* of the DistinguishedName.
diff --git a/core/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorTest.java b/core/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorTest.java
index c912e566..7008bb86 100644
--- a/core/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorTest.java
+++ b/core/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorTest.java
@@ -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);
diff --git a/core/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java b/core/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
index 253b0580..2f110f1b 100644
--- a/core/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
+++ b/core/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
@@ -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, "");
+ }
+ }
}
diff --git a/core/src/test/java/org/springframework/ldap/core/support/DefaultDirObjectFactoryTest.java b/core/src/test/java/org/springframework/ldap/core/support/DefaultDirObjectFactoryTest.java
index e1947d24..1dec0505 100644
--- a/core/src/test/java/org/springframework/ldap/core/support/DefaultDirObjectFactoryTest.java
+++ b/core/src/test/java/org/springframework/ldap/core/support/DefaultDirObjectFactoryTest.java
@@ -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());
}
diff --git a/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DefaultTempEntryRenamingStrategyTest.java b/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DefaultTempEntryRenamingStrategyTest.java
index e2812cc4..99cd7de2 100644
--- a/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DefaultTempEntryRenamingStrategyTest.java
+++ b/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DefaultTempEntryRenamingStrategyTest.java
@@ -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());
}
diff --git a/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DifferentSubtreeTempEntryRenamingStrategyTest.java b/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DifferentSubtreeTempEntryRenamingStrategyTest.java
index 86a27f99..7519b97b 100644
--- a/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DifferentSubtreeTempEntryRenamingStrategyTest.java
+++ b/core/src/test/java/org/springframework/ldap/transaction/compensating/support/DifferentSubtreeTempEntryRenamingStrategyTest.java
@@ -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());
}
diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/InvalidBackslashITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/InvalidBackslashITest.java
index cb29a76f..3f4ba2c4 100644
--- a/test/integration-tests/src/test/java/org/springframework/ldap/InvalidBackslashITest.java
+++ b/test/integration-tests/src/test/java/org/springframework/ldap/InvalidBackslashITest.java
@@ -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();
}
diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java
index 86d12d69..efd80c91 100644
--- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java
+++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java
@@ -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());
}
/**
diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java
index 8f63d8b2..dd3a8ecc 100644
--- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java
+++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java
@@ -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");