Skip to content

Commit 1e40301

Browse files
committed
Fixed tests that only run against replica sets and were failing with auth enabled
Conflicts: src/test/com/mongodb/DBCollectionTest.java
1 parent c3c9fcd commit 1e40301

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

src/test/com/mongodb/AggregationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void testDollarOutOnSecondary() throws UnknownHostException {
189189
assumeTrue(isReplicaSet(cleanupMongo));
190190

191191
ServerAddress primary = new ServerAddress("localhost");
192-
MongoClient rsClient = new MongoClient(asList(primary, new ServerAddress("localhost", 27018)));
192+
MongoClient rsClient = new MongoClient(getMongoClientURI());
193193
DB rsDatabase = rsClient.getDB(database.getName());
194194
DBCollection aggCollection = rsDatabase.getCollection(collection.getName());
195195
aggCollection.drop();

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public void testFindAndReplaceA16MDocument() {
525525
@Test
526526
public void testWriteConcernExceptionOnInsert() throws UnknownHostException {
527527
assumeTrue(isReplicaSet(getMongoClient()));
528-
MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress()));
528+
MongoClient mongoClient = new MongoClient(getMongoClientURI());
529529
try {
530530
DBCollection localCollection = mongoClient.getDB(collection.getDB().getName()).getCollection(collection.getName());
531531
WriteResult writeResult = localCollection.insert(new BasicDBObject(), new WriteConcern(5, 1, false, false));
@@ -541,7 +541,7 @@ public void testWriteConcernExceptionOnInsert() throws UnknownHostException {
541541
@Test
542542
public void testWriteConcernExceptionOnUpdate() throws UnknownHostException {
543543
assumeTrue(isReplicaSet(getMongoClient()));
544-
MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress()));
544+
MongoClient mongoClient = new MongoClient(getMongoClientURI());
545545
ObjectId id = new ObjectId();
546546
try {
547547
DBCollection localCollection = mongoClient.getDB(collection.getDB().getName()).getCollection(collection.getName());
@@ -562,7 +562,7 @@ public void testWriteConcernExceptionOnUpdate() throws UnknownHostException {
562562
@Test
563563
public void testWriteConcernExceptionOnRemove() throws UnknownHostException {
564564
assumeTrue(isReplicaSet(getMongoClient()));
565-
MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress()));
565+
MongoClient mongoClient = new MongoClient(getMongoClientURI());
566566
try {
567567
DBCollection localCollection = mongoClient.getDB(collection.getDB().getName()).getCollection(collection.getName());
568568
localCollection.insert(new BasicDBObject());
@@ -578,7 +578,7 @@ public void testWriteConcernExceptionOnRemove() throws UnknownHostException {
578578
@Test
579579
public void testBulkWriteConcernException() throws UnknownHostException {
580580
assumeTrue(isReplicaSet(getMongoClient()));
581-
MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress()));
581+
MongoClient mongoClient = new MongoClient(getMongoClientURI());
582582
try {
583583
DBCollection localCollection = mongoClient.getDB(collection.getDB().getName()).getCollection(collection.getName());
584584
BulkWriteOperation bulkWriteOperation = localCollection.initializeUnorderedBulkOperation();

src/test/com/mongodb/DBTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ private String getExpectedRole(final boolean isReadOnly, final DB database) {
568568

569569

570570
private DB getReplicaSetDB() throws UnknownHostException {
571-
Mongo mongo = new MongoClient(Arrays.asList(new ServerAddress("127.0.0.1"), new ServerAddress("127.0.0.1", 27018)));
571+
Mongo mongo = new MongoClient(getMongoClientURI());
572572
return mongo.getDB("database-" + System.nanoTime());
573573
}
574574

src/test/com/mongodb/DBTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.junit.Test;
2121

2222
import java.net.UnknownHostException;
23-
import java.util.Arrays;
2423

24+
import static java.util.Arrays.asList;
2525
import static org.junit.Assert.assertEquals;
2626
import static org.junit.Assert.assertFalse;
2727
import static org.junit.Assert.assertTrue;
@@ -122,12 +122,16 @@ public void testGetCollectionNamesToSecondary() throws MongoException, UnknownHo
122122
return;
123123
}
124124

125-
Mongo mongo = new MongoClient(Arrays.asList(new ServerAddress("127.0.0.1"), new ServerAddress("127.0.0.1", 27018)));
125+
Mongo mongo = new MongoClient(getMongoClientURI());
126126

127127
try {
128128
String secondary = getASecondaryAsString(mongo);
129129
mongo.close();
130-
mongo = new MongoClient(secondary);
130+
if (getMongoClientURI().getCredentials() == null) {
131+
mongo = new MongoClient(secondary);
132+
} else {
133+
mongo = new MongoClient(new ServerAddress(secondary), asList(getMongoClientURI().getCredentials()));
134+
}
131135
DB db = mongo.getDB("secondaryTest");
132136
db.setReadPreference(ReadPreference.secondary());
133137
db.getCollectionNames();

src/test/com/mongodb/JavaClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,8 @@ public void testMapReduceInlineSecondary() throws Exception {
492492
return;
493493
}
494494

495-
Mongo mongo = new MongoClient(asList(new ServerAddress("127.0.0.1", 27017), new ServerAddress("127.0.0.1", 27018)),
496-
MongoClientOptions.builder().writeConcern(WriteConcern.UNACKNOWLEDGED).build());
495+
Mongo mongo = new MongoClient(getMongoClientURI());
496+
mongo.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
497497

498498
int size = getReplicaSetSize(mongo);
499499
DBCollection c = mongo.getDB(getDatabase().getName()).getCollection( "imr2" );

src/test/com/mongodb/SecondaryReadTest.java

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

2626
import static java.lang.String.format;
27+
import static java.util.Arrays.asList;
2728
import static org.junit.Assert.assertTrue;
2829

2930
public class SecondaryReadTest extends TestCase {
@@ -163,8 +164,7 @@ private boolean serverIsSecondary(final ServerAddress pServerAddr, final List<Te
163164
}
164165

165166
private Mongo loadMongo() throws Exception {
166-
return new MongoClient(new MongoClientURI(
167-
"mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/?connectTimeoutMS=30000;socketTimeoutMS=30000;maxpoolsize=5;autoconnectretry=true"));
167+
return new MongoClient(getMongoClientURI());
168168
}
169169

170170
@SuppressWarnings({"unchecked"})
@@ -180,8 +180,7 @@ private List<TestHost> extractHosts(Mongo mongo) {
180180
hostnameAndPort = hostnameAndPort + ":27017";
181181
}
182182

183-
final String stateStr = member.getString("stateStr");
184-
183+
String stateStr = member.getString("stateStr");
185184
pHosts.add(new TestHost(hostnameAndPort, stateStr));
186185
}
187186

@@ -237,19 +236,25 @@ private void verifySecondaryCounts(final int pSecondaryCount, final List<TestHos
237236
}
238237

239238
private static void loadQueryCount(final List<TestHost> pHosts, final boolean pBefore) throws Exception {
240-
for (final TestHost testHost : pHosts) {
241-
final Mongo mongoHost = new MongoClient(new MongoClientURI("mongodb://"+testHost.hostnameAndPort
242-
+ "/?connectTimeoutMS=30000;socketTimeoutMS=30000;maxpoolsize=5;"
243-
+ "autoconnectretry=true"));
239+
for (TestHost testHost : pHosts) {
240+
Mongo mongoHost;
241+
if (getMongoClientURI().getCredentials() == null || testHost.stateStr.equals("ARBITER")) {
242+
mongoHost = new MongoClient(testHost.hostnameAndPort);
243+
} else {
244+
mongoHost = new MongoClient(new ServerAddress(testHost.hostnameAndPort), asList(getMongoClientURI().getCredentials()));
245+
}
244246
mongoHost.setReadPreference(ReadPreference.nearest());
245247
try {
246-
final CommandResult serverStatusResult
247-
= mongoHost.getDB("com_mongodb_unittest_SecondaryReadTest").command(new BasicDBObject("serverStatus", 1));
248+
CommandResult serverStatusResult = mongoHost.getDB("com_mongodb_unittest_SecondaryReadTest")
249+
.command(new BasicDBObject("serverStatus", 1));
248250

249-
final BasicDBObject opcounters = (BasicDBObject)serverStatusResult.get("opcounters");
251+
BasicDBObject opcounters = (BasicDBObject) serverStatusResult.get("opcounters");
250252

251-
if (pBefore) testHost.queriesBefore = opcounters.getLong("query");
252-
else testHost.queriesAfter = opcounters.getLong("query");
253+
if (pBefore) {
254+
testHost.queriesBefore = opcounters.getLong("query");
255+
} else {
256+
testHost.queriesAfter = opcounters.getLong("query");
257+
}
253258

254259
} finally {
255260
mongoHost.close();

0 commit comments

Comments
 (0)