01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.ldap;
17:
18: import org.jmock.Mock;
19: import org.jmock.MockObjectTestCase;
20:
21: import javax.naming.Context;
22: import javax.naming.NamingException;
23: import javax.naming.directory.DirContext;
24:
25: /**
26: * Tests {@link LdapUtils}
27: *
28: * @author Luke Taylor
29: * @version $Id$
30: */
31: public class LdapUtilsTests extends MockObjectTestCase {
32: //~ Instance fields ================================================================================================
33:
34: private final LdapDataAccessException tempCoverageBoost = new LdapDataAccessException(
35: "");
36:
37: //~ Methods ========================================================================================================
38:
39: public void testCloseContextSwallowsNamingException() {
40: Mock mockCtx = mock(DirContext.class);
41:
42: mockCtx.expects(once()).method("close").will(
43: throwException(new NamingException()));
44:
45: LdapUtils.closeContext((Context) mockCtx.proxy());
46: }
47:
48: public void testGetRelativeNameReturnsEmptyStringForDnEqualToBaseName()
49: throws Exception {
50: Mock mockCtx = mock(DirContext.class);
51:
52: mockCtx.expects(atLeastOnce()).method("getNameInNamespace")
53: .will(returnValue("dc=acegisecurity,dc=org"));
54:
55: assertEquals("", LdapUtils.getRelativeName(
56: "dc=acegisecurity,dc=org", (Context) mockCtx.proxy()));
57: }
58:
59: public void testGetRelativeNameReturnsFullDnWithEmptyBaseName()
60: throws Exception {
61: Mock mockCtx = mock(DirContext.class);
62:
63: mockCtx.expects(atLeastOnce()).method("getNameInNamespace")
64: .will(returnValue(""));
65:
66: assertEquals("cn=jane,dc=acegisecurity,dc=org", LdapUtils
67: .getRelativeName("cn=jane,dc=acegisecurity,dc=org",
68: (Context) mockCtx.proxy()));
69: }
70:
71: public void testRootDnsAreParsedFromUrlsCorrectly() {
72: assertEquals("", LdapUtils
73: .parseRootDnFromUrl("ldap://monkeymachine"));
74: assertEquals("", LdapUtils
75: .parseRootDnFromUrl("ldap://monkeymachine/"));
76: assertEquals("", LdapUtils
77: .parseRootDnFromUrl("ldap://monkeymachine.co.uk/"));
78: assertEquals(
79: "dc=acegisecurity,dc=org",
80: LdapUtils
81: .parseRootDnFromUrl("ldaps://monkeymachine.co.uk/dc=acegisecurity,dc=org"));
82: assertEquals("dc=acegisecurity,dc=org", LdapUtils
83: .parseRootDnFromUrl("ldap:///dc=acegisecurity,dc=org"));
84: assertEquals(
85: "dc=acegisecurity,dc=org",
86: LdapUtils
87: .parseRootDnFromUrl("ldap://monkeymachine/dc=acegisecurity,dc=org"));
88: assertEquals(
89: "dc=acegisecurity,dc=org/ou=blah",
90: LdapUtils
91: .parseRootDnFromUrl("ldap://monkeymachine.co.uk/dc=acegisecurity,dc=org/ou=blah"));
92: }
93: }
|