FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JSON-java/src/test/java/org/json/junit/XMLTest.java at master · rajihul/JSON-java · GitHub
rajihul
/
JSON-java
Public
forked from
stleary/JSON-java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JSON-java
/
src
/
test
/
java
/
org
/
json
/
junit
/
XMLTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
1071 lines (978 loc) · 44.7 KB
Breadcrumbs
JSON-java
/
src
/
test
/
java
/
org
/
json
/
junit
/
XMLTest.java
Copy path
File metadata and controls
1071 lines (978 loc) · 44.7 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
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
package
org
.
json
.
junit
;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
import
java
.
io
.
File
;
import
java
.
io
.
FileReader
;
import
java
.
io
.
FileWriter
;
import
java
.
io
.
IOException
;
import
java
.
io
.
InputStream
;
import
java
.
io
.
InputStreamReader
;
import
java
.
io
.
Reader
;
import
java
.
io
.
StringReader
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
import
org
.
json
.
JSONArray
;
import
org
.
json
.
JSONException
;
import
org
.
json
.
JSONObject
;
import
org
.
json
.
JSONTokener
;
import
org
.
json
.
XML
;
import
org
.
json
.
XMLParserConfiguration
;
import
org
.
json
.
XMLXsiTypeConverter
;
import
org
.
junit
.
Rule
;
import
org
.
junit
.
Test
;
import
org
.
junit
.
rules
.
TemporaryFolder
;
/**
* Tests for JSON-Java XML.java
* Note: noSpace() will be tested by JSONMLTest
*/
public
class
XMLTest
{
/**
* JUnit supports temporary files and folders that are cleaned up after the test.
* https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/
*/
@
Rule
public
TemporaryFolder
testFolder
=
new
TemporaryFolder
();
/**
* JSONObject from a null XML string.
* Expects a NullPointerException
*/
@
Test
(
expected
=
NullPointerException
.
class
)
public
void
shouldHandleNullXML
() {
String
xmlStr
=
null
;
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"jsonObject should be empty"
,
jsonObject
.
isEmpty
());
}
/**
* Empty JSONObject from an empty XML string.
*/
@
Test
public
void
shouldHandleEmptyXML
() {
String
xmlStr
=
""
;
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"jsonObject should be empty"
,
jsonObject
.
isEmpty
());
}
/**
* Empty JSONObject from a non-XML string.
*/
@
Test
public
void
shouldHandleNonXML
() {
String
xmlStr
=
"{
\"
this is
\"
:
\"
not xml
\"
}"
;
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"xml string should be empty"
,
jsonObject
.
isEmpty
());
}
/**
* Invalid XML string (tag contains a frontslash).
* Expects a JSONException
*/
@
Test
public
void
shouldHandleInvalidSlashInTag
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name/x>
\n
"
+
" <street>abc street</street>
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
try
{
XML
.
toJSONObject
(
xmlStr
);
fail
(
"Expecting a JSONException"
);
}
catch
(
JSONException
e
) {
assertEquals
(
"Expecting an exception message"
,
"Misshaped tag at 176 [character 14 line 4]"
,
e
.
getMessage
());
}
}
/**
* Invalid XML string ('!' char in tag)
* Expects a JSONException
*/
@
Test
public
void
shouldHandleInvalidBangInTag
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name/>
\n
"
+
" <!>
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
try
{
XML
.
toJSONObject
(
xmlStr
);
fail
(
"Expecting a JSONException"
);
}
catch
(
JSONException
e
) {
assertEquals
(
"Expecting an exception message"
,
"Misshaped meta tag at 214 [character 12 line 7]"
,
e
.
getMessage
());
}
}
/**
* Invalid XML string ('!' char and no closing tag brace)
* Expects a JSONException
*/
@
Test
public
void
shouldHandleInvalidBangNoCloseInTag
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name/>
\n
"
+
" <!
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
try
{
XML
.
toJSONObject
(
xmlStr
);
fail
(
"Expecting a JSONException"
);
}
catch
(
JSONException
e
) {
assertEquals
(
"Expecting an exception message"
,
"Misshaped meta tag at 213 [character 12 line 7]"
,
e
.
getMessage
());
}
}
/**
* Invalid XML string (no end brace for tag)
* Expects JSONException
*/
@
Test
public
void
shouldHandleNoCloseStartTag
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name/>
\n
"
+
" <abc
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
try
{
XML
.
toJSONObject
(
xmlStr
);
fail
(
"Expecting a JSONException"
);
}
catch
(
JSONException
e
) {
assertEquals
(
"Expecting an exception message"
,
"Misplaced '<' at 193 [character 4 line 6]"
,
e
.
getMessage
());
}
}
/**
* Invalid XML string (partial CDATA chars in tag name)
* Expects JSONException
*/
@
Test
public
void
shouldHandleInvalidCDATABangInTag
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name>Joe Tester</name>
\n
"
+
" <![[]>
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
try
{
XML
.
toJSONObject
(
xmlStr
);
fail
(
"Expecting a JSONException"
);
}
catch
(
JSONException
e
) {
assertEquals
(
"Expecting an exception message"
,
"Expected 'CDATA[' at 204 [character 11 line 5]"
,
e
.
getMessage
());
}
}
/**
* Null JSONObject in XML.toString()
*/
@
Test
public
void
shouldHandleNullJSONXML
() {
JSONObject
jsonObject
=
null
;
String
actualXml
=
XML
.
toString
(
jsonObject
);
assertEquals
(
"generated XML does not equal expected XML"
,
"
\"
null
\"
"
,
actualXml
);
}
/**
* Empty JSONObject in XML.toString()
*/
@
Test
public
void
shouldHandleEmptyJSONXML
() {
JSONObject
jsonObject
=
new
JSONObject
();
String
xmlStr
=
XML
.
toString
(
jsonObject
);
assertTrue
(
"xml string should be empty"
,
xmlStr
.
isEmpty
());
}
/**
* No SML start tag. The ending tag ends up being treated as content.
*/
@
Test
public
void
shouldHandleNoStartTag
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name/>
\n
"
+
" <nocontent/>>
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
String
expectedStr
=
"{
\"
addresses
\"
:{
\"
address
\"
:{
\"
name
\"
:
\"
\"
,
\"
nocontent
\"
:
\"
\"
,
\"
"
+
"content
\"
:
\"
>
\"
},
\"
xsi:noNamespaceSchemaLocation
\"
:
\"
test.xsd
\"
,
\"
"
+
"xmlns:xsi
\"
:
\"
http://www.w3.org/2001/XMLSchema-instance
\"
}}"
;
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
Util
.
compareActualVsExpectedJsonObjects
(
jsonObject
,
expectedJsonObject
);
}
/**
* Valid XML to JSONObject
*/
@
Test
public
void
shouldHandleSimpleXML
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name>Joe Tester</name>
\n
"
+
" <street>[CDATA[Baker street 5]</street>
\n
"
+
" <NothingHere/>
\n
"
+
" <TrueValue>true</TrueValue>
\n
"
+
" <FalseValue>false</FalseValue>
\n
"
+
" <NullValue>null</NullValue>
\n
"
+
" <PositiveValue>42</PositiveValue>
\n
"
+
" <NegativeValue>-23</NegativeValue>
\n
"
+
" <DoubleValue>-23.45</DoubleValue>
\n
"
+
" <Nan>-23x.45</Nan>
\n
"
+
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
String
expectedStr
=
"{
\"
addresses
\"
:{
\"
address
\"
:{
\"
street
\"
:
\"
[CDATA[Baker street 5]
\"
,"
+
"
\"
name
\"
:
\"
Joe Tester
\"
,
\"
NothingHere
\"
:
\"
\"
,TrueValue:true,
\n
"
+
"
\"
FalseValue
\"
:false,
\"
NullValue
\"
:null,
\"
PositiveValue
\"
:42,
\n
"
+
"
\"
NegativeValue
\"
:-23,
\"
DoubleValue
\"
:-23.45,
\"
Nan
\"
:-23x.45,
\n
"
+
"
\"
ArrayOfNum
\"
:
\"
1, 2, 3, 4.1, 5.2
\"
\n
"
+
"},
\"
xsi:noNamespaceSchemaLocation
\"
:"
+
"
\"
test.xsd
\"
,
\"
xmlns:xsi
\"
:
\"
http://www.w3.org/2001/"
+
"XMLSchema-instance
\"
}}"
;
compareStringToJSONObject
(
xmlStr
,
expectedStr
);
compareReaderToJSONObject
(
xmlStr
,
expectedStr
);
compareFileToJSONObject
(
xmlStr
,
expectedStr
);
}
/**
* Tests to verify that supported escapes in XML are converted to actual values.
*/
@
Test
public
void
testXmlEscapeToJson
(){
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<root>"
+
"<rawQuote>
\"
</rawQuote>"
+
"<euro>A €33</euro>"
+
"<euroX>A €22€</euroX>"
+
"<unknown>some text ©</unknown>"
+
"<known>" " & ' < ></known>"
+
"<high>𝄢 𐅥</high>"
+
"</root>"
;
String
expectedStr
=
"{
\"
root
\"
:{"
+
"
\"
rawQuote
\"
:
\"
\\
\"
\"
,"
+
"
\"
euro
\"
:
\"
A €33
\"
,"
+
"
\"
euroX
\"
:
\"
A €22€
\"
,"
+
"
\"
unknown
\"
:
\"
some text ©
\"
,"
+
"
\"
known
\"
:
\"
\\
\"
\\
\"
& ' < >
\"
,"
+
"
\"
high
\"
:
\"
𝄢 𐅥
\"
"
+
"}}"
;
compareStringToJSONObject
(
xmlStr
,
expectedStr
);
compareReaderToJSONObject
(
xmlStr
,
expectedStr
);
compareFileToJSONObject
(
xmlStr
,
expectedStr
);
}
/**
* Tests that control characters are escaped.
*/
@
Test
public
void
testJsonToXmlEscape
(){
final
String
jsonSrc
=
"{
\"
amount
\"
:
\"
10,00 €
\"
,"
+
"
\"
description
\"
:
\"
Ação Válida
\u0085
\"
,"
+
"
\"
xmlEntities
\"
:
\"
\\
\"
' & < >
\"
"
+
"}"
;
JSONObject
json
=
new
JSONObject
(
jsonSrc
);
String
xml
=
XML
.
toString
(
json
);
//test control character not existing
assertFalse
(
"Escaping
\u0085
failed. Found in XML output."
,
xml
.
contains
(
"
\u0085
"
));
assertTrue
(
"Escaping
\u0085
failed. Entity not found in XML output."
,
xml
.
contains
(
"…"
));
// test normal unicode existing
assertTrue
(
"Escaping € failed. Not found in XML output."
,
xml
.
contains
(
"€"
));
assertTrue
(
"Escaping ç failed. Not found in XML output."
,
xml
.
contains
(
"ç"
));
assertTrue
(
"Escaping ã failed. Not found in XML output."
,
xml
.
contains
(
"ã"
));
assertTrue
(
"Escaping á failed. Not found in XML output."
,
xml
.
contains
(
"á"
));
// test XML Entities converted
assertTrue
(
"Escaping
\"
failed. Not found in XML output."
,
xml
.
contains
(
"""
));
assertTrue
(
"Escaping ' failed. Not found in XML output."
,
xml
.
contains
(
"'"
));
assertTrue
(
"Escaping & failed. Not found in XML output."
,
xml
.
contains
(
"&"
));
assertTrue
(
"Escaping < failed. Not found in XML output."
,
xml
.
contains
(
"<"
));
assertTrue
(
"Escaping > failed. Not found in XML output."
,
xml
.
contains
(
">"
));
}
/**
* Valid XML with comments to JSONObject
*/
@
Test
public
void
shouldHandleCommentsInXML
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<!-- this is a comment -->
\n
"
+
"<addresses>
\n
"
+
" <address>
\n
"
+
" <![CDATA[ this is -- <another> comment ]]>
\n
"
+
" <name>Joe Tester</name>
\n
"
+
" <!-- this is a - multi line
\n
"
+
" comment -->
\n
"
+
" <street>Baker street 5</street>
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
String
expectedStr
=
"{
\"
addresses
\"
:{
\"
address
\"
:{
\"
street
\"
:
\"
Baker "
+
"street 5
\"
,
\"
name
\"
:
\"
Joe Tester
\"
,
\"
content
\"
:
\"
this is -- "
+
"<another> comment
\"
}}}"
;
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
Util
.
compareActualVsExpectedJsonObjects
(
jsonObject
,
expectedJsonObject
);
}
/**
* Valid XML to XML.toString()
*/
@
Test
public
void
shouldHandleToString
() {
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<addresses xmlns:xsi=
\"
http://www.w3.org/2001/XMLSchema-instance
\"
"
+
" xsi:noNamespaceSchemaLocation='test.xsd'>
\n
"
+
" <address>
\n
"
+
" <name>[CDATA[Joe & T > e < s " t ' er]]</name>
\n
"
+
" <street>Baker street 5</street>
\n
"
+
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>
\n
"
+
" </address>
\n
"
+
"</addresses>"
;
String
expectedStr
=
"{
\"
addresses
\"
:{
\"
address
\"
:{
\"
street
\"
:
\"
Baker street 5
\"
,"
+
"
\"
name
\"
:
\"
[CDATA[Joe & T > e < s
\\
\"
t
\\
\'
er]]
\"
,"
+
"
\"
ArrayOfNum
\"
:
\"
1, 2, 3, 4.1, 5.2
\"
\n
"
+
"},
\"
xsi:noNamespaceSchemaLocation
\"
:"
+
"
\"
test.xsd
\"
,
\"
xmlns:xsi
\"
:
\"
http://www.w3.org/2001/"
+
"XMLSchema-instance
\"
}}"
;
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
String
xmlToStr
=
XML
.
toString
(
jsonObject
);
JSONObject
finalJsonObject
=
XML
.
toJSONObject
(
xmlToStr
);
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
Util
.
compareActualVsExpectedJsonObjects
(
jsonObject
,
expectedJsonObject
);
Util
.
compareActualVsExpectedJsonObjects
(
finalJsonObject
,
expectedJsonObject
);
}
/**
* Converting a JSON doc containing '>' content to JSONObject, then
* XML.toString() should result in valid XML.
*/
@
Test
public
void
shouldHandleContentNoArraytoString
() {
String
expectedStr
=
"{
\"
addresses
\"
:{
\"
content
\"
:
\"
>
\"
}}"
;
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
String
finalStr
=
XML
.
toString
(
expectedJsonObject
);
String
expectedFinalStr
=
"<addresses>></addresses>"
;
assertEquals
(
"Should handle expectedFinal: ["
+
expectedStr
+
"] final: ["
+
finalStr
+
"]"
,
expectedFinalStr
,
finalStr
);
}
/**
* Converting a JSON doc containing a 'content' array to JSONObject, then
* XML.toString() should result in valid XML.
* TODO: This is probably an error in how the 'content' keyword is used.
*/
@
Test
public
void
shouldHandleContentArraytoString
() {
String
expectedStr
=
"{
\"
addresses
\"
:{"
+
"
\"
content
\"
:[1, 2, 3]}}"
;
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
String
finalStr
=
XML
.
toString
(
expectedJsonObject
);
String
expectedFinalStr
=
"<addresses>"
+
"1
\n
2
\n
3</addresses>"
;
assertEquals
(
"Should handle expectedFinal: ["
+
expectedStr
+
"] final: ["
+
finalStr
+
"]"
,
expectedFinalStr
,
finalStr
);
}
/**
* Converting a JSON doc containing a named array to JSONObject, then
* XML.toString() should result in valid XML.
*/
@
Test
public
void
shouldHandleArraytoString
() {
String
expectedStr
=
"{
\"
addresses
\"
:{"
+
"
\"
something
\"
:[1, 2, 3]}}"
;
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
String
finalStr
=
XML
.
toString
(
expectedJsonObject
);
String
expectedFinalStr
=
"<addresses>"
+
"<something>1</something><something>2</something><something>3</something>"
+
"</addresses>"
;
assertEquals
(
"Should handle expectedFinal: ["
+
expectedStr
+
"] final: ["
+
finalStr
+
"]"
,
expectedFinalStr
,
finalStr
);
}
/**
* Tests that the XML output for empty arrays is consistent.
*/
@
Test
public
void
shouldHandleEmptyArray
(){
final
JSONObject
jo1
=
new
JSONObject
();
jo1
.
put
(
"array"
,
new
Object
[]{});
final
JSONObject
jo2
=
new
JSONObject
();
jo2
.
put
(
"array"
,
new
JSONArray
());
final
String
expected
=
"<jo></jo>"
;
String
output1
=
XML
.
toString
(
jo1
,
"jo"
);
assertEquals
(
"Expected an empty root tag"
,
expected
,
output1
);
String
output2
=
XML
.
toString
(
jo2
,
"jo"
);
assertEquals
(
"Expected an empty root tag"
,
expected
,
output2
);
}
/**
* Tests that the XML output for arrays is consistent when an internal array is empty.
*/
@
Test
public
void
shouldHandleEmptyMultiArray
(){
final
JSONObject
jo1
=
new
JSONObject
();
jo1
.
put
(
"arr"
,
new
Object
[]{
"One"
,
new
String
[]{},
"Four"
});
final
JSONObject
jo2
=
new
JSONObject
();
jo2
.
put
(
"arr"
,
new
JSONArray
(
new
Object
[]{
"One"
,
new
JSONArray
(
new
String
[]{}),
"Four"
}));
final
String
expected
=
"<jo><arr>One</arr><arr></arr><arr>Four</arr></jo>"
;
String
output1
=
XML
.
toString
(
jo1
,
"jo"
);
assertEquals
(
"Expected a matching array"
,
expected
,
output1
);
String
output2
=
XML
.
toString
(
jo2
,
"jo"
);
assertEquals
(
"Expected a matching array"
,
expected
,
output2
);
}
/**
* Tests that the XML output for arrays is consistent when arrays are not empty.
*/
@
Test
public
void
shouldHandleNonEmptyArray
(){
final
JSONObject
jo1
=
new
JSONObject
();
jo1
.
put
(
"arr"
,
new
String
[]{
"One"
,
"Two"
,
"Three"
});
final
JSONObject
jo2
=
new
JSONObject
();
jo2
.
put
(
"arr"
,
new
JSONArray
(
new
String
[]{
"One"
,
"Two"
,
"Three"
}));
final
String
expected
=
"<jo><arr>One</arr><arr>Two</arr><arr>Three</arr></jo>"
;
String
output1
=
XML
.
toString
(
jo1
,
"jo"
);
assertEquals
(
"Expected a non empty root tag"
,
expected
,
output1
);
String
output2
=
XML
.
toString
(
jo2
,
"jo"
);
assertEquals
(
"Expected a non empty root tag"
,
expected
,
output2
);
}
/**
* Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays.
*/
@
Test
public
void
shouldHandleMultiArray
(){
final
JSONObject
jo1
=
new
JSONObject
();
jo1
.
put
(
"arr"
,
new
Object
[]{
"One"
,
new
String
[]{
"Two"
,
"Three"
},
"Four"
});
final
JSONObject
jo2
=
new
JSONObject
();
jo2
.
put
(
"arr"
,
new
JSONArray
(
new
Object
[]{
"One"
,
new
JSONArray
(
new
String
[]{
"Two"
,
"Three"
}),
"Four"
}));
final
String
expected
=
"<jo><arr>One</arr><arr><array>Two</array><array>Three</array></arr><arr>Four</arr></jo>"
;
String
output1
=
XML
.
toString
(
jo1
,
"jo"
);
assertEquals
(
"Expected a matching array"
,
expected
,
output1
);
String
output2
=
XML
.
toString
(
jo2
,
"jo"
);
assertEquals
(
"Expected a matching array"
,
expected
,
output2
);
}
/**
* Converting a JSON doc containing a named array of nested arrays to
* JSONObject, then XML.toString() should result in valid XML.
*/
@
Test
public
void
shouldHandleNestedArraytoString
() {
String
xmlStr
=
"{
\"
addresses
\"
:{
\"
address
\"
:{
\"
name
\"
:
\"
\"
,
\"
nocontent
\"
:
\"
\"
,"
+
"
\"
outer
\"
:[[1], [2], [3]]},
\"
xsi:noNamespaceSchemaLocation
\"
:
\"
test.xsd
\"
,
\"
"
+
"xmlns:xsi
\"
:
\"
http://www.w3.org/2001/XMLSchema-instance
\"
}}"
;
JSONObject
jsonObject
=
new
JSONObject
(
xmlStr
);
String
finalStr
=
XML
.
toString
(
jsonObject
);
JSONObject
finalJsonObject
=
XML
.
toJSONObject
(
finalStr
);
String
expectedStr
=
"<addresses><address><name/><nocontent/>"
+
"<outer><array>1</array></outer><outer><array>2</array>"
+
"</outer><outer><array>3</array></outer>"
+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"
+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"
+
"ma-instance</xmlns:xsi></addresses>"
;
JSONObject
expectedJsonObject
=
XML
.
toJSONObject
(
expectedStr
);
Util
.
compareActualVsExpectedJsonObjects
(
finalJsonObject
,
expectedJsonObject
);
}
/**
* Possible bug:
* Illegal node-names must be converted to legal XML-node-names.
* The given example shows 2 nodes which are valid for JSON, but not for XML.
* Therefore illegal arguments should be converted to e.g. an underscore (_).
*/
@
Test
public
void
shouldHandleIllegalJSONNodeNames
()
{
JSONObject
inputJSON
=
new
JSONObject
();
inputJSON
.
append
(
"123IllegalNode"
,
"someValue1"
);
inputJSON
.
append
(
"Illegal@node"
,
"someValue2"
);
String
result
=
XML
.
toString
(
inputJSON
);
/*
* This is invalid XML. Names should not begin with digits or contain
* certain values, including '@'. One possible solution is to replace
* illegal chars with '_', in which case the expected output would be:
* <___IllegalNode>someValue1</___IllegalNode><Illegal_node>someValue2</Illegal_node>
*/
String
expected
=
"<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>"
;
assertEquals
(
"length"
,
expected
.
length
(),
result
.
length
());
assertTrue
(
"123IllegalNode"
,
result
.
contains
(
"<123IllegalNode>someValue1</123IllegalNode>"
));
assertTrue
(
"Illegal@node"
,
result
.
contains
(
"<Illegal@node>someValue2</Illegal@node>"
));
}
/**
* JSONObject with NULL value, to XML.toString()
*/
@
Test
public
void
shouldHandleNullNodeValue
()
{
JSONObject
inputJSON
=
new
JSONObject
();
inputJSON
.
put
(
"nullValue"
,
JSONObject
.
NULL
);
// This is a possible preferred result
// String expectedXML = "<nullValue/>";
/**
* This is the current behavior. JSONObject.NULL is emitted as
* the string, "null".
*/
String
actualXML
=
"<nullValue>null</nullValue>"
;
String
resultXML
=
XML
.
toString
(
inputJSON
);
assertEquals
(
actualXML
,
resultXML
);
}
/**
* Investigate exactly how the "content" keyword works
*/
@
Test
public
void
contentOperations
() {
/*
* When a standalone <!CDATA[...]] structure is found while parsing XML into a
* JSONObject, the contents are placed in a string value with key="content".
*/
String
xmlStr
=
"<tag1></tag1><![CDATA[if (a < b && a > 0) then return]]><tag2></tag2>"
;
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"1. 3 items"
,
3
==
jsonObject
.
length
());
assertTrue
(
"1. empty tag1"
,
""
.
equals
(
jsonObject
.
get
(
"tag1"
)));
assertTrue
(
"1. empty tag2"
,
""
.
equals
(
jsonObject
.
get
(
"tag2"
)));
assertTrue
(
"1. content found"
,
"if (a < b && a > 0) then return"
.
equals
(
jsonObject
.
get
(
"content"
)));
// multiple consecutive standalone cdatas are accumulated into an array
xmlStr
=
"<tag1></tag1><![CDATA[if (a < b && a > 0) then return]]><tag2></tag2><![CDATA[here is another cdata]]>"
;
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"2. 3 items"
,
3
==
jsonObject
.
length
());
assertTrue
(
"2. empty tag1"
,
""
.
equals
(
jsonObject
.
get
(
"tag1"
)));
assertTrue
(
"2. empty tag2"
,
""
.
equals
(
jsonObject
.
get
(
"tag2"
)));
assertTrue
(
"2. content array found"
,
jsonObject
.
get
(
"content"
)
instanceof
JSONArray
);
JSONArray
jsonArray
=
jsonObject
.
getJSONArray
(
"content"
);
assertTrue
(
"2. array size"
,
jsonArray
.
length
() ==
2
);
assertTrue
(
"2. content array entry 0"
,
"if (a < b && a > 0) then return"
.
equals
(
jsonArray
.
get
(
0
)));
assertTrue
(
"2. content array entry 1"
,
"here is another cdata"
.
equals
(
jsonArray
.
get
(
1
)));
/*
* text content is accumulated in a "content" inside a local JSONObject.
* If there is only one instance, it is saved in the context (a different JSONObject
* from the calling code. and the content element is discarded.
*/
xmlStr
=
"<tag1>value 1</tag1>"
;
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"3. 2 items"
,
1
==
jsonObject
.
length
());
assertTrue
(
"3. value tag1"
,
"value 1"
.
equals
(
jsonObject
.
get
(
"tag1"
)));
/*
* array-style text content (multiple tags with the same name) is
* accumulated in a local JSONObject with key="content" and value=JSONArray,
* saved in the context, and then the local JSONObject is discarded.
*/
xmlStr
=
"<tag1>value 1</tag1><tag1>2</tag1><tag1>true</tag1>"
;
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"4. 1 item"
,
1
==
jsonObject
.
length
());
assertTrue
(
"4. content array found"
,
jsonObject
.
get
(
"tag1"
)
instanceof
JSONArray
);
jsonArray
=
jsonObject
.
getJSONArray
(
"tag1"
);
assertTrue
(
"4. array size"
,
jsonArray
.
length
() ==
3
);
assertTrue
(
"4. content array entry 0"
,
"value 1"
.
equals
(
jsonArray
.
get
(
0
)));
assertTrue
(
"4. content array entry 1"
,
jsonArray
.
getInt
(
1
) ==
2
);
assertTrue
(
"4. content array entry 2"
,
jsonArray
.
getBoolean
(
2
) ==
true
);
/*
* Complex content is accumulated in a "content" field. For example, an element
* may contain a mix of child elements and text. Each text segment is
* accumulated to content.
*/
xmlStr
=
"<tag1>val1<tag2/>val2</tag1>"
;
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"5. 1 item"
,
1
==
jsonObject
.
length
());
assertTrue
(
"5. jsonObject found"
,
jsonObject
.
get
(
"tag1"
)
instanceof
JSONObject
);
jsonObject
=
jsonObject
.
getJSONObject
(
"tag1"
);
assertTrue
(
"5. 2 contained items"
,
2
==
jsonObject
.
length
());
assertTrue
(
"5. contained tag"
,
""
.
equals
(
jsonObject
.
get
(
"tag2"
)));
assertTrue
(
"5. contained content jsonArray found"
,
jsonObject
.
get
(
"content"
)
instanceof
JSONArray
);
jsonArray
=
jsonObject
.
getJSONArray
(
"content"
);
assertTrue
(
"5. array size"
,
jsonArray
.
length
() ==
2
);
assertTrue
(
"5. content array entry 0"
,
"val1"
.
equals
(
jsonArray
.
get
(
0
)));
assertTrue
(
"5. content array entry 1"
,
"val2"
.
equals
(
jsonArray
.
get
(
1
)));
/*
* If there is only 1 complex text content, then it is accumulated in a
* "content" field as a string.
*/
xmlStr
=
"<tag1>val1<tag2/></tag1>"
;
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"6. 1 item"
,
1
==
jsonObject
.
length
());
assertTrue
(
"6. jsonObject found"
,
jsonObject
.
get
(
"tag1"
)
instanceof
JSONObject
);
jsonObject
=
jsonObject
.
getJSONObject
(
"tag1"
);
assertTrue
(
"6. contained content found"
,
"val1"
.
equals
(
jsonObject
.
get
(
"content"
)));
assertTrue
(
"6. contained tag2"
,
""
.
equals
(
jsonObject
.
get
(
"tag2"
)));
/*
* In this corner case, the content sibling happens to have key=content
* We end up with an array within an array, and no content element.
* This is probably a bug.
*/
xmlStr
=
"<tag1>val1<content/></tag1>"
;
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
assertTrue
(
"7. 1 item"
,
1
==
jsonObject
.
length
());
assertTrue
(
"7. jsonArray found"
,
jsonObject
.
get
(
"tag1"
)
instanceof
JSONArray
);
jsonArray
=
jsonObject
.
getJSONArray
(
"tag1"
);
assertTrue
(
"array size 1"
,
jsonArray
.
length
() ==
1
);
assertTrue
(
"7. contained array found"
,
jsonArray
.
get
(
0
)
instanceof
JSONArray
);
jsonArray
=
jsonArray
.
getJSONArray
(
0
);
assertTrue
(
"7. inner array size 2"
,
jsonArray
.
length
() ==
2
);
assertTrue
(
"7. inner array item 0"
,
"val1"
.
equals
(
jsonArray
.
get
(
0
)));
assertTrue
(
"7. inner array item 1"
,
""
.
equals
(
jsonArray
.
get
(
1
)));
/*
* Confirm behavior of original issue
*/
String
jsonStr
=
"{"
+
"
\"
Profile
\"
: {"
+
"
\"
list
\"
: {"
+
"
\"
history
\"
: {"
+
"
\"
entries
\"
: ["
+
"{"
+
"
\"
deviceId
\"
:
\"
id
\"
,"
+
"
\"
content
\"
: {"
+
"
\"
material
\"
: ["
+
"{"
+
"
\"
stuff
\"
: false"
+
"}"
+
"]"
+
"}"
+
"}"
+
"]"
+
"}"
+
"}"
+
"}"
+
"}"
;
jsonObject
=
new
JSONObject
(
jsonStr
);
xmlStr
=
XML
.
toString
(
jsonObject
);
/*
* This is the created XML. Looks like content was mistaken for
* complex (child node + text) XML.
* <Profile>
* <list>
* <history>
* <entries>
* <deviceId>id</deviceId>
* {"material":[{"stuff":false}]}
* </entries>
* </history>
* </list>
* </Profile>
*/
assertTrue
(
"nothing to test here, see comment on created XML, above"
,
true
);
}
/**
* Convenience method, given an input string and expected result,
* convert to JSONObject and compare actual to expected result.
* @param xmlStr the string to parse
* @param expectedStr the expected JSON string
*/
private
void
compareStringToJSONObject
(
String
xmlStr
,
String
expectedStr
) {
JSONObject
jsonObject
=
XML
.
toJSONObject
(
xmlStr
);
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
Util
.
compareActualVsExpectedJsonObjects
(
jsonObject
,
expectedJsonObject
);
}
/**
* Convenience method, given an input string and expected result,
* convert to JSONObject via reader and compare actual to expected result.
* @param xmlStr the string to parse
* @param expectedStr the expected JSON string
*/
private
void
compareReaderToJSONObject
(
String
xmlStr
,
String
expectedStr
) {
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
Reader
reader
=
new
StringReader
(
xmlStr
);
JSONObject
jsonObject
=
XML
.
toJSONObject
(
reader
);
Util
.
compareActualVsExpectedJsonObjects
(
jsonObject
,
expectedJsonObject
);
}
/**
* Convenience method, given an input string and expected result, convert to
* JSONObject via file and compare actual to expected result.
*
* @param xmlStr
* the string to parse
* @param expectedStr
* the expected JSON string
* @throws IOException
*/
private
void
compareFileToJSONObject
(
String
xmlStr
,
String
expectedStr
) {
try
{
JSONObject
expectedJsonObject
=
new
JSONObject
(
expectedStr
);
File
tempFile
=
this
.
testFolder
.
newFile
(
"fileToJSONObject.xml"
);
FileWriter
fileWriter
=
new
FileWriter
(
tempFile
);
try
{
fileWriter
.
write
(
xmlStr
);
}
finally
{
fileWriter
.
close
();
}
Reader
reader
=
new
FileReader
(
tempFile
);
try
{
JSONObject
jsonObject
=
XML
.
toJSONObject
(
reader
);
Util
.
compareActualVsExpectedJsonObjects
(
jsonObject
,
expectedJsonObject
);
}
finally
{
reader
.
close
();
}
}
catch
(
IOException
e
) {
fail
(
"Error: "
+
e
.
getMessage
());
}
}
/**
* JSON string lost leading zero and converted "True" to true.
*/
@
Test
public
void
testToJSONArray_jsonOutput
() {
final
String
originalXml
=
"<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=
\"
01
\"
/><title>True</title></root>"
;
final
JSONObject
expectedJson
=
new
JSONObject
(
"{
\"
root
\"
:{
\"
item
\"
:{
\"
id
\"
:
\"
01
\"
},
\"
id
\"
:[
\"
01
\"
,1,
\"
00
\"
,0],
\"
title
\"
:true}}"
);
final
JSONObject
actualJsonOutput
=
XML
.
toJSONObject
(
originalXml
,
false
);
Util
.
compareActualVsExpectedJsonObjects
(
actualJsonOutput
,
expectedJson
);
}
/**
* JSON string cannot be reverted to original xml.
*/
@
Test
public
void
testToJSONArray_reversibility
() {
final
String
originalXml
=
"<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=
\"
01
\"
/><title>True</title></root>"
;
final
String
revertedXml
=
XML
.
toString
(
XML
.
toJSONObject
(
originalXml
,
false
));
assertNotEquals
(
revertedXml
,
originalXml
);
}
/**
* test passes when using the new method toJsonArray.
*/
@
Test
public
void
testToJsonXML
() {
final
String
originalXml
=
"<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=
\"
01
\"
/><title>True</title></root>"
;
final
JSONObject
expected
=
new
JSONObject
(
"{
\"
root
\"
:{
\"
item
\"
:{
\"
id
\"
:
\"
01
\"
},
\"
id
\"
:[
\"
01
\"
,
\"
1
\"
,
\"
00
\"
,
\"
0
\"
],
\"
title
\"
:
\"
True
\"
}}"
);
final
JSONObject
actual
=
XML
.
toJSONObject
(
originalXml
,
true
);
Util
.
compareActualVsExpectedJsonObjects
(
actual
,
expected
);
final
String
reverseXml
=
XML
.
toString
(
actual
);
// this reversal isn't exactly the same. use JSONML for an exact reversal
// the order of the elements may be differnet as well.
final
String
expectedReverseXml
=
"<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>"
;
assertEquals
(
"length"
,
expectedReverseXml
.
length
(),
reverseXml
.
length
());
assertTrue
(
"array contents"
,
reverseXml
.
contains
(
"<id>01</id><id>1</id><id>00</id><id>0</id>"
));
assertTrue
(
"item contents"
,
reverseXml
.
contains
(
"<item><id>01</id></item>"
));
assertTrue
(
"title contents"
,
reverseXml
.
contains
(
"<title>True</title>"
));
}
/**
* test to validate certain conditions of XML unescaping.
*/
@
Test
public
void
testUnescape
() {
assertEquals
(
"{
\"
xml
\"
:
\"
Can cope <;
\"
}"
,
XML
.
toJSONObject
(
"<xml>Can cope <; </xml>"
).
toString
());
assertEquals
(
"Can cope <; "
,
XML
.
unescape
(
"Can cope <; "
));
assertEquals
(
"{
\"
xml
\"
:
\"
Can cope & ;
\"
}"
,
XML
.
toJSONObject
(
"<xml>Can cope & ; </xml>"
).
toString
());
assertEquals
(
"Can cope & ; "
,
XML
.
unescape
(
"Can cope & ; "
));
assertEquals
(
"{
\"
xml
\"
:
\"
Can cope &;
\"
}"
,
XML
.
toJSONObject
(
"<xml>Can cope &; </xml>"
).
toString
());
assertEquals
(
"Can cope &; "
,
XML
.
unescape
(
"Can cope &; "
));
// unicode entity
assertEquals
(
"{
\"
xml
\"
:
\"
Can cope 4;
\"
}"
,
XML
.
toJSONObject
(
"<xml>Can cope 4; </xml>"
).
toString
());
assertEquals
(
"Can cope 4; "
,
XML
.
unescape
(
"Can cope 4; "
));
// double escaped
assertEquals
(
"{
\"
xml
\"
:
\"
Can cope <
\"
}"
,
XML
.
toJSONObject
(
"<xml>Can cope &lt; </xml>"
).
toString
());
assertEquals
(
"Can cope < "
,
XML
.
unescape
(
"Can cope &lt; "
));
assertEquals
(
"{
\"
xml
\"
:
\"
Can cope 4
\"
}"
,
XML
.
toJSONObject
(
"<xml>Can cope &#x34; </xml>"
).
toString
());
assertEquals
(
"Can cope 4 "
,
XML
.
unescape
(
"Can cope &#x34; "
));
}
/**
* test passes when xsi:nil="true" converting to null (JSON specification-like nil conversion enabled)
*/
@
Test
public
void
testToJsonWithNullWhenNilConversionEnabled
() {
final
String
originalXml
=
"<root><id xsi:nil=
\"
true
\"
/></root>"
;
final
String
expectedJsonString
=
"{
\"
root
\"
:{
\"
id
\"
:null}}"
;
final
JSONObject
json
=
XML
.
toJSONObject
(
originalXml
,
new
XMLParserConfiguration
()
.
withKeepStrings
(
false
)
.
withcDataTagName
(
"content"
)
.
withConvertNilAttributeToNull
(
true
));
assertEquals
(
expectedJsonString
,
json
.
toString
());
}
/**
* test passes when xsi:nil="true" not converting to null (JSON specification-like nil conversion disabled)
*/
@
Test
public
void
testToJsonWithNullWhenNilConversionDisabled
() {
final
String
originalXml
=
"<root><id xsi:nil=
\"
true
\"
/></root>"
;
final
String
expectedJsonString
=
"{
\"
root
\"
:{
\"
id
\"
:{
\"
xsi:nil
\"
:true}}}"
;
final
JSONObject
json
=
XML
.
toJSONObject
(
originalXml
,
new
XMLParserConfiguration
());
assertEquals
(
expectedJsonString
,
json
.
toString
());
}
/**
* Tests to verify that supported escapes in XML are converted to actual values.
*/
@
Test
public
void
testIssue537CaseSensitiveHexEscapeMinimal
(){
String
xmlStr
=
"<?xml version=
\"
1.0
\"
encoding=
\"
UTF-8
\"
?>
\n
"
+
"<root>Neutrophils.Hypersegmented | Bld-Ser-Plas</root>"
;
String
expectedStr
=
"{
\"
root
\"
:
\"
Neutrophils.Hypersegmented | Bld-Ser-Plas
\"
}"
;
JSONObject
xmlJSONObj
=
XML
.
toJSONObject
(
xmlStr
,
true
);
JSONObject
expected
=
new
JSONObject
(
expectedStr
);
Util
.
compareActualVsExpectedJsonObjects
(
xmlJSONObj
,
expected
);
}
/**
* Tests to verify that supported escapes in XML are converted to actual values.
*/
@
Test
public
void
testIssue537CaseSensitiveHexEscapeFullFile
(){
try
{
InputStream
xmlStream
=
null
;
try
{
xmlStream
=
XMLTest
.
class
.
getClassLoader
().
getResourceAsStream
(
"Issue537.xml"
);
Reader
xmlReader
=
new
InputStreamReader
(
xmlStream
);
JSONObject
actual
=
XML
.
toJSONObject
(
xmlReader
,
true
);
InputStream
jsonStream
=
null
;
try
{
jsonStream
=
XMLTest
.
class
.
getClassLoader
().
getResourceAsStream
(
"Issue537.json"
);
final
JSONObject
expected
=
new
JSONObject
(
new
JSONTokener
(
jsonStream
));
Util
.
compareActualVsExpectedJsonObjects
(
actual
,
expected
);
}
finally
{
if
(
jsonStream
!=
null
) {
jsonStream
.
close
();
}
}
}
finally
{
if
(
xmlStream
!=
null
) {
xmlStream
.
close
();
}
}
}
catch
(
IOException
e
) {
fail
(
"file writer error: "
+
e
.
getMessage
());
}
}
/**
* Tests to verify that supported escapes in XML are converted to actual values.
*/
@
Test
public
void
testIssue537CaseSensitiveHexUnEscapeDirect
(){
String
origStr
=
"Neutrophils.Hypersegmented | Bld-Ser-Plas"
;
String
expectedStr
=
"Neutrophils.Hypersegmented | Bld-Ser-Plas"
;
String
actualStr
=
XML
.
unescape
(
origStr
);
assertEquals
(
"Case insensitive Entity unescape"
,
expectedStr
,
actualStr
);
}
/**
* test passes when xsi:type="java.lang.String" not converting to string
*/
@
Test
public
void
testToJsonWithTypeWhenTypeConversionDisabled
() {
String
originalXml
=
"<root><id xsi:type=
\"
string
\"
>1234</id></root>"
;
String
expectedJsonString
=
"{
\"
root
\"
:{
\"
id
\"
:{
\"
xsi:type
\"
:
\"
string
\"
,
\"
content
\"
:1234}}}"
;
JSONObject
expectedJson
=
new
JSONObject
(
expectedJsonString
);
JSONObject
actualJson
=
XML
.
toJSONObject
(
originalXml
,
new
XMLParserConfiguration
());
Util
.
compareActualVsExpectedJsonObjects
(
actualJson
,
expectedJson
);
}
/**
* test passes when xsi:type="java.lang.String" converting to String
*/
@
Test
public
void
testToJsonWithTypeWhenTypeConversionEnabled
() {
String
originalXml
=
"<root><id1 xsi:type=
\"
string
\"
>1234</id1>"
+
"<id2 xsi:type=
\"
integer
\"
>1234</id2></root>"
;
String
expectedJsonString
=
"{
\"
root
\"
:{
\"
id2
\"
:1234,
\"
id1
\"
:
\"
1234
\"
}}"
;
JSONObject
expectedJson
=
new
JSONObject
(
expectedJsonString
);
Map
<
String
,
XMLXsiTypeConverter
<?>>
xsiTypeMap
=
new
HashMap
<
String
,
XMLXsiTypeConverter
<?>>();
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL