polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [35/48] polygene-java git commit: POLYGENE-231 Assert various deserialization scenarii
Date Mon, 13 Mar 2017 15:28:58 GMT
POLYGENE-231 Assert various deserialization scenarii

- do not choke on missing optional state
- do not choke on superfluous state
- works with shuffled state

And fix msgpack serialization that failed on the former use case


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/1d8f6fb6
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/1d8f6fb6
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/1d8f6fb6

Branch: refs/heads/serialization-3.0
Commit: 1d8f6fb63d30b0466d17022d149b7ae02bf0de2d
Parents: 514617f
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Mon Mar 6 14:54:17 2017 +0100
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Mon Mar 13 16:27:47 2017 +0100

----------------------------------------------------------------------
 ...AbstractValueCompositeSerializationTest.java | 110 ++++++++++++++++++-
 .../msgpack/MessagePackDeserializer.java        |   2 +-
 2 files changed, 107 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/1d8f6fb6/core/testsupport/src/main/java/org/apache/polygene/test/serialization/AbstractValueCompositeSerializationTest.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/main/java/org/apache/polygene/test/serialization/AbstractValueCompositeSerializationTest.java
b/core/testsupport/src/main/java/org/apache/polygene/test/serialization/AbstractValueCompositeSerializationTest.java
index 6c10776..dced530 100644
--- a/core/testsupport/src/main/java/org/apache/polygene/test/serialization/AbstractValueCompositeSerializationTest.java
+++ b/core/testsupport/src/main/java/org/apache/polygene/test/serialization/AbstractValueCompositeSerializationTest.java
@@ -71,9 +71,6 @@ import static org.junit.Assert.assertThat;
  */
 // TODO Assert Arrays behaviour!
 // TODO Assert Generics behaviour!
-// TODO Assert deserialization using a value super type that has less properties/associations
(e.g. ignore spurious state)
-// TODO Assert deserialization from state missing optional values (e.g. do not fail on missing
optional values)
-// TODO Assert deserialization from state with different properties/associations order (e.g.
do not fail on unordered values)
 public abstract class AbstractValueCompositeSerializationTest
     extends AbstractPolygeneTest
 {
@@ -99,7 +96,8 @@ public abstract class AbstractValueCompositeSerializationTest
     public void assemble( ModuleAssembly module )
         throws AssemblyException
     {
-        module.values( Some.class, AnotherValue.class, FooValue.class, CustomFooValue.class,
+        module.values( Some.class, SomeExtended.class, SomeShuffled.class,
+                       AnotherValue.class, FooValue.class, CustomFooValue.class,
                        SpecificCollection.class /*, SpecificValue.class, GenericValue.class
*/ );
 
         new EntityTestAssembler().visibleIn( Visibility.layer ).assemble( module.layer().module(
"persistence" ) );
@@ -147,6 +145,61 @@ public abstract class AbstractValueCompositeSerializationTest
     }
 
     @Test
+    public void canDeserializeUsingSuperTypeWithLessState()
+    {
+        try( UnitOfWork uow = unitOfWorkFactory.newUnitOfWork() )
+        {
+            SomeExtended someExtended = buildSomeExtendedValue( moduleInstance, uow, "42"
);
+
+            String serialized = serialization.serialize( someExtended );
+            System.out.println( serialized );
+
+            Some deserialized = serialization.deserialize( module, Some.class, serialized
);
+            System.out.println( deserialized );
+        }
+    }
+
+    @Test
+    public void canDeserializeUsingChildTypeWithSupplementaryOptionalState()
+    {
+        try( UnitOfWork uow = unitOfWorkFactory.newUnitOfWork() )
+        {
+            Some some = buildSomeValue( moduleInstance, uow, "42" );
+
+            String serialized = serialization.serialize( some );
+            System.out.println( serialized );
+
+            SomeExtended deserialized = serialization.deserialize( module, SomeExtended.class,
serialized );
+            System.out.println( deserialized );
+        }
+    }
+
+    /**
+     * State model order depend on declaration order, this test ensures that moving a state
method up/down into a type
+     * does not break deserialization.
+     */
+    @Test
+    public void canDeserializeFromShuffledState()
+    {
+        try( UnitOfWork uow = unitOfWorkFactory.newUnitOfWork() )
+        {
+            SomeExtended someExtended = buildSomeExtendedValue( moduleInstance, uow, "42"
);
+
+            String serialized = serialization.serialize( someExtended );
+            System.out.println( serialized );
+
+            SomeShuffled deserialized = serialization.deserialize( module, SomeShuffled.class,
serialized );
+            System.out.println( deserialized );
+
+            serialized = serialization.serialize( deserialized );
+            System.out.println( serialized );
+
+            serialization.deserialize( module, SomeExtended.class, serialized );
+            System.out.println( deserialized );
+        }
+    }
+
+    @Test
     @Ignore( "JSONEntityState cannot handle polymorphic deserialization" )
     // TODO Entity == Identity + Value
     // JSONEntityState does not allow for polymorphic serialization
@@ -208,6 +261,19 @@ public abstract class AbstractValueCompositeSerializationTest
         return builder.newInstance();
     }
 
+    protected static SomeExtended buildSomeExtendedValue( Module module, UnitOfWork uow,
String identity )
+    {
+        ValueBuilder<SomeExtended> builder = module.newValueBuilder( SomeExtended.class
);
+        SomeExtended proto = builder.prototype();
+        proto.identity().set( StringIdentity.fromString( identity ) );
+        setSomeValueState( module, uow, proto );
+        proto.extraProperty().set( "extra property" );
+        proto.extraAssociation().set( buildBarEntity( uow, "extra association" ) );
+        proto.extraManyAssociation().add( buildBarEntity( uow, "extra many association" )
);
+        proto.extraNamedAssociation().put( "extra", buildBarEntity( uow, "extra named association"
) );
+        return builder.newInstance();
+    }
+
     /**
      * @return a Some EntityComposite whose state is populated with test data.
      */
@@ -407,6 +473,42 @@ public abstract class AbstractValueCompositeSerializationTest
         NamedAssociation<BarEntity> barEntityNamedAssociation();
     }
 
+    interface SomeExtended extends Some
+    {
+        @Optional
+        Property<String> extraProperty();
+
+        @Optional
+        Association<Bar> extraAssociation();
+
+        ManyAssociation<Bar> extraManyAssociation();
+
+        NamedAssociation<Bar> extraNamedAssociation();
+    }
+
+    interface SomeShuffled extends SomeExtended
+    {
+        NamedAssociation<Bar> extraNamedAssociation();
+
+        @Override
+        NamedAssociation<Bar> barNamedAssociation();
+
+        ManyAssociation<Bar> extraManyAssociation();
+
+        @Override
+        ManyAssociation<Bar> barManyAssociation();
+
+        Association<Bar> extraAssociation();
+
+        @Override
+        Association<Bar> barAssociation();
+
+        Property<String> extraProperty();
+
+        @Override
+        Property<String> string();
+    }
+
     public interface SpecificCollection
         extends GenericCollection<String>
     {

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/1d8f6fb6/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
----------------------------------------------------------------------
diff --git a/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
b/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
index caba249..56f32e6 100644
--- a/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
+++ b/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
@@ -94,7 +94,7 @@ public interface MessagePackDeserializer extends Deserializer
         {
             try
             {
-                if( value.isNilValue() )
+                if( value == null || value.isNilValue() )
                 {
                     return null;
                 }


Mime
View raw message