From afec8c216ef40ff8d433e8267ddda5fadebb19b8 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 1 Oct 2015 22:35:23 -0700 Subject: [PATCH] SGF-429 - GemfirePersistentProperty considers a BigDecimal property an entity. (cherry picked from commit 6113bfbea146a1036352fcc93f955f52d087822a) Signed-off-by: John Blum --- .../mapping/GemfireMappingContext.java | 3 +- .../mapping/GemfirePersistentProperty.java | 3 +- .../model/GemfireSimpleTypeHolder.java | 55 ++++++++++++++++ .../GemfirePersistentEntityUnitTests.java | 64 +++++++++++++++++-- .../model/GemfireSimpleTypeHolderTest.java | 56 ++++++++++++++++ 5 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolder.java create mode 100644 src/test/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolderTest.java diff --git a/src/main/java/org/springframework/data/gemfire/mapping/GemfireMappingContext.java b/src/main/java/org/springframework/data/gemfire/mapping/GemfireMappingContext.java index 7a951e2e..febb4cc7 100644 --- a/src/main/java/org/springframework/data/gemfire/mapping/GemfireMappingContext.java +++ b/src/main/java/org/springframework/data/gemfire/mapping/GemfireMappingContext.java @@ -25,6 +25,7 @@ import org.springframework.data.util.TypeInformation; /** * * @author Oliver Gierke + * @author John Blum */ public class GemfireMappingContext extends AbstractMappingContext, GemfirePersistentProperty> { @@ -33,7 +34,7 @@ public class GemfireMappingContext extends AbstractMappingContext GemfirePersistentEntity createPersistentEntity(TypeInformation typeInformation) { + protected GemfirePersistentEntity createPersistentEntity(TypeInformation typeInformation) { return new GemfirePersistentEntity(typeInformation); } diff --git a/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentProperty.java b/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentProperty.java index c92ebc18..bb66606d 100644 --- a/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentProperty.java +++ b/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentProperty.java @@ -18,6 +18,7 @@ package org.springframework.data.gemfire.mapping; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; +import org.springframework.data.gemfire.mapping.model.GemfireSimpleTypeHolder; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; @@ -41,7 +42,7 @@ public class GemfirePersistentProperty extends AnnotationBasedPersistentProperty */ public GemfirePersistentProperty(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { - super(field, propertyDescriptor, owner, simpleTypeHolder); + super(field, propertyDescriptor, owner, new GemfireSimpleTypeHolder(simpleTypeHolder)); } /* diff --git a/src/main/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolder.java b/src/main/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolder.java new file mode 100644 index 00000000..f046a7f4 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolder.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 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.gemfire.mapping.model; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.HashSet; +import java.util.Set; + +import org.springframework.data.mapping.model.SimpleTypeHolder; + +/** + * The GemfireSimpleTypeHolder class is a Spring Data Commons SimpleTypeHolder implementation adding additional + * simple types to the collection. + * + * @author John Blum + * @see org.springframework.data.mapping.model.SimpleTypeHolder + * @since 1.6.3 + */ +@SuppressWarnings("unused") +public class GemfireSimpleTypeHolder extends SimpleTypeHolder { + + protected static final Set> CUSTOM_SIMPLE_TYPES = new HashSet>(2); + + static { + CUSTOM_SIMPLE_TYPES.add(BigDecimal.class); + CUSTOM_SIMPLE_TYPES.add(BigInteger.class); + } + + /** + * Constructs an instance of the GemfireSimpleTypeHolder initialized with a source {@link SimpleTypeHolder}. + * + * @param source the SimpleTypeHolder used as the source for GemFire's {@link SimpleTypeHolder} implementation. + * source must not be {@literal null}. + * @throws NullPointerException if source is null. + */ + public GemfireSimpleTypeHolder(SimpleTypeHolder source) { + super(CUSTOM_SIMPLE_TYPES, source); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntityUnitTests.java b/src/test/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntityUnitTests.java index 8515c585..849360bd 100644 --- a/src/test/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntityUnitTests.java +++ b/src/test/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntityUnitTests.java @@ -15,19 +15,30 @@ */ package org.springframework.data.gemfire.mapping; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +import java.math.BigDecimal; +import java.math.BigInteger; import org.junit.Test; +import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.util.ClassTypeInformation; /** * Unit tests for {@link GemfirePersistentEntity}. * * @author Oliver Gierke + * @author John Blum */ public class GemfirePersistentEntityUnitTests { + protected MappingContext, GemfirePersistentProperty> getMappingContext() { + return new GemfireMappingContext(); + } + @Test public void defaultsRegionNameToClassName() { GemfirePersistentEntity entity = new GemfirePersistentEntity( @@ -50,17 +61,60 @@ public class GemfirePersistentEntityUnitTests { assertThat(entity.getRegionName(), is("Foo")); } - static class UnannotatedRegion { + @Test + @SuppressWarnings("unchecked") + public void bigDecimalPersistentPropertyIsNotEntity() { + GemfirePersistentEntity entity = (GemfirePersistentEntity) + getMappingContext().getPersistentEntity(ExampleDomainObject.class); + assertThat(entity.getRegionName(), is(equalTo("Example"))); + + GemfirePersistentProperty currency = entity.getPersistentProperty("currency"); + + assertThat(currency, is(notNullValue())); + assertThat(currency.isEntity(), is(false)); + } + + @Test + @SuppressWarnings("unchecked") + public void bigIntegerPersistentPropertyIsNotEntity() { + GemfirePersistentEntity entity = (GemfirePersistentEntity) + getMappingContext().getPersistentEntity(ExampleDomainObject.class); + + assertThat(entity.getRegionName(), is(equalTo("Example"))); + + GemfirePersistentProperty bigNumber = entity.getPersistentProperty("bigNumber"); + + assertThat(bigNumber, is(notNullValue())); + assertThat(bigNumber.isEntity(), is(false)); + } + + static class UnannotatedRegion { } @Region("Foo") static class AnnotatedRegion { - } @Region static class UnnamedRegion { - } + + @Region("Example") + @SuppressWarnings("unused") + static class ExampleDomainObject { + + private BigDecimal currency; + + private BigInteger bigNumber; + + public BigDecimal getCurrency() { + return currency; + } + + public BigInteger getBigNumber() { + return bigNumber; + } + } + } diff --git a/src/test/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolderTest.java b/src/test/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolderTest.java new file mode 100644 index 00000000..48f8ee0e --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/mapping/model/GemfireSimpleTypeHolderTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2010-2013 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.gemfire.mapping.model; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import org.junit.Test; +import org.springframework.data.mapping.model.SimpleTypeHolder; + +/** + * The GemfireSimpleTypeHolderTest class is a test suite of test cases testing the contract and functionality + * of the GemfireSimpleTypeHolder class. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.mapping.model.GemfireSimpleTypeHolder + * @see org.springframework.data.mapping.model.SimpleTypeHolder + * @since 1.6.3 + */ +public class GemfireSimpleTypeHolderTest { + + private final GemfireSimpleTypeHolder holder = new GemfireSimpleTypeHolder(new SimpleTypeHolder()); + + @Test + public void bigDecimalAndBigIntegerAreSimpleTypes() { + assertThat(holder.isSimpleType(BigDecimal.class), is(true)); + assertThat(holder.isSimpleType(BigInteger.class), is(true)); + } + + @Test + public void personIsNotASimpleType() { + assertThat(holder.isSimpleType(Person.class), is(false)); + } + + class Person { + } + +}