forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestXML.java
More file actions
1320 lines (1231 loc) · 52.1 KB
/
TestXML.java
File metadata and controls
1320 lines (1231 loc) · 52.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
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
/*
* File: TestXML.java Author: JSON.org
*/
package org.json.tests;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import junit.framework.TestCase;
/*
* Copyright (c) 2002 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.
*/
/**
* The Class TestXML.
*/
public class TestXML extends TestCase
{
/** The jsonobject. */
JSONObject jsonobject = new JSONObject();
/** The jsonarray. */
JSONArray jsonarray = new JSONArray();
/** The string. */
String string;
/**
* Tests the toJsonObject method using Simple xml.
*/
public static void testToJsonObject_SimpleXML()
{
try
{
String XMLString = "<!--comment--><tag1><tag2><?Skip Me?><![CDATA[--comment--]]></tag2><tag3>!123321</tag3></tag1>";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("tag2", "--comment--");
jo2.put("tag3", "!123321");
jo.put("tag1", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Bad name.
*/
public static void testToJsonObject_BadName()
{
try
{
String XMLString = "<!-abc>123</!-abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Mismatched close tag ! at 13 [character 14 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Bad cdata.
*/
public static void testToJsonObject_BadCDATA()
{
try
{
String XMLString = "<abc><![CDATA?[--comment--]]></abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Expected 'CDATA[' at 14 [character 15 line 1]",
e.getMessage());
}
try
{
String XMLString = "<abc><![CDATA[--comment--]></abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unclosed CDATA at 34 [character 35 line 1]",
e.getMessage());
}
try
{
String XMLString = "<abc><![CDATA[--comment--]]?></abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unclosed CDATA at 36 [character 37 line 1]",
e.getMessage());
}
try
{
String XMLString = "<abc><![CDAT[--comment--]]></abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Expected 'CDATA[' at 12 [character 13 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using null character.
*/
public static void testToJsonObject_NullCharacter()
{
try
{
String XMLString = "\0";
JSONObject jo = new JSONObject();
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toJsonObject method using Empty cdata.
*/
public static void testToJsonObject_EmptyCdata()
{
try
{
String XMLString = "<abc><![CDATA[]]></abc>";
JSONObject jo = new JSONObject();
jo.put("abc", "");
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Bad meta.
*/
public static void testToJsonObject_BadMeta()
{
try
{
String XMLString = "<! abc";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped meta tag at 7 [character 8 line 1]",
e.getMessage());
}
try
{
String XMLString = "<!-<abc";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped meta tag at 8 [character 9 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Open cdata.
*/
public static void testToJsonObject_OpenCDATA()
{
try
{
String XMLString = "<abc><![CDATA[";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unclosed CDATA at 15 [character 16 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Mismatched tags.
*/
public static void testToJsonObject_MismatchedTags()
{
try
{
String XMLString = "<abc>123</def>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Mismatched abc and def at 13 [character 14 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Misshaped close tag.
*/
public static void testToJsonObject_MisshapedCloseTag()
{
try
{
String XMLString = "<abc>123</abc?";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped close tag at 14 [character 15 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using unclosed tag.
*/
public static void testToJsonObject_UnclosedTag()
{
try
{
String XMLString = "<abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unclosed tag abc at 6 [character 7 line 1]",
e.getMessage());
}
}
/**
* Tests the stringToValue method using true.
*/
public static void testStringToValue_true()
{
assertEquals(Boolean.TRUE, XML.stringToValue("true"));
assertEquals(Boolean.TRUE, XML.stringToValue("tRUe"));
assertEquals(Boolean.TRUE, XML.stringToValue("TruE"));
assertEquals(Boolean.TRUE, XML.stringToValue("TRUE"));
}
/**
* Tests the stringToValue method using false.
*/
public static void testStringToValue_false()
{
assertEquals(Boolean.FALSE, XML.stringToValue("false"));
assertEquals(Boolean.FALSE, XML.stringToValue("fALSe"));
assertEquals(Boolean.FALSE, XML.stringToValue("FalsE"));
assertEquals(Boolean.FALSE, XML.stringToValue("FALSE"));
}
/**
* Tests the stringToValue method using blank.
*/
public static void testStringToValue_blank()
{
assertEquals("", XML.stringToValue(""));
}
/**
* Tests the stringToValue method using null.
*/
public static void testStringToValue_null()
{
assertEquals(JSONObject.NULL, XML.stringToValue("null"));
}
/**
* Tests the stringToValue method using numbers.
*/
public static void testStringToValue_Numbers()
{
assertEquals(0, XML.stringToValue("0"));
assertEquals(10, XML.stringToValue("10"));
assertEquals(-10, XML.stringToValue("-10"));
assertEquals(34.5, XML.stringToValue("34.5"));
assertEquals(-34.5, XML.stringToValue("-34.5"));
assertEquals(34054535455454355L, XML.stringToValue("34054535455454355"));
assertEquals(-34054535455454355L, XML.stringToValue("-34054535455454355"));
assertEquals("00123", XML.stringToValue("00123"));
assertEquals("-00123", XML.stringToValue("-00123"));
assertEquals(123, XML.stringToValue("0123"));
assertEquals(-123, XML.stringToValue("-0123"));
assertEquals("-", XML.stringToValue("-"));
assertEquals("-0abc", XML.stringToValue("-0abc"));
}
/**
* Tests the toJsonObject method using Misshaped tag.
*/
public static void testToJsonObject_MisshapedTag()
{
try
{
String XMLString = "<=abc>123<=abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 2 [character 3 line 1]",
e.getMessage());
}
try
{
String XMLString = "<abc=>123<abc=>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 5 [character 6 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Attributes.
*
* @throws Exception the exception
*/
public static void testToJsonObject_Attributes() throws Exception
{
String XMLString = "<abc \"abc\"=\"123\">123</abc>";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("content", 123);
jo2.put("abc", 123);
jo.put("abc", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
}
/**
* Tests the toJsonObject method using attributes with open string.
*/
public static void testToJsonObject_AttributesWithOpenString()
{
try
{
String XMLString = "<abc \"abc>123</abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Attributes with ampersand.
*/
public static void testToJsonObject_AttributesWithAmpersand()
{
try
{
String XMLString = "<abc \"abc \">123</abc>";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("content", 123);
jo2.put("abc ", "");
jo.put("abc", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Attributes missing value.
*/
public static void testToJsonObject_AttributesMissingValue()
{
try
{
String XMLString = "<abc \"abc\"=>123</abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Missing value at 12 [character 13 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using empty tag.
*/
public static void testToJsonObject_EmptyTag()
{
try
{
String XMLString = "<abc />";
JSONObject jo = new JSONObject();
jo.put("abc", "");
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toJsonObject method using empty tag with attributes.
*/
public static void testToJsonObject_EmptyTagWithAttributes()
{
try
{
String XMLString = "<abc 'def'='jkk' />";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("def","jkk");
jo.put("abc", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toJsonObject method using broken empty tag.
*/
public static void testToJsonObject_BrokenEmptyTag()
{
try
{
String XMLString = "<abc /?>";
XML.toJSONObject(XMLString).toString();
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 7 [character 8 line 1]",
e.getMessage());
}
}
/**
* Tests the toString method using jSON object.
*/
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", "123");
assertEquals("<abc>123</abc>", XML.toString(jo));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using empty json object.
*/
public static void testToString_EmptyJsonObject()
{
try
{
JSONObject jo = new JSONObject();
assertEquals("", XML.toString(jo));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using jSON object and name.
*/
public static void testToString_JsonObjectAndName()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", "123");
assertEquals("<my name><abc>123</abc></my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using empty json object and name.
*/
public static void testToString_EmptyJsonObjectAndName()
{
try
{
JSONObject jo = new JSONObject();
assertEquals("<my name></my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using empty json object and empty name.
*/
public static void testToString_EmptyJsonObjectAndEmptyName()
{
try
{
JSONObject jo = new JSONObject();
assertEquals("<></>", XML.toString(jo, ""));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using json object with null string value.
*/
public static void testToString_JsonObjectWithNullStringValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", "null");
assertEquals("<my name><abc>null</abc></my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with json object null value.
*/
public static void testToString_JsonObjectWithJSONObjectNullValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", JSONObject.NULL);
assertEquals("<my name><abc>null</abc></my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with null key.
*/
public static void testToString_JsonObjectWithNullKey()
{
try
{
JSONObject jo = new JSONObject();
jo.put(null, "abc");
XML.toString(jo, "my name");
fail("Should have thrown Exception");
} catch (JSONException e)
{
assertEquals("Null key.", e.getMessage());
}
}
/**
* Tests the toString method using json object with integer.
*/
public static void testToString_JsonObjectWithInteger()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", 45);
assertEquals("<my name><abc>45</abc></my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with content key int value.
*/
public static void testToString_JsonObjectWithContentKeyIntValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("content", 45);
assertEquals("<my name>45</my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with content key json array value.
*/
public static void testToString_JsonObjectWithContentKeyJsonArrayValue()
{
try
{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
ja.put("123");
ja.put(72);
jo.put("content", ja);
assertEquals("<my name>123\n72</my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with content key string value.
*/
public static void testToString_JsonObjectWithContentKeyStringValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("content", "42");
assertEquals("<my name>42</my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with json array value.
*/
public static void testToString_JsonObjectWithJsonArrayValue()
{
try
{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
ja.put("123");
ja.put(72);
jo.put("abc", ja);
assertEquals("<my name><abc>123</abc><abc>72</abc></my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with json array of json arrays value.
*/
public static void testToString_JsonObjectWithJsonArrayOfJsonArraysValue()
{
try
{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
JSONArray ja2 = new JSONArray();
JSONArray ja3 = new JSONArray();
ja2.put("cat");
ja.put(ja2);
ja.put(ja3);
jo.put("abc", ja);
assertEquals("<my name><abc><array>cat</array></abc><abc></abc></my name>", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using array.
*/
public static void testToString_Array()
{
try
{
String[] strings = {"abc", "123"};
assertEquals("<my name>abc</my name><my name>123</my name>", XML.toString(strings, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json array.
*/
public static void testToString_JsonArray()
{
try
{
JSONArray ja = new JSONArray();
ja.put("hi");
ja.put("bye");
assertEquals("<my name>hi</my name><my name>bye</my name>", XML.toString(ja, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using empty string.
*/
public static void testToString_EmptyString()
{
try
{
assertEquals("<my name/>", XML.toString("", "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using string no name.
*/
public static void testToString_StringNoName()
{
try
{
assertEquals("\"123\"", XML.toString("123"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the escape method.
*/
public static void testEscape()
{
try
{
assertEquals("\"&<>"'\"", XML.toString("&<>\"'"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the noSpace method using empty string.
*/
public static void testNoSpace_EmptyString()
{
try
{
XML.noSpace("");
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("Empty string.", e.getMessage());
}
}
/**
* Tests the noSpace method using string with no spaces.
*/
public static void testNoSpace_StringWithNoSpaces()
{
try
{
XML.noSpace("123");
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the noSpace method using string with spaces.
*/
public static void testNoSpace_StringWithSpaces()
{
try
{
XML.noSpace("1 23");
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("'1 23' contains a space character.", e.getMessage());
}
}
/**
* Test.
*/
public static void testNoSpace()
{
try
{
XML.noSpace("1 23");
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("'1 23' contains a space character.", e.getMessage());
}
}
/**
* Tests the XML method.
*
* @throws Exception the exception
*/
public void testXML() throws Exception
{
jsonobject = XML
.toJSONObject("<![CDATA[This is a collection of test patterns and examples for json.]]> Ignore the stuff past the end. ");
assertEquals(
"{\"content\":\"This is a collection of test patterns and examples for json.\"}",
jsonobject.toString());
assertEquals(
"This is a collection of test patterns and examples for json.",
jsonobject.getString("content"));
string = "<test><blank></blank><empty/></test>";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"test\": {\n \"blank\": \"\",\n \"empty\": \"\"\n}}",
jsonobject.toString(2));
assertEquals("<test><blank/><empty/></test>", XML.toString(jsonobject));
string = "<subsonic-response><playlists><playlist id=\"476c65652e6d3375\" int=\"12345678901234567890123456789012345678901234567890213991133777039355058536718668104339937\"/><playlist id=\"50617274792e78737066\"/></playlists></subsonic-response>";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"subsonic-response\":{\"playlists\":{\"playlist\":[{\"id\":\"476c65652e6d3375\",\"int\":\"12345678901234567890123456789012345678901234567890213991133777039355058536718668104339937\"},{\"id\":\"50617274792e78737066\"}]}}}",
jsonobject.toString());
}
/**
* Tests the XML2 method.
*/
public void testXML2()
{
try {
jsonobject = XML
.toJSONObject("<?xml version='1.0' encoding='UTF-8'?>"
+ "\n\n"
+ "<SOAP-ENV:Envelope"
+ " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""
+ " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\""
+ " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">"
+ "<SOAP-ENV:Body><ns1:doGoogleSearch"
+ " xmlns:ns1=\"urn:GoogleSearch\""
+ " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<key xsi:type=\"xsd:string\">GOOGLEKEY</key> <q"
+ " xsi:type=\"xsd:string\">'+search+'</q> <start"
+ " xsi:type=\"xsd:int\">0</start> <maxResults"
+ " xsi:type=\"xsd:int\">10</maxResults> <filter"
+ " xsi:type=\"xsd:boolean\">true</filter> <restrict"
+ " xsi:type=\"xsd:string\"></restrict> <safeSearch"
+ " xsi:type=\"xsd:boolean\">false</safeSearch> <lr"
+ " xsi:type=\"xsd:string\"></lr> <ie"
+ " xsi:type=\"xsd:string\">latin1</ie> <oe"
+ " xsi:type=\"xsd:string\">latin1</oe>"
+ "</ns1:doGoogleSearch>"
+ "</SOAP-ENV:Body></SOAP-ENV:Envelope>");
assertEquals(
"{\"SOAP-ENV:Envelope\": {\n \"SOAP-ENV:Body\": {\"ns1:doGoogleSearch\": {\n \"oe\": {\n \"content\": \"latin1\",\n \"xsi:type\": \"xsd:string\"\n },\n \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n \"lr\": {\"xsi:type\": \"xsd:string\"},\n \"start\": {\n \"content\": 0,\n \"xsi:type\": \"xsd:int\"\n },\n \"q\": {\n \"content\": \"'+search+'\",\n \"xsi:type\": \"xsd:string\"\n },\n \"ie\": {\n \"content\": \"latin1\",\n \"xsi:type\": \"xsd:string\"\n },\n \"safeSearch\": {\n \"content\": false,\n \"xsi:type\": \"xsd:boolean\"\n },\n \"xmlns:ns1\": \"urn:GoogleSearch\",\n \"restrict\": {\"xsi:type\": \"xsd:string\"},\n \"filter\": {\n \"content\": true,\n \"xsi:type\": \"xsd:boolean\"\n },\n \"maxResults\": {\n \"content\": 10,\n \"xsi:type\": \"xsd:int\"\n },\n \"key\": {\n \"content\": \"GOOGLEKEY\",\n \"xsi:type\": \"xsd:string\"\n }\n }},\n \"xmlns:xsd\": \"http://www.w3.org/1999/XMLSchema\",\n \"xmlns:xsi\": \"http://www.w3.org/1999/XMLSchema-instance\",\n \"xmlns:SOAP-ENV\": \"http://schemas.xmlsoap.org/soap/envelope/\"\n}}",
jsonobject.toString(2));
assertEquals(
"<SOAP-ENV:Envelope><SOAP-ENV:Body><ns1:doGoogleSearch><oe>latin1<xsi:type>xsd:string</xsi:type></oe><SOAP-ENV:encodingStyle>http://schemas.xmlsoap.org/soap/encoding/</SOAP-ENV:encodingStyle><lr><xsi:type>xsd:string</xsi:type></lr><start>0<xsi:type>xsd:int</xsi:type></start><q>'+search+'<xsi:type>xsd:string</xsi:type></q><ie>latin1<xsi:type>xsd:string</xsi:type></ie><safeSearch>false<xsi:type>xsd:boolean</xsi:type></safeSearch><xmlns:ns1>urn:GoogleSearch</xmlns:ns1><restrict><xsi:type>xsd:string</xsi:type></restrict><filter>true<xsi:type>xsd:boolean</xsi:type></filter><maxResults>10<xsi:type>xsd:int</xsi:type></maxResults><key>GOOGLEKEY<xsi:type>xsd:string</xsi:type></key></ns1:doGoogleSearch></SOAP-ENV:Body><xmlns:xsd>http://www.w3.org/1999/XMLSchema</xmlns:xsd><xmlns:xsi>http://www.w3.org/1999/XMLSchema-instance</xmlns:xsi><xmlns:SOAP-ENV>http://schemas.xmlsoap.org/soap/envelope/</xmlns:SOAP-ENV></SOAP-ENV:Envelope>",
XML.toString(jsonobject));
} catch (JSONException e) {
fail(e.toString());
}
}
/**
* Tests the constructor method.
*/
public static void testConstructor()
{
XML xml = new XML();
assertEquals("XML", xml.getClass().getSimpleName());
}
/**
* Tests the toJSONObject method using unclosed tag.
*/
public void testToJSONObject_UnclosedTag()
{
try {
jsonobject = XML.toJSONObject("<a><b> ");
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals("Unclosed tag b at 11 [character 12 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toJSONObject method using mismatched tags.
*/
public void testToJSONObject_MismatchedTags()
{
try {
jsonobject = XML.toJSONObject("<a></b> ");
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals("Mismatched a and b at 6 [character 7 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toJSONObject method using open tag.
*/
public void testToJSONObject_OpenTag()
{
try {
jsonobject = XML.toJSONObject("<a></a ");
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals("Misshaped element at 11 [character 12 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toString method using listof lists.
*/
public void testToString_ListofLists()
{
try
{
string = "{ \"list of lists\" : [ [1, 2, 3], [4, 5, 6], ] }";
jsonobject = new JSONObject(string);
assertEquals(
"<list of lists><array>1</array><array>2</array><array>3</array></list of lists><list of lists><array>4</array><array>5</array><array>6</array></list of lists>",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toJSONObject method using xml recipe.
*/
public void testToJSONObject_XmlRecipe()
{
try
{
string = "<recipe name=\"bread\" prep_time=\"5 mins\" cook_time=\"3 hours\"> <title>Basic bread</title> <ingredient amount=\"8\" unit=\"dL\">Flour</ingredient> <ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient> <ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient> <ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> ";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"recipe\": {\n \"title\": \"Basic bread\",\n \"cook_time\": \"3 hours\",\n \"instructions\": {\"step\": [\n \"Mix all ingredients together.\",\n \"Knead thoroughly.\",\n \"Cover with a cloth, and leave for one hour in warm room.\",\n \"Knead again.\",\n \"Place in a bread baking tin.\",\n \"Cover with a cloth, and leave for one hour in warm room.\",\n \"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]},\n \"name\": \"bread\",\n \"ingredient\": [\n {\n \"content\": \"Flour\",\n \"amount\": 8,\n \"unit\": \"dL\"\n },\n {\n \"content\": \"Yeast\",\n \"amount\": 10,\n \"unit\": \"grams\"\n },\n {\n \"content\": \"Water\",\n \"amount\": 4,\n \"unit\": \"dL\",\n \"state\": \"warm\"\n },\n {\n \"content\": \"Salt\",\n \"amount\": 1,\n \"unit\": \"teaspoon\"\n }\n ],\n \"prep_time\": \"5 mins\"\n}}",
jsonobject.toString(4));
} catch (JSONException e)
{