Skip to content

Commit 1b7678a

Browse files
DATAMONGO-1919 - Polishing.
Remove unused imports, deprecated MongoClientVersion methods for drivers no longer supported and remove their usage throughout the codebase and partially revert changes in MongoSimpleTypes because all org.bson types have been included in the 3.8 release of org.mongodb.bson. Original Pull Request: spring-projects#572
1 parent 0d06e14 commit 1b7678a

File tree

10 files changed

+60
-114
lines changed

10 files changed

+60
-114
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractReactiveMongoConfiguration.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public ReactiveMongoDatabaseFactory reactiveMongoDbFactory() {
8181
@Bean
8282
public MappingMongoConverter mappingMongoConverter() throws Exception {
8383

84-
MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE,
85-
mongoMappingContext());
84+
MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mongoMappingContext());
8685
converter.setCustomConversions(customConversions());
8786

8887
return converter;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,7 @@ protected WriteConcern prepareWriteConcern(MongoAction mongoAction) {
12211221
@Nullable
12221222
private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc) {
12231223

1224-
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)
1225-
&& MongoClientVersion.isMongo3Driver()) {
1224+
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
12261225
if (wc == null || wc.getWObject() == null
12271226
|| (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) {
12281227
return WriteConcern.ACKNOWLEDGED;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -2626,8 +2626,7 @@ protected WriteConcern prepareWriteConcern(MongoAction mongoAction) {
26262626
@Nullable
26272627
private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc) {
26282628

2629-
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)
2630-
&& MongoClientVersion.isMongo3Driver()) {
2629+
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
26312630
if (wc == null || wc.getWObject() == null
26322631
|| (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) {
26332632
return WriteConcern.ACKNOWLEDGED;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.springframework.data.mongodb.ClientSessionException;
4444
import org.springframework.data.mongodb.LazyLoadingException;
4545
import org.springframework.data.mongodb.MongoDbFactory;
46-
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
4746
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
4847
import org.springframework.lang.Nullable;
4948
import org.springframework.objenesis.ObjenesisStd;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/NoOpDbRefResolver.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717

1818
import java.util.List;
1919

20-
import javax.annotation.Nonnull;
21-
2220
import org.bson.Document;
23-
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2421
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
2522
import org.springframework.lang.Nullable;
26-
import org.springframework.util.StringUtils;
2723

2824
import com.mongodb.DBRef;
2925

@@ -32,6 +28,7 @@
3228
* {@link UnsupportedOperationException} when attempting to resolve database references.
3329
*
3430
* @author Mark Paluch
31+
* @author Christoph Strobl
3532
* @since 2.1
3633
*/
3734
public enum NoOpDbRefResolver implements DbRefResolver {
@@ -40,22 +37,24 @@ public enum NoOpDbRefResolver implements DbRefResolver {
4037

4138
/*
4239
* (non-Javadoc)
43-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback)
40+
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback, org.springframework.data.mongodb.core.convert.DbRefProxyHandler)
4441
*/
4542
@Override
4643
@Nullable
47-
public Object resolveDbRef(@Nonnull MongoPersistentProperty property, @Nonnull DBRef dbref,
48-
@Nonnull DbRefResolverCallback callback, @Nonnull DbRefProxyHandler proxyHandler) {
49-
throw new UnsupportedOperationException("DBRef resolution not supported!");
44+
public Object resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback,
45+
DbRefProxyHandler proxyHandler) {
46+
47+
return handle();
5048
}
5149

5250
/*
5351
* (non-Javadoc)
5452
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
5553
*/
5654
@Override
55+
@Nullable
5756
public Document fetch(DBRef dbRef) {
58-
throw new UnsupportedOperationException("DBRef resolution not supported!");
57+
return handle();
5958
}
6059

6160
/*
@@ -64,6 +63,10 @@ public Document fetch(DBRef dbRef) {
6463
*/
6564
@Override
6665
public List<Document> bulkFetch(List<DBRef> dbRefs) {
67-
throw new UnsupportedOperationException("DBRef resolution not supported!");
66+
return handle();
67+
}
68+
69+
private <T> T handle() throws UnsupportedOperationException {
70+
throw new UnsupportedOperationException("DBRef resolution is not supported!");
6871
}
6972
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java

+8-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
import org.bson.BsonObjectId;
2626
import org.bson.types.Binary;
27+
import org.bson.types.CodeWScope;
28+
import org.bson.types.CodeWithScope;
29+
import org.bson.types.Decimal128;
2730
import org.bson.types.ObjectId;
2831
import org.springframework.data.mapping.model.SimpleTypeHolder;
2932
import org.springframework.data.mongodb.util.MongoClientVersion;
@@ -36,49 +39,34 @@
3639
*
3740
* @author Oliver Gierke
3841
* @author Christoph Strobl
39-
* @author Mark Paluch
4042
*/
4143
public abstract class MongoSimpleTypes {
4244

4345
public static final Set<Class<?>> AUTOGENERATED_ID_TYPES;
4446

4547
static {
46-
Set<Class<?>> classes = new HashSet<>();
48+
Set<Class<?>> classes = new HashSet<Class<?>>();
4749
classes.add(ObjectId.class);
4850
classes.add(String.class);
4951
classes.add(BigInteger.class);
5052
AUTOGENERATED_ID_TYPES = Collections.unmodifiableSet(classes);
5153

52-
Set<Class<?>> simpleTypes = new HashSet<>();
54+
Set<Class<?>> simpleTypes = new HashSet<Class<?>>();
5355
simpleTypes.add(DBRef.class);
5456
simpleTypes.add(ObjectId.class);
5557
simpleTypes.add(BsonObjectId.class);
58+
simpleTypes.add(CodeWScope.class);
59+
simpleTypes.add(CodeWithScope.class);
5660
simpleTypes.add(org.bson.Document.class);
5761
simpleTypes.add(Pattern.class);
5862
simpleTypes.add(Binary.class);
5963
simpleTypes.add(UUID.class);
60-
61-
if (ClassUtils.isPresent("org.bson.types.CodeWScope", MongoSimpleTypes.class.getClassLoader())) {
62-
simpleTypes.add(resolveClassName("org.bson.types.CodeWScope"));
63-
}
64-
65-
if (ClassUtils.isPresent("org.bson.types.CodeWithScope", MongoSimpleTypes.class.getClassLoader())) {
66-
simpleTypes.add(resolveClassName("org.bson.types.CodeWithScope"));
67-
}
68-
69-
if (MongoClientVersion.isMongo34Driver()) {
70-
simpleTypes.add(resolveClassName("org.bson.types.Decimal128"));
71-
}
72-
64+
simpleTypes.add(Decimal128.class);
7365
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
7466
}
7567

7668
private static final Set<Class<?>> MONGO_SIMPLE_TYPES;
7769
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true);
7870

7971
private MongoSimpleTypes() {}
80-
81-
private static Class<?> resolveClassName(String className) {
82-
return ClassUtils.resolveClassName(className, MongoSimpleTypes.class.getClassLoader());
83-
}
8472
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/MongoClientVersion.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,54 @@
2626
*/
2727
public class MongoClientVersion {
2828

29-
private static final boolean IS_MONGO_30 = ClassUtils.isPresent("com.mongodb.binding.SingleServerBinding",
29+
private static final boolean SYNC_CLIENT_PRESENT = ClassUtils.isPresent("com.mongodb.MongoClient",
3030
MongoClientVersion.class.getClassLoader());
3131

32-
private static final boolean IS_MONGO_34 = ClassUtils.isPresent("org.bson.types.Decimal128",
32+
private static final boolean ASYNC_CLIENT_PRESENT = ClassUtils.isPresent("com.mongodb.async.client.MongoClient",
3333
MongoClientVersion.class.getClassLoader());
3434

35-
private static final boolean IS_ASYNC_CLIENT = ClassUtils.isPresent("com.mongodb.async.client.MongoClient",
36-
MongoClientVersion.class.getClassLoader());
35+
private static final boolean REACTIVE_CLIENT_PRESENT = ClassUtils
36+
.isPresent("com.mongodb.reactivestreams.client.MongoClient", MongoClientVersion.class.getClassLoader());
3737

3838
/**
3939
* @return {@literal true} if MongoDB Java driver version 3.0 or later is on classpath.
40+
* @deprecated since 2.1, which requires MongoDB Java driver 3.8. Returns {@literal true} by default.
4041
*/
42+
@Deprecated
4143
public static boolean isMongo3Driver() {
42-
return IS_MONGO_30;
44+
return true;
4345
}
4446

4547
/**
4648
* @return {@literal true} if MongoDB Java driver version 3.4 or later is on classpath.
4749
* @since 1.10
50+
* @deprecated since 2.1, which requires MongoDB Java driver 3.8. Returns {@literal true} by default.
4851
*/
52+
@Deprecated
4953
public static boolean isMongo34Driver() {
50-
return IS_MONGO_34;
54+
return true;
5155
}
5256

5357
/**
54-
* @return {@literal true} if MongoDB Java driver is on classpath.
58+
* @return {@literal true} if the async MongoDB Java driver is on classpath.
5559
*/
5660
public static boolean isAsyncClient() {
57-
return IS_ASYNC_CLIENT;
61+
return ASYNC_CLIENT_PRESENT;
62+
}
63+
64+
/**
65+
* @return {@literal true} if the sync MongoDB Java driver is on classpath.
66+
* @since 2.1
67+
*/
68+
public static boolean isSyncClientPresent() {
69+
return SYNC_CLIENT_PRESENT;
70+
}
71+
72+
/**
73+
* @return {@literal true} if the reactive MongoDB Java driver is on classpath.
74+
* @since 2.1
75+
*/
76+
public static boolean isReactiveClientPresent() {
77+
return REACTIVE_CLIENT_PRESENT;
5878
}
5979
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBeanUnitTests.java

-55
This file was deleted.

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -3420,8 +3420,6 @@ public void findsByGenericNestedListElements() {
34203420
public void decimal128TypeShouldBeSavedAndLoadedCorrectly()
34213421
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
34223422

3423-
assumeThat(MongoClientVersion.isMongo34Driver(), is(true));
3424-
34253423
Class<?> decimal128Type = ClassUtils.resolveClassName("org.bson.types.Decimal128", null);
34263424

34273425
WithObjectTypeProperty source = new WithObjectTypeProperty();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,14 @@ public void convertDocumentWithMapDBRef() {
111111
when(dbRef.getId()).thenReturn(BigInteger.ONE);
112112
when(dbRef.getCollectionName()).thenReturn("collection-1");
113113

114-
if (MongoClientVersion.isMongo3Driver()) {
115-
MongoDatabase dbMock = mock(MongoDatabase.class);
116-
MongoCollection collectionMock = mock(MongoCollection.class);
117-
when(dbFactory.getDb()).thenReturn(dbMock);
118-
when(dbMock.getCollection(anyString(), eq(Document.class))).thenReturn(collectionMock);
119-
120-
FindIterable fi = mock(FindIterable.class);
121-
when(fi.first()).thenReturn(mapValDocument);
122-
when(collectionMock.find(Mockito.any(Bson.class))).thenReturn(fi);
123-
} else {
124-
when(dbRefResolver.fetch(dbRef)).thenReturn(mapValDocument);
125-
}
114+
MongoDatabase dbMock = mock(MongoDatabase.class);
115+
MongoCollection collectionMock = mock(MongoCollection.class);
116+
when(dbFactory.getDb()).thenReturn(dbMock);
117+
when(dbMock.getCollection(anyString(), eq(Document.class))).thenReturn(collectionMock);
118+
119+
FindIterable fi = mock(FindIterable.class);
120+
when(fi.first()).thenReturn(mapValDocument);
121+
when(collectionMock.find(Mockito.any(Bson.class))).thenReturn(fi);
126122

127123
MapDBRef mapDBRef = new MapDBRef();
128124

0 commit comments

Comments
 (0)