From ca10190bd22cd366e6bdae0d46d27e5ed388c44c Mon Sep 17 00:00:00 2001 From: etrandafir93 Date: Sat, 10 May 2025 14:58:01 +0300 Subject: [PATCH 1/5] test-support module to JUnit5 Issue #1058 Signed-off-by: etrandafir93 --- build.gradle | 9 ++++++ buildSrc/build.gradle | 6 ++-- gradle.properties | 1 + test-support/build.gradle | 8 ++++-- .../test/AttributeCheckAttributesMapper.java | 17 ++++++----- .../test/AttributeCheckContextMapper.java | 18 +++++++----- .../EmbeddedLdapServerFactoryBeanTests.java | 8 +++--- .../EmbeddedLdapServerFactoryBeanTests.java | 12 ++++---- .../unboundid/EmbeddedLdapServerTests.java | 28 +++++++++---------- .../TestContextSourceFactoryBeanTests.java | 12 ++++---- 10 files changed, 69 insertions(+), 50 deletions(-) diff --git a/build.gradle b/build.gradle index a6c5f9faf..2b8480c0e 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ buildscript { plugins { id 'io.spring.antora.generate-antora-yml' version '0.0.1' id 'org.antora' version '1.0.0' + id("org.openrewrite.rewrite") version "7.6.1" } apply plugin: 'io.spring.convention.root' @@ -104,3 +105,11 @@ allprojects { } } } + +rewrite { + activeRecipe("org.openrewrite.java.testing.junit5.JUnit5BestPractices") + dependencies { + rewrite "org.openrewrite.recipe:rewrite-recipe-bom:3.8.0" + rewrite "org.openrewrite.recipe:rewrite-testing-frameworks:3.8.0" + } +} \ No newline at end of file diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 513eec49e..f9aa43e2d 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -105,9 +105,9 @@ dependencies { testImplementation "org.junit.jupiter:junit-jupiter-engine" testImplementation 'org.apache.commons:commons-io:1.3.2' testImplementation 'org.assertj:assertj-core:3.27.3' - testImplementation 'org.mockito:mockito-core:3.12.4' - testImplementation 'org.mockito:mockito-junit-jupiter:3.12.4' - testImplementation "com.squareup.okhttp3:mockwebserver:3.14.9" + testImplementation 'org.mockito:mockito-core:4.11.0' + testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0' + testImplementation "com.squareup.okhttp3:mockwebserver:4.12.0" } diff --git a/gradle.properties b/gradle.properties index 83cd7b533..d5e51ce4a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,4 @@ version=3.3.0-SNAPSHOT springJavaformatVersion=0.0.38 org.gradle.caching=true +org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m \ No newline at end of file diff --git a/test-support/build.gradle b/test-support/build.gradle index db86a010b..d4db0925e 100644 --- a/test-support/build.gradle +++ b/test-support/build.gradle @@ -13,6 +13,8 @@ dependencies { implementation "com.google.code.typica:typica" + implementation "org.junit.jupiter:junit-jupiter:5.12.2" + optional "org.apache.directory.server:apacheds-core-entry" optional "org.apache.directory.server:apacheds-core" optional "org.apache.directory.server:apacheds-protocol-ldap" @@ -20,9 +22,9 @@ dependencies { optional "org.apache.directory.server:apacheds-server-jndi" 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.assertj:assertj-core" } +tasks.withType(Test).configureEach { + useJUnitPlatform() +} 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..fbd6beb20 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,10 +22,12 @@ import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; -import junit.framework.Assert; - import org.springframework.ldap.core.AttributesMapper; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + /** * Dummy AttributesMapper for testing purposes to check that the received Attributes are * the expected ones. @@ -41,16 +43,17 @@ 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); + assertEquals(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()); + assertNotNull(attribute, "Attribute " + this.expectedAttributes[i] + " was not present"); + assertEquals(this.expectedValues[i], attribute.get()); } for (String absentAttribute : this.absentAttributes) { - Assert.assertNull(attributes.get(absentAttribute)); + assertNull(attributes.get(absentAttribute)); } 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..bf5076403 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,11 +18,14 @@ import java.util.Arrays; -import junit.framework.Assert; - import org.springframework.ldap.core.ContextMapper; + import org.springframework.ldap.core.DirContextAdapter; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + /** * Dummy ContextMapper for testing purposes to check that the received Attributes are the * expected ones. @@ -39,16 +42,17 @@ public class AttributeCheckContextMapper implements ContextMapper Date: Sat, 10 May 2025 15:41:21 +0300 Subject: [PATCH 2/5] formatting and fixes Issue #1058 Signed-off-by: etrandafir93 --- .../test/AttributeCheckAttributesMapper.java | 17 +++++++---------- .../ldap/test/AttributeCheckContextMapper.java | 18 +++++++----------- .../EmbeddedLdapServerFactoryBeanTests.java | 15 +++++++-------- .../EmbeddedLdapServerFactoryBeanTests.java | 11 +++++------ .../unboundid/EmbeddedLdapServerTests.java | 11 +++++------ .../TestContextSourceFactoryBeanTests.java | 11 +++++------ 6 files changed, 36 insertions(+), 47 deletions(-) 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 fbd6beb20..3ef90e386 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,11 +22,9 @@ import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; -import org.springframework.ldap.core.AttributesMapper; +import junit.framework.Assert; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; +import org.springframework.ldap.core.AttributesMapper; /** * Dummy AttributesMapper for testing purposes to check that the received Attributes are @@ -43,17 +41,16 @@ public class AttributeCheckAttributesMapper implements AttributesMapper private String[] absentAttributes = new String[0]; public Object mapFromAttributes(Attributes attributes) throws NamingException { - assertEquals(this.expectedAttributes.length, - this.expectedValues.length, - "Values and attributes need to have the same length "); + Assert.assertEquals("Values and attributes need to have the same length ", this.expectedAttributes.length, + this.expectedValues.length); for (int i = 0; i < this.expectedAttributes.length; i++) { Attribute attribute = attributes.get(this.expectedAttributes[i]); - assertNotNull(attribute, "Attribute " + this.expectedAttributes[i] + " was not present"); - assertEquals(this.expectedValues[i], attribute.get()); + Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attribute); + Assert.assertEquals(this.expectedValues[i], attribute.get()); } for (String absentAttribute : this.absentAttributes) { - assertNull(attributes.get(absentAttribute)); + Assert.assertNull(attributes.get(absentAttribute)); } 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 bf5076403..25efbab5c 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,14 +18,11 @@ import java.util.Arrays; -import org.springframework.ldap.core.ContextMapper; +import junit.framework.Assert; +import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - /** * Dummy ContextMapper for testing purposes to check that the received Attributes are the * expected ones. @@ -42,17 +39,16 @@ 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(); - } - }); + this::mapFromAttributes); assertThat(5).isEqualTo(list.size()); } + private String mapFromAttributes(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 216243b02..ba9057c82 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 @@ -25,7 +25,6 @@ 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; @@ -49,12 +48,12 @@ void serverStartup() throws Exception { 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(); - } - }); + this::mapFromAttributes); assertThat(list.size()).isEqualTo(5); } + private String mapFromAttributes(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 745537bff..f16e7a8a0 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 @@ -31,7 +31,6 @@ 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; @@ -140,11 +139,7 @@ void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException { 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::mapFromAttributes); } assertThat(Path.of(tempLogFile)) @@ -185,4 +180,8 @@ static LdapTemplate ldapTemplate(String base, int port) { return new LdapTemplate(ctx); } + private String mapFromAttributes(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); + } + } 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 9380a3efe..5738f6c48 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 @@ -25,7 +25,6 @@ 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; @@ -49,12 +48,12 @@ void serverStartup() throws Exception { 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(); - } - }); + this::mapFromAttributes); assertThat(list.size()).isEqualTo(5); } + private String mapFromAttributes(Attributes attrs) throws NamingException { + return (String) attrs.get("cn").get(); + } + } From eb472b73b32621187cd2ec2ca3855e7368850a74 Mon Sep 17 00:00:00 2001 From: etrandafir93 Date: Sat, 10 May 2025 16:04:18 +0300 Subject: [PATCH 3/5] swap Assert imports Issue #1058 Signed-off-by: etrandafir93 --- .../test/AttributeCheckAttributesMapper.java | 18 +++++++++++------- .../ldap/test/AttributeCheckContextMapper.java | 17 ++++++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) 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..113417788 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,21 @@ 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 " + this.expectedAttributes.length + "!=" + + this.expectedValues.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"); + + Assert.isTrue(attribute.get().equals(this.expectedValues[i]), "Attribute " + this.expectedAttributes[i] + + " had value " + attribute.get() + " instead of " + this.expectedValues[i]); } for (String absentAttribute : this.absentAttributes) { - Assert.assertNull(attributes.get(absentAttribute)); + Assert.isNull(attributes.get(absentAttribute), "Attribute " + absentAttribute + " was 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..444ed8a52 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,20 @@ public class AttributeCheckContextMapper implements ContextMapper Date: Sat, 10 May 2025 16:06:12 +0300 Subject: [PATCH 4/5] polish assertj assertions Issue #1058 Signed-off-by: etrandafir93 --- .../ldap/test/EmbeddedLdapServerFactoryBeanTests.java | 2 +- .../ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java | 2 +- .../ldap/test/unboundid/TestContextSourceFactoryBeanTests.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test-support/src/test/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBeanTests.java b/test-support/src/test/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBeanTests.java index ddaf26a7d..49418f1eb 100644 --- a/test-support/src/test/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBeanTests.java +++ b/test-support/src/test/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBeanTests.java @@ -39,7 +39,7 @@ void serverStartup() throws Exception { List list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), this::mapFromAttributes); - assertThat(5).isEqualTo(list.size()); + assertThat(list).hasSize(5); } private String mapFromAttributes(Attributes attrs) throws NamingException { 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 ba9057c82..01c834a21 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 @@ -49,7 +49,7 @@ void serverStartup() throws Exception { List list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), this::mapFromAttributes); - assertThat(list.size()).isEqualTo(5); + assertThat(list).hasSize(5); } private String mapFromAttributes(Attributes attrs) throws NamingException { 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 5738f6c48..e68360db5 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 @@ -49,7 +49,7 @@ void serverStartup() throws Exception { List list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"), this::mapFromAttributes); - assertThat(list.size()).isEqualTo(5); + assertThat(list).hasSize(5); } private String mapFromAttributes(Attributes attrs) throws NamingException { From a42332e3d61f000fc425236be0eda713c3611b82 Mon Sep 17 00:00:00 2001 From: etrandafir93 Date: Sat, 10 May 2025 16:14:45 +0300 Subject: [PATCH 5/5] cleanup Issue #1058 Signed-off-by: etrandafir93 --- build.gradle | 9 --------- gradle.properties | 1 - test-support/build.gradle | 3 +-- 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 2b8480c0e..a6c5f9faf 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,6 @@ buildscript { plugins { id 'io.spring.antora.generate-antora-yml' version '0.0.1' id 'org.antora' version '1.0.0' - id("org.openrewrite.rewrite") version "7.6.1" } apply plugin: 'io.spring.convention.root' @@ -105,11 +104,3 @@ allprojects { } } } - -rewrite { - activeRecipe("org.openrewrite.java.testing.junit5.JUnit5BestPractices") - dependencies { - rewrite "org.openrewrite.recipe:rewrite-recipe-bom:3.8.0" - rewrite "org.openrewrite.recipe:rewrite-testing-frameworks:3.8.0" - } -} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index d5e51ce4a..83cd7b533 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,3 @@ version=3.3.0-SNAPSHOT springJavaformatVersion=0.0.38 org.gradle.caching=true -org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m \ No newline at end of file diff --git a/test-support/build.gradle b/test-support/build.gradle index d4db0925e..769cebf77 100644 --- a/test-support/build.gradle +++ b/test-support/build.gradle @@ -13,8 +13,6 @@ dependencies { implementation "com.google.code.typica:typica" - implementation "org.junit.jupiter:junit-jupiter:5.12.2" - optional "org.apache.directory.server:apacheds-core-entry" optional "org.apache.directory.server:apacheds-core" optional "org.apache.directory.server:apacheds-protocol-ldap" @@ -24,6 +22,7 @@ dependencies { optional "com.unboundid:unboundid-ldapsdk" testImplementation platform('org.junit:junit-bom') testImplementation "org.assertj:assertj-core" + testImplementation "org.junit.jupiter:junit-jupiter:5.12.2" } tasks.withType(Test).configureEach { useJUnitPlatform()