diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/pom.xml b/mvn-build/integration-tests/acegi-support-integration-tests/pom.xml deleted file mode 100644 index 866bbef1..00000000 --- a/mvn-build/integration-tests/acegi-support-integration-tests/pom.xml +++ /dev/null @@ -1,131 +0,0 @@ - - - 4.0.0 - org.springframework.ldap - spring-ldap-acegi-support-integration-tests - Spring LDAP Acegi Support Integration Tests - 1.2.2-SNAPSHOT - - - 2.0.8 - - - - - - maven-eclipse-plugin - - true - - - - - maven-compiler-plugin - - 1.4 - 1.4 - - - - org.apache.maven.plugins - maven-surefire-plugin - - true - - - - default - test - - - - integration-test - integration-test - - test - - - false - once - - - - - - - - - - org.springframework.ldap - spring-ldap-test - 1.2.2-SNAPSHOT - - - org.springframework.ldap - spring-ldap-acegi-support - 1.2.2-SNAPSHOT - - - commons-pool - commons-pool - 1.3 - provided - - - com.sun - ldapbp - 1.0 - system - ${basedir}/../../lib/ldapbp.jar - - - - org.springframework - spring-dao - ${spring.version} - provided - - - org.springframework - spring-jdbc - ${spring.version} - provided - - - org.springframework - spring-aop - ${spring.version} - test - - - org.acegisecurity - acegi-security - 1.0.7 - test - - - aspectj - aspectjrt - 1.5.3 - test - - - aspectj - aspectjweaver - 1.5.3 - test - - - gsbase - gsbase - 2.0.1 - test - - - hsqldb - hsqldb - 1.8.0.7 - test - - - diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceITest.java b/mvn-build/integration-tests/acegi-support-integration-tests/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceITest.java deleted file mode 100644 index 8e562008..00000000 --- a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceITest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2005-2007 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.authentication; - -import java.util.List; - -import javax.naming.NamingException; -import javax.naming.directory.Attributes; - - -import org.acegisecurity.Authentication; -import org.acegisecurity.context.SecurityContextHolder; -import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; -import org.acegisecurity.providers.ldap.LdapAuthenticationProvider; -import org.springframework.ldap.authentication.AcegiAuthenticationSource; -import org.springframework.ldap.core.AttributesMapper; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; - -public class AcegiAuthenticationSourceITest extends - AbstractDependencyInjectionSpringContextTests { - - private LdapAuthenticationProvider ldapAuthProvider; - - private LdapTemplate ldapTemplate; - - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateAcegiTestContext.xml" }; - } - - public void setLdapAuthProvider(LdapAuthenticationProvider ldapAuthProvider) { - this.ldapAuthProvider = ldapAuthProvider; - } - - public void setLdapTemplate(LdapTemplate ldapTemplate) { - this.ldapTemplate = ldapTemplate; - } - - public void testGetPrincipalAndCredentials() { - Authentication authentication = ldapAuthProvider - .authenticate(new UsernamePasswordAuthenticationToken( - "Some Person3", "password")); - SecurityContextHolder.getContext().setAuthentication(authentication); - - AcegiAuthenticationSource tested = new AcegiAuthenticationSource(); - assertEquals("cn=Some Person3,ou=company1,c=Sweden,dc=jayway,dc=se", - tested.getPrincipal()); - assertEquals("password", tested.getCredentials()); - } - - public void testSearchIndividualAuthentication() { - Authentication authentication = ldapAuthProvider - .authenticate(new UsernamePasswordAuthenticationToken( - "Some Person3", "password")); - SecurityContextHolder.getContext().setAuthentication(authentication); - - List result = ldapTemplate.search("dc=jayway,dc=se", - "(objectclass=person)", new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) - throws NamingException { - return attributes.get("cn"); - } - }); - assertEquals(5, result.size()); - } - -} diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/ldap.properties b/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/ldap.properties deleted file mode 100644 index 0d201e31..00000000 --- a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:3905 -userDn=uid=admin,ou=system -password=secret -base=dc=jayway,dc=se diff --git a/mvn-build/integration-tests/core-integration-tests/pom.xml b/mvn-build/integration-tests/core-integration-tests/pom.xml index d78b474c..1cbdba87 100644 --- a/mvn-build/integration-tests/core-integration-tests/pom.xml +++ b/mvn-build/integration-tests/core-integration-tests/pom.xml @@ -6,51 +6,21 @@ Spring LDAP Core Integration Tests 1.2.2-SNAPSHOT - - 2.0.8 - + + org.springframework.ldap + master + 1.2.2-SNAPSHOT + - - maven-eclipse-plugin - - true - - - maven-compiler-plugin - 1.4 - 1.4 + 1.5 + 1.5 - - org.apache.maven.plugins - maven-surefire-plugin - - true - - - - default - test - - - - integration-test - integration-test - - test - - - false - once - - - - @@ -65,20 +35,14 @@ 1.3 provided - - com.sun - ldapbp - 1.0 - system - ${basedir}/../../lib/ldapbp.jar - - - org.springframework - spring-dao - ${spring.version} - provided + com.sun + ldapbp + 1.0 + system + ${basedir}/../../lib/ldapbp.jar + org.springframework spring-jdbc diff --git a/mvn-build/integration-tests/pom.xml b/mvn-build/integration-tests/pom.xml index 41e19536..90815a10 100644 --- a/mvn-build/integration-tests/pom.xml +++ b/mvn-build/integration-tests/pom.xml @@ -1,49 +1,81 @@ - + 4.0.0 - org.springframework.ldap spring-ldap-integration-tests + Spring LDAP Integration Tests 1.2.2-SNAPSHOT - pom - Spring LDAP Integration Tests - - - core-integration-tests - tiger-integration-tests - acegi-support-integration-tests - + + org.springframework.ldap + master + 1.2.2-SNAPSHOT + - - org.apache.maven.plugins - maven-surefire-plugin - - true - - - - default - test - - - - integration-test - integration-test - - test - - - false - once - - - - + + maven-compiler-plugin + + 1.5 + 1.5 + + + + + org.springframework.ldap + spring-ldap-test + 1.2.2-SNAPSHOT + + + org.springframework.ldap + spring-ldap-tiger + 1.2.2-SNAPSHOT + + + commons-pool + commons-pool + 1.3 + provided + + + + org.springframework + spring-jdbc + ${spring.version} + provided + + + org.springframework + spring-aop + ${spring.version} + test + + + aspectj + aspectjrt + 1.5.3 + test + + + aspectj + aspectjweaver + 1.5.3 + test + + + gsbase + gsbase + 2.0.1 + test + + + hsqldb + hsqldb + 1.8.0.7 + test + + diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/Person.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/Person.java new file mode 100644 index 00000000..50cae554 --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/Person.java @@ -0,0 +1,64 @@ +/* + * Copyright 2005-2007 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; + +/** + * Dummy bean to be used in the LdapTemplate integration tests. + * + * @author Mattias Arthursson + */ +public class Person { + private String fullname; + + private String lastname; + + private String description; + + private String phone; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getFullname() { + return fullname; + } + + public void setFullname(String fullname) { + this.fullname = fullname; + } + + public String getLastname() { + return lastname; + } + + public void setLastname(String lastname) { + this.lastname = lastname; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/PersonAttributesMapper.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/PersonAttributesMapper.java new file mode 100644 index 00000000..6b0f0a4d --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/PersonAttributesMapper.java @@ -0,0 +1,47 @@ +/* + * Copyright 2005-2007 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; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; + +import org.springframework.ldap.core.AttributesMapper; + + +/** + * Dummy implementation of AttributesMapper for use in integration tests. + * + * @author Mattias Arthursson + * + */ +public class PersonAttributesMapper implements AttributesMapper { + + /** + * Maps the given attributes into a {@link Person} object. + * + * @see org.springframework.ldap.core.AttributesMapper#mapFromAttributes(javax.naming.directory.Attributes) + */ + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + Person person = new Person(); + person.setFullname((String) attributes.get("cn").get()); + person.setLastname((String) attributes.get("sn").get()); + person.setPhone((String) attributes.get("telephoneNumber").get()); + person.setDescription((String) attributes.get("description").get()); + return person; + } +} diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/PersonContextMapper.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/PersonContextMapper.java new file mode 100644 index 00000000..da560d01 --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/PersonContextMapper.java @@ -0,0 +1,39 @@ +/* + * Copyright 2005-2007 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; + +import org.springframework.ldap.core.DirContextOperations; +import org.springframework.ldap.core.support.AbstractContextMapper; + +/** + * Dummy implemention of ContextMapper for use in the integration tests and for + * illustration purposes. + * + * @author Mattias Arthursson + */ +public class PersonContextMapper extends AbstractContextMapper { + + protected Object doMapFromContext(DirContextOperations ctx) { + Person person = new Person(); + person.setFullname(ctx.getStringAttribute("cn")); + person.setLastname(ctx.getStringAttribute("sn")); + person.setPhone(ctx.getStringAttribute("telephoneNumber")); + person.setDescription(ctx.getStringAttribute("description")); + + return person; + } +} diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyDao.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyDao.java new file mode 100644 index 00000000..7d6f0e78 --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyDao.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2007 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.transaction.compensating.manager; + +public interface DummyDao { + void createWithException(String country, String company, String fullname, + String lastname, String description); + + void create(String country, String company, String fullname, + String lastname, String description); + + void update(String dn, String fullname, String lastname, String description); + + void updateWithException(String dn, String fullname, String lastname, + String description); + + void updateAndRename(String dn, String newDn, String description); + + void updateAndRenameWithException(String dn, String newDn, + String description); + + void modifyAttributes(String dn, String lastName, String description); + + void modifyAttributesWithException(String dn, String lastName, + String description); + + void unbind(String dn, String fullname); + + void unbindWithException(String dn, String fullname); + +} diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyException.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyException.java new file mode 100644 index 00000000..dacb7839 --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyException.java @@ -0,0 +1,22 @@ +/* + * Copyright 2002-2007 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.transaction.compensating.manager; + +public class DummyException extends RuntimeException { + public DummyException(String message) { + super(message); + } +} diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyServiceImpl.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyServiceImpl.java new file mode 100644 index 00000000..f023fea5 --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/DummyServiceImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright 2002-2007 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.transaction.compensating.manager; + +public class DummyServiceImpl { + private DummyDao dummyDaoImpl; + + public void setDummyDaoImpl(DummyDao dummyDaoImpl) { + this.dummyDaoImpl = dummyDaoImpl; + } +} diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/LdapAndJdbcDummyDaoImpl.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/LdapAndJdbcDummyDaoImpl.java new file mode 100644 index 00000000..ec0b43e6 --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/LdapAndJdbcDummyDaoImpl.java @@ -0,0 +1,178 @@ +/* + * Copyright 2002-2007 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.transaction.compensating.manager; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; + +public class LdapAndJdbcDummyDaoImpl implements DummyDao { + private LdapTemplate ldapTemplate; + + private JdbcTemplate jdbcTemplate; + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public void setLdapTemplate(LdapTemplate ldapTemplate) { + this.ldapTemplate = ldapTemplate; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#createWithException(java.lang.String, + * java.lang.String, java.lang.String, java.lang.String, + * java.lang.String) + */ + public void createWithException(String country, String company, + String fullname, String lastname, String description) { + create(country, company, fullname, lastname, description); + throw new DummyException("This method failed"); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#create(java.lang.String, + * java.lang.String, java.lang.String, java.lang.String, + * java.lang.String) + */ + public void create(String country, String company, String fullname, + String lastname, String description) { + DistinguishedName dn = new DistinguishedName(); + dn.add("c", country); + dn.add("ou", company); + dn.add("cn", fullname); + + DirContextAdapter ctx = new DirContextAdapter(); + ctx.setAttributeValues("objectclass", new String[] { "top", "person" }); + ctx.setAttributeValue("cn", fullname); + ctx.setAttributeValue("sn", lastname); + ctx.setAttributeValue("description", description); + ldapTemplate.bind(dn, ctx, null); + jdbcTemplate.update("insert into PERSON values(?, ?, ?)", new Object[] { + fullname, lastname, description }); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#update(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void update(String dn, String fullname, String lastname, + String description) { + DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); + ctx.setAttributeValue("sn", lastname); + ctx.setAttributeValue("description", description); + ctx.update(); + + ldapTemplate.rebind(dn, ctx, null); + jdbcTemplate + .update( + "update PERSON set lastname=?, description = ? where fullname = ?", + new Object[] { lastname, description, fullname }); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#updateWithException(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void updateWithException(String dn, String fullname, + String lastname, String description) { + update(dn, fullname, lastname, description); + throw new DummyException("This method failed."); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#updateAndRename(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void updateAndRename(String dn, String newDn, String description) { + DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); + ctx.setAttributeValue("description", description); + ctx.update(); + + ldapTemplate.rebind(dn, ctx, null); + ldapTemplate.rename(dn, newDn); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#updateAndRenameWithException(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void updateAndRenameWithException(String dn, String newDn, + String description) { + updateAndRename(dn, newDn, description); + throw new DummyException("This method failed."); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributes(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void modifyAttributes(String dn, String lastName, String description) { + DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); + ctx.setAttributeValue("sn", lastName); + ctx.setAttributeValue("description", description); + + ldapTemplate.modifyAttributes(dn, ctx.getModificationItems()); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributesWithException(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void modifyAttributesWithException(String dn, String lastName, + String description) { + modifyAttributes(dn, lastName, description); + throw new DummyException("This method failed."); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#unbind(java.lang.String) + */ + public void unbind(String dn, String fullname) { + ldapTemplate.unbind(dn); + jdbcTemplate.update("delete from PERSON where fullname=?", + new Object[] { fullname }); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#unbindWithException(java.lang.String) + */ + public void unbindWithException(String dn, String fullname) { + unbind(dn, fullname); + throw new DummyException("This operation failed."); + } +} diff --git a/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/LdapDummyDaoImpl.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/LdapDummyDaoImpl.java new file mode 100644 index 00000000..4db82c0a --- /dev/null +++ b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/transaction/compensating/manager/LdapDummyDaoImpl.java @@ -0,0 +1,163 @@ +/* + * Copyright 2002-2007 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.transaction.compensating.manager; + +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; + +public class LdapDummyDaoImpl implements DummyDao { + private LdapTemplate ldapTemplate; + + public void setLdapTemplate(LdapTemplate ldapTemplate) { + this.ldapTemplate = ldapTemplate; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#createWithException(java.lang.String, + * java.lang.String, java.lang.String, java.lang.String, + * java.lang.String) + */ + public void createWithException(String country, String company, + String fullname, String lastname, String description) { + create(country, company, fullname, lastname, description); + throw new DummyException("This method failed"); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#create(java.lang.String, + * java.lang.String, java.lang.String, java.lang.String, + * java.lang.String) + */ + public void create(String country, String company, String fullname, + String lastname, String description) { + DistinguishedName dn = new DistinguishedName(); + dn.add("c", country); + dn.add("ou", company); + dn.add("cn", fullname); + + DirContextAdapter ctx = new DirContextAdapter(); + ctx.setAttributeValues("objectclass", new String[] { "top", "person" }); + ctx.setAttributeValue("cn", fullname); + ctx.setAttributeValue("sn", lastname); + ctx.setAttributeValue("description", description); + ldapTemplate.bind(dn, ctx, null); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#update(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void update(String dn, String fullname, String lastname, + String description) { + DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); + ctx.setAttributeValue("sn", lastname); + ctx.setAttributeValue("description", description); + ctx.update(); + + ldapTemplate.rebind(dn, ctx, null); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#updateWithException(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void updateWithException(String dn, String fullname, + String lastname, String description) { + update(dn, fullname, lastname, description); + throw new DummyException("This method failed."); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#updateAndRename(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void updateAndRename(String dn, String newDn, String description) { + DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); + ctx.setAttributeValue("description", description); + ctx.update(); + + ldapTemplate.rebind(dn, ctx, null); + ldapTemplate.rename(dn, newDn); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#updateAndRenameWithException(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void updateAndRenameWithException(String dn, String newDn, + String description) { + updateAndRename(dn, newDn, description); + throw new DummyException("This method failed."); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributes(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void modifyAttributes(String dn, String lastName, String description) { + DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); + ctx.setAttributeValue("sn", lastName); + ctx.setAttributeValue("description", description); + + ldapTemplate.modifyAttributes(dn, ctx.getModificationItems()); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributesWithException(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void modifyAttributesWithException(String dn, String lastName, + String description) { + modifyAttributes(dn, lastName, description); + throw new DummyException("This method failed."); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#unbind(java.lang.String) + */ + public void unbind(String dn, String fullname) { + ldapTemplate.unbind(dn); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.ldap.transaction.support.DummyDao#unbindWithException(java.lang.String) + */ + public void unbindWithException(String dn, String fullname) { + unbind(dn, fullname); + throw new DummyException("This operation failed."); + } +} diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/log4j.properties b/mvn-build/integration-tests/src/test/java/log4j.properties similarity index 100% rename from mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/log4j.properties rename to mvn-build/integration-tests/src/test/java/log4j.properties diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java new file mode 100644 index 00000000..e09c0e94 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2005-2007 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; + +import org.springframework.test.AbstractDependencyInjectionSpringContextTests; + +public abstract class AbstractLdapTemplateIntegrationTest extends + AbstractDependencyInjectionSpringContextTests { + + + protected void onSetUp() throws Exception { + super.onSetUp(); + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java new file mode 100644 index 00000000..8acd316f --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2005-2007 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; + +import java.util.LinkedList; +import java.util.List; + +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attributes; + +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Tests the attributes mapper search method. + * + * @author Mattias Arthursson + */ +public class LdapTemplateAttributesMapperITest extends + AbstractLdapTemplateIntegrationTest { + private LdapTemplate tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + public void testSearch_AttributeMapper() throws Exception { + AttributesMapper mapper = new PersonAttributesMapper(); + List result = tested.search("ou=company1,c=Sweden", + "(&(objectclass=person)(sn=Person2))", mapper); + + assertEquals(1, result.size()); + Person person = (Person) result.get(0); + assertEquals("Some Person2", person.getFullname()); + assertEquals("Person2", person.getLastname()); + assertEquals("Sweden, Company1, Some Person2", person.getDescription()); + } + + /** + * Demonstrates how to retrieve all values of a multi-value attribute. + * + * @see LdapTemplateContextMapperITest#testSearch_ContextMapper_MultiValue() + */ + public void testSearch_AttributesMapper_MultiValue() throws Exception { + AttributesMapper mapper = new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + LinkedList list = new LinkedList(); + NamingEnumeration enumeration = attributes.get("uniqueMember").getAll(); + while (enumeration.hasMoreElements()) { + String value = (String) enumeration.nextElement(); + list.add(value); + } + String[] members = (String[]) list.toArray(new String[0]); + return members; + } + }; + List result = tested.search("ou=groups", + "(objectclass=groupOfUniqueNames)", mapper); + + assertEquals(2, result.size()); + assertEquals(1, ((String[]) result.get(0)).length); + assertEquals(5, ((String[]) result.get(1)).length); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java new file mode 100644 index 00000000..42297c48 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java @@ -0,0 +1,119 @@ +/* + * Copyright 2005-2007 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; + +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; + +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Tests the bind and unbind methods of LdapTemplate. The test methods in this + * class tests a little too much, but we need to clean up after binding, so the + * most efficient way to test is to do it all in one test method. Also, the + * methods in this class relies on that the lookup method works as it should - + * that should be ok, since that is verified in a separate test class. NOTE: if + * any of the tests in this class fails, it may be necessary to run the cleanup + * script as described in README.txt under /src/iutest/. + * + * @author Mattias Arthursson + */ +public class LdapTemplateBindUnbindITest extends + AbstractLdapTemplateIntegrationTest { + private LdapTemplate tested; + + private static String DN = "cn=Some Person4,ou=company1,c=Sweden"; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + public void testBindAndUnbind_Attributes_Plain() { + Attributes attributes = setupAttributes(); + tested.bind(DN, null, attributes); + verifyBoundCorrectData(); + tested.unbind(DN); + verifyCleanup(); + } + + public void testBindAndUnbind_Attributes_DIstinguishedName() { + Attributes attributes = setupAttributes(); + tested.bind(new DistinguishedName(DN), null, attributes); + verifyBoundCorrectData(); + tested.unbind(new DistinguishedName(DN)); + verifyCleanup(); + } + + public void testBindAndUnbind_DirContextAdapter_Plain() { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); + + tested.bind(DN, adapter, null); + verifyBoundCorrectData(); + tested.unbind(DN); + verifyCleanup(); + } + + public void testBindAndUnbind_DirContextAdapter_DIstinguishedName() { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); + + tested.bind(new DistinguishedName(DN), adapter, null); + verifyBoundCorrectData(); + tested.unbind(new DistinguishedName(DN)); + verifyCleanup(); + } + + private Attributes setupAttributes() { + Attributes attributes = new BasicAttributes(); + BasicAttribute ocattr = new BasicAttribute("objectclass"); + ocattr.add("top"); + ocattr.add("person"); + attributes.put(ocattr); + attributes.put("cn", "Some Person4"); + attributes.put("sn", "Person4"); + return attributes; + } + + private void verifyBoundCorrectData() { + DirContextAdapter result = (DirContextAdapter) tested.lookup(DN); + assertEquals("Some Person4", result.getStringAttribute("cn")); + assertEquals("Person4", result.getStringAttribute("sn")); + } + + private void verifyCleanup() { + try { + tested.lookup(DN); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextExecutorTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextExecutorTest.java new file mode 100644 index 00000000..8561d09d --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextExecutorTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2005-2007 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; + +import javax.naming.NamingException; +import javax.naming.directory.DirContext; + +import org.springframework.ldap.core.ContextExecutor; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Tests for LdapTemplate's context executor methods. + * + * @author Mattias Arthursson + */ +public class LdapTemplateContextExecutorTest extends + AbstractLdapTemplateIntegrationTest { + + private LdapTemplate tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + super.onSetUp(); + } + + protected void onTearDown() throws Exception { + super.onTearDown(); + } + + public void testLookupLink() { + ContextExecutor executor = new ContextExecutor(){ + public Object executeWithContext(DirContext ctx) throws NamingException { + return ctx.lookupLink("cn=Some Person,ou=company1,c=Sweden"); + } + }; + + Object object = tested.executeReadOnly(executor); + assertTrue("Should be a DirContextAdapter", object instanceof DirContextAdapter); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextMapperITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextMapperITest.java new file mode 100644 index 00000000..d2ca77b2 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextMapperITest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2005-2007 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; + +import java.util.List; + +import org.springframework.ldap.core.ContextMapper; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Tests the ContextMapper search method. In its way this method also + * demonstrates the use of DirContextAdapter and the DirObjectFactory. + * + * @author Mattias Arthursson + */ +public class LdapTemplateContextMapperITest extends + AbstractLdapTemplateIntegrationTest { + private LdapTemplate tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + /** + * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) + * being set in the ContextSource. + */ + public void testSearch_ContextMapper() { + ContextMapper mapper = new PersonContextMapper(); + List result = tested.search("ou=company1,c=Sweden", + "(&(objectclass=person)(sn=Person2))", mapper); + + assertEquals(1, result.size()); + Person person = (Person) result.get(0); + assertEquals("Some Person2", person.getFullname()); + assertEquals("Person2", person.getLastname()); + assertEquals("Sweden, Company1, Some Person2", person.getDescription()); + } + + /** + * Demonstrates how to retrieve all values of a multi-value attribute. + * + * @see LdapTemplateAttributesMapperITest#testSearch_AttributesMapper_MultiValue() + */ + public void testSearch_ContextMapper_MultiValue() throws Exception { + ContextMapper mapper = new ContextMapper() { + public Object mapFromContext(Object ctx) { + DirContextAdapter adapter = (DirContextAdapter) ctx; + String[] members = adapter.getStringAttributes("uniqueMember"); + return members; + } + }; + List result = tested.search("ou=groups", + "(objectclass=groupOfUniqueNames)", mapper); + + assertEquals(2, result.size()); + assertEquals(1, ((String[]) result.get(0)).length); + assertEquals(5, ((String[]) result.get(1)).length); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java new file mode 100644 index 00000000..58d0e27c --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java @@ -0,0 +1,148 @@ +/* + * Copyright 2005-2007 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; + +import java.util.List; + +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; +import org.springframework.ldap.test.AttributeCheckContextMapper; + +/** + * Tests for LdapTemplate's list methods. + * + * @author Ulrik Sandberg + */ +public class LdapTemplateListITest extends + AbstractLdapTemplateIntegrationTest { + + private LdapTemplate tested; + + private AttributeCheckContextMapper contextMapper; + + private static final String BASE_STRING = ""; + + private static final DistinguishedName BASE_NAME = new DistinguishedName( + BASE_STRING); + + private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", + "telephoneNumber" }; + + private static final String[] ALL_VALUES = { "Some Person", "Person", + "Sweden, Company2, Some Person", "+46 555-456321" }; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + super.onSetUp(); + + contextMapper = new AttributeCheckContextMapper(); + } + + protected void onTearDown() throws Exception { + super.onTearDown(); + + contextMapper = null; + } + + public void testListBindings_ContextMapper() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.listBindings("ou=company2,c=Sweden" + BASE_STRING, + contextMapper); + assertEquals(1, list.size()); + } + + public void testListBindings_ContextMapper_Name() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + DistinguishedName dn = new DistinguishedName("ou=company2,c=Sweden"); + dn.append(BASE_NAME); + List list = tested.listBindings(dn, contextMapper); + assertEquals(1, list.size()); + } + + public void testListBindings_ContextMapper_MapToPersons() { + DistinguishedName dn = new DistinguishedName("ou=company1,c=Sweden"); + dn.append(BASE_NAME); + List list = tested.listBindings(dn, new PersonContextMapper()); + assertEquals(3, list.size()); + String personClass = "org.springframework.ldap.Person"; + assertEquals(personClass, list.get(0).getClass().getName()); + assertEquals(personClass, list.get(1).getClass().getName()); + assertEquals(personClass, list.get(2).getClass().getName()); + } + + public void testList() { + List list = tested.list(BASE_STRING); + assertEquals(3, list.size()); + assertTrue(list.contains("ou=groups")); + assertTrue(list.contains("c=Norway")); + assertTrue(list.contains("c=Sweden")); + } + + public void testList_Name() { + List list = tested.list(BASE_NAME); + assertEquals(3, list.size()); + assertTrue(list.contains("ou=groups")); + assertTrue(list.contains("c=Norway")); + assertTrue(list.contains("c=Sweden")); + } + + public void testList_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.list(BASE_STRING, handler); + assertEquals(3, handler.getNoOfRows()); + } + + public void testList_Name_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.list(BASE_NAME, handler); + assertEquals(3, handler.getNoOfRows()); + } + + public void testListBindings() { + List list = tested.listBindings(BASE_STRING); + assertEquals(3, list.size()); + assertTrue(list.contains("ou=groups")); + assertTrue(list.contains("c=Norway")); + assertTrue(list.contains("c=Sweden")); + } + + public void testListBindings_Name() { + List list = tested.listBindings(BASE_NAME); + assertEquals(3, list.size()); + } + + public void testListBindings_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.listBindings(BASE_STRING, handler); + assertEquals(3, handler.getNoOfRows()); + } + + public void testListBindings_Name_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.listBindings(BASE_NAME, handler); + assertEquals(3, handler.getNoOfRows()); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java new file mode 100644 index 00000000..cb479a9b --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java @@ -0,0 +1,245 @@ +/* + * Copyright 2005-2007 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; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; + +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.ContextMapper; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.core.support.AbstractContextSource; + +/** + * Tests the lookup methods of LdapTemplate. + * + * @author Mattias Arthursson + * @author Ulrik Sandberg + */ +public class LdapTemplateLookupITest extends + AbstractLdapTemplateIntegrationTest { + + private LdapTemplate tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + /** + * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) + * being set in the ContextSource. + */ + public void testLookup_Plain() { + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person2, ou=company1,c=Sweden"); + + assertEquals("Some Person2", result.getStringAttribute("cn")); + assertEquals("Person2", result.getStringAttribute("sn")); + assertEquals("Sweden, Company1, Some Person2", result + .getStringAttribute("description")); + } + + public void testLookup_AttributesMapper() { + AttributesMapper mapper = new PersonAttributesMapper(); + Person person = (Person) tested.lookup( + "cn=Some Person2, ou=company1,c=Sweden", mapper); + + assertEquals("Some Person2", person.getFullname()); + assertEquals("Person2", person.getLastname()); + assertEquals("Sweden, Company1, Some Person2", person.getDescription()); + } + + public void testLookup_AttributesMapper_DistinguishedName() { + AttributesMapper mapper = new PersonAttributesMapper(); + Person person = (Person) tested.lookup(new DistinguishedName( + "cn=Some Person2, ou=company1,c=Sweden"), mapper); + + assertEquals("Some Person2", person.getFullname()); + assertEquals("Person2", person.getLastname()); + assertEquals("Sweden, Company1, Some Person2", person.getDescription()); + } + + /** + * An {@link AttributesMapper} that only maps a subset of the full + * attributes list. Used in tests where the return attributes list has been + * limited. + * + * @author Ulrik Sandberg + */ + private final class SubsetPersonAttributesMapper implements + AttributesMapper { + /** + * Maps the cn attribute into a {@link Person} object. + * Also verifies that the other attributes haven't been set. + * + * @see org.springframework.ldap.core.AttributesMapper#mapFromAttributes(javax.naming.directory.Attributes) + */ + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + Person person = new Person(); + person.setFullname((String) attributes.get("cn").get()); + assertNull("sn should be null", attributes.get("sn")); + assertNull("description should be null", attributes + .get("description")); + return person; + } + } + + /** + * Verifies that only the subset is used when specifying a subset of the + * available attributes as return attributes. + */ + public void testLookup_ReturnAttributes_AttributesMapper() { + AttributesMapper mapper = new SubsetPersonAttributesMapper(); + + Person person = (Person) tested.lookup( + "cn=Some Person2, ou=company1,c=Sweden", new String[] { "cn" }, + mapper); + + assertEquals("Some Person2", person.getFullname()); + assertNull("lastName should not be set", person.getLastname()); + assertNull("description should not be set", person.getDescription()); + } + + /** + * Verifies that only the subset is used when specifying a subset of the + * available attributes as return attributes. Uses DistinguishedName instead + * of plain string as name. + */ + public void testLookup_ReturnAttributes_AttributesMapper_DistinguishedName() { + AttributesMapper mapper = new SubsetPersonAttributesMapper(); + Person person = (Person) tested.lookup(new DistinguishedName( + "cn=Some Person2, ou=company1,c=Sweden"), + new String[] { "cn" }, mapper); + + assertEquals("Some Person2", person.getFullname()); + assertNull("lastName should not be set", person.getLastname()); + assertNull("description should not be set", person.getDescription()); + } + + /** + * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) + * being set in the ContextSource. + */ + public void testLookup_ContextMapper() { + ContextMapper mapper = new PersonContextMapper(); + Person person = (Person) tested.lookup( + "cn=Some Person2, ou=company1,c=Sweden", mapper); + + assertEquals("Some Person2", person.getFullname()); + assertEquals("Person2", person.getLastname()); + assertEquals("Sweden, Company1, Some Person2", person.getDescription()); + } + + /** + * Verifies that only the subset is used when specifying a subset of the + * available attributes as return attributes. + */ + public void testLookup_ReturnAttributes_ContextMapper() { + ContextMapper mapper = new PersonContextMapper(); + + Person person = (Person) tested.lookup( + "cn=Some Person2, ou=company1,c=Sweden", new String[] { "cn" }, + mapper); + + assertEquals("Some Person2", person.getFullname()); + assertNull("lastName should not be set", person.getLastname()); + assertNull("description should not be set", person.getDescription()); + } + + /** + * Verifies that we can lookup an entry that has a multi-valued rdn, which + * means more than one attribute is part of the relative DN for the entry. + * + * TODO Enable test when ApacheDS supports multi-valued rdns. + */ + public void DISABLED_testLookup_MultiValuedRdn() { + AttributesMapper mapper = new PersonAttributesMapper(); + Person person = (Person) tested.lookup( + "cn=Some Person+sn=Person, ou=company1,c=Norway", mapper); + + assertEquals("Some Person", person.getFullname()); + assertEquals("Person", person.getLastname()); + assertEquals("Norway, Company1, Some Person2", person.getDescription()); + } + + /** + * Verifies that we can lookup an entry that has a multi-valued rdn, which + * means more than one attribute is part of the relative DN for the entry. + * + * TODO Enable test when ApacheDS supports multi-valued rdns. + */ + public void DISABLED_testLookup_MultiValuedRdn_DirContextAdapter() { + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person+sn=Person, ou=company1,c=Norway"); + + assertEquals("Some Person", result.getStringAttribute("cn")); + assertEquals("Person", result.getStringAttribute("sn")); + assertEquals("Norway, Company1, Some Person", result + .getStringAttribute("description")); + } + + public void testLookup_GetNameInNamespace_Plain() { + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person2, ou=company1,c=Sweden"); + + assertEquals("cn=Some Person2, ou=company1, c=Sweden", result.getDn() + .toString()); + assertEquals( + "cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", + result.getNameInNamespace()); + } + + public void testLookup_GetNameInNamespace_MultiRdn() { + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person+sn=Person, ou=company1,c=Norway"); + + assertEquals("cn=Some Person+sn=Person, ou=company1, c=Norway", result + .getDn().toString()); + assertEquals( + "cn=Some Person+sn=Person, ou=company1, c=Norway, dc=jayway, dc=se", + result.getNameInNamespace()); + } + + /** + * Tests bind and lookup with Java objects where the ContextSource already + * has a DirObjectFactory configured. + */ + public void testBindJavaObject() throws Exception { + AbstractContextSource contextSource = (AbstractContextSource) tested + .getContextSource(); + Class originalObjectFactory = contextSource.getDirObjectFactory(); + try { + contextSource.setDirObjectFactory(null); + contextSource.afterPropertiesSet(); + tested.bind("cn=myRandomInt", new Integer(54321), null); + Integer result = (Integer) tested.lookup("cn=myRandomInt"); + assertEquals(54321, result.intValue()); + } finally { + // reset the DirObjectFactory so as not to disturb other tests + tested.unbind("cn=myRandomInt"); + contextSource.setDirObjectFactory(originalObjectFactory); + contextSource.afterPropertiesSet(); + } + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java new file mode 100644 index 00000000..bf5560c6 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java @@ -0,0 +1,253 @@ +/* + * Copyright 2005-2007 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; + +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 org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Tests the modification methods (rebind and modifyAttributes) of LdapTemplate. + * It also illustrates the use of DirContextAdapter as a means of getting + * ModificationItems, in order to avoid doing a full rebind and use + * modifyAttributes() instead. We rely on that the bind, unbind and lookup + * methods work as they should - that should be ok, since that is verified in a + * separate test class. NOTE: if any of the tests in this class fails, it may be + * necessary to run the cleanup script as described in README.txt under + * /src/iutest/. + * + * @author Mattias Arthursson + * @author Ulrik Sandberg + */ +public class LdapTemplateModifyITest extends + AbstractLdapTemplateIntegrationTest { + private LdapTemplate tested; + + private static String PERSON4_DN = "cn=Some Person4,ou=company1,c=Sweden"; + + private static String PERSON5_DN = "cn=Some Person5,ou=company1,c=Sweden"; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + super.onSetUp(); + + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); + adapter.setAttributeValue("description", "Some description"); + + tested.bind(PERSON4_DN, adapter, null); + + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "Some Person5"); + adapter.setAttributeValue("sn", "Person5"); + adapter.setAttributeValues("description", new String[] { "qwe", "123", + "rty", "uio" }); + + tested.bind(PERSON5_DN, adapter, null); + + } + + protected void onTearDown() throws Exception { + tested.unbind(PERSON4_DN); + tested.unbind(PERSON5_DN); + } + + public void testRebind_Attributes_Plain() { + Attributes attributes = setupAttributes(); + + tested.rebind(PERSON4_DN, null, attributes); + + verifyBoundCorrectData(); + } + + public void testRebind_Attributes_DistinguishedName() { + Attributes attributes = setupAttributes(); + + tested.rebind(new DistinguishedName(PERSON4_DN), null, attributes); + + verifyBoundCorrectData(); + } + + public void testModifyAttributes_MultiValueReplace() { + BasicAttribute attr = new BasicAttribute("description", + "Some other description"); + attr.add("Another description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr); + + tested.modifyAttributes(PERSON4_DN, mods); + + DirContextAdapter result = (DirContextAdapter) tested + .lookup(PERSON4_DN); + String[] attributes = result.getStringAttributes("description"); + assertEquals(2, attributes.length); + assertEquals("Some other description", attributes[0]); + assertEquals("Another description", attributes[1]); + } + + public void testModifyAttributes_MultiValueAdd() { + BasicAttribute attr = new BasicAttribute("description", + "Some other description"); + attr.add("Another description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); + + tested.modifyAttributes(PERSON4_DN, mods); + + DirContextAdapter result = (DirContextAdapter) tested + .lookup(PERSON4_DN); + String[] attributes = result.getStringAttributes("description"); + assertEquals(3, attributes.length); + assertEquals("Some description", attributes[0]); + assertEquals("Some other description", attributes[1]); + assertEquals("Another description", attributes[2]); + } + + public void testModifyAttributes_MultiValueAddDuplicateToUnordered() { + BasicAttribute attr = new BasicAttribute("description", + "Some description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); + + try { + tested.modifyAttributes(PERSON4_DN, mods); + fail("AttributeInUseException expected"); + } catch (AttributeInUseException expected) { + // expected + } + } + + /** + * Test written originally to verify that duplicates are allowed on ordered + * attributes, but had to be changed since Apache DS seems to disallow + * duplicates even for ordered attributes. + */ + public void testModifyAttributes_MultiValueAddDuplicateToOrdered() { + BasicAttribute attr = new BasicAttribute("description", + "Some other description", true); // ordered + attr.add("Another description"); + // Commented out duplicate to make test work for Apache DS + // attr.add("Some description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); + + tested.modifyAttributes(PERSON4_DN, mods); + + DirContextAdapter result = (DirContextAdapter) tested + .lookup(PERSON4_DN); + String[] attributes = result.getStringAttributes("description"); + assertEquals(3, attributes.length); + assertEquals("Some description", attributes[0]); + assertEquals("Some other description", attributes[1]); + assertEquals("Another description", attributes[2]); + } + + public void testModifyAttributes_Plain() { + ModificationItem item = new ModificationItem( + DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", + "Some other description")); + + tested.modifyAttributes(PERSON4_DN, new ModificationItem[] { item }); + + verifyBoundCorrectData(); + } + + public void testModifyAttributes_DistinguishedName() { + ModificationItem item = new ModificationItem( + DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", + "Some other description")); + + tested.modifyAttributes(new DistinguishedName(PERSON4_DN), + new ModificationItem[] { item }); + + verifyBoundCorrectData(); + } + + public void testModifyAttributes_DirContextAdapter_MultiAttributes() { + DirContextAdapter adapter = (DirContextAdapter) tested + .lookup(PERSON5_DN); + adapter.setAttributeValues("description", new String[] { "qwe", "123", + "klytt", "kalle" }); + + tested.modifyAttributes(PERSON5_DN, adapter.getModificationItems()); + + // Verify + adapter = (DirContextAdapter) tested.lookup(PERSON5_DN); + String[] attributes = adapter.getStringAttributes("description"); + assertEquals(4, attributes.length); + assertEquals("qwe", attributes[0]); + assertEquals("123", attributes[1]); + assertEquals("klytt", attributes[2]); + assertEquals("kalle", attributes[3]); + } + + /** + * Demonstrates how the DirContextAdapter can be used to automatically keep + * track of changes of the attributes and deliver ModificationItems to use + * in moifyAttributes(). + */ + public void testModifyAttributes_DirContextAdapter() throws Exception { + DirContextAdapter adapter = (DirContextAdapter) tested + .lookup(PERSON4_DN); + + adapter.setAttributeValue("description", "Some other description"); + + ModificationItem[] modificationItems = adapter.getModificationItems(); + tested.modifyAttributes(PERSON4_DN, modificationItems); + + verifyBoundCorrectData(); + } + + private Attributes setupAttributes() { + Attributes attributes = new BasicAttributes(); + BasicAttribute ocattr = new BasicAttribute("objectclass"); + ocattr.add("top"); + ocattr.add("person"); + attributes.put(ocattr); + attributes.put("cn", "Some Person4"); + attributes.put("sn", "Person4"); + attributes.put("description", "Some other description"); + return attributes; + } + + private void verifyBoundCorrectData() { + DirContextAdapter result = (DirContextAdapter) tested + .lookup(PERSON4_DN); + assertEquals("Some Person4", result.getStringAttribute("cn")); + assertEquals("Person4", result.getStringAttribute("sn")); + assertEquals("Some other description", result + .getStringAttribute("description")); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java new file mode 100644 index 00000000..f82a2e6d --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2005-2007 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; + +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; + +/** + * Tests to verify that not setting a base suffix on the ContextSource (as + * defined in ldapTemplateNoBaseSuffixTestContext.xml) works as expected. + * + * NOTE: This test will not work under Java 1.4.1 or earlier. + * + * @author Mattias Arthursson + */ +public class LdapTemplateNoBaseSuffixITest extends + AbstractLdapTemplateIntegrationTest { + private LdapTemplate tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateNoBaseSuffixTestContext.xml" }; + } + + /** + * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) + * being set in the ContextSource. + */ + public void testLookup_Plain() { + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se"); + + assertEquals("Some Person2", result.getStringAttribute("cn")); + assertEquals("Person2", result.getStringAttribute("sn")); + assertEquals("Sweden, Company1, Some Person2", result + .getStringAttribute("description")); + assertEquals( + "cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", + result.getDn().toString()); + assertEquals( + "cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", + result.getNameInNamespace()); + } + + public void testSearch_Plain() { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + + tested.search("dc=jayway, dc=se", "(objectclass=person)", handler); + assertEquals(5, handler.getNoOfRows()); + } + + public void testBindAndUnbind_Plain() { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); + tested.bind("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se", + adapter, null); + + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); + + assertEquals("Some Person4", result.getStringAttribute("cn")); + assertEquals("Person4", result.getStringAttribute("sn")); + assertEquals( + "cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se", + result.getDn().toString()); + + tested + .unbind("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); + try { + tested + .lookup("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateObjectBindIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateObjectBindIntegrationTest.java new file mode 100644 index 00000000..f4b92d3d --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateObjectBindIntegrationTest.java @@ -0,0 +1,145 @@ +/* + * Copyright 2005-2007 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; + +import java.io.Serializable; +import java.util.LinkedList; + +import javax.naming.directory.DirContext; + +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.core.support.AbstractContextSource; + +/** + * Tests bind and lookup with Java objects where the ContextSource has a + * null DirObjectFactory configured. + * + * @author Ulrik Sandberg + */ +public class LdapTemplateObjectBindIntegrationTest extends + AbstractLdapTemplateIntegrationTest { + + private LdapTemplate tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateObjectBindTestContext.xml" }; + } + + public void testBindJavaObject() throws Exception { + String dn = "cn=myRandomInt"; + tested.bind(dn, new Integer(54321), null); + + Integer result = (Integer) tested.lookup(dn); + assertEquals(54321, result.intValue()); + tested.unbind(dn); + } + + public void testBindLinkedList() { + LinkedList list = new LinkedList(); + list.add(new Integer(54321)); + list.add(new Integer(67890)); + + String dn = "cn=myRandomList"; + tested.bind(dn, list, null); + + LinkedList result = (LinkedList) tested.lookup(dn); + assertEquals(2, result.size()); + assertEquals(54321, ((Integer) result.get(0)).intValue()); + assertEquals(67890, ((Integer) result.get(1)).intValue()); + tested.unbind(dn); + } + + public void testBindNonSerializableJavaObjectShouldFail() throws Exception { + NonSerializablePojo pojo = new NonSerializablePojo(); + pojo.setName("A Name"); + + try { + tested.bind("cn=myRandomObject", pojo, null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertEquals( + "can only bind Referenceable, Serializable, DirContext", + expected.getMessage()); + } + } + + /** + * Custom non-serializable class used for demonstrating bind and lookup of + * Java objects. + */ + public static class NonSerializablePojo { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + public void testBindSerializableJavaObjectShouldSucceed() throws Exception { + SerializablePojo pojo = new SerializablePojo(); + pojo.setName("A Name"); + + tested.bind("cn=myRandomObject", pojo, null); + + SerializablePojo result = (SerializablePojo) tested + .lookup("cn=myRandomObject"); + assertEquals("A Name", result.getName()); + tested.unbind("cn=myRandomObject"); + } + + /** + * Custom serializable class used for demonstrating bind and lookup of Java + * objects. + */ + public static class SerializablePojo implements Serializable { + private static final long serialVersionUID = 3655768927093734908L; + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + /** + * This test demonstrates that it's fully possible to perform plain JNDI + * operations from Spring LDAP. + */ + public void testPlainJndiBindJavaObject() throws Exception { + AbstractContextSource contextSource = (AbstractContextSource) tested + .getContextSource(); + DirContext ctx = contextSource.getReadWriteContext(); + + ctx.bind("cn=myRandomInt", new Integer(28420)); + + Integer result = (Integer) ctx.lookup("cn=myRandomInt"); + assertEquals(28420, result.intValue()); + tested.unbind("cn=myRandomInt"); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java new file mode 100644 index 00000000..e43e14aa --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java @@ -0,0 +1,127 @@ +/* + * Copyright 2005-2007 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; + +import javax.naming.Name; + +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Tests the recursive modification methods (unbind and the protected delete + * methods) of LdapTemplate. + * + * @author Mattias Arthursson + * @author Ulrik Sandberg + */ +public class LdapTemplateRecursiveDeleteITest extends + AbstractLdapTemplateIntegrationTest { + private LdapTemplate tested; + + private static DistinguishedName DN = new DistinguishedName( + "cn=Some Person5,ou=company1,c=Sweden"); + + private DistinguishedName firstSubDn; + + private DistinguishedName secondSubDn; + + private DistinguishedName leafDn; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + super.onSetUp(); + + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "Some Person5"); + adapter.setAttributeValue("sn", "Person5"); + adapter.setAttributeValue("description", "Some description"); + tested.bind(DN, adapter, null); + + firstSubDn = new DistinguishedName("cn=subPerson"); + firstSubDn.prepend(DN); + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "subPerson"); + adapter.setAttributeValue("sn", "subPerson"); + adapter.setAttributeValue("description", + "Should be recursively deleted"); + tested.bind(firstSubDn, adapter, null); + secondSubDn = new DistinguishedName("cn=subPerson2"); + secondSubDn.prepend(DN); + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "subPerson2"); + adapter.setAttributeValue("sn", "subPerson2"); + adapter.setAttributeValue("description", + "Should be recursively deleted"); + tested.bind(secondSubDn, adapter, null); + + leafDn = new DistinguishedName("cn=subSubPerson"); + leafDn.prepend(firstSubDn); + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "subSubPerson"); + adapter.setAttributeValue("sn", "subSubPerson"); + adapter.setAttributeValue("description", + "Should be recursively deleted"); + tested.bind(leafDn, adapter, null); + } + + protected void onTearDown() throws Exception { + try { + tested.unbind(DN, true); + } catch (NameNotFoundException ignore) { + // ignore + } + } + + public void testRecursiveUnbind() { + tested.unbind(DN, true); + + verifyDeleted(DN); + verifyDeleted(firstSubDn); + verifyDeleted(secondSubDn); + verifyDeleted(leafDn); + } + + public void testRecursiveUnbindOnLeaf() { + tested.unbind(leafDn, true); + verifyDeleted(leafDn); + } + + private void verifyDeleted(Name dn) { + try { + tested.lookup(dn); + fail("Expected entry '" + dn + "' to be non-existent"); + } catch (NameNotFoundException expected) { + // expected + } + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java new file mode 100644 index 00000000..64a78c51 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2005-2007 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; + +import javax.naming.Name; + +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Tests the rename methods of LdapTemplate. + * + * We rely on that the bind, unbind and lookup methods work as they should - + * that should be ok, since that is verified in a separate test class. * + * + * @author Ulrik Sandberg + */ +public class LdapTemplateRenameITest extends + AbstractLdapTemplateIntegrationTest { + private LdapTemplate tested; + + private static String DN = "cn=Some Person6,ou=company1,c=Sweden"; + + private static String NEWDN = "cn=Some Person6,ou=company2,c=Sweden"; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + super.onSetUp(); + + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", + "person" }); + adapter.setAttributeValue("cn", "Some Person6"); + adapter.setAttributeValue("sn", "Person6"); + adapter.setAttributeValue("description", "Some description"); + + tested.bind(DN, adapter, null); + } + + protected void onTearDown() throws Exception { + tested.unbind(NEWDN); + tested.unbind(DN); + } + + public void testRename() { + tested.rename(DN, NEWDN); + + verifyDeleted(new DistinguishedName(DN)); + verifyBoundCorrectData(); + } + + public void testRename_DistinguishedName() throws Exception { + Name oldDn = new DistinguishedName(DN); + Name newDn = new DistinguishedName(NEWDN); + tested.rename(oldDn, newDn); + + + verifyDeleted(oldDn); + verifyBoundCorrectData(); + } + + private void verifyDeleted(Name dn) { + try { + tested.lookup(dn); + fail("Expected entry '" + dn + "' to be non-existent"); + } catch (NameNotFoundException expected) { + // expected + } + } + + private void verifyBoundCorrectData() { + DirContextAdapter result = (DirContextAdapter) tested.lookup(NEWDN); + assertEquals("Some Person6", result.getStringAttribute("cn")); + assertEquals("Person6", result.getStringAttribute("sn")); + assertEquals("Some description", result + .getStringAttribute("description")); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchCountITest.xls b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchCountITest.xls new file mode 100644 index 00000000..a7f7e40c Binary files /dev/null and b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchCountITest.xls differ diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java new file mode 100644 index 00000000..f0f3f867 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java @@ -0,0 +1,181 @@ +/* + * Copyright 2005-2007 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; + +import java.util.List; + +import javax.naming.Name; +import javax.naming.directory.SearchControls; + +import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.test.AttributeCheckAttributesMapper; +import org.springframework.ldap.test.AttributeCheckContextMapper; +import org.springframework.test.AbstractDependencyInjectionSpringContextTests; + +/** + * Tests for LdapTemplate's search methods. This test class tests all the + * different versions of the search methods except the generic ones covered in + * other tests. + * + * @author Mattias Arthursson + */ +public class LdapTemplateSearchResultITest extends + AbstractDependencyInjectionSpringContextTests { + + private LdapTemplate tested; + + private AttributeCheckAttributesMapper attributesMapper; + + private AttributeCheckContextMapper contextMapper; + + private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", + "telephoneNumber" }; + + private static final String[] CN_SN_ATTRS = { "cn", "sn" }; + + private static final String[] ABSENT_ATTRIBUTES = { "description", + "telephoneNumber" }; + + private static final String[] CN_SN_VALUES = { "Some Person2", "Person2" }; + + private static final String[] ALL_VALUES = { "Some Person2", "Person2", + "Sweden, Company1, Some Person2", "+46 555-654321" }; + + private static final String BASE_STRING = ""; + + private static final String FILTER_STRING = "(&(objectclass=person)(sn=Person2))"; + + private static final Name BASE_NAME = new DistinguishedName(BASE_STRING); + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + super.onSetUp(); + + attributesMapper = new AttributeCheckAttributesMapper(); + contextMapper = new AttributeCheckContextMapper(); + } + + protected void onTearDown() throws Exception { + super.onTearDown(); + + attributesMapper = null; + contextMapper = null; + } + + public void testSearch_AttributesMapper() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_STRING, FILTER_STRING, attributesMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_AttributesMapper() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_STRING, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, attributesMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_LimitedAttrs_AttributesMapper() { + attributesMapper.setExpectedAttributes(CN_SN_ATTRS); + attributesMapper.setExpectedValues(CN_SN_VALUES); + attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); + List list = tested.search(BASE_STRING, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper); + assertEquals(1, list.size()); + } + + public void testSearch_AttributesMapper_Name() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_NAME, FILTER_STRING, attributesMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_AttributesMapper_Name() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_NAME, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, attributesMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_LimitedAttrs_AttributesMapper_Name() { + attributesMapper.setExpectedAttributes(CN_SN_ATTRS); + attributesMapper.setExpectedValues(CN_SN_VALUES); + attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); + List list = tested.search(BASE_NAME, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper); + assertEquals(1, list.size()); + } + + public void testSearch_ContextMapper() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_STRING, FILTER_STRING, contextMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_ContextMapper() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_STRING, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, contextMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_LimitedAttrs_ContextMapper() { + contextMapper.setExpectedAttributes(CN_SN_ATTRS); + contextMapper.setExpectedValues(CN_SN_VALUES); + contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); + List list = tested.search(BASE_STRING, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper); + assertEquals(1, list.size()); + } + + public void testSearch_ContextMapper_Name() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_NAME, FILTER_STRING, contextMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_ContextMapper_Name() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(BASE_NAME, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, contextMapper); + assertEquals(1, list.size()); + } + + public void testSearch_SearchScope_LimitedAttrs_ContextMapper_Name() { + contextMapper.setExpectedAttributes(CN_SN_ATTRS); + contextMapper.setExpectedValues(CN_SN_VALUES); + contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); + List list = tested.search(BASE_NAME, FILTER_STRING, + SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper); + assertEquals(1, list.size()); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java new file mode 100644 index 00000000..58880164 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2005-2007 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.control; + +import java.util.Arrays; + +import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; +import org.springframework.ldap.core.ContextMapper; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; + +/** + * Provides tests that verify that the server supports certain controls. + * + * @author Ulrik Sandberg + */ +public class SupportedControlsITest extends AbstractLdapTemplateIntegrationTest { + /** must use a context source that has no base set */ + private LdapTemplate tested; + + private static final String SUPPORTED_CONTROL = "supportedcontrol"; + + protected String[] getConfigLocations() { + return new String[] { "/conf/rootContextSourceTestContext.xml" }; + } + + public void testExpectedControlsSupported() throws Exception { + /** + * Maps the 'supportedcontrol' attribute to a string array. + */ + ContextMapper mapper = new ContextMapper() { + + public Object mapFromContext(Object ctx) { + DirContextAdapter adapter = (DirContextAdapter) ctx; + return adapter.getStringAttributes(SUPPORTED_CONTROL); + } + + }; + + String[] controls = (String[]) tested.lookup("", + new String[] { SUPPORTED_CONTROL }, mapper); + System.out.println(Arrays.toString(controls)); + assertEquals("Persistent Search LDAPv3 control,", + "2.16.840.1.113730.3.4.3", controls[0]); + assertEquals("Entry Change Notification LDAPv3 control,", + "2.16.840.1.113730.3.4.7", controls[1]); + assertEquals("Subentries Control,", "1.3.6.1.4.1.4203.1.10.1", + controls[2]); + assertEquals("Manage DSA IT LDAPv3 control,", + "2.16.840.1.113730.3.4.2", controls[3]); + } + + public void setTested(LdapTemplate tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorITest.java new file mode 100644 index 00000000..4450e8f8 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorITest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2005-2007 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.test.AbstractDependencyInjectionSpringContextTests; + +/** + * Integration tests for {@link DistinguishedNameEditor}. + * + * @author Mattias Arthursson + */ +public class DistinguishedNameEditorITest extends AbstractDependencyInjectionSpringContextTests { + + private DummyDistinguishedNameConsumer distinguishedNameConsumer; + + protected String[] getConfigLocations() { + return new String[] { "classpath:conf/distinguishedNameEditorTestContext.xml" }; + } + + public void testDistinguishedNameEditor() throws Exception { + assertNotNull(distinguishedNameConsumer); + DistinguishedName name = distinguishedNameConsumer.getDistinguishedName(); + assertEquals(new DistinguishedName("dc=jayway, dc=se"), name); + } + + public void setDistinguishedNameConsumer(DummyDistinguishedNameConsumer distinguishedNameConsumer) { + this.distinguishedNameConsumer = distinguishedNameConsumer; + } + +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DnParsePerformanceITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DnParsePerformanceITest.java new file mode 100644 index 00000000..6573942e --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DnParsePerformanceITest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2005-2007 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.DistinguishedName; + +import junit.framework.Test; +import junit.framework.TestCase; + +import com.clarkware.junitperf.LoadTest; + +/** + * Performance test for the {@link DistinguishedName} class. + * + * @author Ulrik Sandberg + */ +public class DnParsePerformanceITest extends TestCase { + + public DnParsePerformanceITest(String name) { + super(name); + } + + /** + * 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"); + } + + public static Test suite() { + int users = 1800; + Test testCase = new DnParsePerformanceITest("testContains"); + Test loadTest = new LoadTest(testCase, users); + return loadTest; + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } +} \ No newline at end of file diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java new file mode 100644 index 00000000..0b9c800d --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java @@ -0,0 +1,34 @@ +/* + * Copyright 2005-2007 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; + +/** + * Dummy implementation of a class that has a {@link DistinguishedName} setter, + * used for testing purposes. + * + * @author Mattias Arthursson + */ +public class DummyDistinguishedNameConsumer { + private DistinguishedName distinguishedName; + + public DistinguishedName getDistinguishedName() { + return distinguishedName; + } + + public void setDistinguishedName(DistinguishedName distinguishedName) { + this.distinguishedName = distinguishedName; + } +} diff --git a/mvn-build/integration-tests/tiger-integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java similarity index 94% rename from mvn-build/integration-tests/tiger-integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java rename to mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java index 6da647cd..2a2d3eed 100644 --- a/mvn-build/integration-tests/tiger-integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java @@ -26,10 +26,10 @@ public class SimpleLdapTemplateITest extends AbstractDependencyInjectionSpringCo private SimpleLdapTemplate ldapTemplate; protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; + return new String[] { "/conf/simpleLdapTemplateTestContext.xml" }; } - public void setSimplLdapTemplate(SimpleLdapTemplate ldapTemplate) { + public void setSimpleLdapTemplate(SimpleLdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java new file mode 100644 index 00000000..ea001ef6 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2005-2007 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 junit.framework.TestCase; + +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.ldap.core.DistinguishedName; + +/** + * Integration tests for {@link BaseLdapPathBeanPostProcessor}. + * + * @author Mattias Arthursson + */ +public class BaseLdapPathBeanPostprocessorITest extends TestCase { + + protected String[] getConfigLocations() { + return new String[] { "/conf/baseLdapPathPostProcessorTestContext.xml" }; + } + + public void testPostProcessBeforeInitialization() throws Exception { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( + "/conf/baseLdapPathPostProcessorTestContext.xml"); + DummyBaseLdapPathAware tested = (DummyBaseLdapPathAware) ctx.getBean("dummyBaseContextAware"); + + DistinguishedName base = tested.getBase(); + assertNotNull(base); + assertEquals(new DistinguishedName("dc=jayway,dc=se"), base); + } + + public void testPostProcessBeforeInitializationMultipleContextSources() throws Exception { + try { + new ClassPathXmlApplicationContext("/conf/baseLdapPathPostProcessorMultiContextSourceTestContext.xml"); + fail("BeanCreationException expected"); + } + catch (BeanCreationException expected) { + Throwable cause = expected.getCause(); + assertTrue(cause instanceof NoSuchBeanDefinitionException); + } + } + + public void testPostProcessBeforeInitializationMultipleContextSourcesOneSpecified() throws Exception { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( + "/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml"); + DummyBaseLdapPathAware tested = (DummyBaseLdapPathAware) ctx.getBean("dummyBaseContextAware"); + + DistinguishedName base = tested.getBase(); + assertNotNull(base); + assertEquals(new DistinguishedName("cn=john doe,dc=jayway,dc=se"), base); + } + + public void testPostProcessBeforeInitializationNoContextSource() throws Exception { + try { + new ClassPathXmlApplicationContext("/conf/baseLdapPathPostProcessorNoContextSourceTestContext.xml"); + fail("BeanCreationException expected"); + } + catch (BeanCreationException expected) { + Throwable cause = expected.getCause(); + assertTrue(cause instanceof NoSuchBeanDefinitionException); + } + } + + public void testPostProcessBeforeInitializationBaseSetInProperty() throws Exception { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( + "/conf/baseLdapPathPostProcessorPropertyOverrideTestContext.xml"); + DummyBaseLdapPathAware tested = (DummyBaseLdapPathAware) ctx.getBean("dummyBaseContextAware"); + + DistinguishedName base = tested.getBase(); + assertNotNull(base); + assertEquals(new DistinguishedName("cn=john doe"), base); + } + + public void testPostProcessBeforeInitializationTransactionProxy() throws Exception { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( + "/conf/baseLdapPathPostProcessorTransactionTestContext.xml"); + DummyBaseLdapPathAware tested = (DummyBaseLdapPathAware) ctx.getBean("dummyBaseContextAware"); + + DistinguishedName base = tested.getBase(); + assertNotNull(base); + assertEquals(new DistinguishedName("dc=jayway,dc=se"), base); + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java new file mode 100644 index 00000000..443980b3 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java @@ -0,0 +1,37 @@ +/* + * Copyright 2005-2007 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 org.springframework.ldap.core.DistinguishedName; + +/** + * Dummy implementation of {@link BaseLdapPathAware}. + * + * @author Mattias Arthursson + */ +public class DummyBaseLdapPathAware implements BaseLdapPathAware { + + private DistinguishedName base; + + public void setBaseLdapPath(DistinguishedName baseLdapPath) { + this.base = baseLdapPath; + } + + public DistinguishedName getBase() { + return base; + } + +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java new file mode 100644 index 00000000..d1f5df7c --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2005-2007 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.NamingException; +import javax.naming.directory.DirContext; + +import org.springframework.ldap.core.ContextSource; +import org.springframework.test.AbstractDependencyInjectionSpringContextTests; + +/** + * Integration tests for ContextSourceImpl. + * + * @author Mattias Arthursson + */ +public class LdapContextSourcelITest extends + AbstractDependencyInjectionSpringContextTests { + + private ContextSource tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } + + public void testGetReadOnlyContext() throws NamingException { + DirContext ctx = null; + + try { + ctx = tested.getReadOnlyContext(); + assertNotNull(ctx); + Hashtable environment = ctx.getEnvironment(); + assertTrue(environment + .containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)); + assertTrue(environment.containsKey(Context.SECURITY_PRINCIPAL)); + assertTrue(environment.containsKey(Context.SECURITY_CREDENTIALS)); + } finally { + // Always clean up. + if (ctx != null) { + try { + ctx.close(); + } catch (Exception e) { + // Never mind this + } + } + } + } + + public void testGetReadWriteContext() throws NamingException { + DirContext ctx = null; + + try { + ctx = tested.getReadWriteContext(); + assertNotNull(ctx); + // Double check to see that we are authenticated. + Hashtable environment = ctx.getEnvironment(); + assertTrue(environment.containsKey(Context.SECURITY_PRINCIPAL)); + assertTrue(environment.containsKey(Context.SECURITY_CREDENTIALS)); + } finally { + // Always clean up. + if (ctx != null) { + try { + ctx.close(); + } catch (Exception e) { + // Never mind this + } + } + } + } + + public void setTested(ContextSource tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java new file mode 100644 index 00000000..caec3a68 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2005-2007 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 javax.naming.NamingException; + +import org.springframework.test.AbstractDependencyInjectionSpringContextTests; + +/** + * Integration tests for ContextSourceImpl. + * + * @author Mattias Arthursson + */ +public class LdapContextSourcelMultiServerITest extends + AbstractDependencyInjectionSpringContextTests { + + private LdapContextSource tested; + + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapContextSourceTestContext.xml" }; + } + + public void testUrls() throws NamingException { + String[] urls = tested.getUrls(); + String string = tested.assembleProviderUrlString(urls); + + assertEquals("ldap://127.0.0.1:389 ldap://127.0.0.2:389", string); + } + + public void setTested(LdapContextSource tested) { + this.tested = tested; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java new file mode 100644 index 00000000..ee844529 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java @@ -0,0 +1,371 @@ +/* + * Copyright 2005-2007 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.transaction.compensating.manager; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.ldap.NameNotFoundException; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +/** + * Integration tests for {@link ContextSourceAndDataSourceTransactionManager}. + * + * @author Mattias Arthursson + */ +public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends + AbstractDependencyInjectionSpringContextTests { + + private static Log log = LogFactory + .getLog(ContextSourceAndDataSourceTransactionManagerIntegrationTest.class); + + private DummyDao dummyDao; + + private LdapTemplate ldapTemplate; + + private JdbcTemplate jdbcTemplate; + + public ContextSourceAndDataSourceTransactionManagerIntegrationTest() { + setAutowireMode(AbstractDependencyInjectionSpringContextTests.AUTOWIRE_BY_NAME); + } + + public void setLdapTemplate(LdapTemplate ldapTemplate) { + this.ldapTemplate = ldapTemplate; + } + + public void setDummyDao(DummyDao dummyDaoImpl) { + this.dummyDao = dummyDaoImpl; + } + + protected String[] getConfigLocations() { + return new String[] { "conf/ldapAndJdbcTransactionTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + if (TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.clearSynchronization(); + } + + jdbcTemplate + .execute("create table PERSON(fullname VARCHAR, lastname VARCHAR, description VARCHAR)"); + jdbcTemplate.update("insert into PERSON values(?, ?, ?)", new Object[] { + "Some Person", "Person", "Sweden, Company1, Some Person" }); + } + + protected void onTearDown() throws Exception { + jdbcTemplate.execute("drop table PERSON"); + } + + public void testCreateWithException() { + try { + dummyDao.createWithException("Sweden", "company1", + "some testperson", "testperson", "some description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + // Verify that no entry was created + try { + ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + + try { + jdbcTemplate.queryForObject( + "select * from PERSON where fullname='some testperson'", + new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) + throws SQLException { + return null; + } + }); + fail("EmptyResultDataAccessException expected"); + } catch (EmptyResultDataAccessException expected) { + assertTrue(true); + } + } + + public void testCreate() { + dummyDao.create("Sweden", "company1", "some testperson", "testperson", + "some description"); + + log.debug("Verifying result"); + Object ldapResult = ldapTemplate + .lookup("cn=some testperson, ou=company1, c=Sweden"); + Object dbResult = jdbcTemplate.queryForObject( + "select * from PERSON where fullname='some testperson'", + new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) + throws SQLException { + return new Object(); + } + }); + assertNotNull(ldapResult); + assertNotNull(dbResult); + + ldapTemplate.unbind("cn=some testperson, ou=company1, c=Sweden"); + } + + public void testUpdateWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + dummyDao.updateWithException(dn, "Some Person", "Updated Person", + "Updated description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get( + "description").get()); + return new Object(); + } + }); + + Object jdbcResult = jdbcTemplate.queryForObject( + "select * from PERSON where fullname=?", + new Object[] { "Some Person" }, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) + throws SQLException { + assertEquals("Person", rs.getString("lastname")); + assertEquals("Sweden, Company1, Some Person", rs + .getString("description")); + return new Object(); + } + }); + + assertNotNull(ldapResult); + assertNotNull(jdbcResult); + } + + public void testUpdate() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + dummyDao.update(dn, "Some Person", "Updated Person", + "Updated description"); + + log.debug("Verifying result"); + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Updated Person", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get( + "description").get()); + return new Object(); + } + }); + + Object jdbcResult = jdbcTemplate.queryForObject( + "select * from PERSON where fullname=?", + new Object[] { "Some Person" }, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) + throws SQLException { + assertEquals("Updated Person", rs.getString("lastname")); + assertEquals("Updated description", rs + .getString("description")); + return new Object(); + } + }); + + assertNotNull(ldapResult); + assertNotNull(jdbcResult); + dummyDao.update(dn, "Some Person", "Person", + "Sweden, Company1, Some Person"); + } + + public void testUpdateAndRenameWithException() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + try { + // Perform test + dummyDao.updateAndRenameWithException(dn, newDn, + "Updated description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + // Verify that entry was not moved. + try { + ldapTemplate.lookup(newDn); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + + // Verify that original entry was not updated. + Object object = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Sweden, Company1, Some Person2", attributes.get( + "description").get()); + return new Object(); + } + }); + assertNotNull(object); + } + + public void testUpdateAndRename() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + // Perform test + dummyDao.updateAndRename(dn, newDn, "Updated description"); + + // Verify that entry was moved and updated. + Object object = ldapTemplate.lookup(newDn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Updated description", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(object); + dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2"); + } + + public void testModifyAttributesWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + // Perform test + dummyDao.modifyAttributesWithException(dn, "Updated lastname", + "Updated description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + // Verify result - check that the operation was properly rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(result); + } + + public void testModifyAttributes() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + dummyDao + .modifyAttributes(dn, "Updated lastname", "Updated description"); + + // Verify result - check that the operation was not rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Updated lastname", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(result); + } + + public void testUnbindWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + // Perform test + dummyDao.unbindWithException(dn, "Some Person"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + // Verify result - check that the operation was properly rolled back + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + // Just verify that the entry still exists. + return new Object(); + } + }); + + Object jdbcResult = jdbcTemplate.queryForObject( + "select * from PERSON where fullname=?", + new Object[] { "Some Person" }, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) + throws SQLException { + // Just verify that the entry still exists. + return new Object(); + } + }); + + assertNotNull(ldapResult); + assertNotNull(jdbcResult); + } + + public void testUnbind() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + dummyDao.unbind(dn, "Some Person"); + + try { + // Verify result - check that the operation was not rolled back + ldapTemplate.lookup(dn); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + + try { + jdbcTemplate.queryForObject( + "select * from PERSON where fullname=?", + new Object[] { "Some Person" }, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) + throws SQLException { + return null; + } + }); + fail("EmptyResultDataAccessException expected"); + } catch (EmptyResultDataAccessException expected) { + assertTrue(true); + } + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerIntegrationTest.java new file mode 100644 index 00000000..aaf94e12 --- /dev/null +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerIntegrationTest.java @@ -0,0 +1,281 @@ +/* + * Copyright 2005-2007 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.transaction.compensating.manager; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.ldap.NameNotFoundException; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +/** + * Integration tests for {@link ContextSourceAndDataSourceTransactionManager}. + * + * @author Mattias Arthursson + */ +public class ContextSourceTransactionManagerIntegrationTest extends + AbstractDependencyInjectionSpringContextTests { + + private static Log log = LogFactory + .getLog(ContextSourceTransactionManagerIntegrationTest.class); + + public ContextSourceTransactionManagerIntegrationTest() { + setAutowireMode(AbstractDependencyInjectionSpringContextTests.AUTOWIRE_BY_NAME); + } + + private DummyDao dummyDao; + + private LdapTemplate ldapTemplate; + + public void setLdapTemplate(LdapTemplate ldapTemplate) { + this.ldapTemplate = ldapTemplate; + } + + public void setDummyDao(DummyDao dummyDaoImpl) { + this.dummyDao = dummyDaoImpl; + } + + protected String[] getConfigLocations() { + return new String[] { "conf/ldapTemplateTransactionTestContext.xml" }; + } + + protected void onSetUp() throws Exception { + if (TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.clearSynchronization(); + } + } + + protected void onTearDown() throws Exception { + } + + public void testCreateWithException() { + try { + dummyDao.createWithException("Sweden", "company1", + "some testperson", "testperson", "some description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + // Verify that no entry was created + try { + ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + } + + public void testCreate() { + dummyDao.create("Sweden", "company1", "some testperson", "testperson", + "some description"); + + log.debug("Verifying result"); + String expectedDn = "cn=some testperson, ou=company1, c=Sweden"; + Object ldapResult = ldapTemplate.lookup(expectedDn); + assertNotNull(ldapResult); + + ldapTemplate.unbind(expectedDn); + } + + public void testUpdateWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + dummyDao.updateWithException(dn, "Some Person", "Updated Person", + "Updated description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(ldapResult); + } + + public void testUpdate() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + dummyDao.update(dn, "Some Person", "Updated Person", + "Updated description"); + + log.debug("Verifying result"); + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Updated Person", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(ldapResult); + + dummyDao.update(dn, "Some Person", "Person", + "Sweden, Company1, Some Person"); + } + + public void testUpdateAndRenameWithException() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + try { + // Perform test + dummyDao.updateAndRenameWithException(dn, newDn, + "Updated description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + // Verify that entry was not moved. + try { + ldapTemplate.lookup(newDn); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + + // Verify that original entry was not updated. + Object object = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Sweden, Company1, Some Person2", attributes.get( + "description").get()); + return new Object(); + } + }); + assertNotNull(object); + } + + public void testUpdateAndRename() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + // Perform test + dummyDao.updateAndRename(dn, newDn, "Updated description"); + + // Verify that entry was moved and updated. + Object object = ldapTemplate.lookup(newDn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Updated description", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(object); + dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2"); + } + + public void testModifyAttributesWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + // Perform test + dummyDao.modifyAttributesWithException(dn, "Updated lastname", + "Updated description"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + // Verify result - check that the operation was properly rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(result); + } + + public void testModifyAttributes() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + dummyDao + .modifyAttributes(dn, "Updated lastname", "Updated description"); + + // Verify result - check that the operation was not rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + assertEquals("Updated lastname", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get( + "description").get()); + return new Object(); + } + }); + + assertNotNull(result); + } + + public void testUnbindWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + // Perform test + dummyDao.unbindWithException(dn, "Some Person"); + fail("DummyException expected"); + } catch (DummyException expected) { + assertTrue(true); + } + + // Verify result - check that the operation was properly rolled back + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) + throws NamingException { + // Just verify that the entry still exists. + return new Object(); + } + }); + + assertNotNull(ldapResult); + } + + public void testUnbind() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + dummyDao.unbind(dn, "Some Person"); + + try { + // Verify result - check that the operation was not rolled back + ldapTemplate.lookup(dn); + fail("NameNotFoundException expected"); + } catch (NameNotFoundException expected) { + assertTrue(true); + } + + } +} diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/setup_data.ldif b/mvn-build/integration-tests/src/test/java/setup_data.ldif similarity index 100% rename from mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/setup_data.ldif rename to mvn-build/integration-tests/src/test/java/setup_data.ldif diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/teardown_data.ldif b/mvn-build/integration-tests/src/test/java/teardown_data.ldif similarity index 100% rename from mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/teardown_data.ldif rename to mvn-build/integration-tests/src/test/java/teardown_data.ldif diff --git a/mvn-build/integration-tests/src/test/resources/conf/apacheDsContext.xml b/mvn-build/integration-tests/src/test/resources/conf/apacheDsContext.xml new file mode 100644 index 00000000..6620b80d --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/apacheDsContext.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + simple + + + ${userDn} + + + ${password} + + + + + + + + + + + + + + + + + + + + + objectClass: top + objectClass: domain + objectClass: extensibleObject + dc: jayway + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml new file mode 100644 index 00000000..2d312a68 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorMultiContextSourceTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorMultiContextSourceTestContext.xml new file mode 100644 index 00000000..1a1d104c --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorMultiContextSourceTestContext.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorNoContextSourceTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorNoContextSourceTestContext.xml new file mode 100644 index 00000000..52798e86 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorNoContextSourceTestContext.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorPropertyOverrideTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorPropertyOverrideTestContext.xml new file mode 100644 index 00000000..8b42a613 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorPropertyOverrideTestContext.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTestContext.xml new file mode 100644 index 00000000..49a67621 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTestContext.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTransactionTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTransactionTestContext.xml new file mode 100644 index 00000000..066e6a58 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTransactionTestContext.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/commonTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/commonTestContext.xml similarity index 100% rename from mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/commonTestContext.xml rename to mvn-build/integration-tests/src/test/resources/conf/commonTestContext.xml diff --git a/mvn-build/integration-tests/src/test/resources/conf/distinguishedNameEditorTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/distinguishedNameEditorTestContext.xml new file mode 100644 index 00000000..86f40c59 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/distinguishedNameEditorTestContext.xml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/conf/ldap.properties b/mvn-build/integration-tests/src/test/resources/conf/ldap.properties similarity index 67% rename from mvn-build/integration-tests/tiger-integration-tests/src/test/resources/conf/ldap.properties rename to mvn-build/integration-tests/src/test/resources/conf/ldap.properties index 6789a99b..8843f1f1 100644 --- a/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/conf/ldap.properties +++ b/mvn-build/integration-tests/src/test/resources/conf/ldap.properties @@ -1,4 +1,4 @@ -urls=ldap://127.0.0.1:3901 +urls=ldap://127.0.0.1:3900 userDn=uid=admin,ou=system password=secret base=dc=jayway,dc=se diff --git a/mvn-build/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionTestContext.xml new file mode 100644 index 00000000..abaaab86 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionTestContext.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PROPAGATION_REQUIRES_NEW + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/ldapContextSourceTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/ldapContextSourceTestContext.xml new file mode 100644 index 00000000..2adc5219 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/ldapContextSourceTestContext.xml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/ldapTemplateAcegiTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateAcegiTestContext.xml similarity index 98% rename from mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/ldapTemplateAcegiTestContext.xml rename to mvn-build/integration-tests/src/test/resources/conf/ldapTemplateAcegiTestContext.xml index 9d73a7c1..a7ba9c2c 100644 --- a/mvn-build/integration-tests/acegi-support-integration-tests/src/test/resources/conf/ldapTemplateAcegiTestContext.xml +++ b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateAcegiTestContext.xml @@ -13,7 +13,7 @@ - + diff --git a/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateNoBaseSuffixTestContext.xml similarity index 67% rename from mvn-build/integration-tests/tiger-integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml rename to mvn-build/integration-tests/src/test/resources/conf/ldapTemplateNoBaseSuffixTestContext.xml index ff552c0d..312b761f 100644 --- a/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml +++ b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateNoBaseSuffixTestContext.xml @@ -3,11 +3,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> - - - - + + @@ -16,14 +13,15 @@ - + + - - - - + + \ No newline at end of file diff --git a/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateObjectBindTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateObjectBindTestContext.xml new file mode 100644 index 00000000..f67e95d5 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateObjectBindTestContext.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml new file mode 100644 index 00000000..3c85161c --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateTransactionTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateTransactionTestContext.xml new file mode 100644 index 00000000..8b111f02 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/ldapTemplateTransactionTestContext.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PROPAGATION_REQUIRES_NEW + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/rootContextSourceTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/rootContextSourceTestContext.xml new file mode 100644 index 00000000..13cc7894 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/rootContextSourceTestContext.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/conf/simpleLdapTemplateTestContext.xml b/mvn-build/integration-tests/src/test/resources/conf/simpleLdapTemplateTestContext.xml new file mode 100644 index 00000000..b351ad52 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/conf/simpleLdapTemplateTestContext.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + diff --git a/mvn-build/integration-tests/src/test/resources/log4j.properties b/mvn-build/integration-tests/src/test/resources/log4j.properties new file mode 100644 index 00000000..12c4b1c8 --- /dev/null +++ b/mvn-build/integration-tests/src/test/resources/log4j.properties @@ -0,0 +1,6 @@ +log4j.rootCategory=warn, stdout +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout.ConversionPattern=%r [%t] %-5p\: %-15c{2} - %m%n +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout + +log4j.logger.org.apache.directory.server.schema.registries.DefaultSyntaxRegistry=WARN \ No newline at end of file diff --git a/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/setup_data.ldif b/mvn-build/integration-tests/src/test/resources/setup_data.ldif similarity index 100% rename from mvn-build/integration-tests/tiger-integration-tests/src/test/resources/setup_data.ldif rename to mvn-build/integration-tests/src/test/resources/setup_data.ldif diff --git a/mvn-build/integration-tests/tiger-integration-tests/pom.xml b/mvn-build/integration-tests/tiger-integration-tests/pom.xml deleted file mode 100644 index 0dd7f59f..00000000 --- a/mvn-build/integration-tests/tiger-integration-tests/pom.xml +++ /dev/null @@ -1,125 +0,0 @@ - - - 4.0.0 - org.springframework.ldap - spring-ldap-tiger-integration-tests - Spring LDAP Tiger Integration Tests - 1.2.2-SNAPSHOT - - - 2.0.8 - - - - - - maven-eclipse-plugin - - true - - - - - maven-compiler-plugin - - 1.5 - 1.5 - - - - org.apache.maven.plugins - maven-surefire-plugin - - true - - - - default - test - - - - integration-test - integration-test - - test - - - false - once - - - - - - - - - - org.springframework.ldap - spring-ldap-test - 1.2.2-SNAPSHOT - - - org.springframework.ldap - spring-ldap-tiger - 1.2.2-SNAPSHOT - - - commons-pool - commons-pool - 1.3 - provided - - - com.sun - ldapbp - 1.0 - system - ${basedir}/../../lib/ldapbp.jar - - - - org.springframework - spring-dao - ${spring.version} - provided - - - org.springframework - spring-jdbc - ${spring.version} - provided - - - org.springframework - spring-aop - ${spring.version} - test - - - aspectj - aspectjrt - 1.5.3 - test - - - aspectj - aspectjweaver - 1.5.3 - test - - - gsbase - gsbase - 2.0.1 - test - - - hsqldb - hsqldb - 1.8.0.7 - test - - - diff --git a/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/log4j.properties b/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/log4j.properties deleted file mode 100644 index f57302e8..00000000 --- a/mvn-build/integration-tests/tiger-integration-tests/src/test/resources/log4j.properties +++ /dev/null @@ -1,9 +0,0 @@ -log4j.rootCategory=WARN, stdout - -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n - -#Enable debug logging -log4j.category.net.sf.ldaptemplate=INFO -log4j.category.net.sf.ldaptemplate.LdapTemplate=DEBUG