Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
7edfb1aa
Unverified
Commit
7edfb1aa
authored
Feb 20, 2018
by
Jason Song
Committed by
GitHub
Feb 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #967 from nobodyiam/nested-property-test
add nested property test
parents
cf3194b2
61a5aad5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
127 additions
and
0 deletions
+127
-0
JavaConfigPlaceholderTest.java
...ip/framework/apollo/spring/JavaConfigPlaceholderTest.java
+127
-0
No files found.
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigPlaceholderTest.java
View file @
7edfb1aa
...
@@ -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
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment