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
40a7445f
Commit
40a7445f
authored
Nov 26, 2014
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.1.x'
parents
8ee237a9
64599261
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
7 deletions
+90
-7
MongoProperties.java
...ngframework/boot/autoconfigure/mongo/MongoProperties.java
+20
-7
MongoPropertiesTests.java
...mework/boot/autoconfigure/mongo/MongoPropertiesTests.java
+70
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java
View file @
40a7445f
...
@@ -35,13 +35,16 @@ import com.mongodb.ServerAddress;
...
@@ -35,13 +35,16 @@ import com.mongodb.ServerAddress;
* @author Dave Syer
* @author Dave Syer
* @author Phillip Webb
* @author Phillip Webb
* @author Josh Long
* @author Josh Long
* @author Andy Wilkinson
*/
*/
@ConfigurationProperties
(
prefix
=
"spring.data.mongodb"
)
@ConfigurationProperties
(
prefix
=
"spring.data.mongodb"
)
public
class
MongoProperties
{
public
class
MongoProperties
{
private
static
final
int
DEFAULT_PORT
=
27017
;
private
String
host
;
private
String
host
;
private
int
port
=
27017
;
private
Integer
port
=
null
;
private
String
uri
=
"mongodb://localhost/test"
;
private
String
uri
=
"mongodb://localhost/test"
;
...
@@ -112,11 +115,11 @@ public class MongoProperties {
...
@@ -112,11 +115,11 @@ public class MongoProperties {
this
.
uri
=
uri
;
this
.
uri
=
uri
;
}
}
public
int
getPort
()
{
public
Integer
getPort
()
{
return
this
.
port
;
return
this
.
port
;
}
}
public
void
setPort
(
int
port
)
{
public
void
setPort
(
Integer
port
)
{
this
.
port
=
port
;
this
.
port
=
port
;
}
}
...
@@ -138,17 +141,19 @@ public class MongoProperties {
...
@@ -138,17 +141,19 @@ public class MongoProperties {
public
MongoClient
createMongoClient
(
MongoClientOptions
options
)
public
MongoClient
createMongoClient
(
MongoClientOptions
options
)
throws
UnknownHostException
{
throws
UnknownHostException
{
try
{
try
{
if
(
this
.
host
!=
null
)
{
if
(
customAddress
()
||
customCredentials
()
)
{
if
(
options
==
null
)
{
if
(
options
==
null
)
{
options
=
MongoClientOptions
.
builder
().
build
();
options
=
MongoClientOptions
.
builder
().
build
();
}
}
List
<
MongoCredential
>
credentials
=
null
;
List
<
MongoCredential
>
credentials
=
null
;
if
(
this
.
password
!=
null
&&
this
.
username
!=
null
)
{
if
(
customCredentials
()
)
{
credentials
=
Arrays
.
asList
(
MongoCredential
.
createMongoCRCredential
(
credentials
=
Arrays
.
asList
(
MongoCredential
.
createMongoCRCredential
(
this
.
username
,
getMongoClientDatabase
(),
this
.
password
));
this
.
username
,
getMongoClientDatabase
(),
this
.
password
));
}
}
return
new
MongoClient
(
Arrays
.
asList
(
new
ServerAddress
(
this
.
host
,
String
host
=
this
.
host
==
null
?
"localhost"
:
this
.
host
;
this
.
port
)),
credentials
,
options
);
int
port
=
this
.
port
==
null
?
DEFAULT_PORT
:
this
.
port
;
return
new
MongoClient
(
Arrays
.
asList
(
new
ServerAddress
(
host
,
port
)),
credentials
,
options
);
}
}
// The options and credentials are in the URI
// The options and credentials are in the URI
return
new
MongoClient
(
new
MongoClientURI
(
this
.
uri
,
builder
(
options
)));
return
new
MongoClient
(
new
MongoClientURI
(
this
.
uri
,
builder
(
options
)));
...
@@ -158,6 +163,14 @@ public class MongoProperties {
...
@@ -158,6 +163,14 @@ public class MongoProperties {
}
}
}
}
private
boolean
customAddress
()
{
return
this
.
host
!=
null
||
this
.
port
!=
null
;
}
private
boolean
customCredentials
()
{
return
this
.
username
!=
null
&&
this
.
password
!=
null
;
}
private
Builder
builder
(
MongoClientOptions
options
)
{
private
Builder
builder
(
MongoClientOptions
options
)
{
Builder
builder
=
MongoClientOptions
.
builder
();
Builder
builder
=
MongoClientOptions
.
builder
();
if
(
options
!=
null
)
{
if
(
options
!=
null
)
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java
View file @
40a7445f
...
@@ -16,19 +16,29 @@
...
@@ -16,19 +16,29 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
mongo
;
package
org
.
springframework
.
boot
.
autoconfigure
.
mongo
;
import
java.net.UnknownHostException
;
import
java.util.List
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
com.mongodb.MongoClient
;
import
com.mongodb.MongoCredential
;
import
com.mongodb.ServerAddress
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
hasSize
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
/**
* Tests for {@link MongoProperties}.
* Tests for {@link MongoProperties}.
*
*
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
*/
*/
public
class
MongoPropertiesTests
{
public
class
MongoPropertiesTests
{
...
@@ -43,6 +53,66 @@ public class MongoPropertiesTests {
...
@@ -43,6 +53,66 @@ public class MongoPropertiesTests {
assertThat
(
properties
.
getPassword
(),
equalTo
(
"word"
.
toCharArray
()));
assertThat
(
properties
.
getPassword
(),
equalTo
(
"word"
.
toCharArray
()));
}
}
@Test
public
void
portCanBeCustomized
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setPort
(
12345
);
MongoClient
client
=
properties
.
createMongoClient
(
null
);
List
<
ServerAddress
>
allAddresses
=
client
.
getAllAddress
();
assertThat
(
allAddresses
,
hasSize
(
1
));
assertServerAddress
(
allAddresses
.
get
(
0
),
"localhost"
,
12345
);
}
@Test
public
void
hostCanBeCustomized
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setHost
(
"mongo.example.com"
);
MongoClient
client
=
properties
.
createMongoClient
(
null
);
List
<
ServerAddress
>
allAddresses
=
client
.
getAllAddress
();
assertThat
(
allAddresses
,
hasSize
(
1
));
assertServerAddress
(
allAddresses
.
get
(
0
),
"mongo.example.com"
,
27017
);
}
@Test
public
void
credentialsCanBeCustomized
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setUsername
(
"user"
);
properties
.
setPassword
(
"secret"
.
toCharArray
());
MongoClient
client
=
properties
.
createMongoClient
(
null
);
assertMongoCredential
(
client
.
getCredentialsList
().
get
(
0
),
"user"
,
"secret"
);
}
@Test
public
void
uriCanBeCustomized
()
throws
UnknownHostException
{
MongoProperties
properties
=
new
MongoProperties
();
properties
.
setUri
(
"mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test"
);
MongoClient
client
=
properties
.
createMongoClient
(
null
);
List
<
ServerAddress
>
allAddresses
=
client
.
getAllAddress
();
assertEquals
(
2
,
allAddresses
.
size
());
assertServerAddress
(
allAddresses
.
get
(
0
),
"mongo1.example.com"
,
12345
);
assertServerAddress
(
allAddresses
.
get
(
1
),
"mongo2.example.com"
,
23456
);
List
<
MongoCredential
>
credentialsList
=
client
.
getCredentialsList
();
assertEquals
(
1
,
credentialsList
.
size
());
assertMongoCredential
(
credentialsList
.
get
(
0
),
"user"
,
"secret"
);
}
private
void
assertServerAddress
(
ServerAddress
serverAddress
,
String
expectedHost
,
int
expectedPort
)
{
assertThat
(
serverAddress
.
getHost
(),
equalTo
(
expectedHost
));
assertThat
(
serverAddress
.
getPort
(),
equalTo
(
expectedPort
));
}
private
void
assertMongoCredential
(
MongoCredential
credentials
,
String
expectedUsername
,
String
expectedPassword
)
{
assertThat
(
credentials
.
getUserName
(),
equalTo
(
expectedUsername
));
assertThat
(
credentials
.
getPassword
(),
equalTo
(
expectedPassword
.
toCharArray
()));
}
@Configuration
@Configuration
@EnableConfigurationProperties
(
MongoProperties
.
class
)
@EnableConfigurationProperties
(
MongoProperties
.
class
)
static
class
Conf
{
static
class
Conf
{
...
...
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