Fix for LDAP-71: Added getLdapRdn(String key) and getValue(String key) in DistinguishedName, and getKey(), getValue() and getValue(String key) in LdapRdn.

This commit is contained in:
Mattias Arthursson
2007-06-24 17:31:48 +00:00
parent 9da903942b
commit f879ee21a4
5 changed files with 176 additions and 2 deletions

View File

@@ -43,7 +43,7 @@ import org.springframework.ldap.support.ListComparator;
* the most significant part be in position 0.
* <p>
* Example:
*
*
* <dl>
* <dt>The path</dt>
* <dd>uid=adam.skogman, ou=People, ou=EU</dd>
@@ -56,6 +56,7 @@ import org.springframework.ldap.support.ListComparator;
* </dl>
*
* Example:
*
* <pre>
* DistinguishedName path = new DistinguishedName();
* path.addLast(&quot;uid&quot;, person.getUid());
@@ -176,6 +177,44 @@ public class DistinguishedName implements Name {
return (LdapRdn) names.get(index);
}
/**
* Get the LdapRdn with the specified key. If there are several Rdns with
* the same key, the first one found (in order of significance) will be
* returned.
*
* @param key
* Attribute name of the LdapRdn to retrieve.
* @return the LdapRdn with the requested key.
* @throws IllegalArgumentException
* if no Rdn matches the given key.
*/
public LdapRdn getLdapRdn(String key) {
for (Iterator iter = names.iterator(); iter.hasNext();) {
LdapRdn rdn = (LdapRdn) iter.next();
if (StringUtils.equals(rdn.getKey(), key)) {
return rdn;
}
}
throw new IllegalArgumentException("No Rdn with the requested key: '"
+ key + "'");
}
/**
* Get the value of the RdnComponent with the specified key (Attribute
* value). If there are several Rdns with the same key, the value of the
* first one found (in order of significance) will be returned.
*
* @param key
* Attribute name of the LdapRdn to retrieve.
* @return the value.
* @throws IllegalArgumentException
* if no Rdn matches the given key.
*/
public String getValue(String key) {
return getLdapRdn(key).getValue();
}
/**
* Get the name list.
*

View File

@@ -22,6 +22,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.ldap.BadLdapGrammarException;
import org.springframework.ldap.support.ListComparator;
@@ -195,4 +196,54 @@ public class LdapRdn implements Serializable, Comparable {
public String toString() {
return getLdapEncoded();
}
/**
* Get the value of this LdapRdn. Note that if this Rdn is multi-value the
* first value will be returned. E.g. for the Rdn
* <code>cn=john doe+sn=doe</code>, the return value would be
* <code>john doe</code>.
*
* @return the (first) value of this LdapRdn.
* @throws IndexOutOfBoundsException
* if there is no components in this Rdn.
*/
public String getValue() {
return getComponent().getValue();
}
/**
* Get the key of this LdapRdn. Note that if this Rdn is multi-value the
* first key will be returned. E.g. for the Rdn
* <code>cn=john doe+sn=doe</code>, the return value would be
* <code>cn</code>.
*
* @return the (first) key of this LdapRdn.
* @throws IndexOutOfBoundsException
* if there is no components in this Rdn.
*/
public String getKey() {
return getComponent().getKey();
}
/**
* Get the value of the LdapComponent with the specified key (Attribute
* name).
*
* @param key
* the key
* @return the value.
* @throws IllegalArgumentException
* if there is no component with the specified key.
*/
public String getValue(String key) {
for (Iterator iter = components.iterator(); iter.hasNext();) {
LdapRdnComponent component = (LdapRdnComponent) iter.next();
if (StringUtils.equals(component.getKey(), key)) {
return component.getValue();
}
}
throw new IllegalArgumentException("No RdnComponent with the key "
+ key);
}
}

View File

@@ -75,18 +75,40 @@ public class LdapRdnComponent implements Comparable, Serializable {
}
}
/**
* Get the key (Attribute name) of this component.
*
* @return the key.
*/
public String getKey() {
return key;
}
/**
* Set the key (Attribute name) of this component.
*
* @param key
* the key.
*/
public void setKey(String key) {
this.key = key;
}
/**
* Get the (Attribute) value of this component.
*
* @return the value.
*/
public String getValue() {
return value;
}
/**
* Set the (Attribute) value of this component.
*
* @param value
* the value.
*/
public void setValue(String value) {
this.value = value;
}

View File

@@ -448,6 +448,43 @@ public class DistinguishedNameTest extends TestCase {
assertTrue(result < 0);
}
public void testGetLdapRdnForKey() throws Exception {
DistinguishedName dn = new DistinguishedName(
"cn=john doe, ou=Some company, c=SE");
LdapRdn ldapRdn = dn.getLdapRdn("ou");
assertEquals(new LdapRdn("ou=Some company"), ldapRdn);
}
public void testGetLdapRdnForKeyNoMatchingKeyThrowsException()
throws Exception {
DistinguishedName dn = new DistinguishedName(
"cn=john doe, ou=Some company, c=SE");
try {
dn.getLdapRdn("nosuchkey");
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testGetValue() throws Exception {
DistinguishedName dn = new DistinguishedName(
"cn=john doe, ou=Some company, c=SE");
String value = dn.getValue("ou");
assertEquals("Some company", value);
}
public void testGetValueNoMatchingKeyThrowsException() throws Exception {
DistinguishedName dn = new DistinguishedName(
"cn=john doe, ou=Some company, c=SE");
try {
dn.getValue("nosuchkey");
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void test_longDN() throws InvalidNameException {
DistinguishedName name = new DistinguishedName("");
assertNotNull(name);

View File

@@ -19,7 +19,6 @@ package org.springframework.ldap.core;
import junit.framework.TestCase;
import org.springframework.ldap.BadLdapGrammarException;
import org.springframework.ldap.core.LdapRdn;
import com.gargoylesoftware.base.testing.EqualsTester;
@@ -37,6 +36,8 @@ public class LdapRdnTest extends TestCase {
assertEquals("foo", rdn.getComponent().getKey());
assertEquals("bar", rdn.getComponent().getValue());
assertEquals("foo=bar", rdn.getComponent().getLdapEncoded());
assertEquals("foo", rdn.getKey());
assertEquals("bar", rdn.getValue());
}
public void testLdapRdn_parse_spaces() {
@@ -144,6 +145,30 @@ public class LdapRdnTest extends TestCase {
assertEquals("cn=John Doe", rdn.getComponent(0).encodeLdap());
assertEquals("sn=Doe", rdn.getComponent(1).encodeLdap());
assertEquals("cn=John Doe+sn=Doe", rdn.getLdapEncoded());
assertEquals("cn", rdn.getKey());
assertEquals("John Doe", rdn.getValue());
assertEquals("John Doe", rdn.getValue("cn"));
assertEquals("Doe", rdn.getValue("sn"));
}
public void testGetValueNoKeyWithCorrectValue() {
LdapRdn tested = new LdapRdn("cn=john doe");
try {
tested.getValue("sn");
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testGetValueNoComponents() {
LdapRdn tested = new LdapRdn();
try {
tested.getValue("sn");
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testEquals() throws Exception {