Skip to content

Commit 2245240

Browse files
author
Eugen
committed
Merge pull request eugenp#223 from Doha2012/master
parent child context test
2 parents f03d62c + 671d78b commit 2245240

10 files changed

Lines changed: 204 additions & 23 deletions

‎spring-all/src/test/java/org/baeldung/properties/config/ChildConfig.java‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎spring-all/src/test/java/org/baeldung/properties/config/ParentConfig.java‎

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.properties.parentchild;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class ChildValueHolder {
8+
@Value("${parent.name:-}")
9+
private String parentName;
10+
11+
@Value("${child.name:-}")
12+
private String childName;
13+
14+
15+
public String getParentName() {
16+
return parentName;
17+
}
18+
19+
public String getChildName() {
20+
return childName;
21+
}
22+
23+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.baeldung.properties.parentchild;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
6+
import org.baeldung.properties.parentchild.config.ChildConfig2;
7+
import org.baeldung.properties.parentchild.config.ParentConfig2;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.core.env.Environment;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.ContextHierarchy;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
import org.springframework.test.context.web.WebAppConfiguration;
16+
import org.springframework.web.context.WebApplicationContext;
17+
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@WebAppConfiguration
20+
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig2.class), @ContextConfiguration(classes = ChildConfig2.class) })
21+
public class ParentChildPropertyPlaceHolderPropertiesTest {
22+
23+
@Autowired
24+
private WebApplicationContext wac;
25+
26+
@Test
27+
public void givenPropertyPlaceHolder_whenGetPropertyUsingEnv_thenCorrect() {
28+
final Environment childEnv = wac.getEnvironment();
29+
final Environment parentEnv = wac.getParent().getEnvironment();
30+
31+
assertNull(parentEnv.getProperty("parent.name"));
32+
assertNull(parentEnv.getProperty("child.name"));
33+
34+
assertNull(childEnv.getProperty("parent.name"));
35+
assertNull(childEnv.getProperty("child.name"));
36+
}
37+
38+
@Test
39+
public void givenPropertyPlaceHolder_whenGetPropertyUsingValueAnnotation_thenCorrect() {
40+
final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class);
41+
final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class);
42+
43+
assertEquals(parentValueHolder.getParentName(), "parent");
44+
assertEquals(parentValueHolder.getChildName(), "-");
45+
46+
assertEquals(childValueHolder.getParentName(), "-");
47+
assertEquals(childValueHolder.getChildName(), "child");
48+
}
49+
50+
}

spring-all/src/test/java/org/baeldung/properties/config/ParentChildPropertiesTest.java renamed to spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package org.baeldung.properties.config;
1+
package org.baeldung.properties.parentchild;
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNull;
55

6+
import org.baeldung.properties.parentchild.config.ChildConfig;
7+
import org.baeldung.properties.parentchild.config.ParentConfig;
68
import org.junit.Test;
79
import org.junit.runner.RunWith;
810
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,13 +18,13 @@
1618
@RunWith(SpringJUnit4ClassRunner.class)
1719
@WebAppConfiguration
1820
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextConfiguration(classes = ChildConfig.class) })
19-
public class ParentChildPropertiesTest {
21+
public class ParentChildPropertySourcePropertiesTest {
2022

2123
@Autowired
2224
private WebApplicationContext wac;
2325

2426
@Test
25-
public void givenParentPropertySource_whenGetValue_thenCorrect() {
27+
public void givenPropertySource_whenGetPropertyUsingEnv_thenCorrect() {
2628
final Environment childEnv = wac.getEnvironment();
2729
final Environment parentEnv = wac.getParent().getEnvironment();
2830

@@ -32,4 +34,17 @@ public void givenParentPropertySource_whenGetValue_thenCorrect() {
3234
assertEquals(childEnv.getProperty("parent.name"), "parent");
3335
assertEquals(childEnv.getProperty("child.name"), "child");
3436
}
37+
38+
@Test
39+
public void givenPropertySource_whenGetPropertyUsingValueAnnotation_thenCorrect() {
40+
final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class);
41+
final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class);
42+
43+
assertEquals(parentValueHolder.getParentName(), "parent");
44+
assertEquals(parentValueHolder.getChildName(), "-");
45+
46+
assertEquals(childValueHolder.getParentName(), "parent");
47+
assertEquals(childValueHolder.getChildName(), "child");
48+
}
49+
3550
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.properties.parentchild;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class ParentValueHolder {
8+
@Value("${parent.name:-}")
9+
private String parentName;
10+
11+
@Value("${child.name:-}")
12+
private String childName;
13+
14+
15+
public String getParentName() {
16+
return parentName;
17+
}
18+
19+
public String getChildName() {
20+
return childName;
21+
}
22+
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.properties.parentchild.config;
2+
3+
import org.baeldung.properties.parentchild.ChildValueHolder;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.PropertySource;
7+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
8+
9+
@Configuration
10+
@PropertySource("classpath:child.properties")
11+
public class ChildConfig {
12+
@Bean
13+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
14+
return new PropertySourcesPlaceholderConfigurer();
15+
}
16+
17+
@Bean
18+
public ChildValueHolder childValueHolder() {
19+
return new ChildValueHolder();
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.properties.parentchild.config;
2+
3+
import org.baeldung.properties.parentchild.ChildValueHolder;
4+
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.core.io.ClassPathResource;
8+
9+
@Configuration
10+
public class ChildConfig2 {
11+
12+
@Bean
13+
public ChildValueHolder childValueHolder() {
14+
return new ChildValueHolder();
15+
}
16+
17+
@Bean
18+
public static PropertyPlaceholderConfigurer configurer() {
19+
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
20+
ppc.setLocations(new ClassPathResource("child.properties"));
21+
return ppc;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.properties.parentchild.config;
2+
3+
import org.baeldung.properties.parentchild.ParentValueHolder;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.PropertySource;
7+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
8+
9+
@Configuration
10+
@PropertySource("classpath:parent.properties")
11+
public class ParentConfig {
12+
13+
@Bean
14+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
15+
return new PropertySourcesPlaceholderConfigurer();
16+
}
17+
18+
@Bean
19+
public ParentValueHolder parentValueHolder() {
20+
return new ParentValueHolder();
21+
}
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.properties.parentchild.config;
2+
3+
import org.baeldung.properties.parentchild.ParentValueHolder;
4+
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.core.io.ClassPathResource;
8+
9+
@Configuration
10+
public class ParentConfig2 {
11+
12+
@Bean
13+
public ParentValueHolder parentValueHolder() {
14+
return new ParentValueHolder();
15+
}
16+
17+
@Bean
18+
public static PropertyPlaceholderConfigurer configurer() {
19+
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
20+
ppc.setLocations(new ClassPathResource("parent.properties"));
21+
return ppc;
22+
}
23+
}

0 commit comments

Comments
 (0)