From 32a866f86844c61e0c64be412d229bf3638684fd Mon Sep 17 00:00:00 2001 From: Josh Cummings <3627351+jzheaux@users.noreply.github.com> Date: Mon, 16 Jun 2025 14:06:38 -0600 Subject: [PATCH] Remove LDAP + Data Source Transaction Wrappers Closes gh-1109 --- .../ldap/config/TransactionManagerParser.java | 21 +- ...SourceAndDataSourceTransactionManager.java | 218 ---------- ...tSourceAndHibernateTransactionManager.java | 221 ---------- .../ldap/config/spring-ldap-4.0.xsd | 18 +- .../LdapTemplateNamespaceHandlerTests.java | 9 +- .../ContextSourceTransactionManagerTests.java | 3 +- modules/ROOT/pages/transaction-support.adoc | 29 +- .../DummyDaoLdapAndHibernateImpl.java | 120 ------ ...rceTransactionManagerIntegrationTests.java | 358 ----------------- ...sactionManagerLdap179IntegrationTests.java | 96 ----- ...urceTransactionManagerNamespaceITests.java | 359 ----------------- ...ateTransactionManagerIntegrationTests.java | 375 ----------------- ...sactionManagerLdap179IntegrationTests.java | 89 ----- ...nateTransactionManagerNamespaceITests.java | 376 ------------------ .../src/test/resources/conf/OrgPerson.hbm.xml | 16 - ...bernateTransactionNamespaceTestContext.xml | 51 --- ...ldapAndHibernateTransactionTestContext.xml | 63 --- ...AndJdbcTransactionNamespaceTestContext.xml | 52 --- .../ldapAndJdbcTransactionTestContext.xml | 49 --- ...LdapAndHibernateTransactionTestContext.xml | 80 ---- ...ssingLdapAndJdbcTransactionTestContext.xml | 66 --- 21 files changed, 11 insertions(+), 2658 deletions(-) delete mode 100644 core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManager.java delete mode 100755 core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndHibernateTransactionManager.java delete mode 100755 test/integration-tests/src/main/java/org/springframework/ldap/itest/transaction/compensating/manager/hibernate/DummyDaoLdapAndHibernateImpl.java delete mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTests.java delete mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerLdap179IntegrationTests.java delete mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerNamespaceITests.java delete mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerIntegrationTests.java delete mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerLdap179IntegrationTests.java delete mode 100644 test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerNamespaceITests.java delete mode 100755 test/integration-tests/src/test/resources/conf/OrgPerson.hbm.xml delete mode 100755 test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionNamespaceTestContext.xml delete mode 100755 test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionTestContext.xml delete mode 100644 test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionNamespaceTestContext.xml delete mode 100644 test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionTestContext.xml delete mode 100755 test/integration-tests/src/test/resources/conf/missingLdapAndHibernateTransactionTestContext.xml delete mode 100644 test/integration-tests/src/test/resources/conf/missingLdapAndJdbcTransactionTestContext.xml diff --git a/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java b/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java index 550a2904..37078544 100644 --- a/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java +++ b/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java @@ -24,8 +24,6 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager; -import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager; import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager; import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy; import org.springframework.ldap.transaction.compensating.support.DifferentSubtreeTempEntryRenamingStrategy; @@ -58,24 +56,13 @@ public class TransactionManagerParser implements BeanDefinitionParser { String dataSourceRef = element.getAttribute(ATT_DATA_SOURCE_REF); String sessionFactoryRef = element.getAttribute(ATT_SESSION_FACTORY_REF); - if (StringUtils.hasText(dataSourceRef) && StringUtils.hasText(sessionFactoryRef)) { - throw new IllegalArgumentException(String.format("Only one of %s and %s can be specified", + if (StringUtils.hasText(dataSourceRef) || StringUtils.hasText(sessionFactoryRef)) { + throw new IllegalArgumentException(String.format( + "ContextSourceAndHibernateTransactionManager and ContextSourceAndDataSourceTransactionManager are removed in Spring LDAP 4.0. Please remove your usage of data-source-ref and session-factory-ref.", ATT_DATA_SOURCE_REF, ATT_SESSION_FACTORY_REF)); } - BeanDefinitionBuilder builder; - if (StringUtils.hasText(dataSourceRef)) { - builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndDataSourceTransactionManager.class); - builder.addPropertyReference("dataSource", dataSourceRef); - } - else if (StringUtils.hasText(sessionFactoryRef)) { - builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndHibernateTransactionManager.class); - builder.addPropertyReference("sessionFactory", sessionFactoryRef); - } - else { - // Standard transaction manager - builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceTransactionManager.class); - } + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceTransactionManager.class); builder.addPropertyReference("contextSource", contextSourceRef); diff --git a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManager.java b/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManager.java deleted file mode 100644 index 37562b4b..00000000 --- a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManager.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * 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 - * - * https://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.datasource.DataSourceTransactionManager; -import org.springframework.ldap.core.ContextSource; -import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionException; -import org.springframework.transaction.TransactionSuspensionNotSupportedException; -import org.springframework.transaction.support.DefaultTransactionStatus; - -/** - * A Transaction Manager to manage LDAP and JDBC operations within the same transaction. - * Note that even though the same logical transaction is used, this is not a JTA XA - * transaction; no two-phase commit will be performed, and thus commit and rollback may - * yield unexpected results. - * - * Note that nested transactions are not supported. - * - * @author Mattias Hellborg Arthursson - * @since 1.2 - * @deprecated The idea of wrapping two transaction managers without actual XA support is - * probably not such a good idea after all. AbstractPlatformTransactionManager is not - * designed for this usage. - */ -@Deprecated -public class ContextSourceAndDataSourceTransactionManager extends DataSourceTransactionManager { - - private static final long serialVersionUID = 6832868697460384648L; - - private ContextSourceTransactionManagerDelegate ldapManagerDelegate = new ContextSourceTransactionManagerDelegate(); - - public ContextSourceAndDataSourceTransactionManager() { - super(); - // Override the default behaviour. - setNestedTransactionAllowed(false); - } - - /* - * @see org.springframework.jdbc.datasource.DataSourceTransactionManager# - * isExistingTransaction(java.lang.Object) - */ - @Override - protected boolean isExistingTransaction(Object transaction) { - // We don't support nested transactions here - return false; - } - - /* - * @see - * org.springframework.jdbc.datasource.DataSourceTransactionManager#doGetTransaction() - */ - @Override - protected Object doGetTransaction() { - Object dataSourceTransactionObject = super.doGetTransaction(); - Object contextSourceTransactionObject = this.ldapManagerDelegate.doGetTransaction(); - - return new ContextSourceAndDataSourceTransactionObject(contextSourceTransactionObject, - dataSourceTransactionObject); - } - - /* - * @see - * org.springframework.jdbc.datasource.DataSourceTransactionManager#doBegin(java.lang. - * Object, org.springframework.transaction.TransactionDefinition) - */ - @Override - protected void doBegin(Object transaction, TransactionDefinition definition) { - ContextSourceAndDataSourceTransactionObject actualTransactionObject = (ContextSourceAndDataSourceTransactionObject) transaction; - - super.doBegin(actualTransactionObject.getDataSourceTransactionObject(), definition); - try { - this.ldapManagerDelegate.doBegin(actualTransactionObject.getLdapTransactionObject(), definition); - } - catch (TransactionException ex) { - // Failed to start LDAP transaction - make sure we clean up properly - super.doCleanupAfterCompletion(actualTransactionObject.getDataSourceTransactionObject()); - throw ex; - } - } - - /* - * @see org.springframework.jdbc.datasource.DataSourceTransactionManager# - * doCleanupAfterCompletion(java.lang.Object) - */ - @Override - protected void doCleanupAfterCompletion(Object transaction) { - ContextSourceAndDataSourceTransactionObject actualTransactionObject = (ContextSourceAndDataSourceTransactionObject) transaction; - - super.doCleanupAfterCompletion(actualTransactionObject.getDataSourceTransactionObject()); - this.ldapManagerDelegate.doCleanupAfterCompletion(actualTransactionObject.getLdapTransactionObject()); - } - - /* - * @see org.springframework.jdbc.datasource.DataSourceTransactionManager#doCommit(org. - * springframework.transaction.support.DefaultTransactionStatus) - */ - @Override - protected void doCommit(DefaultTransactionStatus status) { - - ContextSourceAndDataSourceTransactionObject actualTransactionObject = (ContextSourceAndDataSourceTransactionObject) status - .getTransaction(); - - try { - super.doCommit(new DefaultTransactionStatus(actualTransactionObject.getDataSourceTransactionObject(), - status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), - status.getSuspendedResources())); - } - catch (TransactionException ex) { - if (isRollbackOnCommitFailure()) { - logger.debug("Failed to commit db resource, rethrowing", ex); - // If we are to rollback on commit failure, just rethrow the - // exception - this will cause a rollback to be performed on - // both resources. - throw ex; - } - else { - logger.warn("Failed to commit and resource is rollbackOnCommit not set -" - + " proceeding to commit ldap resource."); - } - } - this.ldapManagerDelegate.doCommit(new DefaultTransactionStatus( - actualTransactionObject.getLdapTransactionObject(), status.isNewTransaction(), - status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources())); - } - - /* - * @see - * org.springframework.jdbc.datasource.DataSourceTransactionManager#doRollback(org. - * springframework.transaction.support.DefaultTransactionStatus) - */ - @Override - protected void doRollback(DefaultTransactionStatus status) { - ContextSourceAndDataSourceTransactionObject actualTransactionObject = (ContextSourceAndDataSourceTransactionObject) status - .getTransaction(); - - super.doRollback(new DefaultTransactionStatus(actualTransactionObject.getDataSourceTransactionObject(), - status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), - status.getSuspendedResources())); - this.ldapManagerDelegate.doRollback(new DefaultTransactionStatus( - actualTransactionObject.getLdapTransactionObject(), status.isNewTransaction(), - status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources())); - } - - public ContextSource getContextSource() { - return this.ldapManagerDelegate.getContextSource(); - } - - public void setContextSource(ContextSource contextSource) { - this.ldapManagerDelegate.setContextSource(contextSource); - } - - public void setRenamingStrategy(TempEntryRenamingStrategy renamingStrategy) { - this.ldapManagerDelegate.setRenamingStrategy(renamingStrategy); - } - - /* - * @see - * org.springframework.jdbc.datasource.DataSourceTransactionManager#doSuspend(java. - * lang.Object) - */ - protected Object doSuspend(Object transaction) { - throw new TransactionSuspensionNotSupportedException( - "Transaction manager [" + getClass().getName() + "] does not support transaction suspension"); - } - - /* - * @see - * org.springframework.jdbc.datasource.DataSourceTransactionManager#doResume(java.lang - * .Object, java.lang.Object) - */ - protected void doResume(Object transaction, Object suspendedResources) { - throw new TransactionSuspensionNotSupportedException( - "Transaction manager [" + getClass().getName() + "] does not support transaction suspension"); - } - - public void afterPropertiesSet() { - super.afterPropertiesSet(); - this.ldapManagerDelegate.checkRenamingStrategy(); - } - - private static final class ContextSourceAndDataSourceTransactionObject { - - private Object ldapTransactionObject; - - private Object dataSourceTransactionObject; - - ContextSourceAndDataSourceTransactionObject(Object ldapTransactionObject, Object dataSourceTransactionObject) { - this.ldapTransactionObject = ldapTransactionObject; - this.dataSourceTransactionObject = dataSourceTransactionObject; - } - - Object getDataSourceTransactionObject() { - return this.dataSourceTransactionObject; - } - - Object getLdapTransactionObject() { - return this.ldapTransactionObject; - } - - } - -} diff --git a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndHibernateTransactionManager.java b/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndHibernateTransactionManager.java deleted file mode 100755 index 3d32dc72..00000000 --- a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndHibernateTransactionManager.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * 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 - * - * https://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.ContextSource; -import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy; -import org.springframework.orm.hibernate5.HibernateTransactionManager; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionException; -import org.springframework.transaction.TransactionSuspensionNotSupportedException; -import org.springframework.transaction.support.DefaultTransactionStatus; - -/** - * A Transaction Manager to manage LDAP and Hibernate 3 operations within the same - * transaction. Note that even though the same logical transaction is used, this is - * not a JTA XA transaction; no two-phase commit will be performed, and thus commit - * and rollback may yield unexpected results.
- * This Transaction Manager is as good as it gets when you are using in LDAP in - * combination with a Hibernate 3 and unable to use XA transactions because LDAP is not - * transactional by design to begin with.
- * - * Furthermore, this manager does not support nested transactions - * - * @author Hans Westerbeek - * @since 1.2.2 - * @deprecated The idea of wrapping two transaction managers without actual XA support is - * probably not such a good idea after all. AbstractPlatformTransactionManager is not - * designed for this usage. - */ -@Deprecated -public class ContextSourceAndHibernateTransactionManager extends HibernateTransactionManager { - - /** - * - */ - private static final long serialVersionUID = 1L; - - private ContextSourceTransactionManagerDelegate ldapManagerDelegate = new ContextSourceTransactionManagerDelegate(); - - /* - * @see org.springframework.orm.hibernate5.HibernateTransactionManager# - * isExistingTransaction(java.lang.Object) - */ - @Override - protected boolean isExistingTransaction(Object transaction) { - ContextSourceAndHibernateTransactionObject actualTransactionObject = (ContextSourceAndHibernateTransactionObject) transaction; - - return super.isExistingTransaction(actualTransactionObject.getHibernateTransactionObject()); - } - - /* - * @see - * org.springframework.orm.hibernate5.HibernateTransactionManager#doGetTransaction() - */ - @Override - protected Object doGetTransaction() { - Object dataSourceTransactionObject = super.doGetTransaction(); - Object contextSourceTransactionObject = this.ldapManagerDelegate.doGetTransaction(); - - return new ContextSourceAndHibernateTransactionObject(contextSourceTransactionObject, - dataSourceTransactionObject); - } - - /* - * @see - * org.springframework.orm.hibernate5.HibernateTransactionManager#doBegin(java.lang. - * Object, org.springframework.transaction.TransactionDefinition) - */ - @Override - protected void doBegin(Object transaction, TransactionDefinition definition) { - ContextSourceAndHibernateTransactionObject actualTransactionObject = (ContextSourceAndHibernateTransactionObject) transaction; - - super.doBegin(actualTransactionObject.getHibernateTransactionObject(), definition); - try { - this.ldapManagerDelegate.doBegin(actualTransactionObject.getLdapTransactionObject(), definition); - } - catch (TransactionException ex) { - // Failed to start LDAP transaction - make sure we clean up properly - super.doCleanupAfterCompletion(actualTransactionObject.getHibernateTransactionObject()); - throw ex; - } - } - - /* - * @see org.springframework.orm.hibernate5.HibernateTransactionManager# - * doCleanupAfterCompletion(java.lang.Object) - */ - @Override - protected void doCleanupAfterCompletion(Object transaction) { - ContextSourceAndHibernateTransactionObject actualTransactionObject = (ContextSourceAndHibernateTransactionObject) transaction; - - super.doCleanupAfterCompletion(actualTransactionObject.getHibernateTransactionObject()); - this.ldapManagerDelegate.doCleanupAfterCompletion(actualTransactionObject.getLdapTransactionObject()); - } - - /* - * @see org.springframework.orm.hibernate5.HibernateTransactionManager#doCommit(org. - * springframework.transaction.support.DefaultTransactionStatus) - */ - @Override - protected void doCommit(DefaultTransactionStatus status) { - - ContextSourceAndHibernateTransactionObject actualTransactionObject = (ContextSourceAndHibernateTransactionObject) status - .getTransaction(); - - try { - super.doCommit(new DefaultTransactionStatus(actualTransactionObject.getHibernateTransactionObject(), - status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), - status.getSuspendedResources())); - } - catch (TransactionException ex) { - if (isRollbackOnCommitFailure()) { - logger.debug("Failed to commit db resource, rethrowing", ex); - // If we are to rollback on commit failure, just rethrow the - // exception - this will cause a rollback to be performed on - // both resources. - throw ex; - } - else { - logger.warn("Failed to commit and resource is rollbackOnCommit not set -" - + " proceeding to commit ldap resource."); - } - } - this.ldapManagerDelegate.doCommit(new DefaultTransactionStatus( - actualTransactionObject.getLdapTransactionObject(), status.isNewTransaction(), - status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources())); - } - - /* - * @see org.springframework.orm.hibernate5.HibernateTransactionManager#doRollback(org. - * springframework.transaction.support.DefaultTransactionStatus) - */ - @Override - protected void doRollback(DefaultTransactionStatus status) { - ContextSourceAndHibernateTransactionObject actualTransactionObject = (ContextSourceAndHibernateTransactionObject) status - .getTransaction(); - - super.doRollback(new DefaultTransactionStatus(actualTransactionObject.getHibernateTransactionObject(), - status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), - status.getSuspendedResources())); - this.ldapManagerDelegate.doRollback(new DefaultTransactionStatus( - actualTransactionObject.getLdapTransactionObject(), status.isNewTransaction(), - status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources())); - } - - public ContextSource getContextSource() { - return this.ldapManagerDelegate.getContextSource(); - } - - public void setContextSource(ContextSource contextSource) { - this.ldapManagerDelegate.setContextSource(contextSource); - } - - public void setRenamingStrategy(TempEntryRenamingStrategy renamingStrategy) { - this.ldapManagerDelegate.setRenamingStrategy(renamingStrategy); - } - - /* - * @see - * org.springframework.orm.hibernate5.HibernateTransactionManager#doSuspend(java.lang. - * Object) - */ - @Override - protected Object doSuspend(Object transaction) { - throw new TransactionSuspensionNotSupportedException( - "Transaction manager [" + getClass().getName() + "] does not support transaction suspension"); - } - - /* - * @see - * org.springframework.orm.hibernate5.HibernateTransactionManager#doResume(java.lang. - * Object, java.lang.Object) - */ - @Override - protected void doResume(Object transaction, Object suspendedResources) { - throw new TransactionSuspensionNotSupportedException( - "Transaction manager [" + getClass().getName() + "] does not support transaction suspension"); - } - - @Override - public void afterPropertiesSet() { - super.afterPropertiesSet(); - this.ldapManagerDelegate.checkRenamingStrategy(); - } - - private static final class ContextSourceAndHibernateTransactionObject { - - private Object ldapTransactionObject; - - private Object hibernateTransactionObject; - - ContextSourceAndHibernateTransactionObject(Object ldapTransactionObject, Object hibernateTransactionObject) { - this.ldapTransactionObject = ldapTransactionObject; - this.hibernateTransactionObject = hibernateTransactionObject; - } - - Object getHibernateTransactionObject() { - return this.hibernateTransactionObject; - } - - Object getLdapTransactionObject() { - return this.ldapTransactionObject; - } - - } - -} diff --git a/core/src/main/resources/org/springframework/ldap/config/spring-ldap-4.0.xsd b/core/src/main/resources/org/springframework/ldap/config/spring-ldap-4.0.xsd index 142b29b6..16919e28 100644 --- a/core/src/main/resources/org/springframework/ldap/config/spring-ldap-4.0.xsd +++ b/core/src/main/resources/org/springframework/ldap/config/spring-ldap-4.0.xsd @@ -600,28 +600,12 @@ - - - - Id of the DataSource instance to use. - - - - - - - Id of the Hibernate SessionFactory instance to use. - - - - Creates an ContextSourceTransactionManager. If data-source-ref or session-factory-ref is specified, - a DataSourceAndContextSourceTransactionManager/HibernateAndContextSourceTransactionManager will be - created. + Creates an ContextSourceTransactionManager diff --git a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTests.java b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTests.java index a645098d..a528d91e 100644 --- a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTests.java +++ b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTests.java @@ -42,7 +42,6 @@ import org.springframework.ldap.pool.validation.DefaultDirContextValidator; import org.springframework.ldap.pool2.factory.PooledContextSource; import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy; -import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager; import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager; import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy; import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy; @@ -51,6 +50,7 @@ import org.springframework.transaction.PlatformTransactionManager; import org.springframework.util.ReflectionUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Mattias Hellborg Arthursson @@ -238,11 +238,8 @@ public class LdapTemplateNamespaceHandlerTests { @Test public void verifyParseTransactionWithDataSource() { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( - "/ldap-namespace-config-transactional-datasource.xml"); - PlatformTransactionManager transactionManager = ctx.getBean(PlatformTransactionManager.class); - - assertThat(transactionManager instanceof ContextSourceAndDataSourceTransactionManager).isTrue(); + assertThatExceptionOfType(BeansException.class).isThrownBy( + () -> new ClassPathXmlApplicationContext("/ldap-namespace-config-transactional-datasource.xml")); } @Test diff --git a/core/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerTests.java b/core/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerTests.java index f9771071..c6b9ae77 100644 --- a/core/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerTests.java +++ b/core/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerTests.java @@ -114,7 +114,8 @@ public class ContextSourceTransactionManagerTests { CompensatingTransactionObject transactionObject = new CompensatingTransactionObject(null); transactionObject.setHolder(expectedContextHolder); - this.tested.doRollback(new DefaultTransactionStatus(transactionObject, false, false, false, false, null)); + this.tested.doRollback( + new DefaultTransactionStatus("name", transactionObject, false, false, false, false, false, null)); verify(this.transactionDataManagerMock).rollback(); } diff --git a/modules/ROOT/pages/transaction-support.adoc b/modules/ROOT/pages/transaction-support.adoc index cfe27a0b..f847ce57 100644 --- a/modules/ROOT/pages/transaction-support.adoc +++ b/modules/ROOT/pages/transaction-support.adoc @@ -62,34 +62,7 @@ In a real-world situation, you would probably apply the transactions on the serv [[spring-ldap-jdbc-transaction-integration]] == JDBC Transaction Integration -A common use case when working against LDAP is that some of the data is stored in the LDAP tree but other data is stored in a relational database. In this case, transaction support becomes even more important, since the update of the different resources should be synchronized. - -While actual XA transactions is not supported, support is provided to conceptually wrap JDBC and LDAP access within the same transaction by supplying a `data-source-ref` attribute to the `` element. This creates a `ContextSourceAndDataSourceTransactionManager`, which then manages the two transactions virtually as if they were one. When performing a commit, the LDAP part of the operation is always performed first, letting both transactions be rolled back should the LDAP commit fail. The JDBC part of the transaction is managed exactly as in `DataSourceTransactionManager`, except that nested transactions are not supported. The following example shows an `ldap:transaction-manager` element with a `data-source-ref` attribute: - -==== -[source,java] -[subs="verbatim,quotes"] ----- - - - ----- -==== - -NOTE: The provided support is all client-side. -The wrapped transaction is not an XA transaction. No two-phase commit is performed, as the LDAP server cannot vote on its outcome. - -You can accomplish the same thing for Hibernate integration by supplying a `session-factory-ref` attribute to the `` element, as follows: - -==== -[source,xml] -[subs="verbatim,quotes"] ----- - - - ----- -==== +This support was removed in Spring LDAP 4.0. [[ldap-compensating-transactions-explained]] == LDAP Compensating Transactions Explained diff --git a/test/integration-tests/src/main/java/org/springframework/ldap/itest/transaction/compensating/manager/hibernate/DummyDaoLdapAndHibernateImpl.java b/test/integration-tests/src/main/java/org/springframework/ldap/itest/transaction/compensating/manager/hibernate/DummyDaoLdapAndHibernateImpl.java deleted file mode 100755 index 3b263add..00000000 --- a/test/integration-tests/src/main/java/org/springframework/ldap/itest/transaction/compensating/manager/hibernate/DummyDaoLdapAndHibernateImpl.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright 2005-2023 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 - * - * https://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.transaction.compensating.manager.hibernate; - -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.itest.transaction.compensating.manager.DummyException; -import org.springframework.orm.hibernate5.support.HibernateDaoSupport; -import org.springframework.transaction.annotation.Transactional; - -/** - * @author Hans Westerbeek - */ -@Transactional -public class DummyDaoLdapAndHibernateImpl extends HibernateDaoSupport implements OrgPersonDao { - - private LdapTemplate ldapTemplate; - - public void create(OrgPerson person) { - DistinguishedName dn = new DistinguishedName(); - dn.add("ou", person.getCountry()); - dn.add("ou", person.getCompany()); - dn.add("cn", person.getFullname()); - - DirContextAdapter ctx = new DirContextAdapter(); - ctx.setAttributeValues("objectclass", new String[] { "top", "person" }); - ctx.setAttributeValue("cn", person.getFullname()); - ctx.setAttributeValue("sn", person.getLastname()); - ctx.setAttributeValue("description", person.getDescription()); - this.ldapTemplate.bind(dn, ctx, null); - this.getHibernateTemplate().saveOrUpdate(person); - - } - - public void createWithException(OrgPerson person) { - this.create(person); - throw new DummyException("This method failed"); - - } - - public void modifyAttributes(String dn, String lastName, String description) { - DirContextAdapter ctx = (DirContextAdapter) this.ldapTemplate.lookup(dn); - ctx.setAttributeValue("sn", lastName); - ctx.setAttributeValue("description", description); - - this.ldapTemplate.modifyAttributes(dn, ctx.getModificationItems()); - } - - public void modifyAttributesWithException(String dn, String lastName, String description) { - modifyAttributes(dn, lastName, description); - throw new DummyException("This method failed."); - } - - public void unbind(OrgPerson person) { - String dn = prepareDn(person); - this.ldapTemplate.unbind(dn); - this.getHibernateTemplate().delete(person); - - } - - public void unbindWithException(OrgPerson person) { - this.unbind(person); - throw new DummyException("This method failed"); - } - - public void update(OrgPerson person) { - String dn = prepareDn(person); - DirContextAdapter ctx = (DirContextAdapter) this.ldapTemplate.lookup(dn); - ctx.setAttributeValue("sn", person.getLastname()); - ctx.setAttributeValue("description", person.getDescription()); - - this.ldapTemplate.modifyAttributes(ctx); - this.getHibernateTemplate().saveOrUpdate(person); - - } - - public void updateWithException(OrgPerson person) { - this.update(person); - throw new DummyException("This method failed"); - } - - public void updateAndRename(String dn, String newDn, String updatedDescription) { - - DirContextAdapter ctx = (DirContextAdapter) this.ldapTemplate.lookup(dn); - ctx.setAttributeValue("description", updatedDescription); - - this.ldapTemplate.modifyAttributes(ctx); - this.ldapTemplate.rename(dn, newDn); - - } - - public void updateAndRenameWithException(String dn, String newDn, String updatedDescription) { - this.updateAndRename(dn, newDn, updatedDescription); - throw new DummyException("This method failed"); - } - - public void setLdapTemplate(LdapTemplate ldapTemplate) { - this.ldapTemplate = ldapTemplate; - } - - private String prepareDn(OrgPerson person) { - return "cn=" + person.getFullname() + ",ou=" + person.getCompany() + ",ou=" + person.getCountry(); - } - -} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTests.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTests.java deleted file mode 100644 index 83b8561d..00000000 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTests.java +++ /dev/null @@ -1,358 +0,0 @@ -/* - * Copyright 2005-2016 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 - * - * https://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 java.sql.ResultSet; -import java.sql.SQLException; - -import javax.naming.NamingException; -import javax.naming.directory.Attributes; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -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.ldap.itest.AbstractLdapTemplateIntegrationTests; -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 static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; - -/** - * Integration tests for - * {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager}. - * - * @author Mattias Hellborg Arthursson - */ -@ContextConfiguration(locations = { "/conf/ldapAndJdbcTransactionTestContext.xml" }) -public class ContextSourceAndDataSourceTransactionManagerIntegrationTests extends AbstractLdapTemplateIntegrationTests { - - private static Logger log = LoggerFactory - .getLogger(ContextSourceAndDataSourceTransactionManagerIntegrationTests.class); - - @Autowired - @Qualifier("dummyDao") - private DummyDao dummyDao; - - @Autowired - private LdapTemplate ldapTemplate; - - @Autowired - private JdbcTemplate jdbcTemplate; - - @Before - public void prepareTestedInstance() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - - this.jdbcTemplate.execute("drop table PERSON if exists"); - this.jdbcTemplate - .execute("create table PERSON(fullname VARCHAR(256), lastname VARCHAR(256), description VARCHAR(256))"); - this.jdbcTemplate.update("insert into PERSON values(?, ?, ?)", - new Object[] { "Some Person", "Person", "Sweden, Company1, Some Person" }); - } - - @After - public void cleanup() throws Exception { - this.jdbcTemplate.execute("drop table PERSON if exists"); - } - - @Test - public void testCreateWithException() { - try { - this.dummyDao.createWithException("Sweden", "company1", "some testperson", "testperson", - "some description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - // Verify that no entry was created - try { - this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - try { - this.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) { - assertThat(true).isTrue(); - } - } - - @Test - public void testCreate() { - this.dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); - - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - Object dbResult = this.jdbcTemplate.queryForObject("select * from PERSON where fullname='some testperson'", - new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - return new Object(); - } - }); - assertThat(ldapResult).isNotNull(); - assertThat(dbResult).isNotNull(); - - this.ldapTemplate.unbind("cn=some testperson, ou=company1, ou=Sweden"); - } - - @Test - public void testUpdateWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - this.dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Person"); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - Object jdbcResult = this.jdbcTemplate.queryForObject("select * from PERSON where fullname=?", - new Object[] { "Some Person" }, new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - assertThat(rs.getString("lastname")).isEqualTo("Person"); - assertThat(rs.getString("description")).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - assertThat(ldapResult).isNotNull(); - assertThat(jdbcResult).isNotNull(); - } - - @Test - public void testUpdate() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - this.dummyDao.update(dn, "Some Person", "Updated Person", "Updated description"); - - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated Person"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - Object jdbcResult = this.jdbcTemplate.queryForObject("select * from PERSON where fullname=?", - new Object[] { "Some Person" }, new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - assertThat(rs.getString("lastname")).isEqualTo("Updated Person"); - assertThat(rs.getString("description")).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(ldapResult).isNotNull(); - assertThat(jdbcResult).isNotNull(); - this.dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person"); - } - - @Test - public void testUpdateAndRenameWithException() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - try { - // Perform test - this.dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify that entry was not moved. - try { - this.ldapTemplate.lookup(newDn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - // Verify that original entry was not updated. - Object object = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person2"); - return new Object(); - } - }); - assertThat(object).isNotNull(); - } - - @Test - public void testUpdateAndRename() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - // Perform test - this.dummyDao.updateAndRename(dn, newDn, "Updated description"); - - // Verify that entry was moved and updated. - Object object = this.ldapTemplate.lookup(newDn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(object).isNotNull(); - this.dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2"); - } - - @Test - public void testModifyAttributesWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - // Perform test - this.dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify result - check that the operation was properly rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Person"); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - } - - @Test - public void testModifyAttributes() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - this.dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description"); - - // Verify result - check that the operation was not rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - this.dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person"); - } - - @Test - public void testUnbindWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - // Perform test - this.dummyDao.unbindWithException(dn, "Some Person"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify result - check that the operation was properly rolled back - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - // Just verify that the entry still exists. - return new Object(); - } - }); - - Object jdbcResult = this.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(); - } - }); - - assertThat(ldapResult).isNotNull(); - assertThat(jdbcResult).isNotNull(); - } - - @Test - public void testUnbind() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - this.dummyDao.unbind(dn, "Some Person"); - - try { - // Verify result - check that the operation was not rolled back - this.ldapTemplate.lookup(dn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - try { - this.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) { - assertThat(true).isTrue(); - } - } - -} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerLdap179IntegrationTests.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerLdap179IntegrationTests.java deleted file mode 100644 index ba76a914..00000000 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerLdap179IntegrationTests.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2005-2016 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 - * - * https://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.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.ldap.CommunicationException; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.itest.transaction.compensating.manager.DummyDao; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; -import org.springframework.transaction.CannotCreateTransactionException; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; - -/** - * Integration tests for - * {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager}. - * - * @author Mattias Hellborg Arthursson - */ -@ContextConfiguration(locations = { "/conf/missingLdapAndJdbcTransactionTestContext.xml" }) -public class ContextSourceAndDataSourceTransactionManagerLdap179IntegrationTests - extends AbstractJUnit4SpringContextTests { - - private static Logger log = LoggerFactory - .getLogger(ContextSourceAndDataSourceTransactionManagerLdap179IntegrationTests.class); - - @Autowired - @Qualifier("dummyDao") - private DummyDao dummyDao; - - @Autowired - private LdapTemplate ldapTemplate; - - @Autowired - private JdbcTemplate jdbcTemplate; - - @Before - public void prepareTestedInstance() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - } - - @After - public void cleanup() throws Exception { - this.jdbcTemplate.execute("drop table PERSON if exists"); - } - - @Test - public void verifyThatJdbcTransactionIsClosedIfLdapServerUnavailable_ldap179() { - try { - this.dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); - fail("CannotCreateTransactionException expected"); - } - catch (CannotCreateTransactionException expected) { - assertThat(expected.getCause() instanceof CommunicationException).isTrue(); - } - - // Make sure there is no transaction synchronization - assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse(); - - try { - this.dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); - fail("CannotCreateTransactionException expected"); - } - catch (CannotCreateTransactionException expected) { - assertThat(expected.getCause() instanceof CommunicationException).isTrue(); - } - } - -} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerNamespaceITests.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerNamespaceITests.java deleted file mode 100644 index e83cedba..00000000 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerNamespaceITests.java +++ /dev/null @@ -1,359 +0,0 @@ -/* - * Copyright 2005-2016 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 - * - * https://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 java.sql.ResultSet; -import java.sql.SQLException; - -import javax.naming.NamingException; -import javax.naming.directory.Attributes; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -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.ldap.itest.AbstractLdapTemplateIntegrationTests; -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 static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; - -/** - * Integration tests for - * {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager} - * with namespace configuration. - * - * @author Mattias Hellborg Arthursson - */ -@ContextConfiguration(locations = { "/conf/ldapAndJdbcTransactionNamespaceTestContext.xml" }) -public class ContextSourceAndDataSourceTransactionManagerNamespaceITests extends AbstractLdapTemplateIntegrationTests { - - private static Logger log = LoggerFactory - .getLogger(ContextSourceAndDataSourceTransactionManagerNamespaceITests.class); - - @Autowired - @Qualifier("dummyDao") - private DummyDao dummyDao; - - @Autowired - private LdapTemplate ldapTemplate; - - @Autowired - private JdbcTemplate jdbcTemplate; - - @Before - public void prepareTestedInstance() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - - this.jdbcTemplate.execute("drop table PERSON if exists"); - this.jdbcTemplate - .execute("create table PERSON(fullname VARCHAR(256), lastname VARCHAR(256), description VARCHAR(256))"); - this.jdbcTemplate.update("insert into PERSON values(?, ?, ?)", - new Object[] { "Some Person", "Person", "Sweden, Company1, Some Person" }); - } - - @After - public void cleanup() throws Exception { - this.jdbcTemplate.execute("drop table PERSON if exists"); - } - - @Test - public void testCreateWithException() { - try { - this.dummyDao.createWithException("Sweden", "company1", "some testperson", "testperson", - "some description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - // Verify that no entry was created - try { - this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - try { - this.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) { - assertThat(true).isTrue(); - } - } - - @Test - public void testCreate() { - this.dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); - - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - Object dbResult = this.jdbcTemplate.queryForObject("select * from PERSON where fullname='some testperson'", - new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - return new Object(); - } - }); - assertThat(ldapResult).isNotNull(); - assertThat(dbResult).isNotNull(); - - this.ldapTemplate.unbind("cn=some testperson, ou=company1, ou=Sweden"); - } - - @Test - public void testUpdateWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - this.dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Person"); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - Object jdbcResult = this.jdbcTemplate.queryForObject("select * from PERSON where fullname=?", - new Object[] { "Some Person" }, new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - assertThat(rs.getString("lastname")).isEqualTo("Person"); - assertThat(rs.getString("description")).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - assertThat(ldapResult).isNotNull(); - assertThat(jdbcResult).isNotNull(); - } - - @Test - public void testUpdate() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - this.dummyDao.update(dn, "Some Person", "Updated Person", "Updated description"); - - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated Person"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - Object jdbcResult = this.jdbcTemplate.queryForObject("select * from PERSON where fullname=?", - new Object[] { "Some Person" }, new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) throws SQLException { - assertThat(rs.getString("lastname")).isEqualTo("Updated Person"); - assertThat(rs.getString("description")).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(ldapResult).isNotNull(); - assertThat(jdbcResult).isNotNull(); - this.dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person"); - } - - @Test - public void testUpdateAndRenameWithException() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - try { - // Perform test - this.dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify that entry was not moved. - try { - this.ldapTemplate.lookup(newDn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - // Verify that original entry was not updated. - Object object = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person2"); - return new Object(); - } - }); - assertThat(object).isNotNull(); - } - - @Test - public void testUpdateAndRename() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - // Perform test - this.dummyDao.updateAndRename(dn, newDn, "Updated description"); - - // Verify that entry was moved and updated. - Object object = this.ldapTemplate.lookup(newDn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(object).isNotNull(); - this.dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2"); - } - - @Test - public void testModifyAttributesWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - // Perform test - this.dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify result - check that the operation was properly rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Person"); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - } - - @Test - public void testModifyAttributes() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - this.dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description"); - - // Verify result - check that the operation was not rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - this.dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person"); - } - - @Test - public void testUnbindWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - // Perform test - this.dummyDao.unbindWithException(dn, "Some Person"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify result - check that the operation was properly rolled back - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - // Just verify that the entry still exists. - return new Object(); - } - }); - - Object jdbcResult = this.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(); - } - }); - - assertThat(ldapResult).isNotNull(); - assertThat(jdbcResult).isNotNull(); - } - - @Test - public void testUnbind() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - this.dummyDao.unbind(dn, "Some Person"); - - try { - // Verify result - check that the operation was not rolled back - this.ldapTemplate.lookup(dn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - try { - this.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) { - assertThat(true).isTrue(); - } - } - -} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerIntegrationTests.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerIntegrationTests.java deleted file mode 100644 index bb844578..00000000 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerIntegrationTests.java +++ /dev/null @@ -1,375 +0,0 @@ -/* - * Copyright 2005-2016 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 - * - * https://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.hibernate; - -import java.util.List; - -import javax.naming.NamingException; -import javax.naming.directory.Attributes; - -import org.hibernate.Query; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.Transaction; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -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.AbstractLdapTemplateIntegrationTests; -import org.springframework.ldap.itest.transaction.compensating.manager.DummyException; -import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPerson; -import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPersonDao; -import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager; -import org.springframework.orm.hibernate5.HibernateTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; - -/** - * Integration tests for {@link ContextSourceAndHibernateTransactionManager}. - * - * @author Hans Westerbeek - */ -@ContextConfiguration(locations = { "/conf/ldapAndHibernateTransactionTestContext.xml" }) -public class ContextSourceAndHibernateTransactionManagerIntegrationTests extends AbstractLdapTemplateIntegrationTests { - - private static Logger log = LoggerFactory - .getLogger(ContextSourceAndHibernateTransactionManagerIntegrationTests.class); - - @Autowired - @Qualifier("dummyDao") - private OrgPersonDao dummyDao; - - @Autowired - private LdapTemplate ldapTemplate; - - @Autowired - private HibernateTemplate hibernateTemplate; - - @Autowired - private SessionFactory sessionFactory; - - @Before - public void prepareTest() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - - OrgPerson person = new OrgPerson(); - person.setId(1); - person.setLastname("Person"); - person.setFullname("Some Person"); - person.setDescription("Sweden, Company1, Some Person"); - person.setCountry("Sweden"); - person.setCompany("Company1"); - // "Some Person", "Person", "Sweden, Company1, Some Person" - // avoid the transaction manager we have configured, do it manually - Session session = this.sessionFactory.openSession(); - Transaction tx = session.beginTransaction(); - session.saveOrUpdate(person); - tx.commit(); - session.close(); - - } - - @After - public void cleanup() throws Exception { - // probably the wrong idea, this will use the thing i am trying to - // test.. - - Session session = this.sessionFactory.openSession(); - Transaction tx = session.beginTransaction(); - Query query = session.createQuery("delete from OrgPerson"); - query.executeUpdate(); - tx.commit(); - session.close(); - } - - @Test - public void testCreateWithException() { - OrgPerson person = new OrgPerson(); - - person.setId(2); - person.setDescription("some description"); - person.setFullname("Some testperson"); - person.setLastname("testperson"); - person.setCountry("Sweden"); - person.setCompany("company1"); - - try { - this.dummyDao.createWithException(person); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - // Verify that no entry was created in ldap or hibernate db - try { - this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - List result = this.hibernateTemplate.findByNamedParam("from OrgPerson person where person.lastname = :lastname", - "lastname", person.getLastname()); - assertThat(result.size() == 0).isTrue(); - - } - - @Test - public void testCreate() { - OrgPerson person = new OrgPerson(); - - person.setId(2); - person.setDescription("some description"); - person.setFullname("Some testperson"); - person.setLastname("testperson"); - person.setCountry("Sweden"); - person.setCompany("company1"); - // dummyDao.create("Sweden", "company1", "some testperson", - // "testperson", "some description"); - - this.dummyDao.create(person); - person = null; - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - OrgPerson fromDb = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, 2); - assertThat(ldapResult).isNotNull(); - assertThat(fromDb).isNotNull(); - } - - @Test - public void testUpdateWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - OrgPerson originalPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - originalPerson.setLastname("fooo"); - try { - this.dummyDao.updateWithException(originalPerson); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).as("Person").isNotNull(); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - OrgPerson notUpdatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - assertThat(notUpdatedPerson.getLastname()).isEqualTo("Person"); - assertThat(notUpdatedPerson.getDescription()).isEqualTo("Sweden, Company1, Some Person"); - - assertThat(ldapResult).isNotNull(); - // no need to assert if notUpdatedPerson exists - } - - @Test - public void testUpdate() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - person.setLastname("Updated Person"); - person.setDescription("Updated description"); - - this.dummyDao.update(person); - - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated Person"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - OrgPerson updatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - assertThat(updatedPerson.getLastname()).isEqualTo("Updated Person"); - assertThat(updatedPerson.getDescription()).isEqualTo("Updated description"); - assertThat(ldapResult).isNotNull(); - } - - @Test - public void testUpdateAndRenameWithException() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - person.setLastname("Updated Person"); - person.setDescription("Updated description"); - - try { - // Perform test - this.dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify that entry was not moved. - try { - this.ldapTemplate.lookup(newDn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - // Verify that original entry was not updated. - Object object = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person2"); - return new Object(); - } - }); - assertThat(object).isNotNull(); - } - - @Test - public void testUpdateAndRename() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - // Perform test - this.dummyDao.updateAndRename(dn, newDn, "Updated description"); - - // Verify that entry was moved and updated. - Object object = this.ldapTemplate.lookup(newDn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(object).isNotNull(); - } - - @Test - public void testModifyAttributesWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - // Perform test - this.dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify result - check that the operation was properly rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Person"); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - } - - @Test - public void testModifyAttributes() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - this.dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description"); - - // Verify result - check that the operation was not rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - } - - @Test - public void testUnbindWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - - try { - // Perform test - this.dummyDao.unbindWithException(person); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - person = null; - // Verify result - check that the operation was properly rolled back - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - // Just verify that the entry still exists. - return new Object(); - } - }); - - person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); // will - // throw - // exception - // of - // person - // does - // not - // exist - - assertThat(ldapResult).isNotNull(); - } - - @Test - public void testUnbind() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - this.dummyDao.unbind(person); - - try { - // Verify result - check that the operation was not rolled back - this.ldapTemplate.lookup(dn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - person = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, 1); - assertThat(person).isNull(); - } - -} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerLdap179IntegrationTests.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerLdap179IntegrationTests.java deleted file mode 100644 index 0f73056b..00000000 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerLdap179IntegrationTests.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2005-2016 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 - * - * https://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.hibernate; - -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.ldap.CommunicationException; -import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPerson; -import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPersonDao; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; -import org.springframework.transaction.CannotCreateTransactionException; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Integration tests for - * {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager}. - * - * @author Hans Westerbeek - */ -@ContextConfiguration(locations = { "/conf/missingLdapAndHibernateTransactionTestContext.xml" }) -public class ContextSourceAndHibernateTransactionManagerLdap179IntegrationTests - extends AbstractJUnit4SpringContextTests { - - private static Logger log = LoggerFactory - .getLogger(ContextSourceAndHibernateTransactionManagerLdap179IntegrationTests.class); - - @Autowired - @Qualifier("dummyDao") - private OrgPersonDao dummyDao; - - @Before - public void prepareTest() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - } - - @Test - public void testCreate() { - OrgPerson person = new OrgPerson(); - - person.setId(2); - person.setDescription("some description"); - person.setFullname("Some testperson"); - person.setLastname("testperson"); - person.setCountry("Sweden"); - person.setCompany("company1"); - - try { - this.dummyDao.create(person); - } - catch (CannotCreateTransactionException expected) { - assertThat(expected.getCause() instanceof CommunicationException).isTrue(); - } - - // Make sure there is no transaction synchronization - assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse(); - - try { - this.dummyDao.create(person); - } - catch (CannotCreateTransactionException expected) { - assertThat(expected.getCause() instanceof CommunicationException).isTrue(); - } - } - -} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerNamespaceITests.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerNamespaceITests.java deleted file mode 100644 index 97f4bf25..00000000 --- a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerNamespaceITests.java +++ /dev/null @@ -1,376 +0,0 @@ -/* - * Copyright 2005-2016 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 - * - * https://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.hibernate; - -import java.util.List; - -import javax.naming.NamingException; -import javax.naming.directory.Attributes; - -import org.hibernate.Query; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.Transaction; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -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.AbstractLdapTemplateIntegrationTests; -import org.springframework.ldap.itest.transaction.compensating.manager.DummyException; -import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPerson; -import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPersonDao; -import org.springframework.orm.hibernate5.HibernateTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; - -/** - * Integration tests for - * {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager} - * with namespace configuration. - * - * @author Hans Westerbeek - */ -@ContextConfiguration(locations = { "/conf/ldapAndHibernateTransactionNamespaceTestContext.xml" }) -public class ContextSourceAndHibernateTransactionManagerNamespaceITests extends AbstractLdapTemplateIntegrationTests { - - private static Logger log = LoggerFactory - .getLogger(ContextSourceAndHibernateTransactionManagerNamespaceITests.class); - - @Autowired - @Qualifier("dummyDao") - private OrgPersonDao dummyDao; - - @Autowired - private LdapTemplate ldapTemplate; - - @Autowired - private HibernateTemplate hibernateTemplate; - - @Autowired - private SessionFactory sessionFactory; - - @Before - public void prepareTest() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - - OrgPerson person = new OrgPerson(); - person.setId(1); - person.setLastname("Person"); - person.setFullname("Some Person"); - person.setDescription("Sweden, Company1, Some Person"); - person.setCountry("Sweden"); - person.setCompany("Company1"); - // "Some Person", "Person", "Sweden, Company1, Some Person" - // avoid the transaction manager we have configured, do it manually - Session session = this.sessionFactory.openSession(); - Transaction tx = session.beginTransaction(); - session.saveOrUpdate(person); - tx.commit(); - session.close(); - - } - - @After - public void cleanup() throws Exception { - // probably the wrong idea, this will use the thing i am trying to - // test.. - - Session session = this.sessionFactory.openSession(); - Transaction tx = session.beginTransaction(); - Query query = session.createQuery("delete from OrgPerson"); - query.executeUpdate(); - tx.commit(); - session.close(); - } - - @Test - public void testCreateWithException() { - OrgPerson person = new OrgPerson(); - - person.setId(2); - person.setDescription("some description"); - person.setFullname("Some testperson"); - person.setLastname("testperson"); - person.setCountry("Sweden"); - person.setCompany("company1"); - - try { - this.dummyDao.createWithException(person); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - // Verify that no entry was created in ldap or hibernate db - try { - this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - List result = this.hibernateTemplate.findByNamedParam("from OrgPerson person where person.lastname = :lastname", - "lastname", person.getLastname()); - assertThat(result.size() == 0).isTrue(); - - } - - @Test - public void testCreate() { - OrgPerson person = new OrgPerson(); - - person.setId(2); - person.setDescription("some description"); - person.setFullname("Some testperson"); - person.setLastname("testperson"); - person.setCountry("Sweden"); - person.setCompany("company1"); - // dummyDao.create("Sweden", "company1", "some testperson", - // "testperson", "some description"); - - this.dummyDao.create(person); - person = null; - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); - OrgPerson fromDb = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, 2); - assertThat(ldapResult).isNotNull(); - assertThat(fromDb).isNotNull(); - } - - @Test - public void testUpdateWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - OrgPerson originalPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - originalPerson.setLastname("fooo"); - try { - this.dummyDao.updateWithException(originalPerson); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - log.debug("Verifying result"); - - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).as("Person").isNotNull(); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - OrgPerson notUpdatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - assertThat(notUpdatedPerson.getLastname()).isEqualTo("Person"); - assertThat(notUpdatedPerson.getDescription()).isEqualTo("Sweden, Company1, Some Person"); - - assertThat(ldapResult).isNotNull(); - // no need to assert if notUpdatedPerson exists - } - - @Test - public void testUpdate() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - person.setLastname("Updated Person"); - person.setDescription("Updated description"); - - this.dummyDao.update(person); - - log.debug("Verifying result"); - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated Person"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - OrgPerson updatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - assertThat(updatedPerson.getLastname()).isEqualTo("Updated Person"); - assertThat(updatedPerson.getDescription()).isEqualTo("Updated description"); - assertThat(ldapResult).isNotNull(); - } - - @Test - public void testUpdateAndRenameWithException() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - person.setLastname("Updated Person"); - person.setDescription("Updated description"); - - try { - // Perform test - this.dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify that entry was not moved. - try { - this.ldapTemplate.lookup(newDn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - // Verify that original entry was not updated. - Object object = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person2"); - return new Object(); - } - }); - assertThat(object).isNotNull(); - } - - @Test - public void testUpdateAndRename() { - String dn = "cn=Some Person2,ou=company1,ou=Sweden"; - String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; - // Perform test - this.dummyDao.updateAndRename(dn, newDn, "Updated description"); - - // Verify that entry was moved and updated. - Object object = this.ldapTemplate.lookup(newDn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(object).isNotNull(); - } - - @Test - public void testModifyAttributesWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - try { - // Perform test - this.dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - // Verify result - check that the operation was properly rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Person"); - assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - } - - @Test - public void testModifyAttributes() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - this.dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description"); - - // Verify result - check that the operation was not rolled back - Object result = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertThat(attributes.get("sn").get()).isEqualTo("Updated lastname"); - assertThat(attributes.get("description").get()).isEqualTo("Updated description"); - return new Object(); - } - }); - - assertThat(result).isNotNull(); - } - - @Test - public void testUnbindWithException() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - - try { - // Perform test - this.dummyDao.unbindWithException(person); - fail("DummyException expected"); - } - catch (DummyException expected) { - assertThat(true).isTrue(); - } - - person = null; - // Verify result - check that the operation was properly rolled back - Object ldapResult = this.ldapTemplate.lookup(dn, new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - // Just verify that the entry still exists. - return new Object(); - } - }); - - person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); // will - // throw - // exception - // of - // person - // does - // not - // exist - - assertThat(ldapResult).isNotNull(); - } - - @Test - public void testUnbind() { - String dn = "cn=Some Person,ou=company1,ou=Sweden"; - // Perform test - OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, 1); - this.dummyDao.unbind(person); - - try { - // Verify result - check that the operation was not rolled back - this.ldapTemplate.lookup(dn); - fail("NameNotFoundException expected"); - } - catch (NameNotFoundException expected) { - assertThat(true).isTrue(); - } - - person = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, 1); - assertThat(person).isNull(); - } - -} diff --git a/test/integration-tests/src/test/resources/conf/OrgPerson.hbm.xml b/test/integration-tests/src/test/resources/conf/OrgPerson.hbm.xml deleted file mode 100755 index 54384147..00000000 --- a/test/integration-tests/src/test/resources/conf/OrgPerson.hbm.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionNamespaceTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionNamespaceTestContext.xml deleted file mode 100755 index b7c100cb..00000000 --- a/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionNamespaceTestContext.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - conf/OrgPerson.hbm.xml - - - - - hibernate.dialect=org.hibernate.dialect.HSQLDialect - hibernate.hbm2ddl.auto=create - - - - - - - - - - - diff --git a/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionTestContext.xml deleted file mode 100755 index 4b0546ed..00000000 --- a/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionTestContext.xml +++ /dev/null @@ -1,63 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - conf/OrgPerson.hbm.xml - - - - - hibernate.dialect=org.hibernate.dialect.HSQLDialect - hibernate.hbm2ddl.auto=create - - - - - - - - - - - - - - - - - - - diff --git a/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionNamespaceTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionNamespaceTestContext.xml deleted file mode 100644 index 4957f3e2..00000000 --- a/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionNamespaceTestContext.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionTestContext.xml deleted file mode 100644 index 80f105c4..00000000 --- a/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionTestContext.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/integration-tests/src/test/resources/conf/missingLdapAndHibernateTransactionTestContext.xml b/test/integration-tests/src/test/resources/conf/missingLdapAndHibernateTransactionTestContext.xml deleted file mode 100755 index 3d04bcdb..00000000 --- a/test/integration-tests/src/test/resources/conf/missingLdapAndHibernateTransactionTestContext.xml +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - conf/OrgPerson.hbm.xml - - - - - hibernate.dialect=org.hibernate.dialect.HSQLDialect - hibernate.hbm2ddl.auto=create - - - - - - - - - - - - - - - - - - - - - - - PROPAGATION_REQUIRES_NEW - - - - diff --git a/test/integration-tests/src/test/resources/conf/missingLdapAndJdbcTransactionTestContext.xml b/test/integration-tests/src/test/resources/conf/missingLdapAndJdbcTransactionTestContext.xml deleted file mode 100644 index deee2666..00000000 --- a/test/integration-tests/src/test/resources/conf/missingLdapAndJdbcTransactionTestContext.xml +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - PROPAGATION_REQUIRES_NEW - - - -