Skip to content

Commit d22f4b3

Browse files
committed
Modernize InitialDnsSeedlistDiscoveryTest code
1 parent 6973491 commit d22f4b3

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

driver-reactive-streams/src/test/functional/com/mongodb/reactivestreams/client/ReactiveInitialDnsSeedlistDiscoveryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.MongoClientSettings;
2020
import com.mongodb.client.InitialDnsSeedlistDiscoveryTest;
2121
import com.mongodb.client.MongoClient;
22+
import com.mongodb.lang.NonNull;
2223
import com.mongodb.reactivestreams.client.syncadapter.SyncMongoClient;
2324
import org.bson.BsonDocument;
2425
import org.junit.runner.RunWith;
@@ -38,7 +39,7 @@ public ReactiveInitialDnsSeedlistDiscoveryTest(final String filename, final Path
3839
}
3940

4041
@Override
41-
public MongoClient createMongoClient(final MongoClientSettings settings) {
42+
public MongoClient createMongoClient(@NonNull final MongoClientSettings settings) {
4243
return new SyncMongoClient(MongoClients.create(settings));
4344
}
4445
}

driver-sync/src/test/functional/com/mongodb/client/InitialDnsSeedlistDiscoveryTest.java

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
// See https://github.com/mongodb/specifications/tree/master/source/initial-dns-seedlist-discovery/tests
7272
@RunWith(Parameterized.class)
7373
public abstract class InitialDnsSeedlistDiscoveryTest {
74-
private final String filename;
7574
private final Path parentDirectory;
7675
private final String uri;
7776
@Nullable
@@ -85,11 +84,10 @@ public abstract class InitialDnsSeedlistDiscoveryTest {
8584
private final boolean isError;
8685
private final BsonDocument options;
8786

88-
public InitialDnsSeedlistDiscoveryTest(final String filename, final Path parentDirectory, final String uri,
87+
public InitialDnsSeedlistDiscoveryTest(@SuppressWarnings("unused") final String filename, final Path parentDirectory, final String uri,
8988
@Nullable final List<String> seeds, @Nullable final Integer numSeeds,
9089
@Nullable final List<String> hosts, @Nullable final Integer numHosts,
9190
final boolean isError, final BsonDocument options) {
92-
this.filename = filename;
9391
this.parentDirectory = parentDirectory;
9492
this.uri = uri;
9593
this.seeds = seeds;
@@ -121,7 +119,7 @@ public void shouldResolveTxtRecord() throws InterruptedException {
121119
if (isError) {
122120
MongoClient client = null;
123121
try {
124-
final AtomicReference<MongoException> exceptionReference = new AtomicReference<MongoException>();
122+
final AtomicReference<MongoException> exceptionReference = new AtomicReference<>();
125123
final CountDownLatch latch = new CountDownLatch(1);
126124

127125
ConnectionString connectionString;
@@ -131,27 +129,21 @@ public void shouldResolveTxtRecord() throws InterruptedException {
131129
final SslSettings sslSettings = getSslSettings(connectionString);
132130
assumeTrue("SSL settings don't match", getSslSettings().isEnabled() == sslSettings.isEnabled());
133131
settings = MongoClientSettings.builder().applyConnectionString(connectionString)
134-
.applyToSslSettings(new Block<SslSettings.Builder>() {
135-
@Override
136-
public void apply(final SslSettings.Builder builder) {
137-
builder.applySettings(sslSettings);
138-
builder.invalidHostNameAllowed(true);
139-
}
132+
.applyToSslSettings(builder -> {
133+
builder.applySettings(sslSettings);
134+
builder.invalidHostNameAllowed(true);
140135
})
141-
.applyToClusterSettings(new Block<ClusterSettings.Builder>() {
142-
@Override
143-
public void apply(final ClusterSettings.Builder builder) {
144-
builder.serverSelectionTimeout(5, TimeUnit.SECONDS);
145-
builder.addClusterListener(new ClusterListener() {
146-
@Override
147-
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
148-
if (event.getNewDescription().getSrvResolutionException() != null) {
149-
exceptionReference.set(event.getNewDescription().getSrvResolutionException());
150-
latch.countDown();
151-
}
136+
.applyToClusterSettings(builder -> {
137+
builder.serverSelectionTimeout(5, TimeUnit.SECONDS);
138+
builder.addClusterListener(new ClusterListener() {
139+
@Override
140+
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
141+
if (event.getNewDescription().getSrvResolutionException() != null) {
142+
exceptionReference.set(event.getNewDescription().getSrvResolutionException());
143+
latch.countDown();
152144
}
153-
});
154-
}
145+
}
146+
});
155147
})
156148
.build();
157149
} catch (MongoClientException | IllegalArgumentException e) {
@@ -160,7 +152,7 @@ public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event
160152
}
161153
client = createMongoClient(settings);
162154
// Load balancing mode has special rules regarding cluster event publishing, so we can't rely on those here.
163-
// Instead we just try to execute an operation and assert that it throws
155+
// Instead, we just try to execute an operation and assert that it throws
164156
if (settings.getClusterSettings().getMode() == ClusterConnectionMode.LOAD_BALANCED) {
165157
try {
166158
client.getDatabase("admin").runCommand(new Document("ping", 1));
@@ -186,25 +178,33 @@ public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event
186178
ConnectionString connectionString = new ConnectionString(this.uri);
187179

188180
for (Map.Entry<String, BsonValue> entry : options.entrySet()) {
189-
if (entry.getKey().equals("replicaSet")) {
190-
assertEquals(entry.getValue().asString().getValue(), connectionString.getRequiredReplicaSetName());
191-
} else if (entry.getKey().equals("ssl")) {
192-
assertEquals(entry.getValue().asBoolean().getValue(), connectionString.getSslEnabled());
193-
} else if (entry.getKey().equals("authSource")) {
194-
// ignoring authSource for now, because without at least a userName also in the connection string,
195-
// the authSource is ignored. If the test gets this far, at least we know that a TXT record
196-
// containing in authSource doesn't blow up. We just don't test that it's actually used.
197-
assertTrue(true);
198-
} else if (entry.getKey().equals("directConnection")) {
199-
assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isDirectConnection());
200-
} else if (entry.getKey().equals("loadBalanced")) {
201-
assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isLoadBalanced());
202-
} else if (entry.getKey().equals("srvMaxHosts")) {
203-
assertEquals(Integer.valueOf(entry.getValue().asInt32().getValue()), connectionString.getSrvMaxHosts());
204-
} else if (entry.getKey().equals("srvServiceName")) {
205-
assertEquals(entry.getValue().asString().getValue(), connectionString.getSrvServiceName());
206-
} else {
207-
throw new UnsupportedOperationException("No support configured yet for " + entry.getKey());
181+
switch (entry.getKey()) {
182+
case "replicaSet":
183+
assertEquals(entry.getValue().asString().getValue(), connectionString.getRequiredReplicaSetName());
184+
break;
185+
case "ssl":
186+
assertEquals(entry.getValue().asBoolean().getValue(), connectionString.getSslEnabled());
187+
break;
188+
case "authSource":
189+
// ignoring authSource for now, because without at least a userName also in the connection string,
190+
// the authSource is ignored. If the test gets this far, at least we know that a TXT record
191+
// containing in authSource doesn't blow up. We just don't test that it's actually used.
192+
assertTrue(true);
193+
break;
194+
case "directConnection":
195+
assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isDirectConnection());
196+
break;
197+
case "loadBalanced":
198+
assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isLoadBalanced());
199+
break;
200+
case "srvMaxHosts":
201+
assertEquals(Integer.valueOf(entry.getValue().asInt32().getValue()), connectionString.getSrvMaxHosts());
202+
break;
203+
case "srvServiceName":
204+
assertEquals(entry.getValue().asString().getValue(), connectionString.getSrvServiceName());
205+
break;
206+
default:
207+
throw new UnsupportedOperationException("No support configured yet for " + entry.getKey());
208208
}
209209
}
210210
}
@@ -266,12 +266,9 @@ private void hostsCheck(final List<String> actual, @Nullable final List<String>
266266
}
267267
}
268268
})
269-
.applyToSslSettings(new Block<SslSettings.Builder>() {
270-
@Override
271-
public void apply(final SslSettings.Builder builder) {
272-
builder.applySettings(sslSettings);
273-
builder.invalidHostNameAllowed(true);
274-
}
269+
.applyToSslSettings(builder -> {
270+
builder.applySettings(sslSettings);
271+
builder.invalidHostNameAllowed(true);
275272
})
276273
.build();
277274

@@ -284,7 +281,7 @@ public void apply(final SslSettings.Builder builder) {
284281

285282
@Parameterized.Parameters(name = "{0}")
286283
public static Collection<Object[]> data() throws URISyntaxException, IOException {
287-
List<Object[]> data = new ArrayList<Object[]>();
284+
List<Object[]> data = new ArrayList<>();
288285
for (File file : JsonPoweredTestHelper.getTestFiles("/initial-dns-seedlist-discovery")) {
289286
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
290287
data.add(new Object[]{
@@ -316,7 +313,7 @@ private static List<String> toStringList(@Nullable final BsonArray bsonArray) {
316313
if (bsonArray == null) {
317314
return null;
318315
}
319-
List<String> retVal = new ArrayList<String>(bsonArray.size());
316+
List<String> retVal = new ArrayList<>(bsonArray.size());
320317
for (BsonValue cur : bsonArray) {
321318
retVal.add(cur.asString().getValue());
322319
}

0 commit comments

Comments
 (0)