diff --git a/spring-ldap/changelog.txt b/spring-ldap/changelog.txt
index 9c4022fc..9444f7b6 100644
--- a/spring-ldap/changelog.txt
+++ b/spring-ldap/changelog.txt
@@ -13,6 +13,9 @@ http://www.ietf.org/rfc/rfc2696.txt
Changes in version 1.2.2 (XXX 2008)
-------------------------------------------
+* Added a method DistinguishedName.toCompactString that returns a more
+ compact String representation without blanks. (LDAP-91)
+
* Improved search methods that take handlers so that they will work
with handlers that use ContextMappers, eg ContextMapperCallbackHandler.
(LDAP-107)
diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java b/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java
index ed7d211b..94bbff8e 100644
--- a/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java
+++ b/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java
@@ -71,6 +71,10 @@ import org.springframework.ldap.support.ListComparator;
* @author Mattias Arthursson
*/
public class DistinguishedName implements Name {
+ private static final boolean COMPACT = true;
+
+ private static final boolean NON_COMPACT = false;
+
private static final long serialVersionUID = 3514344371999042586L;
/**
@@ -223,12 +227,25 @@ public class DistinguishedName implements Name {
/**
* Get the String representation of this DistinguishedName.
+ * Add a space after each comma, to make it readable.
+ *
+ * @return a syntactically correct, properly escaped, nicely formatted
+ * String representation of the DistinguishedName.
+ */
+ public String toString() {
+ return format(NON_COMPACT);
+ }
+
+ /**
+ * Get the compact String representation of this
+ * DistinguishedName. Add no space after each comma, to make
+ * it compact.
*
* @return a syntactically correct, properly escaped String representation
* of the DistinguishedName.
*/
- public String toString() {
- return encode();
+ public String toCompactString() {
+ return format(COMPACT);
}
/**
@@ -239,7 +256,10 @@ public class DistinguishedName implements Name {
* @return the LDAP path.
*/
public String encode() {
+ return format(NON_COMPACT);
+ }
+ private String format(boolean compact) {
// empty path
if (names.size() == 0)
return "";
@@ -252,12 +272,18 @@ public class DistinguishedName implements Name {
buffer.append(rdn.getLdapEncoded());
// add comma, except in last iteration
- if (i.hasPrevious())
- buffer.append(", ");
+ if (i.hasPrevious()) {
+ if (compact) {
+ buffer.append(",");
+ }
+ else {
+ buffer.append(", ");
+
+ }
+ }
}
return buffer.toString();
-
}
/**
diff --git a/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java b/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
index 44c33413..58e27bf4 100644
--- a/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
+++ b/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
@@ -37,6 +37,16 @@ import com.gargoylesoftware.base.testing.EqualsTester;
* @author Mattias Arthursson
*/
public class DistinguishedNameTest extends TestCase {
+
+ public void testToCompactString() throws Exception {
+ DistinguishedName path = new DistinguishedName("cn=foo, ou=bar");
+ assertEquals("cn=foo,ou=bar", path.toCompactString());
+ }
+
+ public void testEncode() throws Exception {
+ DistinguishedName path = new DistinguishedName("cn=foo, ou=bar");
+ assertEquals("cn=foo, ou=bar", path.encode());
+ }
public void testDistinguishedName_CompositeWithSlash() throws Exception {
Name testPath = new CompositeName("cn=foo\\/bar");
@@ -94,6 +104,26 @@ public class DistinguishedNameTest extends TestCase {
assertEquals("cn=john.doe, ou=Some Company, ou=G, ou=M", path.toString());
}
+ public void testRemoveLast() {
+
+ String testPath = "cn=john.doe, OU=Users,OU=Some Company,OU=G,OU=I,OU=M";
+ DistinguishedName path = new DistinguishedName(testPath);
+
+ path.removeLast();
+
+ assertEquals("ou=Users, ou=Some Company, ou=G, ou=I, ou=M", path.toString());
+ }
+
+ public void testRemoveFirstWithName() {
+
+ String testPath = "cn=john.doe, OU=Users,OU=Some Company,OU=G,OU=I,OU=M";
+ DistinguishedName path = new DistinguishedName(testPath);
+
+ path.removeFirst(new DistinguishedName("OU=G,OU=I,OU=M"));
+
+ assertEquals("cn=john.doe, ou=Users, ou=Some Company", path.toString());
+ }
+
/**
* Tests parsing and toString.
*/
@@ -185,8 +215,10 @@ public class DistinguishedNameTest extends TestCase {
DistinguishedName path2 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com");
DistinguishedName ending2 = new DistinguishedName("ou=EU, o=example.com");
- assertFalse(path1.endsWith(ending1));
- assertFalse(path2.endsWith(ending2));
+ assertFalse("single rdn", path1.endsWith(ending1));
+ assertFalse("multiple rdns", path2.endsWith(ending2));
+ assertFalse("too long name", ending2.endsWith(path2));
+ assertFalse("empty name", path2.endsWith(DistinguishedName.EMPTY_PATH));
}
public void testGetAll() throws Exception {