Author: davidb
Date: Mon Apr 11 16:18:06 2016
New Revision: 1738598

URL: http://svn.apache.org/viewvc?rev=1738598&view=rev
Log:
Additional tests for the Felix Converter.

Added:
    
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java
Modified:
    felix/trunk/converter/pom.xml
    
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonDecodingImpl.java

Modified: felix/trunk/converter/pom.xml
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/pom.xml?rev=1738598&r1=1738597&r2=1738598&view=diff
==============================================================================
--- felix/trunk/converter/pom.xml (original)
+++ felix/trunk/converter/pom.xml Mon Apr 11 16:18:06 2016
@@ -90,5 +90,12 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.16</version>
+            <scope>test</scope>            
+        </dependency>
     </dependencies>
 </project>

Modified: 
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonDecodingImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonDecodingImpl.java?rev=1738598&r1=1738597&r2=1738598&view=diff
==============================================================================
--- 
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonDecodingImpl.java
 (original)
+++ 
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonDecodingImpl.java
 Mon Apr 11 16:18:06 2016
@@ -88,12 +88,12 @@ public class JsonDecodingImpl<T> impleme
                     parsed = true;
                 else if ("false".equals(val))
                     parsed = false;
-                else if (val.startsWith("\""))
-                    parsed = val;
+                else if (val.startsWith("\"") && val.endsWith("\""))
+                    parsed = val.substring(1, val.length() - 1);
                 else if (val.contains("."))
                     parsed = Double.valueOf(val);
                 else
-                    parsed = Integer.valueOf(val);
+                    parsed = Long.valueOf(val);
             }
             m.put(key, parsed);
         } while (commaIdx > 0);

Added: 
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=1738598&view=auto
==============================================================================
--- 
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java
 (added)
+++ 
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java
 Mon Apr 11 16:18:06 2016
@@ -0,0 +1,56 @@
+/*
+ * 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;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.commons.json.JSONObject;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class JsonCodecTest {
+    @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"));
+
+        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"));
+    }
+}


Reply via email to