parent child context test

This commit is contained in:
DOHA 2015-07-11 17:53:19 +02:00
parent f4460a003a
commit 671d78b701
10 changed files with 204 additions and 23 deletions

View File

@ -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 {
}

View File

@ -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 {
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}