OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / json / src / test / java / org / json / JSONStringerTest.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.json;
18
19 import junit.framework.TestCase;
20
21 /**
22  * This black box test was written without inspecting the non-free org.json sourcecode.
23  */
24 public class JSONStringerTest extends TestCase {
25
26     public void testEmptyStringer() {
27         // why isn't this the empty string?
28         assertNull(new JSONStringer().toString());
29     }
30
31     public void testValueJSONNull() throws JSONException {
32         JSONStringer stringer = new JSONStringer();
33         stringer.array();
34         stringer.value(JSONObject.NULL);
35         stringer.endArray();
36         assertEquals("[null]", stringer.toString());
37     }
38
39     public void testEmptyObject() throws JSONException {
40         JSONStringer stringer = new JSONStringer();
41         stringer.object();
42         stringer.endObject();
43         assertEquals("{}", stringer.toString());
44     }
45
46     public void testEmptyArray() throws JSONException {
47         JSONStringer stringer = new JSONStringer();
48         stringer.array();
49         stringer.endArray();
50         assertEquals("[]", stringer.toString());
51     }
52
53     public void testArray() throws JSONException {
54         JSONStringer stringer = new JSONStringer();
55         stringer.array();
56         stringer.value(false);
57         stringer.value(5.0);
58         stringer.value(5L);
59         stringer.value("five");
60         stringer.value(null);
61         stringer.endArray();
62         assertEquals("[false,5,5,\"five\",null]", stringer.toString());
63     }
64
65     public void testValueObjectMethods() throws JSONException {
66         JSONStringer stringer = new JSONStringer();
67         stringer.array();
68         stringer.value(Boolean.FALSE);
69         stringer.value(Double.valueOf(5.0));
70         stringer.value(Long.valueOf(5L));
71         stringer.endArray();
72         assertEquals("[false,5,5]", stringer.toString());
73     }
74
75     public void testKeyValue() throws JSONException {
76         JSONStringer stringer = new JSONStringer();
77         stringer.object();
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);
83         stringer.endObject();
84         assertEquals("{\"a\":false," +
85                 "\"b\":5," +
86                 "\"c\":5," +
87                 "\"d\":\"five\"," +
88                 "\"e\":null}", stringer.toString());
89     }
90
91     /**
92      * Test what happens when extreme values are emitted. Such values are likely
93      * to be rounded during parsing.
94      */
95     public void testNumericRepresentations() throws JSONException {
96         JSONStringer stringer = new JSONStringer();
97         stringer.array();
98         stringer.value(Long.MAX_VALUE);
99         stringer.value(Double.MIN_VALUE);
100         stringer.endArray();
101         assertEquals("[9223372036854775807,4.9E-324]", stringer.toString());
102     }
103
104     public void testWeirdNumbers() throws JSONException {
105         try {
106             new JSONStringer().array().value(Double.NaN);
107             fail();
108         } catch (JSONException e) {
109         }
110         try {
111             new JSONStringer().array().value(Double.NEGATIVE_INFINITY);
112             fail();
113         } catch (JSONException e) {
114         }
115         try {
116             new JSONStringer().array().value(Double.POSITIVE_INFINITY);
117             fail();
118         } catch (JSONException e) {
119         }
120
121         JSONStringer stringer = new JSONStringer();
122         stringer.array();
123         stringer.value(-0.0d);
124         stringer.value(0.0d);
125         stringer.endArray();
126         assertEquals("[-0,0]", stringer.toString());
127     }
128
129     public void testMismatchedScopes() {
130         try {
131             new JSONStringer().key("a");
132             fail();
133         } catch (JSONException e) {
134         }
135         try {
136             new JSONStringer().value("a");
137             fail();
138         } catch (JSONException e) {
139         }
140         try {
141             new JSONStringer().endObject();
142             fail();
143         } catch (JSONException e) {
144         }
145         try {
146             new JSONStringer().endArray();
147             fail();
148         } catch (JSONException e) {
149         }
150         try {
151             new JSONStringer().array().endObject();
152             fail();
153         } catch (JSONException e) {
154         }
155         try {
156             new JSONStringer().object().endArray();
157             fail();
158         } catch (JSONException e) {
159         }
160         try {
161             new JSONStringer().object().key("a").key("a");
162             fail();
163         } catch (JSONException e) {
164         }
165         try {
166             new JSONStringer().object().value(false);
167             fail();
168         } catch (JSONException e) {
169         }
170     }
171
172     public void testNullKey() {
173         try {
174             new JSONStringer().object().key(null);
175             fail();
176         } catch (JSONException e) {
177         }
178     }
179
180     public void testRepeatedKey() throws JSONException {
181         JSONStringer stringer = new JSONStringer();
182         stringer.object();
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());
188     }
189
190     public void testEmptyKey() throws JSONException {
191         JSONStringer stringer = new JSONStringer();
192         stringer.object();
193         stringer.key("").value(false);
194         stringer.endObject();
195         assertEquals("{\"\":false}", stringer.toString()); // legit behaviour!
196     }
197
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");
218     }
219
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));
228     }
229
230     public void testJSONArrayAsValue() throws JSONException {
231         JSONArray array = new JSONArray();
232         array.put(false);
233         JSONStringer stringer = new JSONStringer();
234         stringer.array();
235         stringer.value(array);
236         stringer.endArray();
237         assertEquals("[[false]]", stringer.toString());
238     }
239
240     public void testJSONObjectAsValue() throws JSONException {
241         JSONObject object = new JSONObject();
242         object.put("a", false);
243         JSONStringer stringer = new JSONStringer();
244         stringer.object();
245         stringer.key("b").value(object);
246         stringer.endObject();
247         assertEquals("{\"b\":{\"a\":false}}", stringer.toString());
248     }
249
250     public void testArrayNestingMaxDepthSupports20() throws JSONException {
251         JSONStringer stringer = new JSONStringer();
252         for (int i = 0; i < 20; i++) {
253             stringer.array();
254         }
255         for (int i = 0; i < 20; i++) {
256             stringer.endArray();
257         }
258         assertEquals("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]", stringer.toString());
259
260         stringer = new JSONStringer();
261         for (int i = 0; i < 20; i++) {
262             stringer.array();
263         }
264     }
265
266     public void testObjectNestingMaxDepthSupports20() throws JSONException {
267         JSONStringer stringer = new JSONStringer();
268         for (int i = 0; i < 20; i++) {
269             stringer.object();
270             stringer.key("a");
271         }
272         stringer.value(false);
273         for (int i = 0; i < 20; i++) {
274             stringer.endObject();
275         }
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());
279
280         stringer = new JSONStringer();
281         for (int i = 0; i < 20; i++) {
282             stringer.object();
283             stringer.key("a");
284         }
285     }
286
287     public void testMixedMaxDepthSupports20() throws JSONException {
288         JSONStringer stringer = new JSONStringer();
289         for (int i = 0; i < 20; i+=2) {
290             stringer.array();
291             stringer.object();
292             stringer.key("a");
293         }
294         stringer.value(false);
295         for (int i = 0; i < 20; i+=2) {
296             stringer.endObject();
297             stringer.endArray();
298         }
299         assertEquals("[{\"a\":[{\"a\":[{\"a\":[{\"a\":[{\"a\":" +
300                 "[{\"a\":[{\"a\":[{\"a\":[{\"a\":[{\"a\":false" +
301                 "}]}]}]}]}]}]}]}]}]}]", stringer.toString());
302
303         stringer = new JSONStringer();
304         for (int i = 0; i < 20; i+=2) {
305             stringer.array();
306             stringer.object();
307             stringer.key("a");
308         }
309     }
310
311     public void testMaxDepthWithArrayValue() throws JSONException {
312         JSONArray array = new JSONArray();
313         array.put(false);
314
315         JSONStringer stringer = new JSONStringer();
316         for (int i = 0; i < 20; i++) {
317             stringer.array();
318         }
319         stringer.value(array);
320         for (int i = 0; i < 20; i++) {
321             stringer.endArray();
322         }
323         assertEquals("[[[[[[[[[[[[[[[[[[[[[false]]]]]]]]]]]]]]]]]]]]]", stringer.toString());
324     }
325
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++) {
331             stringer.object();
332             stringer.key("b");
333         }
334         stringer.value(object);
335         for (int i = 0; i < 20; i++) {
336             stringer.endObject();
337         }
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());
341     }
342
343     public void testMultipleRoots() throws JSONException {
344         JSONStringer stringer = new JSONStringer();
345         stringer.array();
346         stringer.endArray();
347         try {
348             stringer.object();
349             fail();
350         } catch (JSONException e) {
351         }
352     }
353 }