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 android.util.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 22 import android.support.test.filters.SmallTest; 23 import android.support.test.runner.AndroidJUnit4; 24 import android.util.JsonWriter; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import java.io.IOException; 30 import java.io.StringWriter; 31 import java.math.BigDecimal; 32 import java.math.BigInteger; 33 34 @SmallTest 35 @RunWith(AndroidJUnit4.class) 36 public final class JsonWriterTest { 37 @Test 38 public void testWrongTopLevelType() throws IOException { 39 StringWriter stringWriter = new StringWriter(); 40 JsonWriter jsonWriter = new JsonWriter(stringWriter); 41 try { 42 jsonWriter.value("a"); 43 fail(); 44 } catch (IllegalStateException expected) { 45 } 46 } 47 48 @Test 49 public void testTwoNames() throws IOException { 50 StringWriter stringWriter = new StringWriter(); 51 JsonWriter jsonWriter = new JsonWriter(stringWriter); 52 jsonWriter.beginObject(); 53 jsonWriter.name("a"); 54 try { 55 jsonWriter.name("a"); 56 fail(); 57 } catch (IllegalStateException expected) { 58 } 59 } 60 61 @Test 62 public void testNameWithoutValue() throws IOException { 63 StringWriter stringWriter = new StringWriter(); 64 JsonWriter jsonWriter = new JsonWriter(stringWriter); 65 jsonWriter.beginObject(); 66 jsonWriter.name("a"); 67 try { 68 jsonWriter.endObject(); 69 fail(); 70 } catch (IllegalStateException expected) { 71 } 72 } 73 74 @Test 75 public void testValueWithoutName() throws IOException { 76 StringWriter stringWriter = new StringWriter(); 77 JsonWriter jsonWriter = new JsonWriter(stringWriter); 78 jsonWriter.beginObject(); 79 try { 80 jsonWriter.value(true); 81 fail(); 82 } catch (IllegalStateException expected) { 83 } 84 } 85 86 @Test 87 public void testMultipleTopLevelValues() throws IOException { 88 StringWriter stringWriter = new StringWriter(); 89 JsonWriter jsonWriter = new JsonWriter(stringWriter); 90 jsonWriter.beginArray().endArray(); 91 try { 92 jsonWriter.beginArray(); 93 fail(); 94 } catch (IllegalStateException expected) { 95 } 96 } 97 98 @Test 99 public void testBadNestingObject() throws IOException { 100 StringWriter stringWriter = new StringWriter(); 101 JsonWriter jsonWriter = new JsonWriter(stringWriter); 102 jsonWriter.beginArray(); 103 jsonWriter.beginObject(); 104 try { 105 jsonWriter.endArray(); 106 fail(); 107 } catch (IllegalStateException expected) { 108 } 109 } 110 111 @Test 112 public void testBadNestingArray() throws IOException { 113 StringWriter stringWriter = new StringWriter(); 114 JsonWriter jsonWriter = new JsonWriter(stringWriter); 115 jsonWriter.beginArray(); 116 jsonWriter.beginArray(); 117 try { 118 jsonWriter.endObject(); 119 fail(); 120 } catch (IllegalStateException expected) { 121 } 122 } 123 124 @Test 125 public void testNullName() throws IOException { 126 StringWriter stringWriter = new StringWriter(); 127 JsonWriter jsonWriter = new JsonWriter(stringWriter); 128 jsonWriter.beginObject(); 129 try { 130 jsonWriter.name(null); 131 fail(); 132 } catch (NullPointerException expected) { 133 } 134 } 135 136 @Test 137 public void testNullStringValue() throws IOException { 138 StringWriter stringWriter = new StringWriter(); 139 JsonWriter jsonWriter = new JsonWriter(stringWriter); 140 jsonWriter.beginObject(); 141 jsonWriter.name("a"); 142 jsonWriter.value((String) null); 143 jsonWriter.endObject(); 144 assertEquals("{\"a\":null}", stringWriter.toString()); 145 } 146 147 @Test 148 public void testNonFiniteDoubles() throws IOException { 149 StringWriter stringWriter = new StringWriter(); 150 JsonWriter jsonWriter = new JsonWriter(stringWriter); 151 jsonWriter.beginArray(); 152 try { 153 jsonWriter.value(Double.NaN); 154 fail(); 155 } catch (IllegalArgumentException expected) { 156 } 157 try { 158 jsonWriter.value(Double.NEGATIVE_INFINITY); 159 fail(); 160 } catch (IllegalArgumentException expected) { 161 } 162 try { 163 jsonWriter.value(Double.POSITIVE_INFINITY); 164 fail(); 165 } catch (IllegalArgumentException expected) { 166 } 167 } 168 169 @Test 170 public void testNonFiniteBoxedDoubles() throws IOException { 171 StringWriter stringWriter = new StringWriter(); 172 JsonWriter jsonWriter = new JsonWriter(stringWriter); 173 jsonWriter.beginArray(); 174 try { 175 jsonWriter.value(new Double(Double.NaN)); 176 fail(); 177 } catch (IllegalArgumentException expected) { 178 } 179 try { 180 jsonWriter.value(new Double(Double.NEGATIVE_INFINITY)); 181 fail(); 182 } catch (IllegalArgumentException expected) { 183 } 184 try { 185 jsonWriter.value(new Double(Double.POSITIVE_INFINITY)); 186 fail(); 187 } catch (IllegalArgumentException expected) { 188 } 189 } 190 191 @Test 192 public void testDoubles() throws IOException { 193 StringWriter stringWriter = new StringWriter(); 194 JsonWriter jsonWriter = new JsonWriter(stringWriter); 195 jsonWriter.beginArray(); 196 jsonWriter.value(-0.0); 197 jsonWriter.value(1.0); 198 jsonWriter.value(Double.MAX_VALUE); 199 jsonWriter.value(Double.MIN_VALUE); 200 jsonWriter.value(0.0); 201 jsonWriter.value(-0.5); 202 jsonWriter.value(Double.MIN_NORMAL); 203 jsonWriter.value(Math.PI); 204 jsonWriter.value(Math.E); 205 jsonWriter.endArray(); 206 jsonWriter.close(); 207 assertEquals("[-0.0," 208 + "1.0," 209 + "1.7976931348623157E308," 210 + "4.9E-324," 211 + "0.0," 212 + "-0.5," 213 + "2.2250738585072014E-308," 214 + "3.141592653589793," 215 + "2.718281828459045]", stringWriter.toString()); 216 } 217 218 @Test 219 public void testLongs() throws IOException { 220 StringWriter stringWriter = new StringWriter(); 221 JsonWriter jsonWriter = new JsonWriter(stringWriter); 222 jsonWriter.beginArray(); 223 jsonWriter.value(0); 224 jsonWriter.value(1); 225 jsonWriter.value(-1); 226 jsonWriter.value(Long.MIN_VALUE); 227 jsonWriter.value(Long.MAX_VALUE); 228 jsonWriter.endArray(); 229 jsonWriter.close(); 230 assertEquals("[0," 231 + "1," 232 + "-1," 233 + "-9223372036854775808," 234 + "9223372036854775807]", stringWriter.toString()); 235 } 236 237 @Test 238 public void testNumbers() throws IOException { 239 StringWriter stringWriter = new StringWriter(); 240 JsonWriter jsonWriter = new JsonWriter(stringWriter); 241 jsonWriter.beginArray(); 242 jsonWriter.value(new BigInteger("0")); 243 jsonWriter.value(new BigInteger("9223372036854775808")); 244 jsonWriter.value(new BigInteger("-9223372036854775809")); 245 jsonWriter.value(new BigDecimal("3.141592653589793238462643383")); 246 jsonWriter.endArray(); 247 jsonWriter.close(); 248 assertEquals("[0," 249 + "9223372036854775808," 250 + "-9223372036854775809," 251 + "3.141592653589793238462643383]", stringWriter.toString()); 252 } 253 254 @Test 255 public void testBooleans() throws IOException { 256 StringWriter stringWriter = new StringWriter(); 257 JsonWriter jsonWriter = new JsonWriter(stringWriter); 258 jsonWriter.beginArray(); 259 jsonWriter.value(true); 260 jsonWriter.value(false); 261 jsonWriter.endArray(); 262 assertEquals("[true,false]", stringWriter.toString()); 263 } 264 265 @Test 266 public void testNulls() throws IOException { 267 StringWriter stringWriter = new StringWriter(); 268 JsonWriter jsonWriter = new JsonWriter(stringWriter); 269 jsonWriter.beginArray(); 270 jsonWriter.nullValue(); 271 jsonWriter.endArray(); 272 assertEquals("[null]", stringWriter.toString()); 273 } 274 275 @Test 276 public void testStrings() throws IOException { 277 StringWriter stringWriter = new StringWriter(); 278 JsonWriter jsonWriter = new JsonWriter(stringWriter); 279 jsonWriter.beginArray(); 280 jsonWriter.value("a"); 281 jsonWriter.value("a\""); 282 jsonWriter.value("\""); 283 jsonWriter.value(":"); 284 jsonWriter.value(","); 285 jsonWriter.value("\b"); 286 jsonWriter.value("\f"); 287 jsonWriter.value("\n"); 288 jsonWriter.value("\r"); 289 jsonWriter.value("\t"); 290 jsonWriter.value(" "); 291 jsonWriter.value("\\"); 292 jsonWriter.value("{"); 293 jsonWriter.value("}"); 294 jsonWriter.value("["); 295 jsonWriter.value("]"); 296 jsonWriter.value("\0"); 297 jsonWriter.value("\u0019"); 298 jsonWriter.endArray(); 299 assertEquals("[\"a\"," 300 + "\"a\\\"\"," 301 + "\"\\\"\"," 302 + "\":\"," 303 + "\",\"," 304 + "\"\\b\"," 305 + "\"\\f\"," 306 + "\"\\n\"," 307 + "\"\\r\"," 308 + "\"\\t\"," 309 + "\" \"," 310 + "\"\\\\\"," 311 + "\"{\"," 312 + "\"}\"," 313 + "\"[\"," 314 + "\"]\"," 315 + "\"\\u0000\"," 316 + "\"\\u0019\"]", stringWriter.toString()); 317 } 318 319 @Test 320 public void testUnicodeLineBreaksEscaped() throws IOException { 321 StringWriter stringWriter = new StringWriter(); 322 JsonWriter jsonWriter = new JsonWriter(stringWriter); 323 jsonWriter.beginArray(); 324 jsonWriter.value("\u2028 \u2029"); 325 jsonWriter.endArray(); 326 assertEquals("[\"\\u2028 \\u2029\"]", stringWriter.toString()); 327 } 328 329 @Test 330 public void testEmptyArray() throws IOException { 331 StringWriter stringWriter = new StringWriter(); 332 JsonWriter jsonWriter = new JsonWriter(stringWriter); 333 jsonWriter.beginArray(); 334 jsonWriter.endArray(); 335 assertEquals("[]", stringWriter.toString()); 336 } 337 338 @Test 339 public void testEmptyObject() throws IOException { 340 StringWriter stringWriter = new StringWriter(); 341 JsonWriter jsonWriter = new JsonWriter(stringWriter); 342 jsonWriter.beginObject(); 343 jsonWriter.endObject(); 344 assertEquals("{}", stringWriter.toString()); 345 } 346 347 @Test 348 public void testObjectsInArrays() throws IOException { 349 StringWriter stringWriter = new StringWriter(); 350 JsonWriter jsonWriter = new JsonWriter(stringWriter); 351 jsonWriter.beginArray(); 352 jsonWriter.beginObject(); 353 jsonWriter.name("a").value(5); 354 jsonWriter.name("b").value(false); 355 jsonWriter.endObject(); 356 jsonWriter.beginObject(); 357 jsonWriter.name("c").value(6); 358 jsonWriter.name("d").value(true); 359 jsonWriter.endObject(); 360 jsonWriter.endArray(); 361 assertEquals("[{\"a\":5,\"b\":false}," 362 + "{\"c\":6,\"d\":true}]", stringWriter.toString()); 363 } 364 365 @Test 366 public void testArraysInObjects() throws IOException { 367 StringWriter stringWriter = new StringWriter(); 368 JsonWriter jsonWriter = new JsonWriter(stringWriter); 369 jsonWriter.beginObject(); 370 jsonWriter.name("a"); 371 jsonWriter.beginArray(); 372 jsonWriter.value(5); 373 jsonWriter.value(false); 374 jsonWriter.endArray(); 375 jsonWriter.name("b"); 376 jsonWriter.beginArray(); 377 jsonWriter.value(6); 378 jsonWriter.value(true); 379 jsonWriter.endArray(); 380 jsonWriter.endObject(); 381 assertEquals("{\"a\":[5,false]," 382 + "\"b\":[6,true]}", stringWriter.toString()); 383 } 384 385 @Test 386 public void testDeepNestingArrays() throws IOException { 387 StringWriter stringWriter = new StringWriter(); 388 JsonWriter jsonWriter = new JsonWriter(stringWriter); 389 for (int i = 0; i < 20; i++) { 390 jsonWriter.beginArray(); 391 } 392 for (int i = 0; i < 20; i++) { 393 jsonWriter.endArray(); 394 } 395 assertEquals("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]", stringWriter.toString()); 396 } 397 398 @Test 399 public void testDeepNestingObjects() throws IOException { 400 StringWriter stringWriter = new StringWriter(); 401 JsonWriter jsonWriter = new JsonWriter(stringWriter); 402 jsonWriter.beginObject(); 403 for (int i = 0; i < 20; i++) { 404 jsonWriter.name("a"); 405 jsonWriter.beginObject(); 406 } 407 for (int i = 0; i < 20; i++) { 408 jsonWriter.endObject(); 409 } 410 jsonWriter.endObject(); 411 assertEquals("{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":" 412 + "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{" 413 + "}}}}}}}}}}}}}}}}}}}}}", stringWriter.toString()); 414 } 415 416 @Test 417 public void testRepeatedName() throws IOException { 418 StringWriter stringWriter = new StringWriter(); 419 JsonWriter jsonWriter = new JsonWriter(stringWriter); 420 jsonWriter.beginObject(); 421 jsonWriter.name("a").value(true); 422 jsonWriter.name("a").value(false); 423 jsonWriter.endObject(); 424 // JsonWriter doesn't attempt to detect duplicate names 425 assertEquals("{\"a\":true,\"a\":false}", stringWriter.toString()); 426 } 427 428 @Test 429 public void testPrettyPrintObject() throws IOException { 430 StringWriter stringWriter = new StringWriter(); 431 JsonWriter jsonWriter = new JsonWriter(stringWriter); 432 jsonWriter.setIndent(" "); 433 434 jsonWriter.beginObject(); 435 jsonWriter.name("a").value(true); 436 jsonWriter.name("b").value(false); 437 jsonWriter.name("c").value(5.0); 438 jsonWriter.name("e").nullValue(); 439 jsonWriter.name("f").beginArray(); 440 jsonWriter.value(6.0); 441 jsonWriter.value(7.0); 442 jsonWriter.endArray(); 443 jsonWriter.name("g").beginObject(); 444 jsonWriter.name("h").value(8.0); 445 jsonWriter.name("i").value(9.0); 446 jsonWriter.endObject(); 447 jsonWriter.endObject(); 448 449 String expected = "{\n" 450 + " \"a\": true,\n" 451 + " \"b\": false,\n" 452 + " \"c\": 5.0,\n" 453 + " \"e\": null,\n" 454 + " \"f\": [\n" 455 + " 6.0,\n" 456 + " 7.0\n" 457 + " ],\n" 458 + " \"g\": {\n" 459 + " \"h\": 8.0,\n" 460 + " \"i\": 9.0\n" 461 + " }\n" 462 + "}"; 463 assertEquals(expected, stringWriter.toString()); 464 } 465 466 @Test 467 public void testPrettyPrintArray() throws IOException { 468 StringWriter stringWriter = new StringWriter(); 469 JsonWriter jsonWriter = new JsonWriter(stringWriter); 470 jsonWriter.setIndent(" "); 471 472 jsonWriter.beginArray(); 473 jsonWriter.value(true); 474 jsonWriter.value(false); 475 jsonWriter.value(5.0); 476 jsonWriter.nullValue(); 477 jsonWriter.beginObject(); 478 jsonWriter.name("a").value(6.0); 479 jsonWriter.name("b").value(7.0); 480 jsonWriter.endObject(); 481 jsonWriter.beginArray(); 482 jsonWriter.value(8.0); 483 jsonWriter.value(9.0); 484 jsonWriter.endArray(); 485 jsonWriter.endArray(); 486 487 String expected = "[\n" 488 + " true,\n" 489 + " false,\n" 490 + " 5.0,\n" 491 + " null,\n" 492 + " {\n" 493 + " \"a\": 6.0,\n" 494 + " \"b\": 7.0\n" 495 + " },\n" 496 + " [\n" 497 + " 8.0,\n" 498 + " 9.0\n" 499 + " ]\n" 500 + "]"; 501 assertEquals(expected, stringWriter.toString()); 502 } 503 } 504