sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1612584 - in /sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json: DespacedResult.java JSONArrayToStringTest.java JSONObjectToStringTest.java JSONWriterTest.java
Date Tue, 22 Jul 2014 15:00:22 GMT
Author: bdelacretaz
Date: Tue Jul 22 15:00:22 2014
New Revision: 1612584

URL: http://svn.apache.org/r1612584
Log:
SLING-3786 - add tests for the conversion of JSON data to strings

Added:
    sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/DespacedResult.java
    sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONArrayToStringTest.java
    sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectToStringTest.java
    sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java

Added: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/DespacedResult.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/DespacedResult.java?rev=1612584&view=auto
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/DespacedResult.java
(added)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/DespacedResult.java
Tue Jul 22 15:00:22 2014
@@ -0,0 +1,82 @@
+/*
+ * 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.sling.commons.json;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/** Test the String formatting functionality of JSONObject */
+public class DespacedResult {
+    private final String despaced;
+    
+    /** Simplify whitespace in str and replace quotes
+     *  to make it easier to compare the output 
+     *  @param spaceReplacement replaces spaces after the first one
+     *  */ 
+    DespacedResult(String str, String spaceReplacement) throws JSONException {
+        
+        boolean previousWasSpace = false;
+        
+        // Verify that str parses
+        new JSONObject(str);
+        
+        // And convert whitespace and quotes to
+        // make comparisons easier
+        final StringBuilder sb = new StringBuilder();
+        for(int i=0; i < str.length(); i++) {
+            final Character c = str.charAt(i);
+            final boolean isWhitespace = Character.isWhitespace(c); 
+            if(c == '\n') {
+                sb.append("-nl-");
+            } else if(isWhitespace) {
+                if(previousWasSpace && spaceReplacement != null) {
+                    sb.append(spaceReplacement);
+                }
+            } else if(c == '\"') {
+                sb.append("_");
+            } else if(c == '\"') {
+                sb.append("-dq-");
+            } else {
+                sb.append(c);
+            }
+            previousWasSpace = isWhitespace;
+        }
+        despaced = sb.toString();
+    }
+    
+    DespacedResult(String str) throws JSONException {
+        this(str, null);
+    }
+    
+
+    DespacedResult expect(String ...expected) {
+        for(String e : expected) {
+            assertTrue("Expecting " + e + " to be contained in " + despaced, despaced.contains(e));
+        }
+        return this;
+    }
+    
+    DespacedResult assertExactMatch(String expected) {
+        assertEquals("Expecting an exact match with " + expected + " at " + despaced, expected,
despaced);
+        return this;
+    }
+    
+    DespacedResult expectLength(int n) {
+        assertEquals("Expecting a String length of " + n + " for " + despaced, n, despaced.length());
+        return this;
+    }
+};
\ No newline at end of file

Added: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONArrayToStringTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONArrayToStringTest.java?rev=1612584&view=auto
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONArrayToStringTest.java
(added)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONArrayToStringTest.java
Tue Jul 22 15:00:22 2014
@@ -0,0 +1,40 @@
+/*
+ * 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.sling.commons.json;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/** Test the String formatting functionality of JSONObject */
+public class JSONArrayToStringTest {
+    @Test
+    public void testJsonArraySingle() throws JSONException {
+        final JSONArray a = new JSONArray();
+        a.put("foo");
+        assertEquals("[\"foo\"]", a.toString());
+    }
+    
+    @Test
+    public void testJsonArrayTwo() throws JSONException {
+        final JSONArray a = new JSONArray();
+        a.put("foo").put(42);
+        final DespacedResult r = new DespacedResult("{array:" + a.toString(2) + "}");
+        r.expect("[-nl-_foo_,-nl-42-nl-]");
+    }
+    
+}

