forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestJSONML.java
More file actions
782 lines (719 loc) · 36.5 KB
/
TestJSONML.java
File metadata and controls
782 lines (719 loc) · 36.5 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
/*
* File: TestJSONML.java Author: JSON.org
*/
package org.json.tests;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONML;
import org.json.JSONObject;
import org.json.XML;
import junit.framework.TestCase;
/**
* The Class TestJSONML.
*/
public class TestJSONML extends TestCase
{
/** The jsonobject. */
private JSONObject jsonobject;
/** The jsonarray. */
private JSONArray jsonarray;
/** The string. */
private String string;
/**
* Tests the toJsonArray method using open xml tag.
*/
public void testToJsonArray_OpenXmlTag()
{
try {
string = "<xml";
jsonarray = JSONML.toJSONArray(string);
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals("Misshaped element at 6 [character 7 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toJsonArray method using mismatched tags.
*/
public void testToJsonArray_MismatchedTags()
{
try {
string = "<right></wrong>";
jsonarray = JSONML.toJSONArray(string);
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals(
"Mismatched 'right' and 'wrong' at 15 [character 16 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toJsonArray method using text string.
*/
public void testToJsonArray_TextString()
{
try {
string = "This ain't XML.";
jsonarray = JSONML.toJSONArray(string);
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals("Bad XML at 17 [character 18 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toString method using xml recipe as json object.
*/
public void testToString_XmlRecipeAsJsonObject()
{
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 = JSONML.toJSONObject(string);
assertEquals(
"{\"cook_time\":\"3 hours\",\"name\":\"bread\",\"tagName\":\"recipe\",\"childNodes\":[{\"tagName\":\"title\",\"childNodes\":[\"Basic bread\"]},{\"amount\":8,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"childNodes\":[\"Flour\"]},{\"amount\":10,\"unit\":\"grams\",\"tagName\":\"ingredient\",\"childNodes\":[\"Yeast\"]},{\"amount\":4,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"state\":\"warm\",\"childNodes\":[\"Water\"]},{\"amount\":1,\"unit\":\"teaspoon\",\"tagName\":\"ingredient\",\"childNodes\":[\"Salt\"]},{\"tagName\":\"instructions\",\"childNodes\":[{\"tagName\":\"step\",\"childNodes\":[\"Mix all ingredients together.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead thoroughly.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead again.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Place in a bread baking tin.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Bake in the oven at 180(degrees)C for 30 minutes.\"]}]}],\"prep_time\":\"5 mins\"}",
jsonobject.toString());
assertEquals(
"<recipe cook_time=\"3 hours\" name=\"bread\" prep_time=\"5 mins\"><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>",
JSONML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using xml recipe as json array.
*/
public void testToString_XmlRecipeAsJsonArray()
{
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> ";
jsonarray = JSONML.toJSONArray(string);
assertEquals(
"[\n \"recipe\",\n {\n \"cook_time\": \"3 hours\",\n \"name\": \"bread\",\n \"prep_time\": \"5 mins\"\n },\n [\n \"title\",\n \"Basic bread\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 8,\n \"unit\": \"dL\"\n },\n \"Flour\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 10,\n \"unit\": \"grams\"\n },\n \"Yeast\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 4,\n \"unit\": \"dL\",\n \"state\": \"warm\"\n },\n \"Water\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 1,\n \"unit\": \"teaspoon\"\n },\n \"Salt\"\n ],\n [\n \"instructions\",\n [\n \"step\",\n \"Mix all ingredients together.\"\n ],\n [\n \"step\",\n \"Knead thoroughly.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Knead again.\"\n ],\n [\n \"step\",\n \"Place in a bread baking tin.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]\n ]\n]",
jsonarray.toString(4));
assertEquals(
"<recipe cook_time=\"3 hours\" name=\"bread\" prep_time=\"5 mins\"><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>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONObjectHtml method.
*/
public void testToJSONObjectHtml()
{
try
{
string = "<div id=\"demo\" class=\"JSONML\"><p>JSONML is a transformation between <b>JSON</b> and <b>XML</b> that preserves ordering of document features.</p><p>JSONML can work with JSON arrays or JSON objects.</p><p>Three<br/>little<br/>words</p></div>";
jsonobject = JSONML.toJSONObject(string);
assertEquals(
"{\n \"id\": \"demo\",\n \"tagName\": \"div\",\n \"class\": \"JSONML\",\n \"childNodes\": [\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"JSONML is a transformation between\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"JSON\"]\n },\n \"and\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"XML\"]\n },\n \"that preserves ordering of document features.\"\n ]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\"JSONML can work with JSON arrays or JSON objects.\"]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"Three\",\n {\"tagName\": \"br\"},\n \"little\",\n {\"tagName\": \"br\"},\n \"words\"\n ]\n }\n ]\n}",
jsonobject.toString(4));
assertEquals(
"<div id=\"demo\" class=\"JSONML\"><p>JSONML is a transformation between<b>JSON</b>and<b>XML</b>that preserves ordering of document features.</p><p>JSONML can work with JSON arrays or JSON objects.</p><p>Three<br/>little<br/>words</p></div>",
JSONML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONArrayHtml method.
*/
public void testToJSONArrayHtml()
{
try
{
string = "<div id=\"demo\" class=\"JSONML\"><p>JSONML is a transformation between <b>JSON</b> and <b>XML</b> that preserves ordering of document features.</p><p>JSONML can work with JSON arrays or JSON objects.</p><p>Three<br/>little<br/>words</p></div>";
jsonarray = JSONML.toJSONArray(string);
assertEquals(
"[\n \"div\",\n {\n \"id\": \"demo\",\n \"class\": \"JSONML\"\n },\n [\n \"p\",\n \"JSONML is a transformation between\",\n [\n \"b\",\n \"JSON\"\n ],\n \"and\",\n [\n \"b\",\n \"XML\"\n ],\n \"that preserves ordering of document features.\"\n ],\n [\n \"p\",\n \"JSONML can work with JSON arrays or JSON objects.\"\n ],\n [\n \"p\",\n \"Three\",\n [\"br\"],\n \"little\",\n [\"br\"],\n \"words\"\n ]\n]",
jsonarray.toString(4));
assertEquals(
"<div id=\"demo\" class=\"JSONML\"><p>JSONML is a transformation between<b>JSON</b>and<b>XML</b>that preserves ordering of document features.</p><p>JSONML can work with JSON arrays or JSON objects.</p><p>Three<br/>little<br/>words</p></div>",
JSONML.toString(jsonarray));
string = "{\"xmlns:soap\":\"http://www.w3.org/2003/05/soap-envelope\",\"tagName\":\"soap:Envelope\",\"childNodes\":[{\"tagName\":\"soap:Header\"},{\"tagName\":\"soap:Body\",\"childNodes\":[{\"tagName\":\"ws:listProducts\",\"childNodes\":[{\"tagName\":\"ws:delay\",\"childNodes\":[1]}]}]}],\"xmlns:ws\":\"http://warehouse.acme.com/ws\"}";
jsonobject = new JSONObject(string);
assertEquals(
"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ws=\"http://warehouse.acme.com/ws\"><soap:Header/><soap:Body><ws:listProducts><ws:delay>1</ws:delay></ws:listProducts></soap:Body></soap:Envelope>",
JSONML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonArray method using json information.
*/
public void testToJsonArray_JsonInformation()
{
try
{
string = "<xml one = 1 two=' \"2\" '><five></five>First \u0009<content><five></five> This is \"content\". <three> 3 </three>JSON does not preserve the sequencing of elements and contents.<three> III </three> <three> T H R E E</three><four/>Content text is an implied structure in XML. <six content=\"6\"/>JSON does not have implied structure:<seven>7</seven>everything is explicit.<![CDATA[CDATA blocks<are><supported>!]]></xml>";
jsonarray = JSONML.toJSONArray(string);
assertEquals(
"[\n \"xml\",\n {\n \"two\": \" \\\"2\\\" \",\n \"one\": 1\n },\n [\"five\"],\n \"First \\t<content>\",\n [\"five\"],\n \"This is \\\"content\\\".\",\n [\n \"three\",\n 3\n ],\n \"JSON does not preserve the sequencing of elements and contents.\",\n [\n \"three\",\n \"III\"\n ],\n [\n \"three\",\n \"T H R E E\"\n ],\n [\"four\"],\n \"Content text is an implied structure in XML.\",\n [\n \"six\",\n {\"content\": 6}\n ],\n \"JSON does not have implied structure:\",\n [\n \"seven\",\n 7\n ],\n \"everything is explicit.\",\n \"CDATA blocks<are><supported>!\"\n]",
jsonarray.toString(4));
assertEquals(
"<xml two=\" "2" \" one=\"1\"><five/>First \t<content><five/>This is "content".<three></three>JSON does not preserve the sequencing of elements and contents.<three>III</three><three>T H R E E</three><four/>Content text is an implied structure in XML.<six content=\"6\"/>JSON does not have implied structure:<seven></seven>everything is explicit.CDATA blocks<are><supported>!</xml>",
JSONML.toString(jsonarray));
}catch(JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonArray method using spanish numbers.
*/
public void testToJsonArray_SpanishNumbers()
{
try
{
string = "<xml do='0'>uno<a re='1' mi='2'>dos<b fa='3'/>tres<c>true</c>quatro</a>cinqo<d>seis<e/></d></xml>";
jsonarray = JSONML.toJSONArray(string);
assertEquals(
"[\n \"xml\",\n {\"do\": 0},\n \"uno\",\n [\n \"a\",\n {\n \"re\": 1,\n \"mi\": 2\n },\n \"dos\",\n [\n \"b\",\n {\"fa\": 3}\n ],\n \"tres\",\n [\n \"c\",\n true\n ],\n \"quatro\"\n ],\n \"cinqo\",\n [\n \"d\",\n \"seis\",\n [\"e\"]\n ]\n]",
jsonarray.toString(4));
assertEquals(
"<xml do=\"0\">uno<a re=\"1\" mi=\"2\">dos<b fa=\"3\"/>tres<c></c>quatro</a>cinqo<d>seis<e/></d></xml>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonArray method using music notes.
*/
public void testToJsonArray_MusicNotes()
{
try
{
string = "<a ichi='1' ni='2'><b>The content of b</b> and <c san='3'>The content of c</c><d>do</d><e></e><d>re</d><f/><d>mi</d></a>";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"a\":{\"f\":\"\",\"content\":\"and\",\"d\":[\"do\",\"re\",\"mi\"],\"ichi\":1,\"e\":\"\",\"b\":\"The content of b\",\"c\":{\"content\":\"The content of c\",\"san\":3},\"ni\":2}}",
jsonobject.toString());
assertEquals(
"<a><f/>and<d>do</d><d>re</d><d>mi</d><ichi>1</ichi><e/><b>The content of b</b><c>The content of c<san>3</san></c><ni>2</ni></a>",
XML.toString(jsonobject));
jsonarray = JSONML.toJSONArray(string);
assertEquals("[\n" + " \"a\",\n" + " {\n"
+ " \"ichi\": 1,\n" + " \"ni\": 2\n" + " },\n"
+ " [\n" + " \"b\",\n"
+ " \"The content of b\"\n" + " ],\n"
+ " \"and\",\n" + " [\n" + " \"c\",\n"
+ " {\"san\": 3},\n" + " \"The content of c\"\n"
+ " ],\n" + " [\n" + " \"d\",\n"
+ " \"do\"\n" + " ],\n" + " [\"e\"],\n"
+ " [\n" + " \"d\",\n" + " \"re\"\n"
+ " ],\n" + " [\"f\"],\n" + " [\n"
+ " \"d\",\n" + " \"mi\"\n" + " ]\n" + "]",
jsonarray.toString(4));
assertEquals(
"<a ichi=\"1\" ni=\"2\"><b>The content of b</b>and<c san=\"3\">The content of c</c><d>do</d><e/><d>re</d><f/><d>mi</d></a>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonArray method using table of contents.
*/
public void testToJsonArray_TableOfContents()
{
try
{
string = "<book><chapter>Content of the first chapter</chapter><chapter>Content of the second chapter <chapter>Content of the first subchapter</chapter> <chapter>Content of the second subchapter</chapter></chapter><chapter>Third Chapter</chapter></book>";
jsonarray = JSONML.toJSONArray(string);
assertEquals("[\n" + " \"book\",\n" + " [\n"
+ " \"chapter\",\n"
+ " \"Content of the first chapter\"\n" + " ],\n"
+ " [\n" + " \"chapter\",\n"
+ " \"Content of the second chapter\",\n"
+ " [\n" + " \"chapter\",\n"
+ " \"Content of the first subchapter\"\n"
+ " ],\n" + " [\n" + " \"chapter\",\n"
+ " \"Content of the second subchapter\"\n"
+ " ]\n" + " ],\n" + " [\n"
+ " \"chapter\",\n" + " \"Third Chapter\"\n"
+ " ]\n" + "]", jsonarray.toString(4));
assertEquals(
"<book><chapter>Content of the first chapter</chapter><chapter>Content of the second chapter<chapter>Content of the first subchapter</chapter><chapter>Content of the second subchapter</chapter></chapter><chapter>Third Chapter</chapter></book>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using message xml.
*/
public void testToJsonObject_MessageXml()
{
try
{
string = "<Root><MsgType type=\"node\"><BatchType type=\"string\">111111111111111</BatchType></MsgType></Root>";
jsonobject = JSONML.toJSONObject(string);
assertEquals(
"{\"tagName\":\"Root\",\"childNodes\":[{\"tagName\":\"MsgType\",\"childNodes\":[{\"tagName\":\"BatchType\",\"childNodes\":[111111111111111],\"type\":\"string\"}],\"type\":\"node\"}]}",
jsonobject.toString());
jsonarray = JSONML.toJSONArray(string);
assertEquals(
"[\"Root\",[\"MsgType\",{\"type\":\"node\"},[\"BatchType\",{\"type\":\"string\"},111111111111111]]]",
jsonarray.toString());
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonArray method using table mapping.
*/
public void testToJsonArray_TableMapping()
{
try
{
string = "<mapping><empty/> <class name = \"Customer\"> <field name = \"ID\" type = \"string\"> <bind-xml name=\"ID\" node=\"attribute\"/> </field> <field name = \"FirstName\" type = \"FirstName\"/> <field name = \"MI\" type = \"MI\"/> <field name = \"LastName\" type = \"LastName\"/> </class> <class name = \"FirstName\"> <field name = \"text\"> <bind-xml name = \"text\" node = \"text\"/> </field> </class> <class name = \"MI\"> <field name = \"text\"> <bind-xml name = \"text\" node = \"text\"/> </field> </class> <class name = \"LastName\"> <field name = \"text\"> <bind-xml name = \"text\" node = \"text\"/> </field> </class></mapping>";
jsonarray = JSONML.toJSONArray(string);
assertEquals(
"[\n \"mapping\",\n [\"empty\"],\n [\n \"class\",\n {\"name\": \"Customer\"},\n [\n \"field\",\n {\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n [\n \"bind-xml\",\n {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n }\n ]\n ],\n [\n \"field\",\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ]\n ],\n [\n \"class\",\n {\"name\": \"FirstName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"MI\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"LastName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ]\n]",
jsonarray.toString(4));
assertEquals(
"<mapping><empty/><class name=\"Customer\"><field name=\"ID\" type=\"string\"><bind-xml node=\"attribute\" name=\"ID\"/></field><field name=\"FirstName\" type=\"FirstName\"/><field name=\"MI\" type=\"MI\"/><field name=\"LastName\" type=\"LastName\"/></class><class name=\"FirstName\"><field name=\"text\"><bind-xml node=\"text\" name=\"text\"/></field></class><class name=\"MI\"><field name=\"text\"><bind-xml node=\"text\" name=\"text\"/></field></class><class name=\"LastName\"><field name=\"text\"><bind-xml node=\"text\" name=\"text\"/></field></class></mapping>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the constructor method.
*/
public static void testConstructor()
{
JSONML jsonml = new JSONML();
assertEquals("JSONML", jsonml.getClass().getSimpleName());
}
/**
* Tests the toJSONArray method using empty closing tag.
*/
public void testToJSONArray_EmptyClosingTag()
{
try
{
jsonarray = JSONML.toJSONArray("<abc></>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Expected a closing name instead of '>'.", e.getMessage());
}
}
/**
* Tests the toJSONArray method using closing tag with question.
*/
public void testToJSONArray_ClosingTagWithQuestion()
{
try
{
jsonarray = JSONML.toJSONArray("<abc></abc?>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Misshaped close tag at 11 [character 12 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONArray method using meta tag with two dashes.
*/
public void testToJSONArray_MetaTagWithTwoDashes()
{
try
{
jsonarray = JSONML.toJSONArray("<abc><!--abc--></abc>");
assertEquals(
"[\"abc\",\">\"]",
jsonarray.toString());
assertEquals(
"<abc>></abc>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONArray method using meta tag with one dash.
*/
public void testToJSONArray_MetaTagWithOneDash()
{
try
{
jsonarray = JSONML.toJSONArray("<abc><!-abc--></abc>");
assertEquals(
"[\"abc\",\"abc-->\"]",
jsonarray.toString());
assertEquals(
"<abc>abc--></abc>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONArray method using meta tag with cdata.
*/
public void testToJSONArray_MetaTagWithCdata()
{
try
{
jsonarray = JSONML.toJSONArray("<abc><![CDATA[<abc></abc>]]></abc>");
assertEquals(
"[\"abc\",\"<abc><\\/abc>\"]",
jsonarray.toString());
assertEquals(
"<abc><abc></abc></abc>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONArray method using meta tag with bad cdata.
*/
public void testToJSONArray_MetaTagWithBadCdata()
{
try
{
jsonarray = JSONML.toJSONArray("<abc><![CDATA[<abc></abc>?]></abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Unclosed CDATA at 35 [character 36 line 1]", e.getMessage());
}
try
{
jsonarray = JSONML.toJSONArray("<abc><![CDATA[<abc></abc>]?></abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Unclosed CDATA at 35 [character 36 line 1]", e.getMessage());
}
try
{
jsonarray = JSONML.toJSONArray("<abc><![CDAT[<abc></abc>]]></abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Expected 'CDATA[' at 12 [character 13 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONArray method using meta tag with cdata only.
*/
public void testToJSONArray_MetaTagWithCdataOnly()
{
try
{
jsonarray = JSONML.toJSONArray("<![CDATA[<abc></abc>]]>");
assertEquals(
"[\"abc\"]",
jsonarray.toString());
assertEquals(
"<abc/>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONArray method using meta tag with broken cdata.
*/
public void testToJSONArray_MetaTagWithBrokenCdata()
{
try
{
jsonarray = JSONML.toJSONArray("<!CDATA[<abc></abc>]]>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Bad XML at 23 [character 24 line 1]", e.getMessage());
}
try
{
jsonarray = JSONML.toJSONArray("<![CDATA?[abc]]>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Expected 'CDATA[' at 9 [character 10 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONArray method using php tag.
*/
public void testToJSONArray_PhpTag()
{
try
{
jsonarray = JSONML.toJSONArray("<abc><?abcde?></abc>");
assertEquals(
"[\"abc\"]",
jsonarray.toString());
assertEquals(
"<abc/>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONArray method using misshaped tag.
*/
public void testToJSONArray_MisshapedTag()
{
try
{
jsonarray = JSONML.toJSONArray("<abc><=abcde?></abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 7 [character 8 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using reserved attribute tag name.
*/
public void testToJSONObject_ReservedAttributeTagName()
{
try
{
jsonobject = JSONML.toJSONObject("<abc tagName=\"theName\">def</abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Reserved attribute. at 12 [character 13 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using reserved attribute child node.
*/
public void testToJSONObject_ReservedAttributeChildNode()
{
try
{
jsonobject = JSONML.toJSONObject("<abc childNode=\"theChild\">def</abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Reserved attribute. at 14 [character 15 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using no value attribute.
*/
public void testToJSONObject_NoValueAttribute()
{
try
{
jsonobject = JSONML.toJSONObject("<abc novalue>def</abc>");
assertEquals(
"{\"novalue\":\"\",\"tagName\":\"abc\",\"childNodes\":[\"def\"]}",
jsonobject.toString());
assertEquals(
"<abc novalue=\"\">def</abc>",
JSONML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONObject method using no value attribute with equals.
*/
public void testToJSONObject_NoValueAttributeWithEquals()
{
try
{
jsonobject = JSONML.toJSONObject("<abc novalue=>def</abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Missing value at 14 [character 15 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using empty tag.
*/
public void testToJSONObject_EmptyTag()
{
try
{
jsonobject = JSONML.toJSONObject("<abc/>");
assertEquals(
"{\"tagName\":\"abc\"}",
jsonobject.toString());
assertEquals(
"<abc/>",
JSONML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONArray method using empty tag.
*/
public void testToJSONArray_EmptyTag()
{
try
{
jsonarray = JSONML.toJSONArray("<abc/>");
assertEquals(
"[\"abc\"]",
jsonarray.toString());
assertEquals(
"<abc/>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONObject method using broken empty tag.
*/
public void testToJSONObject_BrokenEmptyTag()
{
try
{
jsonobject = JSONML.toJSONObject("<abc><def/?>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 11 [character 12 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using misshaped tag.
*/
public void testToJSONObject_MisshapedTag()
{
try
{
jsonobject = JSONML.toJSONObject("<abc?");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 5 [character 6 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using no close tag.
*/
public void testToJSONObject_NoCloseTag()
{
try
{
jsonobject = JSONML.toJSONObject("<abc>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Bad XML at 6 [character 7 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using no name tag.
*/
public void testToJSONObject_NoNameTag()
{
try
{
jsonobject = JSONML.toJSONObject("<>");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 2 [character 3 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using space.
*/
public void testToJSONObject_Space()
{
try
{
jsonobject = JSONML.toJSONObject(" ");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Bad XML at 3 [character 4 line 1]", e.getMessage());
}
}
/**
* Tests the toJSONObject method using space content.
*/
public void testToJSONObject_SpaceContent()
{
try
{
jsonobject = JSONML.toJSONObject("<abc> </abc>");
assertEquals(
"{\"tagName\":\"abc\"}",
jsonobject.toString());
assertEquals(
"<abc/>",
JSONML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using json array of json objects.
*/
public void testToString_JsonArrayOfJsonObjects()
{
try
{
jsonarray = new JSONArray();
jsonarray.put("tagName");
jsonarray.put(new JSONObject().put("tagName", "myName"));
jsonarray.put(new JSONObject().put("tagName", "otherName"));
assertEquals(
"<tagName tagName=\"myName\"><otherName/></tagName>",
JSONML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using json object of json arrays.
*/
public void testToString_JsonObjectOfJsonArrays()
{
try
{
jsonobject = new JSONObject();
jsonobject.put("tagName", "MyName");
jsonobject.put("childNodes", new JSONArray().put("abc").put(new JSONArray().put("def")));
jsonobject.put("123", new JSONArray("[\"abc\"]"));
assertEquals(
"<MyName 123=\"["abc"]\">abc<def/></MyName>",
JSONML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
}