diff --git a/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java b/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java index 0a069c40..16e8b3f3 100644 --- a/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java +++ b/core/src/main/java/org/springframework/ldap/config/TransactionManagerParser.java @@ -22,6 +22,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager; +import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager; import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager; import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy; import org.springframework.ldap.transaction.compensating.support.DifferentSubtreeTempEntryRenamingStrategy; @@ -58,7 +60,18 @@ public class TransactionManagerParser implements BeanDefinitionParser { ATT_DATA_SOURCE_REF, ATT_SESSION_FACTORY_REF)); } - BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceTransactionManager.class); + BeanDefinitionBuilder builder; + if(StringUtils.hasText(dataSourceRef)) { + builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndDataSourceTransactionManager.class); + builder.addPropertyReference("dataSource", dataSourceRef); + } else if(StringUtils.hasText(sessionFactoryRef)) { + builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndHibernateTransactionManager.class); + builder.addPropertyReference("sessionFactory", sessionFactoryRef); + } else { + // Standard transaction manager + builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceTransactionManager.class); + } + builder.addPropertyReference("contextSource", contextSourceRef); Element defaultStrategyChild = DomUtils.getChildElementByTagName(element, Elements.DEFAULT_RENAMING_STRATEGY); diff --git a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java index c6a32086..efd09336 100644 --- a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java +++ b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java @@ -29,6 +29,7 @@ import org.springframework.ldap.pool.factory.PoolingContextSource; import org.springframework.ldap.pool.validation.DefaultDirContextValidator; import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy; +import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager; import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager; import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy; import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy; @@ -177,6 +178,14 @@ public class LdapTemplateNamespaceHandlerTest { assertEquals("_temp", getInternalState(renamingStrategy, "tempSuffix")); } + @Test + public void verifyParseTransactionWithDataSource() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-transactional-datasource.xml"); + PlatformTransactionManager transactionManager = ctx.getBean(PlatformTransactionManager.class); + + assertTrue(transactionManager instanceof ContextSourceAndDataSourceTransactionManager); + } + @Test public void verifyParseTransactionsWithDefaultStrategyAndSuffix() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-transactional-defaults-with-suffix.xml"); diff --git a/core/src/test/java/org/springframework/ldap/config/MockFactoryBean.java b/core/src/test/java/org/springframework/ldap/config/MockFactoryBean.java new file mode 100644 index 00000000..9f27fd86 --- /dev/null +++ b/core/src/test/java/org/springframework/ldap/config/MockFactoryBean.java @@ -0,0 +1,42 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ldap.config; + +import org.springframework.beans.factory.config.AbstractFactoryBean; + +import static org.mockito.Mockito.mock; + +/** + * @author Mattias Hellborg Arthursson + */ +public class MockFactoryBean extends AbstractFactoryBean { + private final Class clazz; + + public MockFactoryBean(Class clazz) { + this.clazz = clazz; + } + + @Override + public Class getObjectType() { + return clazz; + } + + @Override + protected Object createInstance() throws Exception { + return mock(clazz); + } +} diff --git a/core/src/test/resources/ldap-namespace-config-transactional-datasource.xml b/core/src/test/resources/ldap-namespace-config-transactional-datasource.xml new file mode 100644 index 00000000..444a5278 --- /dev/null +++ b/core/src/test/resources/ldap-namespace-config-transactional-datasource.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerNamespaceITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerNamespaceITest.java new file mode 100644 index 00000000..d55fecbb --- /dev/null +++ b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/ContextSourceAndDataSourceTransactionManagerNamespaceITest.java @@ -0,0 +1,354 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ldap.itest.manager; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.ldap.NameNotFoundException; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest; +import org.springframework.ldap.itest.transaction.compensating.manager.DummyDao; +import org.springframework.ldap.itest.transaction.compensating.manager.DummyException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +/** + * Integration tests for {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager} + * with namespace configuration. + * + * @author Mattias Hellborg Arthursson + */ +@ContextConfiguration(locations = {"/conf/ldapAndJdbcTransactionNamespaceTestContext.xml"}) +public class ContextSourceAndDataSourceTransactionManagerNamespaceITest extends AbstractLdapTemplateIntegrationTest { + + private static Log log = LogFactory.getLog(ContextSourceAndDataSourceTransactionManagerNamespaceITest.class); + + @Autowired + @Qualifier("dummyDao") + private DummyDao dummyDao; + + @Autowired + private LdapTemplate ldapTemplate; + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Before + public void prepareTestedInstance() throws Exception { + if (TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.clearSynchronization(); + } + + jdbcTemplate.execute("drop table PERSON if exists"); + jdbcTemplate.execute("create table PERSON(fullname VARCHAR, lastname VARCHAR, description VARCHAR)"); + jdbcTemplate.update("insert into PERSON values(?, ?, ?)", new Object[] { "Some Person", "Person", + "Sweden, Company1, Some Person" }); + } + + @After + public void cleanup() throws Exception { + jdbcTemplate.execute("drop table PERSON if exists"); + } + + @Test + public void testCreateWithException() { + try { + dummyDao.createWithException("Sweden", "company1", "some testperson", "testperson", "some description"); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + // Verify that no entry was created + try { + ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + + try { + jdbcTemplate.queryForObject("select * from PERSON where fullname='some testperson'", new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + return null; + } + }); + fail("EmptyResultDataAccessException expected"); + } + catch (EmptyResultDataAccessException expected) { + assertTrue(true); + } + } + + @Test + public void testCreate() { + dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); + + log.debug("Verifying result"); + Object ldapResult = ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); + Object dbResult = jdbcTemplate.queryForObject("select * from PERSON where fullname='some testperson'", + new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + return new Object(); + } + }); + assertNotNull(ldapResult); + assertNotNull(dbResult); + + ldapTemplate.unbind("cn=some testperson, ou=company1, c=Sweden"); + } + + @Test + public void testUpdateWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description"); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); + return new Object(); + } + }); + + Object jdbcResult = jdbcTemplate.queryForObject("select * from PERSON where fullname=?", + new Object[] { "Some Person" }, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + assertEquals("Person", rs.getString("lastname")); + assertEquals("Sweden, Company1, Some Person", rs.getString("description")); + return new Object(); + } + }); + + assertNotNull(ldapResult); + assertNotNull(jdbcResult); + } + + @Test + public void testUpdate() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + dummyDao.update(dn, "Some Person", "Updated Person", "Updated description"); + + log.debug("Verifying result"); + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated Person", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get("description").get()); + return new Object(); + } + }); + + Object jdbcResult = jdbcTemplate.queryForObject("select * from PERSON where fullname=?", + new Object[] { "Some Person" }, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + assertEquals("Updated Person", rs.getString("lastname")); + assertEquals("Updated description", rs.getString("description")); + return new Object(); + } + }); + + assertNotNull(ldapResult); + assertNotNull(jdbcResult); + dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person"); + } + + @Test + public void testUpdateAndRenameWithException() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + try { + // Perform test + dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + // Verify that entry was not moved. + try { + ldapTemplate.lookup(newDn); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + + // Verify that original entry was not updated. + Object object = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Sweden, Company1, Some Person2", attributes.get("description").get()); + return new Object(); + } + }); + assertNotNull(object); + } + + @Test + public void testUpdateAndRename() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + // Perform test + dummyDao.updateAndRename(dn, newDn, "Updated description"); + + // Verify that entry was moved and updated. + Object object = ldapTemplate.lookup(newDn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated description", attributes.get("description").get()); + return new Object(); + } + }); + + assertNotNull(object); + dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2"); + } + + @Test + public void testModifyAttributesWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + // Perform test + dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + // Verify result - check that the operation was properly rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); + return new Object(); + } + }); + + assertNotNull(result); + } + + @Test + public void testModifyAttributes() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description"); + + // Verify result - check that the operation was not rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated lastname", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get("description").get()); + return new Object(); + } + }); + + assertNotNull(result); + dummyDao.update(dn, "Some Person", "Person", "Sweden, Company1, Some Person"); + } + + @Test + public void testUnbindWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + // Perform test + dummyDao.unbindWithException(dn, "Some Person"); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + // Verify result - check that the operation was properly rolled back + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + // Just verify that the entry still exists. + return new Object(); + } + }); + + Object jdbcResult = jdbcTemplate.queryForObject("select * from PERSON where fullname=?", + new Object[] { "Some Person" }, new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + // Just verify that the entry still exists. + return new Object(); + } + }); + + assertNotNull(ldapResult); + assertNotNull(jdbcResult); + } + + @Test + public void testUnbind() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + dummyDao.unbind(dn, "Some Person"); + + try { + // Verify result - check that the operation was not rolled back + ldapTemplate.lookup(dn); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + + try { + jdbcTemplate.queryForObject("select * from PERSON where fullname=?", new Object[] { "Some Person" }, + new RowMapper() { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + return null; + } + }); + fail("EmptyResultDataAccessException expected"); + } + catch (EmptyResultDataAccessException expected) { + assertTrue(true); + } + } +} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerNamespaceITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerNamespaceITest.java new file mode 100755 index 00000000..7c57a0f8 --- /dev/null +++ b/test/integration-tests/src/test/java/org/springframework/ldap/itest/manager/hibernate/ContextSourceAndHibernateTransactionManagerNamespaceITest.java @@ -0,0 +1,373 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.itest.manager.hibernate; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.ldap.NameNotFoundException; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.itest.AbstractLdapTemplateIntegrationTest; +import org.springframework.ldap.itest.transaction.compensating.manager.DummyException; +import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPerson; +import org.springframework.ldap.itest.transaction.compensating.manager.hibernate.OrgPersonDao; +import org.springframework.orm.hibernate3.HibernateTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +/** + * Integration tests for {@link org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager} + * with namespace configuration. + * + * @author Hans Westerbeek + */ +@ContextConfiguration(locations = {"/conf/ldapAndHibernateTransactionNamespaceTestContext.xml"}) +public class ContextSourceAndHibernateTransactionManagerNamespaceITest extends AbstractLdapTemplateIntegrationTest { + + private static Log log = LogFactory.getLog(ContextSourceAndHibernateTransactionManagerNamespaceITest.class); + + @Autowired + @Qualifier("dummyDao") + private OrgPersonDao dummyDao; + + @Autowired + private LdapTemplate ldapTemplate; + + @Autowired + private HibernateTemplate hibernateTemplate; + + @Autowired + private SessionFactory sessionFactory; + + @Before + public void prepareTest() throws Exception { + if (TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.clearSynchronization(); + } + + OrgPerson person = new OrgPerson(); + person.setId(new Integer(1)); + person.setLastname("Person"); + person.setFullname("Some Person"); + person.setDescription("Sweden, Company1, Some Person"); + person.setCountry("Sweden"); + person.setCompany("Company1"); + // "Some Person", "Person", "Sweden, Company1, Some Person" + // avoid the transaction manager we have configured, do it manually + Session session = this.sessionFactory.openSession(); + Transaction tx = session.beginTransaction(); + session.saveOrUpdate(person); + tx.commit(); + session.close(); + + } + + @After + public void cleanup() throws Exception { + // probably the wrong idea, this will use the thing i am trying to + // test.. + + Session session = this.sessionFactory.openSession(); + Transaction tx = session.beginTransaction(); + Query query = session.createQuery("delete from OrgPerson"); + query.executeUpdate(); + tx.commit(); + session.close(); + } + + @Test + public void testCreateWithException() { + OrgPerson person = new OrgPerson(); + + person.setId(new Integer(2)); + person.setDescription("some description"); + person.setFullname("Some testperson"); + person.setLastname("testperson"); + person.setCountry("Sweden"); + person.setCompany("company1"); + + try { + dummyDao.createWithException(person); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + // Verify that no entry was created in ldap or hibernate db + try { + ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + + List result = hibernateTemplate.findByNamedParam("from OrgPerson person where person.lastname = :lastname", + "lastname", person.getLastname()); + assertTrue(result.size() == 0); + + } + + @Test + public void testCreate() { + OrgPerson person = new OrgPerson(); + + person.setId(new Integer(2)); + person.setDescription("some description"); + person.setFullname("Some testperson"); + person.setLastname("testperson"); + person.setCountry("Sweden"); + person.setCompany("company1"); + // dummyDao.create("Sweden", "company1", "some testperson", + // "testperson", "some description"); + + this.dummyDao.create(person); + person = null; + log.debug("Verifying result"); + Object ldapResult = ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); + OrgPerson fromDb = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, new Integer(2)); + assertNotNull(ldapResult); + assertNotNull(fromDb); + } + + @Test + public void testUpdateWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + OrgPerson originalPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); + originalPerson.setLastname("fooo"); + try { + dummyDao.updateWithException(originalPerson); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + log.debug("Verifying result"); + + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertNotNull("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); + return new Object(); + } + }); + + OrgPerson notUpdatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); + assertEquals("Person", notUpdatedPerson.getLastname()); + assertEquals("Sweden, Company1, Some Person", notUpdatedPerson.getDescription()); + + assertNotNull(ldapResult); + // no need to assert if notUpdatedPerson exists + } + + @Test + public void testUpdate() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); + person.setLastname("Updated Person"); + person.setDescription("Updated description"); + + dummyDao.update(person); + + log.debug("Verifying result"); + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated Person", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get("description").get()); + return new Object(); + } + }); + + OrgPerson updatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); + assertEquals("Updated Person", updatedPerson.getLastname()); + assertEquals("Updated description", updatedPerson.getDescription()); + assertNotNull(ldapResult); + } + + @Test + public void testUpdateAndRenameWithException() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); + person.setLastname("Updated Person"); + person.setDescription("Updated description"); + + try { + // Perform test + dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + // Verify that entry was not moved. + try { + ldapTemplate.lookup(newDn); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + + // Verify that original entry was not updated. + Object object = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Sweden, Company1, Some Person2", attributes.get("description").get()); + return new Object(); + } + }); + assertNotNull(object); + } + + @Test + public void testUpdateAndRename() { + String dn = "cn=Some Person2,ou=company1,c=Sweden"; + String newDn = "cn=Some Person2,ou=company2,c=Sweden"; + // Perform test + dummyDao.updateAndRename(dn, newDn, "Updated description"); + + // Verify that entry was moved and updated. + Object object = ldapTemplate.lookup(newDn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated description", attributes.get("description").get()); + return new Object(); + } + }); + + assertNotNull(object); + } + + @Test + public void testModifyAttributesWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + try { + // Perform test + dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + // Verify result - check that the operation was properly rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Person", attributes.get("sn").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); + return new Object(); + } + }); + + assertNotNull(result); + } + + @Test + public void testModifyAttributes() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description"); + + // Verify result - check that the operation was not rolled back + Object result = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated lastname", attributes.get("sn").get()); + assertEquals("Updated description", attributes.get("description").get()); + return new Object(); + } + }); + + assertNotNull(result); + } + + @Test + public void testUnbindWithException() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); + + try { + // Perform test + dummyDao.unbindWithException(person); + fail("DummyException expected"); + } + catch (DummyException expected) { + assertTrue(true); + } + + person = null; + // Verify result - check that the operation was properly rolled back + Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + // Just verify that the entry still exists. + return new Object(); + } + }); + + person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); // will + // throw + // exception + // of + // person + // does + // not + // exist + + assertNotNull(ldapResult); + } + + @Test + public void testUnbind() { + String dn = "cn=Some Person,ou=company1,c=Sweden"; + // Perform test + OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); + dummyDao.unbind(person); + + try { + // Verify result - check that the operation was not rolled back + ldapTemplate.lookup(dn); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + + person = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, new Integer(1)); + assertNull(person); + } +} diff --git a/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionNamespaceTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionNamespaceTestContext.xml new file mode 100755 index 00000000..48bc6f3f --- /dev/null +++ b/test/integration-tests/src/test/resources/conf/ldapAndHibernateTransactionNamespaceTestContext.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + conf/OrgPerson.hbm.xml + + + + + hibernate.dialect=org.hibernate.dialect.HSQLDialect + hibernate.hbm2ddl.auto=create + + + + + + + + + + + diff --git a/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionNamespaceTestContext.xml b/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionNamespaceTestContext.xml new file mode 100644 index 00000000..efd50198 --- /dev/null +++ b/test/integration-tests/src/test/resources/conf/ldapAndJdbcTransactionNamespaceTestContext.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +