Skip to content

Dev/1058 junit5 #1085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}


Expand Down
7 changes: 4 additions & 3 deletions test-support/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ 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"
testImplementation "org.junit.jupiter:junit-jupiter:5.12.2"
}
tasks.withType(Test).configureEach {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,16 +40,21 @@ public class AttributeCheckAttributesMapper implements AttributesMapper<Object>
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,16 +38,20 @@ public class AttributeCheckContextMapper implements ContextMapper<DirContextAdap

public DirContextAdapter mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
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++) {
String attributeValue = adapter.getStringAttribute(this.expectedAttributes[i]);
Assert.assertNotNull("Attribute " + this.expectedAttributes[i] + " was not present", attributeValue);
Assert.assertEquals(this.expectedValues[i], attributeValue);
Assert.notNull(attributeValue, "Attribute " + this.expectedAttributes[i] + " was not present");

Assert.isTrue(attributeValue.equals(this.expectedValues[i]), "Attribute " + this.expectedAttributes[i]
+ " had value " + attributeValue + " instead of " + this.expectedValues[i]);
}

for (String absentAttribute : this.absentAttributes) {
Assert.assertNull(adapter.getStringAttribute(absentAttribute));
Assert.notNull(adapter.getStringAttribute(absentAttribute),
"Attribute " + absentAttribute + " was present");
}

return adapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,29 @@
import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.junit.Test;
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 {

@Test
public void testServerStartup() throws Exception {
void serverStartup() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
assertThat(ldapTemplate).isNotNull();

List<String> 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::mapFromAttributes);
assertThat(list).hasSize(5);
}

private String mapFromAttributes(Attributes attrs) throws NamingException {
return (String) attrs.get("cn").get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,39 @@
import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
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 {

ClassPathXmlApplicationContext ctx;

@After
public void setup() {
@AfterEach
void setup() {
if (this.ctx != null) {
this.ctx.close();
}
}

@Test
public void testServerStartup() throws Exception {
void serverStartup() throws Exception {
this.ctx = new ClassPathXmlApplicationContext("/applicationContext-ldifPopulator.xml");
LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class);
assertThat(ldapTemplate).isNotNull();

List<String> 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::mapFromAttributes);
assertThat(list).hasSize(5);
}

private String mapFromAttributes(Attributes attrs) throws NamingException {
return (String) attrs.get("cn").get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,27 @@
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;

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);
Expand All @@ -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",
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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")
Expand All @@ -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::mapFromAttributes);
}

assertThat(Path.of(tempLogFile))
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,39 @@
import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
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 {

ClassPathXmlApplicationContext ctx;

@After
public void setup() {
@AfterEach
void setup() {
if (this.ctx != null) {
this.ctx.close();
}
}

@Test
public void testServerStartup() throws Exception {
void serverStartup() throws Exception {
this.ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml");
LdapTemplate ldapTemplate = this.ctx.getBean(LdapTemplate.class);
assertThat(ldapTemplate).isNotNull();

List<String> 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::mapFromAttributes);
assertThat(list).hasSize(5);
}

private String mapFromAttributes(Attributes attrs) throws NamingException {
return (String) attrs.get("cn").get();
}

}
Loading