001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.ldap;
017:
018: import java.util.Set;
019:
020: import javax.naming.NamingException;
021: import javax.naming.directory.DirContext;
022:
023: /**
024: *
025: *
026: * @author Luke Taylor
027: * @version $Id: LdapTemplateTests.java 1779 2007-02-06 18:21:31Z luke_t $
028: */
029: public class LdapTemplateTests extends AbstractLdapServerTestCase {
030: //~ Instance fields ================================================================================================
031:
032: private LdapTemplate template;
033:
034: //~ Methods ========================================================================================================
035:
036: protected void onSetUp() {
037: getInitialCtxFactory().setManagerDn(MANAGER_USER);
038: getInitialCtxFactory().setManagerPassword(MANAGER_PASSWORD);
039: template = new LdapTemplate(getInitialCtxFactory());
040: }
041:
042: public void testCompareOfCorrectByteValueSucceeds() {
043: assertTrue(template.compare(
044: "uid=bob,ou=people,dc=acegisecurity,dc=org",
045: "userPassword", LdapUtils.getUtf8Bytes("bobspassword")));
046: }
047:
048: public void testCompareOfCorrectValueSucceeds() {
049: assertTrue(template.compare(
050: "uid=bob,ou=people,dc=acegisecurity,dc=org", "uid",
051: "bob"));
052: }
053:
054: public void testCompareOfWrongByteValueFails() {
055: assertFalse(template.compare(
056: "uid=bob,ou=people,dc=acegisecurity,dc=org",
057: "userPassword", LdapUtils.getUtf8Bytes("wrongvalue")));
058: }
059:
060: public void testCompareOfWrongValueFails() {
061: assertFalse(template.compare(
062: "uid=bob,ou=people,dc=acegisecurity,dc=org", "uid",
063: "wrongvalue"));
064: }
065:
066: public void testNameExistsForInValidNameFails() {
067: assertFalse(template
068: .nameExists("ou=doesntexist,dc=acegisecurity,dc=org"));
069: }
070:
071: public void testNameExistsForValidNameSucceeds() {
072: assertTrue(template
073: .nameExists("ou=groups,dc=acegisecurity,dc=org"));
074: }
075:
076: public void testNamingExceptionIsTranslatedCorrectly() {
077: try {
078: template.execute(new LdapCallback() {
079: public Object doInDirContext(DirContext dirContext)
080: throws NamingException {
081: throw new NamingException();
082: }
083: });
084: fail("Expected LdapDataAccessException on NamingException");
085: } catch (LdapDataAccessException expected) {
086: }
087: }
088:
089: public void testSearchForSingleAttributeValues() {
090: String param = "uid=ben,ou=people,dc=acegisecurity,dc=org";
091:
092: Set values = template.searchForSingleAttributeValues(
093: "ou=groups", "(member={0})", new String[] { param },
094: "ou");
095:
096: assertEquals("Expected 2 results from search", 2, values.size());
097: assertTrue(values.contains("developer"));
098: assertTrue(values.contains("manager"));
099: }
100: }
|