Fixed LDAP-91: Spaces after commas in distinguished names.

This commit is contained in:
Ulrik Sandberg
2008-04-30 20:40:55 +00:00
parent 59ceb62cdd
commit 70a147b21e
3 changed files with 68 additions and 7 deletions

View File

@@ -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)

View File

@@ -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 <code>DistinguishedName</code>.
* Add a space after each comma, to make it readable.
*
* @return a syntactically correct, properly escaped, nicely formatted
* String representation of the <code>DistinguishedName</code>.
*/
public String toString() {
return format(NON_COMPACT);
}
/**
* Get the compact String representation of this
* <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>.
*/
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();
}
/**

View File

@@ -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 {