Remove LDAP + Data Source Transaction Wrappers

Closes gh-1109
This commit is contained in:
Josh Cummings
2025-06-16 14:06:38 -06:00
parent b6b4158fad
commit 32a866f868
21 changed files with 11 additions and 2658 deletions

View File

@@ -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);

View File

@@ -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 <b>not</b> 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;
}
}
}

View File

@@ -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
* <b>not</b> a JTA XA transaction; no two-phase commit will be performed, and thus commit
* and rollback may yield unexpected results.<br>
* 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.<br>
*
* Furthermore, this manager <b>does not support nested transactions</b>
*
* @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;
}
}
}

View File

@@ -600,28 +600,12 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="data-source-ref" type="xs:token">
<xs:annotation>
<xs:documentation>
Id of the DataSource instance to use.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="session-factory-ref" type="xs:token">
<xs:annotation>
<xs:documentation>
Id of the Hibernate SessionFactory instance to use.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:attributeGroup>
<xs:element name="transaction-manager">
<xs:annotation>
<xs:documentation>
Creates an ContextSourceTransactionManager. If data-source-ref or session-factory-ref is specified,
a DataSourceAndContextSourceTransactionManager/HibernateAndContextSourceTransactionManager will be
created.
Creates an ContextSourceTransactionManager
</xs:documentation>
</xs:annotation>
<xs:complexType>

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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 `<ldap:transaction-manager>` 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"]
----
<ldap:transaction-manager data-source-ref="dataSource" >
<ldap:default-renaming-strategy />
<ldap:transaction-manager />
----
====
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 `<ldap:transaction-manager>` element, as follows:
====
[source,xml]
[subs="verbatim,quotes"]
----
<ldap:transaction-manager session-factory-ref="dataSource" >
<ldap:default-renaming-strategy />
<ldap:transaction-manager />
----
====
This support was removed in Spring LDAP 4.0.
[[ldap-compensating-transactions-explained]]
== LDAP Compensating Transactions Explained

View File

@@ -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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}

View File

@@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false">
<class name="org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPerson" table="PERSON">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="fullname" column="fullname"/>
<property name="lastname" column="lastname"/>
<property name="company" column="company"/>
<property name="country" column="country"/>
<property name="description" column="description"/>
</class>
</hibernate-mapping>

View File

@@ -1,51 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/ldap https://www.springframework.org/schema/ldap/spring-ldap.xsd">
<import resource="classpath:/conf/commonTestContext.xml"/>
<import resource="classpath:/conf/commonContextSourceConfig.xml"/>
<ldap:ldap-template id="ldapTemplate"/>
<ldap:transaction-manager session-factory-ref="sessionFactory">
<ldap:default-renaming-strategy />
</ldap:transaction-manager>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:aname" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>conf/OrgPerson.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<bean name="dummyDao"
class="org.springframework.ldap.itest.transaction.compensating.manager.hibernate.DummyDaoLdapAndHibernateImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
</beans>

View File

@@ -1,63 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="classpath:/conf/commonTestContext.xml"/>
<import resource="classpath:/conf/commonNoNamespaceContextSourceConfig.xml"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:aname" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactedContextSource"
class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="transactedContextSource" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>conf/OrgPerson.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="contextSource" ref="transactedContextSource" />
<property name="renamingStrategy">
<bean class="org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy" />
</property>
</bean>
<bean name="dummyDao"
class="org.springframework.ldap.itest.transaction.compensating.manager.hibernate.DummyDaoLdapAndHibernateImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
</beans>

View File

@@ -1,52 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/ldap https://www.springframework.org/schema/ldap/spring-ldap.xsd">
<import resource="classpath:/conf/commonTestContext.xml"/>
<import resource="classpath:/conf/commonContextSourceConfig.xml"/>
<ldap:ldap-template id="ldapTemplate"/>
<ldap:transaction-manager data-source-ref="dataSource">
<ldap:default-renaming-strategy />
</ldap:transaction-manager>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:aname" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean name="dummyDao"
class="org.springframework.ldap.itest.transaction.compensating.manager.LdapAndJdbcDummyDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<tx:annotation-driven />
</beans>

View File

@@ -1,49 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="classpath:/conf/commonTestContext.xml"/>
<import resource="classpath:/conf/commonNoNamespaceContextSourceConfig.xml"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:aname" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactedContextSource"
class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="transactedContextSource" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager"
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="contextSource" ref="transactedContextSource" />
<property name="renamingStrategy">
<bean class="org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy" />
</property>
</bean>
<bean name="dummyDao"
class="org.springframework.ldap.itest.transaction.compensating.manager.LdapAndJdbcDummyDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<tx:annotation-driven />
</beans>

View File

@@ -1,80 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:/conf/commonTestContext.xml"/>
<!-- This LDAP server shouldn't exist, and that's what we're testing -->
<bean id="contextSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="userDn" value="${userDn}" />
<property name="password" value="${password}" />
<property name="url" value="ldap://127.0.0.1:1337" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:aname" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactedContextSource"
class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="transactedContextSource" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>conf/OrgPerson.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="contextSource" ref="transactedContextSource" />
<property name="renamingStrategy">
<bean class="org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy" />
</property>
</bean>
<bean name="dummyDaoTarget"
class="org.springframework.ldap.itest.transaction.compensating.manager.hibernate.DummyDaoLdapAndHibernateImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="dummyDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"
ref="transactionManager" />
<property name="target" ref="dummyDaoTarget" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
</beans>

View File

@@ -1,66 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:/conf/commonTestContext.xml"/>
<!-- This LDAP server shouldn't exist, and that's what we're testing -->
<bean id="contextSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="userDn" value="${userDn}" />
<property name="password" value="${password}" />
<property name="url" value="ldap://127.0.0.1:1337" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:aname" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactedContextSource"
class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="transactedContextSource" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager"
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="contextSource" ref="transactedContextSource" />
<property name="renamingStrategy">
<bean class="org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy" />
</property>
</bean>
<bean name="dummyDaoTarget"
class="org.springframework.ldap.itest.transaction.compensating.manager.LdapAndJdbcDummyDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean name="dummyDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"
ref="transactionManager" />
<property name="target" ref="dummyDaoTarget" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
</beans>