Added: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectToStringTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectToStringTest.java?rev=1612584&view=auto
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectToStringTest.java
(added)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONObjectToStringTest.java
Tue Jul 22 15:00:22 2014
@@ -0,0 +1,242 @@
+/*
+ * 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.sling.commons.json;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.math.BigInteger;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/** Test the String formatting functionality of JSONObject */
+public class JSONObjectToStringTest {
+    private JSONObject j;
+    
+    static class FailingWriter extends Writer {
+        public void write(char[] cbuf, int off, int len) throws IOException {
+            throw new IOException("write");
+        }
+
+        public void flush() throws IOException {
+            throw new IOException("flush");
+        }
+
+        public void close() throws IOException {
+            throw new IOException("close");
+        }
+    }
+    
+    @Before
+    public void setup() throws JSONException {
+        j = new JSONObject();
+        j.put("string", "this string");
+        j.put("int", 12);
+        j.put("long", 42L);
+        j.put("boolean", true);
+        j.put("float", 12.34f);
+        j.put("double", 45.67d);
+        
+        final JSONString js = new JSONString() {
+            public String toJSONString() {
+                return "json.string here";
+            }
+            
+        };
+        j.put("JSONString", js);
+        
+        for(int i=0; i<2; i++) {
+            final JSONObject k = new JSONObject();
+            final String name = "k" + i;
+            k.put("name", name);
+            k.put("this is", name);
+            j.put(name,  k);
+        }
+        
+        final JSONArray a = new JSONArray();
+        a.put(true).put("hello").put(52.0).put(new BigInteger("212"));
+        j.put("array", a);
+    }
+
+    @Test
+    public void testToString() throws JSONException {
+        final DespacedResult r = new DespacedResult(j.toString());
+        r.expect(
+                "_long_:42", 
+                "_string_:_thisstring_",
+                "_int_:12",
+                "_k0_:{",
+                "_name_:_k0_",
+                "_thisis_:_k0_",
+                "_k1_:{",
+                "_name_:_k1_",
+                "_thisis_:_k1_",
+                "_boolean_:true",
+                "_JSONString_:json.stringhere",
+                "_array_:[true,_hello_,52,212]",
+                "_double_:45.67",
+                "_float_:12.34"
+                );
+    }
+    
+    @Test
+    public void testToStringWithIndents() throws JSONException {
+        final DespacedResult r = new DespacedResult(j.toString(2));
+        r.expect(
+                "_long_:42,-nl-", 
+                "_string_:_thisstring_,-nl-",
+                "_int_:12,-nl-",
+                "_k0_:{",
+                "_name_:_k0_",
+                "_thisis_:_k0_",
+                "_k1_:{",
+                "_name_:_k1_",
+                "_thisis_:_k1_",
+                "_boolean_:true",
+                "_JSONString_:json.stringhere",
+                "_array_:[-nl-true",
+                "_double_:45.67",
+                "_float_:12.34"
+                );
+    }
+    
+    @Test
+    public void testToStringWithInitialIndent() throws JSONException {
+        final DespacedResult r = new DespacedResult(j.toString(2, 3), "S_");
+        r.expect(
+                "{-nl-S_S_S_S_S_",
+                "_long_:42,-nl-", 
+                "_string_:_thisstring_,-nl-",
+                "_int_:12,-nl-",
+                "_k0_:{",
+                "_name_:_k0_",
+                "_thisis_:_k0_",
+                "_k1_:{",
+                "_name_:_k1_",
+                "_thisis_:_k1_",
+                "_array_:[-nl-S_S_S_S_S_S_S_true"
+                );
+    }
+    
+    @Test
+    public void testToStringEmpty() throws JSONException {
+        final DespacedResult r = new DespacedResult(new JSONObject().toString(2));
+        r.assertExactMatch("{}");
+    }
+    
+    @Test
+    public void testToStringSingle() throws JSONException {
+        j = new JSONObject();
+        j.put("foo", "bar");
+        final DespacedResult r = new DespacedResult(j.toString(2));
+        r.assertExactMatch("{_foo_:_bar_}");
+    }
+    
+    @Test
+    public void testJSONStringException() throws JSONException {
+        final JSONString js = new JSONString() {
+            @Override
+            public String toString() {
+                return toJSONString();
+            }
+            public String toJSONString() {
+                throw new UnsupportedOperationException("toJSONString");
+            }
+            
+        };
+        j.put("should.fail", js);
+        
+        // if any exception, toString returns null. 
+        // Not sure why but that's what it is  
+        assertNull("Expecting null output for toString()", j.toString());
+        
+        // but toString(s) rethrows...
+        try {
+            assertNull("Expecting null output for toString(2)", j.toString(2));
+            fail("Expected UnsupportedOperationException");
+        } catch(UnsupportedOperationException uso) {
+            // as expected
+        }
+    }
+    
+    @Test
+    public void testQuote() {
+        final String inp = "<tag/></thing>\\\\0\"A/B\bT\tN\nF\fT\r\u0082";
+        final String out = "\"<tag/><\\/thing>\\\\\\\\0\\\"A/B\\bT\\tN\\nF\\fT\\r\\u0082\"";
+        assertEquals(out, JSONObject.quote(inp));
+    }
+    
+    @Test
+    public void testQuoteNull() {
+        assertEquals("\"\"", JSONObject.quote(null));
+        assertEquals("\"\"", JSONObject.quote(""));
+    }
+    
+    @Test
+    public void testToJsonArray() throws JSONException {
+        final DespacedResult r = new DespacedResult("{array:" + j.toJSONArray(j.names()).toString()
+ "}");
+        r.expect("_thisstring_","12","42","true","json.stringhere");
+    }
+    
+    @Test
+    public void testWriteJsonArray() throws JSONException {
+        final JSONArray a = j.toJSONArray(j.names());
+        final StringWriter w = new StringWriter();
+        a.write(w);
+        final DespacedResult r = new DespacedResult("{array:" + w.toString() + "}");
+        r.expect("_thisstring_","12","42","true","json.stringhere");
+    }
+    
+    @Test(expected=JSONException.class)
+    public void testWriteJsonArrayException() throws JSONException {
+        final JSONArray a = j.toJSONArray(j.names());
+        a.write(new FailingWriter());
+    }
+    
+    @Test
+    public void testWrite() throws JSONException {
+        final StringWriter w = new StringWriter();
+        j.write(w);
+        final DespacedResult r = new DespacedResult(w.toString());
+        r.expect(
+                "_long_:42", 
+                "_string_:_thisstring_",
+                "_int_:12",
+                "_k0_:{",
+                "_name_:_k0_",
+                "_thisis_:_k0_",
+                "_k1_:{",
+                "_name_:_k1_",
+                "_thisis_:_k1_",
+                "_boolean_:true",
+                "_JSONString_:json.stringhere",
+                "_array_:[true,_hello_,52,212]",
+                "_double_:45.67",
+                "_float_:12.34"
+                );
+    }
+    
+    @Test(expected=JSONException.class)
+    public void testWriteException() throws JSONException {
+        j.write(new FailingWriter());
+    }
+}
\ No newline at end of file

