wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ngalla...@apache.org
Subject svn commit: r1021933 [7/9] - in /incubator/wink/trunk: ./ wink-json4j/ wink-json4j/src/ wink-json4j/src/main/ wink-json4j/src/main/java/ wink-json4j/src/main/java/org/ wink-json4j/src/main/java/org/apache/ wink-json4j/src/main/java/org/apache/wink/ win...
Date Tue, 12 Oct 2010 21:30:32 GMT
Added: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONWriterTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONWriterTest.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONWriterTest.java (added)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONWriterTest.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,633 @@
+/*
+ * 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.wink.json4j.compat.tests;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import java.io.*;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashMap;
+
+import org.apache.wink.json4j.compat.*;
+
+/**
+ * Tests for the basic Java JSONWriter
+ */
+public class ApacheJSONWriterTest extends TestCase {
+
+    /**
+     * Test the contructor.
+     */
+    public void test_new() {
+        StringWriter w = new StringWriter();
+        System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+        JSONFactory factory = JSONFactory.newInstance();
+        JSONWriter jWriter = factory.createJSONWriter(w);
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_WriteEmptyObject() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.endObject();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_WriteEmptyObjectClose() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.close();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_WriteEmptyArray() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.array();
+            jWriter.endArray();
+            String str = w.toString();
+            // Verify it parses.
+            JSONArray test = factory.createJSONArray(str);
+            assertTrue(str.equals("[]"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_WriteEmptyArrayClose() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.array();
+            jWriter.close();
+
+            String str = w.toString();
+            // Verify it parses.
+            JSONArray test = factory.createJSONArray(str);
+            assertTrue(str.equals("[]"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of string
+     */
+    public void test_WriteObjectAttrString() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value("bar");
+            jWriter.close();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":\"bar\"}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of int
+     */
+    public void test_WriteObjectAttrInt() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value(1);
+            jWriter.close();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":1}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of long
+     */
+    public void test_WriteObjectAttrLong() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value((long)1);
+            jWriter.close();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":1}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of short
+     */
+    public void test_WriteObjectAttrShort() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value((short)1);
+            jWriter.close();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":1}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of Double
+     */
+    public void test_WriteObjectAttrDouble() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value((double)100.959);
+            jWriter.close();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":100.959}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of boolean
+     */
+    public void test_WriteObjectAttrBoolean() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value(true);
+            jWriter.close();
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":true}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of Object
+     */
+    public void test_WriteObjectAttrObject() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value(true);
+            jWriter.endObject();
+            jWriter.endObject();
+            jWriter.close();
+            String str = w.toString();
+            
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":{\"foo\":true}}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of Object
+     */
+    public void test_WriteObjectAttrArray() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.array();
+            jWriter.value(true);
+            jWriter.endArray();
+            jWriter.endObject();
+            jWriter.close();
+            String str = w.toString();
+            
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":[true]}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of Object
+     */
+    public void test_WriteObjectAttrJSONObject() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+
+            // Verify we can put a JSONObject into the stream!
+            JSONObject jObj = factory.createJSONObject();
+
+            jObj.put("foo", true);
+            jWriter.value(jObj);
+
+            jWriter.endObject();
+            jWriter.close();
+
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":{\"foo\":true}}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with a key + value of array
+     */
+    public void test_WriteObjectAttrJSONArray() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+
+            // Verify we can put a JSONObject into the stream!
+            JSONArray jArray = factory.createJSONArray();
+            jArray.put(true);
+            jWriter.value(jArray);
+
+            jWriter.endObject();
+            jWriter.close();
+
+            String str = w.toString();
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"foo\":[true]}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+
+    /**
+     * Test a simple object with multiple keys of varying types
+     */
+    public void test_WriteObjectComplex() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("string");
+            jWriter.value("String1");
+            jWriter.key("bool");
+            jWriter.value(false);
+            jWriter.key("number");
+            jWriter.value(1);
+
+            // Place an object
+            jWriter.key("object");
+            jWriter.object();
+            jWriter.key("string");
+            jWriter.value("String2");
+            jWriter.endObject();
+
+            // Place an array
+            jWriter.key("array");
+            jWriter.array();
+            jWriter.value(1);
+            jWriter.value((double)2);
+            jWriter.value((short)3);
+            jWriter.endArray();
+
+            //Close top object.
+            jWriter.endObject();
+
+            jWriter.close();
+
+            String str = w.toString();
+
+            // Verify it parses.
+            JSONObject test = factory.createJSONObject(str);
+            assertTrue(str.equals("{\"string\":\"String1\",\"bool\":false,\"number\":1,\"object\":{\"string\":\"String2\"},\"array\":[1,2.0,3]}"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a simple object with multiple keys of varying types
+     */
+    public void test_WriteArrayComplex() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.array();
+            jWriter.value("String1");
+            jWriter.value(false);
+            jWriter.value(1);
+
+            // Place an object
+            jWriter.object();
+            jWriter.key("string");
+            jWriter.value("String2");
+            jWriter.endObject();
+
+            // Place an array
+            jWriter.array();
+            jWriter.value(1);
+            jWriter.value((double)2);
+            jWriter.value((short)3);
+            jWriter.endArray();
+
+            //Close top array.
+            jWriter.endArray();
+
+            jWriter.close();
+            String str = w.toString();
+
+            // Verify it parses.
+            JSONArray test = factory.createJSONArray(str);
+            assertTrue(str.equals("[\"String1\",false,1,{\"string\":\"String2\"},[1,2.0,3]]"));
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /*******************************/
+    /*All the error condition tests*/
+    /*******************************/
+
+    /**
+     * Test that setting a value in an object without defining its key fails.
+     */
+    public void test_ObjectNoKeyValueFail() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.value(true);
+            jWriter.endObject();
+        }catch(Exception ex1){
+            ex = ex1;
+        }
+        assertTrue(ex instanceof IllegalStateException);
+    }
+
+
+    /**
+     * Test that setting a value without a key (after another key/value was set), fails
+     */
+    public void test_ObjectKeyValueNoKeyValueFail() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.object();
+            jWriter.key("foo");
+            jWriter.value(true);
+            
+            // This should die with IllegalStateException...
+            jWriter.value(false);
+            jWriter.endObject();
+        }catch(Exception ex1){
+            ex = ex1;
+        }
+        assertTrue(ex instanceof IllegalStateException);
+    }
+
+    /**
+     * Test that setting a key while not in an object fails
+     */
+    public void test_NoObjectKeyFail() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            
+            // This should die.
+            jWriter.key("foo");
+        }catch(Exception ex1){
+            ex = ex1;
+        }
+        assertTrue(ex instanceof IllegalStateException);
+    }
+
+    /**
+     * Test that setting a value while not in an array or object fails
+     */
+    public void test_NoObjectValueFail() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            
+            // This should die.
+            jWriter.value("foo");
+        }catch(Exception ex1){
+            ex = ex1;
+        }
+        assertTrue(ex instanceof IllegalStateException);
+    }
+
+    /**
+     * Test that trying to set keys while in an array fails.
+     */
+    public void test_ArrayKeyFail() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.array();
+
+            // This should die.
+            jWriter.key("foo");
+        }catch(Exception ex1){
+            ex = ex1;
+        }
+        assertTrue(ex instanceof IllegalStateException);
+    }
+
+    /**
+     * Test that trying operations after the writer has been closed fails.
+     */
+    public void test_OptsAfterCloseFail() {
+        Exception ex = null;
+        try{
+            StringWriter w = new StringWriter();
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONWriter jWriter = factory.createJSONWriter(w);
+            jWriter.array();
+            jWriter.close();
+
+            // This should die.
+            jWriter.endArray();
+        }catch(Exception ex1){
+            ex = ex1;
+        }
+        assertTrue(ex instanceof IllegalStateException);
+    }
+}

