From 795701fd58c4012f5785147e8c8fd8d59adedeac Mon Sep 17 00:00:00 2001 From: Mattias Hellborg Arthursson Date: Tue, 8 Oct 2013 08:17:49 +0200 Subject: [PATCH] LDAP-267: Added integration tests and modified samples to use new xml namespace. --- .../ldap/pool/DelegatingContext.java | 4 +- .../src/main/resources/applicationContext.xml | 43 +- .../src/main/resources/applicationContext.xml | 42 +- .../ldap/test/EmbeddedLdapServer.java | 4 + .../test/EmbeddedLdapServerFactoryBean.java | 34 ++ .../ldap/test/LdapTestUtils.java | 4 +- .../test/TestContextSourceFactoryBean.java | 51 ++- .../LdapTemplateLookup25ITest.java | 40 +- .../conf/ldapTemplateNamespaceTestContext.xml | 23 ++ .../LdapTemplateLookup30ITest.java | 2 + .../LdapTemplateNamespaceLookup30ITest.java | 55 +++ .../conf/ldapTemplateNamespaceTestContext.xml | 23 ++ ...LdapTemplateObjectBindIntegrationTest.java | 159 ------- .../ldap/itest/LdapTemplatePooledITest.java | 7 +- ...plateSearchResultNamespaceConfigITest.java | 391 ++++++++++++++++++ ...actionManagerNamespaceIntegrationTest.java | 275 ++++++++++++ .../conf/ldapTemplateNamespaceTestContext.xml | 23 ++ ...emplateNamespaceTransactionTestContext.xml | 36 ++ .../ldapTemplateObjectBindTestContext.xml | 24 -- .../conf/ldapTemplatePooledTestContext.xml | 42 +- .../conf/ldapTemplateTestContext.xml | 4 - 21 files changed, 980 insertions(+), 306 deletions(-) create mode 100644 test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBean.java create mode 100644 test/integration-tests-spring25/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml create mode 100644 test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateNamespaceLookup30ITest.java create mode 100644 test/integration-tests-spring30/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml delete mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateObjectBindIntegrationTest.java create mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultNamespaceConfigITest.java create mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceTransactionManagerNamespaceIntegrationTest.java create mode 100644 test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml create mode 100644 test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTransactionTestContext.xml delete mode 100644 test/integration-tests/src/test/resources/conf/ldapTemplateObjectBindTestContext.xml diff --git a/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java b/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java index 1f70a288..68dc519e 100644 --- a/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java +++ b/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java @@ -231,14 +231,14 @@ public class DelegatingContext implements Context { * @see javax.naming.Context#destroySubcontext(javax.naming.Name) */ public void destroySubcontext(Name name) throws NamingException { - throw new UnsupportedOperationException("Cannot call createSubcontext on a pooled context"); + throw new UnsupportedOperationException("Cannot call destroySubcontext on a pooled context"); } /** * @see javax.naming.Context#destroySubcontext(java.lang.String) */ public void destroySubcontext(String name) throws NamingException { - throw new UnsupportedOperationException("Cannot call createSubcontext on a pooled context"); + throw new UnsupportedOperationException("Cannot call destroySubcontext on a pooled context"); } /** diff --git a/samples/odm/src/main/resources/applicationContext.xml b/samples/odm/src/main/resources/applicationContext.xml index b39e1648..946f0e8c 100644 --- a/samples/odm/src/main/resources/applicationContext.xml +++ b/samples/odm/src/main/resources/applicationContext.xml @@ -2,42 +2,33 @@ + xmlns:ldap="http://www.springframework.org/schema/ldap" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd"> + + + + + - - - - - - - - - - - - - - diff --git a/samples/plain/src/main/resources/applicationContext.xml b/samples/plain/src/main/resources/applicationContext.xml index 55561d29..6c89fe90 100644 --- a/samples/plain/src/main/resources/applicationContext.xml +++ b/samples/plain/src/main/resources/applicationContext.xml @@ -2,42 +2,32 @@ + xmlns:ldap="http://www.springframework.org/schema/ldap" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd"> + + + + - - - - - - - - - - - - - - diff --git a/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java b/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java index ed8c0ee2..45c19aa1 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java +++ b/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java @@ -16,6 +16,8 @@ package org.springframework.ldap.test; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.directory.server.core.DefaultDirectoryService; import org.apache.directory.server.core.DirectoryService; import org.apache.directory.server.core.entry.ServerEntry; @@ -33,6 +35,8 @@ import java.io.File; * @since 1.3.2 */ public class EmbeddedLdapServer { + private static final Log log = LogFactory.getLog(EmbeddedLdapServer.class); + private final DirectoryService directoryService; private final LdapServer ldapServer; diff --git a/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBean.java b/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBean.java new file mode 100644 index 00000000..bb25e679 --- /dev/null +++ b/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBean.java @@ -0,0 +1,34 @@ +package org.springframework.ldap.test; + +import org.springframework.beans.factory.config.AbstractFactoryBean; + +/** + * @author Mattias Hellborg Arthursson + */ +public class EmbeddedLdapServerFactoryBean extends AbstractFactoryBean { + private int port; + private String partitionName; + private String partitionSuffix; + + @Override + public Class getObjectType() { + return EmbeddedLdapServer.class; + } + + public void setPartitionName(String partitionName) { + this.partitionName = partitionName; + } + + public void setPartitionSuffix(String partitionSuffix) { + this.partitionSuffix = partitionSuffix; + } + + public void setPort(int port) { + this.port = port; + } + + @Override + protected EmbeddedLdapServer createInstance() throws Exception { + return EmbeddedLdapServer.newEmbeddedServer(partitionName, partitionSuffix, port); + } +} diff --git a/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java b/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java index 5e39673b..722e154d 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java +++ b/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java @@ -189,10 +189,10 @@ public class LdapTestUtils { childName = LdapUtils.prepend(childName, name); try { - ctx.destroySubcontext(childName); + ctx.unbind(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); - ctx.destroySubcontext(childName); + ctx.unbind(childName); } } } catch (NamingException e) { diff --git a/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java b/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java index dc433bca..746a76b8 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java +++ b/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java @@ -48,6 +48,8 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean { private AuthenticationSource authenticationSource; + private ContextSource contextSource; + public void setAuthenticationSource(AuthenticationSource authenticationSource) { this.authenticationSource = authenticationSource; } @@ -88,37 +90,48 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean { this.port = port; } - protected Object createInstance() throws Exception { + public void setContextSource(ContextSource contextSource) { + this.contextSource = contextSource; + } + + protected Object createInstance() throws Exception { LdapTestUtils.startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName); - LdapContextSource targetContextSource = new LdapContextSource(); - if (baseOnTarget) { - targetContextSource.setBase(defaultPartitionSuffix); - } + if (contextSource == null) { + // If not explicitly configured, create a new instance. + LdapContextSource targetContextSource = new LdapContextSource(); + if (baseOnTarget) { + targetContextSource.setBase(defaultPartitionSuffix); + } - targetContextSource.setUrl("ldap://localhost:" + port); - targetContextSource.setUserDn(principal); - targetContextSource.setPassword(password); - targetContextSource.setDirObjectFactory(dirObjectFactory); - targetContextSource.setPooled(pooled); + targetContextSource.setUrl("ldap://localhost:" + port); + targetContextSource.setUserDn(principal); + targetContextSource.setPassword(password); + targetContextSource.setDirObjectFactory(dirObjectFactory); + targetContextSource.setPooled(pooled); - if (authenticationSource != null) { - targetContextSource.setAuthenticationSource(authenticationSource); - } - targetContextSource.afterPropertiesSet(); + if (authenticationSource != null) { + targetContextSource.setAuthenticationSource(authenticationSource); + } + targetContextSource.afterPropertiesSet(); - if (baseOnTarget) { - LdapTestUtils.clearSubContexts(targetContextSource, LdapUtils.emptyLdapName()); + contextSource = targetContextSource; + } + + Thread.sleep(1000); + + if (baseOnTarget) { + LdapTestUtils.clearSubContexts(contextSource, LdapUtils.emptyLdapName()); } else { - LdapTestUtils.clearSubContexts(targetContextSource, LdapUtils.newLdapName(defaultPartitionSuffix)); + LdapTestUtils.clearSubContexts(contextSource, LdapUtils.newLdapName(defaultPartitionSuffix)); } if (ldifFile != null) { - LdapTestUtils.loadLdif(targetContextSource, ldifFile); + LdapTestUtils.loadLdif(contextSource, ldifFile); } - return targetContextSource; + return contextSource; } public Class getObjectType() { diff --git a/test/integration-tests-spring25/src/test/java/org.springframework.ldap.itest25/LdapTemplateLookup25ITest.java b/test/integration-tests-spring25/src/test/java/org.springframework.ldap.itest25/LdapTemplateLookup25ITest.java index 1bf9cf24..5cfd21ce 100644 --- a/test/integration-tests-spring25/src/test/java/org.springframework.ldap.itest25/LdapTemplateLookup25ITest.java +++ b/test/integration-tests-spring25/src/test/java/org.springframework.ldap.itest25/LdapTemplateLookup25ITest.java @@ -18,11 +18,9 @@ package org.springframework.ldap.itest25; import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.LdapTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import static junit.framework.Assert.assertEquals; @@ -31,12 +29,7 @@ import static junit.framework.Assert.assertEquals; * * @author Mattias Hellborg Arthursson */ -@ContextConfiguration(locations = {"/conf/ldapTemplateTestContext.xml"}) -public class LdapTemplateLookup25ITest extends AbstractJUnit4SpringContextTests { - - @Autowired - private LdapTemplate tested; - +public class LdapTemplateLookup25ITest { /** * This method depends on a DirObjectFactory ( * {@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) @@ -44,10 +37,29 @@ public class LdapTemplateLookup25ITest extends AbstractJUnit4SpringContextTests */ @Test public void testThatPlainLookupWorksWithSpring25() { - DirContextOperations result = tested.lookupContext("cn=Some Person2, ou=company1,c=Sweden"); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/conf/ldapTemplateTestContext.xml"); + LdapTemplate tested = (LdapTemplate) ctx.getBean("ldapTemplate"); - assertEquals("Some Person2", result.getStringAttribute("cn")); - assertEquals("Person2", result.getStringAttribute("sn")); - assertEquals("Sweden, Company1, Some Person2", result.getStringAttribute("description")); - } + performTestAndShutdownContext(ctx, tested); + } + + @Test + public void testThatNamespaceConfigurationWorksWithSpring25() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/conf/ldapTemplateNamespaceTestContext.xml"); + LdapTemplate tested = (LdapTemplate) ctx.getBean("ldapTemplate"); + + performTestAndShutdownContext(ctx, tested); + } + + private void performTestAndShutdownContext(ClassPathXmlApplicationContext ctx, LdapTemplate tested) { + try { + DirContextOperations result = tested.lookupContext("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")); + } finally { + ctx.close(); + } + } } diff --git a/test/integration-tests-spring25/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml b/test/integration-tests-spring25/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml new file mode 100644 index 00000000..49e52eb3 --- /dev/null +++ b/test/integration-tests-spring25/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateLookup30ITest.java b/test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateLookup30ITest.java index 7bb2e13a..e1d2fad1 100644 --- a/test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateLookup30ITest.java +++ b/test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateLookup30ITest.java @@ -21,6 +21,7 @@ import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; @@ -32,6 +33,7 @@ import static junit.framework.Assert.assertEquals; * @author Mattias Hellborg Arthursson */ @ContextConfiguration(locations = {"/conf/ldapTemplateTestContext.xml"}) +@DirtiesContext public class LdapTemplateLookup30ITest extends AbstractJUnit4SpringContextTests { @Autowired diff --git a/test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateNamespaceLookup30ITest.java b/test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateNamespaceLookup30ITest.java new file mode 100644 index 00000000..6290cac9 --- /dev/null +++ b/test/integration-tests-spring30/src/test/java/org.springframework.ldap.itest30/LdapTemplateNamespaceLookup30ITest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2005-2013 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.itest30; + + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ldap.core.DirContextOperations; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +import static junit.framework.Assert.assertEquals; + +/** + * Tests the lookup methods of LdapTemplate together with Spring 3.0. + * + * @author Mattias Hellborg Arthursson + */ +@ContextConfiguration(locations = {"/conf/ldapTemplateNamespaceTestContext.xml"}) +@DirtiesContext +public class LdapTemplateNamespaceLookup30ITest extends AbstractJUnit4SpringContextTests { + + @Autowired + private LdapTemplate tested; + + /** + * This method depends on a DirObjectFactory ( + * {@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) + * being set in the ContextSource. + */ + @Test + public void testThatPlainLookupWorksWithSpring30() { + DirContextOperations result = tested.lookupContext("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")); + } +} diff --git a/test/integration-tests-spring30/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml b/test/integration-tests-spring30/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml new file mode 100644 index 00000000..49e52eb3 --- /dev/null +++ b/test/integration-tests-spring30/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateObjectBindIntegrationTest.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateObjectBindIntegrationTest.java deleted file mode 100644 index 06678467..00000000 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateObjectBindIntegrationTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright 2005-2010 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.itest; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.fail; - -import java.io.Serializable; -import java.util.Date; -import java.util.LinkedList; - -import javax.naming.directory.DirContext; - -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.core.support.AbstractContextSource; -import org.springframework.test.context.ContextConfiguration; - -/** - * Tests bind and lookup with Java objects where the ContextSource has a - * null DirObjectFactory configured. - * - * @author Ulrik Sandberg - */ -@ContextConfiguration(locations = {"/conf/ldapTemplateObjectBindTestContext.xml"}) -public class LdapTemplateObjectBindIntegrationTest extends AbstractLdapTemplateIntegrationTest { - - @Autowired - private LdapTemplate tested; - - @Test - public void testBindJavaObjectDate() throws Exception { - String dn = "cn=myRandomDate"; - Date now = new Date(); - tested.bind(dn, now, null); - - Date result = (Date) tested.lookup(dn); - assertEquals(now, result); - tested.unbind(dn); - } - - @Test - public void testBindJavaObjectInteger() throws Exception { - String dn = "cn=myRandomInt"; - int i = 54321; - tested.bind(dn, new Integer(i), null); - - Integer result = (Integer) tested.lookup(dn); - assertEquals(i, result.intValue()); - tested.unbind(dn); - } - - @Test - 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); - } - - @Test - 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; - } - } - - @Test - 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. - */ - @Test - 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"); - } -} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplatePooledITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplatePooledITest.java index ee263116..5c01c6c4 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplatePooledITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplatePooledITest.java @@ -17,15 +17,17 @@ package org.springframework.ldap.itest; import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.core.io.ClassPathResource; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.pool.factory.PoolingContextSource; import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.test.LdapTestUtils; +import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; @@ -46,8 +48,7 @@ public class LdapTemplatePooledITest extends AbstractJUnit4SpringContextTests { private LdapTemplate tested; @Autowired - @Qualifier("contextSourceTarget") - protected ContextSource contextSource; + private ContextSource contextSource; @After public void cleanup() throws Exception { diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultNamespaceConfigITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultNamespaceConfigITest.java new file mode 100644 index 00000000..6f1f47cf --- /dev/null +++ b/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultNamespaceConfigITest.java @@ -0,0 +1,391 @@ +/* + * Copyright 2005-2013 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.itest; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.dao.IncorrectResultSizeDataAccessException; +import org.springframework.ldap.NameNotFoundException; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.DirContextOperations; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.core.support.AbstractContextMapper; +import org.springframework.ldap.query.SearchScope; +import org.springframework.ldap.support.LdapUtils; +import org.springframework.ldap.test.AttributeCheckAttributesMapper; +import org.springframework.ldap.test.AttributeCheckContextMapper; +import org.springframework.test.context.ContextConfiguration; + +import javax.naming.Name; +import javax.naming.directory.SearchControls; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; +import static org.springframework.ldap.query.LdapQueryBuilder.query; + +/** + * 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 Hellborg Arthursson + */ +@ContextConfiguration(locations = {"/conf/ldapTemplateNamespaceTestContext.xml"}) +public class LdapTemplateSearchResultNamespaceConfigITest extends AbstractLdapTemplateIntegrationTest { + + @Autowired + 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 = LdapUtils.newLdapName(BASE_STRING); + + @Before + public void prepareTestedInstance() throws Exception { + attributesMapper = new AttributeCheckAttributesMapper(); + contextMapper = new AttributeCheckContextMapper(); + } + + @After + public void cleanup() throws Exception { + attributesMapper = null; + contextMapper = null; + } + + @Test + 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()); + } + + @Test + public void testSearch_LdapQuery_AttributesMapper() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + + List list = tested.search(query() + .base(BASE_STRING) + .where("objectclass").is("person").and("sn").is("Person2"), + attributesMapper); + assertEquals(1, list.size()); + } + + @Test + public void testSearch_LdapQuery_AttributesMapper_FewerAttributes() { + attributesMapper.setExpectedAttributes(new String[] {"cn"}); + attributesMapper.setExpectedValues(new String[]{"Some Person2"}); + + List list = tested.search(query() + .base(BASE_STRING) + .attributes("cn") + .where("objectclass").is("person").and("sn").is("Person2"), + attributesMapper); + assertEquals(1, list.size()); + } + + @Test + public void testSearch_LdapQuery_AttributesMapper_SearchScope() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + + List list = tested.search(query() + .base(BASE_STRING) + .searchScope(SearchScope.ONELEVEL) + .where("objectclass").is("person").and("sn").is("Person2"), + attributesMapper); + assertEquals(0, list.size()); + } + + @Test + public void testSearch_LdapQuery_AttributesMapper_SearchScope_CorrectBase() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + + List list = tested.search(query() + .base("ou=company1,c=Sweden") + .searchScope(SearchScope.ONELEVEL) + .where("objectclass").is("person").and("sn").is("Person2"), + attributesMapper); + assertEquals(1, list.size()); + } + + @Test + public void testSearch_LdapQuery_AttributesMapper_NoBase() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + + List list = tested.search(query() + .where("objectclass").is("person").and("sn").is("Person2"), + attributesMapper); + assertEquals(1, list.size()); + } + + @Test + public void testSearch_LdapQuery_AttributesMapper_DifferentBase() { + attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); + attributesMapper.setExpectedValues(ALL_VALUES); + + List list = tested.search(query() + .base("c=Norway") + .where("objectclass").is("person").and("sn").is("Person2"), + attributesMapper); + assertEquals(0, list.size()); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + public void testSearchForObject() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + DirContextAdapter result = (DirContextAdapter) tested + .searchForObject(BASE_STRING, FILTER_STRING, contextMapper); + assertNotNull(result); + } + + @Test(expected = IncorrectResultSizeDataAccessException.class) + public void testSearchForObjectWithMultipleHits() { + tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=*))", new AbstractContextMapper() { + @Override + protected Object doMapFromContext(DirContextOperations ctx) { + return ctx; + } + }); + } + + @Test(expected = EmptyResultDataAccessException.class) + public void testSearchForObjectNoHits() { + tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=Person does not exist))", new AbstractContextMapper() { + @Override + protected Object doMapFromContext(DirContextOperations ctx) { + return ctx; + } + }); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + public void testSearch_ContextMapper_LdapQuery() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(query() + .base(BASE_NAME) + .where("objectclass").is("person").and("sn").is("Person2"), + contextMapper); + assertEquals(1, list.size()); + } + + @Test + public void testSearch_ContextMapper_LdapQuery_NoBase() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(query() + .where("objectclass").is("person").and("sn").is("Person2"), + contextMapper); + assertEquals(1, list.size()); + } + + @Test + public void testSearch_ContextMapper_LdapQuery_SearchScope() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(query() + .base(BASE_NAME) + .searchScope(SearchScope.ONELEVEL) + .where("objectclass").is("person").and("sn").is("Person2"), + contextMapper); + assertEquals(0, list.size()); + } + + @Test + public void testSearch_ContextMapper_LdapQuery_SearchScope_CorrectBase() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.search(query() + .base("ou=company1,c=Sweden") + .searchScope(SearchScope.ONELEVEL) + .where("objectclass").is("person").and("sn").is("Person2"), + contextMapper); + assertEquals(1, list.size()); + } + + @Test + public void testSearchForContext_LdapQuery() { + DirContextOperations result = tested.searchForContext(query() + .where("objectclass").is("person").and("sn").is("Person2")); + + assertNotNull(result); + assertEquals("Person2", result.getStringAttribute("sn")); + } + + @Test(expected = EmptyResultDataAccessException.class) + public void testSearchForContext_LdapQuery_SearchScopeNotFound() { + tested.searchForContext(query() + .searchScope(SearchScope.ONELEVEL) + .where("objectclass").is("person").and("sn").is("Person2")); + } + + @Test + public void testSearchForContext_LdapQuery_SearchScope_CorrectBase() { + DirContextOperations result = + tested.searchForContext(query() + .searchScope(SearchScope.ONELEVEL) + .base("ou=company1,c=Sweden") + .where("objectclass").is("person").and("sn").is("Person2")); + + assertNotNull(result); + assertEquals("Person2", result.getStringAttribute("sn")); + } + + @Test + 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()); + } + + @Test + 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()); + } + + @Test + public void testSearchWithInvalidSearchBaseShouldByDefaultThrowException() { + try { + tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, + contextMapper); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + } + + @Test + public void testSearchWithInvalidSearchBaseCanBeConfiguredToSwallowException() { + tested.setIgnoreNameNotFoundException(true); + contextMapper.setExpectedAttributes(CN_SN_ATTRS); + contextMapper.setExpectedValues(CN_SN_VALUES); + contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); + List list = tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, + contextMapper); + assertEquals(0, list.size()); + } +} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceTransactionManagerNamespaceIntegrationTest.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceTransactionManagerNamespaceIntegrationTest.java new file mode 100644 index 00000000..31db2d22 --- /dev/null +++ b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceTransactionManagerNamespaceIntegrationTest.java @@ -0,0 +1,275 @@ +/* + * Copyright 2005-2013 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.itest.manager; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.ldap.NameNotFoundException; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest; +import org.springframework.ldap.itest.transaction.compensating.manager.DummyDao; +import org.springframework.ldap.itest.transaction.compensating.manager.DummyException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +/** + * Integration tests for {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager} + * that uses the spring ldap namespace for configuration. + * + * @author Mattias Hellborg Arthursson + */ +@ContextConfiguration(locations = {"/conf/ldapTemplateNamespaceTransactionTestContext.xml"}) +public class ContextSourceTransactionManagerNamespaceIntegrationTest extends AbstractLdapTemplateIntegrationTest { + + private static Log log = LogFactory.getLog(ContextSourceTransactionManagerNamespaceIntegrationTest.class); + + @Autowired + @Qualifier("dummyDao") + private DummyDao dummyDao; + + @Autowired + private LdapTemplate ldapTemplate; + + @Before + public void prepareTestedInstance() throws Exception { + if (TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.clearSynchronization(); + } + } + + @Test + 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); + } + } + + @Test + 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); + } + + @Test + 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); + } + + @Test + 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"); + } + + @Test + 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); + } + + @Test + 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"); + } + + @Test + 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); + } + + @Test + 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); + } + + @Test + 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); + } + + @Test + 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/test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml new file mode 100644 index 00000000..7e1e4325 --- /dev/null +++ b/test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTestContext.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTransactionTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTransactionTestContext.xml new file mode 100644 index 00000000..5b14e615 --- /dev/null +++ b/test/integration-tests/src/test/resources/conf/ldapTemplateNamespaceTransactionTestContext.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/integration-tests/src/test/resources/conf/ldapTemplateObjectBindTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapTemplateObjectBindTestContext.xml deleted file mode 100644 index 2bb26603..00000000 --- a/test/integration-tests/src/test/resources/conf/ldapTemplateObjectBindTestContext.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/test/integration-tests/src/test/resources/conf/ldapTemplatePooledTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapTemplatePooledTestContext.xml index 80d94570..ba7c1154 100644 --- a/test/integration-tests/src/test/resources/conf/ldapTemplatePooledTestContext.xml +++ b/test/integration-tests/src/test/resources/conf/ldapTemplatePooledTestContext.xml @@ -16,35 +16,23 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ldap="http://www.springframework.org/schema/ldap" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd"> - - - - - - + - - - - - - - - - + + - - - - - + diff --git a/test/integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml index 244f5eac..1b70ae83 100644 --- a/test/integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml +++ b/test/integration-tests/src/test/resources/conf/ldapTemplateTestContext.xml @@ -19,8 +19,4 @@ class="org.springframework.ldap.core.LdapTemplate"> - -