Skip to content

Commit a171d8b

Browse files
eddumelendezjzheaux
authored andcommitted
Make ldap integration tests independent
Fixes gh-5942
1 parent 76718c4 commit a171d8b

12 files changed

+196
-232
lines changed

ldap/spring-security-ldap.gradle

-10
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ dependencies {
2626
}
2727

2828
integrationTest {
29-
include('**/ApacheDSServerIntegrationTests.class',
30-
'**/ApacheDSEmbeddedLdifTests.class',
31-
'**/LdapUserDetailsManagerModifyPasswordTests.class')
3229
// exclude('**/OpenLDAPIntegrationTestSuite.class')
3330
maxParallelForks = 1
3431
}
35-
36-
// Runs a server for running the integration tests against (from an IDE, for example)
37-
task(ldapServer, dependsOn: 'integrationTestClasses', type: JavaExec) {
38-
classpath = sourceSets.integrationTest.runtimeClasspath
39-
main = 'org.springframework.security.ldap.ApacheDSServerIntegrationTests'
40-
}
41-

ldap/src/integration-test/java/org/springframework/security/ldap/AbstractLdapIntegrationTests.java

-44
This file was deleted.

ldap/src/integration-test/java/org/springframework/security/ldap/ApacheDSServerIntegrationTests.java

-121
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.ldap;
17+
18+
import javax.annotation.PreDestroy;
19+
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.ldap.core.ContextSource;
23+
import org.springframework.security.ldap.server.ApacheDSContainer;
24+
25+
/**
26+
* @author Eddú Meléndez
27+
*/
28+
@Configuration
29+
public class ApacheDsContainerConfig {
30+
31+
private ApacheDSContainer container;
32+
33+
@Bean
34+
ApacheDSContainer ldapContainer() throws Exception {
35+
this.container = new ApacheDSContainer("dc=springframework,dc=org",
36+
"classpath:test-server.ldif");
37+
return this.container;
38+
}
39+
40+
@Bean
41+
ContextSource contextSource() throws Exception {
42+
return new DefaultSpringSecurityContextSource("ldap://127.0.0.1:"
43+
+ ldapContainer().getPort() + "/dc=springframework,dc=org");
44+
}
45+
46+
@PreDestroy
47+
void shutdown() {
48+
this.container.stop();
49+
}
50+
51+
}

ldap/src/integration-test/java/org/springframework/security/ldap/DefaultSpringSecurityContextSourceTests.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,13 +24,24 @@
2424
import javax.naming.directory.DirContext;
2525

2626
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
29+
import org.springframework.beans.factory.annotation.Autowired;
2730
import org.springframework.ldap.AuthenticationException;
2831
import org.springframework.ldap.core.support.AbstractContextSource;
32+
import org.springframework.test.context.ContextConfiguration;
33+
import org.springframework.test.context.junit4.SpringRunner;
2934

3035
/**
3136
* @author Luke Taylor
37+
* @author Eddú Meléndez
3238
*/
33-
public class DefaultSpringSecurityContextSourceTests extends AbstractLdapIntegrationTests {
39+
@RunWith(SpringRunner.class)
40+
@ContextConfiguration(classes = ApacheDsContainerConfig.class)
41+
public class DefaultSpringSecurityContextSourceTests {
42+
43+
@Autowired
44+
private DefaultSpringSecurityContextSource contextSource;
3445

3546
@Test
3647
public void instantiationSucceedsWithExpectedProperties() {
@@ -76,7 +87,7 @@ public void cantBindWithWrongPasswordImmediatelyAfterSuccessfulBind()
7687
throws Exception {
7788
DirContext ctx = null;
7889
try {
79-
ctx = getContextSource().getContext(
90+
ctx = this.contextSource.getContext(
8091
"uid=Bob,ou=people,dc=springframework,dc=org", "bobspassword");
8192
}
8293
catch (Exception e) {
@@ -86,16 +97,16 @@ public void cantBindWithWrongPasswordImmediatelyAfterSuccessfulBind()
8697
ctx.close();
8798
// com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
8899
// Now get it gain, with wrong password. Should fail.
89-
ctx = getContextSource().getContext(
100+
ctx = this.contextSource.getContext(
90101
"uid=Bob,ou=people,dc=springframework,dc=org", "wrongpassword");
91102
ctx.close();
92103
}
93104

94105
@Test
95106
public void serverUrlWithSpacesIsSupported() throws Exception {
96107
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
97-
"ldap://127.0.0.1:" + ApacheDSServerIntegrationTests.getServerPort()
98-
+ "/ou=space%20cadets,dc=springframework,dc=org");
108+
this.contextSource.getUrls()[0]
109+
+ "ou=space%20cadets,dc=springframework,dc=org");
99110
contextSource.afterPropertiesSet();
100111
contextSource.getContext(
101112
"uid=space cadet,ou=space cadets,dc=springframework,dc=org",

ldap/src/integration-test/java/org/springframework/security/ldap/SpringSecurityLdapTemplateITests.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,35 @@
2929
import javax.naming.directory.SearchResult;
3030

3131
import org.junit.*;
32+
import org.junit.runner.RunWith;
33+
34+
import org.springframework.beans.factory.annotation.Autowired;
3235
import org.springframework.ldap.UncategorizedLdapException;
3336
import org.springframework.ldap.core.ContextExecutor;
3437
import org.springframework.security.crypto.codec.Utf8;
38+
import org.springframework.test.context.ContextConfiguration;
39+
import org.springframework.test.context.junit4.SpringRunner;
3540

3641
/**
3742
* @author Luke Taylor
43+
* @author Eddú Meléndez
3844
*/
39-
public class SpringSecurityLdapTemplateITests extends AbstractLdapIntegrationTests {
45+
@RunWith(SpringRunner.class)
46+
@ContextConfiguration(classes = ApacheDsContainerConfig.class)
47+
public class SpringSecurityLdapTemplateITests {
4048
// ~ Instance fields
4149
// ================================================================================================
4250

51+
@Autowired
52+
private DefaultSpringSecurityContextSource contextSource;
4353
private SpringSecurityLdapTemplate template;
4454

4555
// ~ Methods
4656
// ========================================================================================================
4757

4858
@Before
4959
public void setUp() throws Exception {
50-
template = new SpringSecurityLdapTemplate(getContextSource());
60+
template = new SpringSecurityLdapTemplate(this.contextSource);
5161
}
5262

5363
@Test
@@ -184,8 +194,7 @@ public void roleSearchWithEscapedCharacterSucceeds() throws Exception {
184194
public void nonSpringLdapSearchCodeTestMethod() throws Exception {
185195
java.util.Hashtable<String, String> env = new java.util.Hashtable<>();
186196
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
187-
env.put(Context.PROVIDER_URL, "ldap://localhost:"
188-
+ ApacheDSServerIntegrationTests.getServerPort());
197+
env.put(Context.PROVIDER_URL, this.contextSource.getUrls()[0]);
189198
env.put(Context.SECURITY_PRINCIPAL, "");
190199
env.put(Context.SECURITY_CREDENTIALS, "");
191200

0 commit comments

Comments
 (0)