2 * Copyright (C) 2010 Google Inc.
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.
17 package com.android.i18n.addressinput;
19 import junit.framework.TestCase;
21 import org.json.JSONArray;
22 import org.json.JSONException;
24 import java.util.Arrays;
25 import java.util.HashSet;
29 * Unit test for {@link JsoMap}.
31 public class JsoMapTest extends TestCase {
33 private static final String VALID_JSON = "{\"a\":\"b\",\"c\":1,\"d\":{\"e\":\"f\"}}";
34 private static final String INVALID_JSON = "!";
36 public void testBuildJsoMap() throws Exception {
37 assertNotNull(JsoMap.buildJsoMap(VALID_JSON));
40 JsoMap.buildJsoMap(INVALID_JSON);
41 fail("Expected JSONException.");
42 } catch (JSONException e) {
47 public void testCreateEmptyJsoMap() throws Exception {
48 assertNotNull(JsoMap.createEmptyJsoMap());
51 public void testDelKey() throws Exception {
52 JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
54 assertEquals("b", map.get("a"));
56 assertNull(map.get("a"));
63 public void testGet() throws Exception {
64 JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
65 assertEquals("b", map.get("a"));
66 assertNull(map.get("b"));
70 fail("Expected IllegalArgumentException.");
71 } catch (IllegalArgumentException e) {
77 fail("Expected ClassCastException.");
78 } catch (ClassCastException e) {
83 public void testGetInt() throws Exception {
84 JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
88 fail("Expected RuntimeException.");
89 } catch (RuntimeException e) {
93 assertEquals(-1, map.getInt("b"));
94 assertEquals(1, map.getInt("c"));
98 fail("Expected RuntimeException.");
99 } catch (RuntimeException e) {
104 public void testGetKeys() throws Exception {
105 JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
106 JSONArray keys = map.getKeys();
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));
113 assertEquals(new HashSet<String>(Arrays.asList("a", "c", "d")), keySet);
116 public void testGetObj() throws Exception {
117 JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
121 fail("Expected ClassCastException.");
122 } catch (ClassCastException e) {
126 assertNull(map.getObj("b"));
130 fail("Expected IllegalArgumentException.");
131 } catch (IllegalArgumentException e) {
135 JsoMap obj = map.getObj("d");
137 assertEquals("f", obj.get("e"));
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"));
148 public void testMergeData() throws Exception {
149 JsoMap mapA = JsoMap.createEmptyJsoMap();
150 JsoMap mapB = JsoMap.createEmptyJsoMap();
158 mapA.mergeData(mapB);
159 assertEquals(1, mapA.getInt("a"));
160 assertEquals(2, mapA.getInt("b"));
161 assertEquals(4, mapA.getInt("c"));
164 public void testPut() throws Exception {
165 JsoMap map = JsoMap.createEmptyJsoMap();
168 assertEquals("b", map.get("a"));
171 assertEquals("c", map.get("a"));
174 public void testPutInt() throws Exception {
175 JsoMap map = JsoMap.createEmptyJsoMap();
178 assertEquals(1, map.getInt("a"));
181 assertEquals(2, map.getInt("a"));
184 public void testPutObj() throws Exception {
185 JsoMap map = JsoMap.createEmptyJsoMap();
186 JsoMap obj = JsoMap.createEmptyJsoMap();
189 map.putObj("b", obj);
190 assertEquals(obj.toString(), map.getObj("b").toString());
193 map.putObj("b", obj);
194 assertEquals(obj.toString(), map.getObj("b").toString());
197 public void testString() throws Exception {
198 JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
201 // This should fail on the integer "c" or the map "d".
203 fail("Expected IllegalArgumentException.");
204 } catch (IllegalArgumentException e) {
206 } catch (ClassCastException e) {
212 // This should fail on the object "d".
214 fail("Expected ClassCastException.");
215 } catch (ClassCastException e) {
220 assertEquals("JsoMap[\n(a:b)\n]", map.string());
223 public void testMap() throws Exception {
224 JsoMap map = JsoMap.buildJsoMap(VALID_JSON);
226 // This should fail on the string "a" or the integer "c".
228 fail("Expected ClassCastException.");
229 } catch (ClassCastException e) {
231 } catch (IllegalArgumentException e) {
237 // This should fail on the integer "c".
239 fail("Expected IllegalArgumentException.");
240 } catch (IllegalArgumentException e) {
245 assertEquals("JsoMap[\n(d:JsoMap[\n(e:f)\n])\n]", map.map());