Skip to content

Commit 7a6a055

Browse files
committed
Added support in build.gradle for setting the MongoDB URI, and fixed all the tests that failed when that URI contains a credential
1 parent feb81ef commit 7a6a055

File tree

8 files changed

+35
-29
lines changed

8 files changed

+35
-29
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ tasks.withType(AbstractCompile) {
8080
}
8181

8282
test {
83+
systemProperties(
84+
'org.mongodb.test.uri': System.getProperty('org.mongodb.test.uri', null)
85+
)
86+
8387
jvmArgs '-XX:MaxPermSize=1024m'
8488
maxHeapSize = "1024m"
8589

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void testFindOneSort(){
208208
*/
209209
@Test
210210
public void testDropDatabase() throws Exception {
211-
final Mongo mongo = new MongoClient( "127.0.0.1" );
211+
final Mongo mongo = new MongoClient(getMongoClientURI());
212212
mongo.getDB("com_mongodb_unittest_dropDatabaseTest").dropDatabase();
213213
mongo.close();
214214
}

src/test/com/mongodb/DBCursorTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.concurrent.Future;
3232
import java.util.concurrent.TimeoutException;
3333

34+
import static java.util.Arrays.asList;
3435
import static java.util.concurrent.TimeUnit.SECONDS;
3536
import static org.junit.Assert.assertEquals;
3637
import static org.junit.Assert.assertFalse;
@@ -470,7 +471,13 @@ public void testHasFinalizer() throws UnknownHostException {
470471

471472
// finally, no finalizer if disabled in mongo options
472473
MongoClientOptions mongoOptions = new MongoClientOptions.Builder().cursorFinalizerEnabled(false).build();
473-
Mongo m = new MongoClient("127.0.0.1", mongoOptions);
474+
Mongo m;
475+
if (getMongoClientURI().getCredentials() != null) {
476+
m = new MongoClient(new ServerAddress(getMongoClientURI().getHosts().get(0)), asList(getMongoClientURI().getCredentials()),
477+
mongoOptions);
478+
} else {
479+
m = new MongoClient(new ServerAddress(getMongoClientURI().getHosts().get(0)), mongoOptions);
480+
}
474481
try {
475482
c = m.getDB(getDatabase().getName()).getCollection("HasFinalizerTest");
476483
cursor = c.find();

src/test/com/mongodb/DBPortTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
import java.util.HashSet;
2525
import java.util.Set;
2626

27-
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
2829
import static org.junit.Assert.fail;
2930
public class DBPortTest extends TestCase {
3031
@Test
3132
@SuppressWarnings("deprecation")
3233
public void testAuthentication() throws IOException {
33-
Mongo m = new MongoClient();
34+
Mongo m = new MongoClient(getMongoClientURI());
3435
DB db1 = m.getDB("DBPortTest1");
3536
DB db2 = m.getDB("DBPortTest2");
3637
db1.dropDatabase();
@@ -45,16 +46,16 @@ public void testAuthentication() throws IOException {
4546

4647
Set<String> expected = new HashSet<String>();
4748

48-
assertEquals(expected, port.getAuthenticatedDatabases());
49+
assertFalse(port.getAuthenticatedDatabases().contains("DBPortTest1"));
50+
assertFalse(port.getAuthenticatedDatabases().contains("DBPortTest2"));
4951

5052
m.getAuthority().getCredentialsStore().add(MongoCredential.createMongoCRCredential("u1", "DBPortTest1", "e".toCharArray()));
5153
m.getAuthority().getCredentialsStore().add(MongoCredential.createMongoCRCredential("u2", "DBPortTest2", "e".toCharArray()));
5254

5355
port.checkAuth(m);
5456

55-
expected.add("DBPortTest1");
56-
expected.add("DBPortTest2");
57-
assertEquals(expected, port.getAuthenticatedDatabases());
57+
assertTrue(port.getAuthenticatedDatabases().contains("DBPortTest1"));
58+
assertTrue(port.getAuthenticatedDatabases().contains("DBPortTest2"));
5859

5960
m.getAuthority().getCredentialsStore().add(MongoCredential.createMongoCRCredential("u2", "DBPortTest3", "e".toCharArray()));
6061

src/test/com/mongodb/DBTCPConnectorTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.junit.Test;
2727

2828
import java.net.UnknownHostException;
29-
import java.util.Arrays;
3029

3130
import static org.junit.Assert.assertEquals;
3231
import static org.junit.Assert.assertNotEquals;
@@ -46,12 +45,7 @@ public class DBTCPConnectorTest extends TestCase {
4645

4746
@BeforeClass
4847
public static void beforeClass() throws UnknownHostException {
49-
if (isStandalone(getMongoClient())) {
50-
_mongoClient = new MongoClient();
51-
}
52-
else {
53-
_mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost:27017"), new ServerAddress("localhost:27018")));
54-
}
48+
_mongoClient = new MongoClient(getMongoClientURI());
5549
_db = _mongoClient.getDB(getDatabase().getName());
5650
_collection = _db.getCollection(DBTCPConnectorTest.class.getName());
5751
}

src/test/com/mongodb/DBTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,13 @@ public void shouldAddReadWriteAdminUser() throws UnknownHostException {
492492
String userName = "newUser";
493493
String pwd = "pwd";
494494
MongoClient mongoClient = new MongoClient(getMongoClientURI());
495-
DB adminDB = mongoClient.getDB("admin");
496-
adminDB.addUser(userName, pwd.toCharArray(), false);
495+
DB database = mongoClient.getDB(getDatabase().getName());
496+
database.addUser(userName, pwd.toCharArray(), false);
497497
try {
498-
assertTrue(adminDB.authenticate(userName, pwd.toCharArray()));
499-
assertCorrectUserExists(userName, pwd, false, adminDB);
498+
assertTrue(database.authenticate(userName, pwd.toCharArray()));
499+
assertCorrectUserExists(userName, pwd, false, database);
500500
} finally {
501-
adminDB.removeUser(userName);
501+
database.removeUser(userName);
502502
mongoClient.close();
503503
}
504504
}
@@ -510,15 +510,15 @@ public void shouldAddReadOnlyAdminUser() throws UnknownHostException {
510510
String readOnlyUserName = "newUser";
511511
String pwd = "pwd";
512512
MongoClient mongoClient = new MongoClient(getMongoClientURI());
513-
DB adminDB = mongoClient.getDB("admin");
514-
adminDB.addUser(readWriteUserName, pwd.toCharArray(), false);
515-
adminDB.authenticate(readWriteUserName, pwd.toCharArray());
516-
adminDB.addUser(readOnlyUserName, pwd.toCharArray(), true);
513+
DB database = mongoClient.getDB(getDatabase().getName());
514+
database.addUser(readWriteUserName, pwd.toCharArray(), false);
515+
database.authenticate(readWriteUserName, pwd.toCharArray());
516+
database.addUser(readOnlyUserName, pwd.toCharArray(), true);
517517
try {
518-
assertCorrectUserExists(readOnlyUserName, pwd, true, adminDB);
518+
assertCorrectUserExists(readOnlyUserName, pwd, true, database);
519519
} finally {
520-
adminDB.removeUser(readOnlyUserName);
521-
adminDB.removeUser(readWriteUserName);
520+
database.removeUser(readOnlyUserName);
521+
database.removeUser(readWriteUserName);
522522
mongoClient.close();
523523
}
524524
}

src/test/com/mongodb/JavaClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ public void testMulti(){
664664
public void testAuthenticate() throws UnknownHostException {
665665
assertEquals( "26e3d12bd197368526409177b3e8aab6" , getDatabase()._hash( "e" , "j".toCharArray() ) );
666666

667-
Mongo m = new MongoClient();
667+
Mongo m = new MongoClient(getMongoClientURI());
668668
DB db = m.getDB(getDatabase().getName());
669669

670670
try {

src/test/com/mongodb/WriteConcernTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void shouldNotUseServerDefaultForAnyOtherWriteConcern() {
176176
// integration test to ensure that server doesn't mind a getlasterror command with wtimeout but no w.
177177
@Test
178178
public void testGetLastError() throws UnknownHostException {
179-
MongoClient mc = new MongoClient();
179+
MongoClient mc = new MongoClient(getMongoClientURI());
180180
DB db = mc.getDB("WriteConcernTest");
181181
DBCollection collection = db.getCollection("testGetLastError");
182182
try {

0 commit comments

Comments
 (0)