felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1748383 - in /felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json: JSONCodecTest.java JSONSerializationTest.java JsonCodecTest.java JsonSerializationTest.java
Date Tue, 14 Jun 2016 10:42:50 GMT
Author: davidb
Date: Tue Jun 14 10:42:50 2016
New Revision: 1748383

URL: http://svn.apache.org/viewvc?rev=1748383&view=rev
Log:
Felix Converter Service - rename some tests

Added:
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
Modified:
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONCodecTest.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONSerializationTest.java

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONCodecTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONCodecTest.java?rev=1748383&r1=1748382&r2=1748383&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONCodecTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONCodecTest.java
Tue Jun 14 10:42:50 2016
@@ -32,7 +32,7 @@ import org.osgi.service.converter.Conver
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
-public class JSONCodecTest {
+public class JsonCodecTest {
     private Converter converter;
 
     @Before

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONSerializationTest.java?rev=1748383&r1=1748382&r2=1748383&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONSerializationTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JSONSerializationTest.java
Tue Jun 14 10:42:50 2016
@@ -26,7 +26,7 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
-public class JSONSerializationTest {
+public class JsonSerializationTest {
     @Test
     public void testComplexMapSerialization() {
         Map<String, Object> m = new HashMap<>();

Added: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java?rev=1748383&view=auto
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
(added)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
Tue Jun 14 10:42:50 2016
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.felix.converter.impl.json;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.converter.impl.ConverterImpl;
+import org.apache.felix.converter.impl.json.JsonCodecImpl;
+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<>();
+        m1.put("x", true);
+        m1.put("y", null);
+        Map<Object, Object> m = new HashMap<>();
+        m.put(1, 11L);
+        m.put("ab", "cd");
+        m.put(true, m1);
+
+        JsonCodecImpl jsonCodec = new JsonCodecImpl();
+        String json = jsonCodec.encode(m).toString();
+
+        JSONObject jo = new JSONObject(json);
+        assertEquals(11, jo.getInt("1"));
+        assertEquals("cd", jo.getString("ab"));
+        JSONObject jo2 = jo.getJSONObject("true");
+        assertEquals(true, jo2.getBoolean("x"));
+        assertTrue(jo2.isNull("y"));
+
+        @SuppressWarnings("rawtypes")
+        Map m2 = jsonCodec.decode(Map.class).from(json);
+        // m2 is not exactly equal to m, as the keys are all strings now, this is unavoidable
with JSON
+        assertEquals(m.size(), m2.size());
+        assertEquals(m.get(1), m2.get("1"));
+        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));
+        }
+    }
+}

Added: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java?rev=1748383&view=auto
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
(added)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
Tue Jun 14 10:42:50 2016
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.felix.converter.impl.json;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.converter.impl.json.JsonCodecImpl;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class JsonSerializationTest {
+    @Test
+    public void testComplexMapSerialization() {
+        Map<String, Object> m = new HashMap<>();
+        m.put("sKey", "a string");
+        m.put("iKey", 42);
+        m.put("bKey",  true);
+        m.put("noKey", null);
+        m.put("simpleArray", new int[] {1,2,3});
+
+        Map<String, Object> m1 = new HashMap<>();
+        m1.put("a", 1L);
+        m1.put("b", "hello");
+        m.put("simpleObject", m1);
+
+        String expected = "{\"bKey\":true,"
+                + "\"simpleArray\":[1,2,3],"
+                + "\"iKey\":42,"
+                + "\"sKey\":\"a string\","
+                + "\"simpleObject\":{\"a\":1,\"b\":\"hello\"},"
+                + "\"noKey\":null}";
+        assertEquals(expected, new JsonCodecImpl().encode(m).toString());
+    }
+
+    @Test
+    public void testComplexMapSerialization2() {
+        Map<String, Object> m2 = new HashMap<>();
+        m2.put("yes", Boolean.TRUE);
+        m2.put("no", Collections.singletonMap("maybe", false));
+
+        Map<String, Object> cm = new HashMap<>();
+        cm.put("list", Arrays.asList(
+                Collections.singletonMap("x", "y"),
+                Collections.singletonMap("x", "b")));
+        cm.put("embedded", m2);
+
+        String expected = "{\"list\":[{\"x\":\"y\"},{\"x\":\"b\"}],"
+                + "\"embedded\":"
+                + "{\"no\":{\"maybe\":false},\"yes\":true}}";
+        assertEquals(expected, new JsonCodecImpl().encode(cm).toString());
+    }
+}



Mime
View raw message