IN PROGRESS - issue LDAP-33: Remove package dependency cycle between ldap.core and ldap.support
http://opensource.atlassian.com/projects/spring/browse/LDAP-33
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.core;
|
||||
|
||||
import javax.naming.Name;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.BasicAttributes;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests that serve as regression tests for bugs that have been fixed.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class DirContextAdapterBugTest extends TestCase {
|
||||
|
||||
public void testResetAttributeValuesNotReportedAsModifications() {
|
||||
BasicAttributes attrs = new BasicAttributes("myattr", "a");
|
||||
attrs.get("myattr").add("b");
|
||||
attrs.get("myattr").add("c");
|
||||
UpdateAdapter ctx = new UpdateAdapter(attrs, new DistinguishedName());
|
||||
|
||||
ctx.setAttributeValues("myattr", new String[] { "a", "b" });
|
||||
ctx.setAttributeValues("myattr", new String[] { "a", "b", "c" });
|
||||
|
||||
assertEquals(0, ctx.getModificationItems().length);
|
||||
}
|
||||
|
||||
public void testResetAttributeValuesSameLengthNotReportedAsModifications() {
|
||||
BasicAttributes attrs = new BasicAttributes("myattr", "a");
|
||||
attrs.get("myattr").add("b");
|
||||
attrs.get("myattr").add("c");
|
||||
UpdateAdapter ctx = new UpdateAdapter(attrs, new DistinguishedName());
|
||||
|
||||
ctx.setAttributeValues("myattr", new String[] { "a", "b", "d" });
|
||||
ctx.setAttributeValues("myattr", new String[] { "a", "b", "c" });
|
||||
|
||||
assertEquals(0, ctx.getModificationItems().length);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test starts with an array with a null value in it (because that's
|
||||
* how BasicAttributes will do it), changes to <code>[a]</code>, and then
|
||||
* changes to <code>null</code>. The current code interprets this as a
|
||||
* change and will replace the original array with an empty array.
|
||||
*
|
||||
* TODO Is this correct behaviour?
|
||||
*/
|
||||
public void testResetNullAttributeValuesReportedAsModifications() {
|
||||
BasicAttributes attrs = new BasicAttributes("myattr", null);
|
||||
UpdateAdapter ctx = new UpdateAdapter(attrs, new DistinguishedName());
|
||||
|
||||
ctx.setAttributeValues("myattr", new String[] { "a" });
|
||||
ctx.setAttributeValues("myattr", null);
|
||||
|
||||
assertEquals(1, ctx.getModificationItems().length);
|
||||
}
|
||||
|
||||
public void testResetNullAttributeValueNotReportedAsModification() throws Exception {
|
||||
BasicAttributes attrs = new BasicAttributes("myattr", "b");
|
||||
UpdateAdapter ctx = new UpdateAdapter(attrs, new DistinguishedName());
|
||||
|
||||
ctx.setAttributeValue("myattr", "a");
|
||||
ctx.setAttributeValue("myattr", "b");
|
||||
|
||||
assertEquals(0, ctx.getModificationItems().length);
|
||||
}
|
||||
|
||||
private static class UpdateAdapter extends DirContextAdapter {
|
||||
public UpdateAdapter(Attributes attrs, Name dn) {
|
||||
super(attrs, dn);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,692 @@
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.core;
|
||||
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.BasicAttribute;
|
||||
import javax.naming.directory.BasicAttributes;
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.ModificationItem;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Tests the DirContextAdapter class.
|
||||
*
|
||||
* @author Andreas Ronge
|
||||
* @author Mattias Arthursson
|
||||
*/
|
||||
public class DirContextAdapterTest extends TestCase {
|
||||
private static final DistinguishedName BASE_NAME = new DistinguishedName(
|
||||
"dc=jayway, dc=se");
|
||||
|
||||
private static final DistinguishedName DUMMY_NAME = new DistinguishedName(
|
||||
"c=SE, dc=jayway, dc=se");
|
||||
|
||||
private DirContextAdapter classUnderTest;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
classUnderTest = new DirContextAdapter();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testSetUpdateMode() throws Exception {
|
||||
assertFalse(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setUpdateMode(true);
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setUpdateMode(false);
|
||||
assertFalse(classUnderTest.isUpdateMode());
|
||||
}
|
||||
|
||||
public void testGetModificationItems() throws Exception {
|
||||
ModificationItem[] items = classUnderTest.getModificationItems();
|
||||
assertEquals(0, items.length);
|
||||
classUnderTest.setUpdateMode(true);
|
||||
assertEquals(0, items.length);
|
||||
}
|
||||
|
||||
public void testAlwaysReplace() throws Exception {
|
||||
ModificationItem[] items = classUnderTest.getModificationItems();
|
||||
assertEquals(0, items.length);
|
||||
classUnderTest.setUpdateMode(true);
|
||||
assertEquals(0, items.length);
|
||||
}
|
||||
|
||||
public void testGetStringAttributeNotExists() throws Exception {
|
||||
String s = classUnderTest.getStringAttribute("does not exist");
|
||||
assertNull(s);
|
||||
}
|
||||
|
||||
public void testGetStringAttributeExists() throws Exception {
|
||||
final Attributes attrs = new BasicAttributes();
|
||||
attrs.put(new BasicAttribute("abc", "def"));
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(attrs, null);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
String s = classUnderTest.getStringAttribute("abc");
|
||||
assertEquals("def", s);
|
||||
}
|
||||
|
||||
public void testGetStringAttributesExists() throws Exception {
|
||||
final Attributes attrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("234");
|
||||
attrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(attrs, null);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
String s[] = classUnderTest.getStringAttributes("abc");
|
||||
assertEquals("123", s[0]);
|
||||
assertEquals("234", s[1]);
|
||||
assertEquals(2, s.length);
|
||||
}
|
||||
|
||||
public void testGetStringAttributesNotExists() throws Exception {
|
||||
String s[] = classUnderTest.getStringAttributes("abc");
|
||||
assertNull(s);
|
||||
}
|
||||
|
||||
public void testSetStringAttribute() throws Exception {
|
||||
assertFalse(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValue("abc", "123");
|
||||
Attributes attrs = classUnderTest.getAttributes();
|
||||
Attribute attr = attrs.get("abc");
|
||||
assertEquals("123", (String) attr.get());
|
||||
}
|
||||
|
||||
public void testSetStringAttributeNull() throws Exception {
|
||||
assertFalse(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValue("abc", null);
|
||||
Attributes attrs = classUnderTest.getAttributes();
|
||||
Attribute attr = attrs.get("abc");
|
||||
assertNull(attr);
|
||||
}
|
||||
|
||||
public void testAddAttribute() throws Exception {
|
||||
classUnderTest.setUpdateMode(true);
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValue("abc", "123");
|
||||
Attributes attrs = classUnderTest.getAttributes();
|
||||
Attribute attr = attrs.get("abc");
|
||||
assertNull(attr);
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.ADD_ATTRIBUTE, mods[0].getModificationOp());
|
||||
attr = mods[0].getAttribute();
|
||||
assertEquals("123", (String) attr.get());
|
||||
|
||||
String[] modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(1, modNames.length);
|
||||
assertEquals("abc", modNames[0]);
|
||||
|
||||
classUnderTest.update();
|
||||
mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(0, modNames.length);
|
||||
attrs = classUnderTest.getAttributes();
|
||||
attr = attrs.get("abc");
|
||||
assertEquals("123", (String) attr.get());
|
||||
}
|
||||
|
||||
public void testGetDn() throws Exception {
|
||||
DirContextAdapter tested = new DirContextAdapter(DUMMY_NAME);
|
||||
Name result = tested.getDn();
|
||||
assertEquals(DUMMY_NAME, result);
|
||||
}
|
||||
|
||||
public void testGetDn_BasePath() {
|
||||
DirContextAdapter tested = new DirContextAdapter(null, DUMMY_NAME,
|
||||
BASE_NAME);
|
||||
Name result = tested.getDn();
|
||||
assertEquals(DUMMY_NAME, result);
|
||||
}
|
||||
|
||||
public void testGetNameInNamespace() {
|
||||
DirContextAdapter tested = new DirContextAdapter(DUMMY_NAME);
|
||||
String result = tested.getNameInNamespace();
|
||||
assertEquals(DUMMY_NAME.toString(), result);
|
||||
}
|
||||
|
||||
public void testGetNameInNamespace_BasePath() {
|
||||
DirContextAdapter tested = new DirContextAdapter(null,
|
||||
new DistinguishedName("c=SE"), BASE_NAME);
|
||||
String result = tested.getNameInNamespace();
|
||||
assertEquals(DUMMY_NAME.toString(), result);
|
||||
}
|
||||
|
||||
public void testAddMultiAttributes() throws Exception {
|
||||
classUnderTest.setUpdateMode(true);
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "123", "456" });
|
||||
Attributes attrs = classUnderTest.getAttributes();
|
||||
Attribute attr = attrs.get("abc");
|
||||
assertNull(attr);
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.ADD_ATTRIBUTE, mods[0].getModificationOp());
|
||||
attr = mods[0].getAttribute();
|
||||
assertEquals(2, attr.size());
|
||||
assertEquals("123", (String) attr.get(0));
|
||||
assertEquals("456", (String) attr.get(1));
|
||||
|
||||
String[] modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(1, modNames.length);
|
||||
assertEquals("abc", modNames[0]);
|
||||
|
||||
classUnderTest.update();
|
||||
mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(0, modNames.length);
|
||||
attrs = classUnderTest.getAttributes();
|
||||
attr = attrs.get("abc");
|
||||
assertEquals("123", (String) attr.get(0));
|
||||
assertEquals("456", (String) attr.get(1));
|
||||
}
|
||||
|
||||
public void testRemoveAttribute() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
fixtureAttrs.put(new BasicAttribute("abc", "123"));
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
|
||||
classUnderTest.setUpdateMode(true);
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValue("abc", null);
|
||||
Attributes attrs = classUnderTest.getAttributes();
|
||||
Attribute attr = attrs.get("abc");
|
||||
assertEquals("123", (String) attr.get());
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.REMOVE_ATTRIBUTE, mods[0].getModificationOp());
|
||||
attr = mods[0].getAttribute();
|
||||
assertEquals("abc", (String) attr.getID());
|
||||
String[] modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(1, modNames.length);
|
||||
assertEquals("abc", modNames[0]);
|
||||
|
||||
classUnderTest.update();
|
||||
mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(0, modNames.length);
|
||||
attrs = classUnderTest.getAttributes();
|
||||
attr = attrs.get("abc");
|
||||
assertNull(attr);
|
||||
}
|
||||
|
||||
public void testRemoveMultiAttribute() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute abc = new BasicAttribute("abc");
|
||||
abc.add("123");
|
||||
abc.add("456");
|
||||
fixtureAttrs.put(abc);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
|
||||
classUnderTest.setUpdateMode(true);
|
||||
classUnderTest.setAttributeValues("abc", new String[] {});
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.REMOVE_ATTRIBUTE, mods[0].getModificationOp());
|
||||
Attribute attr = mods[0].getAttribute();
|
||||
assertEquals("abc", (String) attr.getID());
|
||||
assertEquals(0, attr.size());
|
||||
}
|
||||
|
||||
public void testChangeAttribute() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
fixtureAttrs.put(new BasicAttribute("abc", "123"));
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
classUnderTest.setAttributeValue("abc", "234"); // change
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.REPLACE_ATTRIBUTE, mods[0].getModificationOp());
|
||||
Attribute attr = mods[0].getAttribute();
|
||||
assertEquals("abc", (String) attr.getID());
|
||||
assertEquals("234", (String) attr.get());
|
||||
}
|
||||
|
||||
public void testNoChangeAttribute() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
fixtureAttrs.put(new BasicAttribute("abc", "123"));
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValue("abc", "123"); // change
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
}
|
||||
|
||||
public void testNoChangeMultiAttribute() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "123", "qwe" });
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
String[] modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(0, modNames.length);
|
||||
}
|
||||
|
||||
public void testNoChangeMultiAttributeOrderDoesNotMatter() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "qwe", "123" });
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
String[] modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(0, modNames.length);
|
||||
}
|
||||
|
||||
public void testChangeMultiAttributeOrderDoesMatter() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "qwe", "123" },
|
||||
true);
|
||||
|
||||
// change
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.REPLACE_ATTRIBUTE, mods[0].getModificationOp());
|
||||
Attribute attr = mods[0].getAttribute();
|
||||
assertEquals("qwe", attr.get(0));
|
||||
assertEquals("123", attr.get(1));
|
||||
}
|
||||
|
||||
public void testChangeMultiAttribute_AddValue() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "123", "qwe",
|
||||
"klytt" });
|
||||
|
||||
ModificationItem[] modificationItems = classUnderTest
|
||||
.getModificationItems();
|
||||
assertEquals(1, modificationItems.length);
|
||||
assertEquals(DirContext.ADD_ATTRIBUTE, modificationItems[0]
|
||||
.getModificationOp());
|
||||
assertEquals("klytt", modificationItems[0].getAttribute().get());
|
||||
}
|
||||
|
||||
public void testChangeMultiAttribute_RemoveValue() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "123" });
|
||||
|
||||
ModificationItem[] modificationItems = classUnderTest
|
||||
.getModificationItems();
|
||||
assertEquals(1, modificationItems.length);
|
||||
assertEquals(DirContext.REMOVE_ATTRIBUTE, modificationItems[0]
|
||||
.getModificationOp());
|
||||
assertEquals("qwe", modificationItems[0].getAttribute().get());
|
||||
}
|
||||
|
||||
public void testChangeMultiAttribute_RemoveTwoValues() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
multi.add("rty");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "123" });
|
||||
|
||||
ModificationItem[] modificationItems = classUnderTest
|
||||
.getModificationItems();
|
||||
assertEquals(1, modificationItems.length);
|
||||
assertEquals(DirContext.REMOVE_ATTRIBUTE, modificationItems[0]
|
||||
.getModificationOp());
|
||||
assertEquals("qwe", modificationItems[0].getAttribute().get(0));
|
||||
assertEquals("rty", modificationItems[0].getAttribute().get(1));
|
||||
}
|
||||
|
||||
public void testChangeMultiAttribute_RemoveAllValues() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", null);
|
||||
|
||||
ModificationItem[] modificationItems = classUnderTest
|
||||
.getModificationItems();
|
||||
assertEquals(1, modificationItems.length);
|
||||
assertEquals(DirContext.REMOVE_ATTRIBUTE, modificationItems[0]
|
||||
.getModificationOp());
|
||||
}
|
||||
|
||||
public void testChangeMultiAttribute_SameValue() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "123", "qwe" });
|
||||
|
||||
ModificationItem[] modificationItems = classUnderTest
|
||||
.getModificationItems();
|
||||
assertEquals(0, modificationItems.length);
|
||||
}
|
||||
|
||||
public void testChangeMultiAttribute_AddAndRemoveValue() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
multi.add("rty");
|
||||
multi.add("uio");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("abc", new String[] { "123", "qwe",
|
||||
"klytt", "kalle" });
|
||||
|
||||
ModificationItem[] modificationItems = classUnderTest
|
||||
.getModificationItems();
|
||||
assertEquals(2, modificationItems.length);
|
||||
|
||||
assertEquals(DirContext.ADD_ATTRIBUTE, modificationItems[0]
|
||||
.getModificationOp());
|
||||
Attribute modifiedAttribute = modificationItems[0].getAttribute();
|
||||
assertEquals("abc", modifiedAttribute.getID());
|
||||
assertEquals(2, modifiedAttribute.size());
|
||||
assertEquals("klytt", modifiedAttribute.get(0));
|
||||
assertEquals("kalle", modifiedAttribute.get(1));
|
||||
|
||||
modifiedAttribute = modificationItems[1].getAttribute();
|
||||
assertEquals(DirContext.REMOVE_ATTRIBUTE, modificationItems[1]
|
||||
.getModificationOp());
|
||||
assertEquals("abc", modifiedAttribute.getID());
|
||||
assertEquals(2, modifiedAttribute.size());
|
||||
assertEquals("rty", modifiedAttribute.get(0));
|
||||
assertEquals("uio", modifiedAttribute.get(1));
|
||||
}
|
||||
|
||||
public void testAddAttribute_Multivalue() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
Attribute multi = new BasicAttribute("abc");
|
||||
multi.add("123");
|
||||
multi.add("qwe");
|
||||
fixtureAttrs.put(multi);
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValues("def", new String[] { "kalle",
|
||||
"klytt" });
|
||||
|
||||
ModificationItem[] modificationItems = classUnderTest
|
||||
.getModificationItems();
|
||||
assertEquals(1, modificationItems.length);
|
||||
assertEquals("def", modificationItems[0].getAttribute().getID());
|
||||
}
|
||||
|
||||
public void testChangeAttributeTwice() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
fixtureAttrs.put(new BasicAttribute("abc", "123"));
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValue("abc", "234"); // change
|
||||
classUnderTest.setAttributeValue("abc", "987");
|
||||
// change a second time
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.REPLACE_ATTRIBUTE, mods[0].getModificationOp());
|
||||
Attribute attr = mods[0].getAttribute();
|
||||
assertEquals("abc", (String) attr.getID());
|
||||
assertEquals("987", (String) attr.get());
|
||||
|
||||
classUnderTest.update();
|
||||
mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
String[] modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(0, modNames.length);
|
||||
Attributes attrs = classUnderTest.getAttributes();
|
||||
attr = attrs.get("abc");
|
||||
assertEquals("987", (String) attr.get());
|
||||
assertEquals("987", classUnderTest.getStringAttribute("abc"));
|
||||
}
|
||||
|
||||
public void testAddReplaceAndChangeAttribute() throws Exception {
|
||||
final Attributes fixtureAttrs = new BasicAttributes();
|
||||
fixtureAttrs.put(new BasicAttribute("abc", "123"));
|
||||
fixtureAttrs.put(new BasicAttribute("qwe", "42"));
|
||||
class TestableDirContextAdapter extends DirContextAdapter {
|
||||
public TestableDirContextAdapter() {
|
||||
super(fixtureAttrs, null);
|
||||
setUpdateMode(true);
|
||||
}
|
||||
}
|
||||
classUnderTest = new TestableDirContextAdapter();
|
||||
assertTrue(classUnderTest.isUpdateMode());
|
||||
classUnderTest.setAttributeValue("abc", "234"); // change
|
||||
classUnderTest.setAttributeValue("qwe", null); // remove
|
||||
classUnderTest.setAttributeValue("zzz", "new"); // new
|
||||
Attributes attrs = classUnderTest.getAttributes();
|
||||
Attribute attr = attrs.get("abc");
|
||||
assertEquals("123", (String) attr.get());
|
||||
assertEquals(2, attrs.size());
|
||||
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(3, mods.length);
|
||||
String[] modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(3, modNames.length);
|
||||
|
||||
ModificationItem mod = getModificationItem(mods,
|
||||
DirContext.REPLACE_ATTRIBUTE);
|
||||
assertNotNull(mod);
|
||||
attr = mod.getAttribute();
|
||||
assertEquals("abc", (String) attr.getID());
|
||||
assertEquals("234", (String) attr.get());
|
||||
|
||||
mod = getModificationItem(mods, DirContext.REMOVE_ATTRIBUTE);
|
||||
assertNotNull(mod);
|
||||
attr = mod.getAttribute();
|
||||
assertEquals("qwe", (String) attr.getID());
|
||||
|
||||
mod = getModificationItem(mods, DirContext.ADD_ATTRIBUTE);
|
||||
assertNotNull(mod);
|
||||
attr = mod.getAttribute();
|
||||
assertEquals("zzz", (String) attr.getID());
|
||||
assertEquals("new", (String) attr.get());
|
||||
|
||||
classUnderTest.update();
|
||||
mods = classUnderTest.getModificationItems();
|
||||
assertEquals(0, mods.length);
|
||||
modNames = classUnderTest.getNamesOfModifiedAttributes();
|
||||
assertEquals(0, modNames.length);
|
||||
|
||||
attrs = classUnderTest.getAttributes();
|
||||
assertEquals(2, attrs.size());
|
||||
attr = attrs.get("abc");
|
||||
assertEquals("234", (String) attr.get());
|
||||
assertEquals("new", classUnderTest.getStringAttribute("zzz"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for LDAP-15: DirContextAdapter.setAttribute(). Verifies that setting
|
||||
* an Attribute should modify updatedAttrs if in update mode.
|
||||
*
|
||||
* @throws NamingException
|
||||
*/
|
||||
public void testSetAttribute_UpdateMode() throws NamingException {
|
||||
// Set original attribute value
|
||||
Attribute attribute = new BasicAttribute("cn", "john doe");
|
||||
classUnderTest.setAttribute(attribute);
|
||||
|
||||
// Set to update mode
|
||||
classUnderTest.setUpdateMode(true);
|
||||
|
||||
// Perform test - update the attribute
|
||||
Attribute updatedAttribute = new BasicAttribute("cn", "nisse hult");
|
||||
classUnderTest.setAttribute(updatedAttribute);
|
||||
|
||||
// Verify result
|
||||
ModificationItem[] mods = classUnderTest.getModificationItems();
|
||||
assertEquals(1, mods.length);
|
||||
assertEquals(DirContext.REPLACE_ATTRIBUTE, mods[0].getModificationOp());
|
||||
|
||||
Attribute modificationAttribute = mods[0].getAttribute();
|
||||
assertEquals("cn", modificationAttribute.getID());
|
||||
assertEquals("nisse hult", modificationAttribute.get());
|
||||
}
|
||||
|
||||
private ModificationItem getModificationItem(ModificationItem[] mods,
|
||||
int operation) {
|
||||
for (int i = 0; i < mods.length; i++) {
|
||||
if (mods[i].getModificationOp() == operation)
|
||||
return mods[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,450 @@
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ldap.core;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.naming.CompositeName;
|
||||
import javax.naming.InvalidNameException;
|
||||
import javax.naming.Name;
|
||||
|
||||
import org.springframework.ldap.core.DistinguishedName;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.gargoylesoftware.base.testing.EqualsTester;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link DistinguishedName} class.
|
||||
*
|
||||
* @author Adam Skogman
|
||||
* @author Mattias Arthursson
|
||||
*/
|
||||
public class DistinguishedNameTest extends TestCase {
|
||||
|
||||
public void testDistinguishedName_CompositeWithSlash() throws Exception {
|
||||
Name testPath = new CompositeName("cn=foo\\/bar");
|
||||
DistinguishedName path = new DistinguishedName(testPath.toString());
|
||||
assertEquals("cn=foo/bar", path.toString());
|
||||
}
|
||||
|
||||
public void testEmptyPathImmutable() throws Exception {
|
||||
DistinguishedName emptyPath = DistinguishedName.EMPTY_PATH;
|
||||
try {
|
||||
emptyPath.add("cn=John Doe");
|
||||
fail("UnsupportedOperationException expected");
|
||||
} catch (UnsupportedOperationException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testDistinguishedName() {
|
||||
|
||||
String testPath = "cn=foo\\,bar,OU=FOO\\,bar , OU=foo\\;bar;OU=foo\\;bar"
|
||||
+ " ; ou=foo\\,,ou=foo\\,;ou=foo\\;;ou=foo\\,;ou=bar\\,";
|
||||
System.out.println(testPath);
|
||||
|
||||
DistinguishedName path = new DistinguishedName(testPath);
|
||||
|
||||
assertEquals("cn", path.getLdapRdn(8).getComponent().getKey());
|
||||
assertEquals("foo,bar", path.getLdapRdn(8).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(7).getComponent().getKey());
|
||||
assertEquals("FOO,bar", path.getLdapRdn(7).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(6).getComponent().getKey());
|
||||
assertEquals("foo;bar", path.getLdapRdn(6).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(5).getComponent().getKey());
|
||||
assertEquals("foo;bar", path.getLdapRdn(5).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(4).getComponent().getKey());
|
||||
assertEquals("foo,", path.getLdapRdn(4).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(3).getComponent().getKey());
|
||||
assertEquals("foo,", path.getLdapRdn(3).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(2).getComponent().getKey());
|
||||
assertEquals("foo;", path.getLdapRdn(2).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(1).getComponent().getKey());
|
||||
assertEquals("foo,", path.getLdapRdn(1).getComponent().getValue());
|
||||
assertEquals("ou", path.getLdapRdn(0).getComponent().getKey());
|
||||
assertEquals("bar,", path.getLdapRdn(0).getComponent().getValue());
|
||||
}
|
||||
|
||||
public void testRemove() throws InvalidNameException {
|
||||
|
||||
String testPath = "cn=john.doe, OU=Users,OU=Some Company,OU=G,OU=I,OU=M";
|
||||
DistinguishedName path = new DistinguishedName(testPath);
|
||||
|
||||
path.remove(1);
|
||||
path.remove(3);
|
||||
|
||||
assertEquals("cn=john.doe, ou=Some Company, ou=G, ou=M", path
|
||||
.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests parsing and toString.
|
||||
*/
|
||||
public void testContains() {
|
||||
|
||||
DistinguishedName migpath = new DistinguishedName("OU=G,OU=I,OU=M");
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=SE,OU=G,OU=I,OU=M");
|
||||
DistinguishedName path2 = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=SE,ou=G,OU=i,OU=M, ou=foo");
|
||||
DistinguishedName path3 = new DistinguishedName(
|
||||
"ou=G,OU=i,OU=M, ou=foo");
|
||||
DistinguishedName path4 = new DistinguishedName("ou=G,OU=i,ou=m");
|
||||
|
||||
DistinguishedName pathE1 = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=SE,ou=G,OU=L,OU=M, ou=foo");
|
||||
DistinguishedName pathE2 = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=SE");
|
||||
|
||||
assertTrue("Contains MIG", path1.contains(migpath));
|
||||
assertTrue("Contains MIG", path2.contains(migpath));
|
||||
assertTrue("Contains MIG", path3.contains(migpath));
|
||||
assertTrue("Contains MIG", path4.contains(migpath));
|
||||
|
||||
assertFalse("Does not contain MIG", pathE1.contains(migpath));
|
||||
assertFalse("Does not contain MIG", pathE2.contains(migpath));
|
||||
}
|
||||
|
||||
public void testAppend() {
|
||||
DistinguishedName path1 = new DistinguishedName("ou=foo, OU=bar");
|
||||
DistinguishedName path2 = new DistinguishedName("OU=baz");
|
||||
|
||||
path1.append(path2);
|
||||
|
||||
assertEquals("Append failed", "ou=baz, ou=foo, ou=bar", path1
|
||||
.toString());
|
||||
}
|
||||
|
||||
public void testPrepend() {
|
||||
DistinguishedName path1 = new DistinguishedName("ou=foo, OU=bar");
|
||||
DistinguishedName path2 = new DistinguishedName("cn=fie, OU=baz");
|
||||
|
||||
path1.prepend(path2);
|
||||
|
||||
assertEquals("Append failed", "ou=foo, ou=bar, cn=fie, ou=baz", path1
|
||||
.toString());
|
||||
}
|
||||
|
||||
public void testEquals() throws Exception {
|
||||
|
||||
// original object
|
||||
final Object originalObject = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=Some company,C=SE");
|
||||
|
||||
// another object that has the same values as the original
|
||||
final Object identicalObject = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=Some company,C=SE");
|
||||
|
||||
// another object with different values
|
||||
final Object differentObject = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=Some other company,C=SE");
|
||||
|
||||
// a subclass with the same values as the original
|
||||
final Object subclassObject = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=Some company,C=SE") {
|
||||
private static final long serialVersionUID = 1L;
|
||||
};
|
||||
|
||||
new EqualsTester(originalObject, identicalObject, differentObject,
|
||||
subclassObject);
|
||||
}
|
||||
|
||||
public void testClone() {
|
||||
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"cn=john.doe, OU=Users,OU=Some company,C=SE");
|
||||
|
||||
DistinguishedName path2 = (DistinguishedName) path1.clone();
|
||||
|
||||
assertEquals("Should be equal", path1, path2);
|
||||
|
||||
path2.removeFirst();
|
||||
assertFalse("Should not be equal", path1.equals(path2));
|
||||
|
||||
}
|
||||
|
||||
public void testEndsWith_true() {
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
DistinguishedName ending1 = new DistinguishedName("uid=mtah.test");
|
||||
|
||||
DistinguishedName path2 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
DistinguishedName ending2 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU");
|
||||
|
||||
assertTrue(path1.endsWith(ending1));
|
||||
assertTrue(path2.endsWith(ending2));
|
||||
}
|
||||
|
||||
public void testEndsWith_false() {
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
DistinguishedName ending1 = new DistinguishedName("ou=people");
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
public void testGetAll() throws Exception {
|
||||
DistinguishedName path = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
|
||||
Enumeration elements = path.getAll();
|
||||
|
||||
String element = (String) elements.nextElement();
|
||||
assertEquals("o=example.com", element);
|
||||
|
||||
element = (String) elements.nextElement();
|
||||
assertEquals("ou=EU", element);
|
||||
|
||||
element = (String) elements.nextElement();
|
||||
assertEquals("ou=people", element);
|
||||
|
||||
element = (String) elements.nextElement();
|
||||
assertEquals("uid=mtah.test", element);
|
||||
}
|
||||
|
||||
public void testGet() throws Exception {
|
||||
DistinguishedName path = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
|
||||
String string = path.get(1);
|
||||
|
||||
assertEquals("ou=EU", string);
|
||||
}
|
||||
|
||||
public void testSize() {
|
||||
DistinguishedName path1 = new DistinguishedName();
|
||||
assertEquals(0, path1.size());
|
||||
|
||||
DistinguishedName path2 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
assertEquals(4, path2.size());
|
||||
}
|
||||
|
||||
public void testGetPrefix() {
|
||||
DistinguishedName path = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
|
||||
Name prefix = path.getPrefix(0);
|
||||
assertEquals(0, prefix.size());
|
||||
|
||||
prefix = path.getPrefix(1);
|
||||
assertEquals(1, prefix.size());
|
||||
assertEquals("o=example.com", prefix.get(0));
|
||||
|
||||
prefix = path.getPrefix(2);
|
||||
assertEquals(2, prefix.size());
|
||||
assertEquals("o=example.com", prefix.get(0));
|
||||
assertEquals("ou=EU", prefix.get(1));
|
||||
}
|
||||
|
||||
public void testGetSuffix() {
|
||||
DistinguishedName path = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
|
||||
Name suffix = path.getSuffix(0);
|
||||
assertEquals(4, suffix.size());
|
||||
|
||||
suffix = path.getSuffix(2);
|
||||
assertEquals(2, suffix.size());
|
||||
assertEquals("ou=people", suffix.get(0));
|
||||
|
||||
suffix = path.getSuffix(4);
|
||||
assertEquals(0, suffix.size());
|
||||
|
||||
try {
|
||||
path.getSuffix(5);
|
||||
fail("ArrayIndexOutOfBoundsException expected");
|
||||
} catch (ArrayIndexOutOfBoundsException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testStartsWith_true() {
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
DistinguishedName start1 = new DistinguishedName("o=example.com");
|
||||
|
||||
DistinguishedName path2 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
DistinguishedName start2 = new DistinguishedName(
|
||||
"ou=people, ou=EU, o=example.com");
|
||||
|
||||
assertTrue(path1.startsWith(start1));
|
||||
assertTrue(path2.startsWith(start2));
|
||||
}
|
||||
|
||||
public void testStartsWith_false() {
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
DistinguishedName start1 = new DistinguishedName("ou=people");
|
||||
|
||||
DistinguishedName path2 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
DistinguishedName start2 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=EU, ou=people");
|
||||
|
||||
assertFalse(path1.startsWith(start1));
|
||||
assertFalse(path2.startsWith(start2));
|
||||
}
|
||||
|
||||
public void testStartsWith_Longer() {
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
|
||||
DistinguishedName path2 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com, o=a.com");
|
||||
|
||||
assertFalse(path1.startsWith(path2));
|
||||
}
|
||||
|
||||
public void testStartsWith_EmptyPath() {
|
||||
DistinguishedName path1 = new DistinguishedName(
|
||||
"uid=mtah.test, ou=people, ou=EU, o=example.com");
|
||||
|
||||
DistinguishedName path2 = new DistinguishedName();
|
||||
|
||||
assertFalse(path1.startsWith(path2));
|
||||
}
|
||||
|
||||
public void testIsEmpty_True() {
|
||||
DistinguishedName path = new DistinguishedName();
|
||||
assertTrue(path.isEmpty());
|
||||
}
|
||||
|
||||
public void testIsEmpty_False() {
|
||||
DistinguishedName path = new DistinguishedName("o=example.com");
|
||||
assertFalse(path.isEmpty());
|
||||
}
|
||||
|
||||
public void testAddAll() throws Exception {
|
||||
DistinguishedName path1 = new DistinguishedName("ou=foo, OU=bar");
|
||||
DistinguishedName path2 = new DistinguishedName("OU=baz");
|
||||
|
||||
path1.addAll(path2);
|
||||
|
||||
assertEquals("AddAll failed", "ou=baz, ou=foo, ou=bar", path1
|
||||
.toString());
|
||||
}
|
||||
|
||||
public void testAddAll_Index() throws InvalidNameException {
|
||||
DistinguishedName path1 = new DistinguishedName("ou=foo, OU=bar");
|
||||
DistinguishedName path2 = new DistinguishedName("OU=baz");
|
||||
|
||||
path1.addAll(1, path2);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
public void testToUrl() {
|
||||
DistinguishedName path = new DistinguishedName("dc=jayway, dc=se");
|
||||
String url = path.toUrl();
|
||||
|
||||
assertEquals("dc=jayway,dc=se", url);
|
||||
}
|
||||
|
||||
public void testMultiValueRdn() throws Exception {
|
||||
DistinguishedName path = new DistinguishedName(
|
||||
"firstName=Rod+lastName=Johnson,ou=UK,dc=interface21,dc=com");
|
||||
assertEquals(4, path.size());
|
||||
assertEquals("firstname=Rod+lastname=Johnson", path.get(3));
|
||||
}
|
||||
|
||||
public void testCompareTo_Equals() throws Exception {
|
||||
DistinguishedName name1 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=SE");
|
||||
DistinguishedName name2 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=SE");
|
||||
|
||||
int result = name1.compareTo(name2);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
public void testCompareTo_Less() throws Exception {
|
||||
DistinguishedName name1 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=DK");
|
||||
DistinguishedName name2 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=SE");
|
||||
|
||||
int result = name1.compareTo(name2);
|
||||
assertTrue(result < 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Less_MoreSignificant() throws Exception {
|
||||
DistinguishedName name1 = new DistinguishedName(
|
||||
"an=john doe, ou=Some company, c=DK");
|
||||
DistinguishedName name2 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=SE");
|
||||
|
||||
int result = name1.compareTo(name2);
|
||||
assertTrue(result < 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Greater() throws Exception {
|
||||
DistinguishedName name1 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=SE");
|
||||
DistinguishedName name2 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=DK");
|
||||
|
||||
int result = name1.compareTo(name2);
|
||||
assertTrue(result > 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Longer() throws Exception {
|
||||
DistinguishedName name1 = new DistinguishedName(
|
||||
"leaf=someleaf, cn=john doe, ou=Some company, c=SE");
|
||||
DistinguishedName name2 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=SE");
|
||||
|
||||
int result = name1.compareTo(name2);
|
||||
assertTrue(result > 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Shorter() throws Exception {
|
||||
DistinguishedName name1 = new DistinguishedName(
|
||||
"cn=john doe, ou=Some company, c=SE");
|
||||
DistinguishedName name2 = new DistinguishedName(
|
||||
"leaf=someleaf, cn=john doe, ou=Some company, c=SE");
|
||||
|
||||
int result = name1.compareTo(name2);
|
||||
assertTrue(result < 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.core;
|
||||
|
||||
import org.springframework.ldap.core.LdapRdnComponent;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for LdapRdnComponent.
|
||||
*
|
||||
* @author Mattias Arthursson
|
||||
*/
|
||||
public class LdapRdnComponentTest extends TestCase {
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testCompareTo_Less() {
|
||||
LdapRdnComponent component1 = new LdapRdnComponent("cn", "john doe");
|
||||
LdapRdnComponent component2 = new LdapRdnComponent("sn", "doe");
|
||||
int result = component1.compareTo(component2);
|
||||
assertTrue(result < 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Greater() {
|
||||
LdapRdnComponent component1 = new LdapRdnComponent("sn", "doe");
|
||||
LdapRdnComponent component2 = new LdapRdnComponent("cn", "john doe");
|
||||
int result = component1.compareTo(component2);
|
||||
assertTrue(result > 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Equal() {
|
||||
LdapRdnComponent component1 = new LdapRdnComponent("cn", "john doe");
|
||||
LdapRdnComponent component2 = new LdapRdnComponent("cn", "john doe");
|
||||
int result = component1.compareTo(component2);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Unit test for the LdapRdn class.
|
||||
*
|
||||
* @author Adam Skogman
|
||||
*/
|
||||
public class LdapRdnTest extends TestCase {
|
||||
|
||||
public void testLdapRdn_parse_simple() {
|
||||
|
||||
LdapRdn rdn = new LdapRdn("foo=bar");
|
||||
|
||||
assertEquals("foo", rdn.getComponent().getKey());
|
||||
assertEquals("bar", rdn.getComponent().getValue());
|
||||
assertEquals("foo=bar", rdn.getComponent().getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_spaces() {
|
||||
|
||||
LdapRdn rdn = new LdapRdn(" foo = bar ");
|
||||
|
||||
assertEquals("foo", rdn.getComponent().getKey());
|
||||
assertEquals("bar", rdn.getComponent().getValue());
|
||||
assertEquals("foo=bar", rdn.getComponent().getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_escape() {
|
||||
|
||||
LdapRdn rdn = new LdapRdn("foo=bar\\=fum");
|
||||
|
||||
assertEquals("foo", rdn.getComponent().getKey());
|
||||
assertEquals("bar=fum", rdn.getComponent().getValue());
|
||||
assertEquals("foo=bar\\=fum", rdn.getComponent().getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_hexEscape() {
|
||||
|
||||
LdapRdn rdn = new LdapRdn("foo=bar\\0dfum");
|
||||
|
||||
assertEquals("foo", rdn.getComponent().getKey());
|
||||
assertEquals("bar\rfum", rdn.getComponent().getValue());
|
||||
assertEquals("foo=bar\\0Dfum", rdn.getComponent().getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_trailingBackslash() {
|
||||
|
||||
try {
|
||||
new LdapRdn("foo=bar\\");
|
||||
fail("Should throw BadLdapGrammarException");
|
||||
} catch (BadLdapGrammarException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_spaces_escape() {
|
||||
|
||||
LdapRdn rdn = new LdapRdn(" foo = \\ bar\\20 \\ ");
|
||||
|
||||
assertEquals("foo", rdn.getComponent().getKey());
|
||||
assertEquals(" bar ", rdn.getComponent().getValue());
|
||||
assertEquals("foo=\\ bar \\ ", rdn.getComponent().getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_tooMuchTrim() {
|
||||
try {
|
||||
new LdapRdn("foo=bar\\");
|
||||
fail("Should throw BadLdapGrammarException");
|
||||
} catch (BadLdapGrammarException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_slash() {
|
||||
LdapRdn rdn = new LdapRdn("ou=Clerical / Secretarial Staff");
|
||||
|
||||
assertEquals("ou", rdn.getComponent().getKey());
|
||||
assertEquals("Clerical / Secretarial Staff", rdn.getComponent()
|
||||
.getValue());
|
||||
assertEquals("ou=Clerical / Secretarial Staff", rdn.getComponent()
|
||||
.getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testLdapRdn_parse_quoteInKey() {
|
||||
try {
|
||||
new LdapRdn("\"umanroleid=2583");
|
||||
fail("Should throw BadLdapGrammarException");
|
||||
} catch (BadLdapGrammarException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testLdapRdn_KeyValue_simple() {
|
||||
LdapRdn rdn = new LdapRdn("foo", "bar");
|
||||
|
||||
assertEquals("foo", rdn.getComponent().getKey());
|
||||
assertEquals("bar", rdn.getComponent().getValue());
|
||||
assertEquals("foo=bar", rdn.getComponent().getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testLdapRdn_KeyValue_valueNeedsEscape() {
|
||||
LdapRdn rdn = new LdapRdn("foo", "bar\\");
|
||||
|
||||
assertEquals("foo", rdn.getComponent().getKey());
|
||||
assertEquals("bar\\", rdn.getComponent().getValue());
|
||||
assertEquals("foo=bar\\\\", rdn.getComponent().getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testEncodeUrl() {
|
||||
LdapRdn rdn = new LdapRdn("o = example.com ");
|
||||
assertEquals("o=example.com", rdn.encodeUrl());
|
||||
}
|
||||
|
||||
public void testEncodeUrl_SpacesInValue() {
|
||||
LdapRdn rdn = new LdapRdn("o = my organization ");
|
||||
assertEquals("o=my%20organization", rdn.encodeUrl());
|
||||
}
|
||||
|
||||
public void testLdapRdn_Parse_MultipleComponents() {
|
||||
LdapRdn rdn = new LdapRdn("cn=John Doe+sn=Doe");
|
||||
assertEquals("cn=John Doe", rdn.getComponent(0).encodeLdap());
|
||||
assertEquals("sn=Doe", rdn.getComponent(1).encodeLdap());
|
||||
assertEquals("cn=John Doe+sn=Doe", rdn.getLdapEncoded());
|
||||
}
|
||||
|
||||
public void testEquals() throws Exception {
|
||||
// original object
|
||||
final Object originalObject = new LdapRdn("cn", "john.doe");
|
||||
|
||||
// another object that has the same values as the original
|
||||
final Object identicalObject = new LdapRdn("cn", "john.doe");
|
||||
|
||||
// another object with different values
|
||||
final Object differentObject = new LdapRdn("cn", "john.svensson");
|
||||
|
||||
// a subclass with the same values as the original
|
||||
final Object subclassObject = new LdapRdn("cn", "john.doe") {
|
||||
private static final long serialVersionUID = 1L;
|
||||
};
|
||||
|
||||
new EqualsTester(originalObject, identicalObject, differentObject,
|
||||
subclassObject);
|
||||
}
|
||||
|
||||
public void testCompareTo_Equals() throws Exception {
|
||||
LdapRdn rdn1 = new LdapRdn("cn=john doe");
|
||||
LdapRdn rdn2 = new LdapRdn("cn=john doe");
|
||||
|
||||
int result = rdn1.compareTo(rdn2);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
public void testCompareTo_EqualsComplex() throws Exception {
|
||||
LdapRdn rdn1 = new LdapRdn("cn=john doe+sn=doe");
|
||||
LdapRdn rdn2 = new LdapRdn("cn=john doe+sn=doe");
|
||||
|
||||
int result = rdn1.compareTo(rdn2);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
public void testCompareTo_Less() {
|
||||
LdapRdn rdn1 = new LdapRdn("cn=john doe+sn=doe");
|
||||
LdapRdn rdn2 = new LdapRdn("cn=john doe+tn=doe");
|
||||
|
||||
int result = rdn1.compareTo(rdn2);
|
||||
assertTrue(result < 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Greater() {
|
||||
LdapRdn rdn1 = new LdapRdn("cn=john doe+sn=doe");
|
||||
LdapRdn rdn2 = new LdapRdn("cn=john doe+an=doe");
|
||||
|
||||
int result = rdn1.compareTo(rdn2);
|
||||
assertTrue(result > 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Shorter() {
|
||||
LdapRdn rdn1 = new LdapRdn("cn=john doe+sn=doe");
|
||||
LdapRdn rdn2 = new LdapRdn("cn=john doe+sn=doe+description=tjo");
|
||||
|
||||
int result = rdn1.compareTo(rdn2);
|
||||
assertTrue(result < 0);
|
||||
}
|
||||
|
||||
public void testCompareTo_Longer() {
|
||||
LdapRdn rdn1 = new LdapRdn("cn=john doe+sn=doe+description=tjo");
|
||||
LdapRdn rdn2 = new LdapRdn("cn=john doe+sn=doe");
|
||||
|
||||
int result = rdn1.compareTo(rdn2);
|
||||
assertTrue(result > 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user