# $Id: TestDomHelpers.py 1058 2009-01-26 10:39:19Z graham $
#
# Unit testing for WebBrick library functions (Functions.py)
# See http://pyunit.sourceforge.net/pyunit.html
#

import sys
import unittest
import logging
from os import *
from os.path import *

sys.path.append("../..")
from MiscLib.DomHelpers import *

class TestDomHelpers(unittest.TestCase):

    def setUp(self):
        self.testdoc = ( """<?xml version="1.0" encoding="iso-8859-1" ?>
<root attr="attrtext">
  <child1>
  some text
  <child11 />
  more text
  <child12>child text</child12>
  final text
  </child1>
</root>""" )
        self.testpath = "resources/"
        self.testfile = self.testpath+"TestDomHelpers.xml"
        self.savefile = self.testpath+"TestDomHelpersSave.xml"
        return

    def tearDown(self):
        return

    def doAssert(self, cond, msg):
        assert cond , msg

    # Actual tests follow

    def testParseXmlString(self):
        assert parseXmlString(self.testdoc), "Parse XML string failed"

    def testParseXmlFile(self):
        assert parseXmlFile(self.testfile), "Parse XML file failed"

    def testSaveXmlToFile(self):
        # ensure clean first
        logging.debug( self.testfile )
        logging.debug( self.savefile )
        try:
            remove( self.savefile )
        except Exception:
            pass
        testDom = parseXmlFile(self.testfile)
        saveXmlToFile(self.savefile, testDom, False)
        
        assert exists(self.savefile), "save XML file failed"
        # could expand test and run compare of data.
#        remove( self.savefile )

    def testSaveXmlToFileWithBackup(self):
        # ensure clean first
        try:
            remove( self.savefile )
        except Exception:
            pass
        testDom = parseXmlFile(self.testfile)
        saveXmlToFile(self.savefile, testDom)
        saveXmlToFile(self.savefile, testDom)
        
        assert exists(self.savefile), "save XML file failed"
        assert exists(self.savefile+".bak"), "save XML file failed"

        remove( self.savefile+".bak" )
        remove( self.savefile )

    def testGetNamedElem1(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "child1")
        assert elm, "Node not found: child1"

    def testGetNamedElem2(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "child12")
        assert elm, "Node not found: child12"

    def testGetNamedElem3(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "child99")
        assert not elm, "Unexpected dode found: child99"

    #TODO: deprecate me
    def testGetNamedNode(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedNode(dom, "child1")
        assert elm, "Node not found: child1"

    def testElemText(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "child1")
        txt = getElemText(elm)
        assert txt == "\n  some text\n  \n  more text\n  \n  final text\n  ", \
                      "Wrong element text: "+repr(txt)

    def testAttrText(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "root")
        txt = getAttrText(elm,"attr")
        assert txt == "attrtext", "Wrong attribute text: "+txt

    def testNamedNodeAttrText(self):
        dom = parseXmlString(self.testdoc)
        txt = getNamedNodeAttrText(dom, "root", "attr")
        self.assertEqual( txt, "attrtext" )

    def testNodeListText(self):
        dom = parseXmlFile(self.testfile)
        elm = dom.getElementsByTagName("child1")[0]
        txt = getNodeListText(elm.childNodes)
        assert txt == "\n    some text\n    \n    more text\n    \n    final text\n  ", \
                      "Wrong element text: "+repr(txt)

    def testGetNamedNodeText(self):
        dom = parseXmlFile(self.testfile)
        txt = getNamedNodeText(dom, "child1")
        self.assertEqual( txt, "\n    some text\n    \n    more text\n    \n    final text\n  " )

    def testgetElemXml(self):
        dom = parseXmlFile(self.testfile)
        txt = getElemXml( dom.getElementsByTagName("child1")[0] )
        self.assertEqual( txt, "<child1>\n    some text\n    <child11/>\n    more text\n    <child12>child text</child12>\n    final text\n  </child1>" )

    def testgetElemPrettyXml(self):
        dom = parseXmlFile(self.testfile)
        txt = getElemPrettyXml( dom.getElementsByTagName("child1")[0] )
        self.assertEqual( txt, "<child1>\n  \n    some text\n    \n  <child11/>\n  \n    more text\n    \n  <child12>\n    child text\n  </child12>\n  \n    final text\n  \n</child1>\n" )

    def testGetNamedNodeXml(self):
        dom = parseXmlFile(self.testfile)
        txt = getNamedNodeXml( dom, "child1" )
        self.assertEqual( txt, "<child1>\n    some text\n    <child11/>\n    more text\n    <child12>child text</child12>\n    final text\n  </child1>" )

    # --- node type tests ---

    def testIsAttribute(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "root")
        atr = elm.getAttributeNode("attr")
        assert isAttribute(atr),"isAttribute test (root/@attr) failed"

    def testIsAttributeElem(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "root")
        assert not isAttribute(elm),"isAttribute false test (root) failed"

    def testIsAttributeText(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "root")
        txt = elm.childNodes[0]
        assert not isAttribute(elm),"isAttribute false test (root/[0]) failed"

    def testIsElement(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "root")
        assert isElement(elm),"isElement test (root/@attr) failed"

    def testIsElementAttr(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "root")
        atr = elm.getAttributeNode("attr")
        assert not isElement(atr),"isElement false test (root/@attr) failed"

    def testIsElementText(self):
        dom = parseXmlString(self.testdoc)
        elm = getNamedElem(dom, "root")
        txt = elm.childNodes[0]