parent child context test
This commit is contained in:
parent
f4460a003a
commit
671d78b701
|
@ -1,10 +0,0 @@
|
|||
package org.baeldung.properties.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:child.properties")
|
||||
public class ChildConfig {
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package org.baeldung.properties.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:parent.properties")
|
||||
public class ParentConfig {
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package org.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,50 @@
|
|||
package org.baeldung.properties.parentchild;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.baeldung.properties.parentchild.config.ChildConfig2;
|
||||
import org.baeldung.properties.parentchild.config.ParentConfig2;
|
||||
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;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig2.class), @ContextConfiguration(classes = ChildConfig2.class) })
|
||||
public class ParentChildPropertyPlaceHolderPropertiesTest {
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
package org.baeldung.properties.config;
|
||||
package org.baeldung.properties.parentchild;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.baeldung.properties.parentchild.config.ChildConfig;
|
||||
import org.baeldung.properties.parentchild.config.ParentConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -16,13 +18,13 @@ import org.springframework.web.context.WebApplicationContext;
|
|||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextConfiguration(classes = ChildConfig.class) })
|
||||
public class ParentChildPropertiesTest {
|
||||
public class ParentChildPropertySourcePropertiesTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Test
|
||||
public void givenParentPropertySource_whenGetValue_thenCorrect() {
|
||||
public void givenPropertySource_whenGetPropertyUsingEnv_thenCorrect() {
|
||||
final Environment childEnv = wac.getEnvironment();
|
||||
final Environment parentEnv = wac.getParent().getEnvironment();
|
||||
|
||||
|
@ -32,4 +34,17 @@ public class ParentChildPropertiesTest {
|
|||
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,23 @@
|
|||
package org.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,21 @@
|
|||
package org.baeldung.properties.parentchild.config;
|
||||
|
||||
import org.baeldung.properties.parentchild.ChildValueHolder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
@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,23 @@
|
|||
package org.baeldung.properties.parentchild.config;
|
||||
|
||||
import org.baeldung.properties.parentchild.ChildValueHolder;
|
||||
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;
|
||||
|
||||
@Configuration
|
||||
public class ChildConfig2 {
|
||||
|
||||
@Bean
|
||||
public ChildValueHolder childValueHolder() {
|
||||
return new ChildValueHolder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public static PropertyPlaceholderConfigurer configurer() {
|
||||
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||
ppc.setLocations(new ClassPathResource("child.properties"));
|
||||
return ppc;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package org.baeldung.properties.parentchild.config;
|
||||
|
||||
import org.baeldung.properties.parentchild.ParentValueHolder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
@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,23 @@
|
|||
package org.baeldung.properties.parentchild.config;
|
||||
|
||||
import org.baeldung.properties.parentchild.ParentValueHolder;
|
||||
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;
|
||||
|
||||
@Configuration
|
||||
public class ParentConfig2 {
|
||||
|
||||
@Bean
|
||||
public ParentValueHolder parentValueHolder() {
|
||||
return new ParentValueHolder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public static PropertyPlaceholderConfigurer configurer() {
|
||||
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||
ppc.setLocations(new ClassPathResource("parent.properties"));
|
||||
return ppc;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue