Commit d0216c9a authored by Maxym Borodenko's avatar Maxym Borodenko

Property sources update

parent 25c54bef
...@@ -30,12 +30,12 @@ import org.genesys2.transifex.client.TransifexService; ...@@ -30,12 +30,12 @@ import org.genesys2.transifex.client.TransifexService;
import org.genesys2.transifex.client.TransifexServiceImpl; import org.genesys2.transifex.client.TransifexServiceImpl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
...@@ -70,12 +70,10 @@ public class ApplicationConfig { ...@@ -70,12 +70,10 @@ public class ApplicationConfig {
} }
@Bean @Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { public static PropertySourcesPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(false); pspc.setIgnoreResourceNotFound(true);
propertyPlaceholderConfigurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); pspc.setFileEncoding("UTF-8");
propertyPlaceholderConfigurer.setFileEncoding("utf-8");
final List<Resource> locations = new ArrayList<>(); final List<Resource> locations = new ArrayList<>();
locations.add(new ClassPathResource("application.properties")); locations.add(new ClassPathResource("application.properties"));
locations.add(new ClassPathResource("genesys.properties")); locations.add(new ClassPathResource("genesys.properties"));
...@@ -91,8 +89,8 @@ public class ApplicationConfig { ...@@ -91,8 +89,8 @@ public class ApplicationConfig {
} }
} }
propertyPlaceholderConfigurer.setLocations(locations.toArray(new Resource[] {})); pspc.setLocations(locations.toArray(new Resource[] {}));
return propertyPlaceholderConfigurer; return pspc;
} }
@Bean @Bean
......
...@@ -33,12 +33,12 @@ import org.genesys2.spring.config.WebSecurityConfig; ...@@ -33,12 +33,12 @@ import org.genesys2.spring.config.WebSecurityConfig;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
...@@ -85,17 +85,16 @@ public final class ApplicationConfig { ...@@ -85,17 +85,16 @@ public final class ApplicationConfig {
public static class BaseConfig { public static class BaseConfig {
@Bean @Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { public static PropertySourcesPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(false); pspc.setIgnoreResourceNotFound(true);
propertyPlaceholderConfigurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); pspc.setFileEncoding("UTF-8");
propertyPlaceholderConfigurer.setFileEncoding("utf-8");
final List<Resource> locations = new ArrayList<>(); final List<Resource> locations = new ArrayList<>();
locations.add(new ClassPathResource("application.properties")); locations.add(new ClassPathResource("application.properties"));
locations.add(new ClassPathResource("junit.properties")); locations.add(new ClassPathResource("junit.properties"));
propertyPlaceholderConfigurer.setLocations(locations.toArray(new Resource[] {}));
return propertyPlaceholderConfigurer; pspc.setLocations(locations.toArray(new Resource[] {}));
return pspc;
} }
@Bean @Bean
......
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.test.util;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Testing the PropertySourcesPlaceholderConfigurer support in Spring.
*
* Set environment variables:
*
* <pre>
* DB_URL=DB
* TEST_VAR_ONE=Yes
* TEST_INTEGER=13
* TEST_BOOLEAN=true
* TEST_STRINGS=AA,BB,CC
* </pre>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationPropertiesTest.Config.class })
@Ignore
public class ApplicationPropertiesTest {
@Configuration
public static class Config {
@Bean
public CurrentApplicationContext currentApplicationContext() {
return new CurrentApplicationContext();
}
// Adds support for lists
@Bean
public ConversionService conversionService() {
return new DefaultConversionService();
}
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
pspc.setIgnoreResourceNotFound(true);
pspc.setFileEncoding("UTF-8");
final List<Resource> locations = new ArrayList<>();
locations.add(new ClassPathResource("application.properties"));
locations.add(new ClassPathResource("junit.properties"));
pspc.setLocations(locations.toArray(new Resource[] {}));
return pspc;
}
// @Bean
// public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
// final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
// ppc.setIgnoreResourceNotFound(true);
// ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
// ppc.setFileEncoding("utf-8");
//
// final List<Resource> locations = new ArrayList<>();
// locations.add(new ClassPathResource("application.properties"));
// locations.add(new ClassPathResource("junit.properties"));
//
// ppc.setLocations(locations.toArray(new Resource[] {}));
// return ppc;
// }
@Bean
public TestBean testBean() {
return new TestBean();
}
}
public static class TestBean {
@Value("${db.url}")
public String dbUrl;
@Value("${test.var.one}")
public String testVarOne;
@Value("${test.boolean}")
public Boolean testBoolean;
@Value("${test.integer}")
public int testInt;
@Value("${test.integer.default:42}")
public int testDefault;
@Value("${test.strings}")
public List<String> testStrings;
}
@Autowired
private TestBean testBean;
@Test
public void testEnvVar() {
assertThat(testBean.testVarOne, notNullValue());
assertThat(testBean.testVarOne, equalTo("Yes"));
assertThat(testBean.testInt, notNullValue());
assertThat(testBean.testInt, equalTo(13));
assertThat(testBean.testBoolean, notNullValue());
assertThat(testBean.testBoolean, equalTo(true));
assertThat(testBean.testDefault, notNullValue());
assertThat(testBean.testDefault, equalTo(42));
assertThat(testBean.dbUrl, notNullValue());
assertThat(testBean.dbUrl, equalTo("DB"));
assertThat(testBean.testStrings, notNullValue());
assertThat(testBean.testStrings, hasItems("AA", "BB", "CC"));
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment