wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ngalla...@apache.org
Subject svn commit: r1021933 [6/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/ApacheJSONArrayTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONArrayTest.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONArrayTest.java (added)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONArrayTest.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,752 @@
+/*
+ * 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 JSONArray model
+ */
+public class ApacheJSONArrayTest extends TestCase {
+
+    /**
+     * Test the noargs contructor.
+     */
+    public void test_new() {
+        System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+        JSONFactory factory = JSONFactory.newInstance();
+        JSONArray jArray = factory.createJSONArray();
+        assertTrue(jArray != null);
+        assertTrue(jArray.length() == 0);
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_newFromEmptyObjectString() {
+        JSONArray jArray = null;
+        Exception ex = null;
+        // Load from empty object string.
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jArray = factory.createJSONArray("[]");
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+        assertTrue(jArray != null);
+        assertTrue(jArray.length() == 0);
+    }
+
+    /**
+     * Test the String non-empty object contructor.
+     */
+    public void test_newFromString() {
+        JSONArray jArray = null;
+        Exception ex = null;
+        // Load a basic JSON string
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jArray = factory.createJSONArray("[\"foo\", \"bar\", \"bool\", true]");
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+        assertTrue(jArray != null);
+        assertTrue(jArray.length() == 4);
+    }
+
+    /**
+     * Test the construction from a reader.
+     */
+    public void test_newFromReader() {
+        JSONArray jArray = 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");
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jArray = factory.createJSONArray(rdr);
+            rdr.close();
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(jArray != null);
+        assertTrue(jArray.length() == 7);
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the String non-empty object contructor parse failure.
+     */
+    public void test_newFromStringFailure() {
+        JSONArray jArray = null;
+        Exception ex = null;
+        // Load a basic JSON string that's corrupt 
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jArray = factory.createJSONArray("[\"foo\", bar}, \"bool\", 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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            jArray.put(factory.createJSONObject());
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray();
+            jArray.put(factory.createJSONArray());
+            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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[-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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[-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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[-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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[-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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[-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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[\"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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[\"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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[null]");
+            assertTrue(jArray.get(0) == null);
+        } 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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[\"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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[\"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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[\"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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[\"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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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 {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONArray jArray = factory.createJSONArray("[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);
+    }
+}

Added: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONObjectTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONObjectTest.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONObjectTest.java (added)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONObjectTest.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,1002 @@
+/*
+ * 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 JSONObject model
+ */
+public class ApacheJSONObjectTest extends TestCase {
+
+    /**
+     * Test the noargs contructor.
+     */
+    public void test_new() {
+        System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+        JSONFactory factory = JSONFactory.newInstance();
+        JSONObject jObject = factory.createJSONObject();
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 0);
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_newFromEmptyObjectString() {
+        JSONObject jObject = null;
+        Exception ex = null;
+        // Load from empty object string.
+        try{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jObject = factory.createJSONObject("{}");
+        }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() {
+        JSONObject jObject = null;
+        Exception ex = null;
+        // Load a basic JSON string
+        try{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jObject = factory.createJSONObject("{\"foo\":\"bar\", \"bool\": true}");
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 2);
+    }
+
+    /**
+     * Test the construction from a reader.
+     */
+    public void test_newFromReader() {
+        JSONObject jObject = null;
+        Exception ex = null;
+        // read in a basic JSON file that has all the various types in it.
+        try{
+            Reader rdr = new InputStreamReader(new FileInputStream("jsonfiles/utf8_basic.json"), "UTF-8");
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jObject = factory.createJSONObject(rdr);
+            rdr.close();
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 12);
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the construction from a simple map.
+     */
+    public void test_newFromMap() {
+        JSONObject jObject = null;
+        Exception ex = null;
+        HashMap map = new HashMap();
+        map.put("string", "This is a string");
+        map.put("null", null);
+        map.put("integer", new Integer(1));
+        map.put("bool", new Boolean(true));
+
+        // Load a JSON object from a map with JSONable values.
+        try{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jObject = factory.createJSONObject(map);
+        }catch(Exception ex1){
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(jObject != null);
+        assertTrue(jObject.length() == 4);
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction
+     */
+    public void test_compact() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"foo\":\"bar\", \"bool\": true}");
+            assertTrue(jObject.getBoolean("bool") == true);
+            System.out.println("JSON compacted text (jObject):\n");
+            System.out.println(jObject.toString());
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction
+     */
+    public void test_parseFailure() {
+        Exception ex = null;
+
+        try {
+            // Verify a malformed JSON string (no closure), fails parse.
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"foo\":\"bar\", \"bool\": true");
+            assertTrue(jObject.getBoolean("bool") == true);
+            System.out.println("JSON compacted text (jObject):\n");
+            System.out.println(jObject.toString());
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction
+     */
+    /*  Allowing this for now.
+    public void test_noQuotesParseFailure() {
+        Exception ex = null;
+
+        try {
+            // Verify a malformed JSON string (no quotes on attributes), fails parse.
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{foo:\"bar\", bool: true}");
+            assertTrue(jObject.getBoolean("bool") == true);
+            System.out.println("JSON compacted text (jObject):\n");
+            System.out.println(jObject.toString());
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    } */
+
+    /**
+     * Test a basic JSON Object construction, with verbose output
+     */
+    public void test_verbose() {
+        Exception ex = null;
+
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"foo\":\"bar\", \"bool\": true}");
+            assertTrue(jObject.getBoolean("bool") == true);
+            System.out.println("JSON indented 3 space (jObject):\n");
+            System.out.println(jObject.toString(3));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test the key check function.
+     */
+    public void test_has() {
+        Exception ex = null;
+
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"foo\":\"bar\", \"bool\": false, \"null\": null}");
+            assertTrue(jObject.has("foo"));
+            assertTrue(jObject.has("bool"));
+            assertTrue(jObject.has("null"));
+            assertTrue(!jObject.has("noKey"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putLong() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("long", (long)1);
+            Long l = (Long)jObject.get("long");
+            assertTrue(l != null);
+            assertTrue(l instanceof java.lang.Long);
+            assertTrue(jObject.getLong("long") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putInt() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("int", 1);
+            Integer i = (Integer)jObject.get("int");
+            assertTrue(i != null);
+            assertTrue(i instanceof java.lang.Integer);
+            assertTrue(jObject.getInt("int") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putShort() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("short", (short)1);
+            Short s = (Short)jObject.get("short");
+            assertTrue(s != null);
+            assertTrue(s instanceof java.lang.Short);
+            assertTrue(jObject.getShort("short") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putDouble() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("double", (double)1.123);
+            Double d = (Double)jObject.get("double");
+            assertTrue(d != null);
+            assertTrue(d instanceof java.lang.Double);
+            assertTrue(jObject.getDouble("double") == 1.123);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putBoolean() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("bool", true);
+            Boolean b = (Boolean)jObject.get("bool");
+            assertTrue(b != null);
+            assertTrue(b instanceof java.lang.Boolean);
+            assertTrue(jObject.getBoolean("bool") == true);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putString() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("string", "Hello World.");
+            String s = (String)jObject.get("string");
+            assertTrue(s != null);
+            assertTrue(s instanceof java.lang.String);
+            assertTrue(jObject.getString("string").equals("Hello World."));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putJSONObject() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("object", factory.createJSONObject());
+            JSONObject obj = (JSONObject)jObject.get("object");
+            assertTrue(obj != null);
+            assertTrue(obj instanceof JSONObject);
+            assertTrue(((JSONObject)jObject.get("object")).toString().equals("{}"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'put' function
+     */
+    public void test_putJSONArray() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("array", factory.createJSONArray());
+            JSONArray obj = (JSONArray)jObject.get("array");
+            assertTrue(obj != null);
+            assertTrue(obj instanceof JSONArray);
+            assertTrue(((JSONArray)jObject.get("array")).toString().equals("[]"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test append function to convert an existing value to an array.
+     */
+    public void test_append() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("string", "Hello World.");
+            String s = (String)jObject.get("string");
+            assertTrue(s != null);
+            assertTrue(s instanceof java.lang.String);
+            jObject.append("string", "Another string.");
+            JSONArray array = (JSONArray)jObject.get("string");
+            assertTrue(array != null);
+            assertTrue(array instanceof JSONArray);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test append function to convert an existing value (though null) to an array.
+     */
+    public void test_appendtoNull() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            jObject.put("null", (Object)null);
+            String s = (String)jObject.get("null");
+            assertTrue(s == null);
+            assertTrue(jObject.has("null"));
+            jObject.append("null", "Another string.");
+            JSONArray array = (JSONArray)jObject.get("null");
+
+            System.out.println("Array size: " + array.length());
+            for (int i = 0; i < array.length(); i++) {
+                System.out.println("Val at location: [" + i + "] is: " + array.get(i));
+            }
+
+            assertTrue(array != null);
+            assertTrue(array instanceof JSONArray);
+            assertTrue(array.length() == 2);
+            assertTrue(array.get(0) == null);
+            assertTrue(array.get(1).equals("Another string."));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test append function to convert an existing value to an array.
+     */
+    public void test_appendArray() {
+        Exception ex = null;
+        try {
+            System.out.println("In: test_appendArray");
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject();
+            JSONArray array = factory.createJSONArray();
+            array.put("Hello World.");
+            jObject.put("array", array);
+            JSONArray array1 = (JSONArray)jObject.get("array");
+            assertTrue(array1 != null);
+            assertTrue(array1 instanceof JSONArray);
+            assertTrue(array1.length() == 1);
+            jObject.append("array", "Another string.");
+            JSONArray array2 = (JSONArray)jObject.get("array");
+            assertTrue(array2 != null);
+            assertTrue(array2 instanceof JSONArray);
+            assertTrue(array2.length() == 2);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getLong() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"long\":1}");
+            assertTrue(jObject.getLong("long") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getLongNgative() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"long\":-1}");
+            assertTrue(jObject.getLong("long") == -1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getInt() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"int\":1}");
+            assertTrue(jObject.getInt("int") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getIntNegative() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"int\":-1}");
+            assertTrue(jObject.getInt("int") == -1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getDouble() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":1}");
+            assertTrue(jObject.getDouble("double") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getDoubleNegative() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":-1}");
+            assertTrue(jObject.getDouble("double") == -1);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getDoubleWithDecimal() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":100.959}");
+            assertTrue(jObject.getDouble("double") == 100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getDoubleNegativeWithDecimal() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":-100.959}");
+            assertTrue(jObject.getDouble("double") == -100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getDoubleWithExponential() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":100959e-3}");
+            assertTrue(jObject.getDouble("double") == 100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getDoubleNegativeWithExponential() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":-100959e-3}");
+            assertTrue(jObject.getDouble("double") == -100.959);
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getString() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"string\":\"some string\"}");
+            assertTrue(jObject.getString("string").equals("some string"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getBoolean() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"bool\":true}");
+            assertTrue(jObject.getBoolean("bool"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function
+     */
+    public void test_getBoolean_StringValue() {
+        Exception ex = null;
+
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"bool\":\"true\"}");
+            assertTrue(jObject.getBoolean("bool"));
+        } 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 Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getLong_typeMisMatch() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"long\":\"1\"}");
+            assertTrue(jObject.getLong("long") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getDouble_typeMisMatch() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":\"1\"}");
+            assertTrue(jObject.getDouble("double") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getInt_typeMisMatch() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"int\":\"1\"}");
+            assertTrue(jObject.getLong("int") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getString_typeMisMatch() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"string\":null}");
+            assertTrue(jObject.getString("string") == "1");
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getBoolean_typeMisMatch() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"bool\":1}");
+            assertTrue(jObject.getBoolean("bool") == true);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getLong_typeMisMatchNull() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"long\":null}");
+            assertTrue(jObject.getLong("long") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getInt_typeMisMatchNull() {
+        Exception ex = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"int\":null}");
+            assertTrue(jObject.getLong("int") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getDouble_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"double\":null}");
+            assertTrue(jObject.getDouble("double") == 1);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getString_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"string\":null}");
+            assertTrue(jObject.getString("string") == "1");
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test a basic JSON Object construction and helper 'get' function failure due to type mismatch
+     */
+    public void test_getBoolean_typeMisMatchNull() {
+        Exception ex = null;
+
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"bool\":null}");
+            assertTrue(jObject.getBoolean("bool") == true);
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex instanceof JSONException);
+    }
+
+    /**
+     * Test the iterator of the keys.
+     */
+    public void test_keys() {
+        Exception ex = null;
+        HashMap map = new HashMap();
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject("{\"foo\": \"bar\", \"number\": 1, \"bool\":null}");
+            Iterator keys = jObject.keys();
+            while (keys.hasNext()) {
+                String key = (String)keys.next();
+                map.put(key, key);
+            }
+        } catch (Exception ex1) {
+            ex = ex1;
+        }
+        assertTrue(ex  == null);
+        assertTrue(map.size() == 3);
+        assertTrue(map.get("foo") != null);
+        assertTrue(map.get("number") != null);
+        assertTrue(map.get("bool") != null);
+    }
+
+    /**
+     * Test the iterator of the sorted keys.  
+     */
+    public void test_sortedKeys() {
+        HashMap map = new HashMap();
+        JSONObject jObject = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jObject = factory.createJSONObject("{\"foo\": \"bar\", \"number\": 1, \"bool\":null}");
+        } catch (Exception ex) {
+            assertTrue(false);
+        }
+        Iterator keys = jObject.sortedKeys();
+        String[] sKeys = new String[] {"bool", "foo", "number"};
+        int i = 0;
+        while (keys.hasNext()) {
+            String key = (String)keys.next();
+            String sKey = sKeys[i];
+            i++;
+            assertTrue(key.equals(sKey));
+        }
+    }
+
+    /**
+     * Test the toString of JSONObject.
+     * Use the value to construct a new object and verify contents match.
+     */
+    public void test_toString() {
+        HashMap map = new HashMap();
+        JSONObject jObject = null;
+        JSONObject jObject2 = null;
+        try {
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            jObject = factory.createJSONObject("{\"foo\": \"bar\", \"number\": 1, \"bool\":true}");
+            jObject2 = factory.createJSONObject(jObject.toString());
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            assertTrue(false);
+        }
+        try{
+            assertTrue(jObject != jObject2);
+            assertTrue(jObject.length() == jObject2.length());
+            assertTrue(jObject.getString("foo").equals(jObject2.getString("foo")));
+            assertTrue(jObject.getBoolean("bool") == jObject2.getBoolean("bool"));
+            assertTrue(jObject.getInt("number") == jObject2.getInt("number"));
+        }catch(JSONException jex){
+            jex.printStackTrace();
+            assertTrue(false);
+        }
+    }
+
+    /***********************************************************/
+    /* The following tests checks UTF-8 encoded DBCS characters*/
+    /***********************************************************/
+
+    /**
+     * Verify a standard UTF-8 file with high character codes (Korean), can be read via a reader and parsed.
+     */
+    public void test_utf8_korean() {
+        Exception ex = null;
+        try {
+            Reader reader = new InputStreamReader(new FileInputStream(new File("jsonfiles/utf8_helloworld_ko.json")), "UTF-8");
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject(reader);
+            reader.close();
+            assertTrue(jObject.getString("greet").equals("\uc548\ub155 \uc138\uc0c1\uc544"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+    /**
+     * Verify a UTF 8 file with character codes in the lower part will parse and 
+     * serialize correctly in escaped unicode format (which is valid JSON and easier
+     * to debug)
+     */
+    public void test_utf8_lowerchar() {
+        Exception ex = null;
+        try {
+            Reader reader = new InputStreamReader(new FileInputStream(new File("jsonfiles/utf8_lowerchar.json")), "UTF-8");
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONObject jObject = factory.createJSONObject(reader);
+            reader.close();
+            assertTrue(jObject.getString("message").equals("\u00c5\u00c5\u00c5\u00c5"));
+            assertTrue(jObject.toString().equals("{\"message\":\"\\u00c5\\u00c5\\u00c5\\u00c5\"}"));
+        } catch (Exception ex1) {
+            ex = ex1;
+            ex.printStackTrace();
+        }
+        assertTrue(ex == null);
+    }
+
+}

Added: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONStringerTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONStringerTest.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONStringerTest.java (added)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/compat/tests/ApacheJSONStringerTest.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,225 @@
+/*
+ * 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 JSONStringer
+ * Since this clase is very similar to writer, only a few basic
+ * tests are done.  Generally, f the writer works, so will
+ * the stringer.
+ */
+public class ApacheJSONStringerTest extends TestCase {
+
+    /**
+     * Test the contructor.
+     */
+    public void test_new() {
+        System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+        JSONFactory factory = JSONFactory.newInstance();
+        JSONStringer jStringer = factory.createJSONStringer();
+    }
+
+    /**
+     * Test the String empty object contructor.
+     */
+    public void test_WriteEmptyObject() {
+        Exception ex = null;
+        try{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONStringer jStringer = factory.createJSONStringer();
+            jStringer.object();
+            jStringer.endObject();
+            String str = jStringer.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{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONStringer jStringer = factory.createJSONStringer();
+            jStringer.object();
+            jStringer.close();
+            String str = jStringer.toString();
+            System.out.println("STRING: " + str);
+            // 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{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONStringer jStringer = factory.createJSONStringer();
+            jStringer.array();
+            jStringer.endArray();
+            String str = jStringer.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 multiple keys of varying types
+     */
+    public void test_WriteObjectComplex() {
+        Exception ex = null;
+        try{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONStringer jStringer = factory.createJSONStringer();
+            jStringer.object();
+            jStringer.key("string");
+            jStringer.value("String1");
+            jStringer.key("bool");
+            jStringer.value(false);
+            jStringer.key("number");
+            jStringer.value(1);
+
+            // Place an object
+            jStringer.key("object");
+            jStringer.object();
+            jStringer.key("string");
+            jStringer.value("String2");
+            jStringer.endObject();
+
+            // Place an array
+            jStringer.key("array");
+            jStringer.array();
+            jStringer.value(1);
+            jStringer.value((double)2);
+            jStringer.value((short)3);
+            jStringer.endArray();
+
+            //Close top object.
+            jStringer.endObject();
+
+            String str = jStringer.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{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONStringer jStringer = factory.createJSONStringer();
+            jStringer.array();
+            jStringer.value("String1");
+            jStringer.value(false);
+            jStringer.value(1);
+
+            // Place an object
+            jStringer.object();
+            jStringer.key("string");
+            jStringer.value("String2");
+            jStringer.endObject();
+
+            // Place an array
+            jStringer.array();
+            jStringer.value(1);
+            jStringer.value((double)2);
+            jStringer.value((short)3);
+            jStringer.endArray();
+
+            //Close top array.
+            jStringer.endArray();
+
+            String str = jStringer.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);
+    }
+
+
+    /**
+     * Test that trying operations after the stringer was returned that they fail.
+     */
+    public void test_OptsAfterCloseFail() {
+        Exception ex = null;
+        try{
+            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.common.model.json.impl.ApacheJSONFactory");
+            JSONFactory factory = JSONFactory.newInstance();
+            JSONStringer jStringer = factory.createJSONStringer();
+            jStringer.array();
+            jStringer.toString();
+
+            // This should die.
+            jStringer.endArray();
+        }catch(Exception ex1){
+            ex = ex1;
+        }
+        assertTrue(ex instanceof IllegalStateException);
+    }
+
+}



Mime
View raw message