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
17package org.json;
18
19import junit.framework.TestCase;
20
21/**
22 * These tests checks self use calls. For the most part we doesn't attempt to
23 * cover self-use, except in those cases where our clean room implementation
24 * does it.
25 *
26 * <p>This black box test was written without inspecting the non-free org.json
27 * sourcecode.
28 */
29public class SelfUseTest extends TestCase {
30
31    private int objectPutCalls = 0;
32    private int objectGetCalls = 0;
33    private int objectOptCalls = 0;
34    private int objectOptTypeCalls = 0;
35    private int arrayPutCalls = 0;
36    private int arrayGetCalls = 0;
37    private int arrayOptCalls = 0;
38    private int arrayOptTypeCalls = 0;
39    private int tokenerNextCalls = 0;
40    private int tokenerNextValueCalls = 0;
41
42    private final JSONObject object = new JSONObject() {
43        @Override public JSONObject put(String name, Object value) throws JSONException {
44            objectPutCalls++;
45            return super.put(name, value);
46        }
47        @Override public Object get(String name) throws JSONException {
48            objectGetCalls++;
49            return super.get(name);
50        }
51        @Override public Object opt(String name) {
52            objectOptCalls++;
53            return super.opt(name);
54        }
55        @Override public boolean optBoolean(String key, boolean defaultValue) {
56            objectOptTypeCalls++;
57            return super.optBoolean(key, defaultValue);
58        }
59        @Override public double optDouble(String key, double defaultValue) {
60            objectOptTypeCalls++;
61            return super.optDouble(key, defaultValue);
62        }
63        @Override public int optInt(String key, int defaultValue) {
64            objectOptTypeCalls++;
65            return super.optInt(key, defaultValue);
66        }
67        @Override public long optLong(String key, long defaultValue) {
68            objectOptTypeCalls++;
69            return super.optLong(key, defaultValue);
70        }
71        @Override public String optString(String key, String defaultValue) {
72            objectOptTypeCalls++;
73            return super.optString(key, defaultValue);
74        }
75    };
76
77    private final JSONArray array = new JSONArray() {
78        @Override public JSONArray put(int index, Object value) throws JSONException {
79            arrayPutCalls++;
80            return super.put(index, value);
81        }
82        @Override public Object get(int index) throws JSONException {
83            arrayGetCalls++;
84            return super.get(index);
85        }
86        @Override public Object opt(int index) {
87            arrayOptCalls++;
88            return super.opt(index);
89        }
90        @Override public boolean optBoolean(int index, boolean fallback) {
91            arrayOptTypeCalls++;
92            return super.optBoolean(index, fallback);
93        }
94        @Override public double optDouble(int index, double fallback) {
95            arrayOptTypeCalls++;
96            return super.optDouble(index, fallback);
97        }
98        @Override public long optLong(int index, long fallback) {
99            arrayOptTypeCalls++;
100            return super.optLong(index, fallback);
101        }
102        @Override public String optString(int index, String fallback) {
103            arrayOptTypeCalls++;
104            return super.optString(index, fallback);
105        }
106        @Override public int optInt(int index, int fallback) {
107            arrayOptTypeCalls++;
108            return super.optInt(index, fallback);
109        }
110    };
111
112    private final JSONTokener tokener = new JSONTokener("{\"foo\": [true]}") {
113        @Override public char next() {
114            tokenerNextCalls++;
115            return super.next();
116        }
117        @Override public Object nextValue() throws JSONException {
118            tokenerNextValueCalls++;
119            return super.nextValue();
120        }
121    };
122
123
124    public void testObjectPut() throws JSONException {
125        object.putOpt("foo", "bar");
126        assertEquals(1, objectPutCalls);
127    }
128
129    public void testObjectAccumulate() throws JSONException {
130        object.accumulate("foo", "bar");
131        assertEquals(1, objectPutCalls);
132    }
133
134    public void testObjectGetBoolean() throws JSONException {
135        object.put("foo", "true");
136        object.getBoolean("foo");
137        assertEquals(1, objectGetCalls);
138    }
139
140    public void testObjectOptType() throws JSONException {
141        object.optBoolean("foo");
142        assertEquals(1, objectOptCalls);
143        assertEquals(1, objectOptTypeCalls);
144        object.optDouble("foo");
145        assertEquals(2, objectOptCalls);
146        assertEquals(2, objectOptTypeCalls);
147        object.optInt("foo");
148        assertEquals(3, objectOptCalls);
149        assertEquals(3, objectOptTypeCalls);
150        object.optLong("foo");
151        assertEquals(4, objectOptCalls);
152        assertEquals(4, objectOptTypeCalls);
153        object.optString("foo");
154        assertEquals(5, objectOptCalls);
155        assertEquals(5, objectOptTypeCalls);
156    }
157
158    public void testToJSONArray() throws JSONException {
159        object.put("foo", 5);
160        object.put("bar", 10);
161        array.put("foo");
162        array.put("baz");
163        array.put("bar");
164        object.toJSONArray(array);
165        assertEquals(3, arrayOptCalls);
166        assertEquals(0, arrayOptTypeCalls);
167        assertEquals(3, objectOptCalls);
168        assertEquals(0, objectOptTypeCalls);
169    }
170
171    public void testPutAtIndex() throws JSONException {
172        array.put(10, false);
173        assertEquals(1, arrayPutCalls);
174    }
175
176    public void testIsNull() {
177        array.isNull(5);
178        assertEquals(1, arrayOptCalls);
179    }
180
181    public void testArrayGetType() throws JSONException {
182        array.put(true);
183        array.getBoolean(0);
184        assertEquals(1, arrayGetCalls);
185    }
186
187    public void testArrayOptType() throws JSONException {
188        array.optBoolean(3);
189        assertEquals(1, arrayOptCalls);
190        assertEquals(1, arrayOptTypeCalls);
191        array.optDouble(3);
192        assertEquals(2, arrayOptCalls);
193        assertEquals(2, arrayOptTypeCalls);
194        array.optInt(3);
195        assertEquals(3, arrayOptCalls);
196        assertEquals(3, arrayOptTypeCalls);
197        array.optLong(3);
198        assertEquals(4, arrayOptCalls);
199        assertEquals(4, arrayOptTypeCalls);
200        array.optString(3);
201        assertEquals(5, arrayOptCalls);
202        assertEquals(5, arrayOptTypeCalls);
203    }
204
205    public void testToJSONObject() throws JSONException {
206        array.put("foo");
207        array.put("baz");
208        array.put("bar");
209        JSONArray values = new JSONArray();
210        values.put(5.5d);
211        values.put(11d);
212        values.put(30);
213        values.toJSONObject(array);
214        assertEquals(3, arrayOptCalls);
215        assertEquals(0, arrayOptTypeCalls);
216    }
217
218    public void testNextExpecting() throws JSONException {
219        tokener.next('{');
220        assertEquals(1, tokenerNextCalls);
221        tokener.next('\"');
222        assertEquals(2, tokenerNextCalls);
223    }
224
225    public void testNextValue() throws JSONException {
226        tokener.nextValue();
227        assertEquals(4, tokenerNextValueCalls);
228    }
229}
230