Skip to content

Commit 6204576

Browse files
committed
tidy tests
Issue #1058 Signed-off-by: etrandafir93 <[email protected]>
1 parent 38be600 commit 6204576

File tree

5 files changed

+44
-61
lines changed

5 files changed

+44
-61
lines changed

test-support/build.gradle

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

24-
provided "org.junit.jupiter:junit-jupiter-api"
2524
testImplementation "org.junit.jupiter:junit-jupiter-engine"
2625
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
2726
testImplementation "org.assertj:assertj-core"

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,26 @@
2424
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: 10 additions & 17 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.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.AutoClose;
2525
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-
@AfterEach
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: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,24 @@
3131
import org.junit.jupiter.api.BeforeEach;
3232
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

4645
@BeforeEach
47-
public void setUp() throws IOException {
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: 10 additions & 17 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.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.AutoClose;
2525
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-
@AfterEach
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)