Skip to content

Commit

Permalink
Merge pull request #505 from paveljandejsek/yamlPropertySourceRewrite
Browse files Browse the repository at this point in the history
Rewritten yaml properties loading.
  • Loading branch information
paveljandejsek authored Dec 9, 2020
2 parents 10348db + af07551 commit e246544
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
27 changes: 27 additions & 0 deletions jbehave-support-core/docs/Deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
* <p>
* Example usage in spring configuration:
Expand All @@ -34,9 +38,11 @@
* }
* </pre>
*/
@Slf4j
@Getter
@Setter
@NoArgsConstructor
@Deprecated
public class YamlPropertiesConfigurer implements BeanFactoryPostProcessor, EnvironmentAware, ResourceLoaderAware, PriorityOrdered {

private static final String PROFILE_PLACEHOLDER = "{profile}";
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Example usage in spring configuration:
* <pre>
* &#064Configuration
* &#064;PropertySource(value = "common-env.yml", factory = YamlPropertySourceFactory.class)
* public class TestConfig {
* }
* </pre>
*
* To use variables please use standard spring ${} placeholder format see {@link PropertySource} for more information
* For example:
* <pre>
* &#064;PropertySource(value = {"common-env.yml", "${spring.profiles.active}.yml"}, factory = YamlPropertySourceFactory.class)
* </pre>
*/
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -50,6 +51,7 @@ import java.util.concurrent.RejectedExecutionException

@Configuration
@ComponentScan
@PropertySource(value = "test.yml", factory = YamlPropertySourceFactory.class)
class TestConfig {

@Autowired
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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() {
Expand Down

0 comments on commit e246544

Please sign in to comment.