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
a06ec4a8
Commit
a06ec4a8
authored
Oct 10, 2017
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mongo Uri overrides host and port
Fixes gh-4017
parent
ba1cf9a3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
31 deletions
+72
-31
MongoClientFactory.java
...ramework/boot/autoconfigure/mongo/MongoClientFactory.java
+5
-7
MongoClientFactoryTests.java
...ork/boot/autoconfigure/mongo/MongoClientFactoryTests.java
+0
-24
MongoPropertiesTests.java
...mework/boot/autoconfigure/mongo/MongoPropertiesTests.java
+67
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactory.java
View file @
a06ec4a8
...
...
@@ -89,12 +89,11 @@ public class MongoClientFactory {
}
private
MongoClient
createNetworkMongoClient
(
MongoClientOptions
options
)
{
if
(
this
.
properties
.
getUri
()
!=
null
)
{
return
new
MongoClient
(
new
MongoClientURI
(
this
.
properties
.
getUri
(),
builder
(
options
)));
}
if
(
hasCustomAddress
()
||
hasCustomCredentials
())
{
if
(
this
.
properties
.
getUri
()
!=
null
)
{
throw
new
IllegalStateException
(
"Invalid mongo configuration, "
+
"either uri or host/port/credentials must be specified"
);
}
if
(
options
==
null
)
{
options
=
MongoClientOptions
.
builder
().
build
();
}
...
...
@@ -115,9 +114,8 @@ public class MongoClientFactory {
Collections
.
singletonList
(
new
ServerAddress
(
host
,
port
)),
credentials
,
options
);
}
// The options and credentials are in the URI
return
new
MongoClient
(
new
MongoClientURI
(
this
.
properties
.
determineUri
()
,
builder
(
options
)));
new
MongoClientURI
(
MongoProperties
.
DEFAULT_URI
,
builder
(
options
)));
}
private
boolean
hasCustomAddress
()
{
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactoryTests.java
View file @
a06ec4a8
...
...
@@ -117,30 +117,6 @@ public class MongoClientFactoryTests {
assertMongoCredential
(
credentialsList
.
get
(
0
),
"user"
,
"secret"
,
"test"
);
}
@Test
public
void
uriCannotBeSetWithCredentials
()
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setUri
(
"mongodb://127.0.0.1:1234/mydb"
);
properties
.
setUsername
(
"user"
);
properties
.
setPassword
(
"secret"
.
toCharArray
());
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"Invalid mongo configuration, "
+
"either uri or host/port/credentials must be specified"
);
createMongoClient
(
properties
);
}
@Test
public
void
uriCannotBeSetWithHostPort
()
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setUri
(
"mongodb://127.0.0.1:1234/mydb"
);
properties
.
setHost
(
"localhost"
);
properties
.
setPort
(
4567
);
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"Invalid mongo configuration, "
+
"either uri or host/port/credentials must be specified"
);
createMongoClient
(
properties
);
}
@Test
public
void
uriIsIgnoredInEmbeddedMode
()
{
MongoProperties
properties
=
new
MongoProperties
();
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java
View file @
a06ec4a8
...
...
@@ -16,14 +16,21 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
mongo
;
import
java.net.UnknownHostException
;
import
java.util.List
;
import
com.mongodb.MongoClient
;
import
com.mongodb.MongoClientOptions
;
import
com.mongodb.ServerAddress
;
import
com.mongodb.connection.Cluster
;
import
com.mongodb.connection.ClusterSettings
;
import
org.junit.Test
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -106,6 +113,66 @@ public class MongoPropertiesTests {
.
isEqualTo
(
options
.
getRequiredReplicaSetName
());
}
@Test
public
void
uriOverridesHostAndPort
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setHost
(
"localhost"
);
properties
.
setPort
(
27017
);
properties
.
setUri
(
"mongodb://mongo1.example.com:12345"
);
MongoClient
client
=
new
MongoClientFactory
(
properties
,
null
)
.
createMongoClient
(
null
);
List
<
ServerAddress
>
allAddresses
=
extractServerAddresses
(
client
);
assertThat
(
allAddresses
).
hasSize
(
1
);
assertServerAddress
(
allAddresses
.
get
(
0
),
"mongo1.example.com"
,
12345
);
}
@Test
public
void
onlyHostAndPortSetShouldUseThat
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setHost
(
"localhost"
);
properties
.
setPort
(
27017
);
MongoClient
client
=
new
MongoClientFactory
(
properties
,
null
)
.
createMongoClient
(
null
);
List
<
ServerAddress
>
allAddresses
=
extractServerAddresses
(
client
);
assertThat
(
allAddresses
).
hasSize
(
1
);
assertServerAddress
(
allAddresses
.
get
(
0
),
"localhost"
,
27017
);
}
@Test
public
void
onlyUriSetShouldUseThat
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setUri
(
"mongodb://mongo1.example.com:12345"
);
MongoClient
client
=
new
MongoClientFactory
(
properties
,
null
)
.
createMongoClient
(
null
);
List
<
ServerAddress
>
allAddresses
=
extractServerAddresses
(
client
);
assertThat
(
allAddresses
).
hasSize
(
1
);
assertServerAddress
(
allAddresses
.
get
(
0
),
"mongo1.example.com"
,
12345
);
}
@Test
public
void
noCustomAddressAndNoUriUsesDefaultUri
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
MongoClient
client
=
new
MongoClientFactory
(
properties
,
null
)
.
createMongoClient
(
null
);
List
<
ServerAddress
>
allAddresses
=
extractServerAddresses
(
client
);
assertThat
(
allAddresses
).
hasSize
(
1
);
assertServerAddress
(
allAddresses
.
get
(
0
),
"localhost"
,
27017
);
}
private
List
<
ServerAddress
>
extractServerAddresses
(
MongoClient
client
)
{
Cluster
cluster
=
(
Cluster
)
ReflectionTestUtils
.
getField
(
client
,
"cluster"
);
ClusterSettings
clusterSettings
=
(
ClusterSettings
)
ReflectionTestUtils
.
getField
(
cluster
,
"settings"
);
List
<
ServerAddress
>
allAddresses
=
clusterSettings
.
getHosts
();
return
allAddresses
;
}
private
void
assertServerAddress
(
ServerAddress
serverAddress
,
String
expectedHost
,
int
expectedPort
)
{
assertThat
(
serverAddress
.
getHost
()).
isEqualTo
(
expectedHost
);
assertThat
(
serverAddress
.
getPort
()).
isEqualTo
(
expectedPort
);
}
@Configuration
@EnableConfigurationProperties
(
MongoProperties
.
class
)
static
class
Config
{
...
...
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