BAEL-20882 Move Spring Boot Properties module to Spring Boot modules

This commit is contained in:
mikr
2020-01-26 23:58:19 +01:00
parent dd370f90bb
commit 31e161ce81
94 changed files with 174 additions and 175 deletions
@@ -0,0 +1,56 @@
package com.baeldung.configurationproperties;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.configurationproperties.ConfigProperties;
import com.baeldung.properties.AdditionalProperties;
import com.baeldung.properties.ConfigPropertiesDemoApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
@TestPropertySource("classpath:configprops-test.properties")
public class ConfigPropertiesIntegrationTest {
@Autowired
private ConfigProperties properties;
@Autowired
private AdditionalProperties additionalProperties;
@Test
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("From address is read as null!", properties.getFrom() != null);
}
@Test
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2);
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2);
}
@Test
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null);
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3);
}
@Test
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null);
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password"));
}
@Test
public void whenAdditionalPropertyQueriedthenReturnsProperty() {
Assert.assertTrue(additionalProperties.getUnit().equals("km"));
Assert.assertTrue(additionalProperties.getMax() == 100);
}
}
@@ -0,0 +1,46 @@
package com.baeldung.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.unit.DataSize;
import com.baeldung.configurationproperties.PropertiesConversionApplication;
import com.baeldung.configurationproperties.PropertyConversion;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PropertiesConversionApplication.class)
@TestPropertySource("classpath:conversion.properties")
public class PropertiesConversionIntegrationTest {
@Autowired
private PropertyConversion properties;
@Test
public void whenUseTimeUnitPropertyConversion_thenSuccess() throws Exception {
assertEquals(Duration.ofMillis(10), properties.getTimeInDefaultUnit());
assertEquals(Duration.ofNanos(9), properties.getTimeInNano());
assertEquals(Duration.ofDays(2), properties.getTimeInDays());
}
@Test
public void whenUseDataSizePropertyConversion_thenSuccess() throws Exception {
assertEquals(DataSize.ofBytes(300), properties.getSizeInDefaultUnit());
assertEquals(DataSize.ofGigabytes(2), properties.getSizeInGB());
assertEquals(DataSize.ofTerabytes(4), properties.getSizeInTB());
}
@Test
public void whenUseCustomPropertyConverter_thenSuccess() throws Exception {
assertEquals("john", properties.getEmployee().getName());
assertEquals(2000.0, properties.getEmployee().getSalary());
}
}
@@ -0,0 +1,59 @@
package com.baeldung.properties;
import java.util.Arrays;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class)
public class JsonPropertiesIntegrationTest {
@Autowired
private JsonProperties jsonProperties;
@Autowired
private CustomJsonProperties customJsonProperties;
@Test
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() {
Assert.assertEquals("mailer@mail.com", jsonProperties.getHost());
Assert.assertEquals(9090, jsonProperties.getPort());
Assert.assertTrue(jsonProperties.isResend());
}
@Test
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() {
Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot")));
}
@Test
public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() {
Assert.assertEquals("sender", jsonProperties.getSender()
.get("name"));
Assert.assertEquals("street", jsonProperties.getSender()
.get("address"));
}
@Test
public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost());
Assert.assertEquals(9090, customJsonProperties.getPort());
Assert.assertTrue(customJsonProperties.isResend());
}
@Test
public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
Assert.assertNotNull(customJsonProperties.getSender());
Assert.assertEquals("sender", customJsonProperties.getSender()
.getName());
Assert.assertEquals("street", customJsonProperties.getSender()
.getAddress());
}
}
@@ -0,0 +1,30 @@
package com.baeldung.properties.basic;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.spring.BasicPropertiesWithJavaConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { BasicPropertiesWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
public class BasicPropertiesWithJavaIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,31 @@
package com.baeldung.properties.basic;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.spring.BasicPropertiesWithJavaConfig;
import com.baeldung.properties.spring.PropertiesWithJavaConfigOther;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { BasicPropertiesWithJavaConfig.class, PropertiesWithJavaConfigOther.class }, loader = AnnotationConfigContextLoader.class)
public class ExtendedPropertiesWithJavaIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,30 @@
package com.baeldung.properties.basic;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.spring.PropertiesWithJavaConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PropertiesWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
public class PropertiesWithJavaIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,27 @@
package com.baeldung.properties.basic;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:basicConfigForPropertiesOne.xml", "classpath:basicConfigForPropertiesTwo.xml" })
public class PropertiesWithMultipleXmlsIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,27 @@
package com.baeldung.properties.basic;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:basicConfigForProperties.xml")
public class PropertiesWithXmlIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,39 @@
package com.baeldung.properties.external;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.external.ExternalPropertiesWithJavaConfig;
import com.baeldung.properties.spring.PropertiesWithJavaConfigOther;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithJavaConfig.class, PropertiesWithJavaConfigOther.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only")
public class ExternalPropertiesWithJavaIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Value("${external.something}")
private String injectedExternalProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
System.out.println("in test via @Value - external: " + injectedExternalProperty);
System.out.println("in test Environment - external: " + env.getProperty("external.something"));
}
}
@@ -0,0 +1,39 @@
package com.baeldung.properties.external;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.external.ExternalPropertiesWithXmlConfigOne;
import com.baeldung.properties.external.ExternalPropertiesWithXmlConfigTwo;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfigOne.class, ExternalPropertiesWithXmlConfigTwo.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only")
public class ExternalPropertiesWithMultipleXmlsIntegrationTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Value("${external.something}")
private String injectedExternalProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
System.out.println("in test via @Value - external: " + injectedExternalProperty);
System.out.println("in test Environment - external: " + env.getProperty("external.something"));
}
}
@@ -0,0 +1,38 @@
package com.baeldung.properties.external;
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.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.properties.external.ExternalPropertiesWithXmlConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only")
public class ExternalPropertiesWithXmlManualTest {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
@Value("${external.something}")
private String injectedExternalProperty;
@Test
public final void givenContextIsInitialized_thenNoException() {
System.out.println("in test via @Value: " + injectedProperty);
System.out.println("in test Environment: " + env.getProperty("key.something"));
System.out.println("in test via @Value - external: " + injectedExternalProperty);
System.out.println("in test Environment - external: " + env.getProperty("external.something"));
}
}
@@ -0,0 +1,26 @@
package com.baeldung.properties.multiple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.baeldung.properties.spring.PropertiesWithJavaConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(PropertiesWithJavaConfig.class)
public class MultiplePropertiesJavaConfigIntegrationTest {
@Value("${key.something}")
private String something;
@Value("${key.something2}")
private String something2;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("val");
assertThat(something2).isEqualTo("val2");
}
}
@@ -0,0 +1,21 @@
package com.baeldung.properties.multiple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(locations = "classpath:configForProperties.xml")
public class MultiplePropertiesXmlConfigIntegrationTest {
@Value("${key.something}") private String something;
@Value("${key.something2}") private String something2;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("val");
assertThat(something2).isEqualTo("val2");
}
}
@@ -0,0 +1,22 @@
package com.baeldung.properties.parentchild;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ChildValueHolder {
@Value("${parent.name:-}")
private String parentName;
@Value("${child.name:-}")
private String childName;
public String getParentName() {
return parentName;
}
public String getChildName() {
return childName;
}
}
@@ -0,0 +1,51 @@
package com.baeldung.properties.parentchild;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.properties.parentchild.config.ChildConfig2;
import com.baeldung.properties.parentchild.config.ParentConfig2;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig2.class), @ContextConfiguration(classes = ChildConfig2.class) })
public class ParentChildPropertyPlaceHolderPropertiesIntegrationTest {
@Autowired
private WebApplicationContext wac;
@Test
public void givenPropertyPlaceHolder_whenGetPropertyUsingEnv_thenCorrect() {
final Environment childEnv = wac.getEnvironment();
final Environment parentEnv = wac.getParent().getEnvironment();
assertNull(parentEnv.getProperty("parent.name"));
assertNull(parentEnv.getProperty("child.name"));
assertNull(childEnv.getProperty("parent.name"));
assertNull(childEnv.getProperty("child.name"));
}
@Test
public void givenPropertyPlaceHolder_whenGetPropertyUsingValueAnnotation_thenCorrect() {
final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class);
final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class);
assertEquals(parentValueHolder.getParentName(), "parent");
assertEquals(parentValueHolder.getChildName(), "-");
assertEquals(childValueHolder.getParentName(), "-");
assertEquals(childValueHolder.getChildName(), "child");
}
}
@@ -0,0 +1,51 @@
package com.baeldung.properties.parentchild;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.properties.parentchild.config.ChildConfig;
import com.baeldung.properties.parentchild.config.ParentConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextConfiguration(classes = ChildConfig.class) })
public class ParentChildPropertySourcePropertiesIntegrationTest {
@Autowired
private WebApplicationContext wac;
@Test
public void givenPropertySource_whenGetPropertyUsingEnv_thenCorrect() {
final Environment childEnv = wac.getEnvironment();
final Environment parentEnv = wac.getParent().getEnvironment();
assertEquals(parentEnv.getProperty("parent.name"), "parent");
assertNull(parentEnv.getProperty("child.name"));
assertEquals(childEnv.getProperty("parent.name"), "parent");
assertEquals(childEnv.getProperty("child.name"), "child");
}
@Test
public void givenPropertySource_whenGetPropertyUsingValueAnnotation_thenCorrect() {
final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class);
final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class);
assertEquals(parentValueHolder.getParentName(), "parent");
assertEquals(parentValueHolder.getChildName(), "-");
assertEquals(childValueHolder.getParentName(), "parent");
assertEquals(childValueHolder.getChildName(), "child");
}
}
@@ -0,0 +1,22 @@
package com.baeldung.properties.parentchild;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ParentValueHolder {
@Value("${parent.name:-}")
private String parentName;
@Value("${child.name:-}")
private String childName;
public String getParentName() {
return parentName;
}
public String getChildName() {
return childName;
}
}
@@ -0,0 +1,22 @@
package com.baeldung.properties.parentchild.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import com.baeldung.properties.parentchild.ChildValueHolder;
@Configuration
@PropertySource("classpath:child.properties")
public class ChildConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ChildValueHolder childValueHolder() {
return new ChildValueHolder();
}
}
@@ -0,0 +1,24 @@
package com.baeldung.properties.parentchild.config;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.baeldung.properties.parentchild.ChildValueHolder;
@Configuration
public class ChildConfig2 {
@Bean
public ChildValueHolder childValueHolder() {
return new ChildValueHolder();
}
@Bean
public static PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("child.properties"));
return ppc;
}
}
@@ -0,0 +1,24 @@
package com.baeldung.properties.parentchild.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import com.baeldung.properties.parentchild.ParentValueHolder;
@Configuration
@PropertySource("classpath:parent.properties")
public class ParentConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ParentValueHolder parentValueHolder() {
return new ParentValueHolder();
}
}
@@ -0,0 +1,24 @@
package com.baeldung.properties.parentchild.config;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.baeldung.properties.parentchild.ParentValueHolder;
@Configuration
public class ParentConfig2 {
@Bean
public ParentValueHolder parentValueHolder() {
return new ParentValueHolder();
}
@Bean
public static PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("parent.properties"));
return ppc;
}
}
@@ -0,0 +1,161 @@
package com.baeldung.properties.reloading;
import com.baeldung.properties.reloading.beans.ConfigurationPropertiesRefreshConfigBean;
import com.baeldung.properties.reloading.beans.EnvironmentConfigBean;
import com.baeldung.properties.reloading.beans.PropertiesConfigBean;
import com.baeldung.properties.reloading.beans.ValueRefreshConfigBean;
import java.io.FileOutputStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootPropertiesTestApplication.class)
public class PropertiesReloadIntegrationTest {
protected MockMvc mvc;
protected long refreshDelay = 3000;
@Autowired
WebApplicationContext webApplicationContext;
@Autowired
ValueRefreshConfigBean valueRefreshConfigBean;
@Autowired
ConfigurationPropertiesRefreshConfigBean configurationPropertiesRefreshConfigBean;
@Autowired
EnvironmentConfigBean environmentConfigBean;
@Autowired
PropertiesConfigBean propertiesConfigBean;
@Autowired
@Qualifier("singletonValueRefreshConfigBean")
ValueRefreshConfigBean singletonValueRefreshConfigBean;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build();
createConfig("extra.properties", "application.theme.color", "blue");
createConfig("extra2.properties", "application.theme.background", "red");
Thread.sleep(refreshDelay);
callRefresh();
}
@After
public void tearDown() throws Exception {
createConfig("extra.properties", "application.theme.color", "blue");
createConfig("extra2.properties", "application.theme.background", "red");
}
@Test
public void givenEnvironmentReader_whenColorChanged_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", environmentConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("red", environmentConfigBean.getColor());
}
@Test
public void givenEnvironmentReader_whenBackgroundChanged_thenExpectChangeValue() throws Exception {
Assert.assertEquals("red", environmentConfigBean.getBackgroundColor());
createConfig("extra2.properties", "application.theme.background", "blue");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", environmentConfigBean.getBackgroundColor());
}
@Test
public void givenPropertiesReader_whenColorChanged_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", propertiesConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("red", propertiesConfigBean.getColor());
}
@Test
public void givenRefreshScopedValueReader_whenColorChangedAndRefreshCalled_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", valueRefreshConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", valueRefreshConfigBean.getColor());
callRefresh();
Assert.assertEquals("red", valueRefreshConfigBean.getColor());
}
@Test
public void givenSingletonRefreshScopedValueReader_whenColorChangedAndRefreshCalled_thenExpectOldValue() throws Exception {
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
callRefresh();
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
}
@Test
public void givenRefreshScopedConfigurationPropertiesReader_whenColorChangedAndRefreshCalled_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
callRefresh();
Assert.assertEquals("red", configurationPropertiesRefreshConfigBean.getColor());
}
public void callRefresh() throws Exception {
MvcResult mvcResult = mvc
.perform(MockMvcRequestBuilders
.post("/actuator/refresh")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
Assert.assertEquals(response.getStatus(), 200);
}
public void createConfig(String file, String key, String value) throws Exception {
FileOutputStream fo = new FileOutputStream(file);
fo.write(String
.format("%s=%s", key, value)
.getBytes());
fo.close();
}
}
@@ -0,0 +1,28 @@
package com.baeldung.properties.reloading;
import com.baeldung.properties.reloading.beans.ValueRefreshConfigBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.test.context.web.WebAppConfiguration;
@SpringBootApplication
@WebAppConfiguration
public class SpringBootPropertiesTestApplication {
@Bean("singletonValueRefreshConfigBean")
@RefreshScope
@Scope("singleton")
public ValueRefreshConfigBean singletonValueRefreshConfigBean(@Value("${application.theme.color:null}") String val) {
return new ValueRefreshConfigBean(val);
}
@Bean
@RefreshScope
public ValueRefreshConfigBean valueRefreshConfigBean(@Value("${application.theme.color:null}") String val) {
return new ValueRefreshConfigBean(val);
}
}
@@ -0,0 +1,20 @@
package com.baeldung.properties.reloading.beans;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "application.theme")
@RefreshScope
public class ConfigurationPropertiesRefreshConfigBean {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
@@ -0,0 +1,26 @@
package com.baeldung.properties.reloading.beans;
import com.baeldung.properties.reloading.configs.ReloadablePropertySourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "file:extra2.properties", factory = ReloadablePropertySourceFactory.class)
public class EnvironmentConfigBean {
private Environment environment;
public EnvironmentConfigBean(@Autowired Environment environment) {
this.environment = environment;
}
public String getColor() {
return environment.getProperty("application.theme.color");
}
public String getBackgroundColor() {
return environment.getProperty("application.theme.background");
}
}
@@ -0,0 +1,19 @@
package com.baeldung.properties.reloading.beans;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PropertiesConfigBean {
private Properties properties;
public PropertiesConfigBean(@Autowired Properties properties) {
this.properties = properties;
}
public String getColor() {
return properties.getProperty("application.theme.color");
}
}
@@ -0,0 +1,13 @@
package com.baeldung.properties.reloading.beans;
public class ValueRefreshConfigBean {
private String color;
public ValueRefreshConfigBean(String color) {
this.color = color;
}
public String getColor() {
return color;
}
}
@@ -0,0 +1,24 @@
package com.baeldung.properties.testproperty;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@TestPropertySource("/foo.properties")
public class FilePropertyInjectionUnitTest {
@Value("${foo}")
private String foo;
@Test
public void whenFilePropertyProvided_thenProperlyInjected() {
assertThat(foo).isEqualTo("bar");
}
}
@@ -0,0 +1,23 @@
package com.baeldung.properties.testproperty;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@TestPropertySource(properties = {"foo=bar"})
public class PropertyInjectionUnitTest {
@Value("${foo}")
private String foo;
@Test
public void whenPropertyProvided_thenProperlyInjected() {
assertThat(foo).isEqualTo("bar");
}
}
@@ -0,0 +1,23 @@
package com.baeldung.properties.testproperty;
import com.baeldung.properties.reloading.SpringBootPropertiesTestApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class)
public class SpringBootPropertyInjectionIntegrationTest {
@Value("${foo}")
private String foo;
@Test
public void whenSpringBootPropertyProvided_thenProperlyInjected() {
assertThat(foo).isEqualTo("bar");
}
}
@@ -0,0 +1,25 @@
package com.baeldung.test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.baeldung.properties.basic.ExtendedPropertiesWithJavaIntegrationTest;
import com.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest;
import com.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
@RunWith(Suite.class)
@SuiteClasses({ //@formatter:off
PropertiesWithXmlIntegrationTest.class,
ExternalPropertiesWithJavaIntegrationTest.class,
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
ExternalPropertiesWithXmlManualTest.class,
ExtendedPropertiesWithJavaIntegrationTest.class,
PropertiesWithMultipleXmlsIntegrationTest.class,
})// @formatter:on
public final class IntegrationTestSuite {
//
}
@@ -0,0 +1,40 @@
package com.baeldung.value;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import static junit.framework.TestCase.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
public class ClassNotManagedBySpringIntegrationTest {
@MockBean
private InitializerBean initializerBean;
@Before
public void init() {
when(initializerBean.initClass())
.thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value"));
}
@Test
public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception {
//given
ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass();
//when
String initializedValue = classNotManagedBySpring.getCustomVariable();
String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable();
//then
assertEquals("This is only sample value", initializedValue);
assertEquals("Another configured value", anotherCustomVariable);
}
}