From af07551a04aaf47720f5ae74597a50fea2737e0f Mon Sep 17 00:00:00 2001 From: Pavel Jandejsek Date: Sun, 15 Nov 2020 15:42:28 +0100 Subject: [PATCH] Rewritten yaml properties loading. --- jbehave-support-core/docs/Deprecated.md | 27 +++++++++++++ .../support/YamlPropertiesConfigurer.java | 7 ++++ .../support/YamlPropertySourceFactory.java | 38 +++++++++++++++++++ .../org/jbehavesupport/core/TestConfig.groovy | 9 ++--- .../test/support/TestConfig.java | 9 ++--- 5 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertySourceFactory.java diff --git a/jbehave-support-core/docs/Deprecated.md b/jbehave-support-core/docs/Deprecated.md index 9c437a07..a5d90f88 100644 --- a/jbehave-support-core/docs/Deprecated.md +++ b/jbehave-support-core/docs/Deprecated.md @@ -4,6 +4,33 @@ ### To be removed - do not use +Class _YamlPropertiesConfigurer_ + +>Deprecated since version 1.1.4 - will be removed in the future +> +>Replaced with YamlPropertySourceFactory +> +>Example: +>``` +>\\ Deprecated example +>@Bean +>public static YamlPropertiesConfigurer yamlPropertiesConfigurer() { +> return new YamlPropertiesConfigurer("common.yml"); +>} +> +>\\ What should be used instead +>@Configuration +>@PropertySource(value = "common-env.yml", factory = YamlPropertySourceFactory.class) +>public class PropertyConfig { +>} +>``` +>To use variables please use standard spring `${}` placeholder format see `org.springframework.core.env.PropertySource` for more information +>For example: +>``` +>@PropertySource(value = {"common-env.yml", "${spring.profiles.active}.yml"}, factory = YamlPropertySourceFactory.class) +>``` + +--- Column _OPERATOR_ >Deprecated since version 1.0.0 - will be removed soon diff --git a/jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertiesConfigurer.java b/jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertiesConfigurer.java index 63b0f3ac..8aac91d0 100644 --- a/jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertiesConfigurer.java +++ b/jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertiesConfigurer.java @@ -10,6 +10,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; @@ -24,6 +25,9 @@ import org.springframework.core.io.ResourceLoader; /** + * + * @deprecated(since = "1.1.4", forRemoval = true) use {@link YamlPropertySourceFactory} instead + * * The class {@link YamlPropertiesConfigurer} allows to use YAML files as {@link Environment} properties sources. *

* Example usage in spring configuration: @@ -34,9 +38,11 @@ * } * */ +@Slf4j @Getter @Setter @NoArgsConstructor +@Deprecated public class YamlPropertiesConfigurer implements BeanFactoryPostProcessor, EnvironmentAware, ResourceLoaderAware, PriorityOrdered { private static final String PROFILE_PLACEHOLDER = "{profile}"; @@ -52,6 +58,7 @@ public YamlPropertiesConfigurer(String... locations) { @Override public final void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { + log.warn("Using deprecated YamlPropertiesConfigurer, please use YamlPropertySourceFactory instead, more info can be found in: https://embeditcz.github.io/jbehave-support/#/jbehave-support-core/docs/Deprecated"); requireNonNull(locations); requireNonNull(environment); requireNonNull(resourceLoader); diff --git a/jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertySourceFactory.java b/jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertySourceFactory.java new file mode 100644 index 00000000..1ddf15f1 --- /dev/null +++ b/jbehave-support-core/src/main/java/org/jbehavesupport/core/support/YamlPropertySourceFactory.java @@ -0,0 +1,38 @@ +package org.jbehavesupport.core.support; + +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.core.io.support.PropertySourceFactory; + +import java.util.Properties; + +/** + * The class {@link YamlPropertySourceFactory} allows to use YAML files as {@link org.springframework.core.env.Environment} properties sources. + *

+ * Example usage in spring configuration: + *

+ * @Configuration
+ * @PropertySource(value = "common-env.yml", factory = YamlPropertySourceFactory.class)
+ * public class TestConfig {
+ * }
+ * 
+ * + * To use variables please use standard spring ${} placeholder format see {@link PropertySource} for more information + * For example: + *
+ * @PropertySource(value = {"common-env.yml", "${spring.profiles.active}.yml"}, factory = YamlPropertySourceFactory.class)
+ * 
+ */ +public class YamlPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource encodedResource) { + YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); + factory.setResources(encodedResource.getResource()); + Properties properties = factory.getObject(); + return new PropertiesPropertySource(name == null ? encodedResource.getResource().getFilename() : name, properties); + } + +} diff --git a/jbehave-support-core/src/test/groovy/org/jbehavesupport/core/TestConfig.groovy b/jbehave-support-core/src/test/groovy/org/jbehavesupport/core/TestConfig.groovy index a3ee7abb..1a9efec9 100644 --- a/jbehave-support-core/src/test/groovy/org/jbehavesupport/core/TestConfig.groovy +++ b/jbehave-support-core/src/test/groovy/org/jbehavesupport/core/TestConfig.groovy @@ -18,7 +18,7 @@ import org.jbehavesupport.core.ssh.SimpleRollingLogResolver import org.jbehavesupport.core.ssh.SshLog import org.jbehavesupport.core.ssh.SshSetting import org.jbehavesupport.core.ssh.SshTemplate -import org.jbehavesupport.core.support.YamlPropertiesConfigurer +import org.jbehavesupport.core.support.YamlPropertySourceFactory import org.jbehavesupport.core.test.app.oxm.NameRequest import org.jbehavesupport.core.test.app.oxm.NameResponse import org.jbehavesupport.core.verification.Verifier @@ -36,6 +36,7 @@ import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Bean import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.PropertySource import org.springframework.core.env.Environment import static java.util.Objects.nonNull @@ -50,6 +51,7 @@ import java.util.concurrent.RejectedExecutionException @Configuration @ComponentScan +@PropertySource(value = "test.yml", factory = YamlPropertySourceFactory.class) class TestConfig { @Autowired @@ -63,11 +65,6 @@ class TestConfig { applicationContext.getBean(ExamplesEvaluationTableConverter.class).setConfiguration(new MostUsefulConfiguration()) } - @Bean - static YamlPropertiesConfigurer yamlPropertiesConfigurer() { - return new YamlPropertiesConfigurer("test.yml") - } - @Bean HealthCheckSteps healthCheckSteps(ConfigurableListableBeanFactory beanFactory) { return new HealthCheckSteps(beanFactory) diff --git a/jbehave-support-core/src/test/groovy/org/jbehavesupport/test/support/TestConfig.java b/jbehave-support-core/src/test/groovy/org/jbehavesupport/test/support/TestConfig.java index aec861fe..29dc379b 100644 --- a/jbehave-support-core/src/test/groovy/org/jbehavesupport/test/support/TestConfig.java +++ b/jbehave-support-core/src/test/groovy/org/jbehavesupport/test/support/TestConfig.java @@ -32,7 +32,7 @@ import org.jbehavesupport.core.ssh.SshLog; import org.jbehavesupport.core.ssh.SshSetting; import org.jbehavesupport.core.ssh.SshTemplate; -import org.jbehavesupport.core.support.YamlPropertiesConfigurer; +import org.jbehavesupport.core.support.YamlPropertySourceFactory; import org.jbehavesupport.core.test.app.oxm.NameRequest; import org.jbehavesupport.core.web.WebDriverFactory; import org.jbehavesupport.core.web.WebSetting; @@ -45,6 +45,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.env.Environment; @@ -69,6 +70,7 @@ @Configuration @ComponentScan @RequiredArgsConstructor +@PropertySource(value = "test.yml", factory = YamlPropertySourceFactory.class) public class TestConfig { public static final String FIREFOX_BROWSERSTACK = "firefox-browserstack"; @@ -79,11 +81,6 @@ public class TestConfig { final ResourceLoader resourceLoader; - @Bean - public static YamlPropertiesConfigurer yamlPropertiesConfigurer() { - return new YamlPropertiesConfigurer("test.yml"); - } - @Bean @Qualifier("TEST") public WebServiceHandler testWebServiceHandler() {