1/*
2 * Copyright (C) 2010 Google Inc.
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 com.android.i18n.addressinput;
18
19import junit.framework.TestCase;
20
21import org.json.JSONArray;
22import org.json.JSONException;
23
24import java.util.Arrays;
25import java.util.HashSet;
26import java.util.Set;
27
28/**
29 * Unit test for {@link JsoMap}.
30 */
31public class JsoMapTest extends TestCase {
32
33    private static final String VALID_JSON = "{\"a\":\"b\",\"c\":1,\"d\":{\"e\":\"f\"}}";
34    private static final String INVALID_JSON = "!";
35
36    public void testBuildJsoMap() throws Exception {
37        assertNotNull(JsoMap.buildJsoMap(VALID_JSON));
38
39        try {
40            JsoMap.buildJsoMap(INVALID_JSON);
41            fail("Expected JSONException.");
42        } catch (JSONException e) {
43            // Expected.
44        }
45    }
46
47    public void testCreateEmptyJsoMap() throws Exception {
48        assertNotNull(JsoMap.createEmptyJsoMap());
49    }
50
51    public void testDelKey() throws Exception {
52        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
53
54        assertEquals("b", map.get("a"));
55        map.delKey("a");
56        assertNull(map.get("a"));
57
58        map.delKey("b");
59        map.delKey("c");
60        map.delKey("d");
61    }
62
63    public void testGet() throws Exception {
64        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
65        assertEquals("b", map.get("a"));
66        assertNull(map.get("b"));
67
68        try {
69            map.get("c");
70            fail("Expected IllegalArgumentException.");
71        } catch (IllegalArgumentException e) {
72            // Expected.
73        }
74
75        try {
76            map.get("d");
77            fail("Expected ClassCastException.");
78        } catch (ClassCastException e) {
79            // Expected.
80        }
81    }
82
83    public void testGetInt() throws Exception {
84        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
85
86        try {
87            map.getInt("a");
88            fail("Expected RuntimeException.");
89        } catch (RuntimeException e) {
90            // Expected.
91        }
92
93        assertEquals(-1, map.getInt("b"));
94        assertEquals(1, map.getInt("c"));
95
96        try {
97            map.getInt("d");
98            fail("Expected RuntimeException.");
99        } catch (RuntimeException e) {
100            // Expected.
101        }
102    }
103
104    public void testGetKeys() throws Exception {
105        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
106        JSONArray keys = map.getKeys();
107        assertNotNull(keys);
108        assertEquals(3, keys.length());
109        Set<String> keySet = new HashSet<String>(keys.length());
110        for (int i = 0; i < keys.length(); i++) {
111            keySet.add(keys.getString(i));
112        }
113        assertEquals(new HashSet<String>(Arrays.asList("a", "c", "d")), keySet);
114    }
115
116    public void testGetObj() throws Exception {
117        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
118
119        try {
120            map.getObj("a");
121            fail("Expected ClassCastException.");
122        } catch (ClassCastException e) {
123            // Expected.
124        }
125
126        assertNull(map.getObj("b"));
127
128        try {
129            map.getObj("c");
130            fail("Expected IllegalArgumentException.");
131        } catch (IllegalArgumentException e) {
132            // Expected.
133        }
134
135        JsoMap obj = map.getObj("d");
136        assertNotNull(obj);
137        assertEquals("f", obj.get("e"));
138    }
139
140    public void testContainsKey() throws Exception {
141        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
142        assertTrue(map.containsKey("a"));
143        assertFalse(map.containsKey("b"));
144        assertTrue(map.containsKey("c"));
145        assertTrue(map.containsKey("d"));
146    }
147
148    public void testMergeData() throws Exception {
149        JsoMap mapA = JsoMap.createEmptyJsoMap();
150        JsoMap mapB = JsoMap.createEmptyJsoMap();
151
152        mapA.putInt("a", 1);
153        mapA.putInt("b", 2);
154
155        mapB.putInt("b", 3);
156        mapB.putInt("c", 4);
157
158        mapA.mergeData(mapB);
159        assertEquals(1, mapA.getInt("a"));
160        assertEquals(2, mapA.getInt("b"));
161        assertEquals(4, mapA.getInt("c"));
162    }
163
164    public void testPut() throws Exception {
165        JsoMap map = JsoMap.createEmptyJsoMap();
166
167        map.put("a", "b");
168        assertEquals("b", map.get("a"));
169
170        map.put("a", "c");
171        assertEquals("c", map.get("a"));
172    }
173
174    public void testPutInt() throws Exception {
175        JsoMap map = JsoMap.createEmptyJsoMap();
176
177        map.putInt("a", 1);
178        assertEquals(1, map.getInt("a"));
179
180        map.putInt("a", 2);
181        assertEquals(2, map.getInt("a"));
182    }
183
184    public void testPutObj() throws Exception {
185        JsoMap map = JsoMap.createEmptyJsoMap();
186        JsoMap obj = JsoMap.createEmptyJsoMap();
187
188        obj.putInt("a", 1);
189        map.putObj("b", obj);
190        assertEquals(obj.toString(), map.getObj("b").toString());
191
192        obj.putInt("a", 2);
193        map.putObj("b", obj);
194        assertEquals(obj.toString(), map.getObj("b").toString());
195    }
196
197    public void testString() throws Exception {
198        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
199
200        try {
201            // This should fail on the integer "c" or the map "d".
202            map.string();
203            fail("Expected IllegalArgumentException.");
204        } catch (IllegalArgumentException e) {
205            // Expected.
206        } catch (ClassCastException e) {
207            // Expected.
208        }
209
210        map.delKey("c");
211        try {
212            // This should fail on the object "d".
213            map.string();
214            fail("Expected ClassCastException.");
215        } catch (ClassCastException e) {
216            // Expected.
217        }
218
219        map.delKey("d");
220        assertEquals("JsoMap[\n(a:b)\n]", map.string());
221    }
222
223    public void testMap() throws Exception {
224        JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
225        try {
226            // This should fail on the string "a" or the integer "c".
227            map.map();
228            fail("Expected ClassCastException.");
229        } catch (ClassCastException e) {
230            // Expected.
231        } catch (IllegalArgumentException e) {
232            // Expected.
233        }
234
235        map.delKey("a");
236        try {
237            // This should fail on the integer "c".
238            map.map();
239            fail("Expected IllegalArgumentException.");
240        } catch (IllegalArgumentException e) {
241            // Expected.
242        }
243
244        map.delKey("c");
245        assertEquals("JsoMap[\n(d:JsoMap[\n(e:f)\n])\n]", map.map());
246    }
247}
248