Changed strategy for rebind and unbind, storing renaming the entry temporarily.

This caused changes in CompensatingTransactionRollbackOperation - 
this interface along with implementing classes should be renamed.
This commit is contained in:
Mattias Arthursson
2007-01-13 13:10:58 +00:00
parent f08f9adc82
commit 8099b2e312
31 changed files with 784 additions and 214 deletions

View File

@@ -1,52 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" output="target/itest-classes" path="src/itest/java"/>
<classpathentry kind="src" output="target/itest-openldap-classes" path="src/itest-openldap/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/global/commons-logging.jar" sourcepath="lib/source/commons-logging-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-beans.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-context.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-core.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-dao.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/spring-mock.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/gsbase.jar"/>
<classpathentry kind="lib" path="lib/global/commons-lang.jar" sourcepath="lib/source/commons-lang-src.zip"/>
<classpathentry kind="lib" path="lib/global/commons-collections.jar" sourcepath="lib/source/commons-collections-src.zip"/>
<classpathentry kind="lib" path="lib/test/junit.jar" sourcepath="lib/source/junit-src.zip"/>
<classpathentry kind="lib" path="lib/test/easymock.jar" sourcepath="lib/source/easymock-src.zip"/>
<classpathentry kind="lib" path="lib/test/clover.jar"/>
<classpathentry kind="lib" path="lib/test/ehcache.jar"/>
<classpathentry kind="lib" path="lib/test/ehcache-constructs.jar"/>
<classpathentry kind="lib" path="lib/test/jxl.jar"/>
<classpathentry kind="lib" path="lib/test/cglib-nodep.jar"/>
<classpathentry kind="lib" path="lib/buildtime/acegi-security.jar" sourcepath="lib/source/acegi-security-src.zip"/>
<classpathentry kind="lib" path="lib/test/antlr.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-core.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-core-shared.jar"/>
<classpathentry kind="lib" path="lib/test/jdbm.jar"/>
<classpathentry kind="lib" path="lib/test/shared-asn1.jar"/>
<classpathentry kind="lib" path="lib/test/shared-ldap.jar"/>
<classpathentry kind="lib" path="lib/test/slf4j-log4j12.jar"/>
<classpathentry kind="lib" path="lib/test/log4j.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-server-jndi.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-shared.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-changepw.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-kerberos.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-kerberos-shared.jar"/>
<classpathentry kind="lib" path="lib/test/mina-core.jar"/>
<classpathentry kind="lib" path="lib/test/mina-filter-ssl.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-server-main.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-ldap.jar"/>
<classpathentry kind="lib" path="lib/test/junit-addons.jar"/>
<classpathentry kind="lib" path="lib/test/DDSteps.jar"/>
<classpathentry kind="lib" path="lib/global/ldapbp.jar"/>
<classpathentry kind="lib" path="lib/buildtime/spring-jdbc.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/buildtime/spring-jpa.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/spring-aop.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/aopalliance.jar"/>
<classpathentry kind="lib" path="lib/test/backport-util-concurrent.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/spring-ldap"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" output="target/itest-classes" path="src/itest/java"/>
<classpathentry kind="src" output="target/itest-openldap-classes" path="src/itest-openldap/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/global/commons-logging.jar" sourcepath="lib/source/commons-logging-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-beans.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-context.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-core.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/global/spring-dao.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/spring-mock.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/gsbase.jar"/>
<classpathentry kind="lib" path="lib/global/commons-lang.jar" sourcepath="lib/source/commons-lang-src.zip"/>
<classpathentry kind="lib" path="lib/global/commons-collections.jar" sourcepath="lib/source/commons-collections-src.zip"/>
<classpathentry kind="lib" path="lib/test/junit.jar" sourcepath="lib/source/junit-src.zip"/>
<classpathentry kind="lib" path="lib/test/easymock.jar" sourcepath="lib/source/easymock-src.zip"/>
<classpathentry kind="lib" path="lib/test/clover.jar"/>
<classpathentry kind="lib" path="lib/test/ehcache.jar"/>
<classpathentry kind="lib" path="lib/test/ehcache-constructs.jar"/>
<classpathentry kind="lib" path="lib/test/jxl.jar"/>
<classpathentry kind="lib" path="lib/test/cglib-nodep.jar"/>
<classpathentry kind="lib" path="lib/buildtime/acegi-security.jar" sourcepath="lib/source/acegi-security-src.zip"/>
<classpathentry kind="lib" path="lib/test/antlr.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-core.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-core-shared.jar"/>
<classpathentry kind="lib" path="lib/test/jdbm.jar"/>
<classpathentry kind="lib" path="lib/test/shared-asn1.jar"/>
<classpathentry kind="lib" path="lib/test/shared-ldap.jar"/>
<classpathentry kind="lib" path="lib/test/slf4j-log4j12.jar"/>
<classpathentry kind="lib" path="lib/test/log4j.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-server-jndi.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-shared.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-changepw.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-kerberos.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-kerberos-shared.jar"/>
<classpathentry kind="lib" path="lib/test/mina-core.jar"/>
<classpathentry kind="lib" path="lib/test/mina-filter-ssl.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-server-main.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-ldap.jar"/>
<classpathentry kind="lib" path="lib/test/junit-addons.jar"/>
<classpathentry kind="lib" path="lib/test/DDSteps.jar"/>
<classpathentry kind="lib" path="lib/global/ldapbp.jar"/>
<classpathentry kind="lib" path="lib/buildtime/spring-jdbc.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/buildtime/spring-jpa.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/spring-aop.jar" sourcepath="lib/source/spring-src.zip"/>
<classpathentry kind="lib" path="lib/test/aopalliance.jar"/>
<classpathentry kind="lib" path="lib/test/backport-util-concurrent.jar"/>
<classpathentry kind="lib" path="lib/test/hsqldb.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/spring-ldap"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -1,7 +1,7 @@
<?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
<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
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="classpath:/conf/apacheDsContext.xml" />
@@ -21,6 +21,15 @@
<property name="dirObjectFactory"
value="org.springframework.ldap.core.DefaultDirObjectFactory" />
</bean>
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
-->
<bean id="contextSource"
class="org.springframework.ldap.support.transaction.TransactionAwareContextSourceProxy">
@@ -32,19 +41,36 @@
<constructor-arg ref="contextSource" />
</bean>
<bean id="transactionManager"
<!--
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
-->
<bean id="ldapTransactionManager"
class="org.springframework.ldap.support.transaction.ContextSourceTransactionManager">
<property name="contextSource" ref="contextSourceTarget" />
</bean>
<!--
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
-->
<bean name="dummyDaoTarget"
class="org.springframework.ldap.support.transaction.DummyDaoImpl">
<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="transactionManager"
ref="ldapTransactionManager" />
<property name="target" ref="dummyDaoTarget" />
<property name="transactionAttributes">
<props>
@@ -52,4 +78,16 @@
</props>
</property>
</bean>
<!--
<bean name="dummyDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="dataSourceTransactionManager" />
<property name="target" ref="ldapDummyDao" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
-->
</beans>

