forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestJSONTokener.java
More file actions
678 lines (634 loc) · 19.2 KB
/
TestJSONTokener.java
File metadata and controls
678 lines (634 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
* File: TestJSONTokener.java Author: JSON.org
*/
package org.json.tests;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import junit.framework.TestCase;
/**
* The Class TestJSONTokener.
*/
public class TestJSONTokener extends TestCase
{
/** The jsontokener. */
JSONTokener jsontokener;
/** The jsonobject. */
JSONObject jsonobject = new JSONObject();
/**
* The Class MockInputStreamThrowsExceptionOnFourthRead.
*/
class MockInputStreamThrowsExceptionOnFourthRead extends InputStream
{
/** The position. */
int position = 0;
/* (non-Javadoc)
* @see java.io.InputStream#read()
*/
@Override
public int read() throws IOException
{
if(position < 3)
position++;
else
throw new IOException("Mock IOException thrown from read");
return 'a';
}
}
/**
* The Class MockInputStreamThrowsExceptionOnReset.
*/
class MockInputStreamThrowsExceptionOnReset extends BufferedReader
{
/**
* Instantiates a new mock input stream throws exception on reset.
*
* @param in the in
*/
public MockInputStreamThrowsExceptionOnReset(Reader in)
{
super(in);
}
/* (non-Javadoc)
* @see java.io.BufferedReader#read()
*/
@Override
public int read() throws IOException
{
return 0;
}
/* (non-Javadoc)
* @see java.io.BufferedReader#reset()
*/
@Override
public void reset() throws IOException
{
throw new IOException("Mock IOException thrown from reset");
}
}
/**
* Tests the constructor method using input stream.
*/
public void testConstructor_InputStream()
{
byte[] buf;
String string = "{\"abc\":\"123\"}";
buf = string.getBytes();
ByteArrayInputStream is = new ByteArrayInputStream(buf);
try
{
jsontokener = new JSONTokener(is);
assertEquals('{', jsontokener.next());
assertEquals("abc", jsontokener.nextValue());
assertEquals(':', jsontokener.next());
assertEquals("123", jsontokener.nextValue());
assertEquals('}', jsontokener.next());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the back method.
*/
public void testBack()
{
byte[] buf;
String string = "{\"abc\":\"123\"}";
buf = string.getBytes();
ByteArrayInputStream is = new ByteArrayInputStream(buf);
try
{
jsontokener = new JSONTokener(is);
assertEquals('{', jsontokener.next());
assertEquals("abc", jsontokener.nextValue());
assertEquals(':', jsontokener.next());
jsontokener.back();
assertEquals(':', jsontokener.next());
assertEquals("123", jsontokener.nextValue());
assertEquals('}', jsontokener.next());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the back method using fails if used twice.
*/
public void testBack_FailsIfUsedTwice()
{
byte[] buf;
String string = "{\"abc\":\"123\"}";
buf = string.getBytes();
ByteArrayInputStream is = new ByteArrayInputStream(buf);
try
{
jsontokener = new JSONTokener(is);
assertEquals('{', jsontokener.next());
assertEquals("abc", jsontokener.nextValue());
assertEquals(':', jsontokener.next());
jsontokener.back();
jsontokener.back();
} catch (JSONException e)
{
assertEquals("Stepping back two steps is not supported",e.getMessage());
}
}
/**
* Tests the next method using fake input stream to test ioexception.
*/
public void testNext_FakeInputStreamToTestIoexception()
{
try
{
jsontokener = new JSONTokener(new MockInputStreamThrowsExceptionOnFourthRead());
assertEquals('a', jsontokener.next());
assertEquals('a', jsontokener.next());
assertEquals('a', jsontokener.next());
assertEquals('a', jsontokener.next());
assertEquals('a', jsontokener.next());
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("Mock IOException thrown from read",e.getMessage());
}
}
/**
* Tests the next method using empty stream.
*/
public void testNext_EmptyStream()
{
byte[] buf;
String string = "";
buf = string.getBytes();
ByteArrayInputStream is = new ByteArrayInputStream(buf);
try
{
jsontokener = new JSONTokener(is);
assertEquals(0, jsontokener.next());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the next method using line increments on new line.
*/
public void testNext_LineIncrementsOnNewLine()
{
jsontokener = new JSONTokener("abc\n123");
try
{
jsontokener.next();//a
jsontokener.next();//b
jsontokener.next();//c
assertEquals(" at 3 [character 4 line 1]",jsontokener.toString());
jsontokener.next();//\n
assertEquals(" at 4 [character 0 line 2]",jsontokener.toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the next method using line increments on carriage return.
*/
public void testNext_LineIncrementsOnCarriageReturn()
{
jsontokener = new JSONTokener("abc\r123");
try
{
jsontokener.next();//a
jsontokener.next();//b
jsontokener.next();//c
assertEquals(" at 3 [character 4 line 1]",jsontokener.toString());
jsontokener.next();//\r
jsontokener.next();//1
assertEquals(" at 5 [character 1 line 2]",jsontokener.toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the next method using line increments on carriage return and new line.
*/
public void testNext_LineIncrementsOnCarriageReturnAndNewLine()
{
jsontokener = new JSONTokener("abc\r\n123");
try
{
jsontokener.next();//a
jsontokener.next();//b
jsontokener.next();//c
assertEquals(" at 3 [character 4 line 1]",jsontokener.toString());
jsontokener.next();//\r
jsontokener.next();//\n
assertEquals(" at 5 [character 0 line 2]",jsontokener.toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the skipTo method.
*/
public void testSkipTo()
{
byte[] buf;
String string = "{\"abc\":\"123\",\"wer\":\"rty\"}";
buf = string.getBytes();
ByteArrayInputStream is = new ByteArrayInputStream(buf);
try
{
jsontokener = new JSONTokener(is);
assertEquals('{',jsontokener.next());
assertEquals("abc",jsontokener.nextValue());
assertEquals(':',jsontokener.next());
assertEquals("123",jsontokener.nextValue());
assertEquals(',',jsontokener.next());
assertEquals(0,jsontokener.skipTo('g'));
assertEquals('"',jsontokener.next());
assertEquals('t',jsontokener.skipTo('t'));
assertEquals('t',jsontokener.next());
assertEquals('y',jsontokener.next());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the skipTo method using fake input stream to test ioexception.
*/
public void testSkipTo_FakeInputStreamToTestIoexception()
{
jsontokener = new JSONTokener(new MockInputStreamThrowsExceptionOnReset(new StringReader("123")));
try
{
jsontokener.skipTo('l');
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Mock IOException thrown from reset", e.getMessage());
}
}
/**
* Tests the end method.
*/
public void testEnd()
{
jsontokener = new JSONTokener("a");
try
{
assertFalse(jsontokener.end());
jsontokener.next();
jsontokener.next();
assertTrue(jsontokener.end());
jsontokener.back();
assertFalse(jsontokener.end());
jsontokener.next();
jsontokener.next();
assertTrue(jsontokener.end());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the more method.
*/
public void testMore()
{
jsontokener = new JSONTokener("a");
try
{
assertTrue(jsontokener.more());
jsontokener.next();
assertFalse(jsontokener.more());
jsontokener.back();
assertTrue(jsontokener.more());
jsontokener.next();
assertFalse(jsontokener.more());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextValue method using nice string.
*/
public void testNextValue_NiceString()
{
jsontokener = new JSONTokener("abc");
try
{
assertEquals("abc", jsontokener.nextValue());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextValue method using string with new line.
*/
public void testNextValue_StringWithNewLine()
{
jsontokener = new JSONTokener("abc\n123");
try
{
assertEquals("abc", jsontokener.nextValue());
assertEquals(123, jsontokener.nextValue());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextValue method using json object string.
*/
public void testNextValue_JsonObjectString()
{
JSONObject jo = new JSONObject();
try
{
jo.put("abc","123");
jsontokener = new JSONTokener(jo.toString());
assertEquals(jo.toString(), jsontokener.nextValue().toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextValue method using indented json object string.
*/
public void testNextValue_IndentedJsonObjectString()
{
JSONObject jo = new JSONObject();
try
{
jo.put("abc","123");
jsontokener = new JSONTokener(jo.toString(4));
assertEquals(jo.toString(), jsontokener.nextValue().toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextValue method using json array string.
*/
public void testNextValue_JsonArrayString()
{
JSONArray ja = new JSONArray();
try
{
ja.put("abc");
ja.put("123");
jsontokener = new JSONTokener(ja.toString());
assertEquals(ja.toString(), jsontokener.nextValue().toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextValue method using indented json array string.
*/
public void testNextValue_IndentedJsonArrayString()
{
JSONArray ja = new JSONArray();
try
{
ja.put("abc");
ja.put("123");
jsontokener = new JSONTokener(ja.toString(4));
assertEquals(ja.toString(), jsontokener.nextValue().toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextValue method using empty string.
*/
public void testNextValue_EmptyString()
{
jsontokener = new JSONTokener("");
try
{
jsontokener.nextValue();
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Missing value at 0 [character 1 line 1]", e.getMessage());
}
}
/**
* Tests the next method using expected char.
*/
public void testNext_ExpectedChar()
{
jsontokener = new JSONTokener("abc");
char expectedA = 0;
try
{
expectedA = jsontokener.next('a');
jsontokener.next('c');
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals('a', expectedA);
assertEquals("Expected 'c' and instead saw 'b' at 2 [character 3 line 1]", e.getMessage());
}
}
/**
* Tests the next method using expected number of characters.
*/
public void testNext_ExpectedNumberOfCharacters()
{
jsontokener = new JSONTokener("abc123");
String expectedAbc = "";
String expectedBlank = "";
try
{
expectedAbc = jsontokener.next(3);
expectedBlank = jsontokener.next(0);
jsontokener.next(7);
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("abc", expectedAbc);
assertEquals("", expectedBlank);
assertEquals("Substring bounds error at 7 [character 8 line 1]", e.getMessage());
}
}
/**
* Tests the nextTo method using character.
*/
public void testNextTo_Character()
{
jsontokener = new JSONTokener("abc123,test\ntestString1\rsecondString\r\nthird String");
try
{
assertEquals("abc123", jsontokener.nextTo(','));
jsontokener.next(',');
assertEquals("test", jsontokener.nextTo(','));
jsontokener.next('\n');
assertEquals("testString1", jsontokener.nextTo(','));
jsontokener.next('\r');
assertEquals("secondString", jsontokener.nextTo(','));
jsontokener.next('\r');
jsontokener.next('\n');
assertEquals("third String", jsontokener.nextTo(','));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextTo method using string.
*/
public void testNextTo_String()
{
jsontokener = new JSONTokener("abc123,test\ntestString1\rsecondString\r\nthird String");
try
{
assertEquals("abc", jsontokener.nextTo("1,"));
assertEquals("123", jsontokener.nextTo("abc,"));
jsontokener.next(',');
assertEquals("te", jsontokener.nextTo("sabc"));
assertEquals("st", jsontokener.nextTo("ring"));
jsontokener.next('\n');
assertEquals("testSt", jsontokener.nextTo("r"));
assertEquals("ring1", jsontokener.nextTo("qw"));
jsontokener.next('\r');
assertEquals("second", jsontokener.nextTo("bhS"));
assertEquals("String", jsontokener.nextTo("dbh"));
jsontokener.next('\r');
jsontokener.next('\n');
assertEquals("third", jsontokener.nextTo(" ng"));
assertEquals("String", jsontokener.nextTo("qwhdab"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextString method.
*/
public void testNextString()
{
jsontokener = new JSONTokener("'abc'\"1\\\"2\\\"3\"'a\\u1111b\\fc\\trhd\\bdd\\r\\ngghhj'\"hghghgjfjf\"");
try
{
jsontokener.next('\'');
assertEquals("abc", jsontokener.nextString('\''));
jsontokener.next('"');
assertEquals("1\"2\"3", jsontokener.nextString('"'));
jsontokener.next('\'');
assertEquals("a\u1111b\fc\trhd\bdd\r\ngghhj", jsontokener.nextString('\''));
jsontokener.next('"');
assertEquals("hghghgjfjf", jsontokener.nextString('"'));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the nextString method using illegal escape.
*/
public void testNextString_IllegalEscape()
{
jsontokener = new JSONTokener("'ab\\\tc'");
try
{
jsontokener.next('\'');
jsontokener.nextString('\'');
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("Illegal escape. at 5 [character 6 line 1]", e.getMessage());
}
}
/**
* Tests the nextString method using unterminated string.
*/
public void testNextString_UnterminatedString()
{
jsontokener = new JSONTokener("'abc");
try
{
jsontokener.next('\'');
jsontokener.nextString('\'');
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("Unterminated string at 5 [character 6 line 1]", e.getMessage());
}
}
/**
* Tests the dehexChar method.
*/
public static void testDehexChar()
{
char i = '0';
int j = 0;
while(i <= '9')
{
assertEquals(j, JSONTokener.dehexchar(i));
i++;
j++;
}
i = 'A';
while(i <= 'F')
{
assertEquals(j, JSONTokener.dehexchar(i));
i++;
j++;
}
j = 10;
i = 'a';
while(i <= 'f')
{
assertEquals(j, JSONTokener.dehexchar(i));
i++;
j++;
}
assertEquals(-1, JSONTokener.dehexchar('$'));
assertEquals(-1, JSONTokener.dehexchar('g'));
assertEquals(-1, JSONTokener.dehexchar('G'));
assertEquals(-1, JSONTokener.dehexchar('z'));
assertEquals(-1, JSONTokener.dehexchar('Z'));
}
/**
* Tests the multipleThings method.
*/
public void testMultipleThings()
{
try
{
jsontokener = new JSONTokener(
"{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}");
jsonobject = new JSONObject(jsontokener);
assertEquals("{\"to\":\"session\",\"op\":\"test\",\"pre\":1}",
jsonobject.toString());
assertEquals(1, jsonobject.optInt("pre"));
int i = jsontokener.skipTo('{');
assertEquals(123, i);
jsonobject = new JSONObject(jsontokener);
assertEquals("{\"to\":\"session\",\"op\":\"test\",\"pre\":2}",
jsonobject.toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
}