Added: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/BeanSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/BeanSerializerTest.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/BeanSerializerTest.java (added)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/BeanSerializerTest.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,88 @@
+/*
+ * 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.wink.json4j.tests;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import java.io.*;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashMap;
+import java.util.Date;
+
+import org.apache.wink.json4j.*;
+import org.apache.wink.json4j.internal.*;
+
+/**
+ * Tests for the basic Java Bean serializer
+ */
+public class BeanSerializerTest extends TestCase {
+
+    /**
+     * Test the noargs contructor.
+     */
+    public void test_Date_WithSuper() {
+        Exception ex = null;
+        try{
+            Date date = new Date();
+            JSONArtifact ja = BeanSerializer.toJson(date, true);
+            assertTrue(((JSONObject)ja).get("class").equals("java.util.Date"));
+            System.out.println(ja.write(3));
+        } catch (Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the noargs contructor.
+     */
+    public void test_Date_WithOutSuper() {
+        Exception ex = null;
+        try{
+            Date date = new Date();
+            JSONArtifact ja = BeanSerializer.toJson(date, false);
+            assertTrue(((JSONObject)ja).opt("class") == null);
+            System.out.println(ja.write(3));
+        } catch (Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    public void test_Date_RebuildWithOutSuper() {
+        Exception ex = null;
+        try{
+            Date date = new Date();
+            JSONArtifact ja = BeanSerializer.toJson(date, false);
+            assertTrue(((JSONObject)ja).opt("class") == null);
+            System.out.println(ja.write(3));
+            Date date2 = (Date)BeanSerializer.fromJson((JSONObject)ja);
+            System.out.println("Date: " + date2.toString());
+        } catch (Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+}

Added: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONArrayTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONArrayTest.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONArrayTest.java (added)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONArrayTest.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,785 @@
+/*
+ * 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.wink.json4j.tests;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import java.io.*;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashMap;
+import java.util.Date;
+
+import org.apache.wink.json4j.*;
+import org.apache.wink.json4j.utils.*;
+
+/**
+ * Tests for the basic Java JSONArray model
+ */
+public class JSONArrayTest extends TestCase {
+
+    /**
+     * Test the noargs contructor.
+     */
+    public void test_new() {
+        JSONArray jObject = new JSONArray();
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 0);
+    }
+
+    /**
+     * Test the creation of a JSONArray from a string array
+     */
+    public void test_newFromStringArray() {
+        Exception ex = null;
+        try {
+            String[] strArray = new String[] {"hello", "world", null, "after null"};
+            
+            JSONArray jArray = new JSONArray(strArray);
+            assertTrue(jArray.length() == 4);
+            assertTrue(jArray.getString(0).equals("hello"));
+            assertTrue(jArray.optString(2) == null);
+            assertTrue(jArray.getString(3).equals("after null"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_newFromEmptyObjectString() {
+        JSONArray jObject = null;
+        Exception ex = null;
+        // Load from empty object string.
+        try {
+            jObject = new JSONArray("[]");
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 0);
+    }
+
+    /**
+     * Test the String non-empty object contructor.
+     */
+    public void test_newFromString() {
+        JSONArray jObject = null;
+        Exception ex = null;
+        // Load a basic JSON string
+        try {
+            jObject = new JSONArray("[\"foo\", \"bar\", \"bool\", true]");
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 4);
+    }
+
+    /**
+     * Test the construction from a reader.
+     */
+    public void test_newFromReader() {
+        JSONArray jObject = null;
+        Exception ex = null;
+        // read in a basic JSON file of a toplevel array that has all the various types in it.
+        try {
+            Reader rdr = new InputStreamReader(new FileInputStream("jsonfiles/utf8_basic_array.json"), "UTF-8");
+            jObject = new JSONArray(rdr);
+            rdr.close();
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 7);
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the construction from a stream.
+     */
+    public void test_newFromStream() {
+        JSONArray jObject = null;
+        Exception ex = null;
+        // read in a basic JSON file of a toplevel array that has all the various types in it.
+        // Inputstreams are read as UTF-8 by the underlying parser.
+        try {
+            InputStream is = new FileInputStream("jsonfiles/utf8_basic_array.json");
+            jObject = new JSONArray(is);
+            is.close();
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 7);
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the String non-empty object contructor parse failure.
+     */
+    public void test_newFromStringFailure() {
+        JSONArray jObject = null;
+        Exception ex = null;
+        
+        // Load a basic JSON string that's not valid in strict (unquoted string)
+        try {
+            jObject = new JSONArray("[\"foo\", bar, \"bool\", true]", true);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex != null);
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putLong() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put((long)1);
+            Long l = (Long)jArray.get(0);
+            assertTrue(l != null);
+            assertTrue(l instanceof java.lang.Long);
+            assertTrue(jArray.getLong(0) == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putInt() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put(1);
+            Integer i = (Integer)jArray.get(0);
+            assertTrue(i != null);
+            assertTrue(i instanceof java.lang.Integer);
+            assertTrue(jArray.getInt(0) == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putShort() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put((short)1);
+            Short s = (Short)jArray.get(0);
+            assertTrue(s != null);
+            assertTrue(s instanceof java.lang.Short);
+            assertTrue(jArray.getShort(0) == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putDouble() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put((double)1.123);
+            Double d = (Double)jArray.get(0);
+            assertTrue(d != null);
+            assertTrue(d instanceof java.lang.Double);
+            assertTrue(jArray.getDouble(0) == 1.123);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putBoolean() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put(true);
+            Boolean b = (Boolean)jArray.get(0);
+            assertTrue(b != null);
+            assertTrue(b instanceof java.lang.Boolean);
+            assertTrue(jArray.getBoolean(0) == true);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putString() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put("Hello World.");
+            String s = (String)jArray.get(0);
+            assertTrue(s != null);
+            assertTrue(s instanceof java.lang.String);
+            assertTrue(jArray.getString(0).equals("Hello World."));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putNull() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put((Object)null);
+            String s = (String)jArray.get(0);
+            assertTrue(s == null);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putJSONObject() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put(new JSONObject());
+            JSONObject obj = (JSONObject)jArray.get(0);
+            assertTrue(obj != null);
+            assertTrue(obj instanceof JSONObject);
+            assertTrue(((JSONObject)jArray.get(0)).toString().equals("{}"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putJSONArray() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            jArray.put(new JSONArray());
+            JSONArray obj = (JSONArray)jArray.get(0);
+            assertTrue(obj != null);
+            assertTrue(obj instanceof JSONArray);
+            assertTrue(((JSONArray)jArray.get(0)).toString().equals("[]"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getLong() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[1]");
+            assertTrue(jArray.getLong(0) == (long)1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getLongNgative() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[-1]");
+            assertTrue(jArray.getLong(0) == (long)-1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getInt() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[1]");
+            assertTrue(jArray.getInt(0) == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getIntNegative() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[-1]");
+            assertTrue(jArray.getInt(0) == -1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getDouble() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[1]");
+            assertTrue(jArray.getDouble(0) == (double)1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getDoubleNegative() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[-1]");
+            assertTrue(jArray.getDouble(0) == (double)-1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getDoubleWithDecimal() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[100.959]");
+            assertTrue(jArray.getDouble(0) == (double)100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getDoubleNegativeWithDecimal() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[-100.959]");
+            assertTrue(jArray.getDouble(0) == (double)-100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getDoubleWithExponential() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[100959e-3]");
+            assertTrue(jArray.getDouble(0) == (double)100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getDoubleNegativeWithExponential() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[-100959e-3]");
+            assertTrue(jArray.getDouble(0) == (double)-100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getString() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[\"some string\"]");
+            assertTrue(jArray.getString(0).equals("some string"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getBoolean() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[true]");
+            assertTrue(jArray.getBoolean(0));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getBoolean_StringValue() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[\"true\"]");
+            assertTrue(jArray.getBoolean(0));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function
+     */
+    public void test_getNull() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[null]");
+            assertTrue(jArray.get(0) == null);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /***********************************************************************************/
+    /* The following tests array expansion when using indexes > than the current array */
+    /***********************************************************************************/
+
+    /**
+     * Test a basic JSON Array construction and helper 'put' function
+     */
+    public void test_putIntPosition() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray();
+            // Put the int at the noted position (1)
+            jArray.put(5, 1);
+            System.out.println(jArray.toString());
+
+            assertTrue(jArray.size() == 6);
+            Integer i = (Integer)jArray.get(5);
+            assertTrue(i != null);
+            assertTrue(i instanceof java.lang.Integer);
+            assertTrue(jArray.getInt(5) == 1);
+
+            // Verify that the 0 position is a null (expanded)
+            i = (Integer)jArray.get(3);
+            assertTrue(i == null);
+            System.out.println(jArray.toString());
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+     
+
+    /**************************************************************************/
+    /* The following tests all test failure scenarios due to type mismatching.*/  
+    /**************************************************************************/
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getLong_typeMisMatch() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[\"1\"]");
+            assertTrue(jArray.getLong(0) == (long)1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getDouble_typeMisMatch() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[\"1\"]");
+            assertTrue(jArray.getDouble(0) == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getInt_typeMisMatch() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[\"1\"]");
+            assertTrue(jArray.getLong(0) == (int)1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getString_typeMisMatch() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[null]");
+            assertTrue(jArray.getString(0) == "null");
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getBoolean_typeMisMatch() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[1]");
+            assertTrue(jArray.getBoolean(0) == true);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        System.out.println("Error: " + ex);
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getLong_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[null]");
+            assertTrue(jArray.getLong(0) == (long)1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getInt_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[null]");
+            assertTrue(jArray.getLong(0) == (int)1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getDouble_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[null]");
+            assertTrue(jArray.getDouble(0) == (double)1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getString_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[null]");
+            assertTrue(jArray.getString(0) == "1");
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Array construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getBoolean_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            JSONArray jArray = new JSONArray("[null]");
+            assertTrue(jArray.getBoolean(0) == true);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a 'join' of a JSONArray
+     */
+    public void test_JoinNoDelimiter() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[1, true, false, null, \"My String\", [1,2,3], {\"foo\":\"bar\"}]");
+            String joined = jArray.join("");
+            assertTrue(joined.equals("1truefalsenullMy String[1,2,3]{\"foo\":\"bar\"}"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a 'join' of a JSONArray
+     */
+    public void test_JoinDelimiter() {
+        Exception ex = null;
+        try {
+            JSONArray jArray = new JSONArray("[1, true, false, null, \"My String\", [1,2,3], {\"foo\":\"bar\"}]");
+            String joined = jArray.join("|");
+            assertTrue(joined.equals("1|true|false|null|My String|[1,2,3]|{\"foo\":\"bar\"}"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /*****************************************************************/
+    /* The following tests checks basic 'java beans' convert to JSON */
+    /*****************************************************************/
+
+    /**
+     * Test that a new Java Date serializes 'bean style' when encountered.
+     */
+    public void test_Date() {
+        Exception ex = null;
+        try{
+            Date date = new Date();
+            JSONArray ja = new JSONArray();
+            ja.put(date);
+            JSONObject jsonDate = ja.getJSONObject(0);
+            assertTrue(jsonDate instanceof JSONObject);
+            assertTrue(jsonDate.get("class").equals("java.util.Date"));
+            System.out.println(ja.write(3));
+        } catch (Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+}



Mime
View raw message