DATAMONGO-2378 - Polishing.

Consistently use MongoTestUtils for client creations. Reuse MongoClient instead of opening always new client instances.

Original pull request: #793.
This commit is contained in:
Mark Paluch
2019-09-26 16:23:30 +02:00
parent 565132c619
commit 48034a348d
28 changed files with 85 additions and 76 deletions

View File

@@ -38,7 +38,7 @@ public class MongoAdmin implements MongoAdminOperations {
/**
* @param mongoClient
* @deprecated since 2.2 in favor of {@link MongoAdmin(com.mongodb.client.MongoClient)}.
* @deprecated since 2.2 in favor of {@link MongoAdmin#MongoAdmin(com.mongodb.client.MongoClient)}.
*/
@Deprecated
public MongoAdmin(MongoClient mongoClient) {

View File

@@ -85,13 +85,12 @@ public class ReactiveTransactionIntegrationTests {
personService = context.getBean(PersonService.class);
operations = context.getBean(ReactiveMongoOperations.class);
try (MongoClient client = MongoClients.create()) {
MongoClient client = MongoTestUtils.reactiveClient();
Flux.merge( //
MongoTestUtils.createOrReplaceCollection(DATABASE, operations.getCollectionName(Person.class), client),
MongoTestUtils.createOrReplaceCollection(DATABASE, operations.getCollectionName(EventLog.class), client) //
).then().as(StepVerifier::create).verifyComplete();
}
Flux.merge( //
MongoTestUtils.createOrReplaceCollection(DATABASE, operations.getCollectionName(Person.class), client),
MongoTestUtils.createOrReplaceCollection(DATABASE, operations.getCollectionName(EventLog.class), client) //
).then().as(StepVerifier::create).verifyComplete();
}
@Test // DATAMONGO-2265
@@ -224,7 +223,7 @@ public class ReactiveTransactionIntegrationTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create("mongodb://localhost");
return MongoTestUtils.reactiveClient();
}
@Override

View File

@@ -27,6 +27,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -52,7 +53,7 @@ public abstract class AbstractIntegrationTests {
@Override
public MongoClient mongoClient() {
return MongoClients.create();
return MongoTestUtils.client();
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -56,7 +57,7 @@ public class AbstractReactiveMongoConfigurationIntegrationTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override

View File

@@ -38,6 +38,7 @@ import org.springframework.data.mongodb.core.convert.MongoTypeMapper;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.spel.EvaluationContextProvider;
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
import org.springframework.test.util.ReflectionTestUtils;
@@ -155,7 +156,7 @@ public class AbstractReactiveMongoConfigurationUnitTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Bean
@@ -183,7 +184,7 @@ public class AbstractReactiveMongoConfigurationUnitTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override

View File

@@ -38,6 +38,7 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -70,7 +71,7 @@ public class ReactiveAuditingTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Bean

View File

@@ -69,11 +69,6 @@ public class ClientSessionTests {
template.getDb().getCollection(COLLECTION_NAME).insertOne(new Document("_id", "id-1").append("value", "spring"));
}
@After
public void tearDown() {
client.close();
}
@Test // DATAMONGO-1880
public void shouldApplyClientSession() {

View File

@@ -39,6 +39,7 @@ import org.springframework.data.mongodb.core.index.IndexInfo;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.data.mongodb.core.query.Collation.CaseFirst;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.util.Version;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -60,7 +61,7 @@ public class DefaultReactiveIndexOperationsTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override

View File

@@ -33,6 +33,7 @@ import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.mongodb.test.util.MongoVersionRule;
import org.springframework.data.util.Version;
@@ -50,13 +51,13 @@ public class JsonSchemaQueryTests {
public static @ClassRule MongoVersionRule REQUIRES_AT_LEAST_3_6_0 = MongoVersionRule.atLeast(Version.parse("3.6.0"));
static MongoClient client = MongoClients.create();
static MongoClient client = MongoTestUtils.client();
MongoTemplate template;
Person jellyBelly, roseSpringHeart, kazmardBoombub;
@BeforeClass
public static void beforeClass() {
client = MongoClients.create();
client = MongoTestUtils.client();
}
@Before
@@ -93,13 +94,6 @@ public class JsonSchemaQueryTests {
template.save(kazmardBoombub);
}
@AfterClass
public static void afterClass() {
if (client != null) {
client.close();
}
}
@Test // DATAMONGO-1835
public void findsDocumentsWithRequiredFieldsCorrectly() {
@@ -114,12 +108,10 @@ public class JsonSchemaQueryTests {
MongoJsonSchema schema = MongoJsonSchema.builder().required("address").build();
com.mongodb.reactivestreams.client.MongoClient mongoClient = com.mongodb.reactivestreams.client.MongoClients.create();
com.mongodb.reactivestreams.client.MongoClient mongoClient = MongoTestUtils.reactiveClient();
new ReactiveMongoTemplate(mongoClient, DATABASE_NAME).find(query(matchingDocumentStructure(schema)), Person.class)
.as(StepVerifier::create).expectNextCount(2).verifyComplete();
mongoClient.close();
}
@Test // DATAMONGO-1835

View File

@@ -76,7 +76,7 @@ public class ReactiveFindOperationSupportTests {
insertObjects();
template = new ReactiveMongoTemplate(MongoClients.create(), "ExecutableFindOperationSupportTests");
template = new ReactiveMongoTemplate(MongoTestUtils.reactiveClient(), "ExecutableFindOperationSupportTests");
}
void insertObjects() {

View File

@@ -28,17 +28,18 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.mongodb.test.util.MongoVersionRule;
import org.springframework.data.util.Version;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
/**
* @author Mark Paluch
@@ -56,7 +57,7 @@ public class ReactiveMongoTemplateCollationTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override
@@ -98,10 +99,9 @@ public class ReactiveMongoTemplateCollationTests {
return template.execute(db -> {
return Flux
.from(db.runCommand(new Document() //
.append("listCollections", 1) //
.append("filter", new Document("name", collectionName)))) //
return Flux.from(db.runCommand(new Document() //
.append("listCollections", 1) //
.append("filter", new Document("name", collectionName)))) //
.map(it -> it.get("cursor", Document.class))
.flatMapIterable(it -> (List<Document>) it.get("firstBatch", List.class));
}).next();

View File

@@ -62,7 +62,7 @@ public class ReactiveRemoveOperationSupportTests {
blocking.save(han);
blocking.save(luke);
template = new ReactiveMongoTemplate(MongoClients.create(), "ExecutableRemoveOperationSupportTests");
template = new ReactiveMongoTemplate(MongoTestUtils.reactiveClient(), "ExecutableRemoveOperationSupportTests");
}
@Test // DATAMONGO-1719

View File

@@ -65,7 +65,7 @@ public class ReactiveUpdateOperationSupportTests {
blocking.save(han);
blocking.save(luke);
template = new ReactiveMongoTemplate(MongoClients.create(), "ExecutableUpdateOperationSupportTests");
template = new ReactiveMongoTemplate(MongoTestUtils.reactiveClient(), "ExecutableUpdateOperationSupportTests");
}
@Test // DATAMONGO-1719

View File

@@ -149,7 +149,6 @@ public class SessionBoundMongoTemplateTests {
public void tearDown() {
session.close();
client.close();
}
@Test // DATAMONGO-1880

View File

@@ -60,7 +60,7 @@ public class ReactiveMongoTemplateAuditingTests {
@Bean
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override

View File

@@ -35,6 +35,7 @@ import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
@@ -55,7 +56,7 @@ public class MappingMongoConverterTests {
@Before
public void setUp() {
client = MongoClients.create();
client = MongoTestUtils.client();
client.getDatabase("mapping-converter-tests").drop();
MongoDbFactory factory = new SimpleMongoClientDbFactory(client, "mapping-converter-tests");

View File

@@ -46,6 +46,7 @@ import org.springframework.data.mongodb.repository.QPerson;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoClient;
@@ -75,12 +76,7 @@ public class ApplicationContextEventTests {
@BeforeClass
public static void beforeClass() {
mongo = MongoClients.create();
}
@AfterClass
public static void afterClass() {
mongo.close();
mongo = MongoTestUtils.client();
}
@Before

View File

@@ -32,6 +32,7 @@ import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.util.Base64Utils;
import com.mongodb.client.MongoClient;
@@ -59,7 +60,7 @@ public class CriteriaTests {
@Before
public void setUp() {
client = MongoClients.create();
client = MongoTestUtils.client();
ops = new MongoTemplate(client, "criteria-tests");
ops.dropCollection(DocumentWithBitmask.class);

View File

@@ -36,6 +36,7 @@ import org.springframework.data.mongodb.core.CollectionOptions;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.mongodb.test.util.MongoVersionRule;
import org.springframework.data.util.Version;
import org.springframework.test.context.ContextConfiguration;
@@ -60,7 +61,7 @@ public class ReactiveMongoJsonSchemaTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override

View File

@@ -67,7 +67,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
/**
* Test for {@link ReactiveMongoRepository} query methods.
@@ -94,7 +93,7 @@ public class ReactiveMongoRepositoryTests {
@Bean
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override
@@ -133,11 +132,10 @@ public class ReactiveMongoRepositoryTests {
@BeforeClass
public static void cleanDb() {
try (MongoClient client = MongoClients.create()) {
MongoClient client = MongoTestUtils.reactiveClient();
MongoTestUtils.createOrReplaceCollectionNow("reactive", "person", client);
MongoTestUtils.createOrReplaceCollectionNow("reactive", "capped", client);
}
MongoTestUtils.createOrReplaceCollectionNow("reactive", "person", client);
MongoTestUtils.createOrReplaceCollectionNow("reactive", "capped", client);
}
@Before

View File

@@ -25,6 +25,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.repository.ReactivePersonRepository;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -45,7 +46,8 @@ public class ReactiveMongoRepositoriesRegistrarIntegrationTests {
@Bean
public ReactiveMongoTemplate reactiveMongoTemplate() throws Exception {
return new ReactiveMongoTemplate(new SimpleReactiveMongoDatabaseFactory(MongoClients.create(), "database"));
return new ReactiveMongoTemplate(
new SimpleReactiveMongoDatabaseFactory(MongoTestUtils.reactiveClient(), "database"));
}
}

View File

@@ -78,7 +78,7 @@ public class ReactiveQuerydslMongoPredicateExecutorTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override
@@ -90,11 +90,10 @@ public class ReactiveQuerydslMongoPredicateExecutorTests {
@BeforeClass
public static void cleanDb() {
try (MongoClient client = MongoClients.create()) {
MongoClient client = MongoTestUtils.reactiveClient();
MongoTestUtils.createOrReplaceCollectionNow("reactive", "person", client);
MongoTestUtils.createOrReplaceCollectionNow("reactive", "user", client);
}
MongoTestUtils.createOrReplaceCollectionNow("reactive", "person", client);
MongoTestUtils.createOrReplaceCollectionNow("reactive", "user", client);
}
@Before

View File

@@ -33,6 +33,7 @@ import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.VersionedPerson;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.mongodb.test.util.MongoVersion;
import org.springframework.data.mongodb.test.util.ReplicaSet;
import org.springframework.data.util.ClassTypeInformation;
@@ -56,7 +57,7 @@ public class SimpleMongoRepositoryVersionedEntityTests {
@Override
public MongoClient mongoClient() {
return MongoClients.create();
return MongoTestUtils.client();
}
@Override

View File

@@ -31,6 +31,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.VersionedPerson;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -49,7 +50,7 @@ public class SimpleReactiveMongoRepositoryVersionedEntityTests {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
return MongoTestUtils.reactiveClient();
}
@Override

View File

@@ -361,14 +361,13 @@ public class CleanMongoDB implements TestRule {
boolean isInternal = false;
if (client == null) {
client = MongoClients.create();
client = MongoTestUtils.client();
isInternal = true;
}
doClean();
if (isInternal) {
client.close();
client = null;
}
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.mongodb.test.util;
import com.mongodb.client.MongoClient;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -25,20 +24,29 @@ import org.bson.Document;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.Success;
/**
* Utility to create (and reuse) imperative and reactive {@code MongoClient} instances.
*
* @author Christoph Strobl
* @author Mark Paluch
*/
public class MongoTestUtils {
private static final String CONNECTION_STRING_PATTERN = "mongodb://%s:%s/";
public static final String CONNECTION_STRING = "mongodb://localhost:27017/?replicaSet=rs0"; // &readPreference=primary&w=majority
private static final String CONNECTION_STRING_PATTERN = "mongodb://%s:%s/";
/**
* Create a new {@link com.mongodb.client.MongoClient} with defaults.
*
* @return new instance of {@link com.mongodb.client.MongoClient}.
*/
public static MongoClient client() {
return client("localhost", 27017);
}
@@ -46,6 +54,20 @@ public class MongoTestUtils {
public static MongoClient client(String host, int port) {
return com.mongodb.client.MongoClients.create(String.format(CONNECTION_STRING_PATTERN, host, port));
}
/**
* Create a new {@link com.mongodb.reactivestreams.client.MongoClient} with defaults.
*
* @return new instance of {@link com.mongodb.reactivestreams.client.MongoClient}.
*/
public static com.mongodb.reactivestreams.client.MongoClient reactiveClient() {
return reactiveClient("localhost", 27017);
}
public static com.mongodb.reactivestreams.client.MongoClient reactiveClient(String host, int port) {
return MongoClients.create(String.format(CONNECTION_STRING_PATTERN, host, port));
}
/**
* Create a {@link com.mongodb.client.MongoCollection} if it does not exist, or drop and recreate it if it does.
*

View File

@@ -178,7 +178,6 @@ public class MongoVersionRule implements TestRule {
MongoClient client = MongoTestUtils.client(host, port);
MongoDatabase database = client.getDatabase("test");
Document result = database.runCommand(new Document("buildInfo", 1));
client.close();
return Version.parse(result.get("version", String.class));
} catch (Exception e) {

View File

@@ -94,12 +94,11 @@ public class ReplicaSet implements TestRule {
if (runsAsReplicaSet.get() == null) {
try (MongoClient client = MongoTestUtils.client()) {
MongoClient client = MongoTestUtils.client();
boolean tmp = client.getDatabase("admin").runCommand(new Document("getCmdLineOpts", "1"))
.get("argv", List.class).contains("--replSet");
runsAsReplicaSet.compareAndSet(null, tmp);
}
boolean tmp = client.getDatabase("admin").runCommand(new Document("getCmdLineOpts", "1")).get("argv", List.class)
.contains("--replSet");
runsAsReplicaSet.compareAndSet(null, tmp);
}
return runsAsReplicaSet.get();
}