diff --git a/spring-ldap/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java b/spring-ldap/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java index abfc4daa..4cbb4074 100644 --- a/spring-ldap/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java +++ b/spring-ldap/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ldap.support.authentication; +package org.springframework.ldap.authentication; import junit.framework.TestCase; @@ -25,7 +25,7 @@ import org.acegisecurity.context.SecurityContextHolder; import org.acegisecurity.userdetails.User; import org.acegisecurity.userdetails.ldap.LdapUserDetails; import org.easymock.MockControl; -import org.springframework.ldap.support.authentication.AcegiAuthenticationSource; +import org.springframework.ldap.authentication.AcegiAuthenticationSource; public class AcegiAuthenticationSourceTest extends TestCase { diff --git a/spring-ldap/src/test/java/org/springframework/ldap/authentication/DefaultValuesAuthenticationSourceDecoratorTest.java b/spring-ldap/src/test/java/org/springframework/ldap/authentication/DefaultValuesAuthenticationSourceDecoratorTest.java index 26bdd7a7..ec170e07 100644 --- a/spring-ldap/src/test/java/org/springframework/ldap/authentication/DefaultValuesAuthenticationSourceDecoratorTest.java +++ b/spring-ldap/src/test/java/org/springframework/ldap/authentication/DefaultValuesAuthenticationSourceDecoratorTest.java @@ -1,8 +1,8 @@ -package org.springframework.ldap.support.authentication; +package org.springframework.ldap.authentication; import org.easymock.MockControl; +import org.springframework.ldap.authentication.DefaultValuesAuthenticationSourceDecorator; import org.springframework.ldap.core.AuthenticationSource; -import org.springframework.ldap.support.authentication.DefaultValuesAuthenticationSourceDecorator; import junit.framework.TestCase; diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/AbstractRequestControlDirContextProcessorTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/AbstractRequestControlDirContextProcessorTest.java index 07a904d5..cff82ba0 100644 --- a/spring-ldap/src/test/java/org/springframework/ldap/control/AbstractRequestControlDirContextProcessorTest.java +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/AbstractRequestControlDirContextProcessorTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.ldap.support.control; +package org.springframework.ldap.control; import javax.naming.NamingException; import javax.naming.directory.DirContext; @@ -23,6 +23,7 @@ import javax.naming.ldap.LdapContext; import junit.framework.TestCase; import org.easymock.MockControl; +import org.springframework.ldap.control.AbstractRequestControlDirContextProcessor; public class AbstractRequestControlDirContextProcessorTest extends TestCase { diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultTest.java index 1401b74c..d85b3f6d 100644 --- a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultTest.java +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultTest.java @@ -13,11 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.ldap.support.control; +package org.springframework.ldap.control; import java.util.LinkedList; import java.util.List; +import org.springframework.ldap.control.PagedResult; +import org.springframework.ldap.control.PagedResultsCookie; + import com.gargoylesoftware.base.testing.EqualsTester; import junit.framework.TestCase; diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsCookieTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsCookieTest.java index 39ea3c52..08c46f5a 100644 --- a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsCookieTest.java +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsCookieTest.java @@ -13,7 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.ldap.support.control; +package org.springframework.ldap.control; + +import org.springframework.ldap.control.PagedResultsCookie; import com.gargoylesoftware.base.testing.EqualsTester; diff --git a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsRequestControlTest.java b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsRequestControlTest.java index 2cf0e4dc..d03a285b 100644 --- a/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsRequestControlTest.java +++ b/spring-ldap/src/test/java/org/springframework/ldap/control/PagedResultsRequestControlTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.ldap.support.control; +package org.springframework.ldap.control; import java.io.IOException; @@ -23,6 +23,8 @@ import javax.naming.ldap.LdapContext; import junit.framework.TestCase; import org.easymock.MockControl; +import org.springframework.ldap.control.PagedResultsCookie; +import org.springframework.ldap.control.PagedResultsRequestControl; import com.sun.jndi.ldap.Ber; import com.sun.jndi.ldap.BerDecoder; diff --git a/spring-ldap/src/test/java/org/springframework/ldap/core/support/DefaultDirObjectFactoryTest.java b/spring-ldap/src/test/java/org/springframework/ldap/core/support/DefaultDirObjectFactoryTest.java new file mode 100644 index 00000000..8e3dfdd4 --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/core/support/DefaultDirObjectFactoryTest.java @@ -0,0 +1,157 @@ +/* + * 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.support; + +import java.util.Hashtable; + +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.directory.BasicAttributes; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.ldap.core.DefaultDirObjectFactory; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; + +public class DefaultDirObjectFactoryTest extends TestCase { + + private MockControl contextControl; + + private Context contextMock; + + private static final Name DN = new DistinguishedName( + "ou=some unit, dc=jayway, dc=se"); + + private DefaultDirObjectFactory tested; + + private MockControl contextControl2; + + private Context contextMock2; + + protected void setUp() throws Exception { + super.setUp(); + + contextControl = MockControl.createControl(Context.class); + contextMock = (Context) contextControl.getMock(); + + contextControl2 = MockControl.createControl(Context.class); + contextMock2 = (Context) contextControl2.getMock(); + + tested = new DefaultDirObjectFactory(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + + contextControl = null; + contextMock = null; + + contextControl2 = null; + contextMock2 = null; + + tested = null; + } + + protected void replay() { + contextControl.replay(); + contextControl2.replay(); + } + + protected void verify() { + contextControl.verify(); + contextControl2.verify(); + } + + public void testGetObjectInstance() throws Exception { + BasicAttributes expectedAttributes = new BasicAttributes(); + expectedAttributes.put("someAttribute", "someValue"); + + contextMock.close(); + + replay(); + + DirContextAdapter adapter = (DirContextAdapter) tested + .getObjectInstance(contextMock, DN, null, new Hashtable(), + expectedAttributes); + + verify(); + + assertEquals(DN, adapter.getDn()); + assertEquals(expectedAttributes, adapter.getAttributes()); + } + + public void testGetObjectInstance_nullObject() throws Exception { + BasicAttributes expectedAttributes = new BasicAttributes(); + expectedAttributes.put("someAttribute", "someValue"); + + replay(); + + DirContextAdapter adapter = (DirContextAdapter) tested + .getObjectInstance(null, DN, null, new Hashtable(), + expectedAttributes); + + verify(); + + assertEquals(DN, adapter.getDn()); + assertEquals(expectedAttributes, adapter.getAttributes()); + } + + public void testGetObjectInstance_ObjectNotContext() throws Exception { + BasicAttributes expectedAttributes = new BasicAttributes(); + expectedAttributes.put("someAttribute", "someValue"); + + replay(); + + DirContextAdapter adapter = (DirContextAdapter) tested + .getObjectInstance(new Object(), DN, null, new Hashtable(), + expectedAttributes); + + verify(); + + assertEquals(DN, adapter.getDn()); + assertEquals(expectedAttributes, adapter.getAttributes()); + } + + /** + * Make sure that the base suffix is stripped off from the DN. + * + * @throws Exception + */ + public void testGetObjectInstance_BaseSet() throws Exception { + BasicAttributes expectedAttributes = new BasicAttributes(); + expectedAttributes.put("someAttribute", "someValue"); + + contextControl2.expectAndReturn(contextMock2.getNameInNamespace(), + "dc=jayway, dc=se"); + contextMock.close(); + + replay(); + + DirContextAdapter adapter = (DirContextAdapter) tested + .getObjectInstance(contextMock, new DistinguishedName( + "ou=some unit"), contextMock2, new Hashtable(), + expectedAttributes); + + verify(); + + assertEquals("ou=some unit", adapter.getDn().toString()); + assertEquals("ou=some unit, dc=jayway, dc=se", adapter + .getNameInNamespace()); + assertEquals(expectedAttributes, adapter.getAttributes()); + } +} diff --git a/spring-ldap/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java b/spring-ldap/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java new file mode 100644 index 00000000..8b6502ef --- /dev/null +++ b/spring-ldap/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java @@ -0,0 +1,221 @@ +/* + * 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.support; + +import java.util.Hashtable; + +import javax.naming.Context; +import javax.naming.ldap.Control; +import javax.naming.ldap.ControlFactory; + +import junit.framework.TestCase; + +import org.springframework.ldap.core.AuthenticationSource; +import org.springframework.ldap.core.support.LdapContextSource; + +import com.sun.jndi.ldap.ctl.ResponseControlFactory; + +/** + * Unit tests for the LdapContextSource class. + * + * @author Mattias Arthursson + * @author Ulrik Sandberg + */ +public class LdapContextSourceTest extends TestCase { + + private LdapContextSource tested; + + protected void setUp() throws Exception { + tested = new LdapContextSource(); + } + + protected void tearDown() throws Exception { + tested = null; + } + + public void testAfterPropertiesSet_NoUrl() throws Exception { + try { + tested.afterPropertiesSet(); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } + + public void testAfterPropertiesSet_BaseAndTooEarlyJdk() throws Exception { + tested = new LdapContextSource() { + String getJdkVersion() { + return "1.4.1_03"; + } + }; + + tested.setUrl("http://ldap.example.com:389"); + tested.setBase("dc=jayway,dc=se"); + try { + tested.afterPropertiesSet(); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } + + public void testGetAnonymousEnv() throws Exception { + tested.setBase("dc=example,dc=se"); + tested.setUrl("ldap://ldap.example.com:389"); + tested.setPooled(true); + tested.setUserDn("cn=Some User"); + tested.setPassword("secret"); + tested.afterPropertiesSet(); + Hashtable env = tested.getAnonymousEnv(); + assertEquals("ldap://ldap.example.com:389/dc=example,dc=se", env + .get(Context.PROVIDER_URL)); + assertEquals("true", env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)); + assertNull(env.get(Context.SECURITY_PRINCIPAL)); + assertNull(env.get(Context.SECURITY_CREDENTIALS)); + + // Verify that changing values does not change the environment values. + tested.setBase("dc=other,dc=se"); + tested.setUrl("ldap://ldap2.example.com:389"); + tested.setPooled(false); + + env = tested.getAnonymousEnv(); + assertEquals("ldap://ldap.example.com:389/dc=example,dc=se", env + .get(Context.PROVIDER_URL)); + assertEquals("true", env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)); + assertNull(env.get(Context.SECURITY_PRINCIPAL)); + assertNull(env.get(Context.SECURITY_CREDENTIALS)); + } + + public void testGetAuthenticatedEnv() throws Exception { + tested.setBase("dc=example,dc=se"); + tested.setUrl("ldap://ldap.example.com:389"); + tested.setPooled(true); + tested.setUserDn("cn=Some User"); + tested.setPassword("secret"); + tested.afterPropertiesSet(); + + Hashtable env = tested.getAuthenticatedEnv(); + assertEquals("ldap://ldap.example.com:389/dc=example,dc=se", env + .get(Context.PROVIDER_URL)); + assertEquals("true", env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)); + assertEquals("cn=Some User", env.get(Context.SECURITY_PRINCIPAL)); + assertEquals("secret", env.get(Context.SECURITY_CREDENTIALS)); + } + + public void testGetAuthenticatedEnv_DummyAuthenticationProvider() + throws Exception { + tested.setBase("dc=example,dc=se"); + tested.setUrl("ldap://ldap.example.com:389"); + tested.setPooled(true); + DummyAuthenticationProvider authenticationProvider = new DummyAuthenticationProvider(); + tested.setAuthenticationSource(authenticationProvider); + authenticationProvider.setPrincipal("cn=Some User"); + authenticationProvider.setCredentials("secret"); + tested.afterPropertiesSet(); + + Hashtable env = tested.getAuthenticatedEnv(); + assertEquals("ldap://ldap.example.com:389/dc=example,dc=se", env + .get(Context.PROVIDER_URL)); + assertEquals("true", env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)); + assertEquals("cn=Some User", env.get(Context.SECURITY_PRINCIPAL)); + assertEquals("secret", env.get(Context.SECURITY_CREDENTIALS)); + } + + public void testGetAuthenticatedEnv_DummyAuthenticationProvider_Changed() + throws Exception { + tested.setBase("dc=example,dc=se"); + tested.setUrl("ldap://ldap.example.com:389"); + tested.setPooled(true); + DummyAuthenticationProvider authenticationProvider = new DummyAuthenticationProvider(); + tested.setAuthenticationSource(authenticationProvider); + authenticationProvider.setPrincipal("cn=Some User"); + authenticationProvider.setCredentials("secret"); + tested.afterPropertiesSet(); + + authenticationProvider.setPrincipal("cn=Some Other User"); + authenticationProvider.setCredentials("other secret"); + + Hashtable env = tested.getAuthenticatedEnv(); + assertEquals("cn=Some Other User", env.get(Context.SECURITY_PRINCIPAL)); + assertEquals("other secret", env.get(Context.SECURITY_CREDENTIALS)); + + } + + public void testGetAnonymousEnv_DontCacheEnv() throws Exception { + tested.setBase("dc=example,dc=se"); + tested.setUrl("ldap://ldap.example.com:389"); + tested.setPooled(true); + tested.setUserDn("cn=Some User"); + tested.setPassword("secret"); + tested.setCacheEnvironmentProperties(false); + tested.afterPropertiesSet(); + Hashtable env = tested.getAnonymousEnv(); + assertEquals("ldap://ldap.example.com:389/dc=example,dc=se", env + .get(Context.PROVIDER_URL)); + assertEquals("true", env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)); + assertNull(env.get(Context.SECURITY_PRINCIPAL)); + assertNull(env.get(Context.SECURITY_CREDENTIALS)); + + tested.setUrl("ldap://ldap2.example.com:389"); + env = tested.getAnonymousEnv(); + assertEquals("ldap://ldap2.example.com:389/dc=example,dc=se", env + .get(Context.PROVIDER_URL)); + } + + public void testsetResponseControlFactory_Null() throws Exception { + tested.setResponseControlFactory(null); + assertNotNull(tested.getResponseControlFactory()); + assertEquals(ResponseControlFactory.class, tested + .getResponseControlFactory()); + } + + public void testsetResponseControlFactory_Valid() throws Exception { + tested.setResponseControlFactory(ControlFactory.class); + assertEquals(ControlFactory.class, tested.getResponseControlFactory()); + } + + public void testsetResponseControlFactory_Invalid() throws Exception { + try { + tested.setResponseControlFactory(Control.class); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } + + private class DummyAuthenticationProvider implements AuthenticationSource { + private String principal; + + private String credentials; + + public void setCredentials(String credentials) { + this.credentials = credentials; + } + + public void setPrincipal(String principal) { + this.principal = principal; + } + + public String getPrincipal() { + return principal; + } + + public String getCredentials() { + return credentials; + } + } +}