Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
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
DEMO
spring-boot
Commits
06572b01
Commit
06572b01
authored
Jan 08, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
f527c4b8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
100 deletions
+61
-100
MongoAutoConfigurationTests.java
...boot/autoconfigure/mongo/MongoAutoConfigurationTests.java
+25
-41
MongoReactiveAutoConfigurationTests.java
...oconfigure/mongo/MongoReactiveAutoConfigurationTests.java
+36
-59
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfigurationTests.java
View file @
06572b01
...
...
@@ -20,12 +20,10 @@ import javax.net.SocketFactory;
import
com.mongodb.MongoClient
;
import
com.mongodb.MongoClientOptions
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -40,58 +38,44 @@ import static org.mockito.Mockito.mock;
*/
public
class
MongoAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
MongoAutoConfiguration
.
class
));
@Test
public
void
clientExists
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeanNamesForType
(
MongoClient
.
class
)).
hasSize
(
1
);
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
));
}
@Test
public
void
optionsAdded
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.host:localhost"
).
applyTo
(
this
.
context
);
this
.
context
.
register
(
OptionsConfig
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
MongoClient
.
class
).
getMongoClientOptions
()
.
getSocketTimeout
()).
isEqualTo
(
300
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.host:localhost"
)
.
withUserConfiguration
(
OptionsConfig
.
class
).
run
((
context
)
->
assertThat
(
context
.
getBean
(
MongoClient
.
class
).
getMongoClientOptions
()
.
getSocketTimeout
()).
isEqualTo
(
300
));
}
@Test
public
void
optionsAddedButNoHost
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
applyTo
(
this
.
context
);
this
.
context
.
register
(
OptionsConfig
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
MongoClient
.
class
).
getMongoClientOptions
()
.
getSocketTimeout
()).
isEqualTo
(
300
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
withUserConfiguration
(
OptionsConfig
.
class
).
run
((
context
)
->
assertThat
(
context
.
getBean
(
MongoClient
.
class
).
getMongoClientOptions
()
.
getSocketTimeout
()).
isEqualTo
(
300
));
}
@Test
public
void
optionsSslConfig
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
applyTo
(
this
.
context
);
this
.
context
.
register
(
SslOptionsConfig
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
);
this
.
context
.
refresh
();
MongoClient
mongo
=
this
.
context
.
getBean
(
MongoClient
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
withUserConfiguration
(
SslOptionsConfig
.
class
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
);
MongoClient
mongo
=
context
.
getBean
(
MongoClient
.
class
);
MongoClientOptions
options
=
mongo
.
getMongoClientOptions
();
assertThat
(
options
.
isSslEnabled
()).
isTrue
();
assertThat
(
options
.
getSocketFactory
())
.
isSameAs
(
this
.
context
.
getBean
(
"mySocketFactory"
));
.
isSameAs
(
context
.
getBean
(
"mySocketFactory"
));
});
}
@Configuration
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java
View file @
06572b01
...
...
@@ -24,12 +24,10 @@ import com.mongodb.connection.SocketSettings;
import
com.mongodb.connection.StreamFactory
;
import
com.mongodb.connection.StreamFactoryFactory
;
import
com.mongodb.reactivestreams.client.MongoClient
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -42,83 +40,62 @@ import static org.mockito.Mockito.mock;
* Tests for {@link MongoReactiveAutoConfiguration}.
*
* @author Mark Paluch
* @author Stephane Nicoll
*/
public
class
MongoReactiveAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
MongoReactiveAutoConfiguration
.
class
));
@Test
public
void
clientExists
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoReactiveAutoConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeanNamesForType
(
MongoClient
.
class
).
length
)
.
isEqualTo
(
1
);
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
));
}
@Test
public
void
optionsAdded
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.host:localhost"
).
applyTo
(
this
.
context
);
this
.
context
.
register
(
OptionsConfig
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
,
MongoReactiveAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
MongoClient
.
class
).
getSettings
()
.
getSocketSettings
().
getReadTimeout
(
TimeUnit
.
SECONDS
)).
isEqualTo
(
300
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.host:localhost"
)
.
withUserConfiguration
(
OptionsConfig
.
class
).
run
((
context
)
->
assertThat
(
context
.
getBean
(
MongoClient
.
class
).
getSettings
()
.
getSocketSettings
().
getReadTimeout
(
TimeUnit
.
SECONDS
))
.
isEqualTo
(
300
));
}
@Test
public
void
optionsAddedButNoHost
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
applyTo
(
this
.
context
);
this
.
context
.
register
(
OptionsConfig
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
,
MongoReactiveAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
MongoClient
.
class
).
getSettings
().
getReadPreference
())
.
isEqualTo
(
ReadPreference
.
nearest
());
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
withUserConfiguration
(
OptionsConfig
.
class
).
run
((
context
)
->
assertThat
(
context
.
getBean
(
MongoClient
.
class
).
getSettings
()
.
getReadPreference
()).
isEqualTo
(
ReadPreference
.
nearest
()));
}
@Test
public
void
optionsSslConfig
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
applyTo
(
this
.
context
);
this
.
context
.
register
(
SslOptionsConfig
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
,
MongoReactiveAutoConfiguration
.
class
);
this
.
context
.
refresh
();
MongoClient
mongo
=
this
.
context
.
getBean
(
MongoClient
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.uri:mongodb://localhost/test"
)
.
withUserConfiguration
(
SslOptionsConfig
.
class
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
);
MongoClient
mongo
=
context
.
getBean
(
MongoClient
.
class
);
MongoClientSettings
settings
=
mongo
.
getSettings
();
assertThat
(
settings
.
getApplicationName
()).
isEqualTo
(
"test-config"
);
assertThat
(
settings
.
getStreamFactoryFactory
())
.
isSameAs
(
this
.
context
.
getBean
(
"myStreamFactoryFactory"
));
.
isSameAs
(
context
.
getBean
(
"myStreamFactoryFactory"
));
});
}
@Test
public
void
customizerOverridesAutoConfig
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.uri:mongodb://localhost/test?appname=auto-config"
)
.
applyTo
(
this
.
context
);
this
.
context
.
register
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoReactiveAutoConfiguration
.
class
,
SimpleCustomizerConfig
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeanNamesForType
(
MongoClient
.
class
).
length
)
.
isEqualTo
(
1
);
MongoClient
client
=
this
.
context
.
getBean
(
MongoClient
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.uri:mongodb://localhost/test?appname=auto-config"
)
.
withUserConfiguration
(
SimpleCustomizerConfig
.
class
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
);
MongoClient
client
=
context
.
getBean
(
MongoClient
.
class
);
assertThat
(
client
.
getSettings
().
getApplicationName
())
.
isEqualTo
(
"overridden-name"
);
});
}
@Configuration
...
...
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