FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JSON-Java-unit-test/JSONArrayTest.java at master · teocci/JSON-Java-unit-test · GitHub
teocci
/
JSON-Java-unit-test
Public
forked from
stleary/JSON-Java-unit-test
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JSON-Java-unit-test
/
JSONArrayTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
502 lines (445 loc) · 16.1 KB
Breadcrumbs
JSON-Java-unit-test
/
JSONArrayTest.java
Copy path
File metadata and controls
502 lines (445 loc) · 16.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
org
.
json
.
junit
;
import
static
org
.
junit
.
Assert
.*;
import
java
.
util
.*;
import
org
.
json
.*;
import
org
.
junit
.
Test
;
/**
* Tests for JSON-Java JSONArray.java
*/
public
class
JSONArrayTest
{
String
arrayStr
=
"["
+
"true,"
+
"false,"
+
"
\"
true
\"
,"
+
"
\"
false
\"
,"
+
"
\"
hello
\"
,"
+
"23.45e-4,"
+
"
\"
23.45
\"
,"
+
"42,"
+
"
\"
43
\"
,"
+
"["
+
"
\"
world
\"
"
+
"],"
+
"{"
+
"
\"
key1
\"
:
\"
value1
\"
,"
+
"
\"
key2
\"
:
\"
value2
\"
,"
+
"
\"
key3
\"
:
\"
value3
\"
,"
+
"
\"
key4
\"
:
\"
value4
\"
"
+
"},"
+
"0,"
+
"
\"
-1
\"
"
+
"]"
;
@
Test
(
expected
=
NullPointerException
.
class
)
public
void
nullException
() {
String
str
=
null
;
new
JSONArray
(
str
);
}
@
Test
(
expected
=
JSONException
.
class
)
public
void
emptStr
() {
String
str
=
""
;
new
JSONArray
(
str
);
}
@
Test
(
expected
=
JSONException
.
class
)
public
void
badObject
() {
String
str
=
"abc"
;
new
JSONArray
((
Object
)
str
);
}
@
Test
public
void
getArrayValues
() {
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
assertTrue
(
"Array true"
,
true
==
jsonArray
.
getBoolean
(
0
));
assertTrue
(
"Array false"
,
false
==
jsonArray
.
getBoolean
(
1
));
assertTrue
(
"Array string true"
,
true
==
jsonArray
.
getBoolean
(
2
));
assertTrue
(
"Array string false"
,
false
==
jsonArray
.
getBoolean
(
3
));
assertTrue
(
"Array double"
,
new
Double
(
23.45e-4
).
equals
(
jsonArray
.
getDouble
(
5
)));
assertTrue
(
"Array string double"
,
new
Double
(
23.45
).
equals
(
jsonArray
.
getDouble
(
6
)));
assertTrue
(
"Array value int"
,
new
Integer
(
42
).
equals
(
jsonArray
.
getInt
(
7
)));
assertTrue
(
"Array value string int"
,
new
Integer
(
43
).
equals
(
jsonArray
.
getInt
(
8
)));
JSONArray
nestedJsonArray
=
jsonArray
.
getJSONArray
(
9
);
assertTrue
(
"Array value JSONArray"
,
nestedJsonArray
!=
null
);
JSONObject
nestedJsonObject
=
jsonArray
.
getJSONObject
(
10
);
assertTrue
(
"Array value JSONObject"
,
nestedJsonObject
!=
null
);
assertTrue
(
"Array value long"
,
new
Long
(
0
).
equals
(
jsonArray
.
getLong
(
11
)));
assertTrue
(
"Array value string long"
,
new
Long
(-
1
).
equals
(
jsonArray
.
getLong
(
12
)));
assertTrue
(
"Array value string"
,
"hello"
.
equals
(
jsonArray
.
getString
(
4
)));
assertTrue
(
"Array value null"
,
jsonArray
.
isNull
(-
1
));
}
@
Test
public
void
failedGetArrayValues
() {
int
tryCount
=
0
;
int
exceptionCount
=
0
;
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
try
{
tryCount
++;
jsonArray
.
getBoolean
(
4
);
assertTrue
(
"expected getBoolean to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
try
{
tryCount
++;
jsonArray
.
get
(-
1
);
assertTrue
(
"expected get to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
try
{
tryCount
++;
jsonArray
.
getDouble
(
4
);
assertTrue
(
"expected getDouble to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
try
{
tryCount
++;
jsonArray
.
getInt
(
4
);
assertTrue
(
"expected getInt to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
try
{
tryCount
++;
jsonArray
.
getJSONArray
(
4
);
assertTrue
(
"expected getJSONArray to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
try
{
tryCount
++;
jsonArray
.
getJSONObject
(
4
);
assertTrue
(
"expected getJSONObject to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
try
{
tryCount
++;
jsonArray
.
getLong
(
4
);
assertTrue
(
"expected getLong to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
try
{
tryCount
++;
jsonArray
.
getString
(
5
);
assertTrue
(
"expected getString to fail"
,
false
);
}
catch
(
JSONException
ignored
) {
exceptionCount
++; }
assertTrue
(
"tryCount should match exceptionCount"
,
tryCount
==
exceptionCount
);
}
@
Test
public
void
join
() {
String
expectedStr
=
"["
+
"true,"
+
"false,"
+
"
\"
true
\"
,"
+
"
\"
false
\"
,"
+
"
\"
hello
\"
,"
+
"0.002345,"
+
"
\"
23.45
\"
,"
+
"42,"
+
"
\"
43
\"
,"
+
"["
+
"
\"
world
\"
"
+
"],"
+
"{"
+
"
\"
key1
\"
:
\"
value1
\"
,"
+
"
\"
key2
\"
:
\"
value2
\"
,"
+
"
\"
key3
\"
:
\"
value3
\"
,"
+
"
\"
key4
\"
:
\"
value4
\"
"
+
"},"
+
"0,"
+
"
\"
-1
\"
"
+
"]"
;
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
String
joinStr
=
jsonArray
.
join
(
","
);
JSONArray
finalJsonArray
=
new
JSONArray
(
"["
+
joinStr
+
"]"
);
JSONArray
expectedJsonArray
=
new
JSONArray
(
expectedStr
);
Util
.
compareActualVsExpectedJsonArrays
(
jsonArray
,
expectedJsonArray
);
Util
.
compareActualVsExpectedJsonArrays
(
finalJsonArray
,
expectedJsonArray
);
}
@
Test
public
void
length
() {
assertTrue
(
"expected empty JSONArray length 0"
,
new
JSONArray
().
length
() ==
0
);
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
assertTrue
(
"expected JSONArray length 13"
,
jsonArray
.
length
() ==
13
);
JSONArray
nestedJsonArray
=
jsonArray
.
getJSONArray
(
9
);
assertTrue
(
"expected JSONArray length 1"
,
nestedJsonArray
.
length
() ==
1
);
}
@
Test
public
void
opt
() {
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
assertTrue
(
"Array opt value true"
,
Boolean
.
TRUE
==
jsonArray
.
opt
(
0
));
assertTrue
(
"Array opt value out of range"
,
null
==
jsonArray
.
opt
(-
1
));
assertTrue
(
"Array opt boolean"
,
Boolean
.
TRUE
==
jsonArray
.
optBoolean
(
0
));
assertTrue
(
"Array opt boolean default"
,
Boolean
.
FALSE
==
jsonArray
.
optBoolean
(-
1
,
Boolean
.
FALSE
));
assertTrue
(
"Array opt boolean implicit default"
,
Boolean
.
FALSE
==
jsonArray
.
optBoolean
(-
1
));
assertTrue
(
"Array opt double"
,
new
Double
(
23.45e-4
).
equals
(
jsonArray
.
optDouble
(
5
)));
assertTrue
(
"Array opt double default"
,
new
Double
(
1
).
equals
(
jsonArray
.
optDouble
(
0
,
1
)));
assertTrue
(
"Array opt double default implicit"
,
new
Double
(
jsonArray
.
optDouble
(
99
)).
isNaN
());
assertTrue
(
"Array opt int"
,
new
Integer
(
42
).
equals
(
jsonArray
.
optInt
(
7
)));
assertTrue
(
"Array opt int default"
,
new
Integer
(-
1
).
equals
(
jsonArray
.
optInt
(
0
, -
1
)));
assertTrue
(
"Array opt int default implicit"
,
0
==
jsonArray
.
optInt
(
0
));
JSONArray
nestedJsonArray
=
jsonArray
.
optJSONArray
(
9
);
assertTrue
(
"Array opt JSONArray"
,
nestedJsonArray
!=
null
);
assertTrue
(
"Array opt JSONArray default"
,
null
==
jsonArray
.
optJSONArray
(
99
));
JSONObject
nestedJsonObject
=
jsonArray
.
optJSONObject
(
10
);
assertTrue
(
"Array opt JSONObject"
,
nestedJsonObject
!=
null
);
assertTrue
(
"Array opt JSONObject default"
,
null
==
jsonArray
.
optJSONObject
(
99
));
assertTrue
(
"Array opt long"
,
0
==
jsonArray
.
optLong
(
11
));
assertTrue
(
"Array opt long default"
,
-
2
==
jsonArray
.
optLong
(-
1
, -
2
));
assertTrue
(
"Array opt long default implicit"
,
0
==
jsonArray
.
optLong
(-
1
));
assertTrue
(
"Array opt string"
,
"hello"
.
equals
(
jsonArray
.
optString
(
4
)));
assertTrue
(
"Array opt string default implicit"
,
""
.
equals
(
jsonArray
.
optString
(-
1
)));
}
@
Test
public
void
put
() {
String
expectedStr
=
"["
+
"true,"
+
"false,"
+
"["
+
"hello,"
+
"world"
+
"],"
+
"2.5,"
+
"1,"
+
"45,"
+
"
\"
objectPut
\"
,"
+
"{"
+
"
\"
key10
\"
:
\"
val10
\"
,"
+
"
\"
key20
\"
:
\"
val20
\"
,"
+
"
\"
key30
\"
:
\"
val30
\"
"
+
"},"
+
"{"
+
"
\"
k1
\"
:
\"
v1
\"
"
+
"},"
+
"["
+
"1,"
+
"2"
+
"]"
+
"]"
;
JSONArray
jsonArray
=
new
JSONArray
();
JSONArray
expectedJsonArray
=
new
JSONArray
(
expectedStr
);
// index 0
jsonArray
.
put
(
true
);
// 1
jsonArray
.
put
(
false
);
String
jsonArrayStr
=
"["
+
"hello,"
+
"world"
+
"]"
;
// 2
jsonArray
.
put
(
new
JSONArray
(
jsonArrayStr
));
// 3
jsonArray
.
put
(
2.5
);
// 4
jsonArray
.
put
(
1
);
// 5
jsonArray
.
put
(
45L
);
// 6
jsonArray
.
put
(
"objectPut"
);
String
jsonObjectStr
=
"{"
+
"
\"
key10
\"
:
\"
val10
\"
,"
+
"
\"
key20
\"
:
\"
val20
\"
,"
+
"
\"
key30
\"
:
\"
val30
\"
"
+
"}"
;
JSONObject
jsonObject
=
new
JSONObject
(
jsonObjectStr
);
// 7
jsonArray
.
put
(
jsonObject
);
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"k1"
,
"v1"
);
// 8
jsonArray
.
put
(
map
);
Collection
<
Object
>
collection
=
new
ArrayList
<
Object
>();
collection
.
add
(
1
);
collection
.
add
(
2
);
// 9
jsonArray
.
put
(
collection
);
Util
.
compareActualVsExpectedJsonArrays
(
jsonArray
,
expectedJsonArray
);
}
@
Test
public
void
putIndex
() {
String
expectedStr
=
"["
+
"true,"
+
"false,"
+
"["
+
"hello,"
+
"world"
+
"],"
+
"2.5,"
+
"1,"
+
"45,"
+
"
\"
objectPut
\"
,"
+
"null,"
+
"{"
+
"
\"
key10
\"
:
\"
val10
\"
,"
+
"
\"
key20
\"
:
\"
val20
\"
,"
+
"
\"
key30
\"
:
\"
val30
\"
"
+
"},"
+
"["
+
"1,"
+
"2"
+
"],"
+
"{"
+
"
\"
k1
\"
:
\"
v1
\"
"
+
"},"
+
"]"
;
JSONArray
jsonArray
=
new
JSONArray
();
JSONArray
expectedJsonArray
=
new
JSONArray
(
expectedStr
);
// 1
jsonArray
.
put
(
1
,
false
);
// index 0
jsonArray
.
put
(
0
,
true
);
String
jsonArrayStr
=
"["
+
"hello,"
+
"world"
+
"]"
;
// 2
jsonArray
.
put
(
2
,
new
JSONArray
(
jsonArrayStr
));
// 5
jsonArray
.
put
(
5
,
45L
);
// 4
jsonArray
.
put
(
4
,
1
);
// 3
jsonArray
.
put
(
3
,
2.5
);
// 6
jsonArray
.
put
(
6
,
"objectPut"
);
// 7 will be null
String
jsonObjectStr
=
"{"
+
"
\"
key10
\"
:
\"
val10
\"
,"
+
"
\"
key20
\"
:
\"
val20
\"
,"
+
"
\"
key30
\"
:
\"
val30
\"
"
+
"}"
;
JSONObject
jsonObject
=
new
JSONObject
(
jsonObjectStr
);
jsonArray
.
put
(
8
,
jsonObject
);
Collection
<
Object
>
collection
=
new
ArrayList
<
Object
>();
collection
.
add
(
1
);
collection
.
add
(
2
);
jsonArray
.
put
(
9
,
collection
);
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"k1"
,
"v1"
);
jsonArray
.
put
(
10
,
map
);
try
{
jsonArray
.
put
(-
1
,
"abc"
);
assertTrue
(
"put index < 0 should have thrown exception"
,
false
);
}
catch
(
Exception
ignored
) {}
Util
.
compareActualVsExpectedJsonArrays
(
jsonArray
,
expectedJsonArray
);
}
@
Test
public
void
remove
() {
String
arrayStr
=
"["
+
"1"
+
"]"
;
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
JSONArray
expectedJsonArray
=
new
JSONArray
();
jsonArray
.
remove
(
0
);
assertTrue
(
"array should be empty"
,
null
==
jsonArray
.
remove
(
5
));
Util
.
compareActualVsExpectedJsonArrays
(
jsonArray
,
expectedJsonArray
);
}
@
Test
public
void
notSimilar
() {
String
arrayStr
=
"["
+
"1"
+
"]"
;
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
JSONArray
otherJsonArray
=
new
JSONArray
();
assertTrue
(
"arrays lengths differ"
, !
jsonArray
.
similar
(
otherJsonArray
));
JSONObject
jsonObject
=
new
JSONObject
(
"{
\"
k1
\"
:
\"
v1
\"
}"
);
JSONObject
otherJsonObject
=
new
JSONObject
();
jsonArray
=
new
JSONArray
();
jsonArray
.
put
(
jsonObject
);
otherJsonArray
=
new
JSONArray
();
otherJsonArray
.
put
(
otherJsonObject
);
assertTrue
(
"arrays JSONObjects differ"
, !
jsonArray
.
similar
(
otherJsonArray
));
JSONArray
nestedJsonArray
=
new
JSONArray
(
"[1, 2]"
);
JSONArray
otherNestedJsonArray
=
new
JSONArray
();
jsonArray
=
new
JSONArray
();
jsonArray
.
put
(
nestedJsonArray
);
otherJsonArray
=
new
JSONArray
();
otherJsonArray
.
put
(
otherNestedJsonArray
);
assertTrue
(
"arrays nested JSONArrays differ"
,
!
jsonArray
.
similar
(
otherJsonArray
));
jsonArray
=
new
JSONArray
();
jsonArray
.
put
(
"hello"
);
otherJsonArray
=
new
JSONArray
();
otherJsonArray
.
put
(
"world"
);
assertTrue
(
"arrays values differ"
,
!
jsonArray
.
similar
(
otherJsonArray
));
}
@
Test
public
void
toJSONObject
() {
JSONArray
names
=
new
JSONArray
();
JSONArray
jsonArray
=
new
JSONArray
();
assertTrue
(
"toJSONObject should return null"
,
null
==
jsonArray
.
toJSONObject
(
names
));
}
@
Test
public
void
objectArrayVsIsArray
() {
String
expectedStr
=
"["
+
"1,2,3,4,5,6,7"
+
"]"
;
int
[]
myInts
= {
1
,
2
,
3
,
4
,
5
,
6
,
7
};
Object
myObject
=
myInts
;
JSONArray
jsonArray
=
new
JSONArray
(
myObject
);
JSONArray
expectedJsonArray
=
new
JSONArray
(
expectedStr
);
Util
.
compareActualVsExpectedJsonArrays
(
jsonArray
,
expectedJsonArray
);
}
@
Test
public
void
iterator
() {
JSONArray
jsonArray
=
new
JSONArray
(
arrayStr
);
Iterator
<
Object
>
it
=
jsonArray
.
iterator
();
assertTrue
(
"Array true"
,
Boolean
.
TRUE
.
equals
(
it
.
next
()));
assertTrue
(
"Array false"
,
Boolean
.
FALSE
.
equals
(
it
.
next
()));
assertTrue
(
"Array string true"
,
"true"
.
equals
(
it
.
next
()));
assertTrue
(
"Array string false"
,
"false"
.
equals
(
it
.
next
()));
assertTrue
(
"Array string"
,
"hello"
.
equals
(
it
.
next
()));
assertTrue
(
"Array double"
,
new
Double
(
23.45e-4
).
equals
(
it
.
next
()));
assertTrue
(
"Array string double"
,
new
Double
(
23.45
).
equals
(
Double
.
parseDouble
((
String
)
it
.
next
())));
assertTrue
(
"Array value int"
,
new
Integer
(
42
).
equals
(
it
.
next
()));
assertTrue
(
"Array value string int"
,
new
Integer
(
43
).
equals
(
Integer
.
parseInt
((
String
)
it
.
next
())));
JSONArray
nestedJsonArray
= (
JSONArray
)
it
.
next
();
assertTrue
(
"Array value JSONArray"
,
nestedJsonArray
!=
null
);
JSONObject
nestedJsonObject
= (
JSONObject
)
it
.
next
();
assertTrue
(
"Array value JSONObject"
,
nestedJsonObject
!=
null
);
assertTrue
(
"Array value long"
,
new
Long
(
0
).
equals
(((
Number
)
it
.
next
()).
longValue
()));
assertTrue
(
"Array value string long"
,
new
Long
(-
1
).
equals
(
Long
.
parseLong
((
String
)
it
.
next
())));
assertTrue
(
"should be at end of array"
, !
it
.
hasNext
());
}
}
Back
|
FazBrowse Home
|
New Git URL