Added: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java?rev=1612584&view=auto
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java
(added)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/JSONWriterTest.java
Tue Jul 22 15:00:22 2014
@@ -0,0 +1,97 @@
+/*
+ * 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.sling.commons.json;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.StringWriter;
+
+import org.apache.sling.commons.json.io.JSONWriter;
+import org.junit.Before;
+import org.junit.Test;
+
+/** Test the String formatting functionality of JSONObject */
+public class JSONWriterTest {
+    private JSONWriter w;
+    private StringWriter output;
+    
+    @Before
+    public void setup() {
+        output = new StringWriter();
+        w = new JSONWriter(output);
+    }
+    
+    private DespacedResult write() throws JSONException {
+        w.object();
+        w.key("foo").value("bar");
+        w.key("array");
+        w.array().value(1).value("two").value(3.0).value(false).endArray();
+        w.key("last").value("one");
+        w.endObject();
+        return new DespacedResult(output.toString());
+    }
+    
+    @Test
+    public void testSetTidy() {
+        assertFalse(w.isTidy());
+        w.setTidy(true);
+        assertTrue(w.isTidy());
+    }
+    
+    @Test
+    public void testStandardWrite() throws JSONException {
+        final DespacedResult r = write();
+        r.expect(
+                "_foo_:_bar_", 
+                "_array_:[1,_two_,3,false]");
+    }
+    
+    @Test
+    public void testTidyWrite() throws JSONException {
+        w.setTidy(true);
+        final DespacedResult r = write();
+        r.expect(
+                "-nl-_foo_:_bar_", 
+                "-nl-_array_:[-nl-1,-nl-_two_,-nl-3,-nl-false-nl-]");
+    }
+    
+    @Test
+    public void testEmpty() throws JSONException {
+        assertTrue(output.toString().length() == 0);
+    }
+    
+    @Test(expected=JSONException.class)
+    public void testMisplacedArray() throws JSONException {
+        w.object().array();
+    }
+    
+    @Test(expected=JSONException.class)
+    public void testMisplacedKey() throws JSONException {
+        w.array().key("foo");
+    }
+    
+    @Test(expected=JSONException.class)
+    public void testMisplacedEndObjectA() throws JSONException {
+        w.object().endObject().endObject();
+    }
+    
+    @Test(expected=JSONException.class)
+    public void testMisplacedEndObjectB() throws JSONException {
+        w.endObject();
+    }
+}
\ No newline at end of file



Mime
View raw message