Skip to content

Commit ec9e5c0

Browse files
etrandafir93jzheaux
authored andcommitted
Migrate test-support to JUnit 5
Issue gh-1058 Signed-off-by: etrandafir93 <emanueltrandafir1993@gmail.com>
1 parent ae34a65 commit ec9e5c0

File tree

7 files changed

+68
-83
lines changed

7 files changed

+68
-83
lines changed

test-support/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ dependencies {
2121
optional "org.apache.directory.shared:shared-ldap"
2222
optional "com.unboundid:unboundid-ldapsdk"
2323

24-
provided "junit:junit"
25-
testImplementation platform('org.junit:junit-bom')
26-
testImplementation "org.junit.vintage:junit-vintage-engine"
24+
testImplementation "org.junit.jupiter:junit-jupiter-engine"
25+
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
2726
testImplementation "org.assertj:assertj-core"
2827
}

test-support/src/main/java/org/springframework/ldap/test/AttributeCheckAttributesMapper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import javax.naming.directory.Attribute;
2323
import javax.naming.directory.Attributes;
2424

25-
import junit.framework.Assert;
26-
2725
import org.springframework.ldap.core.AttributesMapper;
26+
import org.springframework.util.Assert;
2827

2928
/**
3029
* Dummy AttributesMapper for testing purposes to check that the received Attributes are
@@ -41,16 +40,18 @@ public class AttributeCheckAttributesMapper implements AttributesMapper<Object>
4140
private String[] absentAttributes = new String[0];
4241

4342
public Object mapFromAttributes(Attributes attributes) throws NamingException {
44-
Assert.assertEquals("Values and attributes need to have the same length ", this.expectedAttributes.length,
45-
this.expectedValues.length);
43+
Assert.isTrue(this.expectedAttributes.length == this.expectedValues.length,
44+
"Values and attributes need to have the same length");
4645
for (int i = 0; i < this.expectedAttributes.length; i++) {
4746
Attribute attribute = attributes.get(this.expectedAttributes[i]);
48-
Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attribute);
49-
Assert.assertEquals(this.expectedValues[i], attribute.get());
47+
Assert.notNull(attribute, "Attribute " + this.expectedAttributes[i] + " was not present");
48+
Object value = attribute.get();
49+
Assert.isTrue(this.expectedValues[i].equals(value),
50+
"Expected '" + this.expectedValues[i] + "' but got '" + value + "'");
5051
}
5152

5253
for (String absentAttribute : this.absentAttributes) {
53-
Assert.assertNull(attributes.get(absentAttribute));
54+
Assert.isNull(attributes.get(absentAttribute), "Attribute '" + absentAttribute + "' should not be present");
5455
}
5556

5657
return new Object();

test-support/src/main/java/org/springframework/ldap/test/AttributeCheckContextMapper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
import java.util.Arrays;
2020

21-
import junit.framework.Assert;
22-
2321
import org.springframework.ldap.core.ContextMapper;
2422
import org.springframework.ldap.core.DirContextAdapter;
23+
import org.springframework.util.Assert;
2524

2625
/**
2726
* Dummy ContextMapper for testing purposes to check that the received Attributes are the
@@ -39,16 +38,18 @@ public class AttributeCheckContextMapper implements ContextMapper<DirContextAdap
3938

4039
public DirContextAdapter mapFromContext(Object ctx) {
4140
DirContextAdapter adapter = (DirContextAdapter) ctx;
42-
Assert.assertEquals("Values and attributes need to have the same length ", this.expectedAttributes.length,
43-
this.expectedValues.length);
41+
Assert.isTrue(this.expectedAttributes.length == this.expectedValues.length,
42+
"Values and attributes need to have the same length");
4443
for (int i = 0; i < this.expectedAttributes.length; i++) {
4544
String attributeValue = adapter.getStringAttribute(this.expectedAttributes[i]);
46-
Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attributeValue);
47-
Assert.assertEquals(this.expectedValues[i], attributeValue);
45+
Assert.notNull(attributeValue, "Attribute " + this.expectedAttributes[i] + " was not present");
46+
Assert.isTrue(this.expectedValues[i].equals(attributeValue),
47+
"Expected '" + this.expectedValues[i] + "' but got '" + attributeValue + "'");
4848
}
4949

5050
for (String absentAttribute : this.absentAttributes) {
51-
Assert.assertNull(adapter.getStringAttribute(absentAttribute));
51+
Assert.isNull(adapter.getStringAttribute(absentAttribute),
52+
"Attribute '" + absentAttribute + "' should not be present");
5253
}
5354

5455
return adapter;

test-support/src/test/java/org/springframework/ldap/test/EmbeddedLdapServerFactoryBeanTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,29 @@
2121
import javax.naming.NamingException;
2222
import javax.naming.directory.Attributes;
2323

24-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.context.support.ClassPathXmlApplicationContext;
27-
import org.springframework.ldap.core.AttributesMapper;
2827
import org.springframework.ldap.core.LdapTemplate;
2928
import org.springframework.ldap.query.LdapQueryBuilder;
3029

3130
import static org.assertj.core.api.Assertions.assertThat;
3231

33-
public class EmbeddedLdapServerFactoryBeanTests {
32+
class EmbeddedLdapServerFactoryBeanTests {
3433

3534
@Test
36-
public void testServerStartup() throws Exception {
35+
void testServerStartup() {
3736
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
3837
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
3938
assertThat(ldapTemplate).isNotNull();
4039

4140
List<String> list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"),
42-
new AttributesMapper<>() {
43-
public String mapFromAttributes(Attributes attrs) throws NamingException {
44-
return (String) attrs.get("cn").get();
45-
}
46-
});
47-
assertThat(5).isEqualTo(list.size());
41+
this::commonNameAttribute);
42+
assertThat(list).hasSize(5);
43+
}
44+
45+
private String commonNameAttribute(Attributes attrs) throws NamingException {
46+
return (String) attrs.get("cn").get();
4847
}
4948

5049
}

test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBeanTests.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,33 @@
2121
import javax.naming.NamingException;
2222
import javax.naming.directory.Attributes;
2323

24-
import org.junit.After;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.AutoClose;
25+
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.context.support.ClassPathXmlApplicationContext;
28-
import org.springframework.ldap.core.AttributesMapper;
2928
import org.springframework.ldap.core.LdapTemplate;
3029
import org.springframework.ldap.query.LdapQueryBuilder;
3130

3231
import static org.assertj.core.api.Assertions.assertThat;
3332

34-
public class EmbeddedLdapServerFactoryBeanTests {
33+
class EmbeddedLdapServerFactoryBeanTests {
3534

35+
@AutoClose
3636
ClassPathXmlApplicationContext ctx;
3737

38-
@After
39-
public void setup() {
40-
if (this.ctx != null) {
41-
this.ctx.close();
42-
}
43-
}
44-
4538
@Test
46-
public void testServerStartup() throws Exception {
39+
void testServerStartup() {
4740
this.ctx = new ClassPathXmlApplicationContext("/applicationContext-ldifPopulator.xml");
4841
LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class);
4942
assertThat(ldapTemplate).isNotNull();
5043

5144
List<String> list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"),
52-
new AttributesMapper<>() {
53-
public String mapFromAttributes(Attributes attrs) throws NamingException {
54-
return (String) attrs.get("cn").get();
55-
}
56-
});
57-
assertThat(list.size()).isEqualTo(5);
45+
this::commonNameAttribute);
46+
assertThat(list).hasSize(5);
47+
}
48+
49+
private String commonNameAttribute(Attributes attrs) throws NamingException {
50+
return (String) attrs.get("cn").get();
5851
}
5952

6053
}

test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,27 @@
2828
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
2929
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
3030
import com.unboundid.ldap.listener.InMemoryListenerConfig;
31-
import org.junit.Before;
32-
import org.junit.Test;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
3333

34-
import org.springframework.ldap.core.AttributesMapper;
3534
import org.springframework.ldap.core.LdapTemplate;
3635
import org.springframework.ldap.core.support.LdapContextSource;
3736
import org.springframework.ldap.query.LdapQueryBuilder;
3837

3938
import static org.assertj.core.api.Assertions.assertThat;
4039
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4140

42-
public class EmbeddedLdapServerTests {
41+
class EmbeddedLdapServerTests {
4342

4443
private int port;
4544

46-
@Before
47-
public void setUp() throws IOException {
45+
@BeforeEach
46+
void setUp() throws IOException {
4847
this.port = getFreePort();
4948
}
5049

5150
@Test
52-
public void shouldStartAndCloseServer() throws Exception {
51+
void shouldStartAndCloseServer() throws Exception {
5352
assertPortIsFree(this.port);
5453

5554
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port);
@@ -60,7 +59,7 @@ public void shouldStartAndCloseServer() throws Exception {
6059
}
6160

6261
@Test
63-
public void shouldStartAndAutoCloseServer() throws Exception {
62+
void shouldStartAndAutoCloseServer() throws Exception {
6463
assertPortIsFree(this.port);
6564

6665
try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se",
@@ -71,7 +70,7 @@ public void shouldStartAndAutoCloseServer() throws Exception {
7170
}
7271

7372
@Test
74-
public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
73+
void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
7574
assertPortIsFree(this.port);
7675

7776
LdapTestUtils.startEmbeddedServer(this.port, "dc=jayway,dc=se", "jayway");
@@ -82,13 +81,13 @@ public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
8281
}
8382

8483
@Test
85-
public void startWhenNewEmbeddedServerThenException() throws Exception {
84+
void startWhenNewEmbeddedServerThenException() throws Exception {
8685
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port);
8786
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
8887
}
8988

9089
@Test
91-
public void startWhenUnstartedThenWorks() throws Exception {
90+
void startWhenUnstartedThenWorks() throws Exception {
9291
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
9392
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
9493
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
@@ -99,7 +98,7 @@ public void startWhenUnstartedThenWorks() throws Exception {
9998
}
10099

101100
@Test
102-
public void startWhenAlreadyStartedThenFails() throws Exception {
101+
void startWhenAlreadyStartedThenFails() throws Exception {
103102
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
104103
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
105104
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
@@ -111,13 +110,13 @@ public void startWhenAlreadyStartedThenFails() throws Exception {
111110
}
112111

113112
@Test
114-
public void shouldBuildButNotStartTheServer() {
113+
void shouldBuildButNotStartTheServer() {
115114
EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(this.port).build();
116115
assertPortIsFree(this.port);
117116
}
118117

119118
@Test
120-
public void shouldBuildTheServerWithCustomPort() {
119+
void shouldBuildTheServerWithCustomPort() {
121120
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
122121
.port(this.port);
123122

@@ -129,7 +128,7 @@ public void shouldBuildTheServerWithCustomPort() {
129128
}
130129

131130
@Test
132-
public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException {
131+
void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException {
133132
String tempLogFile = Files.createTempFile("ldap-log-", ".txt").toAbsolutePath().toString();
134133

135134
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
@@ -140,18 +139,18 @@ public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOExceptio
140139
server.start();
141140

142141
ldapTemplate("dc=jayway,dc=se", this.port)
143-
.search(LdapQueryBuilder.query().where("objectclass").is("person"), new AttributesMapper<>() {
144-
public String mapFromAttributes(Attributes attrs) throws NamingException {
145-
return (String) attrs.get("cn").get();
146-
}
147-
});
142+
.search(LdapQueryBuilder.query().where("objectclass").is("person"), this::commonNameAttribute);
148143
}
149144

150145
assertThat(Path.of(tempLogFile))
151146
.as("Applying the custom configuration should create a log file and populate it with the request")
152147
.isNotEmptyFile();
153148
}
154149

150+
private String commonNameAttribute(Attributes attrs) throws NamingException {
151+
return (String) attrs.get("cn").get();
152+
}
153+
155154
static void assertPortIsFree(int port) {
156155
assertThat(isPortOpen(port)).isFalse();
157156
}

test-support/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTests.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,33 @@
2121
import javax.naming.NamingException;
2222
import javax.naming.directory.Attributes;
2323

24-
import org.junit.After;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.AutoClose;
25+
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.context.support.ClassPathXmlApplicationContext;
28-
import org.springframework.ldap.core.AttributesMapper;
2928
import org.springframework.ldap.core.LdapTemplate;
3029
import org.springframework.ldap.query.LdapQueryBuilder;
3130

3231
import static org.assertj.core.api.Assertions.assertThat;
3332

34-
public class TestContextSourceFactoryBeanTests {
33+
class TestContextSourceFactoryBeanTests {
3534

35+
@AutoClose
3636
ClassPathXmlApplicationContext ctx;
3737

38-
@After
39-
public void setup() {
40-
if (this.ctx != null) {
41-
this.ctx.close();
42-
}
43-
}
44-
4538
@Test
46-
public void testServerStartup() throws Exception {
39+
void testServerStartup() {
4740
this.ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml");
4841
LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class);
4942
assertThat(ldapTemplate).isNotNull();
5043

5144
List<String> list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"),
52-
new AttributesMapper<>() {
53-
public String mapFromAttributes(Attributes attrs) throws NamingException {
54-
return (String) attrs.get("cn").get();
55-
}
56-
});
57-
assertThat(list.size()).isEqualTo(5);
45+
this::commonNameAttribute);
46+
assertThat(list).hasSize(5);
47+
}
48+
49+
private String commonNameAttribute(Attributes attrs) throws NamingException {
50+
return (String) attrs.get("cn").get();
5851
}
5952

6053
}

0 commit comments

Comments
 (0)