SGF-582 - Add support for using bean property "id" as a Region key if @Id annotation is not present.

(cherry picked from commit 3bc7d2e7115aac93727b04004f809f5f345a1e2d)
Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
Gregory Green
2017-01-05 08:50:17 -05:00
committed by John Blum
parent 5b50fd246f
commit 6b7df70355
2 changed files with 114 additions and 21 deletions

View File

@@ -23,11 +23,15 @@ import static org.junit.Assert.assertThat;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.util.ClassTypeInformation;
import lombok.Data;
/**
* Unit tests for {@link GemfirePersistentEntity}.
*
@@ -40,6 +44,36 @@ public class GemfirePersistentEntityUnitTests {
return new GemfireMappingContext();
}
/**
* JIRA ticket SGF-582
*
* Used an object's getId method if the @Id does not exists
*/
@Test
public void supportsGetId() {
GemfirePersistentEntity<UnannotatedRegion> unnamedRegionEntity = new GemfirePersistentEntity<UnannotatedRegion>(
ClassTypeInformation.from(UnannotatedRegion.class));
IdentifierAccessor accessor = unnamedRegionEntity.getIdentifierAccessor(new UnannotatedRegion());
Assert.assertNull(accessor.getIdentifier());
GemfirePersistentEntity<NotAnnotationIdRegion> entity = new GemfirePersistentEntity<NotAnnotationIdRegion>(
ClassTypeInformation.from(NotAnnotationIdRegion.class));
NotAnnotationIdRegion region = new NotAnnotationIdRegion();
region.setId("id");
region.setValue("value");
accessor = entity.getIdentifierAccessor(region);
Assert.assertNotNull(accessor.getIdentifier());
Assert.assertEquals("id", accessor.getIdentifier());
}
@Test
public void defaultsRegionNameToClassName() {
GemfirePersistentEntity<UnannotatedRegion> entity = new GemfirePersistentEntity<UnannotatedRegion>(
@@ -65,8 +99,8 @@ public class GemfirePersistentEntityUnitTests {
@Test
@SuppressWarnings("unchecked")
public void bigDecimalPersistentPropertyIsNotEntity() {
GemfirePersistentEntity<ExampleDomainObject> entity = (GemfirePersistentEntity<ExampleDomainObject>)
getMappingContext().getPersistentEntity(ExampleDomainObject.class);
GemfirePersistentEntity<ExampleDomainObject> entity = (GemfirePersistentEntity<ExampleDomainObject>) getMappingContext()
.getPersistentEntity(ExampleDomainObject.class);
assertThat(entity.getRegionName(), is(equalTo("Example")));
@@ -79,8 +113,8 @@ public class GemfirePersistentEntityUnitTests {
@Test
@SuppressWarnings("unchecked")
public void bigIntegerPersistentPropertyIsNotEntity() {
GemfirePersistentEntity<ExampleDomainObject> entity = (GemfirePersistentEntity<ExampleDomainObject>)
getMappingContext().getPersistentEntity(ExampleDomainObject.class);
GemfirePersistentEntity<ExampleDomainObject> entity = (GemfirePersistentEntity<ExampleDomainObject>) getMappingContext()
.getPersistentEntity(ExampleDomainObject.class);
assertThat(entity.getRegionName(), is(equalTo("Example")));
@@ -90,15 +124,18 @@ public class GemfirePersistentEntityUnitTests {
assertThat(bigNumber.isEntity(), is(false));
}
static class UnannotatedRegion {
}
static class UnannotatedRegion {}
@Region("Foo")
static class AnnotatedRegion {
}
static class AnnotatedRegion {}
@Region
static class UnnamedRegion {
static class UnnamedRegion {}
static @Data class NotAnnotationIdRegion {
private String value;
private String id;
}
@Region("Example")