1: #!/usr/bin/python
2: # -*- coding: utf-8 -*-
3:
4: import sys
5:
6: # import base library
7: import axl
8:
9: def test_01():
10: # do two consecutive parse operations
11: (doc, err) = axl.parse ("<?xml version='1.0' ?><axldoc />", 32)
12: if err:
13: error ("Found error: " + str (err.code) + ", message: " + err.msg)
14: return False
15:
16: (doc, err) = axl.parse ("<?xml version=\"1.0\" \t \n \r encoding='utf-8\" ?> <data />", 63)
17: if err:
18: error ("Found error: " + str (err.code) + ", message: " + err.msg)
19: return False
20:
21: # check doc encoding
22: if doc.encoding != "utf-8":
23: error ("Error, expected to find enconding value utf-8 but found: " + doc.encoding)
24: return False
25:
26: # do a fail parse operation
27: (doc, err) = axl.parse ("aefadsadsf<?xml version=\"1.0\" \t \n \r encoding='utf-8\" ?> <data />", 73)
28: if not err:
29: error ("Expected to find error but found no error report")
30: return False
31:
32: # check error returned
33: if not err.code == -2:
34: error ("Expected to find error code -2 but found: " + str (err.code))
35:
36: # check standalone configuration
37: (doc, err) = axl.parse ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <data/>", 65)
38: if err:
39: error ("Expected to find proper document parse but a failure was found: " + err.msg)
40: return False
41:
42: if not doc.standalone:
43: error ("Expected to find standalone enabled but found it disabled")
44: return False
45:
46: return True
47:
48: def test_01b():
49: # parse document
50: (doc, err) = axl.file_parse ("test_01b.xml")
51: if err:
52: error ("Found error: " + str (err.code) + ", message: " + err.msg)
53: return False
54:
55: info ("Test 01-b: finished document parsing, getting root node")
56:
57: # get root node
58: node = doc.root
59:
60: info ("Test 01-b: checking node name..")
61:
62: # check node name
63: if node.name != "document":
64: error ("Expected to find node name 'document' but found: " + node.name)
65: return False
66:
67: info ("Test 01-b: about to get first reference..")
68:
69: # get first child
70: node = node.first_child
71:
72: info ("Test 01-b: returning..")
73:
74: # check node name
75: if node.name != "child1":
76: error ("Expected to find node name 'child1' but found: " + node.name)
77: return False
78:
79: # get next
80: node = node.next
81:
82: # check node name
83: if node.name != "child2":
84: error ("Expected to find node name 'child2' but found: " + node.name)
85: return False
86:
87: return True
88:
89: def test_01c():
90: # parse document
91: (doc, err) = axl.file_parse ("test_01b.xml")
92: if err:
93: error ("Found error: " + str (err.code) + ", message: " + err.msg)
94: return False
95:
96: # get root node
97: node = doc.root
98:
99: # check node name
100: if node.name != "document":
101: error ("Expected to find node name 'document' but found: " + node.name)
102: return False
103:
104: # get first child
105: node = node.first_child
106:
107: # check node name
108: if node.name != "child1":
109: error ("Expected to find node name 'child1' but found: " + node.name)
110: return False
111:
112: # get parent
113: parent = node.parent
114:
115: # check node name
116: if parent.name != "document":
117: error ("Expected to find node name 'document' but found: " + parent.name)
118: return False
119:
120: node = node.next
121:
122: # check node name
123: if node.name != "child2":
124: error ("Expected to find node name 'child2' but found: " + node.name)
125: return False
126:
127:
128: # check parent again
129: parent = node.parent
130:
131: # check node name
132: if parent.name != "document":
133: error ("Expected to find node name 'document' but found: " + parent.name)
134: return False
135:
136: node = node.next
137:
138: # check node name
139: if node.name != "child3":
140: error ("Expected to find node name 'child3' but found: " + node.name)
141: return False
142:
143: # check parent again
144: parent = node.parent
145:
146: # check node name
147: if parent.name != "document":
148: error ("Expected to find node name 'document' but found: " + parent.name)
149: return False
150:
151: node = node.next
152:
153: # check node name
154: if node.name != "child4":
155: error ("Expected to find node name 'child4' but found: " + node.name)
156: return False
157:
158: # check parent again
159: parent = node.parent
160:
161: # check node name
162: if parent.name != "document":
163: error ("Expected to find node name 'document' but found: " + parent.name)
164: return False
165:
166: node = node.next
167:
168: # check node name
169: if node.name != "child5":
170: error ("Expected to find node name 'child5' but found: " + node.name)
171: return False
172:
173: # check next is none
174: if node.next:
175: error ("Expected to find none value for the next child node found next to child5")
176: return False
177:
178: node = node.previous
179:
180: # check node name
181: if node.name != "child4":
182: error ("Expected to find node name 'child4' but found: " + node.name)
183: return False
184:
185: # check node name
186: if parent.name != "document":
187: error ("Expected to find node name 'document' but found: " + parent.name)
188: return False
189:
190: node = node.previous
191:
192: # check node name
193: if node.name != "child3":
194: error ("Expected to find node name 'child3' but found: " + node.name)
195: return False
196:
197: # check node name
198: if parent.name != "document":
199: error ("Expected to find node name 'document' but found: " + parent.name)
200: return False
201:
202: node = node.previous
203:
204: # check node name
205: if node.name != "child2":
206: error ("Expected to find node name 'child2' but found: " + node.name)
207: return False
208:
209: # check node name
210: if parent.name != "document":
211: error ("Expected to find node name 'document' but found: " + parent.name)
212: return False
213:
214: node = node.previous
215:
216: # check node name
217: if node.name != "child1":
218: error ("Expected to find node name 'child1' but found: " + node.name)
219: return False
220:
221: # check node name
222: if parent.name != "document":
223: error ("Expected to find node name 'document' but found: " + parent.name)
224: return False
225:
226: # check previous is none
227: if node.previous:
228: error ("Expected to find none value for the previous child node found previous to child1")
229: return False
230:
231: # now test called api
232: node = doc.root.first_child
233:
234: # get <child5>
235: node = node.next_called ("child5")
236: if node.name != "child5":
237: error ("Expected to find child node with name child5 but found: " + node.name)
238: return False
239:
240: # get <child5>
241: node = node.next_called ("child5")
242: if node:
243: error ("Expected to find none node after calling next_called with child5 but found: " + node.name)
244: return False
245:
246: node = doc.root.first_child.next_called ("child5")
247: if not node:
248: error ("Expected to find a xml node but found none")
249: return False
250: if node.name != "child5":
251: error ("Expected to find child node child5 but found: " + node.name)
252: return False
253:
254: # get <child1>
255: node = node.previous_called ("child1")
256: if not node:
257: error ("Expected to find a xml node child1 but found none")
258: return False
259: if node.name != "child1":
260: error ("Expected to find child node child1 but found: " + node.name)
261: return False
262:
263: node = node.previous_called ("child1")
264: if node:
265: error ("Expected to not find a xml node but found somethind defined: " + node.name)
266: return False
267:
268: return True
269:
270: def test_01d():
271: (doc, err) = axl.file_parse ("test_01d.xml")
272: if err:
273: error ("Expected to find proper parse operation but found an error: " + err.msg)
274: return False
275:
276: child = doc.root.nth_child (0)
277: if child.name != "child1":
278: error ("Expected to find child1 node but found: " + child.name)
279: return False
280:
281: child = doc.root.nth_child (1)
282: if child.name != "child2":
283: error ("Expected to find child2 node but found: " + child.name)
284: return False
285:
286: child = doc.root.nth_child (2)
287: if child.name != "child3":
288: error ("(2) Expected to find child3 node but found: " + child.name)
289: return False
290:
291: child = doc.root.nth_child (3)
292: if child.name != "child4":
293: error ("Expected to find child4 node but found: " + child.name)
294: return False
295:
296: child = doc.root.nth_child (4)
297: if child.name != "child5":
298: error ("Expected to find child5 node but found: " + child.name)
299: return False
300:
301: # get the child 0 located at the child 2
302: child = doc.root.nth_child (2).nth_child (0)
303:
304: if child.name != "a":
305: error ("Expected to find node name <a> but found: " + child.name)
306: return False
307:
308: child = doc.root.nth_child (2).nth_child (1)
309:
310: if child.name != "b":
311: error ("Expected to find node name <b> but found: " + child.name)
312: return False
313:
314: child = doc.root.nth_child (2).nth_child (2)
315:
316: if child.name != "c":
317: error ("Expected to find node name <c> but found: " + child.name)
318: return False
319:
320: child = doc.root.nth_child (2).nth_child (3)
321:
322: if child.name != "f":
323: error ("Expected to find node name <f> but found: " + child.name)
324: return False
325:
326:
327: child = doc.root.nth_child (2).nth_child (4)
328:
329: if child.name != "g":
330: error ("Expected to find node name <g> but found: " + child.name)
331: return False
332:
333: return True
334:
335: def test_01f():
336:
337: # parse document
338: (doc, err) = axl.file_parse ("test_01f.xml")
339: if err:
340: error ("Expected proper document parse but found a failure: " + err.msg)
341: return False
342:
343: # get root content
344: (content, size) = doc.root.content
345:
346: # check content
347: if content != " ":
348: error ("Expected to find ' ' but found: " + content)
349: return False
350:
351: # parse another document
352: (doc, err) = axl.file_parse ("test_01f2.xml")
353: if err:
354: error ("Expected proper document parse but found a failure: " + err.msg)
355: return False
356:
357: # get content from node id found as child of the root node
358: (content, size) = doc.root.child_called ("id").content
359:
360: # check content
361: if content != " ":
362: error ("(2) Expected to find ' ' but found: " + content)
363: return False
364:
365: (content, size) = doc.root.child_called ("id2").content
366:
367: # check content
368: if content != "":
369: error ("Expected to find '' but found: " + content)
370: return False
371:
372: return True
373:
374: def test_03():
375: (doc, err) = axl.parse ("<?xml version='1.0' ?>\n\
376: <complex>\n\
377: <data>\n\
378: <row>10</row><row>20</row><row>30</row><row>40</row>\n\
379: </data>\n\
380: <data2>\n\
381: <td> 23 </td>\n\
382: </data2>\n\
383: </complex>", -1)
384:
385: if err:
386: error ("Expected to find proper document load, but a failure was found: " + err.msg)
387: return False
388:
389: if doc.root.name != "complex":
390: error ("Expected to find complex node name but found: " + doc.root.name)
391: return False
392:
393: node = doc.get ("/complex/data2/td")
394: if not node:
395: error ("Expected to find node found at /complex/data2/td")
396: return False
397:
398: # check node name
399: if node.name != "td":
400: error ("Expected to find node name td but found: " + node.name)
401: return False
402:
403: node = doc.get ("/complex/data3/td")
404: if node:
405: error ("Expected to not find node in wrong path but found: " + node.name)
406: return False
407:
408: (content, size) = doc.get ("/complex/data2/td").content
409: if content != " 23 ":
410: error ("Expected to find content ' 23 ' but found: " + content)
411: return False
412:
413: node = doc.get ("complex/data3/td")
414: if node:
415: error ("Expected to find no node but found: " + node.name)
416: return False
417:
418: return True
419:
420: def test_04():
421: (doc, err) = axl.parse ("<?xml version='1.0' ?>" +
422: " <?test \"my content\" ?>" +
423: " <complex>" +
424: " <data>" +
425: " <row>" +
426: " <!-- A comment inside the middle of the document " +
427: " more comments ... " +
428: " more comments \n \r <td>..</td> -->" +
429: " <td>10</td>" +
430: " <test />" +
431: " \n \n \r <?test \"my content\" ?> \n \n" +
432: " <?test \"my content\" ?>" +
433: " <more>" +
434: " <test3 attr='2.0' />" +
435: " </more>" +
436: " </row>" +
437: " <test2 />" +
438: " <non-xml-document>" +
439: " \n \r \n \t" +
440: " <![CDATA[<xml><<<<<<>>>>>><<<>>>><<<<<<>>>]]>"
441: " \r \n \r \t" +
442: " </non-xml-document>" +
443: " </data>" +
444: " <!-- <data>" +
445: " <row>" +
446: " A comment inside the middle of the document " +
447: " more comments ... " +
448: " more comments \n \r <td>..</td> " +
449: " <td>10</td>" +
450: " <test />" +
451: " <more>" +
452: " <test2 attr='2.0' attr2='3.0' attr4='4.0'/>" +
453: " </more>" +
454: " </row>" +
455: " <test2 />" +
456: " </data> -->" +
457: " </complex>", -1)
458:
459: if err:
460: error ("Expected to find proper parse result but found a failure: " + err.msg)
461: return False
462:
463: if doc.get ("/complex/data/row/td").content[0] != "10":
464: error ("Expected to find content 10 but found: " + doc.get ("/complex/data/row/td").content[0])
465: return False
466:
467: node = doc.get ("/complex/data/row/more/test3")
468:
469: if not node.has_attr ("attr"):
470: error ("Expected to find attribute attr but not found")
471: return False
472:
473: # now check attr content
474: if node.attr ("attr") != "2.0":
475: error ("Expected to find attribute value 2.0 but found: " + node.attr ("attr"))
476: return False
477:
478: # check pi instructions support
479: if not doc.has_pi ("test"):
480: error ("Expected to find pi instruction test but not found")
481: return False
482:
483: if doc.pi ("test") != "\"my content\"":
484: error ("Expected to find 'my content' but found: " + doc.pi ("test"))
485: return False
486:
487: # check node content
488: if doc.get ("/complex/data/non-xml-document").content[0] != "<xml><<<<<<>>>>>><<<>>>><<<<<<>>>":
489: error ("Expected to find <xml><<<<<<>>>>>><<<>>>><<<<<<>>> but found: " + doc.get ("/complex/data/non-xml-document").content[0])
490: return False
491:
492: return True
493:
494: def test_05():
495:
496: (doc, err) = axl.file_parse ("test.xml")
497:
498: if doc.get ("/complex/data/row/td").content[0] != "10":
499: error ("Expected to find 10 but found: " + doc.get ("/complex/data/row/td").content[0])
500: return False
501:
502: (doc, err) = axl.file_parse ("test2.xml")
503: if err:
504: error ("Expected to find proper document parsing, but error was found: " + err.msg)
505: return False
506:
507: (dtd, err) = axl.dtd_file_parse ("test.dtd")
508: if err:
509: error ("Expected to find proper dtd parsing, but error was found: " + err.msg)
510: return False
511:
512: # validate content
513: err = dtd.validate (doc)
514: if err:
515: error ("Expected to find proper DTD validation, but a failure was found: " + err.msg)
516: return False
517:
518: return True
519:
520: def test_22():
521:
522: # create a document
523: doc = axl.Doc ()
524:
525: # set root node
526: doc.root = axl.Node ("root-node")
527:
528: # check if the node has an attribute not available
529: if doc.root.has_attr ("attribute-not-found"):
530: error ("Expected to not find attribute: attribute-not-found, but found it")
531: return False
532:
533: # check none value
534: if doc.root.attr ("attribute-not-found"):
535: error ("Expected to find None value associated to missing attribute, but found something defined")
536: return False
537:
538: # configure attribute
539: doc.root.attr ("attribute1", "value1")
540:
541: # check if the node has an attribute available
542: if not doc.root.has_attr ("attribute1"):
543: error ("Expected to find attribute: attribute1, but it wasn't found")
544: return False
545:
546: if not doc.root.attr ("attribute1") == "value1":
547: error ("Expected to find value1 as value associated to attribute1 but found: " + doc.root.attr ("attribute1"))
548: return False
549:
550: # set more attributes
551: doc.root.attr ("attribute2", "value2")
552: doc.root.attr ("attribute3", "value3")
553: doc.root.attr ("attribute4", "value4")
554: doc.root.attr ("attribute5", "value5")
555: doc.root.attr ("attribute6", "value6")
556:
557: # check attr iterator
558: cursor = doc.root.attr_cursor_new ()
559: while cursor.has_item ():
560: if cursor.key == "attribute2" and cursor.value != "value2":
561: error ("Expected to find value2 for attribute2 but found: " + cursor.value)
562: return False
563: if cursor.key == "attribute3" and cursor.value != "value3":
564: error ("Expected to find value3 for attribute3 but found: " + cursor.value)
565: return False
566: if cursor.key == "attribute4" and cursor.value != "value4":
567: error ("Expected to find value4 for attribute4 but found: " + cursor.value)
568: return False
569: if cursor.key == "attribute5" and cursor.value != "value5":
570: error ("Expected to find value5 for attribute5 but found: " + cursor.value)
571: return False
572: if cursor.key == "attribute6" and cursor.value != "value6":
573: error ("Expected to find value6 for attribute6 but found: " + cursor.value)
574: return False
575:
576: # next cursor
577: cursor.next ()
578:
579: return True
580:
581: def test_33():
582:
583: # creates a document with default version, default encoding and standalone = true
584: doc = axl.Doc ()
585:
586: # create a node
587: node = axl.Node ("test")
588:
589: # set as root
590: doc.root = node
591:
592: iterator = 0
593: while iterator < 2:
594: # get a temp reference to the current node
595: temp = doc.root
596:
597: # create a new root
598: node = axl.Node ("test")
599:
600: # set new root
601: doc.root = node
602:
603: # set new child
604: node.set_child (temp)
605:
606: # next position
607: iterator += 1
608:
609: # now create empty nodes
610: node = axl.Node ("test")
611:
612: return True
613:
614: def py_test_01():
615:
616: # parse content
617: (doc, err) = axl.parse ("<content />")
618: if err:
619: error ("Expected to find proper parse operation but found an error: " + err.msg)
620: return False
621:
622: # get the node
623: node = doc.root
624:
625: # get document containing node
626: doc2 = node.doc
627:
628: if doc2.root.name != "content":
629: error ("Expected to find node name: content but found: " + doc2.root.name)
630: return False
631:
632: return True
633:
634: def py_test_02():
635:
636: # parse content
637: (doc, err) = axl.parse ("<content><load><value test='10' /></load></content>")
638: if err:
639: error ("Expected to find proper parse operation but found an error: " + err.msg)
640: return False
641:
642: # get the node
643: node = doc.get ("/content/load/value")
644:
645: node2 = axl.Node ("test")
646: node2.attr ('id', "10")
647:
648: # do replace
649: node.replace (node2)
650:
651: (content, size) = doc.dump ()
652:
653: # check value
654: if content != "<?xml version='1.0' ?><content><load><test id='10' /></load></content>":
655: return False
656:
657: # now swap two nodes
658: (doc, err) = axl.parse ("<content><load><value test='10' /><value2 test='20' /></load></content>")
659: if err:
660: error ("Expected to find proper parse operation but found an error: " + err.msg)
661: return False
662:
663: node = doc.get ("/content/load/value")
664: node2 = doc.get ("/content/load/value2")
665:
666: # disconnect node from document
667: node.deattach ()
668:
669: # now place after
670: node2.set_child_after (node)
671:
672: (content, size) = doc.dump ()
673:
674: # check value
675: if content != "<?xml version='1.0' ?><content><load><value2 test='20' /><value test='10' /></load></content>":
676: return False
677:
678: return True
679:
680: def py_test_03():
681: # parse content
682: (doc, err) = axl.parse ("<content><load><value test='10' /></load></content>")
683: if err:
684: error ("Expected to find proper parse operation but found an error: " + err.msg)
685: return False
686:
687: # get the node
688: node = doc.get ("/content/load/value")
689:
690: info ("Got root node, creating child nodes..")
691:
692: iterator = 0
693: while iterator < 100:
694: # now add content to the node
695: # info ("About to create child node, iterator=" + str (iterator))
696: node2 = axl.Node ("test")
697: node.set_child (node2)
698:
699: iterator += 1
700:
701: return True
702:
703: def py_test_04_load ():
704: # parse content
705: (doc, err) = axl.parse ("<content><load><value test='10' /></load></content>")
706: if err:
707: error ("Expected to find proper parse operation but found an error: " + err.msg)
708: return False
709:
710: # get the node
711: return doc.get ("/content/load/value")
712:
713: def py_test_04():
714: # call to get the node from a document
715: node = py_test_04_load ()
716:
717: iterator = 0
718: while iterator < 100:
719:
720: # check value
721: if node.attr ("test") != "10":
722: return False
723:
724: # next position
725: iterator += 1
726:
727: return True
728:
729: def py_test_05 ():
730: # create empty node
731: node = axl.Node ("test")
732:
733: # set is as child
734: node.set_child (axl.Node ("test2"))
735:
736: # query empty
737: node.child_called ("test")
738: node.child_called ("test")
739:
740: del node
741: return True
742:
743:
744:
745: ###########################
746: # intraestructure support #
747: ###########################
748:
749: def info (msg):
750: print "[ INFO ] : " + msg
751:
752: def error (msg):
753: print "[ ERROR ] : " + msg
754:
755: def ok (msg):
756: print "[ OK ] : " + msg
757:
758: def run_all_tests ():
759: test_count = 0
760: for test in tests:
761:
762: # print log
763: info ("TEST-" + str(test_count) + ": Running " + test[1])
764:
765: # call test
766: if not test[0]():
767: error ("detected test failure at: " + test[1])
768: return False
769:
770: # next test
771: test_count += 1
772:
773: ok ("All tests ok!")
774: return True
775:
776: # declare list of tests available
777: tests = [
778: # note test functions starting with test_ are using the same reg test
779: # as defined by test_01.c (axl reg test). Tests that are specific to
780: # py-axl must start with py_test_.
781: (test_01, "Check PyVortex context initialization"),
782: (test_01b, "Check Basic XML parsing, XML document position"),
783: (test_01c, "Check Basic XML parsing, XML document traversing"),
784: (test_01d, "Check Basic XML parsing, node nth access"),
785: (test_01f, "Check Basic XML parsing, white space node content"),
786: (test_03, "Check complex xml error detection"),
787: (test_04, "Check complex xml parsing"),
788: (test_05, "Check DTD basic parsing"),
789: (test_22, "Check Axl node attributes"),
790: (test_33, "Check Recursive root node replace"),
791: (py_test_01, "Check PyAxlNode type attributes"),
792: (py_test_02, "Check PyAxlNode replace, deattach, set_child_after method"),
793: (py_test_03, "Check PyAxlNode and PyAxlDoc relation"),
794: (py_test_04, "Check PyAxlNode reference after PyAxlDoc reference finish"),
795: (py_test_05, "Check PyAxlNode setting childs references crated")
796: ]
797:
798: info (" LibAxl: Another XML library (regression test).")
799: info (" Copyright (C) 2008 Advanced Software Production Line, S.L.")
800: info (" Axl regression tests: version=" + axl.VERSION)
801: info (" To gather information about time performance you can use:")
802: info ("")
803: info (" >> time ./test_01.py")
804: info ("")
805: info (" To gather information about memory consumed (and leaks) use:")
806: info ("")
807: info (" >> valgrind --leak-check=yes --show-reachable=yes --suppressions=suppressions.valgrind --gen-suppressions=yes ./test_01.py ")
808: info ("")
809: info (" Report bugs to:")
810: info ("")
811: info (" <axl@lists.aspl.es> Axl mailing list")
812: info ("")
813:
814: if __name__ == '__main__':
815: iterator = 0
816: for arg in sys.argv:
817: # according to the argument position, take the value
818: if iterator == 1:
819: host = arg
820: elif iterator == 2:
821: port = arg
822:
823: # next iterator
824: iterator += 1
825:
826: # call to run all tests
827: run_all_tests ()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>