View File

@@ -21,7 +21,7 @@ public class ContextSourceTransactionManagerIntegrationTest extends
setAutowireMode(AbstractDependencyInjectionSpringContextTests.AUTOWIRE_BY_NAME);
}
private DummyDaoImpl dummyDao;
private DummyDao dummyDao;
private LdapTemplate ldapTemplate;
@@ -35,7 +35,7 @@ public class ContextSourceTransactionManagerIntegrationTest extends
this.ldapTemplate = ldapTemplate;
}
public void setDummyDao(DummyDaoImpl dummyDaoImpl) {
public void setDummyDao(DummyDao dummyDaoImpl) {
this.dummyDao = dummyDaoImpl;
}

View File

@@ -1,22 +1,43 @@
package org.springframework.ldap.support.transaction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
public class DummyDaoImpl {
public class DummyDaoImpl implements DummyDao {
private LdapTemplate ldapTemplate;
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#createWithException(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
public void createWithException(String country, String company,
String fullname, String lastname, String description) {
create(country, company, fullname, lastname, description);
throw new DummyException("This method failed");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#create(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
public void create(String country, String company, String fullname,
String lastname, String description) {
DistinguishedName dn = new DistinguishedName();
@@ -30,8 +51,15 @@ public class DummyDaoImpl {
ctx.setAttributeValue("sn", lastname);
ctx.setAttributeValue("description", description);
ldapTemplate.bind(dn, ctx, null);
// jdbcTemplate.execute("insert into test values(1, 'kalle', 'pettersson', 123)");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#update(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void update(String dn, String lastname, String description) {
DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
ctx.setAttributeValue("sn", lastname);
@@ -41,12 +69,24 @@ public class DummyDaoImpl {
ldapTemplate.rebind(dn, ctx, null);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#updateWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateWithException(String dn, String lastname,
String description) {
update(dn, lastname, description);
throw new DummyException("This method failed.");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#updateAndRename(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateAndRename(String dn, String newDn, String description) {
DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
ctx.setAttributeValue("description", description);
@@ -57,12 +97,24 @@ public class DummyDaoImpl {
ldapTemplate.rename(dn, newDn);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#updateAndRenameWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateAndRenameWithException(String dn, String newDn,
String description) {
updateAndRename(dn, newDn, description);
throw new DummyException("This method failed.");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#modifyAttributes(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void modifyAttributes(String dn, String lastName, String description) {
DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
ctx.setAttributeValue("sn", lastName);
@@ -71,16 +123,32 @@ public class DummyDaoImpl {
ldapTemplate.modifyAttributes(dn, ctx.getModificationItems());
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#modifyAttributesWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void modifyAttributesWithException(String dn, String lastName,
String description) {
modifyAttributes(dn, lastName, description);
throw new DummyException("This method failed.");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#unbind(java.lang.String)
*/
public void unbind(String dn) {
ldapTemplate.unbind(dn);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.DummyDao#unbindWithException(java.lang.String)
*/
public void unbindWithException(String dn) {
unbind(dn);
throw new DummyException("This operation failed.");

View File

@@ -1,9 +1,9 @@
package org.springframework.ldap.support.transaction;
public class DummyServiceImpl {
private DummyDaoImpl dummyDaoImpl;
private DummyDao dummyDaoImpl;
public void setDummyDaoImpl(DummyDaoImpl dummyDaoImpl) {
public void setDummyDaoImpl(DummyDao dummyDaoImpl) {
this.dummyDaoImpl = dummyDaoImpl;
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.ldap.support.transaction;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.LdapOperations;
@@ -50,8 +51,22 @@ public class BindRecordingOperation implements
*/
public CompensatingTransactionRollbackOperation recordOperation(
Object[] args) {
if (args == null || args.length != 3) {
throw new IllegalArgumentException(
"Invalid arguments for bind operation");
}
Name dn = LdapUtils.getFirstArgumentAsName(args);
return new UnbindRollbackOperation(ldapOperations, dn);
Object object = args[1];
Attributes attributes = null;
if (args[2] != null && !(args[2] instanceof Attributes)) {
throw new IllegalArgumentException(
"Invalid third argument to bind operation");
} else if (args[2] != null) {
attributes = (Attributes) args[2];
}
return new UnbindRollbackOperation(ldapOperations, dn, object,
attributes);
}
/**

View File

@@ -36,7 +36,9 @@ public class BindRollbackOperation implements
private LdapOperations ldapOperations;
private DirContextOperations dirContextOperations;
private Name originalDn;
private Name temporaryDn;
/**
* Constructor.
@@ -44,15 +46,17 @@ public class BindRollbackOperation implements
* @param ldapOperations
* The {@link LdapOperations} to use for performing the rollback
* operation.
* @param dirContextOperations
* a {@link DirContextOperations} instance to be used for
* obtaining the DN of the affected entry and to be used when
* performing the rollback, binding it to the DN.
* @param originalDn
* The original DN of the entry to be removed.
* @param temporaryDn
* Temporary DN of the entry to be removed; this is where the
* entry is temporarily stored during the transaction.
*/
public BindRollbackOperation(LdapOperations ldapOperations,
DirContextOperations dirContextOperations) {
Name originalDn, Name temporaryDn) {
this.ldapOperations = ldapOperations;
this.dirContextOperations = dirContextOperations;
this.originalDn = originalDn;
this.temporaryDn = temporaryDn;
}
/*
@@ -61,20 +65,48 @@ public class BindRollbackOperation implements
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#rollback()
*/
public void rollback() {
Name dn = dirContextOperations.getDn();
try {
ldapOperations.bind(dn, dirContextOperations, null);
ldapOperations.rename(temporaryDn, originalDn);
} catch (Exception e) {
log.warn("Filed to rollback unbind operation, dn: " + dn);
log.warn("Filed to rollback unbind operation, temporaryDn: "
+ temporaryDn + "; originalDn: " + originalDn);
}
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#commit()
*/
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#commit()
*/
public void commit() {
log.debug("Committing unbind operation - unbinding temporary entry");
ldapOperations.unbind(temporaryDn);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#performOperation()
*/
public void performOperation() {
log.debug("Nothing to do in performOperation for unbind");
}
LdapOperations getLdapOperations() {
return ldapOperations;
}
DirContextOperations getDirContextOperations() {
return dirContextOperations;
Name getOriginalDn() {
return originalDn;
}
Name getTemporaryDn() {
return temporaryDn;
}
}

View File

@@ -41,4 +41,10 @@ public interface CompensatingTransactionDataManager {
* rollback operations.
*/
public void rollback();
/**
* Commit all recorded operations. In many cases this means doing nothing,
* but in some cases some temporary data will need to be removed.
*/
public void commit();
}

View File

@@ -32,4 +32,8 @@ public interface CompensatingTransactionRollbackOperation {
* {@link CompensatingTransactionRecordingOperation}).
*/
public void rollback();
public void commit();
public void performOperation();
}

View File

@@ -116,7 +116,10 @@ public class ContextSourceTransactionManager extends
*/
protected void doCommit(DefaultTransactionStatus status)
throws TransactionException {
// Nothing much to do here.
ContextSourceTransactionObject txObject = (ContextSourceTransactionObject) status
.getTransaction();
txObject.getContextHolder().getTransactionDataManager().commit();
}
/*

View File

@@ -79,4 +79,13 @@ public class DefaultCompensatingTransactionDataManager implements
this.rollbackOperations = rollbackOperations;
}
public void commit() {
log.debug("Performing rollback");
while (!rollbackOperations.isEmpty()) {
CompensatingTransactionRollbackOperation rollbackOperation = (CompensatingTransactionRollbackOperation) rollbackOperations
.pop();
rollbackOperation.commit();
}
}
}

View File

@@ -161,11 +161,9 @@ public class LdapUtils {
Object result = null;
// Perform the target operation
try {
result = method.invoke(transactionContextHolder.getCtx(), args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
rollbackOperation.performOperation();
// result = method.invoke(transactionContextHolder.getCtx(),
// args);
transactionDataManager.operationPerformed(rollbackOperation);
return result;

View File

@@ -82,7 +82,7 @@ public class ModifyAttributesRecordingOperation implements
}
return new ModifyAttributesRollbackOperation(ldapOperations, dn,
rollbackItems);
incomingModifications, rollbackItems);
}
/**

View File

@@ -40,7 +40,9 @@ public class ModifyAttributesRollbackOperation implements
private Name dn;
private ModificationItem[] modificationItems;
private ModificationItem[] compensatingModifications;
private ModificationItem[] actualModifications;
/**
* Constructor.
@@ -50,14 +52,19 @@ public class ModifyAttributesRollbackOperation implements
* operation.
* @param dn
* the DN of the target entry.
* @param modificationItems
* @param actualModifications
* the actual modificationItems that were sent to the
* modifyAttributes operation.
* @param compensatingModifications
* the ModificationItems to undo the recorded operation.
*/
public ModifyAttributesRollbackOperation(LdapOperations ldapOperations,
Name dn, ModificationItem[] modificationItems) {
Name dn, ModificationItem[] actualModifications,
ModificationItem[] compensatingModifications) {
this.ldapOperations = ldapOperations;
this.dn = dn;
this.modificationItems = modificationItems;
this.actualModifications = actualModifications;
this.compensatingModifications = compensatingModifications;
}
/*
@@ -67,7 +74,8 @@ public class ModifyAttributesRollbackOperation implements
*/
public void rollback() {
try {
ldapOperations.modifyAttributes(dn, modificationItems);
log.debug("Rolling back modifyAttributes operation");
ldapOperations.modifyAttributes(dn, compensatingModifications);
} catch (Exception e) {
log
.warn("Failed to rollback ModifyAttributes operation, dn: "
@@ -75,6 +83,25 @@ public class ModifyAttributesRollbackOperation implements
}
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#commit()
*/
public void commit() {
log.debug("Nothing to do in commit for modifyAttributes");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#performOperation()
*/
public void performOperation() {
log.debug("Performing modifyAttributes operation");
ldapOperations.modifyAttributes(dn, actualModifications);
}
Name getDn() {
return dn;
}
@@ -83,8 +110,12 @@ public class ModifyAttributesRollbackOperation implements
return ldapOperations;
}
ModificationItem[] getModificationItems() {
return modificationItems;
ModificationItem[] getActualModifications() {
return actualModifications;
}
ModificationItem[] getCompensatingModifications() {
return compensatingModifications;
}
}

View File

@@ -37,4 +37,12 @@ public class NullRollbackOperation implements
log.info("Rolling back null operation");
}
public void commit() {
log.info("Committing back null operation");
}
public void performOperation() {
log.info("Performing null operation");
}
}

View File

@@ -15,12 +15,17 @@
*/
package org.springframework.ldap.support.transaction;
import java.util.List;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapRdn;
import org.springframework.ldap.core.LdapRdnComponent;
/**
* A {@link CompensatingTransactionRecordingOperation} keeping track of a rebind
@@ -54,18 +59,36 @@ public class RebindRecordingOperation implements
*/
public CompensatingTransactionRollbackOperation recordOperation(
Object[] args) {
Name dn = LdapUtils.getFirstArgumentAsName(args);
try {
DirContextAdapter ctx = (DirContextAdapter) ldapOperations
.lookup(dn);
return new RebindRollbackOperation(ldapOperations, ctx);
} catch (Exception e) {
log.warn(
"Failed to create rollback operation, dn " + dn.toString(),
e);
return new NullRollbackOperation();
if (args == null || args.length != 3) {
throw new IllegalArgumentException(
"Invalid arguments for bind operation");
}
Name dn = LdapUtils.getFirstArgumentAsName(args);
Object object = args[1];
Attributes attributes = null;
if (args[2] != null && !(args[2] instanceof Attributes)) {
throw new IllegalArgumentException(
"Invalid third argument to bind operation");
} else if (args[2] != null) {
attributes = (Attributes) args[2];
}
Name temporaryName = getTemporaryName(dn);
ldapOperations.rename(dn, temporaryName);
return new RebindRollbackOperation(ldapOperations, dn, temporaryName,
object, attributes);
}
Name getTemporaryName(Name originalName) {
DistinguishedName temporaryName = new DistinguishedName(originalName);
List names = temporaryName.getNames();
LdapRdn rdn = (LdapRdn) names.get(names.size() - 1);
LdapRdnComponent rdnComponent = rdn.getComponent();
String value = rdnComponent.getValue();
rdnComponent.setValue(value + "_temp");
return temporaryName;
}
/**

View File

@@ -16,6 +16,7 @@
package org.springframework.ldap.support.transaction;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -36,31 +37,36 @@ public class RebindRollbackOperation implements
private LdapOperations ldapOperations;
private DirContextOperations dirContextOperations;
private Name originalDn;
private Name temporaryDn;
private Object originalObject;
private Attributes originalAttributes;
/**
* Constructor.
*
* @param ldapOperations
* the {@link LdapOperations} to use to perform the rollback.
* @param dirContextOperations
* the {@link DirContextOperations} to use as input to the rebind
* operation performing the rollback.
* @param originalDn
* The original DN of the entry to bind.
* @param temporaryDn
* The temporary DN of the entry.
* @param originalObject
* Original 'object' parameter sent to the rebind operation.
* @param originalAttributes
* Original 'attributes' parameter sent to the rebind operation
*/
public RebindRollbackOperation(LdapOperations ldapOperations,
DirContextOperations dirContextOperations) {
Name originalDn, Name temporaryDn, Object originalObject,
Attributes originalAttributes) {
this.ldapOperations = ldapOperations;
this.dirContextOperations = dirContextOperations;
}
/**
* Get the targegt DirContextOperations. Package private for testing
* purposes.
*
* @return the DirContextOperations.
*/
DirContextOperations getDirContextOperations() {
return dirContextOperations;
this.originalDn = originalDn;
this.temporaryDn = temporaryDn;
this.originalObject = originalObject;
this.originalAttributes = originalAttributes;
}
/**
@@ -78,12 +84,51 @@ public class RebindRollbackOperation implements
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#rollback()
*/
public void rollback() {
Name dn = dirContextOperations.getDn();
log.debug("Rolling back rebind operation");
try {
ldapOperations.rebind(dn, dirContextOperations, null);
ldapOperations.unbind(originalDn);
ldapOperations.rename(temporaryDn, originalDn);
} catch (Exception e) {
log.warn("Failed to rollback operation, dn: " + dn, e);
log.warn("Failed to rollback operation, dn: " + originalDn
+ "; temporary DN: " + temporaryDn, e);
}
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#commit()
*/
public void commit() {
log.debug("Committing rebind operation");
ldapOperations.unbind(temporaryDn);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#performOperation()
*/
public void performOperation() {
log.debug("Performing rebind operation - "
+ "binding new contents to entry.");
ldapOperations.bind(originalDn, originalObject, originalAttributes);
}
Attributes getOriginalAttributes() {
return originalAttributes;
}
Name getOriginalDn() {
return originalDn;
}
Object getOriginalObject() {
return originalObject;
}
Name getTemporaryDn() {
return temporaryDn;
}
}

View File

@@ -63,7 +63,7 @@ public class RenameRecordingOperation implements
}
Name oldDn = LdapUtils.getArgumentAsName(args[0]);
Name newDn = LdapUtils.getArgumentAsName(args[1]);
return new RenameRollbackOperation(ldapOperations, newDn, oldDn);
return new RenameRollbackOperation(ldapOperations, oldDn, newDn);
}
LdapOperations getLdapOperations() {

View File

@@ -23,8 +23,7 @@ import org.springframework.ldap.core.LdapOperations;
/**
* A {@link CompensatingTransactionRollbackOperation} to roll back a previous
* rename operation. Has a fromDn and a toDn, representing from and to in the
* rename operation to be rolled back.
* rename operation.
*
* @author Mattias Arthursson
*
@@ -36,9 +35,9 @@ public class RenameRollbackOperation implements
private LdapOperations ldapOperations;
private Name fromDn;
private Name newDn;
private Name toDn;
private Name originalDn;
/**
* Constructor.
@@ -46,38 +45,62 @@ public class RenameRollbackOperation implements
* @param ldapOperations
* The {@link LdapOperations} to use for performing the rollback
* operation.
* @param fromDn
* DN that the entry has been moved to in the recorded operation.
* @param toDn
* @param originalDn
* DN that the entry was moved from in the recorded operation.
* @param newDn
* DN that the entry has been moved to in the recorded operation.
*/
public RenameRollbackOperation(LdapOperations ldapOperations, Name fromDn,
Name toDn) {
public RenameRollbackOperation(LdapOperations ldapOperations,
Name originalDn, Name newDn) {
this.ldapOperations = ldapOperations;
this.fromDn = fromDn;
this.toDn = toDn;
this.originalDn = originalDn;
this.newDn = newDn;
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#rollback()
*/
public void rollback() {
log.debug("Rolling back rename operation");
try {
ldapOperations.rename(fromDn, toDn);
ldapOperations.rename(newDn, originalDn);
} catch (Exception e) {
log.warn("Unable to rollback rename operation. " + "fromDn: "
+ fromDn + "; toDn: " + toDn);
log.warn("Unable to rollback rename operation. " + "originalDn: "
+ newDn + "; newDn: " + originalDn);
}
}
Name getFromDn() {
return fromDn;
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#commit()
*/
public void commit() {
log.debug("Nothing to do in commit for rename operation");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#performOperation()
*/
public void performOperation() {
log.debug("Performing rename operation");
ldapOperations.rename(originalDn, newDn);
}
Name getNewDn() {
return newDn;
}
LdapOperations getLdapOperations() {
return ldapOperations;
}
Name getToDn() {
return toDn;
Name getOriginalDn() {
return originalDn;
}
}

View File

@@ -15,10 +15,14 @@
*/
package org.springframework.ldap.support.transaction;
import java.util.List;
import javax.naming.Name;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapRdn;
import org.springframework.ldap.core.LdapRdnComponent;
/**
* {@link CompensatingTransactionRecordingOperation} to keep track of unbind
@@ -52,8 +56,22 @@ public class UnbindRecordingOperation implements
public CompensatingTransactionRollbackOperation recordOperation(
Object[] args) {
Name dn = LdapUtils.getFirstArgumentAsName(args);
DirContextAdapter ctx = (DirContextAdapter) ldapOperations.lookup(dn);
return new BindRollbackOperation(ldapOperations, ctx);
Name temporaryDn = getTemporaryName(dn);
ldapOperations.rename(dn, temporaryDn);
return new BindRollbackOperation(ldapOperations, dn, temporaryDn);
}
Name getTemporaryName(Name originalName) {
DistinguishedName temporaryName = new DistinguishedName(originalName);
List names = temporaryName.getNames();
LdapRdn rdn = (LdapRdn) names.get(names.size() - 1);
LdapRdnComponent rdnComponent = rdn.getComponent();
String value = rdnComponent.getValue();
rdnComponent.setValue(value + "_temp");
return temporaryName;
}
LdapOperations getLdapOperations() {

View File

@@ -16,6 +16,7 @@
package org.springframework.ldap.support.transaction;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -35,6 +36,10 @@ public class UnbindRollbackOperation implements
private Name dn;
private Object originalObject;
private Attributes originalAttributes;
/**
* Constructor.
*
@@ -43,10 +48,19 @@ public class UnbindRollbackOperation implements
* operation.
* @param dn
* DN of the entry to be unbound.
* @param originalObject
* original value sent to the 'object' parameter of the bind
* operation.
* @param originalAttributes
* original value sent to the 'attributes' parameter of the bind
* operation.
*/
public UnbindRollbackOperation(LdapOperations ldapOperations, Name dn) {
public UnbindRollbackOperation(LdapOperations ldapOperations, Name dn,
Object originalObject, Attributes originalAttributes) {
this.ldapOperations = ldapOperations;
this.dn = dn;
this.originalObject = originalObject;
this.originalAttributes = originalAttributes;
}
/*
@@ -62,6 +76,25 @@ public class UnbindRollbackOperation implements
}
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#commit()
*/
public void commit() {
log.debug("Nothing to do in commit for bind operation");
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionRollbackOperation#performOperation()
*/
public void performOperation() {
log.debug("Performing bind operation");
ldapOperations.bind(dn, originalObject, originalAttributes);
}
/**
* Get the DN. Package private for testing purposes.
*
@@ -80,4 +113,12 @@ public class UnbindRollbackOperation implements
return ldapOperations;
}
Attributes getOriginalAttributes() {
return originalAttributes;
}
Object getOriginalObject() {
return originalObject;
}
}

View File

@@ -1,5 +1,7 @@
package org.springframework.ldap.support.transaction;
import javax.naming.directory.BasicAttributes;
import junit.framework.TestCase;
import org.easymock.MockControl;
@@ -27,14 +29,20 @@ public class BindRecordingOperationTest extends TestCase {
ldapOperationsMock);
DistinguishedName expectedDn = new DistinguishedName("cn=John Doe");
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
// Perform test.
CompensatingTransactionRollbackOperation operation = tested
.recordOperation(new Object[] { expectedDn });
.recordOperation(new Object[] { expectedDn, expectedObject,
expectedAttributes });
assertTrue(operation instanceof UnbindRollbackOperation);
UnbindRollbackOperation rollbackOperation = (UnbindRollbackOperation) operation;
assertSame(expectedDn, rollbackOperation.getDn());
assertSame(ldapOperationsMock, rollbackOperation.getLdapOperations());
assertSame(expectedObject, rollbackOperation.getOriginalObject());
assertSame(expectedAttributes, rollbackOperation
.getOriginalAttributes());
}
public void testPerformOperation_String() {
@@ -42,9 +50,12 @@ public class BindRecordingOperationTest extends TestCase {
ldapOperationsMock);
String expectedDn = "cn=John Doe";
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
// Perform test.
CompensatingTransactionRollbackOperation operation = tested
.recordOperation(new Object[] { expectedDn });
.recordOperation(new Object[] { expectedDn, expectedObject,
expectedAttributes });
assertTrue(operation instanceof UnbindRollbackOperation);
UnbindRollbackOperation rollbackOperation = (UnbindRollbackOperation) operation;

View File

@@ -12,53 +12,65 @@ public class BindRollbackOperationTest extends TestCase {
private LdapOperations ldapOperationsMock;
private MockControl dirContextOperationsControl;
private DirContextOperations dirContextOperationsMock;
protected void setUp() throws Exception {
ldapOperationsControl = MockControl.createControl(LdapOperations.class);
ldapOperationsMock = (LdapOperations) ldapOperationsControl.getMock();
dirContextOperationsControl = MockControl
.createControl(DirContextOperations.class);
dirContextOperationsMock = (DirContextOperations) dirContextOperationsControl
.getMock();
}
protected void tearDown() throws Exception {
ldapOperationsControl = null;
ldapOperationsMock = null;
dirContextOperationsControl = null;
dirContextOperationsMock = null;
}
protected void replay() {
ldapOperationsControl.replay();
dirContextOperationsControl.replay();
}
protected void verify() {
ldapOperationsControl.verify();
dirContextOperationsControl.verify();
}
public void testPerformOperation() {
DistinguishedName expectedOldName = new DistinguishedName("cn=oldDn");
DistinguishedName expectedTempName = new DistinguishedName("cn=newDn");
BindRollbackOperation tested = new BindRollbackOperation(
ldapOperationsMock, expectedOldName, expectedTempName);
// Nothing to do in performOperation for unbind.
replay();
// Perform test
tested.performOperation();
verify();
}
public void testCommit() {
DistinguishedName expectedOldName = new DistinguishedName("cn=oldDn");
DistinguishedName expectedTempName = new DistinguishedName("cn=newDn");
BindRollbackOperation tested = new BindRollbackOperation(
ldapOperationsMock, expectedOldName, expectedTempName);
ldapOperationsMock.unbind(expectedTempName);
replay();
// Perform test
tested.commit();
verify();
}
public void testRollback() {
DistinguishedName expectedOldName = new DistinguishedName("cn=oldDn");
DistinguishedName expectedTempName = new DistinguishedName("cn=newDn");
BindRollbackOperation tested = new BindRollbackOperation(
ldapOperationsMock, dirContextOperationsMock);
ldapOperationsMock, expectedOldName, expectedTempName);
DistinguishedName expectedDn = new DistinguishedName("cn=john doe");
dirContextOperationsControl.expectAndReturn(dirContextOperationsMock
.getDn(), expectedDn);
ldapOperationsMock.bind(expectedDn, dirContextOperationsMock, null);
ldapOperationsMock.rename(expectedTempName, expectedOldName);
replay();
// Perform test
tested.rollback();
verify();
}
}

View File

@@ -98,9 +98,10 @@ public class ModifyAttributesRecordingOperationTest extends TestCase {
ModifyAttributesRollbackOperation rollbackOperation = (ModifyAttributesRollbackOperation) operation;
assertSame(expectedName, rollbackOperation.getDn());
assertSame(ldapOperationsMock, rollbackOperation.getLdapOperations());
assertEquals(1, rollbackOperation.getModificationItems().length);
assertSame(compensatingItem,
rollbackOperation.getModificationItems()[0]);
assertSame(incomingMods, rollbackOperation.getActualModifications());
assertEquals(1, rollbackOperation.getCompensatingModifications().length);
assertSame(compensatingItem, rollbackOperation
.getCompensatingModifications()[0]);
}
public void testGetCompensatingModificationItem_RemoveFullExistingAttribute()

View File

@@ -32,14 +32,56 @@ public class ModifyAttributesRollbackOperationTest extends TestCase {
ldapOperationsControl.verify();
}
public void testRollback() {
ModificationItem[] expectedItems = new ModificationItem[0];
public void testPerformOperation() {
ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
ModificationItem[] expectedActualItems = new ModificationItem[0];
Name expectedDn = new DistinguishedName("cn=john doe");
ModifyAttributesRollbackOperation tested = new ModifyAttributesRollbackOperation(
ldapOperationsMock, expectedDn, expectedItems);
ldapOperationsMock, expectedDn, expectedActualItems,
expectedCompensatingItems);
ldapOperationsMock.modifyAttributes(expectedDn, expectedItems);
ldapOperationsMock.modifyAttributes(expectedDn, expectedActualItems);
replay();
// Perform test
tested.performOperation();
verify();
}
public void testCommit() {
ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
ModificationItem[] expectedActualItems = new ModificationItem[0];
Name expectedDn = new DistinguishedName("cn=john doe");
ModifyAttributesRollbackOperation tested = new ModifyAttributesRollbackOperation(
ldapOperationsMock, expectedDn, expectedActualItems,
expectedCompensatingItems);
// No operation here
replay();
// Perform test
tested.commit();
verify();
}
public void testRollback() {
ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
ModificationItem[] expectedActualItems = new ModificationItem[0];
Name expectedDn = new DistinguishedName("cn=john doe");
ModifyAttributesRollbackOperation tested = new ModifyAttributesRollbackOperation(
ldapOperationsMock, expectedDn, expectedActualItems,
expectedCompensatingItems);
ldapOperationsMock.modifyAttributes(expectedDn,
expectedCompensatingItems);
replay();
// Perform test

View File

@@ -1,9 +1,11 @@
package org.springframework.ldap.support.transaction;
import javax.naming.Name;
import javax.naming.directory.BasicAttributes;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapOperations;
@@ -31,24 +33,36 @@ public class RebindRecordingOperationTest extends TestCase {
}
public void testRecordOperation() {
final DistinguishedName expectedDn = new DistinguishedName(
"cn=john doe");
final DistinguishedName expectedTempDn = new DistinguishedName(
"cn=john doe");
RebindRecordingOperation tested = new RebindRecordingOperation(
ldapOperationsMock);
DistinguishedName expectedDn = new DistinguishedName("cn=john doe");
ldapOperationsMock) {
Name getTemporaryName(Name originalName) {
assertSame(expectedDn, originalName);
return expectedTempDn;
}
};
DirContextAdapter expectedValue = new DirContextAdapter();
ldapOperationsControl.expectAndReturn(ldapOperationsMock
.lookup(expectedDn), expectedValue);
ldapOperationsMock.rename(expectedDn, expectedTempDn);
replay();
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
// perform test
CompensatingTransactionRollbackOperation result = tested
.recordOperation(new Object[] { expectedDn });
.recordOperation(new Object[] { expectedDn, expectedObject,
expectedAttributes });
verify();
assertTrue(result instanceof RebindRollbackOperation);
RebindRollbackOperation rollbackOperation = (RebindRollbackOperation) result;
assertSame(ldapOperationsMock, rollbackOperation.getLdapOperations());
assertSame(expectedValue, rollbackOperation.getDirContextOperations());
assertSame(expectedDn, rollbackOperation.getOriginalDn());
assertSame(expectedTempDn, rollbackOperation.getTemporaryDn());
assertSame(expectedObject, rollbackOperation.getOriginalObject());
assertSame(expectedAttributes, rollbackOperation
.getOriginalAttributes());
}
}

View File

@@ -1,9 +1,10 @@
package org.springframework.ldap.support.transaction;
import javax.naming.directory.BasicAttributes;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapOperations;
@@ -13,46 +14,76 @@ public class RebindRollbackOperationTest extends TestCase {
private LdapOperations ldapOperationsMock;
private MockControl dirContextOperationsControl;
private DirContextOperations dirContextOperationsMock;
protected void setUp() throws Exception {
ldapOperationsControl = MockControl.createControl(LdapOperations.class);
ldapOperationsMock = (LdapOperations) ldapOperationsControl.getMock();
dirContextOperationsControl = MockControl
.createControl(DirContextOperations.class);
dirContextOperationsMock = (DirContextOperations) dirContextOperationsControl
.getMock();
}
protected void tearDown() throws Exception {
ldapOperationsControl = null;
ldapOperationsMock = null;
dirContextOperationsControl = null;
dirContextOperationsMock = null;
}
protected void replay() {
ldapOperationsControl.replay();
dirContextOperationsControl.replay();
}
protected void verify() {
ldapOperationsControl.verify();
dirContextOperationsControl.verify();
}
public void testPerformOperation() {
DistinguishedName expectedOriginalDn = new DistinguishedName(
"cn=john doe");
DistinguishedName expectedTempDn = new DistinguishedName(
"cn=john doe_temp");
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
RebindRollbackOperation tested = new RebindRollbackOperation(
ldapOperationsMock, expectedOriginalDn, expectedTempDn,
expectedObject, expectedAttributes);
ldapOperationsMock.bind(expectedOriginalDn, expectedObject,
expectedAttributes);
replay();
// perform test
tested.performOperation();
verify();
}
public void testCommit() {
DistinguishedName expectedOriginalDn = new DistinguishedName(
"cn=john doe");
DistinguishedName expectedTempDn = new DistinguishedName(
"cn=john doe_temp");
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
RebindRollbackOperation tested = new RebindRollbackOperation(
ldapOperationsMock, expectedOriginalDn, expectedTempDn,
expectedObject, expectedAttributes);
ldapOperationsMock.unbind(expectedTempDn);
replay();
// perform test
tested.commit();
verify();
}
public void testRollback() {
DistinguishedName expectedOriginalDn = new DistinguishedName(
"cn=john doe");
DistinguishedName expectedTempDn = new DistinguishedName(
"cn=john doe_temp");
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
RebindRollbackOperation tested = new RebindRollbackOperation(
ldapOperationsMock, dirContextOperationsMock);
ldapOperationsMock, expectedOriginalDn, expectedTempDn,
expectedObject, expectedAttributes);
DistinguishedName expectedName = new DistinguishedName("cn=john doe");
dirContextOperationsControl.expectAndReturn(dirContextOperationsMock
.getDn(), expectedName);
ldapOperationsMock.rebind(expectedName, dirContextOperationsMock, null);
ldapOperationsMock.unbind(expectedOriginalDn);
ldapOperationsMock.rename(expectedTempDn, expectedOriginalDn);
replay();
// perform test

View File

@@ -42,8 +42,8 @@ public class RenameRecordingOperationTest extends TestCase {
assertTrue(operation instanceof RenameRollbackOperation);
RenameRollbackOperation rollbackOperation = (RenameRollbackOperation) operation;
assertSame(ldapOperationsMock, rollbackOperation.getLdapOperations());
assertEquals("ou=newou", rollbackOperation.getFromDn().toString());
assertEquals("ou=someou", rollbackOperation.getToDn().toString());
assertEquals("ou=newou", rollbackOperation.getNewDn().toString());
assertEquals("ou=someou", rollbackOperation.getOriginalDn().toString());
}
}

View File

@@ -29,11 +29,39 @@ public class RenameRollbackOperationTest extends TestCase {
ldapOperationsControl.verify();
}
public void testPerformOperation() {
DistinguishedName expectedNewName = new DistinguishedName("ou=newOu");
DistinguishedName expectedOldName = new DistinguishedName("ou=someou");
RenameRollbackOperation tested = new RenameRollbackOperation(
ldapOperationsMock, expectedOldName, expectedNewName);
ldapOperationsMock.rename(expectedOldName, expectedNewName);
replay();
// Perform test.
tested.performOperation();
verify();
}
public void testCommit() {
DistinguishedName expectedNewName = new DistinguishedName("ou=newOu");
DistinguishedName expectedOldName = new DistinguishedName("ou=someou");
RenameRollbackOperation tested = new RenameRollbackOperation(
ldapOperationsMock, expectedOldName, expectedNewName);
// Nothing to do for this operation.
replay();
// Perform test.
tested.commit();
verify();
}
public void testRollback() {
DistinguishedName expectedNewName = new DistinguishedName("ou=newOu");
DistinguishedName expectedOldName = new DistinguishedName("ou=someou");
RenameRollbackOperation tested = new RenameRollbackOperation(
ldapOperationsMock, expectedNewName, expectedOldName);
ldapOperationsMock, expectedOldName, expectedNewName);
ldapOperationsMock.rename(expectedNewName, expectedOldName);

View File

@@ -1,5 +1,7 @@
package org.springframework.ldap.support.transaction;
import javax.naming.Name;
import org.easymock.MockControl;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
@@ -30,14 +32,44 @@ public class UnbindRecordingOperationTest extends TestCase {
ldapOperationsControl.verify();
}
public void testRecordOperation() {
public void testGetTemporaryDN() {
DistinguishedName expectedOriginalName = new DistinguishedName(
"cn=john doe, ou=somecompany, c=SE");
UnbindRecordingOperation tested = new UnbindRecordingOperation(
ldapOperationsMock);
DistinguishedName expectedDn = new DistinguishedName("cn=john doe");
DirContextAdapter expectedContext = new DirContextAdapter();
ldapOperationsControl.expectAndReturn(ldapOperationsMock
.lookup(expectedDn), expectedContext);
Name result = tested.getTemporaryName(expectedOriginalName);
assertEquals("cn=john doe_temp, ou=somecompany, c=SE", result
.toString());
assertNotSame(expectedOriginalName, result);
}
public void testGetTemporaryDN_MultivalueDN() {
DistinguishedName expectedOriginalName = new DistinguishedName(
"cn=john doe+sn=doe, ou=somecompany, c=SE");
UnbindRecordingOperation tested = new UnbindRecordingOperation(
ldapOperationsMock);
Name result = tested.getTemporaryName(expectedOriginalName);
assertEquals("cn=john doe_temp+sn=doe, ou=somecompany, c=SE", result
.toString());
}
public void testRecordOperation() {
final DistinguishedName expectedTempName = new DistinguishedName(
"cn=john doe_temp");
final DistinguishedName expectedDn = new DistinguishedName(
"cn=john doe");
UnbindRecordingOperation tested = new UnbindRecordingOperation(
ldapOperationsMock) {
Name getTemporaryName(Name originalName) {
assertSame(expectedDn, originalName);
return expectedTempName;
}
};
ldapOperationsMock.rename(expectedDn, expectedTempName);
replay();
// Perform test
CompensatingTransactionRollbackOperation operation = tested
@@ -48,7 +80,8 @@ public class UnbindRecordingOperationTest extends TestCase {
assertTrue(operation instanceof BindRollbackOperation);
BindRollbackOperation rollbackOperation = (BindRollbackOperation) operation;
assertSame(ldapOperationsMock, rollbackOperation.getLdapOperations());
assertSame(expectedContext, rollbackOperation.getDirContextOperations());
assertSame(expectedDn, rollbackOperation.getOriginalDn());
assertSame(expectedTempName, rollbackOperation.getTemporaryDn());
}
}

View File

@@ -1,5 +1,7 @@
package org.springframework.ldap.support.transaction;
import javax.naming.directory.BasicAttributes;
import junit.framework.TestCase;
import org.easymock.MockControl;
@@ -29,14 +31,47 @@ public class UnbindRollbackOperationTest extends TestCase {
ldapOperationsControl.verify();
}
public void testPerformOperation() {
DistinguishedName expectedDn = new DistinguishedName("cn=john doe");
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
UnbindRollbackOperation tested = new UnbindRollbackOperation(
ldapOperationsMock, expectedDn, expectedObject,
expectedAttributes);
ldapOperationsMock.bind(expectedDn, expectedObject, expectedAttributes);
replay();
// perform teste
tested.performOperation();
verify();
}
public void testCommit() {
DistinguishedName expectedDn = new DistinguishedName("cn=john doe");
Object expectedObject = new Object();
BasicAttributes expectedAttributes = new BasicAttributes();
UnbindRollbackOperation tested = new UnbindRollbackOperation(
ldapOperationsMock, expectedDn, expectedObject,
expectedAttributes);
// Nothing to do here.
replay();
// perform teste
tested.commit();
verify();
}
public void testRollback() {
DistinguishedName expectedDn = new DistinguishedName("cn=john doe");
UnbindRollbackOperation tested = new UnbindRollbackOperation(ldapOperationsMock, expectedDn);
UnbindRollbackOperation tested = new UnbindRollbackOperation(
ldapOperationsMock, expectedDn, null, null);
ldapOperationsMock.unbind(expectedDn);
replay();
//perform teste
// perform teste
tested.rollback();
verify();
}