Browse Source

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: #572
pull/571/head
Christoph Strobl 8 years ago
parent
commit
1b7678a6af
  1. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractReactiveMongoConfiguration.java
  2. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  3. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
  4. 1
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java
  5. 23
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/NoOpDbRefResolver.java
  6. 28
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java
  7. 36
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/MongoClientVersion.java
  8. 55
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBeanUnitTests.java
  9. 2
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java
  10. 4
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java

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

@ -81,8 +81,7 @@ public abstract class AbstractReactiveMongoConfiguration extends MongoConfigurat
@Bean @Bean
public MappingMongoConverter mappingMongoConverter() throws Exception { public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mongoMappingContext());
mongoMappingContext());
converter.setCustomConversions(customConversions()); converter.setCustomConversions(customConversions());
return converter; return converter;

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

@ -1221,8 +1221,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
@Nullable @Nullable
private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc) { private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc) {
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking) if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
&& MongoClientVersion.isMongo3Driver()) {
if (wc == null || wc.getWObject() == null if (wc == null || wc.getWObject() == null
|| (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) { || (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) {
return WriteConcern.ACKNOWLEDGED; return WriteConcern.ACKNOWLEDGED;

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

@ -2626,8 +2626,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@Nullable @Nullable
private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc) { private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc) {
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking) if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
&& MongoClientVersion.isMongo3Driver()) {
if (wc == null || wc.getWObject() == null if (wc == null || wc.getWObject() == null
|| (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) { || (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) {
return WriteConcern.ACKNOWLEDGED; return WriteConcern.ACKNOWLEDGED;

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

@ -43,7 +43,6 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.ClientSessionException; import org.springframework.data.mongodb.ClientSessionException;
import org.springframework.data.mongodb.LazyLoadingException; import org.springframework.data.mongodb.LazyLoadingException;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.objenesis.ObjenesisStd; import org.springframework.objenesis.ObjenesisStd;

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

@ -17,13 +17,9 @@ package org.springframework.data.mongodb.core.convert;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import org.bson.Document; import org.bson.Document;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import com.mongodb.DBRef; import com.mongodb.DBRef;
@ -32,6 +28,7 @@ import com.mongodb.DBRef;
* {@link UnsupportedOperationException} when attempting to resolve database references. * {@link UnsupportedOperationException} when attempting to resolve database references.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
* @since 2.1 * @since 2.1
*/ */
public enum NoOpDbRefResolver implements DbRefResolver { public enum NoOpDbRefResolver implements DbRefResolver {
@ -40,13 +37,14 @@ public enum NoOpDbRefResolver implements DbRefResolver {
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback) * @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)
*/ */
@Override @Override
@Nullable @Nullable
public Object resolveDbRef(@Nonnull MongoPersistentProperty property, @Nonnull DBRef dbref, public Object resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback,
@Nonnull DbRefResolverCallback callback, @Nonnull DbRefProxyHandler proxyHandler) { DbRefProxyHandler proxyHandler) {
throw new UnsupportedOperationException("DBRef resolution not supported!");
return handle();
} }
/* /*
@ -54,8 +52,9 @@ public enum NoOpDbRefResolver implements DbRefResolver {
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef) * @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
*/ */
@Override @Override
@Nullable
public Document fetch(DBRef dbRef) { public Document fetch(DBRef dbRef) {
throw new UnsupportedOperationException("DBRef resolution not supported!"); return handle();
} }
/* /*
@ -64,6 +63,10 @@ public enum NoOpDbRefResolver implements DbRefResolver {
*/ */
@Override @Override
public List<Document> bulkFetch(List<DBRef> dbRefs) { public List<Document> bulkFetch(List<DBRef> dbRefs) {
throw new UnsupportedOperationException("DBRef resolution not supported!"); return handle();
}
private <T> T handle() throws UnsupportedOperationException {
throw new UnsupportedOperationException("DBRef resolution is not supported!");
} }
} }

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

@ -24,6 +24,9 @@ import java.util.regex.Pattern;
import org.bson.BsonObjectId; import org.bson.BsonObjectId;
import org.bson.types.Binary; import org.bson.types.Binary;
import org.bson.types.CodeWScope;
import org.bson.types.CodeWithScope;
import org.bson.types.Decimal128;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.mongodb.util.MongoClientVersion; import org.springframework.data.mongodb.util.MongoClientVersion;
@ -36,40 +39,29 @@ import com.mongodb.DBRef;
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
*/ */
public abstract class MongoSimpleTypes { public abstract class MongoSimpleTypes {
public static final Set<Class<?>> AUTOGENERATED_ID_TYPES; public static final Set<Class<?>> AUTOGENERATED_ID_TYPES;
static { static {
Set<Class<?>> classes = new HashSet<>(); Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(ObjectId.class); classes.add(ObjectId.class);
classes.add(String.class); classes.add(String.class);
classes.add(BigInteger.class); classes.add(BigInteger.class);
AUTOGENERATED_ID_TYPES = Collections.unmodifiableSet(classes); AUTOGENERATED_ID_TYPES = Collections.unmodifiableSet(classes);
Set<Class<?>> simpleTypes = new HashSet<>(); Set<Class<?>> simpleTypes = new HashSet<Class<?>>();
simpleTypes.add(DBRef.class); simpleTypes.add(DBRef.class);
simpleTypes.add(ObjectId.class); simpleTypes.add(ObjectId.class);
simpleTypes.add(BsonObjectId.class); simpleTypes.add(BsonObjectId.class);
simpleTypes.add(CodeWScope.class);
simpleTypes.add(CodeWithScope.class);
simpleTypes.add(org.bson.Document.class); simpleTypes.add(org.bson.Document.class);
simpleTypes.add(Pattern.class); simpleTypes.add(Pattern.class);
simpleTypes.add(Binary.class); simpleTypes.add(Binary.class);
simpleTypes.add(UUID.class); simpleTypes.add(UUID.class);
simpleTypes.add(Decimal128.class);
if (ClassUtils.isPresent("org.bson.types.CodeWScope", MongoSimpleTypes.class.getClassLoader())) {
simpleTypes.add(resolveClassName("org.bson.types.CodeWScope"));
}
if (ClassUtils.isPresent("org.bson.types.CodeWithScope", MongoSimpleTypes.class.getClassLoader())) {
simpleTypes.add(resolveClassName("org.bson.types.CodeWithScope"));
}
if (MongoClientVersion.isMongo34Driver()) {
simpleTypes.add(resolveClassName("org.bson.types.Decimal128"));
}
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes); MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
} }
@ -77,8 +69,4 @@ public abstract class MongoSimpleTypes {
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true); public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true);
private MongoSimpleTypes() {} private MongoSimpleTypes() {}
private static Class<?> resolveClassName(String className) {
return ClassUtils.resolveClassName(className, MongoSimpleTypes.class.getClassLoader());
}
} }

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

@ -26,34 +26,54 @@ import org.springframework.util.ClassUtils;
*/ */
public class MongoClientVersion { public class MongoClientVersion {
private static final boolean IS_MONGO_30 = ClassUtils.isPresent("com.mongodb.binding.SingleServerBinding", private static final boolean SYNC_CLIENT_PRESENT = ClassUtils.isPresent("com.mongodb.MongoClient",
MongoClientVersion.class.getClassLoader()); MongoClientVersion.class.getClassLoader());
private static final boolean IS_MONGO_34 = ClassUtils.isPresent("org.bson.types.Decimal128", private static final boolean ASYNC_CLIENT_PRESENT = ClassUtils.isPresent("com.mongodb.async.client.MongoClient",
MongoClientVersion.class.getClassLoader()); MongoClientVersion.class.getClassLoader());
private static final boolean IS_ASYNC_CLIENT = ClassUtils.isPresent("com.mongodb.async.client.MongoClient", private static final boolean REACTIVE_CLIENT_PRESENT = ClassUtils
MongoClientVersion.class.getClassLoader()); .isPresent("com.mongodb.reactivestreams.client.MongoClient", MongoClientVersion.class.getClassLoader());
/** /**
* @return {@literal true} if MongoDB Java driver version 3.0 or later is on classpath. * @return {@literal true} if MongoDB Java driver version 3.0 or later is on classpath.
* @deprecated since 2.1, which requires MongoDB Java driver 3.8. Returns {@literal true} by default.
*/ */
@Deprecated
public static boolean isMongo3Driver() { public static boolean isMongo3Driver() {
return IS_MONGO_30; return true;
} }
/** /**
* @return {@literal true} if MongoDB Java driver version 3.4 or later is on classpath. * @return {@literal true} if MongoDB Java driver version 3.4 or later is on classpath.
* @since 1.10 * @since 1.10
* @deprecated since 2.1, which requires MongoDB Java driver 3.8. Returns {@literal true} by default.
*/ */
@Deprecated
public static boolean isMongo34Driver() { public static boolean isMongo34Driver() {
return IS_MONGO_34; return true;
} }
/** /**
* @return {@literal true} if MongoDB Java driver is on classpath. * @return {@literal true} if the async MongoDB Java driver is on classpath.
*/ */
public static boolean isAsyncClient() { public static boolean isAsyncClient() {
return IS_ASYNC_CLIENT; return ASYNC_CLIENT_PRESENT;
}
/**
* @return {@literal true} if the sync MongoDB Java driver is on classpath.
* @since 2.1
*/
public static boolean isSyncClientPresent() {
return SYNC_CLIENT_PRESENT;
}
/**
* @return {@literal true} if the reactive MongoDB Java driver is on classpath.
* @since 2.1
*/
public static boolean isReactiveClientPresent() {
return REACTIVE_CLIENT_PRESENT;
} }
} }

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

@ -1,55 +0,0 @@
/*
* Copyright 2011-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.springframework.data.mongodb.util.MongoClientVersion.*;
import javax.net.ssl.SSLSocketFactory;
import org.junit.BeforeClass;
import org.junit.Test;
import com.mongodb.MongoClientOptions;
/**
* Unit tests for {@link MongoOptionsFactoryBean}.
*
* @author Oliver Gierke
* @author Mike Saavedra
* @author Christoph Strobl
*/
@SuppressWarnings("deprecation")
public class MongoOptionsFactoryBeanUnitTests {
@BeforeClass
public static void validateMongoDriver() {
assumeFalse(isMongo3Driver());
}
@Test // DATAMONGO-764
public void testSslConnection() throws Exception {
MongoClientOptionsFactoryBean bean = new MongoClientOptionsFactoryBean();
bean.setSsl(true);
bean.afterPropertiesSet();
MongoClientOptions options = bean.getObject();
assertNotNull(options.getSocketFactory());
assertTrue(options.getSocketFactory() instanceof SSLSocketFactory);
}
}

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

@ -3420,8 +3420,6 @@ public class MongoTemplateTests {
public void decimal128TypeShouldBeSavedAndLoadedCorrectly() public void decimal128TypeShouldBeSavedAndLoadedCorrectly()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
assumeThat(MongoClientVersion.isMongo34Driver(), is(true));
Class<?> decimal128Type = ClassUtils.resolveClassName("org.bson.types.Decimal128", null); Class<?> decimal128Type = ClassUtils.resolveClassName("org.bson.types.Decimal128", null);
WithObjectTypeProperty source = new WithObjectTypeProperty(); WithObjectTypeProperty source = new WithObjectTypeProperty();

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

@ -111,7 +111,6 @@ public class DbRefMappingMongoConverterUnitTests {
when(dbRef.getId()).thenReturn(BigInteger.ONE); when(dbRef.getId()).thenReturn(BigInteger.ONE);
when(dbRef.getCollectionName()).thenReturn("collection-1"); when(dbRef.getCollectionName()).thenReturn("collection-1");
if (MongoClientVersion.isMongo3Driver()) {
MongoDatabase dbMock = mock(MongoDatabase.class); MongoDatabase dbMock = mock(MongoDatabase.class);
MongoCollection collectionMock = mock(MongoCollection.class); MongoCollection collectionMock = mock(MongoCollection.class);
when(dbFactory.getDb()).thenReturn(dbMock); when(dbFactory.getDb()).thenReturn(dbMock);
@ -120,9 +119,6 @@ public class DbRefMappingMongoConverterUnitTests {
FindIterable fi = mock(FindIterable.class); FindIterable fi = mock(FindIterable.class);
when(fi.first()).thenReturn(mapValDocument); when(fi.first()).thenReturn(mapValDocument);
when(collectionMock.find(Mockito.any(Bson.class))).thenReturn(fi); when(collectionMock.find(Mockito.any(Bson.class))).thenReturn(fi);
} else {
when(dbRefResolver.fetch(dbRef)).thenReturn(mapValDocument);
}
MapDBRef mapDBRef = new MapDBRef(); MapDBRef mapDBRef = new MapDBRef();

Loading…
Cancel
Save