You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/11/24 02:26:51 UTC

svn commit: r1038421 - in /pivot/trunk/core/test/org/apache/pivot/json/test: BindTest.java JSONSerializerTest.java SampleBean.java TestBean1.java TestBean2.java list.json map.json sample.json

Author: gbrown
Date: Wed Nov 24 01:26:50 2010
New Revision: 1038421

URL: http://svn.apache.org/viewvc?rev=1038421&view=rev
Log:
Update JSONSerializer test to validate typed list deserialization.

Added:
    pivot/trunk/core/test/org/apache/pivot/json/test/TestBean1.java
      - copied, changed from r1035819, pivot/trunk/core/test/org/apache/pivot/json/test/SampleBean.java
    pivot/trunk/core/test/org/apache/pivot/json/test/TestBean2.java
    pivot/trunk/core/test/org/apache/pivot/json/test/list.json
    pivot/trunk/core/test/org/apache/pivot/json/test/map.json
      - copied, changed from r1038102, pivot/trunk/core/test/org/apache/pivot/json/test/sample.json
Removed:
    pivot/trunk/core/test/org/apache/pivot/json/test/SampleBean.java
    pivot/trunk/core/test/org/apache/pivot/json/test/sample.json
Modified:
    pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java
    pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java

Modified: pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java?rev=1038421&r1=1038420&r2=1038421&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java Wed Nov 24 01:26:50 2010
@@ -16,7 +16,7 @@
  */
 package org.apache.pivot.json.test;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
 
 import java.io.IOException;
 import java.io.StringReader;
@@ -24,34 +24,66 @@ import java.io.StringReader;
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.HashMap;
 import org.apache.pivot.collections.List;
+import org.apache.pivot.collections.Map;
 import org.apache.pivot.json.JSON;
 import org.apache.pivot.json.JSONSerializer;
 import org.apache.pivot.serialization.SerializationException;
