From 633aefa844b42e541b0ec4ac1c5900f0c10de717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 23 Jan 2018 18:28:47 -0500 Subject: [PATCH 1/3] Move tests to use ApplicationContextRunner --- .../ldap/LdapAutoConfigurationTests.java | 85 ++++++++++--------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java index 82614df4ff..85b85a121d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,12 @@ package org.springframework.boot.autoconfigure.ldap; -import org.junit.After; import org.junit.Test; -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -34,58 +33,60 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class LdapAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(LdapAutoConfiguration.class)); @Test public void testDefaultUrl() { - load(); - ContextSource contextSource = this.context.getBean(ContextSource.class); - String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls"); - assertThat(urls).containsExactly("ldap://localhost:389"); + this.contextRunner.run(context -> { + ContextSource contextSource = context.getBean(ContextSource.class); + String[] urls = (String[]) ReflectionTestUtils + .getField(contextSource, "urls"); + assertThat(urls).containsExactly("ldap://localhost:389"); + }); } @Test public void testContextSourceSetOneUrl() { - load("spring.ldap.urls:ldap://localhost:123"); - ContextSource contextSource = this.context.getBean(ContextSource.class); - String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls"); - assertThat(urls).containsExactly("ldap://localhost:123"); + this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123") + .run(context -> { + ContextSource contextSource = context.getBean(ContextSource.class); + String[] urls = (String[]) ReflectionTestUtils.getField( + contextSource, "urls"); + assertThat(urls).containsExactly("ldap://localhost:123"); + }); } @Test public void testContextSourceSetTwoUrls() { - load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123"); - ContextSource contextSource = this.context.getBean(ContextSource.class); - LdapProperties ldapProperties = this.context.getBean(LdapProperties.class); - String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls"); - assertThat(urls).containsExactly("ldap://localhost:123", "ldap://mycompany:123"); - assertThat(ldapProperties.getUrls()).hasSize(2); + this.contextRunner + .withPropertyValues( + "spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123") + .run(context -> { + ContextSource contextSource = context.getBean(ContextSource.class); + LdapProperties ldapProperties = context.getBean(LdapProperties.class); + String[] urls = (String[]) ReflectionTestUtils.getField( + contextSource, "urls"); + assertThat(urls).containsExactly("ldap://localhost:123", + "ldap://mycompany:123"); + assertThat(ldapProperties.getUrls()).hasSize(2); + }); } @Test public void testContextSourceWithMoreProperties() { - load("spring.ldap.urls:ldap://localhost:123", "spring.ldap.username:root", - "spring.ldap.password:root", "spring.ldap.base:cn=SpringDevelopers", - "spring.ldap.baseEnvironment.java.naming.security" - + ".authentication:DIGEST-MD5"); - LdapProperties ldapProperties = this.context.getBean(LdapProperties.class); - assertThat(ldapProperties.getBaseEnvironment()) - .containsEntry("java.naming.security.authentication", "DIGEST-MD5"); - } - - private void load(String... properties) { - this.context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(properties).applyTo(this.context); - this.context.register(LdapAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + this.contextRunner + .withPropertyValues( + "spring.ldap.urls:ldap://localhost:123", + "spring.ldap.username:root", + "spring.ldap.password:root", + "spring.ldap.base:cn=SpringDevelopers", + "spring.ldap.baseEnvironment.java.naming.security.authentication:DIGEST-MD5") + .run(context -> { + LdapProperties ldapProperties = context.getBean(LdapProperties.class); + assertThat(ldapProperties.getBaseEnvironment()).containsEntry( + "java.naming.security.authentication", "DIGEST-MD5"); + }); } } From af0bdc893b5bc08a9f93bcf0442f0f7720eac5cb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 24 Jan 2018 09:57:25 +0100 Subject: [PATCH 2/3] Add support for anonymousReadOnly in LdapProperties See gh-11744 --- .../autoconfigure/ldap/LdapAutoConfiguration.java | 3 ++- .../boot/autoconfigure/ldap/LdapProperties.java | 15 ++++++++++++++- .../ldap/LdapAutoConfigurationTests.java | 9 +++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java index acf6f93929..c3ad246e64 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,6 +58,7 @@ public class LdapAutoConfiguration { source.setUrls(this.properties.determineUrls(this.environment)); source.setBaseEnvironmentProperties( Collections.unmodifiableMap(this.properties.getBaseEnvironment())); + source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly()); return source; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java index fcc9aba071..70b9697ebc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,11 @@ public class LdapProperties { */ private Map baseEnvironment = new HashMap<>(); + /** + * Whether read-only operations should use an anonymous environment. + */ + private boolean anonymousReadOnly; + public String[] getUrls() { return this.urls; } @@ -100,6 +105,14 @@ public class LdapProperties { this.baseEnvironment = baseEnvironment; } + public boolean getAnonymousReadOnly() { + return this.anonymousReadOnly; + } + + public void setAnonymousReadOnly(boolean anonymousReadOnly) { + this.anonymousReadOnly = anonymousReadOnly; + } + public String[] determineUrls(Environment environment) { if (ObjectUtils.isEmpty(this.urls)) { return new String[] { "ldap://localhost:" + determinePort(environment) }; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java index 85b85a121d..d6e8bac69e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java @@ -89,4 +89,13 @@ public class LdapAutoConfigurationTests { }); } + @Test + public void testContextSourceWithDefaultAnonymousReadOnly() { + this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123") + .run(context -> { + LdapContextSource contextSource = context.getBean(LdapContextSource.class); + assertThat(contextSource.isAnonymousReadOnly()).isFalse(); + }); + } + } From 960989cfe60e447229af954c31846ecb3b1f0461 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 24 Jan 2018 10:21:33 +0100 Subject: [PATCH 3/3] Polish "Add support for anonymousReadOnly in LdapProperties" Closes gh-11744 --- .../ldap/LdapAutoConfiguration.java | 2 +- .../autoconfigure/ldap/LdapProperties.java | 22 ++++++------- .../ldap/LdapAutoConfigurationTests.java | 31 ++++++++++--------- .../appendix-application-properties.adoc | 7 +++-- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java index c3ad246e64..404b030e18 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java @@ -54,11 +54,11 @@ public class LdapAutoConfiguration { LdapContextSource source = new LdapContextSource(); source.setUserDn(this.properties.getUsername()); source.setPassword(this.properties.getPassword()); + source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly()); source.setBase(this.properties.getBase()); source.setUrls(this.properties.determineUrls(this.environment)); source.setBaseEnvironmentProperties( Collections.unmodifiableMap(this.properties.getBaseEnvironment())); - source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly()); return source; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java index 70b9697ebc..6c2e74205f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java @@ -55,16 +55,16 @@ public class LdapProperties { */ private String password; - /** - * LDAP specification settings. - */ - private Map baseEnvironment = new HashMap<>(); - /** * Whether read-only operations should use an anonymous environment. */ private boolean anonymousReadOnly; + /** + * LDAP specification settings. + */ + private final Map baseEnvironment = new HashMap<>(); + public String[] getUrls() { return this.urls; } @@ -97,14 +97,6 @@ public class LdapProperties { this.password = password; } - public Map getBaseEnvironment() { - return this.baseEnvironment; - } - - public void setBaseEnvironment(Map baseEnvironment) { - this.baseEnvironment = baseEnvironment; - } - public boolean getAnonymousReadOnly() { return this.anonymousReadOnly; } @@ -113,6 +105,10 @@ public class LdapProperties { this.anonymousReadOnly = anonymousReadOnly; } + public Map getBaseEnvironment() { + return this.baseEnvironment; + } + public String[] determineUrls(Environment environment) { if (ObjectUtils.isEmpty(this.urls)) { return new String[] { "ldap://localhost:" + determinePort(environment) }; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java index d6e8bac69e..df2739fc8e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java @@ -30,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link LdapAutoConfiguration}. * * @author EddĂș MelĂ©ndez + * @author Stephane Nicoll */ public class LdapAutoConfigurationTests { @@ -37,17 +38,18 @@ public class LdapAutoConfigurationTests { .withConfiguration(AutoConfigurations.of(LdapAutoConfiguration.class)); @Test - public void testDefaultUrl() { + public void contextSourceWithDefaultUrl() { this.contextRunner.run(context -> { - ContextSource contextSource = context.getBean(ContextSource.class); + LdapContextSource contextSource = context.getBean(LdapContextSource.class); String[] urls = (String[]) ReflectionTestUtils .getField(contextSource, "urls"); assertThat(urls).containsExactly("ldap://localhost:389"); + assertThat(contextSource.isAnonymousReadOnly()).isFalse(); }); } @Test - public void testContextSourceSetOneUrl() { + public void contextSourceWithSingleUrl() { this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123") .run(context -> { ContextSource contextSource = context.getBean(ContextSource.class); @@ -58,7 +60,7 @@ public class LdapAutoConfigurationTests { } @Test - public void testContextSourceSetTwoUrls() { + public void contextSourceWithSeveralUrls() { this.contextRunner .withPropertyValues( "spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123") @@ -74,28 +76,27 @@ public class LdapAutoConfigurationTests { } @Test - public void testContextSourceWithMoreProperties() { + public void contextSourceWithExtraCustomization() { this.contextRunner .withPropertyValues( "spring.ldap.urls:ldap://localhost:123", "spring.ldap.username:root", - "spring.ldap.password:root", + "spring.ldap.password:secret", + "spring.ldap.anonymous-read-only:true", "spring.ldap.base:cn=SpringDevelopers", "spring.ldap.baseEnvironment.java.naming.security.authentication:DIGEST-MD5") .run(context -> { + LdapContextSource contextSource = context.getBean( + LdapContextSource.class); + assertThat(contextSource.getUserDn()).isEqualTo("root"); + assertThat(contextSource.getPassword()).isEqualTo("secret"); + assertThat(contextSource.isAnonymousReadOnly()).isTrue(); + assertThat(contextSource.getBaseLdapPathAsString()).isEqualTo( + "cn=SpringDevelopers"); LdapProperties ldapProperties = context.getBean(LdapProperties.class); assertThat(ldapProperties.getBaseEnvironment()).containsEntry( "java.naming.security.authentication", "DIGEST-MD5"); }); } - @Test - public void testContextSourceWithDefaultAnonymousReadOnly() { - this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123") - .run(context -> { - LdapContextSource contextSource = context.getBean(LdapContextSource.class); - assertThat(contextSource.isAnonymousReadOnly()).isFalse(); - }); - } - } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 086cb801db..c722206a4b 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -362,11 +362,12 @@ content into your application. Rather, pick only the properties that you need. spring.jersey.type=servlet # Jersey integration type. # SPRING LDAP ({sc-spring-boot-autoconfigure}/ldap/LdapProperties.{sc-ext}[LdapProperties]) - spring.ldap.urls= # LDAP URLs of the server. + spring.ldap.anonymous-read-only=false # Whether read-only operations should use an anonymous environment. spring.ldap.base= # Base suffix from which all operations should originate. - spring.ldap.username= # Login username of the server. - spring.ldap.password= # Login password of the server. spring.ldap.base-environment.*= # LDAP specification settings. + spring.ldap.password= # Login password of the server. + spring.ldap.urls= # LDAP URLs of the server. + spring.ldap.username= # Login username of the server. # EMBEDDED LDAP ({sc-spring-boot-autoconfigure}/ldap/embedded/EmbeddedLdapProperties.{sc-ext}[EmbeddedLdapProperties]) spring.ldap.embedded.base-dn= # The base DN