2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import junit.framework.TestCase;
22 * This black box test was written without inspecting the non-free org.json sourcecode.
24 public class JSONStringerTest extends TestCase {
26 public void testEmptyStringer() {
27 // why isn't this the empty string?
28 assertNull(new JSONStringer().toString());
31 public void testValueJSONNull() throws JSONException {
32 JSONStringer stringer = new JSONStringer();
34 stringer.value(JSONObject.NULL);
36 assertEquals("[null]", stringer.toString());
39 public void testEmptyObject() throws JSONException {
40 JSONStringer stringer = new JSONStringer();
43 assertEquals("{}", stringer.toString());
46 public void testEmptyArray() throws JSONException {
47 JSONStringer stringer = new JSONStringer();
50 assertEquals("[]", stringer.toString());
53 public void testArray() throws JSONException {
54 JSONStringer stringer = new JSONStringer();
56 stringer.value(false);
59 stringer.value("five");
62 assertEquals("[false,5,5,\"five\",null]", stringer.toString());
65 public void testValueObjectMethods() throws JSONException {
66 JSONStringer stringer = new JSONStringer();
68 stringer.value(Boolean.FALSE);
69 stringer.value(Double.valueOf(5.0));
70 stringer.value(Long.valueOf(5L));
72 assertEquals("[false,5,5]", stringer.toString());
75 public void testKeyValue() throws JSONException {
76 JSONStringer stringer = new JSONStringer();
78 stringer.key("a").value(false);
79 stringer.key("b").value(5.0);
80 stringer.key("c").value(5L);
81 stringer.key("d").value("five");
82 stringer.key("e").value(null);
84 assertEquals("{\"a\":false," +
88 "\"e\":null}", stringer.toString());
92 * Test what happens when extreme values are emitted. Such values are likely
93 * to be rounded during parsing.
95 public void testNumericRepresentations() throws JSONException {
96 JSONStringer stringer = new JSONStringer();
98 stringer.value(Long.MAX_VALUE);
99 stringer.value(Double.MIN_VALUE);
101 assertEquals("[9223372036854775807,4.9E-324]", stringer.toString());
104 public void testWeirdNumbers() throws JSONException {
106 new JSONStringer().array().value(Double.NaN);
108 } catch (JSONException e) {
111 new JSONStringer().array().value(Double.NEGATIVE_INFINITY);
113 } catch (JSONException e) {
116 new JSONStringer().array().value(Double.POSITIVE_INFINITY);
118 } catch (JSONException e) {
121 JSONStringer stringer = new JSONStringer();
123 stringer.value(-0.0d);
124 stringer.value(0.0d);
126 assertEquals("[-0,0]", stringer.toString());
129 public void testMismatchedScopes() {
131 new JSONStringer().key("a");
133 } catch (JSONException e) {
136 new JSONStringer().value("a");
138 } catch (JSONException e) {
141 new JSONStringer().endObject();
143 } catch (JSONException e) {
146 new JSONStringer().endArray();
148 } catch (JSONException e) {
151 new JSONStringer().array().endObject();
153 } catch (JSONException e) {
156 new JSONStringer().object().endArray();
158 } catch (JSONException e) {
161 new JSONStringer().object().key("a").key("a");
163 } catch (JSONException e) {
166 new JSONStringer().object().value(false);
168 } catch (JSONException e) {
172 public void testNullKey() {
174 new JSONStringer().object().key(null);
176 } catch (JSONException e) {
180 public void testRepeatedKey() throws JSONException {
181 JSONStringer stringer = new JSONStringer();
183 stringer.key("a").value(true);
184 stringer.key("a").value(false);
185 stringer.endObject();
186 // JSONStringer doesn't attempt to detect duplicates
187 assertEquals("{\"a\":true,\"a\":false}", stringer.toString());
190 public void testEmptyKey() throws JSONException {
191 JSONStringer stringer = new JSONStringer();
193 stringer.key("").value(false);
194 stringer.endObject();
195 assertEquals("{\"\":false}", stringer.toString()); // legit behaviour!
198 public void testEscaping() throws JSONException {
199 assertEscapedAllWays("a", "a");
200 assertEscapedAllWays("a\\\"", "a\"");
201 assertEscapedAllWays("\\\"", "\"");
202 assertEscapedAllWays(":", ":");
203 assertEscapedAllWays(",", ",");
204 assertEscapedAllWays("\\b", "\b");
205 assertEscapedAllWays("\\f", "\f");
206 assertEscapedAllWays("\\n", "\n");
207 assertEscapedAllWays("\\r", "\r");
208 assertEscapedAllWays("\\t", "\t");
209 assertEscapedAllWays(" ", " ");
210 assertEscapedAllWays("\\\\", "\\");
211 assertEscapedAllWays("{", "{");
212 assertEscapedAllWays("}", "}");
213 assertEscapedAllWays("[", "[");
214 assertEscapedAllWays("]", "]");
215 assertEscapedAllWays("\\u0000", "\0");
216 assertEscapedAllWays("\\u0019", "\u0019");
217 assertEscapedAllWays(" ", "\u0020");
220 private void assertEscapedAllWays(String escaped, String original) throws JSONException {
221 assertEquals("{\"" + escaped + "\":false}",
222 new JSONStringer().object().key(original).value(false).endObject().toString());
223 assertEquals("{\"a\":\"" + escaped + "\"}",
224 new JSONStringer().object().key("a").value(original).endObject().toString());
225 assertEquals("[\"" + escaped + "\"]",
226 new JSONStringer().array().value(original).endArray().toString());
227 assertEquals("\"" + escaped + "\"", JSONObject.quote(original));
230 public void testJSONArrayAsValue() throws JSONException {
231 JSONArray array = new JSONArray();
233 JSONStringer stringer = new JSONStringer();
235 stringer.value(array);
237 assertEquals("[[false]]", stringer.toString());
240 public void testJSONObjectAsValue() throws JSONException {
241 JSONObject object = new JSONObject();
242 object.put("a", false);
243 JSONStringer stringer = new JSONStringer();
245 stringer.key("b").value(object);
246 stringer.endObject();
247 assertEquals("{\"b\":{\"a\":false}}", stringer.toString());
250 public void testArrayNestingMaxDepthSupports20() throws JSONException {
251 JSONStringer stringer = new JSONStringer();
252 for (int i = 0; i < 20; i++) {
255 for (int i = 0; i < 20; i++) {
258 assertEquals("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]", stringer.toString());
260 stringer = new JSONStringer();
261 for (int i = 0; i < 20; i++) {
266 public void testObjectNestingMaxDepthSupports20() throws JSONException {
267 JSONStringer stringer = new JSONStringer();
268 for (int i = 0; i < 20; i++) {
272 stringer.value(false);
273 for (int i = 0; i < 20; i++) {
274 stringer.endObject();
276 assertEquals("{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":" +
277 "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":false" +
278 "}}}}}}}}}}}}}}}}}}}}", stringer.toString());
280 stringer = new JSONStringer();
281 for (int i = 0; i < 20; i++) {
287 public void testMixedMaxDepthSupports20() throws JSONException {
288 JSONStringer stringer = new JSONStringer();
289 for (int i = 0; i < 20; i+=2) {
294 stringer.value(false);
295 for (int i = 0; i < 20; i+=2) {
296 stringer.endObject();
299 assertEquals("[{\"a\":[{\"a\":[{\"a\":[{\"a\":[{\"a\":" +
300 "[{\"a\":[{\"a\":[{\"a\":[{\"a\":[{\"a\":false" +
301 "}]}]}]}]}]}]}]}]}]}]", stringer.toString());
303 stringer = new JSONStringer();
304 for (int i = 0; i < 20; i+=2) {
311 public void testMaxDepthWithArrayValue() throws JSONException {
312 JSONArray array = new JSONArray();
315 JSONStringer stringer = new JSONStringer();
316 for (int i = 0; i < 20; i++) {
319 stringer.value(array);
320 for (int i = 0; i < 20; i++) {
323 assertEquals("[[[[[[[[[[[[[[[[[[[[[false]]]]]]]]]]]]]]]]]]]]]", stringer.toString());
326 public void testMaxDepthWithObjectValue() throws JSONException {
327 JSONObject object = new JSONObject();
328 object.put("a", false);
329 JSONStringer stringer = new JSONStringer();
330 for (int i = 0; i < 20; i++) {
334 stringer.value(object);
335 for (int i = 0; i < 20; i++) {
336 stringer.endObject();
338 assertEquals("{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":" +
339 "{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":" +
340 "{\"a\":false}}}}}}}}}}}}}}}}}}}}}", stringer.toString());
343 public void testMultipleRoots() throws JSONException {
344 JSONStringer stringer = new JSONStringer();
350 } catch (JSONException e) {