Unverified Commit 7edfb1aa by Jason Song Committed by GitHub

Merge pull request #967 from nobodyiam/nested-property-test

add nested property test
parents cf3194b2 61a5aad5
...@@ -153,6 +153,112 @@ public class JavaConfigPlaceholderTest extends AbstractSpringIntegrationTest { ...@@ -153,6 +153,112 @@ public class JavaConfigPlaceholderTest extends AbstractSpringIntegrationTest {
assertEquals(someBatch, bean.getBatch()); assertEquals(someBatch, bean.getBatch());
} }
@Test
public void testNestedProperty() throws Exception {
String a = "a";
String b = "b";
int someValue = 1234;
Config config = mock(Config.class);
when(config.getProperty(eq(a), anyString())).thenReturn(a);
when(config.getProperty(eq(b), anyString())).thenReturn(b);
when(config.getProperty(eq(String.format("%s.%s", a, b)), anyString()))
.thenReturn(String.valueOf(someValue));
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NestedPropertyConfig1.class);
TestNestedPropertyBean bean = context.getBean(TestNestedPropertyBean.class);
assertEquals(someValue, bean.getNestedProperty());
}
@Test
public void testNestedPropertyWithDefaultValue() throws Exception {
String a = "a";
String b = "b";
String c = "c";
int someValue = 1234;
Config config = mock(Config.class);
when(config.getProperty(eq(a), anyString())).thenReturn(a);
when(config.getProperty(eq(b), anyString())).thenReturn(b);
when(config.getProperty(eq(c), anyString())).thenReturn(String.valueOf(someValue));
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NestedPropertyConfig1.class);
TestNestedPropertyBean bean = context.getBean(TestNestedPropertyBean.class);
assertEquals(someValue, bean.getNestedProperty());
}
@Test
public void testNestedPropertyWithNestedDefaultValue() throws Exception {
String a = "a";
String b = "b";
Config config = mock(Config.class);
when(config.getProperty(eq(a), anyString())).thenReturn(a);
when(config.getProperty(eq(b), anyString())).thenReturn(b);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NestedPropertyConfig1.class);
TestNestedPropertyBean bean = context.getBean(TestNestedPropertyBean.class);
assertEquals(100, bean.getNestedProperty());
}
@Test
public void testMultipleNestedProperty() throws Exception {
String a = "a";
String b = "b";
String nestedKey = "c.d";
String nestedProperty = String.format("${%s}", nestedKey);
int someValue = 1234;
Config config = mock(Config.class);
when(config.getProperty(eq(a), anyString())).thenReturn(a);
when(config.getProperty(eq(b), anyString())).thenReturn(b);
when(config.getProperty(eq(String.format("%s.%s", a, b)), anyString())).thenReturn(nestedProperty);
when(config.getProperty(eq(nestedKey), anyString())).thenReturn(String.valueOf(someValue));
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NestedPropertyConfig1.class);
TestNestedPropertyBean bean = context.getBean(TestNestedPropertyBean.class);
assertEquals(someValue, bean.getNestedProperty());
}
@Test
public void testMultipleNestedPropertyWithDefaultValue() throws Exception {
String a = "a";
String b = "b";
String nestedKey = "c.d";
int someValue = 1234;
String nestedProperty = String.format("${%s:%d}", nestedKey, someValue);
Config config = mock(Config.class);
when(config.getProperty(eq(a), anyString())).thenReturn(a);
when(config.getProperty(eq(b), anyString())).thenReturn(b);
when(config.getProperty(eq(String.format("%s.%s", a, b)), anyString())).thenReturn(nestedProperty);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NestedPropertyConfig1.class);
TestNestedPropertyBean bean = context.getBean(TestNestedPropertyBean.class);
assertEquals(someValue, bean.getNestedProperty());
}
private void check(int expectedTimeout, int expectedBatch, Class<?>... annotatedClasses) { private void check(int expectedTimeout, int expectedBatch, Class<?>... annotatedClasses) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses);
...@@ -217,6 +323,16 @@ public class JavaConfigPlaceholderTest extends AbstractSpringIntegrationTest { ...@@ -217,6 +323,16 @@ public class JavaConfigPlaceholderTest extends AbstractSpringIntegrationTest {
} }
} }
@Configuration
@EnableApolloConfig
static class NestedPropertyConfig1 {
@Bean
TestNestedPropertyBean testNestedPropertyBean() {
return new TestNestedPropertyBean();
}
}
@Component @Component
static class TestJavaConfigBean { static class TestJavaConfigBean {
@Value("${timeout:100}") @Value("${timeout:100}")
...@@ -257,4 +373,15 @@ public class JavaConfigPlaceholderTest extends AbstractSpringIntegrationTest { ...@@ -257,4 +373,15 @@ public class JavaConfigPlaceholderTest extends AbstractSpringIntegrationTest {
this.batch = batch; this.batch = batch;
} }
} }
static class TestNestedPropertyBean {
@Value("${${a}.${b}:${c:100}}")
private int nestedProperty;
public int getNestedProperty() {
return nestedProperty;
}
}
} }
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