+import org.apache.pivot.util.TypeLiteral;
 import org.junit.Test;
 
 public class BindTest {
     @Test
-    @SuppressWarnings("unchecked")
-    public void testBind() throws IOException, SerializationException {
-        JSONSerializer objectSerializer = new JSONSerializer();
-        Object sampleObject = objectSerializer.readObject(getClass().getResourceAsStream("sample.json"));
-
-        JSONSerializer beanSerializer = new JSONSerializer(SampleBean.class);
-        SampleBean sampleBean = (SampleBean)beanSerializer.readObject(getClass().getResourceAsStream("sample.json"));
-
-        assertEquals(sampleBean.getA(), JSON.get(sampleObject, "a"));
-        assertEquals(sampleBean.getB(), JSON.get(sampleObject, "b"));
-        assertEquals(sampleBean.getC(), JSON.get(sampleObject, "c"));
-        assertEquals(sampleBean.getD(), JSON.get(sampleObject, "d"));
-        assertEquals(sampleBean.getE(), JSON.get(sampleObject, "e"));
-        assertEquals(sampleBean.getI().getA(), JSON.get(sampleObject, "i.a"));
-
+    public void testUntypedList() throws IOException, SerializationException {
         JSONSerializer listSerializer = new JSONSerializer(ArrayList.class);
         List<?> list = (List<?>)listSerializer.readObject(new StringReader("[1, 2, 3, 4, 5]"));
         assertEquals(list.get(0), 1);
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testTypedList() throws IOException, SerializationException {
+        JSONSerializer listSerializer = new JSONSerializer();
+        List<Object> list =
+            (List<Object>)listSerializer.readObject(getClass().getResourceAsStream("list.json"));
+
+        JSONSerializer typedListSerializer =
+            new JSONSerializer((new TypeLiteral<ArrayList<TestBean2>>() {}).getType());
+        ArrayList<TestBean2> typedList =
+            (ArrayList<TestBean2>)typedListSerializer.readObject(getClass().getResourceAsStream("list.json"));
+
+        Object item0 = typedList.get(0);
+        assertTrue(item0 instanceof TestBean2);
+        assertEquals(typedList.get(0).getA(), JSON.get(list, "[0].a"));
+    }
 
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testUntypedMap() throws IOException, SerializationException {
         JSONSerializer mapSerializer = new JSONSerializer(HashMap.class);
         HashMap<String, ?> map = (HashMap<String, ?>)mapSerializer.readObject(new StringReader("{a:1, b:2, c:3}"));
         assertEquals(map.get("a"), 1);
     }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testTypedMap() throws IOException, SerializationException {
+        JSONSerializer mapSerializer = new JSONSerializer();
+        Map<String, Object> map =
+            (Map<String, Object>)mapSerializer.readObject(getClass().getResourceAsStream("map.json"));
+
+        JSONSerializer typedMapSerializer = new JSONSerializer(TestBean1.class);
+        TestBean1 typedMap =
+            (TestBean1)typedMapSerializer.readObject(getClass().getResourceAsStream("map.json"));
+
+        assertEquals(typedMap.getA(), JSON.get(map, "a"));
+        assertEquals(typedMap.getB(), JSON.get(map, "b"));
+        assertEquals(typedMap.getC(), JSON.get(map, "c"));
+        assertEquals(typedMap.getD(), JSON.get(map, "d"));
+        assertEquals(typedMap.getE(), JSON.get(map, "e"));
+        assertEquals(typedMap.getI().getA(), JSON.get(map, "i.a"));
+
+        Object k0 = typedMap.getK().get(0);
+        assertTrue(k0 instanceof TestBean2);
+        assertEquals(typedMap.getK().get(0).getA(), JSON.get(map, "k[0].a"));
+    }
 }

Modified: pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java?rev=1038421&r1=1038420&r2=1038421&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java Wed Nov 24 01:26:50 2010
@@ -131,11 +131,11 @@ public class JSONSerializerTest {
         };
 
         jsonSerializer.getJSONSerializerListeners().add(jsonSerializerListener);
-        Object o1 = jsonSerializer.readObject(getClass().getResourceAsStream("sample.json"));
-        assertEquals(JSON.get(o1, "count"), 6);
+        Object o1 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
+        assertEquals(JSON.get(o1, "count"), 8);
 
         jsonSerializer.getJSONSerializerListeners().remove(jsonSerializerListener);
-        Object o2 = jsonSerializer.readObject(getClass().getResourceAsStream("sample.json"));
+        Object o2 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
 
         assertTrue(o1.equals(o2));
 

Copied: pivot/trunk/core/test/org/apache/pivot/json/test/TestBean1.java (from r1035819, pivot/trunk/core/test/org/apache/pivot/json/test/SampleBean.java)
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/TestBean1.java?p2=pivot/trunk/core/test/org/apache/pivot/json/test/TestBean1.java&p1=pivot/trunk/core/test/org/apache/pivot/json/test/SampleBean.java&r1=1035819&r2=1038421&rev=1038421&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/SampleBean.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/TestBean1.java Wed Nov 24 01:26:50 2010
@@ -19,13 +19,14 @@ package org.apache.pivot.json.test;
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.HashMap;
 
-public class SampleBean {
+public class TestBean1 {
     private int a = 0;
     private String b = null;
     private boolean c = false;
     private ArrayList<String> d = null;
     private HashMap<String, Integer> e = null;
-    private SampleBean i = null;
+    private TestBean1 i = null;
+    private ArrayList<TestBean2> k = null;
 
     public int getA() {
         return a;
@@ -67,11 +68,19 @@ public class SampleBean {
         this.e = e;
     }
 
-    public SampleBean getI() {
+    public TestBean1 getI() {
         return i;
     }
 
-    public void setI(SampleBean i) {
+    public void setI(TestBean1 i) {
         this.i = i;
     }
+
+    public ArrayList<TestBean2> getK() {
+        return k;
+    }
+
+    public void setK(ArrayList<TestBean2> k) {
+        this.k = k;
+    }
 }

Added: pivot/trunk/core/test/org/apache/pivot/json/test/TestBean2.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/TestBean2.java?rev=1038421&view=auto
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/TestBean2.java (added)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/TestBean2.java Wed Nov 24 01:26:50 2010
@@ -0,0 +1,47 @@
+/*
+ * 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.pivot.json.test;
+
+public class TestBean2 {
+    private int a = 0;
+    private int b = 0;
+    private int c = 0;
+
+    public int getA() {
+        return a;
+    }
+
+    public void setA(int a) {
+        this.a = a;
+    }
+
+    public int getB() {
+        return b;
+    }
+
+    public void setB(int b) {
+        this.b = b;
+    }
+
+    public int getC() {
+        return c;
+    }
+
+    public void setC(int c) {
+        this.c = c;
+    }
+}

Added: pivot/trunk/core/test/org/apache/pivot/json/test/list.json
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/list.json?rev=1038421&view=auto
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/list.json (added)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/list.json Wed Nov 24 01:26:50 2010
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+[   {a:1, b:2, c:3},
+    {a:10, b:20, c:30},
+    {a:100, b:200, c:300}
+]
\ No newline at end of file

Copied: pivot/trunk/core/test/org/apache/pivot/json/test/map.json (from r1038102, pivot/trunk/core/test/org/apache/pivot/json/test/sample.json)
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/map.json?p2=pivot/trunk/core/test/org/apache/pivot/json/test/map.json&p1=pivot/trunk/core/test/org/apache/pivot/json/test/sample.json&r1=1038102&r2=1038421&rev=1038421&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/sample.json (original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/map.json Wed Nov 24 01:26:50 2010
@@ -20,5 +20,10 @@
     d: ["1", "2", "3"],
     e: {f: 4, g: 5, h: 6},
     "i": { a: 200, b: "Goodbye", c: true},
-    j: 200
+    j: 200,
+    k:  [
+        {a:1, b:2, c:3},
+        {a:10, b:20, c:30},
+        {a:100, b:200, c:300}
+    ]
 }