From 764611cd35b0c27869ef30cf18a50a3b319ebb78 Mon Sep 17 00:00:00 2001 From: Nicki Watt Date: Mon, 12 Aug 2013 16:22:55 +0100 Subject: [PATCH] DATAGRAPH-311 : Special Handling of managed type fields when serializing/deserializing --- .../fieldaccess/ManagedFieldAccessorSet.java | 42 ++- .../ManagedPrefixedDynamicProperties.java | 46 ++- .../PrefixedDynamicProperties.java | 38 ++- .../data/neo4j/model/Person.java | 14 + .../SerializableEntityRepositoryTests.java | 265 ++++++++++++++++++ ...SerializableGraphQueryRepositoryTests.java | 40 +-- ...ializableEntityRepositoryTests-context.xml | 13 + 7 files changed, 410 insertions(+), 48 deletions(-) create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests.java create mode 100644 spring-data-neo4j/src/test/resources/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests-context.xml diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedFieldAccessorSet.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedFieldAccessorSet.java index 2bc5136d7..9858b1bfa 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedFieldAccessorSet.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedFieldAccessorSet.java @@ -17,12 +17,14 @@ package org.springframework.data.neo4j.fieldaccess; import org.springframework.data.neo4j.core.EntityState; -import org.springframework.data.neo4j.mapping.MappingPolicy; import org.springframework.data.neo4j.mapping.ManagedEntity; +import org.springframework.data.neo4j.mapping.MappingPolicy; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; import org.springframework.data.neo4j.support.DoReturn; import org.springframework.data.neo4j.support.Neo4jTemplate; +import java.io.InvalidObjectException; +import java.io.ObjectInputStream; import java.io.Serializable; import java.util.AbstractSet; import java.util.Collection; @@ -40,12 +42,20 @@ public class ManagedFieldAccessorSet extends AbstractSet implements Serial private static final long serialVersionUID = 1L; - private final Object entity; - final Set delegate; + private Object writeReplace() { + return new SerializationProxy(this); + } + + private void readObject(ObjectInputStream ois) throws InvalidObjectException { + throw new InvalidObjectException("Proxy required"); + } + + private final transient Object entity; + final transient Set delegate; private final transient Neo4jPersistentProperty property; private final transient Neo4jTemplate ctx; private final transient FieldAccessor fieldAccessor; - private final MappingPolicy mappingPolicy; + private final transient MappingPolicy mappingPolicy; @SuppressWarnings("unchecked") public ManagedFieldAccessorSet(final Object entity, final Object newVal, final Neo4jPersistentProperty property, Neo4jTemplate ctx, FieldAccessor fieldAccessor, final MappingPolicy mappingPolicy) { @@ -149,4 +159,26 @@ public class ManagedFieldAccessorSet extends AbstractSet implements Serial delegate.clear(); update(); } -} \ No newline at end of file + + /** + * Implementation of the Serialization Proxy Pattern (ref Item 78 + * of Effective Java - 2nd edition) + * @param Type of the underlying class being stored in the Set. + */ + private static class SerializationProxy implements Serializable { + + private static final long serialVersionUID = 1L; + private Set delegateSet; + + SerializationProxy(ManagedFieldAccessorSet managedFieldAccessorSet) { + this.delegateSet = managedFieldAccessorSet.delegate; + } + + private Object readResolve() { + return delegateSet; + } + + } + +} + diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedPrefixedDynamicProperties.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedPrefixedDynamicProperties.java index 1f4ff30cc..b81ee713e 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedPrefixedDynamicProperties.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/ManagedPrefixedDynamicProperties.java @@ -16,28 +16,40 @@ package org.springframework.data.neo4j.fieldaccess; import org.springframework.data.neo4j.core.EntityState; -import org.springframework.data.neo4j.mapping.MappingPolicy; import org.springframework.data.neo4j.mapping.ManagedEntity; +import org.springframework.data.neo4j.mapping.MappingPolicy; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; import org.springframework.data.neo4j.support.DoReturn; import org.springframework.data.neo4j.support.Neo4jTemplate; +import java.io.InvalidObjectException; +import java.io.ObjectInputStream; +import java.io.Serializable; import java.util.Map; /** * Updates the entity containing such a ManagedPrefixedDynamicProperties when some property is added, changed or * deleted. */ -public class ManagedPrefixedDynamicProperties extends PrefixedDynamicProperties { +public class ManagedPrefixedDynamicProperties extends PrefixedDynamicProperties implements Serializable +{ private static final long serialVersionUID = 1L; - private final Object entity; + private Object writeReplace() { + return new SerializationProxy(this); + } + + private void readObject(ObjectInputStream ois) throws InvalidObjectException { + throw new InvalidObjectException("Proxy required"); + } + + private transient final Object entity; private transient final Neo4jTemplate template; private transient final FieldAccessor fieldAccessor; private transient final Neo4jPersistentProperty property; - private boolean isNode; - private MappingPolicy mappingPolicy; + private transient boolean isNode; + private transient MappingPolicy mappingPolicy; public ManagedPrefixedDynamicProperties(String prefix, final Neo4jPersistentProperty property, final Object entity, Neo4jTemplate template, FieldAccessor fieldAccessor, final MappingPolicy mappingPolicy) { this(prefix,10,property,entity, template,fieldAccessor, mappingPolicy); @@ -105,4 +117,28 @@ public class ManagedPrefixedDynamicProperties extends PrefixedDynamicProperties property.setValue(entity, newValue); return newValue; } + + /** + * Implementation of the Serialization Proxy Pattern (ref Item 78 + * of Effective Java - 2nd edition) + * @param Type of the underlying class being stored in the Set. + */ + private static class SerializationProxy implements Serializable { + + private static final long serialVersionUID = 1L; + private Map actualMapContent; + private String prefix; + + SerializationProxy(ManagedPrefixedDynamicProperties prefixedDynamicProperties) { + this.actualMapContent = prefixedDynamicProperties.asMap(); + this.prefix = prefixedDynamicProperties.prefix; + } + + private Object readResolve() { + PrefixedDynamicProperties val = new PrefixedDynamicProperties(prefix); + val.setPropertiesFrom(actualMapContent); + return val; + } + + } } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PrefixedDynamicProperties.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PrefixedDynamicProperties.java index fea4b1edd..f7c0dcc51 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PrefixedDynamicProperties.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PrefixedDynamicProperties.java @@ -15,6 +15,8 @@ */ package org.springframework.data.neo4j.fieldaccess; +import java.io.InvalidObjectException; +import java.io.ObjectInputStream; import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; @@ -32,8 +34,16 @@ public class PrefixedDynamicProperties implements DynamicProperties , Serializab private static final long serialVersionUID = 1L; - private final Map map; - protected final String prefix; + private Object writeReplace() { + return new SerializationProxy(this); + } + + private void readObject(ObjectInputStream ois) throws InvalidObjectException { + throw new InvalidObjectException("Proxy required"); + } + + private transient final Map map; + protected final transient String prefix; /** * Handles key prefixing @@ -295,4 +305,28 @@ public class PrefixedDynamicProperties implements DynamicProperties , Serializab } return true; } + + /** + * Implementation of the Serialization Proxy Pattern (ref Item 78 + * of Effective Java - 2nd edition) + * @param Type of the underlying class being stored in the Set. + */ + private static class SerializationProxy implements Serializable { + + private static final long serialVersionUID = 1L; + private Map actualMapContent; + private String prefix; + + SerializationProxy(PrefixedDynamicProperties prefixedDynamicProperties) { + this.actualMapContent = prefixedDynamicProperties.map; + this.prefix = prefixedDynamicProperties.prefix; + } + + private Object readResolve() { + PrefixedDynamicProperties val = new PrefixedDynamicProperties(prefix); + val.setPropertiesFrom(actualMapContent); + return val; + } + + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java index b4c59323b..738c4e07c 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java @@ -102,6 +102,9 @@ public class Person implements Being , Serializable { @RelatedTo(elementClass = Group.class, type = "interface_test", direction = Direction.OUTGOING) private Set groups; + @RelatedTo(elementClass = Person.class, type = "serialiation_test", direction = Direction.OUTGOING) + private Set serialFriends; + RootEntity root; public RootEntity getRoot() { @@ -313,4 +316,15 @@ public class Person implements Being , Serializable { public BestFriend getBestFriend() { return bestFriend; } + + public Set getSerialFriends() { + if (serialFriends == null) { + serialFriends = new HashSet(); + } + return serialFriends; + } + + public void addSerialFriend(Person serialFriend) { + getSerialFriends().add(serialFriend); + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests.java new file mode 100644 index 000000000..bdba1fac9 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests.java @@ -0,0 +1,265 @@ +/** + * Copyright 2011 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.neo4j.repository; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.fieldaccess.ManagedFieldAccessorSet; +import org.springframework.data.neo4j.fieldaccess.ManagedPrefixedDynamicProperties; +import org.springframework.data.neo4j.fieldaccess.PrefixedDynamicProperties; +import org.springframework.data.neo4j.model.Person; +import org.springframework.data.neo4j.support.Neo4jTemplate; +import org.springframework.data.neo4j.support.node.Neo4jHelper; +import org.springframework.test.context.CleanContextCacheTestExecutionListener; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.transaction.BeforeTransaction; +import org.springframework.test.context.transaction.TransactionalTestExecutionListener; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.TransactionCallbackWithoutResult; +import org.springframework.transaction.support.TransactionTemplate; + +import java.io.*; +import java.util.Date; +import java.util.HashSet; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.neo4j.helpers.collection.IteratorUtil.asCollection; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) +public class SerializableEntityRepositoryTests { + + protected final Logger log = LoggerFactory.getLogger(getClass()); + + @Autowired + private PlatformTransactionManager transactionManager; + + @Autowired + private Neo4jTemplate neo4jTemplate; + + @Autowired + private PersonRepository personRepository; + + @Autowired + GroupRepository groupRepository; + + @Autowired + FriendshipRepository friendshipRepository; + + private SerialTesters serialTesters; + private Date expectedBirthDate; + + + @BeforeTransaction + public void cleanDb() { + Neo4jHelper.cleanDb(neo4jTemplate); + } + + @Before + public void setUp() throws Exception { + serialTesters = new SerialTesters(); + serialTesters.createUpgraderTeam(personRepository, groupRepository, friendshipRepository); + expectedBirthDate = serialTesters.bdayFormatter.parse("01 JAN 2013 00:00:00"); + + } + + @Test + public void shouldBeAbleToSerializeAndDeserializeBasicEntityGraph() throws Exception { + Person person = personRepository.findOne(serialTesters.nicki.getId()); + assertEntityDetailsForPerson(person); + assertThat(person, instanceOf(Serializable.class)); + + // Do it + byte[] bos = serializeIt(person); + Person aDeserializedPerson = deserializeIt(bos); + + // Verify its the same + assertEntityDetailsForPerson(aDeserializedPerson); + } + + @Test + public void primitiveFieldShouldBeSerializedInOriginalForm() throws Exception { + Person deserializedPerson = assertPreSerializationSetupThenGetDeserializedPerson(); + assertEquals(String.class, deserializedPerson.getName().getClass()); + assertEquals("Nicki", deserializedPerson.getName()); + } + + @Test + public void primitiveFieldUpdatedOnDeserializedEntityShouldBeAbleToBeSavedBackToRepo() throws Exception { + final Person deserializedPerson = assertPreSerializationSetupThenGetDeserializedPerson(); + assertEquals(String.class, deserializedPerson.getName().getClass()); + assertEquals("Nicki", deserializedPerson.getName()); + + deserializedPerson.setName("New Name"); + new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus status) { + personRepository.save(deserializedPerson); + } + }); + + Person personFromDB = personRepository.findOne(deserializedPerson.getId()); + assertEquals("New Name", personFromDB.getName()); + } + + @Test + public void managedFieldAkaRelationshipsShouldBeSerializedAsAHashSet() throws Exception { + Person deserializedPerson = assertPreSerializationSetupThenGetDeserializedPerson(); + assertEquals(HashSet.class, deserializedPerson.getSerialFriends().getClass()); + assertEquals(1, deserializedPerson.getSerialFriends().size()); + assertThat(deserializedPerson.getSerialFriends(), hasItem(serialTesters.michael)); + } + + @Test + public void managedFieldAkaRelationshipUpdatedOnDeserializedEntityShouldBeAbleToBeSavedBackToRepo() throws Exception { + final Person deserializedPerson = assertPreSerializationSetupThenGetDeserializedPerson(); + assertEquals(HashSet.class, deserializedPerson.getSerialFriends().getClass()); + assertEquals(1, deserializedPerson.getSerialFriends().size()); + assertThat(deserializedPerson.getSerialFriends(), hasItem(serialTesters.michael)); + + deserializedPerson.addSerialFriend(serialTesters.david); + assertEquals(2, deserializedPerson.getSerialFriends().size()); + new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus status) { + personRepository.save(deserializedPerson); + } + }); + + Person personFromDB = personRepository.findOne(deserializedPerson.getId()); + assertEquals(2, personFromDB.getSerialFriends().size()); + assertThat(personFromDB.getSerialFriends(), hasItem(serialTesters.michael)); + assertThat(personFromDB.getSerialFriends(), hasItem(serialTesters.david)); + } + + + @Test + public void dynamicPropertiesFieldShouldBeSerializedAsAPrefixedDynamicProperties() throws Exception { + final Person deserializedPerson = assertPreSerializationSetupThenGetDeserializedPerson(); + assertEquals(PrefixedDynamicProperties.class, deserializedPerson.getPersonalProperties().getClass()); + assertEquals(2, deserializedPerson.getPersonalProperties().asMap().size()); + assertThat(asCollection( deserializedPerson.getPersonalProperties().getPropertyKeys()) , hasItems("addressLine1","addressLine2")); + } + + @Test + public void dynamicPropertiesFieldUpdatedOnDeserializedEntityShouldBeAbleToBeSavedBackToRepo() throws Exception { + final Person deserializedPerson = assertPreSerializationSetupThenGetDeserializedPerson(); + assertEquals(PrefixedDynamicProperties.class, deserializedPerson.getPersonalProperties().getClass()); + assertEquals(2, deserializedPerson.getPersonalProperties().asMap().size()); + assertThat(asCollection(deserializedPerson.getPersonalProperties().getPropertyKeys()) , hasItems("addressLine1", "addressLine2")); + + deserializedPerson.setProperty("newDynoProp", "newDynoValue"); + new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus status) { + personRepository.save(deserializedPerson); + } + }); + + Person personFromDB = personRepository.findOne(deserializedPerson.getId()); + assertEquals(3, personFromDB.getPersonalProperties().asMap().size()); + assertThat(asCollection(personFromDB.getPersonalProperties().getPropertyKeys()) , hasItems("addressLine1", "addressLine2", "newDynoProp")); + + } + + private Person assertPreSerializationSetupThenGetDeserializedPerson() throws Exception { + addSerialFriend(serialTesters.nicki.getId(), serialTesters.michael); + + // 1A. Make sure that before we deal with any serialization, we are still operating + // with the expected ManagedFieldAccessorSet class + final Person person = personRepository.findOne(serialTesters.nicki.getId()); + assertEquals(ManagedFieldAccessorSet.class, person.getSerialFriends().getClass()); + assertEquals(1, person.getSerialFriends().size()); + + // 1B. Make sure that before we deal with any serialization, we are still operating + // with the expected ManagedPrefixedDynamicProperties class + assertEquals(ManagedPrefixedDynamicProperties.class, person.getPersonalProperties().getClass()); + assertEquals(2, person.getPersonalProperties().asMap().size()); + assertThat(asCollection( person.getPersonalProperties().getPropertyKeys()) , hasItems("addressLine1","addressLine2")); + + // 2. Do Serialization and return serialized object + byte[] bos = serializeIt(person); + return deserializeIt(bos); + } + + + private void addSerialFriend(Long sourcePersonId, final Person target) { + final Person person1 = personRepository.findOne(sourcePersonId); + new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus status) { + person1.addSerialFriend(target); + personRepository.save(person1); + } + }); + } + + public void assertPOJOContainsExpectedData(MemberDataPOJO pojo) throws Exception { + assertNotNull(pojo); + assertThat(pojo.getBoss(), is(serialTesters.tareq)); + assertThat(asCollection(pojo.getTeams()), hasItem(serialTesters.serialTesterGroup)); + assertThat(pojo.getAnInt(), is(serialTesters.tareq.getAge())); + assertThat(pojo.getAName(), is(serialTesters.tareq.getName())); + } + + private byte[] serializeIt(T someObject) throws Exception { + ObjectOutputStream out = null; + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + out = new ObjectOutputStream(bos); + out.writeObject(someObject); + return bos.toByteArray(); + } finally { + if (out != null) out.close(); + } + } + + private T deserializeIt(byte[] serializedBytes) throws Exception { + ObjectInputStream in = null; + try { + ByteArrayInputStream bis = new ByteArrayInputStream(serializedBytes); + in = new ObjectInputStream(bis); + Object theNewObj = in.readObject(); + return (T)theNewObj; + } finally { + if (in != null) in.close(); + } + } + + private void assertEntityDetailsForPerson(Person aPerson) { + assertThat(aPerson.getAge(), is(equalTo(36))); + assertThat(aPerson.getBoss(), is(serialTesters.tareq)); + assertThat(aPerson.getBirthdate(), is(equalTo(expectedBirthDate))); + assertThat(aPerson.getName(), is(equalTo("Nicki"))); + assertThat(aPerson.getDynamicProperty(), is(equalTo((Object)"What is this???"))); + assertThat(aPerson.getFriendships(), hasItems(serialTesters.friendShip2, serialTesters.friendShip3)) ; + assertThat(aPerson.getHeight(), is(equalTo((short)100))); + assertThat(aPerson.getProperty("addressLine1"), is(equalTo((Object)"Somewhere"))); + assertThat(aPerson.getProperty("addressLine2"), is(equalTo((Object)"Over the rainbow"))); + } + +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableGraphQueryRepositoryTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableGraphQueryRepositoryTests.java index 2027b4f0c..47346b0d6 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableGraphQueryRepositoryTests.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/SerializableGraphQueryRepositoryTests.java @@ -22,7 +22,6 @@ import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.neo4j.model.Person; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.node.Neo4jHelper; import org.springframework.test.context.CleanContextCacheTestExecutionListener; @@ -35,7 +34,6 @@ import org.springframework.test.context.transaction.TransactionalTestExecutionLi import org.springframework.transaction.annotation.Transactional; import java.io.*; -import java.util.Date; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertNotNull; @@ -62,8 +60,6 @@ public class SerializableGraphQueryRepositoryTests { FriendshipRepository friendshipRepository; private SerialTesters serialTesters; - private Date expectedBirthDate; - @BeforeTransaction public void cleanDb() { @@ -74,41 +70,21 @@ public class SerializableGraphQueryRepositoryTests { public void setUp() throws Exception { serialTesters = new SerialTesters(); serialTesters.createUpgraderTeam(personRepository, groupRepository, friendshipRepository); - expectedBirthDate = serialTesters.bdayFormatter.parse("01 JAN 2013 00:00:00"); - } @Test @Transactional - public void shouldBeAbleToTurnQueryResultsIntoAPOJO() throws Exception { + public void shouldBeAbleToTurnQueryResultIntoAPOJO() throws Exception { MemberDataPOJO nickisMemberData = personRepository.findMemberDataPojo(serialTesters.nicki); assertPOJOContainsExpectedData(nickisMemberData); } - @Test @Transactional - public void shouldBeAbleToSerializeAndDeserializeEntity() throws Exception { - Person anSDNUpgrader = personRepository.findOne(serialTesters.nicki.getId()); - assertEntityDetailsForPerson(anSDNUpgrader); - Person aDeserializedSDNUpgrader = assertObjectCanBeSerializedAndDeserialized(anSDNUpgrader); - assertEntityDetailsForPerson(aDeserializedSDNUpgrader); - } - - private void assertEntityDetailsForPerson(Person aPerson) { - assertThat(aPerson.getAge(), is(equalTo(36))); - assertThat(aPerson.getBoss(), is(serialTesters.tareq)); - assertThat(aPerson.getBirthdate(), is(equalTo(expectedBirthDate))); - assertThat(aPerson.getName(), is(equalTo("Nicki"))); - assertThat(aPerson.getDynamicProperty(), is(equalTo((Object)"What is this???"))); - assertThat(aPerson.getFriendships(), hasItems(serialTesters.friendShip2, serialTesters.friendShip3)) ; - assertThat(aPerson.getHeight(), is(equalTo((short)100))); - assertThat(aPerson.getProperty("addressLine1"), is(equalTo((Object)"Somewhere"))); - assertThat(aPerson.getProperty("addressLine2"), is(equalTo((Object)"Over the rainbow"))); - } - @Test @Transactional public void shouldBeAbleToSerializedPOJOReturnedFromQueryResult() throws Exception { MemberDataPOJO nickisOrigMemberData = personRepository.findMemberDataPojo(serialTesters.nicki); assertPOJOContainsExpectedData(nickisOrigMemberData); - MemberDataPOJO nickisDeserMemberData = assertObjectCanBeSerializedAndDeserialized(nickisOrigMemberData); + assertThat(nickisOrigMemberData, instanceOf(Serializable.class)); + byte[] bos = serializeIt(nickisOrigMemberData); + MemberDataPOJO nickisDeserMemberData = deserializeIt(bos); assertPOJOContainsExpectedData(nickisDeserMemberData); } @@ -120,12 +96,6 @@ public class SerializableGraphQueryRepositoryTests { assertThat(pojo.getAName(), is(serialTesters.tareq.getName())); } - private T assertObjectCanBeSerializedAndDeserialized(T someObject) throws Exception { - assertThat(someObject, instanceOf(Serializable.class)); - byte[] bos = serializeIt(someObject); - return deserializeIt(bos); - } - private byte[] serializeIt(T someObject) throws Exception { ObjectOutputStream out = null; try { @@ -151,6 +121,4 @@ public class SerializableGraphQueryRepositoryTests { } - - } diff --git a/spring-data-neo4j/src/test/resources/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests-context.xml b/spring-data-neo4j/src/test/resources/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests-context.xml new file mode 100644 index 000000000..08d6d11c3 --- /dev/null +++ b/spring-data-neo4j/src/test/resources/org/springframework/data/neo4j/repository/SerializableEntityRepositoryTests-context.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file