diff --git a/test-support/build.gradle b/test-support/build.gradle index db86a010b..7f5da2c56 100644 --- a/test-support/build.gradle +++ b/test-support/build.gradle @@ -21,8 +21,7 @@ dependencies { optional "org.apache.directory.shared:shared-ldap" optional "com.unboundid:unboundid-ldapsdk" - provided "junit:junit" - testImplementation platform('org.junit:junit-bom') - testImplementation "org.junit.vintage:junit-vintage-engine" + testImplementation "org.junit.jupiter:junit-jupiter-engine" + testRuntimeOnly "org.junit.platform:junit-platform-launcher" testImplementation "org.assertj:assertj-core" } diff --git a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java index 3ef90e386..ca6bbc5eb 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java +++ b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java @@ -22,9 +22,8 @@ import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; -import junit.framework.Assert; - import org.springframework.ldap.core.AttributesMapper; +import org.springframework.util.Assert; /** * Dummy AttributesMapper for testing purposes to check that the received Attributes are @@ -41,16 +40,18 @@ public class AttributeCheckAttributesMapper implements AttributesMapper private String[] absentAttributes = new String[0]; public Object mapFromAttributes(Attributes attributes) throws NamingException { - Assert.assertEquals("Values and attributes need to have the same length ", this.expectedAttributes.length, - this.expectedValues.length); + Assert.isTrue(this.expectedAttributes.length == this.expectedValues.length, + "Values and attributes need to have the same length"); for (int i = 0; i < this.expectedAttributes.length; i++) { Attribute attribute = attributes.get(this.expectedAttributes[i]); - Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attribute); - Assert.assertEquals(this.expectedValues[i], attribute.get()); + Assert.notNull(attribute, "Attribute " + this.expectedAttributes[i] + " was not present"); + Object value = attribute.get(); + Assert.isTrue(this.expectedValues[i].equals(value), + "Expected '" + this.expectedValues[i] + "' but got '" + value + "'"); } for (String absentAttribute : this.absentAttributes) { - Assert.assertNull(attributes.get(absentAttribute)); + Assert.isNull(attributes.get(absentAttribute), "Attribute '" + absentAttribute + "' should not be present"); } return new Object(); diff --git a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java index 25efbab5c..0b595f23a 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java +++ b/test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java @@ -18,10 +18,9 @@ import java.util.Arrays; -import junit.framework.Assert; - import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.util.Assert; /** * Dummy ContextMapper for testing purposes to check that the received Attributes are the @@ -39,16 +38,18 @@ public class AttributeCheckContextMapper implements ContextMapper list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), - new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); - assertThat(5).isEqualTo(list.size()); + this::commonNameAttribute); + assertThat(list).hasSize(5); + } + + private String commonNameAttribute(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); } } diff --git a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java index a05c8213b..097de33b9 100644 --- a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java +++ b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java @@ -21,40 +21,33 @@ import javax.naming.NamingException; import javax.naming.directory.Attributes; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AutoClose; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.query.LdapQueryBuilder; import static org.assertj.core.api.Assertions.assertThat; -public class EmbeddedLdapServerFactoryBeanTests { +class EmbeddedLdapServerFactoryBeanTests { + @AutoClose ClassPathXmlApplicationContext ctx; - @After - public void setup() { - if (this.ctx != null) { - this.ctx.close(); - } - } - @Test - public void testServerStartup() throws Exception { + void testServerStartup() { this.ctx = new ClassPathXmlApplicationContext("/applicationContext-ldifPopulator.xml"); LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class); assertThat(ldapTemplate).isNotNull(); List list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), - new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); - assertThat(list.size()).isEqualTo(5); + this::commonNameAttribute); + assertThat(list).hasSize(5); + } + + private String commonNameAttribute(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); } } diff --git a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java index 1e2e5dfda..ed7c89bf0 100644 --- a/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java +++ b/test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java @@ -28,10 +28,9 @@ import com.unboundid.ldap.listener.InMemoryDirectoryServer; import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; import com.unboundid.ldap.listener.InMemoryListenerConfig; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.query.LdapQueryBuilder; @@ -39,17 +38,17 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -public class EmbeddedLdapServerTests { +class EmbeddedLdapServerTests { private int port; - @Before - public void setUp() throws IOException { + @BeforeEach + void setUp() throws IOException { this.port = getFreePort(); } @Test - public void shouldStartAndCloseServer() throws Exception { + void shouldStartAndCloseServer() throws Exception { assertPortIsFree(this.port); EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port); @@ -60,7 +59,7 @@ public void shouldStartAndCloseServer() throws Exception { } @Test - public void shouldStartAndAutoCloseServer() throws Exception { + void shouldStartAndAutoCloseServer() throws Exception { assertPortIsFree(this.port); try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", @@ -71,7 +70,7 @@ public void shouldStartAndAutoCloseServer() throws Exception { } @Test - public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception { + void shouldStartAndCloseServerViaLdapTestUtils() throws Exception { assertPortIsFree(this.port); LdapTestUtils.startEmbeddedServer(this.port, "dc=jayway,dc=se", "jayway"); @@ -82,13 +81,13 @@ public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception { } @Test - public void startWhenNewEmbeddedServerThenException() throws Exception { + void startWhenNewEmbeddedServerThenException() throws Exception { EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start); } @Test - public void startWhenUnstartedThenWorks() throws Exception { + void startWhenUnstartedThenWorks() throws Exception { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se"); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config); @@ -99,7 +98,7 @@ public void startWhenUnstartedThenWorks() throws Exception { } @Test - public void startWhenAlreadyStartedThenFails() throws Exception { + void startWhenAlreadyStartedThenFails() throws Exception { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se"); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config); @@ -111,13 +110,13 @@ public void startWhenAlreadyStartedThenFails() throws Exception { } @Test - public void shouldBuildButNotStartTheServer() { + void shouldBuildButNotStartTheServer() { EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(this.port).build(); assertPortIsFree(this.port); } @Test - public void shouldBuildTheServerWithCustomPort() { + void shouldBuildTheServerWithCustomPort() { EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se") .port(this.port); @@ -129,7 +128,7 @@ public void shouldBuildTheServerWithCustomPort() { } @Test - public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException { + void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException { String tempLogFile = Files.createTempFile("ldap-log-", ".txt").toAbsolutePath().toString(); EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se") @@ -140,11 +139,7 @@ public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOExceptio server.start(); ldapTemplate("dc=jayway,dc=se", this.port) - .search(LdapQueryBuilder.query().where("objectclass").is("person"), new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); + .search(LdapQueryBuilder.query().where("objectclass").is("person"), this::commonNameAttribute); } assertThat(Path.of(tempLogFile)) @@ -152,6 +147,10 @@ public String mapFromAttributes(Attributes attrs) throws NamingException { .isNotEmptyFile(); } + private String commonNameAttribute(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); + } + static void assertPortIsFree(int port) { assertThat(isPortOpen(port)).isFalse(); } diff --git a/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java b/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java index 505094571..fba2f1c4d 100644 --- a/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java +++ b/test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java @@ -21,40 +21,33 @@ import javax.naming.NamingException; import javax.naming.directory.Attributes; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AutoClose; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.query.LdapQueryBuilder; import static org.assertj.core.api.Assertions.assertThat; -public class TestContextSourceFactoryBeanTests { +class TestContextSourceFactoryBeanTests { + @AutoClose ClassPathXmlApplicationContext ctx; - @After - public void setup() { - if (this.ctx != null) { - this.ctx.close(); - } - } - @Test - public void testServerStartup() throws Exception { + void testServerStartup() { this.ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml"); LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class); assertThat(ldapTemplate).isNotNull(); List list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), - new AttributesMapper<>() { - public String mapFromAttributes(Attributes attrs) throws NamingException { - return (String) attrs.get("cn").get(); - } - }); - assertThat(list.size()).isEqualTo(5); + this::commonNameAttribute); + assertThat(list).hasSize(5); + } + + private String commonNameAttribute(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); } }