1717
1818import javax .annotation .PreDestroy ;
1919
20- import org .junit .After ;
2120import org .junit .Before ;
2221import org .junit .Test ;
2322import org .junit .runner .RunWith ;
2423
2524import org .springframework .beans .factory .annotation .Autowired ;
26- import org .springframework .context .ConfigurableApplicationContext ;
27- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2825import org .springframework .context .annotation .Bean ;
2926import org .springframework .context .annotation .Configuration ;
3027import org .springframework .ldap .core .ContextSource ;
3330import org .springframework .security .ldap .DefaultSpringSecurityContextSource ;
3431import org .springframework .security .ldap .SpringSecurityLdapTemplate ;
3532import org .springframework .security .ldap .server .UnboundIdContainer ;
36- import org .springframework .security .test .context .annotation .SecurityTestExecutionListeners ;
3733import org .springframework .security .test .context .support .WithMockUser ;
38- import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
34+ import org .springframework .test .context .ContextConfiguration ;
35+ import org .springframework .test .context .junit4 .SpringRunner ;
3936
4037import static org .assertj .core .api .Assertions .assertThat ;
4138import static org .assertj .core .api .Assertions .assertThatCode ;
4643 *
4744 * @author Josh Cummings
4845 */
49- @ RunWith (SpringJUnit4ClassRunner .class )
50- @ SecurityTestExecutionListeners
46+ @ RunWith (SpringRunner .class )
47+ @ ContextConfiguration ( classes = LdapUserDetailsManagerModifyPasswordTests . UnboundIdContainerConfiguration . class )
5148public class LdapUserDetailsManagerModifyPasswordTests {
5249
53- ConfigurableApplicationContext context ;
54-
5550 LdapUserDetailsManager userDetailsManager ;
51+
52+ @ Autowired
5653 ContextSource contextSource ;
5754
5855 @ Before
5956 public void setup () {
60- this .context = new AnnotationConfigApplicationContext (ContainerConfiguration .class , LdapConfiguration .class );
61- this .contextSource = this .context .getBean (ContextSource .class );
62-
6357 this .userDetailsManager = new LdapUserDetailsManager (this .contextSource );
6458 this .userDetailsManager .setUsePasswordModifyExtensionOperation (true );
6559 this .userDetailsManager .setUsernameMapper (new DefaultLdapUsernameToDnMapper ("ou=people" , "uid" ));
6660 }
6761
68- @ After
69- public void teardown () {
70- this .context .close ();
71- }
72-
7362 @ Test
7463 @ WithMockUser (username ="bob" , password ="bobspassword" , authorities ="ROLE_USER" )
7564 public void changePasswordWhenOldPasswordIsIncorrectThenThrowsException () {
@@ -91,19 +80,8 @@ public void changePasswordWhenOldPasswordIsCorrectThenPasses() {
9180 }
9281
9382 @ Configuration
94- static class LdapConfiguration {
95- @ Autowired UnboundIdContainer container ;
96-
97- @ Bean
98- ContextSource contextSource () throws Exception {
99- return new DefaultSpringSecurityContextSource ("ldap://127.0.0.1:"
100- + this .container .getPort () + "/dc=springframework,dc=org" );
101- }
102- }
103-
104- @ Configuration
105- static class ContainerConfiguration {
106- UnboundIdContainer container = new UnboundIdContainer ("dc=springframework,dc=org" ,
83+ static class UnboundIdContainerConfiguration {
84+ private UnboundIdContainer container = new UnboundIdContainer ("dc=springframework,dc=org" ,
10785 "classpath:test-server.ldif" );
10886
10987 @ Bean
@@ -112,6 +90,12 @@ UnboundIdContainer ldapContainer() {
11290 return this .container ;
11391 }
11492
93+ @ Bean
94+ ContextSource contextSource (UnboundIdContainer container ) {
95+ return new DefaultSpringSecurityContextSource ("ldap://127.0.0.1:"
96+ + container .getPort () + "/dc=springframework,dc=org" );
97+ }
98+
11599 @ PreDestroy
116100 void shutdown () {
117101 this .container .stop ();
0 commit comments