DATAMONGO-1517 - Polishing.

Remove ReflectiveSimpleTypes in favor of MongoSimpleTypes.
Add add integration test.
This commit is contained in:
Christoph Strobl
2017-01-12 13:04:50 +01:00
committed by Oliver Gierke
parent 8e3d7f96c4
commit 7413a031c1
5 changed files with 65 additions and 63 deletions

View File

@@ -93,7 +93,7 @@ public class CustomConversions {
this.readingPairs = new LinkedHashSet<ConvertiblePair>();
this.writingPairs = new LinkedHashSet<ConvertiblePair>();
this.customSimpleTypes = new HashSet<Class<?>>(ReflectiveSimpleTypes.getSupportedSimpleTypes());
this.customSimpleTypes = new HashSet<Class<?>>();
this.customReadTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
this.customWriteTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
this.rawWriteTargetTypes = new ConcurrentHashMap<Class<?>, CacheValue<Class<?>>>();

View File

@@ -1,53 +0,0 @@
/*
* Copyright 2017 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.convert;
import java.util.Collection;
import java.util.Collections;
import org.springframework.util.ClassUtils;
/**
* {@link ReflectiveSimpleTypes} provides reflective access to MongoDB types that are not consistently available for
* various driver versions.
*
* @author Mark Paluch
* @since 1.10
*/
class ReflectiveSimpleTypes {
private static final boolean HAS_DECIMAL_128 = ClassUtils.isPresent("org.bson.types.Decimal128",
ReflectiveSimpleTypes.class.getClassLoader());
/**
* Returns a {@link Collection} of simple MongoDB types (i.e. natively supported by the MongoDB driver) that are not
* consistently available for various driver versions.
*
* @return a {@link Collection} of simple MongoDB types.
*/
public static Collection<Class<?>> getSupportedSimpleTypes() {
if (HAS_DECIMAL_128) {
return Collections.<Class<?>> singleton(getDecimal128Class());
}
return Collections.emptySet();
}
private static Class<?> getDecimal128Class() {
return ClassUtils.resolveClassName("org.bson.types.Decimal128", ReflectiveSimpleTypes.class.getClassLoader());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 by the original author(s).
* Copyright (c) 2011-2017 by the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,8 @@ import org.bson.types.Binary;
import org.bson.types.CodeWScope;
import org.bson.types.ObjectId;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.mongodb.util.MongoClientVersion;
import org.springframework.util.ClassUtils;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
@@ -34,6 +36,7 @@ import com.mongodb.DBRef;
* Simple constant holder for a {@link SimpleTypeHolder} enriched with Mongo specific simple types.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public abstract class MongoSimpleTypes {
@@ -54,12 +57,17 @@ public abstract class MongoSimpleTypes {
simpleTypes.add(Pattern.class);
simpleTypes.add(Binary.class);
simpleTypes.add(UUID.class);
if (MongoClientVersion.isMongo34Driver()) {
simpleTypes
.add(ClassUtils.resolveClassName("org.bson.types.Decimal128", MongoSimpleTypes.class.getClassLoader()));
}
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
}
private static final Set<Class<?>> MONGO_SIMPLE_TYPES;
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true);
private MongoSimpleTypes() {
}
private MongoSimpleTypes() {}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2017 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.
@@ -28,16 +28,28 @@ public class MongoClientVersion {
private static final boolean IS_MONGO_30 = ClassUtils.isPresent("com.mongodb.binding.SingleServerBinding",
MongoClientVersion.class.getClassLoader());
private static final boolean IS_MONGO_34 = ClassUtils.isPresent("org.bson.types.Decimal128",
MongoClientVersion.class.getClassLoader());
private static final boolean IS_ASYNC_CLIENT = ClassUtils.isPresent("com.mongodb.async.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.
*/
public static boolean isMongo3Driver() {
return IS_MONGO_30;
}
/**
* @return {@literal true} if MongoDB Java driver version 3.4 or later is on classpath.
* @since 1.10
*/
public static boolean isMongo34Driver() {
return IS_MONGO_34;
}
/**
* @return {lliteral true} if MongoDB Java driver is on classpath.
*/

View File

@@ -24,6 +24,11 @@ import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.core.query.Update.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
@@ -82,10 +87,12 @@ import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.util.MongoClientVersion;
import org.springframework.data.util.CloseableIterator;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -101,10 +108,6 @@ import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* Integration test for {@link MongoTemplate}.
*
@@ -125,6 +128,9 @@ public class MongoTemplateTests {
.parse("2.4");
private static final org.springframework.data.util.Version TWO_DOT_EIGHT = org.springframework.data.util.Version
.parse("2.8");
private static final org.springframework.data.util.Version THREE_DOT_FOUR= org.springframework.data.util.Version
.parse("3.4");
@Autowired MongoTemplate template;
@Autowired MongoDbFactory factory;
@@ -3141,6 +3147,28 @@ public class MongoTemplateTests {
assertThat(document.id, is(notNullValue()));
}
/**
* @see DATAMONGO-1517
*/
@Test
public void decimal128TypeShouldBeSavedAndLoadedCorrectly()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR), is(true));
assumeThat(MongoClientVersion.isMongo34Driver(), is(true));
Class<?> decimal128Type = ClassUtils.resolveClassName("org.bson.types.Decimal128", null);
WithObjectTypeProperty source = new WithObjectTypeProperty();
source.id = "decimal128-property-value";
source.value = decimal128Type.getConstructor(BigDecimal.class).newInstance(new BigDecimal(100));
template.save(source);
WithObjectTypeProperty loaded = template.findOne(query(where("id").is(source.id)), WithObjectTypeProperty.class);
assertThat(loaded.getValue(), instanceOf(decimal128Type));
}
static class TypeWithNumbers {
@Id String id;
@@ -3510,4 +3538,11 @@ public class MongoTemplateTests {
String description;
GeoJsonPoint point;
}
@Data
static class WithObjectTypeProperty {
@Id String id;
Object value;
}
}