From 8099b2e31237cb0510d39a231426fa1dcef5a579 Mon Sep 17 00:00:00 2001 From: Mattias Arthursson Date: Sat, 13 Jan 2007 13:10:58 +0000 Subject: [PATCH] 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. --- sandbox/.classpath | 105 +++++++++--------- .../ldapTemplateTransactionTestContext.xml | 48 +++++++- ...urceTransactionManagerIntegrationTest.java | 4 +- .../support/transaction/DummyDaoImpl.java | 70 +++++++++++- .../support/transaction/DummyServiceImpl.java | 4 +- .../transaction/BindRecordingOperation.java | 17 ++- .../transaction/BindRollbackOperation.java | 56 ++++++++-- .../CompensatingTransactionDataManager.java | 6 + ...pensatingTransactionRollbackOperation.java | 4 + .../ContextSourceTransactionManager.java | 5 +- ...ultCompensatingTransactionDataManager.java | 9 ++ .../ldap/support/transaction/LdapUtils.java | 8 +- .../ModifyAttributesRecordingOperation.java | 2 +- .../ModifyAttributesRollbackOperation.java | 45 ++++++-- .../transaction/NullRollbackOperation.java | 8 ++ .../transaction/RebindRecordingOperation.java | 47 ++++++-- .../transaction/RebindRollbackOperation.java | 83 ++++++++++---- .../transaction/RenameRecordingOperation.java | 2 +- .../transaction/RenameRollbackOperation.java | 59 +++++++--- .../transaction/UnbindRecordingOperation.java | 24 +++- .../transaction/UnbindRollbackOperation.java | 43 ++++++- .../BindRecordingOperationTest.java | 15 ++- .../BindRollbackOperationTest.java | 52 +++++---- ...odifyAttributesRecordingOperationTest.java | 7 +- ...ModifyAttributesRollbackOperationTest.java | 50 ++++++++- .../RebindRecordingOperationTest.java | 32 ++++-- .../RebindRollbackOperationTest.java | 71 ++++++++---- .../RenameRecordingOperationTest.java | 4 +- .../RenameRollbackOperationTest.java | 30 ++++- .../UnbindRecordingOperationTest.java | 45 +++++++- .../UnbindRollbackOperationTest.java | 43 ++++++- 31 files changed, 784 insertions(+), 214 deletions(-) diff --git a/sandbox/.classpath b/sandbox/.classpath index 3b6c6a05..38a9c6a8 100644 --- a/sandbox/.classpath +++ b/sandbox/.classpath @@ -1,52 +1,53 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sandbox/src/itest/java/conf/ldapTemplateTransactionTestContext.xml b/sandbox/src/itest/java/conf/ldapTemplateTransactionTestContext.xml index 59f9410b..2ea9108f 100644 --- a/sandbox/src/itest/java/conf/ldapTemplateTransactionTestContext.xml +++ b/sandbox/src/itest/java/conf/ldapTemplateTransactionTestContext.xml @@ -1,7 +1,7 @@ - @@ -21,6 +21,15 @@ + @@ -32,19 +41,36 @@ - + + + --> + + + + - + @@ -52,4 +78,16 @@ + diff --git a/sandbox/src/itest/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManagerIntegrationTest.java b/sandbox/src/itest/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManagerIntegrationTest.java index d68d79ed..f731c85e 100644 --- a/sandbox/src/itest/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManagerIntegrationTest.java +++ b/sandbox/src/itest/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManagerIntegrationTest.java @@ -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; } diff --git a/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyDaoImpl.java b/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyDaoImpl.java index ae340035..9d1d1396 100644 --- a/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyDaoImpl.java +++ b/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyDaoImpl.java @@ -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."); diff --git a/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyServiceImpl.java b/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyServiceImpl.java index 86f4e638..eb648292 100644 --- a/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyServiceImpl.java +++ b/sandbox/src/itest/java/org/springframework/ldap/support/transaction/DummyServiceImpl.java @@ -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; } } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRecordingOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRecordingOperation.java index 1a59f745..ff3bb7e0 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRecordingOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRecordingOperation.java @@ -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); } /** diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRollbackOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRollbackOperation.java index 3f38f93b..a18d4d30 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRollbackOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/BindRollbackOperation.java @@ -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; } } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionDataManager.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionDataManager.java index 4bd8a9e9..b00c68eb 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionDataManager.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionDataManager.java @@ -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(); } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionRollbackOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionRollbackOperation.java index 1c28e8da..a5a659bf 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionRollbackOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/CompensatingTransactionRollbackOperation.java @@ -32,4 +32,8 @@ public interface CompensatingTransactionRollbackOperation { * {@link CompensatingTransactionRecordingOperation}). */ public void rollback(); + + public void commit(); + + public void performOperation(); } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManager.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManager.java index c578a4f4..6514ffc4 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManager.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/ContextSourceTransactionManager.java @@ -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(); + } /* diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/DefaultCompensatingTransactionDataManager.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/DefaultCompensatingTransactionDataManager.java index 9017b90a..0064cd84 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/DefaultCompensatingTransactionDataManager.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/DefaultCompensatingTransactionDataManager.java @@ -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(); + } + } + } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/LdapUtils.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/LdapUtils.java index f92baea7..e26d91e4 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/LdapUtils.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/LdapUtils.java @@ -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; diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperation.java index f489bea9..747371b6 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperation.java @@ -82,7 +82,7 @@ public class ModifyAttributesRecordingOperation implements } return new ModifyAttributesRollbackOperation(ldapOperations, dn, - rollbackItems); + incomingModifications, rollbackItems); } /** diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperation.java index cb8d3d1c..c1e6c415 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperation.java @@ -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; } } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/NullRollbackOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/NullRollbackOperation.java index 5ca503a2..1ba9dc7a 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/NullRollbackOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/NullRollbackOperation.java @@ -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"); + } + } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRecordingOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRecordingOperation.java index cb308638..7c782082 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRecordingOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRecordingOperation.java @@ -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; } /** diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRollbackOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRollbackOperation.java index 83c61d4d..5947c26f 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRollbackOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RebindRollbackOperation.java @@ -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; + } + } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRecordingOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRecordingOperation.java index 6454cae8..e8d8b1dc 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRecordingOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRecordingOperation.java @@ -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() { diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRollbackOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRollbackOperation.java index b6aa5bb7..4761b1b4 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRollbackOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/RenameRollbackOperation.java @@ -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; } } diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRecordingOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRecordingOperation.java index e438b8f7..3d53a805 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRecordingOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRecordingOperation.java @@ -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() { diff --git a/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRollbackOperation.java b/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRollbackOperation.java index 21a501ca..a54832f8 100644 --- a/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRollbackOperation.java +++ b/sandbox/src/main/java/org/springframework/ldap/support/transaction/UnbindRollbackOperation.java @@ -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; + } + } diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRecordingOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRecordingOperationTest.java index a796b1ba..8117b312 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRecordingOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRecordingOperationTest.java @@ -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; diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRollbackOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRollbackOperationTest.java index aaefe3f9..76715139 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRollbackOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/BindRollbackOperationTest.java @@ -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(); } - } diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperationTest.java index f28d8668..6c6ce237 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRecordingOperationTest.java @@ -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() diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperationTest.java index 0c22eb82..73446f6b 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/ModifyAttributesRollbackOperationTest.java @@ -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 diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRecordingOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRecordingOperationTest.java index 8f5abf47..a59383fb 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRecordingOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRecordingOperationTest.java @@ -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()); } - } diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRollbackOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRollbackOperationTest.java index 199268d3..d64a173f 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRollbackOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RebindRollbackOperationTest.java @@ -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 diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRecordingOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRecordingOperationTest.java index 93cf75bf..8bf8c38f 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRecordingOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRecordingOperationTest.java @@ -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()); } } diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRollbackOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRollbackOperationTest.java index afac7451..71bfcc0d 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRollbackOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/RenameRollbackOperationTest.java @@ -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); diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRecordingOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRecordingOperationTest.java index 7388bab9..61659879 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRecordingOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRecordingOperationTest.java @@ -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()); } } diff --git a/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRollbackOperationTest.java b/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRollbackOperationTest.java index fee98bf7..f3106510 100644 --- a/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRollbackOperationTest.java +++ b/sandbox/src/test/java/org/springframework/ldap/support/transaction/UnbindRollbackOperationTest.java @@ -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(); }