felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dlean...@apache.org
Subject svn commit: r1803763 - /felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonBackingObjectSerializationTest.java
Date Wed, 02 Aug 2017 05:53:57 GMT
Author: dleangen
Date: Wed Aug  2 05:53:56 2017
New Revision: 1803763

URL: http://svn.apache.org/viewvc?rev=1803763&view=rev
Log:
Added another test case containing possible (temporary) solution FELIX-5666

Modified:
    felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonBackingObjectSerializationTest.java

Modified: felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonBackingObjectSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonBackingObjectSerializationTest.java?rev=1803763&r1=1803762&r2=1803763&view=diff
==============================================================================
--- felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonBackingObjectSerializationTest.java
(original)
+++ felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonBackingObjectSerializationTest.java
Wed Aug  2 05:53:56 2017
@@ -16,11 +16,15 @@
  */
 package org.apache.felix.serializer.impl.json;
 
+import java.lang.reflect.Type;
 import java.util.Map;
 
 import org.junit.Ignore;
 import org.junit.Test;
+import org.osgi.dto.DTO;
+import org.osgi.util.converter.ConverterFunction;
 import org.osgi.util.converter.Converters;
+import org.osgi.util.converter.TargetRule;
 
 import static org.junit.Assert.assertEquals;
 
@@ -38,8 +42,8 @@ public class JsonBackingObjectSerializat
 
         final String expected = 
                 "{\"a\":\"A\","
-                + "\"o\":XXX"
-                + "\"b\":\"B\"}";
+                        + "\"o\":{\"a\":\"AA\",\"b\":\"BB\"},"
+                        + "\"b\":\"B\"}";
 
         final String actual = new JsonSerializerImpl().serialize(m).toString();
 
@@ -51,7 +55,7 @@ public class JsonBackingObjectSerializat
     public void testComplexMapSerializationWithoutUsingPreConversion() {
         final String expected = 
                 "{\"a\":\"A\","
-                + "\"o\":XXX"
+                + "\"o\":{\"a\":\"AA\",\"b\":\"BB\"},"
                 + "\"b\":\"B\"}";
 
         final String actual = new JsonSerializerImpl()
@@ -63,7 +67,23 @@ public class JsonBackingObjectSerializat
         assertEquals(expected, actual);
     }
 
-    public static class MyDTOishObject {
+    @Test
+    public void testComplexMapSerializationUsingRule() {
+        final String expected = 
+                "{\"o\":{\"a\":\"AA\",\"b\":\"BB\"},"
+                        + "\"a\":\"A\","
+                        + "\"b\":\"B\"}";
+
+        final String actual = new JsonSerializerImpl()
+                .serialize(MyDTOishObject.factory( "A", "B" ))
+                .with(Converters.newConverterBuilder().rule(new MapTargetRule()).build())
+                .toString();
+
+        // Cannot get result to behave predictably... Order is random.
+        assertEquals(expected.length(), actual.length());
+    }
+
+    public static class MyDTOishObject extends DTO {
         public String a;
         public String b;
         public OtherObject o;
@@ -79,7 +99,7 @@ public class JsonBackingObjectSerializat
         }
     }
 
-    public static class OtherObject {
+    public static class OtherObject extends DTO {
         public String a;
         public String b;
 
@@ -92,4 +112,29 @@ public class JsonBackingObjectSerializat
             return new OtherObject( a, b );
         }
     }
+
+    static class MapTargetRule implements TargetRule {
+
+        @Override
+        public ConverterFunction getFunction() {
+            return new MapConverterFunction();
+        }
+
+        @Override
+        public Type getTargetType() {
+            return Map.class;
+        }        
+    }
+
+    static class MapConverterFunction implements ConverterFunction {
+
+        @Override
+        public Object apply( Object obj, Type targetType ) throws Exception {
+            return Converters
+                    .standardConverter()
+                    .convert(obj)
+                    .sourceAsDTO()
+                    .to(targetType);
+        }
+    }    
 }



Mime
View raw message