felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1738759 - in /felix/trunk/converter/src: main/java/org/apache/felix/converter/impl/AdapterImpl.java test/java/org/apache/felix/converter/impl/JsonCodecTest.java
Date Tue, 12 Apr 2016 10:56:59 GMT
Author: davidb
Date: Tue Apr 12 10:56:59 2016
New Revision: 1738759

URL: http://svn.apache.org/viewvc?rev=1738759&view=rev
Log:
Felix Converter Service: add test for JSON Codec with Adapter

Modified:
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java?rev=1738759&r1=1738758&r2=1738759&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java (original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/AdapterImpl.java Tue
Apr 12 10:56:59 2016
@@ -69,6 +69,16 @@ public class AdapterImpl implements Adap
 
     @Override
     public <F, T> Adapter rule(Function<F, T> toFun, Function<T, F> fromFun)
{
+//        Type[] t = toFun.getClass().getGenericInterfaces();
+//
+//        TypeVariable<?>[] tp = toFun.getClass().getTypeParameters();
+//        System.out.println("*** " + Arrays.toString(tp));
+//
+//        TypeReference<Map<String, Adapter>> tr = new TypeReference<Map<String,Adapter>>(){};
+//        System.out.println("### " + tr);
+//        Type type = tr.getType();
+//        System.out.println("### " + type);
+
         // TODO Auto-generated method stub
         return this;
     }

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java?rev=1738759&r1=1738758&r2=1738759&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java
Tue Apr 12 10:56:59 2016
@@ -19,13 +19,30 @@ package org.apache.felix.converter.impl;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
+import org.osgi.service.converter.Adapter;
+import org.osgi.service.converter.Converter;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 public class JsonCodecTest {
+    private Converter converter;
+
+    @Before
+    public void setUp() {
+        converter = new ConverterImpl();
+    }
+
+    @After
+    public void tearDown() {
+        converter = null;
+    }
+
     @Test
     public void testJSONCodec() throws Exception {
         Map<Object, Object> m1 = new HashMap<>();
@@ -53,4 +70,41 @@ public class JsonCodecTest {
         assertEquals(m.get("ab"), m2.get("ab"));
         assertEquals(m.get(true), m2.get("true"));
     }
+
+    @Test
+    public void testCodecWithAdapter() throws JSONException {
+        Map<String, Foo> m1 = new HashMap<>();
+        m1.put("f", new Foo("fofofo"));
+        Map<String, Object> m = new HashMap<>();
+        m.put("submap", m1);
+
+        Adapter ca = converter.getAdapter();
+        ca.rule(Foo.class, String.class, Foo::tsFun, v -> Foo.fsFun(v));
+
+        JsonCodecImpl jsonCodec = new JsonCodecImpl();
+        String json = jsonCodec.with(ca).encode(m).toString();
+
+        JSONObject jo = new JSONObject(json);
+        assertEquals(1, jo.length());
+        JSONObject jo1 = jo.getJSONObject("submap");
+        assertEquals("<fofofo>", jo1.getString("f"));
+
+        // TODO convert back into a Map<String, Foo> via TypeReference
+    }
+
+    static class Foo {
+        private final String val;
+
+        public Foo(String s) {
+            val = s;
+        }
+
+        public String tsFun() {
+            return "<" + val + ">";
+        }
+
+        public static Foo fsFun(String s) {
+            return new Foo(s.substring(1, s.length() - 1));
+        }
+    }
 }



Mime
View raw message