#
# @file    TestXMLNode.py
# @brief   XMLNode unit tests
#
# @author  Akiya Jouraku (Python conversion)
# @author  Michael Hucka <mhucka@caltech.edu> 
#
# $Id: TestXMLNode.py 8990 2009-02-03 04:32:06Z ajouraku $
# $HeadURL: https://sbml.svn.sourceforge.net/svnroot/sbml/trunk/libsbml/src/bindings/python/test/xml/TestXMLNode.py $
#
# This test file was converted from src/sbml/test/TestXMLNode.c
# with the help of conversion sciprt (ctest_converter.pl).
#
#<!---------------------------------------------------------------------------
# This file is part of libSBML.  Please visit http://sbml.org for more
# information about SBML, and the latest version of libSBML.
#
# Copyright 2005-2009 California Institute of Technology.
# Copyright 2002-2005 California Institute of Technology and
#                     Japan Science and Technology Corporation.
# 
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation.  A copy of the license agreement is provided
# in the file named "LICENSE.txt" included with this software distribution
# and also available online as http://sbml.org/software/libsbml/license.html
#--------------------------------------------------------------------------->*/
import sys
import unittest
import libsbml

def wrapString(s):
  return s
  pass

class TestXMLNode(unittest.TestCase):


  def test_XMLNode_attribute_add_remove(self):
    triple = libsbml.XMLTriple("test","","")
    attr = libsbml.XMLAttributes()
    node = libsbml.XMLNode(triple,attr)
    xt1 = libsbml.XMLTriple("name1", "http://name1.org/", "p1")
    xt2 = libsbml.XMLTriple("name2", "http://name2.org/", "p2")
    xt3 = libsbml.XMLTriple("name3", "http://name3.org/", "p3")
    xt1a = libsbml.XMLTriple("name1", "http://name1a.org/", "p1a")
    xt2a = libsbml.XMLTriple("name2", "http://name2a.org/", "p2a")
    node.addAttr( "name1", "val1", "http://name1.org/", "p1")
    node.addAttr(xt2, "val2")
    self.assert_( node.getAttributesLength() == 2 )
    self.assert_( node.isAttributesEmpty() == False )
    self.assert_( (  "name1" != node.getAttrName(0) ) == False )
    self.assert_( (  "val1"  != node.getAttrValue(0) ) == False )
    self.assert_( (  "http://name1.org/" != node.getAttrURI(0) ) == False )
    self.assert_( (  "p1"    != node.getAttrPrefix(0) ) == False )
    self.assert_( (  "name2" != node.getAttrName(1) ) == False )
    self.assert_( (  "val2"  != node.getAttrValue(1) ) == False )
    self.assert_( (  "http://name2.org/" != node.getAttrURI(1) ) == False )
    self.assert_( (  "p2"    != node.getAttrPrefix(1) ) == False )
    self.assert_( node.getAttrValue( "name1") == "" )
    self.assert_( node.getAttrValue( "name2") == "" )
    self.assert_( (  "val1"  != node.getAttrValue( "name1", "http://name1.org/") ) == False )
    self.assert_( (  "val2"  != node.getAttrValue( "name2", "http://name2.org/") ) == False )
    self.assert_( (  "val1"  != node.getAttrValue(xt1) ) == False )
    self.assert_( (  "val2"  != node.getAttrValue(xt2) ) == False )
    self.assert_( node.hasAttr(-1) == False )
    self.assert_( node.hasAttr(2) == False )
    self.assert_( node.hasAttr(0) == True )
    self.assert_( node.hasAttr( "name1", "http://name1.org/") == True )
    self.assert_( node.hasAttr( "name2", "http://name2.org/") == True )
    self.assert_( node.hasAttr( "name3", "http://name3.org/") == False )
    self.assert_( node.hasAttr(xt1) == True )
    self.assert_( node.hasAttr(xt2) == True )
    self.assert_( node.hasAttr(xt3) == False )
    node.addAttr( "noprefix", "val3")
    self.assert_( node.getAttributesLength() == 3 )
    self.assert_( node.isAttributesEmpty() == False )
    self.assert_( (  "noprefix" != node.getAttrName(2) ) == False )
    self.assert_( (  "val3"     != node.getAttrValue(2) ) == False )
    self.assert_( node.getAttrURI(2) == "" )
    self.assert_( node.getAttrPrefix(2) == "" )
    self.assert_( (      "val3"  != node.getAttrValue( "noprefix") ) == False )
    self.assert_( (  "val3"  != node.getAttrValue( "noprefix", "") ) == False )
    self.assert_( node.hasAttr( "noprefix"    ) == True )
    self.assert_( node.hasAttr( "noprefix", "") == True )
    node.addAttr(xt1, "mval1")
    node.addAttr( "name2", "mval2", "http://name2.org/", "p2")
    self.assert_( node.getAttributesLength() == 3 )
    self.assert_( node.isAttributesEmpty() == False )
    self.assert_( (  "name1" != node.getAttrName(0) ) == False )
    self.assert_( (  "mval1" != node.getAttrValue(0) ) == False )
    self.assert_( (  "http://name1.org/" != node.getAttrURI(0) ) == False )
    self.assert_( (  "p1"    != node.getAttrPrefix(0) ) == False )
    self.assert_( (  "name2"    != node.getAttrName(1) ) == False )
    self.assert_( (  "mval2"    != node.getAttrValue(1) ) == False )
    self.assert_( (  "http://name2.org/" != node.getAttrURI(1) ) == False )
    self.assert_( (  "p2"       != node.getAttrPrefix(1) ) == False )
    self.assert_( node.hasAttr(xt1) == True )
    self.assert_( node.hasAttr( "name1", "http://name1.org/") == True )
    node.addAttr( "noprefix", "mval3")
    self.assert_( node.getAttributesLength() == 3 )
    self.assert_( node.isAttributesEmpty() == False )
    self.assert_( (  "noprefix" != node.getAttrName(2) ) == False )
    self.assert_( (  "mval3"    != node.getAttrValue(2) ) == False )
    self.assert_( node.getAttrURI(2) == "" )
    self.assert_( node.getAttrPrefix(2) == "" )
    self.assert_( node.hasAttr( "noprefix") == True )
    self.assert_( node.hasAttr( "noprefix", "") == True )
    node.addAttr(xt1a, "val1a")
    node.addAttr(xt2a, "val2a")
    self.assert_( node.getAttributesLength() == 5 )
    self.assert_( (  "name1" != node.getAttrName(3) ) == False )
    self.assert_( (  "val1a" != node.getAttrValue(3) ) == False )
    self.assert_( (  "http://name1a.org/" != node.getAttrURI(3) ) == False )
    self.assert_( (  "p1a" != node.getAttrPrefix(3) ) == False )
    self.assert_( (  "name2" != node.getAttrName(4) ) == False )
    self.assert_( (  "val2a" != node.getAttrValue(4) ) == False )
    self.assert_( (  "http://name2a.org/" != node.getAttrURI(4) ) == False )
    self.assert_( (  "p2a" != node.getAttrPrefix(4) ) == False )
    self.assert_( (  "val1a"  != node.getAttrValue( "name1", "http://name1a.org/") ) == False )
    self.assert_( (  "val2a"  != node.getAttrValue( "name2", "http://name2a.org/") ) == False )
    self.assert_( (  "val1a"  != node.getAttrValue(xt1a) ) == False )
    self.assert_( (  "val2a"  != node.getAttrValue(xt2a) ) == False )
    node.removeAttr(xt1a)
    node.removeAttr(xt2a)
    self.assert_( node.getAttributesLength() == 3 )
    node.removeAttr( "name1", "http://name1.org/")
    self.assert_( node.getAttributesLength() == 2 )
    self.assert_( node.isAttributesEmpty() == False )
    self.assert_( (  "name2" != node.getAttrName(0) ) == False )
    self.assert_( (  "mval2" != node.getAttrValue(0) ) == False )
    self.assert_( (  "http://name2.org/" != node.getAttrURI(0) ) == False )
    self.assert_( (  "p2" != node.getAttrPrefix(0) ) == False )
    self.assert_( (  "noprefix" != node.getAttrName(1) ) == False )
    self.assert_( (  "mval3" != node.getAttrValue(1) ) == False )
    self.assert_( node.getAttrURI(1) == "" )
    self.assert_( node.getAttrPrefix(1) == "" )
    self.assert_( node.hasAttr( "name1", "http://name1.org/") == False )
    node.removeAttr(xt2)
    self.assert_( node.getAttributesLength() == 1 )
    self.assert_( node.isAttributesEmpty() == False )
    self.assert_( (  "noprefix" != node.getAttrName(0) ) == False )
    self.assert_( (  "mval3" != node.getAttrValue(0) ) == False )
    self.assert_( node.getAttrURI(0) == "" )
    self.assert_( node.getAttrPrefix(0) == "" )
    self.assert_( node.hasAttr(xt2) == False )
    self.assert_( node.hasAttr( "name2", "http://name2.org/") == False )
    node.removeAttr( "noprefix")
    self.assert_( node.getAttributesLength() == 0 )
    self.assert_( node.isAttributesEmpty() == True )
    self.assert_( node.hasAttr( "noprefix"    ) == False )
    self.assert_( node.hasAttr( "noprefix", "") == False )
    node = None
    xt1 = None
    xt2 = None
    xt3 = None
    xt1a = None
    xt2a = None
    triple = None
    attr = None
    pass  

  def test_XMLNode_attribute_set_clear(self):
    triple = libsbml.XMLTriple("test","","")
    attr = libsbml.XMLAttributes()
    node = libsbml.XMLNode(triple,attr)
    nattr = libsbml.XMLAttributes()
    xt1 = libsbml.XMLTriple("name1", "http://name1.org/", "p1")
    xt2 = libsbml.XMLTriple("name2", "http://name2.org/", "p2")
    xt3 = libsbml.XMLTriple("name3", "http://name3.org/", "p3")
    xt4 = libsbml.XMLTriple("name4", "http://name4.org/", "p4")
    xt5 = libsbml.XMLTriple("name5", "http://name5.org/", "p5")
    nattr.add(xt1, "val1")
    nattr.add(xt2, "val2")
    nattr.add(xt3, "val3")
    nattr.add(xt4, "val4")
    nattr.add(xt5, "val5")
    node.setAttributes(nattr)
    self.assert_( node.getAttributesLength() == 5 )
    self.assert_( node.isAttributesEmpty() == False )
    self.assert_( (  "name1" != node.getAttrName(0) ) == False )
    self.assert_( (  "val1"  != node.getAttrValue(0) ) == False )
    self.assert_( (  "http://name1.org/" != node.getAttrURI(0) ) == False )
    self.assert_( (  "p1"    != node.getAttrPrefix(0) ) == False )
    self.assert_( (  "name2" != node.getAttrName(1) ) == False )
    self.assert_( (  "val2"  != node.getAttrValue(1) ) == False )
    self.assert_( (  "http://name2.org/" != node.getAttrURI(1) ) == False )
    self.assert_( (  "p2"    != node.getAttrPrefix(1) ) == False )
    self.assert_( (  "name3" != node.getAttrName(2) ) == False )
    self.assert_( (  "val3"  != node.getAttrValue(2) ) == False )
    self.assert_( (  "http://name3.org/" != node.getAttrURI(2) ) == False )
    self.assert_( (  "p3"    != node.getAttrPrefix(2) ) == False )
    self.assert_( (  "name4" != node.getAttrName(3) ) == False )
    self.assert_( (  "val4"  != node.getAttrValue(3) ) == False )
    self.assert_( (  "http://name4.org/" != node.getAttrURI(3) ) == False )
    self.assert_( (  "p4"    != node.getAttrPrefix(3) ) == False )
    self.assert_( (  "name5" != node.getAttrName(4) ) == False )
    self.assert_( (  "val5"  != node.getAttrValue(4) ) == False )
    self.assert_( (  "http://name5.org/" != node.getAttrURI(4) ) == False )
    self.assert_( (  "p5"    != node.getAttrPrefix(4) ) == False )
    ntriple = libsbml.XMLTriple("test2","http://test2.org/","p2")
    node.setTriple(ntriple)
    self.assert_( (    "test2" != node.getName() ) == False )
    self.assert_( (     "http://test2.org/" != node.getURI() ) == False )
    self.assert_( (  "p2" != node.getPrefix() ) == False )
    node.clearAttributes()
    self.assert_( node.getAttributesLength() == 0 )
    self.assert_( node.isAttributesEmpty() != False )
    triple = None
    ntriple = None
    node = None
    attr = None
    nattr = None
    xt1 = None
    xt2 = None
    xt3 = None
    xt4 = None
    xt5 = None
    pass  

  def test_XMLNode_convert(self):
    xmlstr = wrapString("<annotation>\n" + "  <test xmlns=\"http://test.org/\" id=\"test\">test</test>\n" + "</annotation>")
    node = libsbml.XMLNode.convertStringToXMLNode(xmlstr,None)
    child = node.getChild(0)
    gchild = child.getChild(0)
    attr = child.getAttributes()
    ns = child.getNamespaces()
    self.assert_( (  "annotation" != node.getName() ) == False )
    self.assert_( ( "test"  != child.getName() ) == False )
    self.assert_( ( "test"  != gchild.getCharacters() ) == False )
    self.assert_( (  "id"    != attr.getName(0) ) == False )
    self.assert_( (  "test"  != attr.getValue(0) ) == False )
    self.assert_( (  "http://test.org/"  != ns.getURI(0) ) == False )
    self.assert_( ns.getPrefix(0) == "" )
    toxmlstring = node.toXMLString()
    self.assert_( ( xmlstr != toxmlstring ) == False )
    node = None
    pass  

  def test_XMLNode_create(self):
    node = libsbml.XMLNode()
    self.assert_( node != None )
    self.assert_( node.getNumChildren() == 0 )
    node = None
    node = libsbml.XMLNode()
    self.assert_( node != None )
    node2 = libsbml.XMLNode()
    self.assert_( node2 != None )
    node.addChild(node2)
    self.assert_( node.getNumChildren() == 1 )
    node3 = libsbml.XMLNode()
    self.assert_( node3 != None )
    node.addChild(node3)
    self.assert_( node.getNumChildren() == 2 )
    node = None
    node2 = None
    node3 = None
    pass  

  def test_XMLNode_createElement(self):
    name =  "test";
    uri =  "http://test.org/";
    prefix =  "p";
    text =  "text node";
    triple = libsbml.XMLTriple(name,uri,prefix)
    ns = libsbml.XMLNamespaces()
    attr = libsbml.XMLAttributes()
    ns.add(uri,prefix)
    attr.add("id", "value",uri,prefix)
    snode = libsbml.XMLNode(triple,attr,ns)
    self.assert_( snode != None )
    self.assert_( snode.getNumChildren() == 0 )
    self.assert_( ( name != snode.getName() ) == False )
    self.assert_( ( prefix != snode.getPrefix() ) == False )
    self.assert_( ( uri != snode.getURI() ) == False )
    self.assert_( snode.isElement() == True )
    self.assert_( snode.isStart() == True )
    self.assert_( snode.isEnd() == False )
    self.assert_( snode.isText() == False )
    snode.setEnd()
    self.assert_( snode.isEnd() == True )
    snode.unsetEnd()
    self.assert_( snode.isEnd() == False )
    cattr = snode.getAttributes()
    self.assert_( cattr != None )
    self.assert_( (  "id"    != cattr.getName(0) ) == False )
    self.assert_( (  "value" != cattr.getValue(0) ) == False )
    self.assert_( ( prefix != cattr.getPrefix(0) ) == False )
    self.assert_( ( uri != cattr.getURI(0) ) == False )
    triple = None
    attr = None
    ns = None
    snode = None
    attr = libsbml.XMLAttributes()
    attr.add("id", "value")
    triple = libsbml.XMLTriple(name, "", "")
    snode = libsbml.XMLNode(triple,attr)
    self.assert_( snode != None )
    self.assert_( snode.getNumChildren() == 0 )
    self.assert_( (  "test" != snode.getName() ) == False )
    self.assert_( snode.getPrefix() == "" )
    self.assert_( snode.getURI() == "" )
    self.assert_( snode.isElement() == True )
    self.assert_( snode.isStart() == True )
    self.assert_( snode.isEnd() == False )
    self.assert_( snode.isText() == False )
    cattr = snode.getAttributes()
    self.assert_( cattr != None )
    self.assert_( (  "id"    != cattr.getName(0) ) == False )
    self.assert_( (  "value" != cattr.getValue(0) ) == False )
    self.assert_( cattr.getPrefix(0) == "" )
    self.assert_( cattr.getURI(0) == "" )
    enode = libsbml.XMLNode(triple)
    self.assert_( enode != None )
    self.assert_( enode.getNumChildren() == 0 )
    self.assert_( (  "test" != enode.getName() ) == False )
    self.assert_( enode.getPrefix() == "" )
    self.assert_( enode.getURI() == "" )
    self.assert_( enode.isElement() == True )
    self.assert_( enode.isStart() == False )
    self.assert_( enode.isEnd() == True )
    self.assert_( enode.isText() == False )
    tnode = libsbml.XMLNode(text)
    self.assert_( tnode != None )
    self.assert_( ( text != tnode.getCharacters() ) == False )
    self.assert_( tnode.getNumChildren() == 0 )
    self.assert_( tnode.getName() == "" )
    self.assert_( tnode.getPrefix() == "" )
    self.assert_( tnode.getURI() == "" )
    self.assert_( tnode.isElement() == False )
    self.assert_( tnode.isStart() == False )
    self.assert_( tnode.isEnd() == False )
    self.assert_( tnode.isText() == True )
    triple = None
    attr = None
    snode = None
    enode = None
    tnode = None
    pass  

  def test_XMLNode_createFromToken(self):
    triple = libsbml.XMLTriple("attr", "uri", "prefix")
    token = libsbml.XMLToken(triple)
    node = libsbml.XMLNode(token)
    self.assert_( node != None )
    self.assert_( node.getNumChildren() == 0 )
    self.assert_( (  "attr" != node.getName() ) == False )
    self.assert_( (  "prefix" != node.getPrefix() ) == False )
    self.assert_( (  "uri" != node.getURI() ) == False )
    self.assert_( node.getChild(1) != None )
    token = None
    triple = None
    node = None
    pass  

  def test_XMLNode_getters(self):
    ns = libsbml.XMLNamespaces()
    ns.add( "http://test1.org/", "test1")
    token = libsbml.XMLToken("This is a test")
    node = libsbml.XMLNode(token)
    self.assert_( node != None )
    self.assert_( node.getNumChildren() == 0 )
    self.assert_( (  "This is a test" != node.getCharacters() ) == False )
    self.assert_( node.getChild(1) != None )
    attr = libsbml.XMLAttributes()
    self.assert_( attr != None )
    attr.add( "attr2", "value")
    triple = libsbml.XMLTriple("attr", "uri", "prefix")
    token = libsbml.XMLToken(triple,attr)
    self.assert_( token != None )
    node = libsbml.XMLNode(token)
    self.assert_( (  "attr" != node.getName() ) == False )
    self.assert_( (  "uri" != node.getURI() ) == False )
    self.assert_( (  "prefix" != node.getPrefix() ) == False )
    returnattr = node.getAttributes()
    self.assert_( (  "attr2" != returnattr.getName(0) ) == False )
    self.assert_( (  "value" != returnattr.getValue(0) ) == False )
    token = libsbml.XMLToken(triple,attr,ns)
    node = libsbml.XMLNode(token)
    returnNS = node.getNamespaces()
    self.assert_( returnNS.getLength() == 1 )
    self.assert_( returnNS.isEmpty() == False )
    triple = None
    token = None
    node = None
    pass  

  def test_XMLNode_insert(self):
    attr = libsbml.XMLAttributes()
    trp_p = libsbml.XMLTriple("parent","","")
    trp_c1 = libsbml.XMLTriple("child1","","")
    trp_c2 = libsbml.XMLTriple("child2","","")
    trp_c3 = libsbml.XMLTriple("child3","","")
    trp_c4 = libsbml.XMLTriple("child4","","")
    trp_c5 = libsbml.XMLTriple("child5","","")
    p = libsbml.XMLNode(trp_p,attr)
    c1 = libsbml.XMLNode(trp_c1,attr)
    c2 = libsbml.XMLNode(trp_c2,attr)
    c3 = libsbml.XMLNode(trp_c3,attr)
    c4 = libsbml.XMLNode(trp_c4,attr)
    c5 = libsbml.XMLNode(trp_c5,attr)
    p.addChild(c2)
    p.addChild(c4)
    p.insertChild(0,c1)
    p.insertChild(2,c3)
    p.insertChild(4,c5)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( (  "child1" != p.getChild(0).getName() ) == False )
    self.assert_( (  "child2" != p.getChild(1).getName() ) == False )
    self.assert_( (  "child3" != p.getChild(2).getName() ) == False )
    self.assert_( (  "child4" != p.getChild(3).getName() ) == False )
    self.assert_( (  "child5" != p.getChild(4).getName() ) == False )
    p.removeChildren()
    p.insertChild(0,c1)
    p.insertChild(0,c2)
    p.insertChild(0,c3)
    p.insertChild(0,c4)
    p.insertChild(0,c5)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( (  "child5" != p.getChild(0).getName() ) == False )
    self.assert_( (  "child4" != p.getChild(1).getName() ) == False )
    self.assert_( (  "child3" != p.getChild(2).getName() ) == False )
    self.assert_( (  "child2" != p.getChild(3).getName() ) == False )
    self.assert_( (  "child1" != p.getChild(4).getName() ) == False )
    p.removeChildren()
    p.insertChild(1,c1)
    p.insertChild(2,c2)
    p.insertChild(3,c3)
    p.insertChild(4,c4)
    p.insertChild(5,c5)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( (  "child1" != p.getChild(0).getName() ) == False )
    self.assert_( (  "child2" != p.getChild(1).getName() ) == False )
    self.assert_( (  "child3" != p.getChild(2).getName() ) == False )
    self.assert_( (  "child4" != p.getChild(3).getName() ) == False )
    self.assert_( (  "child5" != p.getChild(4).getName() ) == False )
    p.removeChildren()
    tmp = p.insertChild(0,c1)
    self.assert_( ( "child1" != tmp.getName() ) == False )
    tmp = p.insertChild(0,c2)
    self.assert_( ( "child2" != tmp.getName() ) == False )
    tmp = p.insertChild(0,c3)
    self.assert_( ( "child3" != tmp.getName() ) == False )
    tmp = p.insertChild(0,c4)
    self.assert_( ( "child4" != tmp.getName() ) == False )
    tmp = p.insertChild(0,c5)
    self.assert_( ( "child5" != tmp.getName() ) == False )
    p.removeChildren()
    tmp = p.insertChild(1,c1)
    self.assert_( ( "child1" != tmp.getName() ) == False )
    tmp = p.insertChild(2,c2)
    self.assert_( ( "child2" != tmp.getName() ) == False )
    tmp = p.insertChild(3,c3)
    self.assert_( ( "child3" != tmp.getName() ) == False )
    tmp = p.insertChild(4,c4)
    self.assert_( ( "child4" != tmp.getName() ) == False )
    tmp = p.insertChild(5,c5)
    self.assert_( ( "child5" != tmp.getName() ) == False )
    p = None
    c1 = None
    c2 = None
    c3 = None
    c4 = None
    c5 = None
    attr = None
    trp_p = None
    trp_c1 = None
    trp_c2 = None
    trp_c3 = None
    trp_c4 = None
    trp_c5 = None
    pass  

  def test_XMLNode_namespace_add(self):
    triple = libsbml.XMLTriple("test","","")
    attr = libsbml.XMLAttributes()
    node = libsbml.XMLNode(triple,attr)
    self.assert_( node.getNamespacesLength() == 0 )
    self.assert_( node.isNamespacesEmpty() == True )
    node.addNamespace( "http://test1.org/", "test1")
    self.assert_( node.getNamespacesLength() == 1 )
    self.assert_( node.isNamespacesEmpty() == False )
    node.addNamespace( "http://test2.org/", "test2")
    self.assert_( node.getNamespacesLength() == 2 )
    self.assert_( node.isNamespacesEmpty() == False )
    node.addNamespace( "http://test1.org/", "test1a")
    self.assert_( node.getNamespacesLength() == 3 )
    self.assert_( node.isNamespacesEmpty() == False )
    node.addNamespace( "http://test1.org/", "test1a")
    self.assert_( node.getNamespacesLength() == 3 )
    self.assert_( node.isNamespacesEmpty() == False )
    self.assert_( (node.getNamespaceIndex( "http://test1.org/") == -1) == False)
    node = None
    triple = None
    attr = None
    pass  

  def test_XMLNode_namespace_get(self):
    triple = libsbml.XMLTriple("test","","")
    attr = libsbml.XMLAttributes()
    node = libsbml.XMLNode(triple,attr)
    node.addNamespace( "http://test1.org/", "test1")
    node.addNamespace( "http://test2.org/", "test2")
    node.addNamespace( "http://test3.org/", "test3")
    node.addNamespace( "http://test4.org/", "test4")
    node.addNamespace( "http://test5.org/", "test5")
    node.addNamespace( "http://test6.org/", "test6")
    node.addNamespace( "http://test7.org/", "test7")
    node.addNamespace( "http://test8.org/", "test8")
    node.addNamespace( "http://test9.org/", "test9")
    self.assert_( node.getNamespacesLength() == 9 )
    self.assert_( node.getNamespaceIndex( "http://test1.org/") == 0 )
    self.assert_( (  "test2" != node.getNamespacePrefix(1) ) == False )
    self.assert_( ( 		      "test1" != node.getNamespacePrefix( "http://test1.org/") ) == False )
    self.assert_( (  "http://test2.org/" != node.getNamespaceURI(1) ) == False )
    self.assert_( ( 		      "http://test2.org/" != node.getNamespaceURI( "test2") ) == False )
    self.assert_( node.getNamespaceIndex( "http://test1.org/") == 0 )
    self.assert_( node.getNamespaceIndex( "http://test2.org/") == 1 )
    self.assert_( node.getNamespaceIndex( "http://test5.org/") == 4 )
    self.assert_( node.getNamespaceIndex( "http://test9.org/") == 8 )
    self.assert_( node.getNamespaceIndex( "http://testX.org/") == -1 )
    self.assert_( node.hasNamespaceURI( "http://test1.org/") != False )
    self.assert_( node.hasNamespaceURI( "http://test2.org/") != False )
    self.assert_( node.hasNamespaceURI( "http://test5.org/") != False )
    self.assert_( node.hasNamespaceURI( "http://test9.org/") != False )
    self.assert_( node.hasNamespaceURI( "http://testX.org/") == False )
    self.assert_( node.getNamespaceIndexByPrefix( "test1") == 0 )
    self.assert_( node.getNamespaceIndexByPrefix( "test5") == 4 )
    self.assert_( node.getNamespaceIndexByPrefix( "test9") == 8 )
    self.assert_( node.getNamespaceIndexByPrefix( "testX") == -1 )
    self.assert_( node.hasNamespacePrefix( "test1") != False )
    self.assert_( node.hasNamespacePrefix( "test5") != False )
    self.assert_( node.hasNamespacePrefix( "test9") != False )
    self.assert_( node.hasNamespacePrefix( "testX") == False )
    self.assert_( node.hasNamespaceNS( "http://test1.org/", "test1") != False )
    self.assert_( node.hasNamespaceNS( "http://test5.org/", "test5") != False )
    self.assert_( node.hasNamespaceNS( "http://test9.org/", "test9") != False )
    self.assert_( node.hasNamespaceNS( "http://testX.org/", "testX") == False )
    node = None
    triple = None
    attr = None
    pass  

  def test_XMLNode_namespace_remove(self):
    triple = libsbml.XMLTriple("test","","")
    attr = libsbml.XMLAttributes()
    node = libsbml.XMLNode(triple,attr)
    node.addNamespace( "http://test1.org/", "test1")
    node.addNamespace( "http://test2.org/", "test2")
    node.addNamespace( "http://test3.org/", "test3")
    node.addNamespace( "http://test4.org/", "test4")
    node.addNamespace( "http://test5.org/", "test5")
    self.assert_( node.getNamespacesLength() == 5 )
    node.removeNamespace(4)
    self.assert_( node.getNamespacesLength() == 4 )
    node.removeNamespace(3)
    self.assert_( node.getNamespacesLength() == 3 )
    node.removeNamespace(2)
    self.assert_( node.getNamespacesLength() == 2 )
    node.removeNamespace(1)
    self.assert_( node.getNamespacesLength() == 1 )
    node.removeNamespace(0)
    self.assert_( node.getNamespacesLength() == 0 )
    node.addNamespace( "http://test1.org/", "test1")
    node.addNamespace( "http://test2.org/", "test2")
    node.addNamespace( "http://test3.org/", "test3")
    node.addNamespace( "http://test4.org/", "test4")
    node.addNamespace( "http://test5.org/", "test5")
    self.assert_( node.getNamespacesLength() == 5 )
    node.removeNamespace(0)
    self.assert_( node.getNamespacesLength() == 4 )
    node.removeNamespace(0)
    self.assert_( node.getNamespacesLength() == 3 )
    node.removeNamespace(0)
    self.assert_( node.getNamespacesLength() == 2 )
    node.removeNamespace(0)
    self.assert_( node.getNamespacesLength() == 1 )
    node.removeNamespace(0)
    self.assert_( node.getNamespacesLength() == 0 )
    node = None
    triple = None
    attr = None
    pass  

  def test_XMLNode_namespace_remove_by_prefix(self):
    triple = libsbml.XMLTriple("test","","")
    attr = libsbml.XMLAttributes()
    node = libsbml.XMLNode(triple,attr)
    node.addNamespace( "http://test1.org/", "test1")
    node.addNamespace( "http://test2.org/", "test2")
    node.addNamespace( "http://test3.org/", "test3")
    node.addNamespace( "http://test4.org/", "test4")
    node.addNamespace( "http://test5.org/", "test5")
    self.assert_( node.getNamespacesLength() == 5 )
    node.removeNamespace( "test1")
    self.assert_( node.getNamespacesLength() == 4 )
    node.removeNamespace( "test2")
    self.assert_( node.getNamespacesLength() == 3 )
    node.removeNamespace( "test3")
    self.assert_( node.getNamespacesLength() == 2 )
    node.removeNamespace( "test4")
    self.assert_( node.getNamespacesLength() == 1 )
    node.removeNamespace( "test5")
    self.assert_( node.getNamespacesLength() == 0 )
    node.addNamespace( "http://test1.org/", "test1")
    node.addNamespace( "http://test2.org/", "test2")
    node.addNamespace( "http://test3.org/", "test3")
    node.addNamespace( "http://test4.org/", "test4")
    node.addNamespace( "http://test5.org/", "test5")
    self.assert_( node.getNamespacesLength() == 5 )
    node.removeNamespace( "test5")
    self.assert_( node.getNamespacesLength() == 4 )
    node.removeNamespace( "test4")
    self.assert_( node.getNamespacesLength() == 3 )
    node.removeNamespace( "test3")
    self.assert_( node.getNamespacesLength() == 2 )
    node.removeNamespace( "test2")
    self.assert_( node.getNamespacesLength() == 1 )
    node.removeNamespace( "test1")
    self.assert_( node.getNamespacesLength() == 0 )
    node.addNamespace( "http://test1.org/", "test1")
    node.addNamespace( "http://test2.org/", "test2")
    node.addNamespace( "http://test3.org/", "test3")
    node.addNamespace( "http://test4.org/", "test4")
    node.addNamespace( "http://test5.org/", "test5")
    self.assert_( node.getNamespacesLength() == 5 )
    node.removeNamespace( "test3")
    self.assert_( node.getNamespacesLength() == 4 )
    node.removeNamespace( "test1")
    self.assert_( node.getNamespacesLength() == 3 )
    node.removeNamespace( "test4")
    self.assert_( node.getNamespacesLength() == 2 )
    node.removeNamespace( "test5")
    self.assert_( node.getNamespacesLength() == 1 )
    node.removeNamespace( "test2")
    self.assert_( node.getNamespacesLength() == 0 )
    node = None
    triple = None
    attr = None
    pass  

  def test_XMLNode_namespace_set_clear(self):
    triple = libsbml.XMLTriple("test","","")
    attr = libsbml.XMLAttributes()
    node = libsbml.XMLNode(triple,attr)
    ns = libsbml.XMLNamespaces()
    self.assert_( node.getNamespacesLength() == 0 )
    self.assert_( node.isNamespacesEmpty() == True )
    ns.add( "http://test1.org/", "test1")
    ns.add( "http://test2.org/", "test2")
    ns.add( "http://test3.org/", "test3")
    ns.add( "http://test4.org/", "test4")
    ns.add( "http://test5.org/", "test5")
    node.setNamespaces(ns)
    self.assert_( node.getNamespacesLength() == 5 )
    self.assert_( node.isNamespacesEmpty() == False )
    self.assert_( (  "test1" != node.getNamespacePrefix(0) ) == False )
    self.assert_( (  "test2" != node.getNamespacePrefix(1) ) == False )
    self.assert_( (  "test3" != node.getNamespacePrefix(2) ) == False )
    self.assert_( (  "test4" != node.getNamespacePrefix(3) ) == False )
    self.assert_( (  "test5" != node.getNamespacePrefix(4) ) == False )
    self.assert_( (  "http://test1.org/" != node.getNamespaceURI(0) ) == False )
    self.assert_( (  "http://test2.org/" != node.getNamespaceURI(1) ) == False )
    self.assert_( (  "http://test3.org/" != node.getNamespaceURI(2) ) == False )
    self.assert_( (  "http://test4.org/" != node.getNamespaceURI(3) ) == False )
    self.assert_( (  "http://test5.org/" != node.getNamespaceURI(4) ) == False )
    node.clearNamespaces()
    self.assert_( node.getNamespacesLength() == 0 )
    self.assert_( node.isAttributesEmpty() != False )
    ns = None
    node = None
    triple = None
    attr = None
    pass  

  def test_XMLNode_remove(self):
    attr = libsbml.XMLAttributes()
    trp_p = libsbml.XMLTriple("parent","","")
    trp_c1 = libsbml.XMLTriple("child1","","")
    trp_c2 = libsbml.XMLTriple("child2","","")
    trp_c3 = libsbml.XMLTriple("child3","","")
    trp_c4 = libsbml.XMLTriple("child4","","")
    trp_c5 = libsbml.XMLTriple("child5","","")
    p = libsbml.XMLNode(trp_p,attr)
    c1 = libsbml.XMLNode(trp_c1,attr)
    c2 = libsbml.XMLNode(trp_c2,attr)
    c3 = libsbml.XMLNode(trp_c3,attr)
    c4 = libsbml.XMLNode(trp_c4,attr)
    c5 = libsbml.XMLNode(trp_c5,attr)
    p.addChild(c1)
    p.addChild(c2)
    p.addChild(c3)
    p.addChild(c4)
    p.addChild(c5)
    r = p.removeChild(5)
    self.assert_( r == None )
    r = p.removeChild(1)
    self.assert_( p.getNumChildren() == 4 )
    self.assert_( ( "child2" != r.getName() ) == False )
    r = None
    r = p.removeChild(3)
    self.assert_( p.getNumChildren() == 3 )
    self.assert_( ( "child5" != r.getName() ) == False )
    r = None
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 2 )
    self.assert_( ( "child1" != r.getName() ) == False )
    r = None
    r = p.removeChild(1)
    self.assert_( p.getNumChildren() == 1 )
    self.assert_( ( "child4" != r.getName() ) == False )
    r = None
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 0 )
    self.assert_( ( "child3" != r.getName() ) == False )
    r = None
    p.addChild(c1)
    p.addChild(c2)
    p.addChild(c3)
    p.addChild(c4)
    p.addChild(c5)
    r = p.removeChild(4)
    self.assert_( p.getNumChildren() == 4 )
    self.assert_( ( "child5" != r.getName() ) == False )
    r = None
    r = p.removeChild(3)
    self.assert_( p.getNumChildren() == 3 )
    self.assert_( ( "child4" != r.getName() ) == False )
    r = None
    r = p.removeChild(2)
    self.assert_( p.getNumChildren() == 2 )
    self.assert_( ( "child3" != r.getName() ) == False )
    r = None
    r = p.removeChild(1)
    self.assert_( p.getNumChildren() == 1 )
    self.assert_( ( "child2" != r.getName() ) == False )
    r = None
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 0 )
    self.assert_( ( "child1" != r.getName() ) == False )
    r = None
    p.addChild(c1)
    p.addChild(c2)
    p.addChild(c3)
    p.addChild(c4)
    p.addChild(c5)
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 4 )
    self.assert_( ( "child1" != r.getName() ) == False )
    r = None
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 3 )
    self.assert_( ( "child2" != r.getName() ) == False )
    r = None
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 2 )
    self.assert_( ( "child3" != r.getName() ) == False )
    r = None
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 1 )
    self.assert_( ( "child4" != r.getName() ) == False )
    r = None
    r = p.removeChild(0)
    self.assert_( p.getNumChildren() == 0 )
    self.assert_( ( "child5" != r.getName() ) == False )
    r = None
    p.addChild(c1)
    p.addChild(c2)
    p.addChild(c3)
    p.addChild(c4)
    p.addChild(c5)
    r = p.removeChild(0)
    self.assert_( ( "child1" != r.getName() ) == False )
    p.insertChild(0,r)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( ( "child1" != p.getChild(0).getName() ) == False )
    r = None
    r = p.removeChild(1)
    self.assert_( ( "child2" != r.getName() ) == False )
    p.insertChild(1,r)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( ( "child2" != p.getChild(1).getName() ) == False )
    r = None
    r = p.removeChild(2)
    self.assert_( ( "child3" != r.getName() ) == False )
    p.insertChild(2,r)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( ( "child3" != p.getChild(2).getName() ) == False )
    r = None
    r = p.removeChild(3)
    self.assert_( ( "child4" != r.getName() ) == False )
    p.insertChild(3,r)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( ( "child4" != p.getChild(3).getName() ) == False )
    r = None
    r = p.removeChild(4)
    self.assert_( ( "child5" != r.getName() ) == False )
    p.insertChild(4,r)
    self.assert_( p.getNumChildren() == 5 )
    self.assert_( ( "child5" != p.getChild(4).getName() ) == False )
    r = None
    p = None
    c1 = None
    c2 = None
    c3 = None
    c4 = None
    c5 = None
    attr = None
    trp_p = None
    trp_c1 = None
    trp_c2 = None
    trp_c3 = None
    trp_c4 = None
    trp_c5 = None
    pass  

def suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestXMLNode))

  return suite

if __name__ == "__main__":
  if unittest.TextTestRunner(verbosity=1).run(suite()).wasSuccessful() :
    sys.exit(0)
  else:
    sys.exit(1)
