DATAGEODE-116 - Add ability to declare include filters in addition to exclude filters on o.s.d.g.mapping.MappingPdxSerializer.
Additionally, o.s.d.g.mapping.MappingPdxSerializer will filter Null types, Apache Geode types, GemStone GemFire types, Java types and Spring Framework types by default.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 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 com.gemstone.gemfire;
|
||||
|
||||
import org.springframework.data.gemfire.mapping.MappingPdxSerializerUnitTests;
|
||||
|
||||
/**
|
||||
* The {@link TestGemStoneGemFireType} class is a test {@link Class type} used by
|
||||
* the {@link MappingPdxSerializerUnitTests}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.data.gemfire.mapping.MappingPdxSerializerUnitTests
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class TestGemStoneGemFireType {
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -67,6 +67,8 @@ import lombok.Setter;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author John Blum
|
||||
* @see org.junit.Test
|
||||
* @see org.springframework.data.gemfire.mapping.MappingPdxSerializer
|
||||
*/
|
||||
public class MappingPdxSerializerIntegrationTests {
|
||||
|
||||
@@ -79,6 +81,9 @@ public class MappingPdxSerializerIntegrationTests {
|
||||
|
||||
MappingPdxSerializer serializer = MappingPdxSerializer.newMappingPdxSerializer();
|
||||
|
||||
serializer.setIncludeTypeFilters(type ->
|
||||
type.getPackage().getName().startsWith("org.springframework.data.gemfire"));
|
||||
|
||||
cache = new CacheFactory()
|
||||
.set("name", MappingPdxSerializerIntegrationTests.class.getSimpleName())
|
||||
.set("log-level", "error")
|
||||
@@ -344,7 +349,7 @@ public class MappingPdxSerializerIntegrationTests {
|
||||
private DataSerializableProperty property;
|
||||
|
||||
public PersonWithDataSerializableProperty(Long id, String firstname,
|
||||
String lastname, DataSerializableProperty property) {
|
||||
String lastname, DataSerializableProperty property) {
|
||||
|
||||
super(id, firstname, lastname);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -34,11 +34,14 @@ import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.gemstone.gemfire.TestGemStoneGemFireType;
|
||||
|
||||
import org.apache.geode.pdx.PdxReader;
|
||||
import org.apache.geode.pdx.PdxSerializer;
|
||||
import org.apache.geode.pdx.PdxWriter;
|
||||
@@ -47,15 +50,22 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.core.SpringVersion;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.core.type.ClassMetadata;
|
||||
import org.springframework.data.convert.EntityInstantiator;
|
||||
import org.springframework.data.convert.EntityInstantiators;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.repository.sample.Account;
|
||||
import org.springframework.data.gemfire.repository.sample.Address;
|
||||
import org.springframework.data.gemfire.repository.sample.Algorithm;
|
||||
import org.springframework.data.gemfire.repository.sample.Animal;
|
||||
import org.springframework.data.gemfire.repository.sample.Customer;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.gemfire.repository.sample.Plant;
|
||||
import org.springframework.data.gemfire.repository.sample.Programmer;
|
||||
import org.springframework.data.gemfire.repository.sample.RootUser;
|
||||
import org.springframework.data.gemfire.repository.sample.User;
|
||||
@@ -73,7 +83,6 @@ import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
* @author John Blum
|
||||
* @see org.junit.Rule
|
||||
* @see org.junit.Test
|
||||
* @see org.junit.rules.ExpectedException
|
||||
* @see org.junit.runner.RunWith
|
||||
* @see org.mockito.Mock
|
||||
* @see org.mockito.Mockito
|
||||
@@ -87,20 +96,20 @@ import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MappingPdxSerializerUnitTests {
|
||||
|
||||
ConversionService conversionService;
|
||||
private ConversionService conversionService;
|
||||
|
||||
@Mock
|
||||
EntityInstantiator mockInstantiator;
|
||||
private EntityInstantiator mockEntityInstantiator;
|
||||
|
||||
GemfireMappingContext mappingContext;
|
||||
private GemfireMappingContext mappingContext;
|
||||
|
||||
MappingPdxSerializer pdxSerializer;
|
||||
private MappingPdxSerializer pdxSerializer;
|
||||
|
||||
@Mock
|
||||
PdxReader mockReader;
|
||||
private PdxReader mockReader;
|
||||
|
||||
@Mock
|
||||
PdxWriter mockWriter;
|
||||
private PdxWriter mockWriter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -110,8 +119,8 @@ public class MappingPdxSerializerUnitTests {
|
||||
this.pdxSerializer = spy(new MappingPdxSerializer(this.mappingContext, this.conversionService));
|
||||
}
|
||||
|
||||
private String toFullyQualifiedPropertyName(PersistentProperty<?> persistentProperty) {
|
||||
return this.pdxSerializer.toFullyQualifiedPropertyName(persistentProperty);
|
||||
private String toFullyQualifiedPropertyName(PersistentProperty<?> property) {
|
||||
return MappingPdxSerializer.PdxSerializerResolvers.toFullyQualifiedPropertyName(property);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -121,7 +130,7 @@ public class MappingPdxSerializerUnitTests {
|
||||
|
||||
assertThat(pdxSerializer.getConversionService()).isInstanceOf(DefaultConversionService.class);
|
||||
assertThat(pdxSerializer.getCustomPdxSerializers()).isEmpty();
|
||||
assertThat(pdxSerializer.getGemfireInstantiators()).isInstanceOf(EntityInstantiators.class);
|
||||
assertThat(pdxSerializer.getEntityInstantiators()).isInstanceOf(EntityInstantiators.class);
|
||||
assertThat(pdxSerializer.getMappingContext()).isInstanceOf(GemfireMappingContext.class);
|
||||
}
|
||||
|
||||
@@ -136,7 +145,7 @@ public class MappingPdxSerializerUnitTests {
|
||||
|
||||
assertThat(pdxSerializer.getConversionService()).isEqualTo(mockConversionService);
|
||||
assertThat(pdxSerializer.getCustomPdxSerializers()).isEmpty();
|
||||
assertThat(pdxSerializer.getGemfireInstantiators()).isInstanceOf(EntityInstantiators.class);
|
||||
assertThat(pdxSerializer.getEntityInstantiators()).isInstanceOf(EntityInstantiators.class);
|
||||
assertThat(pdxSerializer.getMappingContext()).isEqualTo(mockMappingContext);
|
||||
}
|
||||
|
||||
@@ -229,145 +238,31 @@ public class MappingPdxSerializerUnitTests {
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializers()).isEqualTo(customPdxSerializers);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setCustomPdxSerializersToNull() {
|
||||
|
||||
try {
|
||||
this.pdxSerializer.setCustomPdxSerializers(null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("Custom PdxSerializers must not be null");
|
||||
assertThat(expected).hasNoCause();
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void getCustomPdxSerializerForMappedPersistentPropertyReturnsPdxSerializerForProperty() {
|
||||
|
||||
PdxSerializer mockNamedSerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockPropertySerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockTypedSerializer = mock(PdxSerializer.class);
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(MapBuilder.<Object, PdxSerializer>newMapBuilder()
|
||||
.put(addressProperty, mockPropertySerializer)
|
||||
.put(toFullyQualifiedPropertyName(addressProperty), mockNamedSerializer)
|
||||
.put(Address.class, mockTypedSerializer)
|
||||
.build());
|
||||
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializer(addressProperty)).isEqualTo(mockPropertySerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void getCustomPdxSerializerForMappedPersistentPropertyReturnsPdxSerializerForPropertyName() {
|
||||
|
||||
PdxSerializer mockNamedSerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockTypedSerializer = mock(PdxSerializer.class);
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(MapBuilder.<Object, PdxSerializer>newMapBuilder()
|
||||
.put(toFullyQualifiedPropertyName(addressProperty), mockNamedSerializer)
|
||||
.put(Address.class, mockTypedSerializer)
|
||||
.build());
|
||||
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializer(addressProperty)).isEqualTo(mockNamedSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void getCustomPdxSerializerForMappedPersistentPropertyReturnsPdxSerializerForPropertyType() {
|
||||
|
||||
PdxSerializer mockNamedSerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockTypedSerializer = mock(PdxSerializer.class);
|
||||
|
||||
Map<Object, PdxSerializer> customPdxSerializers = new HashMap<>();
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
|
||||
customPdxSerializers.put("example.Type.address", mockNamedSerializer);
|
||||
customPdxSerializers.put(Address.class, mockTypedSerializer);
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(customPdxSerializers);
|
||||
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializer(addressProperty)).isEqualTo(mockTypedSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void getCustomPdxSerializerForUnmappedPersistentPropertyReturnsNull() {
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
public void setCustomPdxSerializersIsNullSafe() {
|
||||
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializers()).isEmpty();
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializer(addressProperty)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void getCustomSerializerForMappedTypeReturnsPdxSerializer() {
|
||||
this.pdxSerializer.setCustomPdxSerializers(null);
|
||||
|
||||
PdxSerializer mockPdxSerializer = mock(PdxSerializer.class);
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(Collections.singletonMap(Person.class, mockPdxSerializer));
|
||||
|
||||
assertThat(this.pdxSerializer.getCustomSerializer(Person.class)).isEqualTo(mockPdxSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void getCustomSerializerForUnmappedTypeReturnsNull() {
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializers()).isEmpty();
|
||||
assertThat(this.pdxSerializer.getCustomSerializer(Address.class)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toFullyQualifiedPropertyName() {
|
||||
|
||||
PersistentEntity mockEntity = mock(PersistentEntity.class);
|
||||
PersistentProperty mockProperty = mock(PersistentProperty.class);
|
||||
|
||||
when(mockProperty.getName()).thenReturn("mockProperty");
|
||||
when(mockProperty.getOwner()).thenReturn(mockEntity);
|
||||
when(mockEntity.getType()).thenReturn(Person.class);
|
||||
|
||||
assertThat(this.pdxSerializer.toFullyQualifiedPropertyName(mockProperty))
|
||||
.isEqualTo(Person.class.getName().concat(".mockProperty"));
|
||||
|
||||
verify(mockEntity, times(1)).getType();
|
||||
verify(mockProperty, times(1)).getName();
|
||||
verify(mockProperty, times(1)).getOwner();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setGemfireInstantiatorsWithEntityInstantiators() {
|
||||
public void setEntityInstantiatorsWithNonNullEntityInstantiators() {
|
||||
|
||||
EntityInstantiators mockEntityInstantiators = mock(EntityInstantiators.class);
|
||||
|
||||
this.pdxSerializer.setGemfireInstantiators(mockEntityInstantiators);
|
||||
this.pdxSerializer.setEntityInstantiators(mockEntityInstantiators);
|
||||
|
||||
assertThat(this.pdxSerializer.getGemfireInstantiators()).isSameAs(mockEntityInstantiators);
|
||||
assertThat(this.pdxSerializer.getEntityInstantiators()).isSameAs(mockEntityInstantiators);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setGemfireInstantiatorsWithNullEntityInstantiators() {
|
||||
public void setEntityInstantiatorsWithNullEntityInstantiators() {
|
||||
|
||||
try {
|
||||
this.pdxSerializer.setGemfireInstantiators((EntityInstantiators) null);
|
||||
this.pdxSerializer.setEntityInstantiators((EntityInstantiators) null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
@@ -379,21 +274,21 @@ public class MappingPdxSerializerUnitTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setGemfireInstantiatorsWithMappingOfClassTypesToEntityInstantiators() {
|
||||
public void setEntityInstantiatorsWithNonNullMappingOfClassTypesToEntityInstantiators() {
|
||||
|
||||
Map<Class<?>, EntityInstantiator> entityInstantiators =
|
||||
Collections.singletonMap(Person.class, mock(EntityInstantiator.class));
|
||||
|
||||
this.pdxSerializer.setGemfireInstantiators(entityInstantiators);
|
||||
this.pdxSerializer.setEntityInstantiators(entityInstantiators);
|
||||
|
||||
assertThat(this.pdxSerializer.getGemfireInstantiators()).isInstanceOf(EntityInstantiators.class);
|
||||
assertThat(this.pdxSerializer.getEntityInstantiators()).isInstanceOf(EntityInstantiators.class);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setGemfireInstantiatorsWithNullMap() {
|
||||
public void setEntityInstantiatorsWithNullMap() {
|
||||
|
||||
try {
|
||||
this.pdxSerializer.setGemfireInstantiators((Map<Class<?>, EntityInstantiator>) null);
|
||||
this.pdxSerializer.setEntityInstantiators((Map<Class<?>, EntityInstantiator>) null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
@@ -404,40 +299,6 @@ public class MappingPdxSerializerUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInstantiatorForManagedPersistentEntityWithInstantiator() {
|
||||
|
||||
EntityInstantiator mockEntityInstantiator = mock(EntityInstantiator.class);
|
||||
|
||||
PersistentEntity mockEntity = mock(PersistentEntity.class);
|
||||
|
||||
when(mockEntity.getType()).thenReturn(Person.class);
|
||||
|
||||
this.pdxSerializer.setGemfireInstantiators(Collections.singletonMap(Person.class, mockEntityInstantiator));
|
||||
|
||||
assertThat(this.pdxSerializer.getInstantiatorFor(mockEntity)).isEqualTo(mockEntityInstantiator);
|
||||
|
||||
verify(mockEntity, atLeast(1)).getType();
|
||||
verifyZeroInteractions(mockEntityInstantiator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInstantiatorForNonManagedPersistentEntityWithNoInstantiator() {
|
||||
|
||||
EntityInstantiator mockEntityInstantiator = mock(EntityInstantiator.class);
|
||||
|
||||
PersistentEntity mockEntity = mock(PersistentEntity.class);
|
||||
|
||||
when(mockEntity.getType()).thenReturn(Address.class);
|
||||
|
||||
this.pdxSerializer.setGemfireInstantiators(Collections.singletonMap(Person.class, mockEntityInstantiator));
|
||||
|
||||
assertThat(this.pdxSerializer.getInstantiatorFor(mockEntity)).isNotEqualTo(mockEntityInstantiator);
|
||||
|
||||
verify(mockEntity, atLeast(1)).getType();
|
||||
verifyZeroInteractions(mockEntityInstantiator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isReadableWithNonTransientPropertyReturnsTrue() {
|
||||
|
||||
@@ -531,9 +392,144 @@ public class MappingPdxSerializerUnitTests {
|
||||
verify(mockProperty, times(1)).isTransient();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void resolveCustomPdxSerializerReturnsNull() {
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
|
||||
assertThat(this.pdxSerializer.getCustomPdxSerializers()).isEmpty();
|
||||
assertThat(this.pdxSerializer.resolveCustomPdxSerializer(addressProperty)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void resolveCustomPdxSerializerReturnsPdxSerializerForProperty() {
|
||||
|
||||
PdxSerializer mockNamedSerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockPropertySerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockTypedSerializer = mock(PdxSerializer.class);
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(MapBuilder.<Object, PdxSerializer>newMapBuilder()
|
||||
.put(addressProperty, mockPropertySerializer)
|
||||
.put(toFullyQualifiedPropertyName(addressProperty), mockNamedSerializer)
|
||||
.put(Address.class, mockTypedSerializer)
|
||||
.build());
|
||||
|
||||
assertThat(this.pdxSerializer.resolveCustomPdxSerializer(addressProperty)).isEqualTo(mockPropertySerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void resolveCustomPdxSerializerReturnsPdxSerializerForPropertyName() {
|
||||
|
||||
PdxSerializer mockNamedSerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockTypedSerializer = mock(PdxSerializer.class);
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(MapBuilder.<Object, PdxSerializer>newMapBuilder()
|
||||
.put(toFullyQualifiedPropertyName(addressProperty), mockNamedSerializer)
|
||||
.put(Address.class, mockTypedSerializer)
|
||||
.build());
|
||||
|
||||
assertThat(this.pdxSerializer.resolveCustomPdxSerializer(addressProperty)).isEqualTo(mockNamedSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void resolveCustomPdxSerializerReturnsPdxSerializerForPropertyType() {
|
||||
|
||||
PdxSerializer mockNamedSerializer = mock(PdxSerializer.class);
|
||||
PdxSerializer mockTypedSerializer = mock(PdxSerializer.class);
|
||||
|
||||
Map<Object, PdxSerializer> customPdxSerializers = new HashMap<>();
|
||||
|
||||
PersistentEntity personEntity = this.mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
PersistentProperty addressProperty = personEntity.getPersistentProperty("address");
|
||||
|
||||
customPdxSerializers.put("example.Type.address", mockNamedSerializer);
|
||||
customPdxSerializers.put(Address.class, mockTypedSerializer);
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(customPdxSerializers);
|
||||
|
||||
assertThat(this.pdxSerializer.resolveCustomPdxSerializer(addressProperty)).isEqualTo(mockTypedSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveEntityInstantiatorForManagedPersistentEntityWithEntityInstantiator() {
|
||||
|
||||
EntityInstantiator mockEntityInstantiator = mock(EntityInstantiator.class);
|
||||
|
||||
PersistentEntity mockEntity = mock(PersistentEntity.class);
|
||||
|
||||
when(mockEntity.getType()).thenReturn(Person.class);
|
||||
|
||||
this.pdxSerializer.setEntityInstantiators(Collections.singletonMap(Person.class, mockEntityInstantiator));
|
||||
|
||||
assertThat(this.pdxSerializer.resolveEntityInstantiator(mockEntity)).isEqualTo(mockEntityInstantiator);
|
||||
|
||||
verify(mockEntity, atLeast(1)).getType();
|
||||
verifyZeroInteractions(mockEntityInstantiator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveEntityInstantiatorForNonManagedPersistentEntityWithNoEntityInstantiator() {
|
||||
|
||||
EntityInstantiator mockEntityInstantiator = mock(EntityInstantiator.class);
|
||||
|
||||
PersistentEntity mockEntity = mock(PersistentEntity.class);
|
||||
|
||||
when(mockEntity.getType()).thenReturn(Address.class);
|
||||
|
||||
this.pdxSerializer.setEntityInstantiators(Collections.singletonMap(Person.class, mockEntityInstantiator));
|
||||
|
||||
assertThat(this.pdxSerializer.resolveEntityInstantiator(mockEntity)).isNotEqualTo(mockEntityInstantiator);
|
||||
|
||||
verify(mockEntity, atLeast(1)).getType();
|
||||
verifyZeroInteractions(mockEntityInstantiator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveTypeWithNonNullType() {
|
||||
assertThat(this.pdxSerializer.resolveType("test")).isEqualTo(String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveTypeWithNullType() {
|
||||
assertThat(this.pdxSerializer.resolveType(null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toFullyQualifiedPropertyName() {
|
||||
|
||||
PersistentEntity mockEntity = mock(PersistentEntity.class);
|
||||
PersistentProperty mockProperty = mock(PersistentProperty.class);
|
||||
|
||||
when(mockProperty.getName()).thenReturn("mockProperty");
|
||||
when(mockProperty.getOwner()).thenReturn(mockEntity);
|
||||
when(mockEntity.getType()).thenReturn(Person.class);
|
||||
|
||||
assertThat(MappingPdxSerializer.PdxSerializerResolvers.toFullyQualifiedPropertyName(mockProperty))
|
||||
.isEqualTo(Person.class.getName().concat(".mockProperty"));
|
||||
|
||||
verify(mockEntity, times(1)).getType();
|
||||
verify(mockProperty, times(1)).getName();
|
||||
verify(mockProperty, times(1)).getOwner();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void fromDataDeserializesPdxBytesAndMapsToApplicationDomainObject() {
|
||||
public void fromDataDeserializesPdxBytesAndMapsToEntity() {
|
||||
|
||||
Address expectedAddress = new Address();
|
||||
|
||||
@@ -543,7 +539,7 @@ public class MappingPdxSerializerUnitTests {
|
||||
|
||||
PdxSerializer mockAddressSerializer = mock(PdxSerializer.class);
|
||||
|
||||
when(this.mockInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
|
||||
when(this.mockEntityInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
|
||||
.thenReturn(new Person(null, null, null));
|
||||
when(this.mockReader.readField(eq("id"))).thenReturn(1L);
|
||||
when(this.mockReader.readField(eq("firstname"))).thenReturn("Jon");
|
||||
@@ -551,7 +547,9 @@ public class MappingPdxSerializerUnitTests {
|
||||
when(mockAddressSerializer.fromData(eq(Address.class), eq(this.mockReader))).thenReturn(expectedAddress);
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(Collections.singletonMap(Address.class, mockAddressSerializer));
|
||||
this.pdxSerializer.setGemfireInstantiators(Collections.singletonMap(Person.class, this.mockInstantiator));
|
||||
this.pdxSerializer.setEntityInstantiators(Collections.singletonMap(Person.class, this.mockEntityInstantiator));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Address.class.isAssignableFrom(type));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Person.class.isAssignableFrom(type));
|
||||
|
||||
Object obj = this.pdxSerializer.fromData(Person.class, this.mockReader);
|
||||
|
||||
@@ -564,7 +562,7 @@ public class MappingPdxSerializerUnitTests {
|
||||
assertThat(jonDoe.getFirstname()).isEqualTo("Jon");
|
||||
assertThat(jonDoe.getLastname()).isEqualTo("Doe");
|
||||
|
||||
verify(this.mockInstantiator, times(1))
|
||||
verify(this.mockEntityInstantiator, times(1))
|
||||
.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class));
|
||||
verify(this.mockReader, times(1)).readField(eq("id"));
|
||||
verify(this.mockReader, times(1)).readField(eq("firstname"));
|
||||
@@ -577,13 +575,14 @@ public class MappingPdxSerializerUnitTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void fromDataHandlesException() {
|
||||
|
||||
when(this.mockInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
|
||||
when(this.mockEntityInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
|
||||
.thenReturn(new Person(null, null, null));
|
||||
|
||||
when(this.mockReader.readField(eq("id"))).thenThrow(newIllegalArgumentException("test"));
|
||||
|
||||
try {
|
||||
this.pdxSerializer.setGemfireInstantiators(Collections.singletonMap(Person.class, this.mockInstantiator));
|
||||
this.pdxSerializer.setEntityInstantiators(Collections.singletonMap(Person.class, this.mockEntityInstantiator));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Person.class.equals(type));
|
||||
this.pdxSerializer.fromData(Person.class, this.mockReader);
|
||||
}
|
||||
catch (MappingException expected) {
|
||||
@@ -596,7 +595,7 @@ public class MappingPdxSerializerUnitTests {
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
verify(this.mockInstantiator, times(1))
|
||||
verify(this.mockEntityInstantiator, times(1))
|
||||
.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class));
|
||||
|
||||
verify(this.mockReader, times(1)).readField(eq("id"));
|
||||
@@ -605,7 +604,7 @@ public class MappingPdxSerializerUnitTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void fromDataUsesRegisteredInstantiator() {
|
||||
public void fromDataUsesRegisteredEntityInstantiator() {
|
||||
|
||||
Address address = new Address();
|
||||
|
||||
@@ -619,17 +618,18 @@ public class MappingPdxSerializerUnitTests {
|
||||
|
||||
person.address = address;
|
||||
|
||||
when(this.mockInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
|
||||
when(this.mockEntityInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
|
||||
.thenReturn(person);
|
||||
|
||||
this.pdxSerializer.setCustomPdxSerializers(Collections.singletonMap(Address.class, mockAddressSerializer));
|
||||
this.pdxSerializer.setGemfireInstantiators(Collections.singletonMap(Person.class, this.mockInstantiator));
|
||||
this.pdxSerializer.setEntityInstantiators(Collections.singletonMap(Person.class, this.mockEntityInstantiator));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Person.class.equals(type));
|
||||
this.pdxSerializer.fromData(Person.class, this.mockReader);
|
||||
|
||||
GemfirePersistentEntity<?> persistentEntity =
|
||||
Optional.ofNullable(this.mappingContext.getPersistentEntity(Person.class)).orElse(null);
|
||||
|
||||
verify(this.mockInstantiator, times(1))
|
||||
verify(this.mockEntityInstantiator, times(1))
|
||||
.createInstance(eq(persistentEntity), any(ParameterValueProvider.class));
|
||||
|
||||
verify(mockAddressSerializer, times(1))
|
||||
@@ -637,48 +637,82 @@ public class MappingPdxSerializerUnitTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromDataWithTypeFilterAcceptsApplicationDomainTypes() {
|
||||
public void fromDataWithTypeFilterAcceptsDeclaredEntityTypes() {
|
||||
|
||||
this.pdxSerializer.setTypeFilters(type -> User.class.getPackage().equals(type.getPackage()));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> User.class.getPackage().equals(type.getPackage()));
|
||||
|
||||
doReturn("test").when(this.pdxSerializer).doFromData(any(Class.class), any(PdxReader.class));
|
||||
|
||||
assertThat(this.pdxSerializer.fromData(Account.class, this.mockReader)).isEqualTo("test");
|
||||
assertThat(this.pdxSerializer.fromData(Algorithm.class, this.mockReader)).isEqualTo("test");
|
||||
assertThat(this.pdxSerializer.fromData(Animal.class, this.mockReader)).isEqualTo("test");
|
||||
assertThat(this.pdxSerializer.fromData(Customer.class, this.mockReader)).isEqualTo("test");
|
||||
assertThat(this.pdxSerializer.fromData(Plant.class, this.mockReader)).isEqualTo("test");
|
||||
assertThat(this.pdxSerializer.fromData(Programmer.class, this.mockReader)).isEqualTo("test");
|
||||
assertThat(this.pdxSerializer.fromData(User.class, this.mockReader)).isEqualTo("test");
|
||||
assertThat(this.pdxSerializer.fromData(org.springframework.data.gemfire.test.model.Person.class, this.mockReader)).isNull();
|
||||
assertThat(this.pdxSerializer.fromData(null, this.mockReader)).isNull();
|
||||
|
||||
verify(this.pdxSerializer, times(1)).doFromData(eq(Account.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, times(1)).doFromData(eq(Algorithm.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, times(1)).doFromData(eq(Animal.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, times(1)).doFromData(eq(Customer.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, times(1)).doFromData(eq(Plant.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, times(1)).doFromData(eq(Programmer.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, times(1)).doFromData(eq(User.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, never()).doFromData(isNull(), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, never())
|
||||
.doFromData(eq(org.springframework.data.gemfire.test.model.Person.class), eq(this.mockReader));
|
||||
verify(this.pdxSerializer, never()).doFromData(eq(org.springframework.data.gemfire.test.model.Person.class),
|
||||
eq(this.mockReader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromDataWithTypeFilterFiltersApacheGeodeTypeReturnsNull() {
|
||||
public void fromDataWithTypeFilterAcceptsIncludedTypesOverridingExcludedTypes() {
|
||||
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> type.getPackage().getName().startsWith("java.security"));
|
||||
|
||||
doReturn("test").when(this.pdxSerializer).doFromData(any(Class.class), any(PdxReader.class));
|
||||
|
||||
assertThat(this.pdxSerializer.fromData(Principal.class, this.mockReader)).isEqualTo("test");
|
||||
|
||||
verify(this.pdxSerializer, times(1))
|
||||
.doFromData(eq(Principal.class), eq(this.mockReader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromDataWithTypeFilterFiltersApacheGeodeTypesReturnsNull() {
|
||||
assertThat(this.pdxSerializer.fromData(org.apache.geode.cache.EntryEvent.class, this.mockReader)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromDataWithTypeFilterFiltersApplicationDomainTypeReturnsNull() {
|
||||
|
||||
this.pdxSerializer.setTypeFilters(type -> !ApplicationDomainType.class.equals(type));
|
||||
|
||||
assertThat(this.pdxSerializer.fromData(ApplicationDomainType.class, this.mockReader)).isNull();
|
||||
public void fromDataWithTypeFilterFiltersGemStoneTypesReturnsNull() {
|
||||
assertThat(this.pdxSerializer.fromData(TestGemStoneGemFireType.class, this.mockReader)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromDataWithTypeFilterFiltersNullTypeReturnsNull() {
|
||||
public void fromDataWithTypeFilterFiltersJavaTypesReturnsNull() {
|
||||
assertThat(this.pdxSerializer.fromData(java.security.Principal.class, this.mockReader)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromDataWithTypeFilterFiltersNullTypesReturnsNull() {
|
||||
assertThat(this.pdxSerializer.fromData(null, this.mockReader)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataSerializesApplicationDomainObjectToPdxBytes() {
|
||||
public void fromDataWithTypeFilterFiltersSpringFrameworkTypesReturnsNull() {
|
||||
|
||||
assertThat(this.pdxSerializer.fromData(ClassMetadata.class, this.mockReader)).isNull();
|
||||
assertThat(this.pdxSerializer.fromData(Page.class, this.mockReader)).isNull();
|
||||
assertThat(this.pdxSerializer.fromData(GemfireTemplate.class, this.mockReader)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromDataWithTypeFilterFiltersUndeclaredEntityTypesReturnsNull() {
|
||||
assertThat(this.pdxSerializer.fromData(ApplicationDomainType.class, this.mockReader)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataSerializesEntityToPdxBytes() {
|
||||
|
||||
Address address = new Address();
|
||||
|
||||
@@ -692,6 +726,8 @@ public class MappingPdxSerializerUnitTests {
|
||||
|
||||
jonDoe.address = address;
|
||||
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Address.class.equals(type));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Person.class.equals(type));
|
||||
this.pdxSerializer.setCustomPdxSerializers(Collections.singletonMap(Address.class, mockAddressSerializer));
|
||||
|
||||
assertThat(this.pdxSerializer.toData(jonDoe, this.mockWriter)).isTrue();
|
||||
@@ -727,6 +763,8 @@ public class MappingPdxSerializerUnitTests {
|
||||
.thenThrow(newIllegalArgumentException("test"));
|
||||
|
||||
try {
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Address.class.equals(type));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> Person.class.equals(type));
|
||||
this.pdxSerializer.setCustomPdxSerializers(Collections.emptyMap());
|
||||
this.pdxSerializer.toData(jonDoe, this.mockWriter);
|
||||
}
|
||||
@@ -761,49 +799,81 @@ public class MappingPdxSerializerUnitTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersNullTypeReturnsFalse() {
|
||||
assertThat(this.pdxSerializer.toData(null, this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersApacheGeodeTypeReturnsFalse() {
|
||||
assertThat(this.pdxSerializer.toData(mock(org.apache.geode.cache.EntryEvent.class), this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersApplicationDomainTypeReturnsFalse() {
|
||||
|
||||
this.pdxSerializer.setTypeFilters(type -> !ApplicationDomainType.class.equals(type));
|
||||
|
||||
assertThat(this.pdxSerializer.toData(new ApplicationDomainType(), this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataAcceptsApplicationDomainObjectTypeReturnsTrue() {
|
||||
public void toDataAcceptsDeclaredEntityTypeReturnsTrue() {
|
||||
|
||||
org.springframework.data.gemfire.test.model.Person jonDoe =
|
||||
new org.springframework.data.gemfire.test.model.Person("Jon", "Doe",
|
||||
null, Gender.MALE);
|
||||
|
||||
this.pdxSerializer.setTypeFilters(type -> User.class.getPackage().equals(type.getPackage()));
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> User.class.getPackage().equals(type.getPackage()));
|
||||
|
||||
doReturn(true).when(this.pdxSerializer).doToData(any(), any(PdxWriter.class));
|
||||
|
||||
assertThat(this.pdxSerializer.toData(new Account(1L), this.mockWriter)).isTrue();
|
||||
assertThat(this.pdxSerializer.toData(new Animal(), this.mockWriter)).isTrue();
|
||||
assertThat(this.pdxSerializer.toData(new Customer(1L), this.mockWriter)).isTrue();
|
||||
assertThat(this.pdxSerializer.toData(new Programmer("jxblum"), this.mockWriter)).isTrue();
|
||||
assertThat(this.pdxSerializer.toData(new Plant(), this.mockWriter)).isTrue();
|
||||
assertThat(this.pdxSerializer.toData(new RootUser("jxblum"), this.mockWriter)).isTrue();
|
||||
assertThat(this.pdxSerializer.toData(null, this.mockWriter)).isFalse();
|
||||
assertThat(this.pdxSerializer.toData(jonDoe, this.mockWriter)).isFalse();
|
||||
|
||||
verify(this.pdxSerializer, times(1)).doToData(isA(Account.class), eq(this.mockWriter));
|
||||
verify(this.pdxSerializer, times(1)).doToData(isA(Animal.class), eq(this.mockWriter));
|
||||
verify(this.pdxSerializer, times(1)).doToData(isA(Customer.class), eq(this.mockWriter));
|
||||
verify(this.pdxSerializer, times(1)).doToData(isA(Plant.class), eq(this.mockWriter));
|
||||
verify(this.pdxSerializer, times(1)).doToData(isA(Programmer.class), eq(this.mockWriter));
|
||||
verify(this.pdxSerializer, times(1)).doToData(isA(RootUser.class), eq(this.mockWriter));
|
||||
verify(this.pdxSerializer, never()).doToData(isNull(), eq(this.mockWriter));
|
||||
verify(this.pdxSerializer, never()).doToData(isA(jonDoe.getClass()), eq(this.mockWriter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataAcceptsIncludedEntityTypesOverridingExcludedEntityTypes() {
|
||||
|
||||
this.pdxSerializer.setIncludeTypeFilters(type -> type.getPackage().getName().startsWith("org.springframework"));
|
||||
|
||||
doReturn(true).when(this.pdxSerializer).doToData(any(), any(PdxWriter.class));
|
||||
|
||||
assertThat(this.pdxSerializer.toData(new SpringVersion(), this.mockWriter)).isTrue();
|
||||
|
||||
verify(this.pdxSerializer, times(1))
|
||||
.doToData(isA(SpringVersion.class), eq(this.mockWriter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersApacheGeodeTypesReturnsFalse() {
|
||||
assertThat(this.pdxSerializer.toData(mock(org.apache.geode.cache.EntryEvent.class), this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersGemStoneTypesReturnsFalse() {
|
||||
assertThat(this.pdxSerializer.toData(TestGemStoneGemFireType.class, this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersJavaTypesReturnsFalse() {
|
||||
assertThat(this.pdxSerializer.toData(java.security.Principal.class, this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersNullTypesReturnsFalse() {
|
||||
assertThat(this.pdxSerializer.toData(null, this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersSpringFrameworkTypesReturnsFalse() {
|
||||
|
||||
assertThat(this.pdxSerializer.toData(ClassMetadata.class, this. mockWriter)).isFalse();
|
||||
assertThat(this.pdxSerializer.toData(Page.class, this.mockWriter)).isFalse();
|
||||
assertThat(this.pdxSerializer.toData(GemfireTemplate.class, this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDataFiltersUndeclaredEntityTypeReturnsFalse() {
|
||||
assertThat(this.pdxSerializer.toData(new ApplicationDomainType(), this.mockWriter)).isFalse();
|
||||
}
|
||||
|
||||
private static class ApplicationDomainType { }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user