-
Notifications
You must be signed in to change notification settings - Fork 59
/
ClientTests.java
1408 lines (1291 loc) · 54.2 KB
/
ClientTests.java
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.jsonp.tck.api.jsonarraytests;
import jakarta.jsonp.tck.api.common.TestResult;
import jakarta.jsonp.tck.common.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import jakarta.json.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
//$Id$
public class ClientTests {
private static final Logger LOGGER = Logger.getLogger(ClientTests.class.getName());
/* Tests */
/*
* @testName: jsonArrayTest1
*
* @assertion_ids: JSONP:JAVADOC:6; JSONP:JAVADOC:8; JSONP:JAVADOC:10;
* JSONP:JAVADOC:12; JSONP:JAVADOC:14; JSONP:JAVADOC:16; JSONP:JAVADOC:18;
* JSONP:JAVADOC:25; JSONP:JAVADOC:21; JSONP:JAVADOC:400; JSONP:JAVADOC:401;
* JSONP:JAVADOC:402; JSONP:JAVADOC:403; JSONP:JAVADOC:404; JSONP:JAVADOC:406;
* JSONP:JAVADOC:408; JSONP:JAVADOC:409;
*
* @test_Strategy: Tests JsonArray/JsonArrayBuilder API's. Build a JsonArray
* using the JsonArrayBuilder API's then verify that the list of JsonArray
* values matches the expected list of JsonArray values.
*/
@Test
public void jsonArrayTest1() {
try {
LOGGER.info("Create sample JsonObject for testing");
JsonObject object = JSONP_Util.createSampleJsonObject();
LOGGER.info("Create sample JsonArray for testing");
JsonArray array = JSONP_Util.createSampleJsonArray();
LOGGER.info("Create the expected list of JsonArray values");
List<JsonValue> expList = new ArrayList<>();
expList.add(JsonValue.FALSE);
expList.add(JsonValue.TRUE);
expList.add(JsonValue.NULL);
expList.add(JSONP_Util.createJsonNumber(Double.MIN_VALUE));
expList.add(JSONP_Util.createJsonNumber(Double.MAX_VALUE));
expList.add(JSONP_Util.createJsonNumber(Integer.MIN_VALUE));
expList.add(JSONP_Util.createJsonNumber(Integer.MAX_VALUE));
expList.add(JSONP_Util.createJsonNumber(Long.MIN_VALUE));
expList.add(JSONP_Util.createJsonNumber(Long.MAX_VALUE));
expList.add(
JSONP_Util.createJsonNumber(BigDecimal.valueOf(123456789.123456789)));
expList.add(JSONP_Util.createJsonNumber(new BigInteger("123456789")));
expList.add(JSONP_Util.createJsonString("string1"));
expList.add(object);
expList.add(array);
JSONP_Util.dumpList(expList, "Expected List");
LOGGER.info("Create JsonArray using all JsonArrayBuilder API's");
JsonArray myJsonArray = Json.createArrayBuilder() // Indices
.add(JsonValue.FALSE) // 0
.add(JsonValue.TRUE) // 1
.add(JsonValue.NULL) // 2
.add(Double.MIN_VALUE) // 3
.add(Double.MAX_VALUE) // 4
.add(Integer.MIN_VALUE) // 5
.add(Integer.MAX_VALUE) // 6
.add(Long.MIN_VALUE) // 7
.add(Long.MAX_VALUE) // 8
.add(BigDecimal.valueOf(123456789.123456789)) // 9
.add(new BigInteger("123456789")) // 10
.add("string1") // 11
.add(object) // 12
.add(array) // 13
.build();
List<JsonValue> actualList = myJsonArray;
JSONP_Util.dumpList(actualList, "Actual List");
LOGGER.info(
"Compare actual list of JsonArray values with expected list of JsonArray values");
assertTrue(JSONP_Util.assertEqualsList(expList, actualList), "jsonArrayTest1 Failed");
} catch (Exception e) {
fail("jsonArrayTest1 Failed: ", e);
}
}
/*
* @testName: jsonArrayTest2
*
* @assertion_ids: JSONP:JAVADOC:6; JSONP:JAVADOC:8; JSONP:JAVADOC:10;
* JSONP:JAVADOC:12; JSONP:JAVADOC:14; JSONP:JAVADOC:16; JSONP:JAVADOC:18;
* JSONP:JAVADOC:105; JSONP:JAVADOC:106; JSONP:JAVADOC:108; JSONP:JAVADOC:96;
* JSONP:JAVADOC:97; JSONP:JAVADOC:21; JSONP:JAVADOC:25; JSONP:JAVADOC:184;
* JSONP:JAVADOC:400; JSONP:JAVADOC:401; JSONP:JAVADOC:402; JSONP:JAVADOC:403;
* JSONP:JAVADOC:404; JSONP:JAVADOC:406; JSONP:JAVADOC:408; JSONP:JAVADOC:409;
*
*
* @test_Strategy: Tests JsonArray/JsonArrayBuilder API's. Build a JsonArray
* using the JsonArrayBuilder API's. Write the JsonArray to a JsonWriter and
* read it back using a JsonReader. Verify that JsonArray written to the
* JsonWriter and then read back using the JsonReader are equal.
*/
@Test
public void jsonArrayTest2() {
try {
LOGGER.info("Create sample JsonObject for testing");
JsonObject object = JSONP_Util.createSampleJsonObject();
LOGGER.info("Create sample JsonArray for testing");
JsonArray array = JSONP_Util.createSampleJsonArray();
LOGGER.info(
"Create JsonArray 'myJsonArray1' using all JsonArrayBuilder API's");
JsonArray myJsonArray1 = Json.createArrayBuilder() // Indices
.add(JsonValue.FALSE) // 0
.add(JsonValue.TRUE) // 1
.add(JsonValue.NULL) // 2
.add(Double.MIN_VALUE) // 3
.add(Double.MAX_VALUE) // 4
.add(Integer.MIN_VALUE) // 5
.add(Integer.MAX_VALUE) // 6
.add(Long.MIN_VALUE) // 7
.add(Long.MAX_VALUE) // 8
.add(BigDecimal.valueOf(123456789.123456789)) // 9
.add(new BigInteger("123456789")) // 10
.add("string1") // 11
.add(object) // 12
.add(array) // 13
.build();
LOGGER.info("Write the JsonArray 'myJsonArray1' out to a JsonWriter");
StringWriter sw = new StringWriter();
try (JsonWriter writer = Json.createWriter(sw)) {
writer.writeArray(myJsonArray1);
LOGGER.info("Close JsonWriter");
}
LOGGER.info("Save contents of the JsonWriter as a String");
String contents = sw.toString();
LOGGER.info("Dump contents of JsonWriter as a String");
LOGGER.info("JsonWriterContents=" + contents);
LOGGER.info("Read the JsonArray back into 'myJsonArray2' using a JsonReader");
JsonArray myJsonArray2;
try (JsonReader reader = Json.createReader(new StringReader(contents))) {
myJsonArray2 = reader.readArray();
}
LOGGER.info("Dump contents of JsonArray read from String Contents");
JSONP_Util.dumpJsonValue(myJsonArray2);
LOGGER.info("Compare myJsonArray1 and myJsonArray2 for equality");
assertTrue(JSONP_Util.assertEqualsJsonArrays(myJsonArray1, myJsonArray2), "jsonArrayTest2 Failed");
} catch (Exception e) {
fail("jsonArrayTest2 Failed: ", e);
}
}
/*
* @testName: jsonArrayTest3
*
* @assertion_ids: JSONP:JAVADOC:6; JSONP:JAVADOC:8; JSONP:JAVADOC:10;
* JSONP:JAVADOC:430; JSONP:JAVADOC:12; JSONP:JAVADOC:14; JSONP:JAVADOC:16;
* JSONP:JAVADOC:18; JSONP:JAVADOC:40; JSONP:JAVADOC:41; JSONP:JAVADOC:42;
* JSONP:JAVADOC:44; JSONP:JAVADOC:45; JSONP:JAVADOC:46; JSONP:JAVADOC:48;
* JSONP:JAVADOC:49; JSONP:JAVADOC:51; JSONP:JAVADOC:21; JSONP:JAVADOC:25;
* JSONP:JAVADOC:101; JSONP:JAVADOC:102; JSONP:JAVADOC:262; JSONP:JAVADOC:263;
* JSONP:JAVADOC:400; JSONP:JAVADOC:401; JSONP:JAVADOC:402; JSONP:JAVADOC:403;
* JSONP:JAVADOC:404; JSONP:JAVADOC:406; JSONP:JAVADOC:408; JSONP:JAVADOC:409;
* JSONP:JAVADOC:433; JSONP:JAVADOC:434; JSONP:JAVADOC:435; JSONP:JAVADOC:490;
* JSONP:JAVADOC:493; JSONP:JAVADOC:496; JSONP:JAVADOC:499; JSONP:JAVADOC:506;
*
* @test_Strategy: Tests JsonArray/JsonArrayBuilder API's. Build a JsonArray
* using the JsonArrayBuilder API's. Verify contents of JsonArray using
* JsonArray().getJsonNumber(int), JsonArray().getJsonString(int),
* JsonArray().getJsonArray(int), JsonArray().getJsonObject().
*
* This also covers testing the following additional API's:
*
* JsonString.getString(), JsonNumber.bigDecimalValue(),
* JsonNumber.bigIntegerValue(), JsonNumber.doubleValue(),
* JsonNumber.intValue(), JsonNumber.longValue(), JsonNumber.isIntegral(),
* JsonNumber.longValueExact(), JsonNumber.intValueExact(),
* JsonNumber.bigIntegerValueExact(), JsonArray.getInt(int),
* JsonArray.getString(int), JsonArrau.getBoolean(int),
* JsonArray.getBoolean(int, boolean), JsonArray.getInt(int, int),
* JsonArray.getString(int, String)
*/
@SuppressWarnings("SuspiciousIndentAfterControlStatement")
@Test
public void jsonArrayTest3() {
boolean pass = true;
try {
LOGGER.info("Create sample JsonObject for testing");
JsonObject object = JSONP_Util.createSampleJsonObject();
LOGGER.info("Create sample JsonArray for testing");
JsonArray array = JSONP_Util.createSampleJsonArray();
int expInt[] = { -1, 1, 1, -1000, 1000, 1000, -2000, 2000, 2000,
Integer.MAX_VALUE, Integer.MIN_VALUE };
long expLong[] = { Long.MAX_VALUE, Long.MIN_VALUE };
double expDouble[] = { Double.MAX_VALUE, Double.MIN_VALUE };
LOGGER.info("Create myArray Jsonarray of 23 elements");
JsonArray myArray = Json.createArrayBuilder() // Indices
.add(-1).add(+1).add(1) // 0,1,2
.add(-1e3).add(+1e3).add(1e3) // 3,4,5
.add(-2E3).add(+2E3).add(2E3) // 6,7,8
.add(Integer.MAX_VALUE) // 9
.add(Integer.MIN_VALUE) // 10
.add(Long.MAX_VALUE) // 11
.add(Long.MIN_VALUE) // 12
.add(Double.MAX_VALUE) // 13
.add(Double.MIN_VALUE) // 14
.add(BigDecimal.valueOf(123456789.123456789)) // 15
.add(new BigInteger("123456789")) // 16
.add(JsonValue.TRUE) // 17
.add(JsonValue.FALSE) // 18
.add(JsonValue.NULL) // 19
.add(JSONP_Data.asciiCharacters) // 20
.add(object) // 21
.add(array) // 22
.build();
LOGGER.info("Array size=" + myArray.size());
// Following array is used to test for Ints that could be one of following
// types:
boolean expectedIntTypes[] = { JSONP_Util.INTEGRAL,
JSONP_Util.NON_INTEGRAL };
// Verify JsonValueType=NUMBER and integer value equals expectedIntValue
for (int i = 0; i < 11; i++) {
LOGGER.info("Checking getValue(" + i + ") for correctness");
LOGGER.info("Retrieve and verify (JsonValueType=NUMBER)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.NUMBER,
myArray.getJsonNumber(i).getValueType()))
pass = false;
LOGGER.info("Retrieve and (expect JsonNumber NumberType be one of "
+ JSONP_Util.toStringJsonNumberTypes(expectedIntTypes) + ")");
if (!JSONP_Util.assertEqualsJsonNumberTypes(expectedIntTypes,
myArray.getJsonNumber(i).isIntegral()))
pass = false;
LOGGER.info("Retrieve and verify integer value via JsonNumber.intValue()");
if (!JSONP_Util.assertEquals(expInt[i],
myArray.getJsonNumber(i).intValue()))
pass = false;
LOGGER.info("Retrieve and verify integer value via JsonArray.getInt");
if (!JSONP_Util.assertEquals(expInt[i], myArray.getInt(i)))
pass = false;
LOGGER.info(
"Retrieve and verify integer value via JsonNumber.intValueExact()");
if (!JSONP_Util.assertEquals(expInt[i],
myArray.getJsonNumber(i).intValueExact()))
pass = false;
}
// Verify JsonValueType=NUMBER and long value equals expectedLongValue
for (int i = 11, j = 0; i < 13; i++, j++) {
LOGGER.info("Checking getValue(" + i + ") for correctness");
LOGGER.info("Retrieve and verify (JsonValueType=NUMBER)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.NUMBER,
myArray.getJsonNumber(i).getValueType()))
pass = false;
LOGGER.info("Retrieve and (expect JsonNumber NumberType be INTEGRAL)");
if (!JSONP_Util.assertEqualsJsonNumberType(JSONP_Util.INTEGRAL,
myArray.getJsonNumber(i).isIntegral()))
pass = false;
LOGGER.info("Retrieve and verify long value via JsonNumber.longValue()");
if (!JSONP_Util.assertEquals(expLong[j],
myArray.getJsonNumber(i).longValue()))
pass = false;
LOGGER.info(
"Retrieve and verify long value via JsonNumber.longValueExact()");
if (!JSONP_Util.assertEquals(expLong[j],
myArray.getJsonNumber(i).longValueExact()))
pass = false;
}
// Following array is used to test for Doubles that could be one of
// following types:
boolean expectedDoubleTypes[] = { JSONP_Util.INTEGRAL,
JSONP_Util.NON_INTEGRAL };
// Verify JsonValueType=NUMBER and double value equals expectedDoubleValue
for (int i = 13, j = 0; i < 15; i++, j++) {
LOGGER.info("Checking getValue(" + i + ") for correctness");
LOGGER.info("Retrieve and verify (JsonValueType=NUMBER)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.NUMBER,
myArray.getJsonNumber(i).getValueType()))
pass = false;
LOGGER.info("Retrieve and (expect JsonNumber NumberType be one of "
+ JSONP_Util.toStringJsonNumberTypes(expectedDoubleTypes) + ")");
if (!JSONP_Util.assertEqualsJsonNumberTypes(expectedDoubleTypes,
myArray.getJsonNumber(i).isIntegral()))
pass = false;
LOGGER.info("Retrieve and verify double value via JsonNumber.doubleValue()");
if (!JSONP_Util.assertEquals(expDouble[j],
myArray.getJsonNumber(i).doubleValue()))
pass = false;
}
// Verify JsonValueType=NUMBER and BigDecimalValue equals
// expectedBigDecimal
LOGGER.info("Checking getValue(15) for correctness");
LOGGER.info("Retrieve and verify (JsonValueType=NUMBER)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.NUMBER,
myArray.getJsonNumber(15).getValueType()))
pass = false;
LOGGER.info("Retrieve and (expect JsonNumber NumberType be one of "
+ JSONP_Util.toStringJsonNumberTypes(expectedDoubleTypes) + ")");
if (!JSONP_Util.assertEqualsJsonNumberTypes(expectedDoubleTypes,
myArray.getJsonNumber(15).isIntegral()))
pass = false;
LOGGER.info(
"Retrieve and verify BigDecimal value via JsonNumber.bigDecimalValue()");
if (!JSONP_Util.assertEquals(BigDecimal.valueOf(123456789.123456789),
myArray.getJsonNumber(15).bigDecimalValue()))
pass = false;
// Verify JsonValueType=NUMBER and BigIntegerValue equals
// expectedBigInteger
LOGGER.info("Checking getValue(16) for correctness");
LOGGER.info("Retrieve and verify (JsonValueType=NUMBER)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.NUMBER,
myArray.getJsonNumber(16).getValueType()))
pass = false;
LOGGER.info("Retrieve and (expect JsonNumber NumberType be INTEGRAL)");
if (!JSONP_Util.assertEqualsJsonNumberType(JSONP_Util.INTEGRAL,
myArray.getJsonNumber(16).isIntegral()))
pass = false;
LOGGER.info(
"Retrieve and verify BigInteger value via JsonNumber.bigIntegerValue()");
if (!JSONP_Util.assertEquals(new BigInteger("123456789"),
myArray.getJsonNumber(16).bigIntegerValue()))
pass = false;
LOGGER.info(
"Retrieve and verify BigInteger value via JsonNumber.bigIntegerValueExact()");
if (!JSONP_Util.assertEquals(new BigInteger("123456789"),
myArray.getJsonNumber(16).bigIntegerValueExact()))
pass = false;
// Verify getBoolean(int)=true
LOGGER.info("Retrieve and verify true value via JsonArray.getBoolean(int)");
if (!JSONP_Util.assertEquals(true, myArray.getBoolean(17)))
pass = false;
// Verify getBoolean(int)=false
LOGGER.info("Retrieve and verify false value via JsonArray.getBoolean(int)");
if (!JSONP_Util.assertEquals(false, myArray.getBoolean(18)))
pass = false;
// Verify isNull(int)=true
LOGGER.info("Retrieve and verify null value via JsonArray.isNull(int)");
if (!JSONP_Util.assertEquals(true, myArray.isNull(19)))
pass = false;
// Verify isNull(int)=false
LOGGER.info("Retrieve and verify non-null value via JsonArray.isNull(int)");
if (!JSONP_Util.assertEquals(false, myArray.isNull(20)))
pass = false;
// Verify JsonValueType=STRING and getJsonString()=expectedString
LOGGER.info("Checking getValue(20) for correctness");
LOGGER.info("Retrieve and (expect JsonValueType=STRING)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.STRING,
myArray.getJsonString(20).getValueType()))
pass = false;
LOGGER.info("Retrieve and verify string value via JsonString.getString()");
if (!JSONP_Util.assertEquals(JSONP_Data.asciiCharacters,
myArray.getJsonString(20).getString()))
pass = false;
LOGGER.info("Retrieve and verify string value via JsonArray.getString()");
if (!JSONP_Util.assertEquals(JSONP_Data.asciiCharacters,
myArray.getString(20)))
pass = false;
// Verify JsonValueType=OBJECT and getJsonObject()=expectedObject
LOGGER.info("Checking getJsonObject(21) for correctness");
LOGGER.info("Retrieve and (expect JsonValueType=OBJECT)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.OBJECT,
myArray.getJsonObject(21).getValueType()))
pass = false;
LOGGER.info(
"Retrieve and verify object value via JsonArray.getJsonObject(int)");
if (!JSONP_Util.assertEqualsJsonObjects(object,
myArray.getJsonObject(21)))
pass = false;
// Verify JsonValueType=ARRAY and getJsonArray()=expectedArray
LOGGER.info("Checking getJsonArray(22) for correctness");
LOGGER.info("Retrieve and (expect JsonValueType=ARRAY)");
if (!JSONP_Util.assertEqualsJsonValueType(JsonValue.ValueType.ARRAY,
myArray.getJsonArray(22).getValueType()))
pass = false;
LOGGER.info("Retrieve and verify array value via JsonArray.getJsonArray(int)");
if (!JSONP_Util.assertEqualsJsonArrays(array, myArray.getJsonArray(22)))
pass = false;
// Verify calls to JsonArray.getBoolean(int)
if (!JSONP_Util.assertEquals(true, myArray.getBoolean(17)))
pass = false;
if (!JSONP_Util.assertEquals(false, myArray.getBoolean(18)))
pass = false;
// Verify calls to JsonArray.getBoolean(int, boolean)
LOGGER.info(
"Testing JsonArray.getBoolean(int, boolean) with/without default value setting.");
if (!JSONP_Util.assertEquals(true, myArray.getBoolean(17, false)))
pass = false;
if (!JSONP_Util.assertEquals(false, myArray.getBoolean(0, false)))
pass = false;
if (!JSONP_Util.assertEquals(false, myArray.getBoolean(19, false)))
pass = false;
if (!JSONP_Util.assertEquals(false, myArray.getBoolean(20, false)))
pass = false;
if (!JSONP_Util.assertEquals(false, myArray.getBoolean(21, false)))
pass = false;
if (!JSONP_Util.assertEquals(false, myArray.getBoolean(22, false)))
pass = false;
// Verify calls to JsonArray.getInt(int, int)
LOGGER.info(
"Testing JsonArray.getInt(int, int) with/without default value setting.");
if (!JSONP_Util.assertEquals(-1, myArray.getInt(0, 10)))
pass = false;
if (!JSONP_Util.assertEquals(10, myArray.getInt(17, 10)))
pass = false;
if (!JSONP_Util.assertEquals(10, myArray.getInt(19, 10)))
pass = false;
if (!JSONP_Util.assertEquals(10, myArray.getInt(20, 10)))
pass = false;
if (!JSONP_Util.assertEquals(10, myArray.getInt(21, 10)))
pass = false;
if (!JSONP_Util.assertEquals(10, myArray.getInt(22, 10)))
pass = false;
// Verify calls to JsonArray.getString(int, String)
LOGGER.info(
"Testing JsonArray.getString(int, String) with/without default value setting.");
if (!JSONP_Util.assertEquals(JSONP_Data.asciiCharacters,
myArray.getString(20, "foo")))
pass = false;
if (!JSONP_Util.assertEquals("foo", myArray.getString(17, "foo")))
pass = false;
if (!JSONP_Util.assertEquals("foo", myArray.getString(19, "foo")))
pass = false;
if (!JSONP_Util.assertEquals("foo", myArray.getString(2, "foo")))
pass = false;
if (!JSONP_Util.assertEquals("foo", myArray.getString(21, "foo")))
pass = false;
if (!JSONP_Util.assertEquals("foo", myArray.getString(22, "foo")))
pass = false;
} catch (Exception e) {
fail("jsonArrayTest3 Failed: ", e);
}
assertTrue(pass, "jsonArrayTest3 Failed");
}
/*
* @testName: jsonArrayTest4
*
* @assertion_ids: JSONP:JAVADOC:6; JSONP:JAVADOC:8; JSONP:JAVADOC:14;
* JSONP:JAVADOC:16; JSONP:JAVADOC:18; JSONP:JAVADOC:21; JSONP:JAVADOC:25;
* JSONP:JAVADOC:106; JSONP:JAVADOC:400; JSONP:JAVADOC:401; JSONP:JAVADOC:402;
* JSONP:JAVADOC:403; JSONP:JAVADOC:404; JSONP:JAVADOC:406; JSONP:JAVADOC:409;
*
* @test_Strategy: Build a JsonArray and than write the JsonArray. Compare the
* Json text from the writer contents with the expected Json text output
* expected based on the JsonArray.
*/
@Test
public void jsonArrayTest4() {
try {
LOGGER.info("Create sample JsonArray for testing");
JsonArray myJsonArray1 = JSONP_Util.createSampleJsonArray();
LOGGER.info("Write the JsonArray 'myJsonArray1' out to a JsonWriter");
StringWriter sw = new StringWriter();
try (JsonWriter writer = Json.createWriter(sw)) {
writer.writeArray(myJsonArray1);
LOGGER.info("Close JsonWriter");
}
LOGGER.info("Save contents of the JsonWriter as a String");
String contents = sw.toString();
LOGGER.info("Dump contents of JsonWriter as a String");
LOGGER.info("JsonWriterContents=" + contents);
LOGGER.info("Remove whitespace from contents.");
String actJsonText = JSONP_Util.removeWhitespace(contents);
LOGGER.info(
"Compare expected JsonArray text with actual JsonArray text for equality");
assertTrue(
JSONP_Util.assertEqualsJsonText(JSONP_Util.EXPECTED_SAMPLEJSONARRAY_TEXT, actJsonText),
"jsonArrayTest4 Failed");
} catch (Exception e) {
fail("jsonArrayTest4 Failed: ", e);
}
}
/*
* @testName: jsonArrayGetValuesAsTest
*
* @assertion_ids: JSONP:JAVADOC:403; JSONP:JAVADOC:481; JSONP:JAVADOC:8;
* JSONP:JAVADOC:14;
*
* @test_Strategy: Build a Json array of values of the same type. Get the
* values as a list for that type. Compare results in list to what is expected
* for equality.
*
* APIs called: JsonArray array = Json.createArrayBuilder().add(...).build()
* List<T> JsonArray.getValuesAs(Class)
*/
@SuppressWarnings("SuspiciousIndentAfterControlStatement")
@Test
public void jsonArrayGetValuesAsTest() {
boolean pass = true;
try {
LOGGER.info("Create sample JsonArray of JsonNumber types for testing");
JsonArray jsonArr = Json.createArrayBuilder().add(100).add(500).build();
LOGGER.info("Create the expected list of JsonArray values");
List<JsonValue> expList = new ArrayList<>();
expList.add(JSONP_Util.createJsonNumber(100));
expList.add(JSONP_Util.createJsonNumber(500));
JSONP_Util.dumpList(expList, "Expected List");
LOGGER.info("Create the JsonNumber list of JsonArray values");
List<JsonNumber> numList = jsonArr.getValuesAs(JsonNumber.class);
LOGGER.info("Create the actual list of JsonArray values");
List<JsonValue> actList = new ArrayList<>();
for (JsonNumber num : numList)
actList.add(num);
LOGGER.info("Compare actual list with expected list for equality");
pass = JSONP_Util.assertEqualsList(expList, actList);
LOGGER.info("Create sample JsonArray of JsonString types for testing");
jsonArr = Json.createArrayBuilder().add("hello").add("world").build();
LOGGER.info("Create the list of JsonString values");
List<JsonString> strList = jsonArr.getValuesAs(JsonString.class);
LOGGER.info("Comparing JsonString list elements to expected values.");
if (!JSONP_Util.assertEquals(jsonArr.getString(0),
strList.get(0).getString()))
pass = false;
if (!JSONP_Util.assertEquals(jsonArr.getString(1),
strList.get(1).getString()))
pass = false;
} catch (Exception e) {
fail("jsonArrayGetValuesAsTest Failed: ", e);
}
assertTrue(pass, "jsonArrayGetValuesAsTest Failed");
}
/*
* @testName: jsonArrayExceptionTests
*
* @assertion_ids: JSONP:JAVADOC:43; JSONP:JAVADOC:47; JSONP:JAVADOC:50;
* JSONP:JAVADOC:20; JSONP:JAVADOC:377; JSONP:JAVADOC:432; JSONP:JAVADOC:379;
* JSONP:JAVADOC:378; JSONP:JAVADOC:380; JSONP:JAVADOC:431; JSONP:JAVADOC:400;
* JSONP:JAVADOC:401; JSONP:JAVADOC:402; JSONP:JAVADOC:403; JSONP:JAVADOC:404;
* JSONP:JAVADOC:406; JSONP:JAVADOC:408; JSONP:JAVADOC:409; JSONP:JAVADOC:491;
* JSONP:JAVADOC:492; JSONP:JAVADOC:494; JSONP:JAVADOC:495; JSONP:JAVADOC:497;
* JSONP:JAVADOC:498; JSONP:JAVADOC:500; JSONP:JAVADOC:501; JSONP:JAVADOC:507;
*
* @test_Strategy: Test JsonArray exception conditions. Trips the exceptions:
* java.lang.IndexOutOfBoundsException java.lang.ArithmeticException
* java.lang.NumberFormatException java.lang.ClassCastException
* java.lang.UnsupportedOperationException
*/
@Test
public void jsonArrayExceptionTests() {
boolean pass = true;
JsonObject testObject = null;
JsonArray testArray = null;
try {
LOGGER.info("Create sample JsonObject for testing");
testObject = JSONP_Util.createSampleJsonObject();
LOGGER.info("Create sample JsonArray for testing");
testArray = JSONP_Util.createSampleJsonArray();
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonObject to JsonNumber via getJsonNumber(int)");
JsonNumber value = testArray.getJsonNumber(0);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonArray to JsonNumber via getJsonNumber(int)");
JsonNumber value = testArray.getJsonNumber(15);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonNumber to JsonString via getJsonString(int)");
JsonString value = testArray.getJsonString(4);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonString to JsonNumber via getJsonNumber(int)");
JsonNumber value = testArray.getJsonNumber(6);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonValue.TRUE to JsonNumber via getJsonNumber(int)");
JsonNumber value = testArray.getJsonNumber(1);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonObject to JsonArray via getJsonArray(int)");
JsonArray value = testArray.getJsonArray(0);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonArray to JsonObject via getJsonObject(int)");
JsonObject value = testArray.getJsonObject(15);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonObject to JsonNumber via getInt(int)");
int value = testArray.getInt(0);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonObject to JsonString via getString(int)");
String value = testArray.getString(0);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonArray to JsonString via getString(int)");
String value = testArray.getString(15);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonObject to boolean via getBoolean(int)");
boolean value = testArray.getBoolean(0);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonArray to boolean via getBoolean(int)");
boolean value = testArray.getBoolean(13);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonString to boolean via getBoolean(int)");
boolean value = testArray.getBoolean(6);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip ClassCastException
try {
LOGGER.info(
"Trip ClassCastException trying to cast a JsonNumber to boolean via getBoolean(int)");
boolean value = testArray.getBoolean(4);
pass = false;
LOGGER.warning("Failed to throw ClassCastException");
} catch (ClassCastException e) {
LOGGER.info("Got expected ClassCastException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing -1 as index to getJsonNumber(int)");
int myInt = testArray.getJsonNumber(-1).intValue();
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing 10000 as index to getJsonNumber(int)");
JsonValue myJsonValue = testArray.getJsonNumber(10000);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing -1 as index to getJsonArray(int)");
JsonValue myJsonValue = testArray.getJsonArray(-1);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing 10000 as index to getJsonArray(int)");
JsonValue myJsonValue = testArray.getJsonArray(10000);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing -1 as index to getJsonObject(int)");
JsonValue myJsonValue = testArray.getJsonObject(-1);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing 10000 as index to getJsonObject(int)");
JsonValue myJsonValue = testArray.getJsonObject(10000);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing -1 as index to getJsonString(int)");
JsonValue myJsonValue = testArray.getJsonString(-1);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing 10000 as index to getJsonString(int)");
JsonValue myJsonValue = testArray.getJsonString(10000);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing -1 as index to getInt(int)");
int myInt = testArray.getInt(-1);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing 10000 as index to getInt(int)");
int myInt = testArray.getInt(10000);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing -1 as index to getString(int)");
String myString = testArray.getString(-1);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing 10000 as index to getString(int)");
String myString = testArray.getString(10000);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing -1 as index to getBoolean(int)");
boolean myBoolean = testArray.getBoolean(-1);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
LOGGER.info("Got expected IndexOutOfBoundsException");
} catch (Exception e) {
pass = false;
LOGGER.warning("Caught unexpected exception: " + e);
}
// Trip IndexOutOfBoundsException
try {
LOGGER.info(
"Trip IndexOutOfBoundsException passing 10000 as index to getBoolean(int)");
boolean myBoolean = testArray.getBoolean(10000);
pass = false;
LOGGER.warning("Failed to throw IndexOutOfBoundsException");