From dd66a7cc1cd9a33c39ef238eada119790937c607 Mon Sep 17 00:00:00 2001 From: Mattias Arthursson Date: Mon, 9 Jun 2008 08:12:55 +0000 Subject: [PATCH] Changed integration tests to use JUnit 4 --- mvn-build/integration-tests/pom.xml | 6 + .../core/DummyDistinguishedNameConsumer.java | 0 .../core/support/DummyBaseLdapPathAware.java | 0 .../AbstractLdapTemplateIntegrationTest.java | 29 +- .../LdapTemplateAttributesMapperITest.java | 90 ++--- .../ldap/LdapTemplateBindUnbindITest.java | 145 ++++---- .../ldap/LdapTemplateContextExecutorTest.java | 47 +-- .../ldap/LdapTemplateContextMapperITest.java | 87 +++-- .../ldap/LdapTemplateListITest.java | 205 +++++------ .../ldap/LdapTemplateLookupITest.java | 107 +++--- .../ldap/LdapTemplateModifyITest.java | 335 +++++++++--------- .../ldap/LdapTemplateNoBaseSuffixITest.java | 110 +++--- ...LdapTemplateObjectBindIntegrationTest.java | 37 +- .../LdapTemplateRecursiveDeleteITest.java | 161 +++++---- .../ldap/LdapTemplateRenameITest.java | 112 +++--- .../ldap/LdapTemplateSearchResultITest.java | 79 +++-- .../ldap/control/SupportedControlsITest.java | 61 ++-- .../core/DistinguishedNameEditorITest.java | 22 +- .../ldap/core/DnParsePerformanceITest.java | 60 ++-- .../core/simple/SimpleLdapTemplateITest.java | 22 +- .../BaseLdapPathBeanPostprocessorITest.java | 18 +- .../core/support/LdapContextSourcelITest.java | 110 +++--- .../LdapContextSourcelMultiServerITest.java | 33 +- ...urceTransactionManagerIntegrationTest.java | 210 +++++------ ...urceTransactionManagerIntegrationTest.java | 141 ++++---- 25 files changed, 1095 insertions(+), 1132 deletions(-) rename mvn-build/integration-tests/src/{test => main}/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java (100%) rename mvn-build/integration-tests/src/{test => main}/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java (100%) diff --git a/mvn-build/integration-tests/pom.xml b/mvn-build/integration-tests/pom.xml index 90815a10..301f89cb 100644 --- a/mvn-build/integration-tests/pom.xml +++ b/mvn-build/integration-tests/pom.xml @@ -77,5 +77,11 @@ 1.8.0.7 test + + junit + junit + 4.4 + test + diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java similarity index 100% rename from mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java rename to mvn-build/integration-tests/src/main/java/org/springframework/ldap/core/DummyDistinguishedNameConsumer.java diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java b/mvn-build/integration-tests/src/main/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java similarity index 100% rename from mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java rename to mvn-build/integration-tests/src/main/java/org/springframework/ldap/core/support/DummyBaseLdapPathAware.java diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java index e09c0e94..9686838a 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java @@ -15,13 +15,28 @@ */ package org.springframework.ldap; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.AfterClass; +import org.springframework.ldap.test.LdapTestUtils; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; -public abstract class AbstractLdapTemplateIntegrationTest extends - AbstractDependencyInjectionSpringContextTests { +public abstract class AbstractLdapTemplateIntegrationTest extends AbstractJUnit4SpringContextTests { + private static final Log log = LogFactory.getLog(AbstractLdapTemplateIntegrationTest.class); - - protected void onSetUp() throws Exception { - super.onSetUp(); - } + @AfterClass + public static final void shutdownServer() { + try { + // This would be the ideal setup, to enable each test to run in + // isolation + // However, due to port timeout problems on linux it doesn't work - + // the port will not be released before the next test class will + // start. + // LdapTestUtils.destroyApacheDirectoryServer("uid=admin,ou=system", + // "credentials"); + } + catch (Exception e) { + log.error("Failed to shut down directory server"); + } + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java index 8acd316f..6146629e 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java @@ -16,6 +16,8 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; + import java.util.LinkedList; import java.util.List; @@ -23,61 +25,61 @@ import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attributes; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Tests the attributes mapper search method. * * @author Mattias Arthursson */ -public class LdapTemplateAttributesMapperITest extends - AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateAttributesMapperITest extends AbstractLdapTemplateIntegrationTest { + @Autowired + private LdapTemplate tested; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + protected String[] getConfigLocations() { + return new String[] { "/conf/ldapTemplateTestContext.xml" }; + } - public void testSearch_AttributeMapper() throws Exception { - AttributesMapper mapper = new PersonAttributesMapper(); - List result = tested.search("ou=company1,c=Sweden", - "(&(objectclass=person)(sn=Person2))", mapper); + @Test + public void testSearch_AttributeMapper() throws Exception { + AttributesMapper mapper = new PersonAttributesMapper(); + List result = tested.search("ou=company1,c=Sweden", "(&(objectclass=person)(sn=Person2))", mapper); - assertEquals(1, result.size()); - Person person = (Person) result.get(0); - assertEquals("Some Person2", person.getFullname()); - assertEquals("Person2", person.getLastname()); - assertEquals("Sweden, Company1, Some Person2", person.getDescription()); - } + assertEquals(1, result.size()); + Person person = (Person) result.get(0); + assertEquals("Some Person2", person.getFullname()); + assertEquals("Person2", person.getLastname()); + assertEquals("Sweden, Company1, Some Person2", person.getDescription()); + } - /** - * Demonstrates how to retrieve all values of a multi-value attribute. - * - * @see LdapTemplateContextMapperITest#testSearch_ContextMapper_MultiValue() - */ - public void testSearch_AttributesMapper_MultiValue() throws Exception { - AttributesMapper mapper = new AttributesMapper() { - public Object mapFromAttributes(Attributes attributes) throws NamingException { - LinkedList list = new LinkedList(); - NamingEnumeration enumeration = attributes.get("uniqueMember").getAll(); - while (enumeration.hasMoreElements()) { - String value = (String) enumeration.nextElement(); - list.add(value); - } - String[] members = (String[]) list.toArray(new String[0]); - return members; - } - }; - List result = tested.search("ou=groups", - "(objectclass=groupOfUniqueNames)", mapper); + /** + * Demonstrates how to retrieve all values of a multi-value attribute. + * + * @see LdapTemplateContextMapperITest#testSearch_ContextMapper_MultiValue() + */ + @Test + public void testSearch_AttributesMapper_MultiValue() throws Exception { + AttributesMapper mapper = new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + LinkedList list = new LinkedList(); + NamingEnumeration enumeration = attributes.get("uniqueMember").getAll(); + while (enumeration.hasMoreElements()) { + String value = (String) enumeration.nextElement(); + list.add(value); + } + String[] members = (String[]) list.toArray(new String[0]); + return members; + } + }; + List result = tested.search("ou=groups", "(objectclass=groupOfUniqueNames)", mapper); - assertEquals(2, result.size()); - assertEquals(1, ((String[]) result.get(0)).length); - assertEquals(5, ((String[]) result.get(1)).length); - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + assertEquals(2, result.size()); + assertEquals(1, ((String[]) result.get(0)).length); + assertEquals(5, ((String[]) result.get(1)).length); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java index 42297c48..f7baa288 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java @@ -16,13 +16,20 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Tests the bind and unbind methods of LdapTemplate. The test methods in this @@ -35,85 +42,81 @@ import org.springframework.ldap.core.LdapTemplate; * * @author Mattias Arthursson */ -public class LdapTemplateBindUnbindITest extends - AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateBindUnbindITest extends AbstractLdapTemplateIntegrationTest { + @Autowired + private LdapTemplate tested; - private static String DN = "cn=Some Person4,ou=company1,c=Sweden"; + private static String DN = "cn=Some Person4,ou=company1,c=Sweden"; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + @Test + public void testBindAndUnbind_Attributes_Plain() { + Attributes attributes = setupAttributes(); + tested.bind(DN, null, attributes); + verifyBoundCorrectData(); + tested.unbind(DN); + verifyCleanup(); + } - public void testBindAndUnbind_Attributes_Plain() { - Attributes attributes = setupAttributes(); - tested.bind(DN, null, attributes); - verifyBoundCorrectData(); - tested.unbind(DN); - verifyCleanup(); - } + @Test + public void testBindAndUnbind_Attributes_DIstinguishedName() { + Attributes attributes = setupAttributes(); + tested.bind(new DistinguishedName(DN), null, attributes); + verifyBoundCorrectData(); + tested.unbind(new DistinguishedName(DN)); + verifyCleanup(); + } - public void testBindAndUnbind_Attributes_DIstinguishedName() { - Attributes attributes = setupAttributes(); - tested.bind(new DistinguishedName(DN), null, attributes); - verifyBoundCorrectData(); - tested.unbind(new DistinguishedName(DN)); - verifyCleanup(); - } + @Test + public void testBindAndUnbind_DirContextAdapter_Plain() { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); - public void testBindAndUnbind_DirContextAdapter_Plain() { - DirContextAdapter adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "Some Person4"); - adapter.setAttributeValue("sn", "Person4"); + tested.bind(DN, adapter, null); + verifyBoundCorrectData(); + tested.unbind(DN); + verifyCleanup(); + } - tested.bind(DN, adapter, null); - verifyBoundCorrectData(); - tested.unbind(DN); - verifyCleanup(); - } + @Test + public void testBindAndUnbind_DirContextAdapter_DIstinguishedName() { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); - public void testBindAndUnbind_DirContextAdapter_DIstinguishedName() { - DirContextAdapter adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "Some Person4"); - adapter.setAttributeValue("sn", "Person4"); + tested.bind(new DistinguishedName(DN), adapter, null); + verifyBoundCorrectData(); + tested.unbind(new DistinguishedName(DN)); + verifyCleanup(); + } - tested.bind(new DistinguishedName(DN), adapter, null); - verifyBoundCorrectData(); - tested.unbind(new DistinguishedName(DN)); - verifyCleanup(); - } + private Attributes setupAttributes() { + Attributes attributes = new BasicAttributes(); + BasicAttribute ocattr = new BasicAttribute("objectclass"); + ocattr.add("top"); + ocattr.add("person"); + attributes.put(ocattr); + attributes.put("cn", "Some Person4"); + attributes.put("sn", "Person4"); + return attributes; + } - private Attributes setupAttributes() { - Attributes attributes = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute("objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attributes.put(ocattr); - attributes.put("cn", "Some Person4"); - attributes.put("sn", "Person4"); - return attributes; - } + private void verifyBoundCorrectData() { + DirContextAdapter result = (DirContextAdapter) tested.lookup(DN); + assertEquals("Some Person4", result.getStringAttribute("cn")); + assertEquals("Person4", result.getStringAttribute("sn")); + } - private void verifyBoundCorrectData() { - DirContextAdapter result = (DirContextAdapter) tested.lookup(DN); - assertEquals("Some Person4", result.getStringAttribute("cn")); - assertEquals("Person4", result.getStringAttribute("sn")); - } - - private void verifyCleanup() { - try { - tested.lookup(DN); - fail("NameNotFoundException expected"); - } catch (NameNotFoundException expected) { - assertTrue(true); - } - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + private void verifyCleanup() { + try { + tested.lookup(DN); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextExecutorTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextExecutorTest.java index 8561d09d..fab9cbcb 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextExecutorTest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextExecutorTest.java @@ -15,47 +15,38 @@ */ package org.springframework.ldap; +import static junit.framework.Assert.assertTrue; + import javax.naming.NamingException; import javax.naming.directory.DirContext; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.ContextExecutor; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Tests for LdapTemplate's context executor methods. * * @author Mattias Arthursson */ -public class LdapTemplateContextExecutorTest extends - AbstractLdapTemplateIntegrationTest { +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateContextExecutorTest extends AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; + @Autowired + private LdapTemplate tested; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + @Test + public void testLookupLink() { + ContextExecutor executor = new ContextExecutor() { + public Object executeWithContext(DirContext ctx) throws NamingException { + return ctx.lookupLink("cn=Some Person,ou=company1,c=Sweden"); + } + }; - protected void onSetUp() throws Exception { - super.onSetUp(); - } - - protected void onTearDown() throws Exception { - super.onTearDown(); - } - - public void testLookupLink() { - ContextExecutor executor = new ContextExecutor(){ - public Object executeWithContext(DirContext ctx) throws NamingException { - return ctx.lookupLink("cn=Some Person,ou=company1,c=Sweden"); - } - }; - - Object object = tested.executeReadOnly(executor); - assertTrue("Should be a DirContextAdapter", object instanceof DirContextAdapter); - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + Object object = tested.executeReadOnly(executor); + assertTrue("Should be a DirContextAdapter", object instanceof DirContextAdapter); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextMapperITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextMapperITest.java index d2ca77b2..0feba181 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextMapperITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateContextMapperITest.java @@ -16,11 +16,16 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; + import java.util.List; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Tests the ContextMapper search method. In its way this method also @@ -28,52 +33,46 @@ import org.springframework.ldap.core.LdapTemplate; * * @author Mattias Arthursson */ -public class LdapTemplateContextMapperITest extends - AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateContextMapperITest extends AbstractLdapTemplateIntegrationTest { + + @Autowired + private LdapTemplate tested; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + /** + * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) + * being set in the ContextSource. + */ + @Test + public void testSearch_ContextMapper() { + ContextMapper mapper = new PersonContextMapper(); + List result = tested.search("ou=company1,c=Sweden", "(&(objectclass=person)(sn=Person2))", mapper); - /** - * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) - * being set in the ContextSource. - */ - public void testSearch_ContextMapper() { - ContextMapper mapper = new PersonContextMapper(); - List result = tested.search("ou=company1,c=Sweden", - "(&(objectclass=person)(sn=Person2))", mapper); + assertEquals(1, result.size()); + Person person = (Person) result.get(0); + assertEquals("Some Person2", person.getFullname()); + assertEquals("Person2", person.getLastname()); + assertEquals("Sweden, Company1, Some Person2", person.getDescription()); + } - assertEquals(1, result.size()); - Person person = (Person) result.get(0); - assertEquals("Some Person2", person.getFullname()); - assertEquals("Person2", person.getLastname()); - assertEquals("Sweden, Company1, Some Person2", person.getDescription()); - } + /** + * Demonstrates how to retrieve all values of a multi-value attribute. + * + * @see LdapTemplateAttributesMapperITest#testSearch_AttributesMapper_MultiValue() + */ + @Test + public void testSearch_ContextMapper_MultiValue() throws Exception { + ContextMapper mapper = new ContextMapper() { + public Object mapFromContext(Object ctx) { + DirContextAdapter adapter = (DirContextAdapter) ctx; + String[] members = adapter.getStringAttributes("uniqueMember"); + return members; + } + }; + List result = tested.search("ou=groups", "(objectclass=groupOfUniqueNames)", mapper); - /** - * Demonstrates how to retrieve all values of a multi-value attribute. - * - * @see LdapTemplateAttributesMapperITest#testSearch_AttributesMapper_MultiValue() - */ - public void testSearch_ContextMapper_MultiValue() throws Exception { - ContextMapper mapper = new ContextMapper() { - public Object mapFromContext(Object ctx) { - DirContextAdapter adapter = (DirContextAdapter) ctx; - String[] members = adapter.getStringAttributes("uniqueMember"); - return members; - } - }; - List result = tested.search("ou=groups", - "(objectclass=groupOfUniqueNames)", mapper); - - assertEquals(2, result.size()); - assertEquals(1, ((String[]) result.get(0)).length); - assertEquals(5, ((String[]) result.get(1)).length); - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + assertEquals(2, result.size()); + assertEquals(1, ((String[]) result.get(0)).length); + assertEquals(5, ((String[]) result.get(1)).length); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java index 58d0e27c..13d5ec6f 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java @@ -15,134 +15,141 @@ */ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + import java.util.List; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; import org.springframework.ldap.test.AttributeCheckContextMapper; +import org.springframework.test.context.ContextConfiguration; /** * Tests for LdapTemplate's list methods. * * @author Ulrik Sandberg */ -public class LdapTemplateListITest extends - AbstractLdapTemplateIntegrationTest { +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateListITest extends AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; + @Autowired + private LdapTemplate tested; - private AttributeCheckContextMapper contextMapper; + private AttributeCheckContextMapper contextMapper; - private static final String BASE_STRING = ""; + private static final String BASE_STRING = ""; - private static final DistinguishedName BASE_NAME = new DistinguishedName( - BASE_STRING); + private static final DistinguishedName BASE_NAME = new DistinguishedName(BASE_STRING); - private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", - "telephoneNumber" }; + private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", "telephoneNumber" }; - private static final String[] ALL_VALUES = { "Some Person", "Person", - "Sweden, Company2, Some Person", "+46 555-456321" }; + private static final String[] ALL_VALUES = { "Some Person", "Person", "Sweden, Company2, Some Person", + "+46 555-456321" }; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + @Before + public void prepareTestedInstance() throws Exception { + contextMapper = new AttributeCheckContextMapper(); + } - protected void onSetUp() throws Exception { - super.onSetUp(); + @After + public void tearDown() throws Exception { + contextMapper = null; + } - contextMapper = new AttributeCheckContextMapper(); - } + @Test + public void testListBindings_ContextMapper() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + List list = tested.listBindings("ou=company2,c=Sweden" + BASE_STRING, contextMapper); + assertEquals(1, list.size()); + } - protected void onTearDown() throws Exception { - super.onTearDown(); + @Test + public void testListBindings_ContextMapper_Name() { + contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); + contextMapper.setExpectedValues(ALL_VALUES); + DistinguishedName dn = new DistinguishedName("ou=company2,c=Sweden"); + dn.append(BASE_NAME); + List list = tested.listBindings(dn, contextMapper); + assertEquals(1, list.size()); + } - contextMapper = null; - } + @Test + public void testListBindings_ContextMapper_MapToPersons() { + DistinguishedName dn = new DistinguishedName("ou=company1,c=Sweden"); + dn.append(BASE_NAME); + List list = tested.listBindings(dn, new PersonContextMapper()); + assertEquals(3, list.size()); + String personClass = "org.springframework.ldap.Person"; + assertEquals(personClass, list.get(0).getClass().getName()); + assertEquals(personClass, list.get(1).getClass().getName()); + assertEquals(personClass, list.get(2).getClass().getName()); + } - public void testListBindings_ContextMapper() { - contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); - contextMapper.setExpectedValues(ALL_VALUES); - List list = tested.listBindings("ou=company2,c=Sweden" + BASE_STRING, - contextMapper); - assertEquals(1, list.size()); - } + @Test + public void testList() { + List list = tested.list(BASE_STRING); + assertEquals(3, list.size()); + assertTrue(list.contains("ou=groups")); + assertTrue(list.contains("c=Norway")); + assertTrue(list.contains("c=Sweden")); + } - public void testListBindings_ContextMapper_Name() { - contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); - contextMapper.setExpectedValues(ALL_VALUES); - DistinguishedName dn = new DistinguishedName("ou=company2,c=Sweden"); - dn.append(BASE_NAME); - List list = tested.listBindings(dn, contextMapper); - assertEquals(1, list.size()); - } + @Test + public void testList_Name() { + List list = tested.list(BASE_NAME); + assertEquals(3, list.size()); + assertTrue(list.contains("ou=groups")); + assertTrue(list.contains("c=Norway")); + assertTrue(list.contains("c=Sweden")); + } - public void testListBindings_ContextMapper_MapToPersons() { - DistinguishedName dn = new DistinguishedName("ou=company1,c=Sweden"); - dn.append(BASE_NAME); - List list = tested.listBindings(dn, new PersonContextMapper()); - assertEquals(3, list.size()); - String personClass = "org.springframework.ldap.Person"; - assertEquals(personClass, list.get(0).getClass().getName()); - assertEquals(personClass, list.get(1).getClass().getName()); - assertEquals(personClass, list.get(2).getClass().getName()); - } + @Test + public void testList_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.list(BASE_STRING, handler); + assertEquals(3, handler.getNoOfRows()); + } - public void testList() { - List list = tested.list(BASE_STRING); - assertEquals(3, list.size()); - assertTrue(list.contains("ou=groups")); - assertTrue(list.contains("c=Norway")); - assertTrue(list.contains("c=Sweden")); - } + @Test + public void testList_Name_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.list(BASE_NAME, handler); + assertEquals(3, handler.getNoOfRows()); + } - public void testList_Name() { - List list = tested.list(BASE_NAME); - assertEquals(3, list.size()); - assertTrue(list.contains("ou=groups")); - assertTrue(list.contains("c=Norway")); - assertTrue(list.contains("c=Sweden")); - } + @Test + public void testListBindings() { + List list = tested.listBindings(BASE_STRING); + assertEquals(3, list.size()); + assertTrue(list.contains("ou=groups")); + assertTrue(list.contains("c=Norway")); + assertTrue(list.contains("c=Sweden")); + } - public void testList_Handler() throws Exception { - CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); - tested.list(BASE_STRING, handler); - assertEquals(3, handler.getNoOfRows()); - } + @Test + public void testListBindings_Name() { + List list = tested.listBindings(BASE_NAME); + assertEquals(3, list.size()); + } - public void testList_Name_Handler() throws Exception { - CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); - tested.list(BASE_NAME, handler); - assertEquals(3, handler.getNoOfRows()); - } + @Test + public void testListBindings_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.listBindings(BASE_STRING, handler); + assertEquals(3, handler.getNoOfRows()); + } - public void testListBindings() { - List list = tested.listBindings(BASE_STRING); - assertEquals(3, list.size()); - assertTrue(list.contains("ou=groups")); - assertTrue(list.contains("c=Norway")); - assertTrue(list.contains("c=Sweden")); - } - - public void testListBindings_Name() { - List list = tested.listBindings(BASE_NAME); - assertEquals(3, list.size()); - } - - public void testListBindings_Handler() throws Exception { - CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); - tested.listBindings(BASE_STRING, handler); - assertEquals(3, handler.getNoOfRows()); - } - - public void testListBindings_Name_Handler() throws Exception { - CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); - tested.listBindings(BASE_NAME, handler); - assertEquals(3, handler.getNoOfRows()); - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + @Test + public void testListBindings_Name_Handler() throws Exception { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + tested.listBindings(BASE_NAME, handler); + assertEquals(3, handler.getNoOfRows()); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java index cb479a9b..86d12d69 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java @@ -16,15 +16,22 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; + import javax.naming.NamingException; import javax.naming.directory.Attributes; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextSource; +import org.springframework.test.context.ContextConfiguration; /** * Tests the lookup methods of LdapTemplate. @@ -32,43 +39,39 @@ import org.springframework.ldap.core.support.AbstractContextSource; * @author Mattias Arthursson * @author Ulrik Sandberg */ -public class LdapTemplateLookupITest extends - AbstractLdapTemplateIntegrationTest { +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateLookupITest extends AbstractLdapTemplateIntegrationTest { + @Autowired private LdapTemplate tested; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } - /** * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) * being set in the ContextSource. */ + @Test public void testLookup_Plain() { - DirContextAdapter result = (DirContextAdapter) tested - .lookup("cn=Some Person2, ou=company1,c=Sweden"); + DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person2, ou=company1,c=Sweden"); assertEquals("Some Person2", result.getStringAttribute("cn")); assertEquals("Person2", result.getStringAttribute("sn")); - assertEquals("Sweden, Company1, Some Person2", result - .getStringAttribute("description")); + assertEquals("Sweden, Company1, Some Person2", result.getStringAttribute("description")); } + @Test public void testLookup_AttributesMapper() { AttributesMapper mapper = new PersonAttributesMapper(); - Person person = (Person) tested.lookup( - "cn=Some Person2, ou=company1,c=Sweden", mapper); + Person person = (Person) tested.lookup("cn=Some Person2, ou=company1,c=Sweden", mapper); assertEquals("Some Person2", person.getFullname()); assertEquals("Person2", person.getLastname()); assertEquals("Sweden, Company1, Some Person2", person.getDescription()); } + @Test public void testLookup_AttributesMapper_DistinguishedName() { AttributesMapper mapper = new PersonAttributesMapper(); - Person person = (Person) tested.lookup(new DistinguishedName( - "cn=Some Person2, ou=company1,c=Sweden"), mapper); + Person person = (Person) tested.lookup(new DistinguishedName("cn=Some Person2, ou=company1,c=Sweden"), mapper); assertEquals("Some Person2", person.getFullname()); assertEquals("Person2", person.getLastname()); @@ -82,21 +85,18 @@ public class LdapTemplateLookupITest extends * * @author Ulrik Sandberg */ - private final class SubsetPersonAttributesMapper implements - AttributesMapper { + private final class SubsetPersonAttributesMapper implements AttributesMapper { /** * Maps the cn attribute into a {@link Person} object. * Also verifies that the other attributes haven't been set. * * @see org.springframework.ldap.core.AttributesMapper#mapFromAttributes(javax.naming.directory.Attributes) */ - public Object mapFromAttributes(Attributes attributes) - throws NamingException { + public Object mapFromAttributes(Attributes attributes) throws NamingException { Person person = new Person(); person.setFullname((String) attributes.get("cn").get()); assertNull("sn should be null", attributes.get("sn")); - assertNull("description should be null", attributes - .get("description")); + assertNull("description should be null", attributes.get("description")); return person; } } @@ -105,12 +105,11 @@ public class LdapTemplateLookupITest extends * Verifies that only the subset is used when specifying a subset of the * available attributes as return attributes. */ + @Test public void testLookup_ReturnAttributes_AttributesMapper() { AttributesMapper mapper = new SubsetPersonAttributesMapper(); - Person person = (Person) tested.lookup( - "cn=Some Person2, ou=company1,c=Sweden", new String[] { "cn" }, - mapper); + Person person = (Person) tested.lookup("cn=Some Person2, ou=company1,c=Sweden", new String[] { "cn" }, mapper); assertEquals("Some Person2", person.getFullname()); assertNull("lastName should not be set", person.getLastname()); @@ -122,10 +121,10 @@ public class LdapTemplateLookupITest extends * available attributes as return attributes. Uses DistinguishedName instead * of plain string as name. */ + @Test public void testLookup_ReturnAttributes_AttributesMapper_DistinguishedName() { AttributesMapper mapper = new SubsetPersonAttributesMapper(); - Person person = (Person) tested.lookup(new DistinguishedName( - "cn=Some Person2, ou=company1,c=Sweden"), + Person person = (Person) tested.lookup(new DistinguishedName("cn=Some Person2, ou=company1,c=Sweden"), new String[] { "cn" }, mapper); assertEquals("Some Person2", person.getFullname()); @@ -137,10 +136,10 @@ public class LdapTemplateLookupITest extends * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) * being set in the ContextSource. */ + @Test public void testLookup_ContextMapper() { ContextMapper mapper = new PersonContextMapper(); - Person person = (Person) tested.lookup( - "cn=Some Person2, ou=company1,c=Sweden", mapper); + Person person = (Person) tested.lookup("cn=Some Person2, ou=company1,c=Sweden", mapper); assertEquals("Some Person2", person.getFullname()); assertEquals("Person2", person.getLastname()); @@ -151,12 +150,11 @@ public class LdapTemplateLookupITest extends * Verifies that only the subset is used when specifying a subset of the * available attributes as return attributes. */ + @Test public void testLookup_ReturnAttributes_ContextMapper() { ContextMapper mapper = new PersonContextMapper(); - Person person = (Person) tested.lookup( - "cn=Some Person2, ou=company1,c=Sweden", new String[] { "cn" }, - mapper); + Person person = (Person) tested.lookup("cn=Some Person2, ou=company1,c=Sweden", new String[] { "cn" }, mapper); assertEquals("Some Person2", person.getFullname()); assertNull("lastName should not be set", person.getLastname()); @@ -166,13 +164,12 @@ public class LdapTemplateLookupITest extends /** * Verifies that we can lookup an entry that has a multi-valued rdn, which * means more than one attribute is part of the relative DN for the entry. - * - * TODO Enable test when ApacheDS supports multi-valued rdns. */ + @Test + @Ignore("Enable test when ApacheDS supports multi-valued rdns") public void DISABLED_testLookup_MultiValuedRdn() { AttributesMapper mapper = new PersonAttributesMapper(); - Person person = (Person) tested.lookup( - "cn=Some Person+sn=Person, ou=company1,c=Norway", mapper); + Person person = (Person) tested.lookup("cn=Some Person+sn=Person, ou=company1,c=Norway", mapper); assertEquals("Some Person", person.getFullname()); assertEquals("Person", person.getLastname()); @@ -183,47 +180,40 @@ public class LdapTemplateLookupITest extends * Verifies that we can lookup an entry that has a multi-valued rdn, which * means more than one attribute is part of the relative DN for the entry. * - * TODO Enable test when ApacheDS supports multi-valued rdns. */ + @Test + @Ignore("Enable test when ApacheDS supports multi-valued rdns") public void DISABLED_testLookup_MultiValuedRdn_DirContextAdapter() { - DirContextAdapter result = (DirContextAdapter) tested - .lookup("cn=Some Person+sn=Person, ou=company1,c=Norway"); + DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person+sn=Person, ou=company1,c=Norway"); assertEquals("Some Person", result.getStringAttribute("cn")); assertEquals("Person", result.getStringAttribute("sn")); - assertEquals("Norway, Company1, Some Person", result - .getStringAttribute("description")); + assertEquals("Norway, Company1, Some Person", result.getStringAttribute("description")); } + @Test public void testLookup_GetNameInNamespace_Plain() { - DirContextAdapter result = (DirContextAdapter) tested - .lookup("cn=Some Person2, ou=company1,c=Sweden"); + DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person2, ou=company1,c=Sweden"); - assertEquals("cn=Some Person2, ou=company1, c=Sweden", result.getDn() - .toString()); - assertEquals( - "cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", - result.getNameInNamespace()); + assertEquals("cn=Some Person2, ou=company1, c=Sweden", result.getDn().toString()); + assertEquals("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", result.getNameInNamespace()); } + @Test public void testLookup_GetNameInNamespace_MultiRdn() { - DirContextAdapter result = (DirContextAdapter) tested - .lookup("cn=Some Person+sn=Person, ou=company1,c=Norway"); + DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person+sn=Person, ou=company1,c=Norway"); - assertEquals("cn=Some Person+sn=Person, ou=company1, c=Norway", result - .getDn().toString()); - assertEquals( - "cn=Some Person+sn=Person, ou=company1, c=Norway, dc=jayway, dc=se", - result.getNameInNamespace()); + assertEquals("cn=Some Person+sn=Person, ou=company1, c=Norway", result.getDn().toString()); + assertEquals("cn=Some Person+sn=Person, ou=company1, c=Norway, dc=jayway, dc=se", result.getNameInNamespace()); } /** * Tests bind and lookup with Java objects where the ContextSource already * has a DirObjectFactory configured. */ + @Test public void testBindJavaObject() throws Exception { - AbstractContextSource contextSource = (AbstractContextSource) tested - .getContextSource(); + AbstractContextSource contextSource = (AbstractContextSource) tested.getContextSource(); Class originalObjectFactory = contextSource.getDirObjectFactory(); try { contextSource.setDirObjectFactory(null); @@ -231,15 +221,12 @@ public class LdapTemplateLookupITest extends tested.bind("cn=myRandomInt", new Integer(54321), null); Integer result = (Integer) tested.lookup("cn=myRandomInt"); assertEquals(54321, result.intValue()); - } finally { + } + finally { // reset the DirObjectFactory so as not to disturb other tests tested.unbind("cn=myRandomInt"); contextSource.setDirObjectFactory(originalObjectFactory); contextSource.afterPropertiesSet(); } } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java index bf5560c6..fe7aa1b2 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java @@ -16,15 +16,23 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; + import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.ModificationItem; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Tests the modification methods (rebind and modifyAttributes) of LdapTemplate. @@ -39,215 +47,202 @@ import org.springframework.ldap.core.LdapTemplate; * @author Mattias Arthursson * @author Ulrik Sandberg */ -public class LdapTemplateModifyITest extends - AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateModifyITest extends AbstractLdapTemplateIntegrationTest { - private static String PERSON4_DN = "cn=Some Person4,ou=company1,c=Sweden"; + @Autowired + private LdapTemplate tested; - private static String PERSON5_DN = "cn=Some Person5,ou=company1,c=Sweden"; + private static String PERSON4_DN = "cn=Some Person4,ou=company1,c=Sweden"; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + private static String PERSON5_DN = "cn=Some Person5,ou=company1,c=Sweden"; - protected void onSetUp() throws Exception { - super.onSetUp(); - - DirContextAdapter adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "Some Person4"); - adapter.setAttributeValue("sn", "Person4"); - adapter.setAttributeValue("description", "Some description"); + @Before + public void prepareTestedInstance() throws Exception { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); + adapter.setAttributeValue("description", "Some description"); - tested.bind(PERSON4_DN, adapter, null); + tested.bind(PERSON4_DN, adapter, null); - adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "Some Person5"); - adapter.setAttributeValue("sn", "Person5"); - adapter.setAttributeValues("description", new String[] { "qwe", "123", - "rty", "uio" }); + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "Some Person5"); + adapter.setAttributeValue("sn", "Person5"); + adapter.setAttributeValues("description", new String[] { "qwe", "123", "rty", "uio" }); - tested.bind(PERSON5_DN, adapter, null); + tested.bind(PERSON5_DN, adapter, null); - } + } - protected void onTearDown() throws Exception { - tested.unbind(PERSON4_DN); - tested.unbind(PERSON5_DN); - } + @After + public void cleanup() throws Exception { + tested.unbind(PERSON4_DN); + tested.unbind(PERSON5_DN); + } - public void testRebind_Attributes_Plain() { - Attributes attributes = setupAttributes(); + @Test + public void testRebind_Attributes_Plain() { + Attributes attributes = setupAttributes(); - tested.rebind(PERSON4_DN, null, attributes); + tested.rebind(PERSON4_DN, null, attributes); - verifyBoundCorrectData(); - } + verifyBoundCorrectData(); + } - public void testRebind_Attributes_DistinguishedName() { - Attributes attributes = setupAttributes(); + @Test + public void testRebind_Attributes_DistinguishedName() { + Attributes attributes = setupAttributes(); - tested.rebind(new DistinguishedName(PERSON4_DN), null, attributes); + tested.rebind(new DistinguishedName(PERSON4_DN), null, attributes); - verifyBoundCorrectData(); - } + verifyBoundCorrectData(); + } - public void testModifyAttributes_MultiValueReplace() { - BasicAttribute attr = new BasicAttribute("description", - "Some other description"); - attr.add("Another description"); - ModificationItem[] mods = new ModificationItem[1]; - mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr); + @Test + public void testModifyAttributes_MultiValueReplace() { + BasicAttribute attr = new BasicAttribute("description", "Some other description"); + attr.add("Another description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr); - tested.modifyAttributes(PERSON4_DN, mods); + tested.modifyAttributes(PERSON4_DN, mods); - DirContextAdapter result = (DirContextAdapter) tested - .lookup(PERSON4_DN); - String[] attributes = result.getStringAttributes("description"); - assertEquals(2, attributes.length); - assertEquals("Some other description", attributes[0]); - assertEquals("Another description", attributes[1]); - } + DirContextAdapter result = (DirContextAdapter) tested.lookup(PERSON4_DN); + String[] attributes = result.getStringAttributes("description"); + assertEquals(2, attributes.length); + assertEquals("Some other description", attributes[0]); + assertEquals("Another description", attributes[1]); + } - public void testModifyAttributes_MultiValueAdd() { - BasicAttribute attr = new BasicAttribute("description", - "Some other description"); - attr.add("Another description"); - ModificationItem[] mods = new ModificationItem[1]; - mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); + @Test + public void testModifyAttributes_MultiValueAdd() { + BasicAttribute attr = new BasicAttribute("description", "Some other description"); + attr.add("Another description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); - tested.modifyAttributes(PERSON4_DN, mods); + tested.modifyAttributes(PERSON4_DN, mods); - DirContextAdapter result = (DirContextAdapter) tested - .lookup(PERSON4_DN); - String[] attributes = result.getStringAttributes("description"); - assertEquals(3, attributes.length); - assertEquals("Some description", attributes[0]); - assertEquals("Some other description", attributes[1]); - assertEquals("Another description", attributes[2]); - } + DirContextAdapter result = (DirContextAdapter) tested.lookup(PERSON4_DN); + String[] attributes = result.getStringAttributes("description"); + assertEquals(3, attributes.length); + assertEquals("Some description", attributes[0]); + assertEquals("Some other description", attributes[1]); + assertEquals("Another description", attributes[2]); + } - public void testModifyAttributes_MultiValueAddDuplicateToUnordered() { - BasicAttribute attr = new BasicAttribute("description", - "Some description"); - ModificationItem[] mods = new ModificationItem[1]; - mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); + @Test + public void testModifyAttributes_MultiValueAddDuplicateToUnordered() { + BasicAttribute attr = new BasicAttribute("description", "Some description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); - try { - tested.modifyAttributes(PERSON4_DN, mods); - fail("AttributeInUseException expected"); - } catch (AttributeInUseException expected) { - // expected - } - } + try { + tested.modifyAttributes(PERSON4_DN, mods); + fail("AttributeInUseException expected"); + } + catch (AttributeInUseException expected) { + // expected + } + } - /** - * Test written originally to verify that duplicates are allowed on ordered - * attributes, but had to be changed since Apache DS seems to disallow - * duplicates even for ordered attributes. - */ - public void testModifyAttributes_MultiValueAddDuplicateToOrdered() { - BasicAttribute attr = new BasicAttribute("description", - "Some other description", true); // ordered - attr.add("Another description"); - // Commented out duplicate to make test work for Apache DS - // attr.add("Some description"); - ModificationItem[] mods = new ModificationItem[1]; - mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); + /** + * Test written originally to verify that duplicates are allowed on ordered + * attributes, but had to be changed since Apache DS seems to disallow + * duplicates even for ordered attributes. + */ + @Test + public void testModifyAttributes_MultiValueAddDuplicateToOrdered() { + BasicAttribute attr = new BasicAttribute("description", "Some other description", true); // ordered + attr.add("Another description"); + // Commented out duplicate to make test work for Apache DS + // attr.add("Some description"); + ModificationItem[] mods = new ModificationItem[1]; + mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr); - tested.modifyAttributes(PERSON4_DN, mods); + tested.modifyAttributes(PERSON4_DN, mods); - DirContextAdapter result = (DirContextAdapter) tested - .lookup(PERSON4_DN); - String[] attributes = result.getStringAttributes("description"); - assertEquals(3, attributes.length); - assertEquals("Some description", attributes[0]); - assertEquals("Some other description", attributes[1]); - assertEquals("Another description", attributes[2]); - } + DirContextAdapter result = (DirContextAdapter) tested.lookup(PERSON4_DN); + String[] attributes = result.getStringAttributes("description"); + assertEquals(3, attributes.length); + assertEquals("Some description", attributes[0]); + assertEquals("Some other description", attributes[1]); + assertEquals("Another description", attributes[2]); + } - public void testModifyAttributes_Plain() { - ModificationItem item = new ModificationItem( - DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", - "Some other description")); + @Test + public void testModifyAttributes_Plain() { + ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", + "Some other description")); - tested.modifyAttributes(PERSON4_DN, new ModificationItem[] { item }); + tested.modifyAttributes(PERSON4_DN, new ModificationItem[] { item }); - verifyBoundCorrectData(); - } + verifyBoundCorrectData(); + } - public void testModifyAttributes_DistinguishedName() { - ModificationItem item = new ModificationItem( - DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", - "Some other description")); + @Test + public void testModifyAttributes_DistinguishedName() { + ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", + "Some other description")); - tested.modifyAttributes(new DistinguishedName(PERSON4_DN), - new ModificationItem[] { item }); + tested.modifyAttributes(new DistinguishedName(PERSON4_DN), new ModificationItem[] { item }); - verifyBoundCorrectData(); - } + verifyBoundCorrectData(); + } - public void testModifyAttributes_DirContextAdapter_MultiAttributes() { - DirContextAdapter adapter = (DirContextAdapter) tested - .lookup(PERSON5_DN); - adapter.setAttributeValues("description", new String[] { "qwe", "123", - "klytt", "kalle" }); + @Test + public void testModifyAttributes_DirContextAdapter_MultiAttributes() { + DirContextAdapter adapter = (DirContextAdapter) tested.lookup(PERSON5_DN); + adapter.setAttributeValues("description", new String[] { "qwe", "123", "klytt", "kalle" }); - tested.modifyAttributes(PERSON5_DN, adapter.getModificationItems()); + tested.modifyAttributes(PERSON5_DN, adapter.getModificationItems()); - // Verify - adapter = (DirContextAdapter) tested.lookup(PERSON5_DN); - String[] attributes = adapter.getStringAttributes("description"); - assertEquals(4, attributes.length); - assertEquals("qwe", attributes[0]); - assertEquals("123", attributes[1]); - assertEquals("klytt", attributes[2]); - assertEquals("kalle", attributes[3]); - } + // Verify + adapter = (DirContextAdapter) tested.lookup(PERSON5_DN); + String[] attributes = adapter.getStringAttributes("description"); + assertEquals(4, attributes.length); + assertEquals("qwe", attributes[0]); + assertEquals("123", attributes[1]); + assertEquals("klytt", attributes[2]); + assertEquals("kalle", attributes[3]); + } - /** - * Demonstrates how the DirContextAdapter can be used to automatically keep - * track of changes of the attributes and deliver ModificationItems to use - * in moifyAttributes(). - */ - public void testModifyAttributes_DirContextAdapter() throws Exception { - DirContextAdapter adapter = (DirContextAdapter) tested - .lookup(PERSON4_DN); + /** + * Demonstrates how the DirContextAdapter can be used to automatically keep + * track of changes of the attributes and deliver ModificationItems to use + * in moifyAttributes(). + */ + @Test + public void testModifyAttributes_DirContextAdapter() throws Exception { + DirContextAdapter adapter = (DirContextAdapter) tested.lookup(PERSON4_DN); - adapter.setAttributeValue("description", "Some other description"); + adapter.setAttributeValue("description", "Some other description"); - ModificationItem[] modificationItems = adapter.getModificationItems(); - tested.modifyAttributes(PERSON4_DN, modificationItems); + ModificationItem[] modificationItems = adapter.getModificationItems(); + tested.modifyAttributes(PERSON4_DN, modificationItems); - verifyBoundCorrectData(); - } + verifyBoundCorrectData(); + } - private Attributes setupAttributes() { - Attributes attributes = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute("objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attributes.put(ocattr); - attributes.put("cn", "Some Person4"); - attributes.put("sn", "Person4"); - attributes.put("description", "Some other description"); - return attributes; - } + private Attributes setupAttributes() { + Attributes attributes = new BasicAttributes(); + BasicAttribute ocattr = new BasicAttribute("objectclass"); + ocattr.add("top"); + ocattr.add("person"); + attributes.put(ocattr); + attributes.put("cn", "Some Person4"); + attributes.put("sn", "Person4"); + attributes.put("description", "Some other description"); + return attributes; + } - private void verifyBoundCorrectData() { - DirContextAdapter result = (DirContextAdapter) tested - .lookup(PERSON4_DN); - assertEquals("Some Person4", result.getStringAttribute("cn")); - assertEquals("Person4", result.getStringAttribute("sn")); - assertEquals("Some other description", result - .getStringAttribute("description")); - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + private void verifyBoundCorrectData() { + DirContextAdapter result = (DirContextAdapter) tested.lookup(PERSON4_DN); + assertEquals("Some Person4", result.getStringAttribute("cn")); + assertEquals("Person4", result.getStringAttribute("sn")); + assertEquals("Some other description", result.getStringAttribute("description")); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java index f82a2e6d..1c4ffe4b 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java @@ -16,9 +16,16 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; +import org.springframework.test.context.ContextConfiguration; /** * Tests to verify that not setting a base suffix on the ContextSource (as @@ -28,71 +35,58 @@ import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; * * @author Mattias Arthursson */ -public class LdapTemplateNoBaseSuffixITest extends - AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; +@ContextConfiguration(locations = { "/conf/ldapTemplateNoBaseSuffixTestContext.xml" }) +public class LdapTemplateNoBaseSuffixITest extends AbstractLdapTemplateIntegrationTest { - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateNoBaseSuffixTestContext.xml" }; - } + @Autowired + private LdapTemplate tested; - /** - * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) - * being set in the ContextSource. - */ - public void testLookup_Plain() { - DirContextAdapter result = (DirContextAdapter) tested - .lookup("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se"); + /** + * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) + * being set in the ContextSource. + */ + @Test + public void testLookup_Plain() { + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se"); - assertEquals("Some Person2", result.getStringAttribute("cn")); - assertEquals("Person2", result.getStringAttribute("sn")); - assertEquals("Sweden, Company1, Some Person2", result - .getStringAttribute("description")); - assertEquals( - "cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", - result.getDn().toString()); - assertEquals( - "cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", - result.getNameInNamespace()); - } + assertEquals("Some Person2", result.getStringAttribute("cn")); + assertEquals("Person2", result.getStringAttribute("sn")); + assertEquals("Sweden, Company1, Some Person2", result.getStringAttribute("description")); + assertEquals("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", result.getDn().toString()); + assertEquals("cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se", result.getNameInNamespace()); + } - public void testSearch_Plain() { - CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); + @Test + public void testSearch_Plain() { + CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); - tested.search("dc=jayway, dc=se", "(objectclass=person)", handler); - assertEquals(5, handler.getNoOfRows()); - } + tested.search("dc=jayway, dc=se", "(objectclass=person)", handler); + assertEquals(5, handler.getNoOfRows()); + } - public void testBindAndUnbind_Plain() { - DirContextAdapter adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "Some Person4"); - adapter.setAttributeValue("sn", "Person4"); - tested.bind("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se", - adapter, null); + @Test + public void testBindAndUnbind_Plain() { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "Some Person4"); + adapter.setAttributeValue("sn", "Person4"); + tested.bind("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se", adapter, null); - DirContextAdapter result = (DirContextAdapter) tested - .lookup("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); + DirContextAdapter result = (DirContextAdapter) tested + .lookup("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); - assertEquals("Some Person4", result.getStringAttribute("cn")); - assertEquals("Person4", result.getStringAttribute("sn")); - assertEquals( - "cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se", - result.getDn().toString()); + assertEquals("Some Person4", result.getStringAttribute("cn")); + assertEquals("Person4", result.getStringAttribute("sn")); + assertEquals("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se", result.getDn().toString()); - tested - .unbind("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); - try { - tested - .lookup("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); - fail("NameNotFoundException expected"); - } catch (NameNotFoundException expected) { - assertTrue(true); - } - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + tested.unbind("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); + try { + tested.lookup("cn=Some Person4, ou=company1, c=Sweden, dc=jayway, dc=se"); + fail("NameNotFoundException expected"); + } + catch (NameNotFoundException expected) { + assertTrue(true); + } + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateObjectBindIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateObjectBindIntegrationTest.java index f4b92d3d..57855498 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateObjectBindIntegrationTest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateObjectBindIntegrationTest.java @@ -16,13 +16,19 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; + import java.io.Serializable; import java.util.LinkedList; import javax.naming.directory.DirContext; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextSource; +import org.springframework.test.context.ContextConfiguration; /** * Tests bind and lookup with Java objects where the ContextSource has a @@ -30,15 +36,13 @@ import org.springframework.ldap.core.support.AbstractContextSource; * * @author Ulrik Sandberg */ -public class LdapTemplateObjectBindIntegrationTest extends - AbstractLdapTemplateIntegrationTest { +@ContextConfiguration(locations = { "/conf/ldapTemplateObjectBindTestContext.xml" }) +public class LdapTemplateObjectBindIntegrationTest extends AbstractLdapTemplateIntegrationTest { + @Autowired private LdapTemplate tested; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateObjectBindTestContext.xml" }; - } - + @Test public void testBindJavaObject() throws Exception { String dn = "cn=myRandomInt"; tested.bind(dn, new Integer(54321), null); @@ -48,6 +52,7 @@ public class LdapTemplateObjectBindIntegrationTest extends tested.unbind(dn); } + @Test public void testBindLinkedList() { LinkedList list = new LinkedList(); list.add(new Integer(54321)); @@ -63,6 +68,7 @@ public class LdapTemplateObjectBindIntegrationTest extends tested.unbind(dn); } + @Test public void testBindNonSerializableJavaObjectShouldFail() throws Exception { NonSerializablePojo pojo = new NonSerializablePojo(); pojo.setName("A Name"); @@ -70,10 +76,9 @@ public class LdapTemplateObjectBindIntegrationTest extends try { tested.bind("cn=myRandomObject", pojo, null); fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException expected) { - assertEquals( - "can only bind Referenceable, Serializable, DirContext", - expected.getMessage()); + } + catch (IllegalArgumentException expected) { + assertEquals("can only bind Referenceable, Serializable, DirContext", expected.getMessage()); } } @@ -93,14 +98,14 @@ public class LdapTemplateObjectBindIntegrationTest extends } } + @Test public void testBindSerializableJavaObjectShouldSucceed() throws Exception { SerializablePojo pojo = new SerializablePojo(); pojo.setName("A Name"); tested.bind("cn=myRandomObject", pojo, null); - SerializablePojo result = (SerializablePojo) tested - .lookup("cn=myRandomObject"); + SerializablePojo result = (SerializablePojo) tested.lookup("cn=myRandomObject"); assertEquals("A Name", result.getName()); tested.unbind("cn=myRandomObject"); } @@ -127,9 +132,9 @@ public class LdapTemplateObjectBindIntegrationTest extends * This test demonstrates that it's fully possible to perform plain JNDI * operations from Spring LDAP. */ + @Test public void testPlainJndiBindJavaObject() throws Exception { - AbstractContextSource contextSource = (AbstractContextSource) tested - .getContextSource(); + AbstractContextSource contextSource = (AbstractContextSource) tested.getContextSource(); DirContext ctx = contextSource.getReadWriteContext(); ctx.bind("cn=myRandomInt", new Integer(28420)); @@ -138,8 +143,4 @@ public class LdapTemplateObjectBindIntegrationTest extends assertEquals(28420, result.intValue()); tested.unbind("cn=myRandomInt"); } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java index e43e14aa..402d43e6 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java @@ -16,11 +16,18 @@ package org.springframework.ldap; +import static junit.framework.Assert.fail; + import javax.naming.Name; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Tests the recursive modification methods (unbind and the protected delete @@ -29,99 +36,89 @@ import org.springframework.ldap.core.LdapTemplate; * @author Mattias Arthursson * @author Ulrik Sandberg */ -public class LdapTemplateRecursiveDeleteITest extends - AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateRecursiveDeleteITest extends AbstractLdapTemplateIntegrationTest { - private static DistinguishedName DN = new DistinguishedName( - "cn=Some Person5,ou=company1,c=Sweden"); + @Autowired + private LdapTemplate tested; - private DistinguishedName firstSubDn; + private static DistinguishedName DN = new DistinguishedName("cn=Some Person5,ou=company1,c=Sweden"); - private DistinguishedName secondSubDn; + private DistinguishedName firstSubDn; - private DistinguishedName leafDn; + private DistinguishedName secondSubDn; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + private DistinguishedName leafDn; - protected void onSetUp() throws Exception { - super.onSetUp(); - - DirContextAdapter adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "Some Person5"); - adapter.setAttributeValue("sn", "Person5"); - adapter.setAttributeValue("description", "Some description"); - tested.bind(DN, adapter, null); + @Before + public void prepareTestedInstance() throws Exception { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "Some Person5"); + adapter.setAttributeValue("sn", "Person5"); + adapter.setAttributeValue("description", "Some description"); + tested.bind(DN, adapter, null); - firstSubDn = new DistinguishedName("cn=subPerson"); - firstSubDn.prepend(DN); - adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "subPerson"); - adapter.setAttributeValue("sn", "subPerson"); - adapter.setAttributeValue("description", - "Should be recursively deleted"); - tested.bind(firstSubDn, adapter, null); - secondSubDn = new DistinguishedName("cn=subPerson2"); - secondSubDn.prepend(DN); - adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "subPerson2"); - adapter.setAttributeValue("sn", "subPerson2"); - adapter.setAttributeValue("description", - "Should be recursively deleted"); - tested.bind(secondSubDn, adapter, null); + firstSubDn = new DistinguishedName("cn=subPerson"); + firstSubDn.prepend(DN); + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "subPerson"); + adapter.setAttributeValue("sn", "subPerson"); + adapter.setAttributeValue("description", "Should be recursively deleted"); + tested.bind(firstSubDn, adapter, null); + secondSubDn = new DistinguishedName("cn=subPerson2"); + secondSubDn.prepend(DN); + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "subPerson2"); + adapter.setAttributeValue("sn", "subPerson2"); + adapter.setAttributeValue("description", "Should be recursively deleted"); + tested.bind(secondSubDn, adapter, null); - leafDn = new DistinguishedName("cn=subSubPerson"); - leafDn.prepend(firstSubDn); - adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "subSubPerson"); - adapter.setAttributeValue("sn", "subSubPerson"); - adapter.setAttributeValue("description", - "Should be recursively deleted"); - tested.bind(leafDn, adapter, null); - } + leafDn = new DistinguishedName("cn=subSubPerson"); + leafDn.prepend(firstSubDn); + adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "subSubPerson"); + adapter.setAttributeValue("sn", "subSubPerson"); + adapter.setAttributeValue("description", "Should be recursively deleted"); + tested.bind(leafDn, adapter, null); + } - protected void onTearDown() throws Exception { - try { - tested.unbind(DN, true); - } catch (NameNotFoundException ignore) { - // ignore - } - } + @After + public void cleanup() throws Exception { + try { + tested.unbind(DN, true); + } + catch (NameNotFoundException ignore) { + // ignore + } + } - public void testRecursiveUnbind() { - tested.unbind(DN, true); + @Test + public void testRecursiveUnbind() { + tested.unbind(DN, true); - verifyDeleted(DN); - verifyDeleted(firstSubDn); - verifyDeleted(secondSubDn); - verifyDeleted(leafDn); - } + verifyDeleted(DN); + verifyDeleted(firstSubDn); + verifyDeleted(secondSubDn); + verifyDeleted(leafDn); + } - public void testRecursiveUnbindOnLeaf() { - tested.unbind(leafDn, true); - verifyDeleted(leafDn); - } + @Test + public void testRecursiveUnbindOnLeaf() { + tested.unbind(leafDn, true); + verifyDeleted(leafDn); + } - private void verifyDeleted(Name dn) { - try { - tested.lookup(dn); - fail("Expected entry '" + dn + "' to be non-existent"); - } catch (NameNotFoundException expected) { - // expected - } - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + private void verifyDeleted(Name dn) { + try { + tested.lookup(dn); + fail("Expected entry '" + dn + "' to be non-existent"); + } + catch (NameNotFoundException expected) { + // expected + } + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java index 64a78c51..24b1808a 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java @@ -16,11 +16,19 @@ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; + import javax.naming.Name; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Tests the rename methods of LdapTemplate. @@ -30,71 +38,65 @@ import org.springframework.ldap.core.LdapTemplate; * * @author Ulrik Sandberg */ -public class LdapTemplateRenameITest extends - AbstractLdapTemplateIntegrationTest { - private LdapTemplate tested; +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateRenameITest extends AbstractLdapTemplateIntegrationTest { - private static String DN = "cn=Some Person6,ou=company1,c=Sweden"; + @Autowired + private LdapTemplate tested; - private static String NEWDN = "cn=Some Person6,ou=company2,c=Sweden"; + private static String DN = "cn=Some Person6,ou=company1,c=Sweden"; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + private static String NEWDN = "cn=Some Person6,ou=company2,c=Sweden"; - protected void onSetUp() throws Exception { - super.onSetUp(); - - DirContextAdapter adapter = new DirContextAdapter(); - adapter.setAttributeValues("objectclass", new String[] { "top", - "person" }); - adapter.setAttributeValue("cn", "Some Person6"); - adapter.setAttributeValue("sn", "Person6"); - adapter.setAttributeValue("description", "Some description"); + @Before + public void prepareTestedInstance() throws Exception { + DirContextAdapter adapter = new DirContextAdapter(); + adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); + adapter.setAttributeValue("cn", "Some Person6"); + adapter.setAttributeValue("sn", "Person6"); + adapter.setAttributeValue("description", "Some description"); - tested.bind(DN, adapter, null); - } + tested.bind(DN, adapter, null); + } - protected void onTearDown() throws Exception { - tested.unbind(NEWDN); - tested.unbind(DN); - } + @After + public void cleanup() throws Exception { + tested.unbind(NEWDN); + tested.unbind(DN); + } - public void testRename() { - tested.rename(DN, NEWDN); + @Test + public void testRename() { + tested.rename(DN, NEWDN); - verifyDeleted(new DistinguishedName(DN)); - verifyBoundCorrectData(); - } + verifyDeleted(new DistinguishedName(DN)); + verifyBoundCorrectData(); + } - public void testRename_DistinguishedName() throws Exception { - Name oldDn = new DistinguishedName(DN); - Name newDn = new DistinguishedName(NEWDN); - tested.rename(oldDn, newDn); - + @Test + public void testRename_DistinguishedName() throws Exception { + Name oldDn = new DistinguishedName(DN); + Name newDn = new DistinguishedName(NEWDN); + tested.rename(oldDn, newDn); - verifyDeleted(oldDn); - verifyBoundCorrectData(); - } + verifyDeleted(oldDn); + verifyBoundCorrectData(); + } - private void verifyDeleted(Name dn) { - try { - tested.lookup(dn); - fail("Expected entry '" + dn + "' to be non-existent"); - } catch (NameNotFoundException expected) { - // expected - } - } + private void verifyDeleted(Name dn) { + try { + tested.lookup(dn); + fail("Expected entry '" + dn + "' to be non-existent"); + } + catch (NameNotFoundException expected) { + // expected + } + } - private void verifyBoundCorrectData() { - DirContextAdapter result = (DirContextAdapter) tested.lookup(NEWDN); - assertEquals("Some Person6", result.getStringAttribute("cn")); - assertEquals("Person6", result.getStringAttribute("sn")); - assertEquals("Some description", result - .getStringAttribute("description")); - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + private void verifyBoundCorrectData() { + DirContextAdapter result = (DirContextAdapter) tested.lookup(NEWDN); + assertEquals("Some Person6", result.getStringAttribute("cn")); + assertEquals("Person6", result.getStringAttribute("sn")); + assertEquals("Some description", result.getStringAttribute("description")); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java index f0f3f867..bf57b524 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java @@ -15,16 +15,22 @@ */ package org.springframework.ldap; +import static junit.framework.Assert.assertEquals; + import java.util.List; import javax.naming.Name; import javax.naming.directory.SearchControls; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.test.AttributeCheckAttributesMapper; import org.springframework.ldap.test.AttributeCheckContextMapper; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.ContextConfiguration; /** * Tests for LdapTemplate's search methods. This test class tests all the @@ -33,27 +39,26 @@ import org.springframework.test.AbstractDependencyInjectionSpringContextTests; * * @author Mattias Arthursson */ -public class LdapTemplateSearchResultITest extends - AbstractDependencyInjectionSpringContextTests { +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapTemplateSearchResultITest extends AbstractLdapTemplateIntegrationTest { + @Autowired private LdapTemplate tested; private AttributeCheckAttributesMapper attributesMapper; private AttributeCheckContextMapper contextMapper; - private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", - "telephoneNumber" }; + private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", "telephoneNumber" }; private static final String[] CN_SN_ATTRS = { "cn", "sn" }; - private static final String[] ABSENT_ATTRIBUTES = { "description", - "telephoneNumber" }; + private static final String[] ABSENT_ATTRIBUTES = { "description", "telephoneNumber" }; private static final String[] CN_SN_VALUES = { "Some Person2", "Person2" }; - private static final String[] ALL_VALUES = { "Some Person2", "Person2", - "Sweden, Company1, Some Person2", "+46 555-654321" }; + private static final String[] ALL_VALUES = { "Some Person2", "Person2", "Sweden, Company1, Some Person2", + "+46 555-654321" }; private static final String BASE_STRING = ""; @@ -61,24 +66,19 @@ public class LdapTemplateSearchResultITest extends private static final Name BASE_NAME = new DistinguishedName(BASE_STRING); - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } - - protected void onSetUp() throws Exception { - super.onSetUp(); - + @Before + public void prepareTestedInstance() throws Exception { attributesMapper = new AttributeCheckAttributesMapper(); contextMapper = new AttributeCheckContextMapper(); } - protected void onTearDown() throws Exception { - super.onTearDown(); - + @After + public void cleanup() throws Exception { attributesMapper = null; contextMapper = null; } + @Test public void testSearch_AttributesMapper() { attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); attributesMapper.setExpectedValues(ALL_VALUES); @@ -86,23 +86,25 @@ public class LdapTemplateSearchResultITest extends assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_AttributesMapper() { attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); attributesMapper.setExpectedValues(ALL_VALUES); - List list = tested.search(BASE_STRING, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, attributesMapper); + List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper); assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_LimitedAttrs_AttributesMapper() { attributesMapper.setExpectedAttributes(CN_SN_ATTRS); attributesMapper.setExpectedValues(CN_SN_VALUES); attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); - List list = tested.search(BASE_STRING, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper); + List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, + attributesMapper); assertEquals(1, list.size()); } + @Test public void testSearch_AttributesMapper_Name() { attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); attributesMapper.setExpectedValues(ALL_VALUES); @@ -110,23 +112,25 @@ public class LdapTemplateSearchResultITest extends assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_AttributesMapper_Name() { attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES); attributesMapper.setExpectedValues(ALL_VALUES); - List list = tested.search(BASE_NAME, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, attributesMapper); + List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper); assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_LimitedAttrs_AttributesMapper_Name() { attributesMapper.setExpectedAttributes(CN_SN_ATTRS); attributesMapper.setExpectedValues(CN_SN_VALUES); attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); - List list = tested.search(BASE_NAME, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper); + List list = tested + .search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper); assertEquals(1, list.size()); } + @Test public void testSearch_ContextMapper() { contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); contextMapper.setExpectedValues(ALL_VALUES); @@ -134,23 +138,24 @@ public class LdapTemplateSearchResultITest extends assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_ContextMapper() { contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); contextMapper.setExpectedValues(ALL_VALUES); - List list = tested.search(BASE_STRING, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, contextMapper); + List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper); assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_LimitedAttrs_ContextMapper() { contextMapper.setExpectedAttributes(CN_SN_ATTRS); contextMapper.setExpectedValues(CN_SN_VALUES); contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); - List list = tested.search(BASE_STRING, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper); + List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper); assertEquals(1, list.size()); } + @Test public void testSearch_ContextMapper_Name() { contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); contextMapper.setExpectedValues(ALL_VALUES); @@ -158,24 +163,20 @@ public class LdapTemplateSearchResultITest extends assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_ContextMapper_Name() { contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); contextMapper.setExpectedValues(ALL_VALUES); - List list = tested.search(BASE_NAME, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, contextMapper); + List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper); assertEquals(1, list.size()); } + @Test public void testSearch_SearchScope_LimitedAttrs_ContextMapper_Name() { contextMapper.setExpectedAttributes(CN_SN_ATTRS); contextMapper.setExpectedValues(CN_SN_VALUES); contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES); - List list = tested.search(BASE_NAME, FILTER_STRING, - SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper); + List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper); assertEquals(1, list.size()); } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java index 58880164..326e9469 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java @@ -16,55 +16,50 @@ package org.springframework.ldap.control; +import static junit.framework.Assert.assertEquals; + import java.util.Arrays; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.test.context.ContextConfiguration; /** * Provides tests that verify that the server supports certain controls. * * @author Ulrik Sandberg */ +@ContextConfiguration(locations = { "/conf/rootContextSourceTestContext.xml" }) public class SupportedControlsITest extends AbstractLdapTemplateIntegrationTest { - /** must use a context source that has no base set */ - private LdapTemplate tested; + /** must use a context source that has no base set */ + @Autowired + private LdapTemplate tested; - private static final String SUPPORTED_CONTROL = "supportedcontrol"; + private static final String SUPPORTED_CONTROL = "supportedcontrol"; - protected String[] getConfigLocations() { - return new String[] { "/conf/rootContextSourceTestContext.xml" }; - } + @Test + public void testExpectedControlsSupported() throws Exception { + /** + * Maps the 'supportedcontrol' attribute to a string array. + */ + ContextMapper mapper = new ContextMapper() { - public void testExpectedControlsSupported() throws Exception { - /** - * Maps the 'supportedcontrol' attribute to a string array. - */ - ContextMapper mapper = new ContextMapper() { + public Object mapFromContext(Object ctx) { + DirContextAdapter adapter = (DirContextAdapter) ctx; + return adapter.getStringAttributes(SUPPORTED_CONTROL); + } - public Object mapFromContext(Object ctx) { - DirContextAdapter adapter = (DirContextAdapter) ctx; - return adapter.getStringAttributes(SUPPORTED_CONTROL); - } + }; - }; - - String[] controls = (String[]) tested.lookup("", - new String[] { SUPPORTED_CONTROL }, mapper); - System.out.println(Arrays.toString(controls)); - assertEquals("Persistent Search LDAPv3 control,", - "2.16.840.1.113730.3.4.3", controls[0]); - assertEquals("Entry Change Notification LDAPv3 control,", - "2.16.840.1.113730.3.4.7", controls[1]); - assertEquals("Subentries Control,", "1.3.6.1.4.1.4203.1.10.1", - controls[2]); - assertEquals("Manage DSA IT LDAPv3 control,", - "2.16.840.1.113730.3.4.2", controls[3]); - } - - public void setTested(LdapTemplate tested) { - this.tested = tested; - } + String[] controls = (String[]) tested.lookup("", new String[] { SUPPORTED_CONTROL }, mapper); + System.out.println(Arrays.toString(controls)); + assertEquals("Persistent Search LDAPv3 control,", "2.16.840.1.113730.3.4.3", controls[0]); + assertEquals("Entry Change Notification LDAPv3 control,", "2.16.840.1.113730.3.4.7", controls[1]); + assertEquals("Subentries Control,", "1.3.6.1.4.1.4203.1.10.1", controls[2]); + assertEquals("Manage DSA IT LDAPv3 control,", "2.16.840.1.113730.3.4.2", controls[3]); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorITest.java index 4450e8f8..7f447fc6 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DistinguishedNameEditorITest.java @@ -15,29 +15,29 @@ */ package org.springframework.ldap.core; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; +import org.springframework.test.context.ContextConfiguration; /** * Integration tests for {@link DistinguishedNameEditor}. * * @author Mattias Arthursson */ -public class DistinguishedNameEditorITest extends AbstractDependencyInjectionSpringContextTests { +@ContextConfiguration(locations = { "/conf/distinguishedNameEditorTestContext.xml" }) +public class DistinguishedNameEditorITest extends AbstractLdapTemplateIntegrationTest { + @Autowired private DummyDistinguishedNameConsumer distinguishedNameConsumer; - protected String[] getConfigLocations() { - return new String[] { "classpath:conf/distinguishedNameEditorTestContext.xml" }; - } - + @Test public void testDistinguishedNameEditor() throws Exception { assertNotNull(distinguishedNameConsumer); DistinguishedName name = distinguishedNameConsumer.getDistinguishedName(); assertEquals(new DistinguishedName("dc=jayway, dc=se"), name); } - - public void setDistinguishedNameConsumer(DummyDistinguishedNameConsumer distinguishedNameConsumer) { - this.distinguishedNameConsumer = distinguishedNameConsumer; - } - } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DnParsePerformanceITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DnParsePerformanceITest.java index 6573942e..6a719ce8 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DnParsePerformanceITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/DnParsePerformanceITest.java @@ -16,52 +16,36 @@ package org.springframework.ldap.core; -import org.springframework.ldap.core.DistinguishedName; - -import junit.framework.Test; -import junit.framework.TestCase; - -import com.clarkware.junitperf.LoadTest; +import org.junit.Test; +import org.springframework.util.StopWatch; /** * Performance test for the {@link DistinguishedName} class. * * @author Ulrik Sandberg */ -public class DnParsePerformanceITest extends TestCase { +public class DnParsePerformanceITest { - public DnParsePerformanceITest(String name) { - super(name); - } + /** + * Tests parsing and toString. + */ + @Test + public void testContains() { + StopWatch stopWatch = new StopWatch("Dn Parse Performance"); + stopWatch.start(); - /** - * Tests parsing and toString. - */ - public void testContains() { + for (int i = 0; i < 2000; i++) { + DistinguishedName migpath = new DistinguishedName("OU=G,OU=I,OU=M"); + DistinguishedName path1 = new DistinguishedName("cn=john.doe, OU=Users,OU=SE,OU=G,OU=I,OU=M"); + DistinguishedName path2 = new DistinguishedName("cn=john.doe, OU=Users,OU=SE,ou=G,OU=i,OU=M, ou=foo"); + DistinguishedName path3 = new DistinguishedName("ou=G,OU=i,OU=M, ou=foo"); + DistinguishedName path4 = new DistinguishedName("ou=G,OU=i,ou=m"); - DistinguishedName migpath = new DistinguishedName("OU=G,OU=I,OU=M"); - DistinguishedName path1 = new DistinguishedName( - "cn=john.doe, OU=Users,OU=SE,OU=G,OU=I,OU=M"); - DistinguishedName path2 = new DistinguishedName( - "cn=john.doe, OU=Users,OU=SE,ou=G,OU=i,OU=M, ou=foo"); - DistinguishedName path3 = new DistinguishedName( - "ou=G,OU=i,OU=M, ou=foo"); - DistinguishedName path4 = new DistinguishedName("ou=G,OU=i,ou=m"); + DistinguishedName pathE1 = new DistinguishedName("cn=john.doe, OU=Users,OU=SE,ou=G,OU=L,OU=M, ou=foo"); + DistinguishedName pathE2 = new DistinguishedName("cn=john.doe, OU=Users,OU=SE"); + } - DistinguishedName pathE1 = new DistinguishedName( - "cn=john.doe, OU=Users,OU=SE,ou=G,OU=L,OU=M, ou=foo"); - DistinguishedName pathE2 = new DistinguishedName( - "cn=john.doe, OU=Users,OU=SE"); - } - - public static Test suite() { - int users = 1800; - Test testCase = new DnParsePerformanceITest("testContains"); - Test loadTest = new LoadTest(testCase, users); - return loadTest; - } - - public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); - } + stopWatch.stop(); + System.out.println(stopWatch.prettyPrint()); + } } \ No newline at end of file diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java index 2a2d3eed..566c9271 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java @@ -15,29 +15,30 @@ */ package org.springframework.ldap.core.simple; +import static junit.framework.Assert.assertEquals; + import java.util.List; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.ContextConfiguration; -public class SimpleLdapTemplateITest extends AbstractDependencyInjectionSpringContextTests { +@ContextConfiguration(locations = { "/conf/simpleLdapTemplateTestContext.xml" }) +public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest { + @Autowired private SimpleLdapTemplate ldapTemplate; - protected String[] getConfigLocations() { - return new String[] { "/conf/simpleLdapTemplateTestContext.xml" }; - } - - public void setSimpleLdapTemplate(SimpleLdapTemplate ldapTemplate) { - this.ldapTemplate = ldapTemplate; - } - + @Test public void testLookup() { String result = ldapTemplate.lookup("cn=Some Person,ou=company1,c=Sweden", new CnContextMapper()); assertEquals("Some Person", result); } + @Test public void testSearch() { List cns = ldapTemplate.search("", "(&(objectclass=person)(sn=Person3))", new CnContextMapper()); @@ -45,6 +46,7 @@ public class SimpleLdapTemplateITest extends AbstractDependencyInjectionSpringCo assertEquals("Some Person3", cns.get(0)); } + @Test public void testModifyAttributes() { DirContextOperations ctx = ldapTemplate.lookupContext("cn=Some Person,ou=company1,c=Sweden"); diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java index ea001ef6..5fb477d6 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java @@ -15,8 +15,12 @@ */ package org.springframework.ldap.core.support; -import junit.framework.TestCase; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; +import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -27,12 +31,9 @@ import org.springframework.ldap.core.DistinguishedName; * * @author Mattias Arthursson */ -public class BaseLdapPathBeanPostprocessorITest extends TestCase { - - protected String[] getConfigLocations() { - return new String[] { "/conf/baseLdapPathPostProcessorTestContext.xml" }; - } +public class BaseLdapPathBeanPostprocessorITest { + @Test public void testPostProcessBeforeInitialization() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/conf/baseLdapPathPostProcessorTestContext.xml"); @@ -43,6 +44,7 @@ public class BaseLdapPathBeanPostprocessorITest extends TestCase { assertEquals(new DistinguishedName("dc=jayway,dc=se"), base); } + @Test public void testPostProcessBeforeInitializationMultipleContextSources() throws Exception { try { new ClassPathXmlApplicationContext("/conf/baseLdapPathPostProcessorMultiContextSourceTestContext.xml"); @@ -54,6 +56,7 @@ public class BaseLdapPathBeanPostprocessorITest extends TestCase { } } + @Test public void testPostProcessBeforeInitializationMultipleContextSourcesOneSpecified() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml"); @@ -64,6 +67,7 @@ public class BaseLdapPathBeanPostprocessorITest extends TestCase { assertEquals(new DistinguishedName("cn=john doe,dc=jayway,dc=se"), base); } + @Test public void testPostProcessBeforeInitializationNoContextSource() throws Exception { try { new ClassPathXmlApplicationContext("/conf/baseLdapPathPostProcessorNoContextSourceTestContext.xml"); @@ -75,6 +79,7 @@ public class BaseLdapPathBeanPostprocessorITest extends TestCase { } } + @Test public void testPostProcessBeforeInitializationBaseSetInProperty() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/conf/baseLdapPathPostProcessorPropertyOverrideTestContext.xml"); @@ -85,6 +90,7 @@ public class BaseLdapPathBeanPostprocessorITest extends TestCase { assertEquals(new DistinguishedName("cn=john doe"), base); } + @Test public void testPostProcessBeforeInitializationTransactionProxy() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/conf/baseLdapPathPostProcessorTransactionTestContext.xml"); diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java index d1f5df7c..6b36e661 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java @@ -15,75 +15,79 @@ */ package org.springframework.ldap.core.support; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; + import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.DirContext; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.core.ContextSource; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.ContextConfiguration; /** * Integration tests for ContextSourceImpl. * * @author Mattias Arthursson */ -public class LdapContextSourcelITest extends - AbstractDependencyInjectionSpringContextTests { +@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) +public class LdapContextSourcelITest extends AbstractLdapTemplateIntegrationTest { - private ContextSource tested; + @Autowired + private ContextSource tested; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapTemplateTestContext.xml" }; - } + @Test + public void testGetReadOnlyContext() throws NamingException { + DirContext ctx = null; - public void testGetReadOnlyContext() throws NamingException { - DirContext ctx = null; + try { + ctx = tested.getReadOnlyContext(); + assertNotNull(ctx); + Hashtable environment = ctx.getEnvironment(); + assertTrue(environment.containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)); + assertTrue(environment.containsKey(Context.SECURITY_PRINCIPAL)); + assertTrue(environment.containsKey(Context.SECURITY_CREDENTIALS)); + } + finally { + // Always clean up. + if (ctx != null) { + try { + ctx.close(); + } + catch (Exception e) { + // Never mind this + } + } + } + } - try { - ctx = tested.getReadOnlyContext(); - assertNotNull(ctx); - Hashtable environment = ctx.getEnvironment(); - assertTrue(environment - .containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)); - assertTrue(environment.containsKey(Context.SECURITY_PRINCIPAL)); - assertTrue(environment.containsKey(Context.SECURITY_CREDENTIALS)); - } finally { - // Always clean up. - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this - } - } - } - } + @Test + public void testGetReadWriteContext() throws NamingException { + DirContext ctx = null; - public void testGetReadWriteContext() throws NamingException { - DirContext ctx = null; - - try { - ctx = tested.getReadWriteContext(); - assertNotNull(ctx); - // Double check to see that we are authenticated. - Hashtable environment = ctx.getEnvironment(); - assertTrue(environment.containsKey(Context.SECURITY_PRINCIPAL)); - assertTrue(environment.containsKey(Context.SECURITY_CREDENTIALS)); - } finally { - // Always clean up. - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this - } - } - } - } - - public void setTested(ContextSource tested) { - this.tested = tested; - } + try { + ctx = tested.getReadWriteContext(); + assertNotNull(ctx); + // Double check to see that we are authenticated. + Hashtable environment = ctx.getEnvironment(); + assertTrue(environment.containsKey(Context.SECURITY_PRINCIPAL)); + assertTrue(environment.containsKey(Context.SECURITY_CREDENTIALS)); + } + finally { + // Always clean up. + if (ctx != null) { + try { + ctx.close(); + } + catch (Exception e) { + // Never mind this + } + } + } + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java index caec3a68..a0074555 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java @@ -15,32 +15,31 @@ */ package org.springframework.ldap.core.support; +import static junit.framework.Assert.assertEquals; + import javax.naming.NamingException; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; +import org.springframework.test.context.ContextConfiguration; /** * Integration tests for ContextSourceImpl. * * @author Mattias Arthursson */ -public class LdapContextSourcelMultiServerITest extends - AbstractDependencyInjectionSpringContextTests { +@ContextConfiguration(locations = { "/conf/ldapContextSourceTestContext.xml" }) +public class LdapContextSourcelMultiServerITest extends AbstractLdapTemplateIntegrationTest { - private LdapContextSource tested; + @Autowired + private LdapContextSource tested; - protected String[] getConfigLocations() { - return new String[] { "/conf/ldapContextSourceTestContext.xml" }; - } + @Test + public void testUrls() throws NamingException { + String[] urls = tested.getUrls(); + String string = tested.assembleProviderUrlString(urls); - public void testUrls() throws NamingException { - String[] urls = tested.getUrls(); - String string = tested.assembleProviderUrlString(urls); - - assertEquals("ldap://127.0.0.1:389 ldap://127.0.0.2:389", string); - } - - public void setTested(LdapContextSource tested) { - this.tested = tested; - } + assertEquals("ldap://127.0.0.1:389 ldap://127.0.0.2:389", string); + } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java index ee844529..900a1425 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java @@ -15,6 +15,11 @@ */ package org.springframework.ldap.transaction.compensating.manager; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + import java.sql.ResultSet; import java.sql.SQLException; @@ -23,13 +28,19 @@ import javax.naming.directory.Attributes; 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.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.NameNotFoundException; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.support.TransactionSynchronizationManager; /** @@ -37,55 +48,48 @@ import org.springframework.transaction.support.TransactionSynchronizationManager * * @author Mattias Arthursson */ -public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends - AbstractDependencyInjectionSpringContextTests { +@ContextConfiguration(locations = { "/conf/ldapAndJdbcTransactionTestContext.xml" }) +public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends AbstractLdapTemplateIntegrationTest { - private static Log log = LogFactory - .getLog(ContextSourceAndDataSourceTransactionManagerIntegrationTest.class); + private static Log log = LogFactory.getLog(ContextSourceAndDataSourceTransactionManagerIntegrationTest.class); + @Autowired + @Qualifier("dummyDao") private DummyDao dummyDao; + @Autowired private LdapTemplate ldapTemplate; + @Autowired private JdbcTemplate jdbcTemplate; - public ContextSourceAndDataSourceTransactionManagerIntegrationTest() { - setAutowireMode(AbstractDependencyInjectionSpringContextTests.AUTOWIRE_BY_NAME); - } - - public void setLdapTemplate(LdapTemplate ldapTemplate) { - this.ldapTemplate = ldapTemplate; - } - public void setDummyDao(DummyDao dummyDaoImpl) { this.dummyDao = dummyDaoImpl; } - protected String[] getConfigLocations() { - return new String[] { "conf/ldapAndJdbcTransactionTestContext.xml" }; - } - - protected void onSetUp() throws Exception { + @Before + public void prepareTestedInstance() throws Exception { if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.clearSynchronization(); } - 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" }); + 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" }); } - protected void onTearDown() throws Exception { + @After + public void cleanup() throws Exception { jdbcTemplate.execute("drop table PERSON"); } + @Test public void testCreateWithException() { try { - dummyDao.createWithException("Sweden", "company1", - "some testperson", "testperson", "some description"); + dummyDao.createWithException("Sweden", "company1", "some testperson", "testperson", "some description"); fail("DummyException expected"); - } catch (DummyException expected) { + } + catch (DummyException expected) { assertTrue(true); } @@ -95,37 +99,33 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends try { ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); fail("NameNotFoundException expected"); - } catch (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; - } - }); + 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) { + } + catch (EmptyResultDataAccessException expected) { assertTrue(true); } } + @Test public void testCreate() { - dummyDao.create("Sweden", "company1", "some testperson", "testperson", - "some description"); + 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'", + 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 { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { return new Object(); } }); @@ -135,36 +135,32 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends 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"); + dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description"); fail("DummyException expected"); - } catch (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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); - assertEquals("Sweden, Company1, Some Person", attributes.get( - "description").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); - Object jdbcResult = jdbcTemplate.queryForObject( - "select * from PERSON where fullname=?", + Object jdbcResult = jdbcTemplate.queryForObject("select * from PERSON where fullname=?", new Object[] { "Some Person" }, new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) - throws SQLException { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { assertEquals("Person", rs.getString("lastname")); - assertEquals("Sweden, Company1, Some Person", rs - .getString("description")); + assertEquals("Sweden, Company1, Some Person", rs.getString("description")); return new Object(); } }); @@ -173,49 +169,44 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends assertNotNull(jdbcResult); } + @Test public void testUpdate() { String dn = "cn=Some Person,ou=company1,c=Sweden"; - dummyDao.update(dn, "Some Person", "Updated Person", - "Updated description"); + 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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Updated Person", attributes.get("sn").get()); - assertEquals("Updated description", attributes.get( - "description").get()); + assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); - Object jdbcResult = jdbcTemplate.queryForObject( - "select * from PERSON where fullname=?", + Object jdbcResult = jdbcTemplate.queryForObject("select * from PERSON where fullname=?", new Object[] { "Some Person" }, new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) - throws SQLException { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { assertEquals("Updated Person", rs.getString("lastname")); - assertEquals("Updated description", rs - .getString("description")); + assertEquals("Updated description", rs.getString("description")); return new Object(); } }); assertNotNull(ldapResult); assertNotNull(jdbcResult); - dummyDao.update(dn, "Some Person", "Person", - "Sweden, Company1, Some Person"); + 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"); + dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); fail("DummyException expected"); - } catch (DummyException expected) { + } + catch (DummyException expected) { assertTrue(true); } @@ -223,22 +214,22 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends try { ldapTemplate.lookup(newDn); fail("NameNotFoundException expected"); - } catch (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()); + 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"; @@ -247,10 +238,8 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends // 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()); + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); @@ -259,24 +248,23 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends 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"); + dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); fail("DummyException expected"); - } catch (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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); - assertEquals("Sweden, Company1, Some Person", attributes.get( - "description").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); @@ -284,19 +272,17 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends assertNotNull(result); } + @Test public void testModifyAttributes() { String dn = "cn=Some Person,ou=company1,c=Sweden"; // Perform test - dummyDao - .modifyAttributes(dn, "Updated lastname", "Updated description"); + 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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Updated lastname", attributes.get("sn").get()); - assertEquals("Updated description", attributes.get( - "description").get()); + assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); @@ -304,30 +290,29 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends assertNotNull(result); } + @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) { + } + 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 { + 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=?", + Object jdbcResult = jdbcTemplate.queryForObject("select * from PERSON where fullname=?", new Object[] { "Some Person" }, new RowMapper() { - public Object mapRow(ResultSet rs, int rowNum) - throws SQLException { + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // Just verify that the entry still exists. return new Object(); } @@ -337,6 +322,7 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends assertNotNull(jdbcResult); } + @Test public void testUnbind() { String dn = "cn=Some Person,ou=company1,c=Sweden"; // Perform test @@ -346,26 +332,22 @@ public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends // Verify result - check that the operation was not rolled back ldapTemplate.lookup(dn); fail("NameNotFoundException expected"); - } catch (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 { + 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) { + } + catch (EmptyResultDataAccessException expected) { assertTrue(true); } } - - public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } } diff --git a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerIntegrationTest.java b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerIntegrationTest.java index aaf94e12..e2c399cb 100644 --- a/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerIntegrationTest.java +++ b/mvn-build/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerIntegrationTest.java @@ -15,15 +15,25 @@ */ package org.springframework.ldap.transaction.compensating.manager; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + import javax.naming.NamingException; import javax.naming.directory.Attributes; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +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.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.NameNotFoundException; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.support.TransactionSynchronizationManager; /** @@ -31,47 +41,32 @@ import org.springframework.transaction.support.TransactionSynchronizationManager * * @author Mattias Arthursson */ -public class ContextSourceTransactionManagerIntegrationTest extends - AbstractDependencyInjectionSpringContextTests { +@ContextConfiguration(locations = { "/conf/ldapTemplateTransactionTestContext.xml" }) +public class ContextSourceTransactionManagerIntegrationTest extends AbstractLdapTemplateIntegrationTest { - private static Log log = LogFactory - .getLog(ContextSourceTransactionManagerIntegrationTest.class); - - public ContextSourceTransactionManagerIntegrationTest() { - setAutowireMode(AbstractDependencyInjectionSpringContextTests.AUTOWIRE_BY_NAME); - } + private static Log log = LogFactory.getLog(ContextSourceTransactionManagerIntegrationTest.class); + @Autowired + @Qualifier("dummyDao") private DummyDao dummyDao; + @Autowired private LdapTemplate ldapTemplate; - public void setLdapTemplate(LdapTemplate ldapTemplate) { - this.ldapTemplate = ldapTemplate; - } - - public void setDummyDao(DummyDao dummyDaoImpl) { - this.dummyDao = dummyDaoImpl; - } - - protected String[] getConfigLocations() { - return new String[] { "conf/ldapTemplateTransactionTestContext.xml" }; - } - - protected void onSetUp() throws Exception { + @Before + public void prepareTestedInstance() throws Exception { if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.clearSynchronization(); } } - protected void onTearDown() throws Exception { - } - + @Test public void testCreateWithException() { try { - dummyDao.createWithException("Sweden", "company1", - "some testperson", "testperson", "some description"); + dummyDao.createWithException("Sweden", "company1", "some testperson", "testperson", "some description"); fail("DummyException expected"); - } catch (DummyException expected) { + } + catch (DummyException expected) { assertTrue(true); } @@ -81,14 +76,15 @@ public class ContextSourceTransactionManagerIntegrationTest extends try { ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden"); fail("NameNotFoundException expected"); - } catch (NameNotFoundException expected) { + } + catch (NameNotFoundException expected) { assertTrue(true); } } + @Test public void testCreate() { - dummyDao.create("Sweden", "company1", "some testperson", "testperson", - "some description"); + dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); log.debug("Verifying result"); String expectedDn = "cn=some testperson, ou=company1, c=Sweden"; @@ -98,24 +94,23 @@ public class ContextSourceTransactionManagerIntegrationTest extends ldapTemplate.unbind(expectedDn); } + @Test public void testUpdateWithException() { String dn = "cn=Some Person,ou=company1,c=Sweden"; try { - dummyDao.updateWithException(dn, "Some Person", "Updated Person", - "Updated description"); + dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description"); fail("DummyException expected"); - } catch (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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); - assertEquals("Sweden, Company1, Some Person", attributes.get( - "description").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); @@ -123,37 +118,35 @@ public class ContextSourceTransactionManagerIntegrationTest extends assertNotNull(ldapResult); } + @Test public void testUpdate() { String dn = "cn=Some Person,ou=company1,c=Sweden"; - dummyDao.update(dn, "Some Person", "Updated Person", - "Updated description"); + 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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Updated Person", attributes.get("sn").get()); - assertEquals("Updated description", attributes.get( - "description").get()); + assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); assertNotNull(ldapResult); - dummyDao.update(dn, "Some Person", "Person", - "Sweden, Company1, Some Person"); + 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"); + dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); fail("DummyException expected"); - } catch (DummyException expected) { + } + catch (DummyException expected) { assertTrue(true); } @@ -161,22 +154,22 @@ public class ContextSourceTransactionManagerIntegrationTest extends try { ldapTemplate.lookup(newDn); fail("NameNotFoundException expected"); - } catch (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()); + 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"; @@ -185,10 +178,8 @@ public class ContextSourceTransactionManagerIntegrationTest extends // 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()); + public Object mapFromAttributes(Attributes attributes) throws NamingException { + assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); @@ -197,24 +188,23 @@ public class ContextSourceTransactionManagerIntegrationTest extends 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"); + dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); fail("DummyException expected"); - } catch (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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); - assertEquals("Sweden, Company1, Some Person", attributes.get( - "description").get()); + assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); @@ -222,19 +212,17 @@ public class ContextSourceTransactionManagerIntegrationTest extends assertNotNull(result); } + @Test public void testModifyAttributes() { String dn = "cn=Some Person,ou=company1,c=Sweden"; // Perform test - dummyDao - .modifyAttributes(dn, "Updated lastname", "Updated description"); + 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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Updated lastname", attributes.get("sn").get()); - assertEquals("Updated description", attributes.get( - "description").get()); + assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); @@ -242,20 +230,21 @@ public class ContextSourceTransactionManagerIntegrationTest extends assertNotNull(result); } + @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) { + } + 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 { + public Object mapFromAttributes(Attributes attributes) throws NamingException { // Just verify that the entry still exists. return new Object(); } @@ -264,6 +253,7 @@ public class ContextSourceTransactionManagerIntegrationTest extends assertNotNull(ldapResult); } + @Test public void testUnbind() { String dn = "cn=Some Person,ou=company1,c=Sweden"; // Perform test @@ -273,7 +263,8 @@ public class ContextSourceTransactionManagerIntegrationTest extends // Verify result - check that the operation was not rolled back ldapTemplate.lookup(dn); fail("NameNotFoundException expected"); - } catch (NameNotFoundException expected) { + } + catch (NameNotFoundException expected) { assertTrue(true); }