Author: vramdal
Date: Fri Apr 17 16:22:39 2009
New Revision: 766073

URL: http://svn.apache.org/viewvc?rev=766073&view=rev
Log:
SLING-929 Fix an error in JSONObject.append() + testcase

Added:
    
incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
Modified:
    
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONObject.java

Modified: 
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONObject.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONObject.java?rev=766073&r1=766072&r2=766073&view=diff
==============================================================================
--- 
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONObject.java
 (original)
+++ 
incubator/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONObject.java
 Fri Apr 17 16:22:39 2009
@@ -324,10 +324,10 @@
         if (o == null) {
             put(key, new JSONArray().put(value));
         } else if (o instanceof JSONArray) {
+            put(key, new JSONArray().put(o).put(value));
+        } else {
             throw new JSONException("JSONObject[" + key +
                        "] is not a JSONArray.");
-        } else {
-            put(key, new JSONArray().put(o).put(value));
         }
         return this;
     }

Added: 
incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java?rev=766073&view=auto
==============================================================================
--- 
incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
 (added)
+++ 
incubator/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
 Fri Apr 17 16:22:39 2009
@@ -0,0 +1,37 @@
+package org.apache.sling.commons.json;
+
+import junit.framework.TestCase;
+
+/**
+ * @author [email protected]
+ * @since Apr 17, 2009 6:04:00 PM
+ */
+public class JSONObjectTest extends TestCase {
+    private static final String KEY = "key";
+
+    /**
+     * See <a 
href="https://issues.apache.org/jira/browse/SLING-929";>SLING-929</a>
+     */
+    public void testAppend() throws JSONException {
+        JSONObject obj = new JSONObject();
+        obj.append(KEY, "value1");
+        obj.append(KEY, "value2");
+        Object result = obj.get(KEY);
+        assertTrue("Did not create an array", result instanceof JSONArray);
+    }
+
+    /**
+     * See <a 
href="https://issues.apache.org/jira/browse/SLING-929";>SLING-929</a>
+     */
+    public void testFailAppend() throws JSONException {
+        JSONObject obj = new JSONObject();
+        obj.put(KEY, "value1");
+        try {
+            obj.append(KEY, "value2");
+            TestCase.fail("Accepted append() to a non-array property");
+        } catch (JSONException ignore) {
+            // this is expected
+        }
+    }
+
+}


Reply via email to