SGF-429 - GemfirePersistentProperty considers a BigDecimal property an entity.

(cherry picked from commit 6113bfbea146a1036352fcc93f955f52d087822a)

Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
John Blum
2015-10-01 22:35:23 -07:00
parent 3b9a43573a
commit afec8c216e
5 changed files with 174 additions and 7 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.data.util.TypeInformation;
/**
*
* @author Oliver Gierke
* @author John Blum
*/
public class GemfireMappingContext extends AbstractMappingContext<GemfirePersistentEntity<?>, GemfirePersistentProperty> {
@@ -33,7 +34,7 @@ public class GemfireMappingContext extends AbstractMappingContext<GemfirePersist
* @see org.springframework.data.mapping.context.AbstractMappingContext#createPersistentEntity(org.springframework.data.util.TypeInformation)
*/
@Override
protected <T> GemfirePersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) {
protected <T> GemfirePersistentEntity<T> createPersistentEntity(TypeInformation<T> typeInformation) {
return new GemfirePersistentEntity<T>(typeInformation);
}

View File

@@ -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<?, GemfirePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
super(field, propertyDescriptor, owner, simpleTypeHolder);
super(field, propertyDescriptor, owner, new GemfireSimpleTypeHolder(simpleTypeHolder));
}
/*

View File

@@ -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<Class<?>> CUSTOM_SIMPLE_TYPES = new HashSet<Class<?>>(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);
}
}

View File

@@ -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<GemfirePersistentEntity<?>, GemfirePersistentProperty> getMappingContext() {
return new GemfireMappingContext();
}
@Test
public void defaultsRegionNameToClassName() {
GemfirePersistentEntity<UnannotatedRegion> entity = new GemfirePersistentEntity<UnannotatedRegion>(
@@ -50,17 +61,60 @@ public class GemfirePersistentEntityUnitTests {
assertThat(entity.getRegionName(), is("Foo"));
}
static class UnannotatedRegion {
@Test
@SuppressWarnings("unchecked")
public void bigDecimalPersistentPropertyIsNotEntity() {
GemfirePersistentEntity<ExampleDomainObject> entity = (GemfirePersistentEntity<ExampleDomainObject>)
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<ExampleDomainObject> entity = (GemfirePersistentEntity<ExampleDomainObject>)
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;
}
}
}

View File

@@ -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 {
}
}