END_TEST


START_TEST (test_Constraint_setMessage2)
{
  //const char * expected = (
  //  "<message>\n"
  //  "  <p xmlns=\"http://www.w3.org/1999/xhtml\"> Some text </p>\n"
  //  "</message>");

  XMLNode_t *text = XMLNode_convertStringToXMLNode(" Some text ", NULL);
  XMLTriple_t *triple = XMLTriple_createWith("p", "http://www.w3.org/1999/xhtml", "");
  XMLAttributes_t *att = XMLAttributes_create();
  XMLNamespaces_t *xmlns = XMLNamespaces_create();
  XMLNamespaces_add(xmlns, "http://www.w3.org/1999/xhtml", "");
  
  XMLNode_t *p = XMLNode_createStartElementNS(triple, att, xmlns);
  XMLNode_addChild(p, text);
  
  XMLTriple_t *triple1 = XMLTriple_createWith("message", "", "");
  XMLAttributes_t *att1 = XMLAttributes_create();
  XMLNode_t *node = XMLNode_createStartElement(triple1, att1);

  XMLNode_addChild(node, p);

  int i = Constraint_setMessage(C, node);

  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( Constraint_isSetMessage(C) == 1);
  /* FIX ME
  printf("Expected: %s\n", expected);
  printf("String  : %s\n", Constraint_getMessage(C));
  fail_unless( strcmp(Constraint_getMessageString(C),
    expected) == 0);
  */
  i = Constraint_unsetMessage(C);
  
  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( !Constraint_isSetMessage(C) );

  if (Constraint_getMessage(C) != NULL)
  {
    fail("Constraint_unsetMessage(C) did not clear XMLNode.");
  }

  XMLNode_free(text);
  XMLTriple_free(triple);
  XMLAttributes_free(att);
  XMLNamespaces_free(xmlns);
  XMLNode_free(p);
  XMLTriple_free(triple1);
  XMLAttributes_free(att1);
  XMLNode_free(node);
}
END_TEST


START_TEST (test_Constraint_setMessage)
{
  XMLNode_t *text = XMLNode_convertStringToXMLNode(" Some text ", NULL);
  XMLTriple_t *triple = XMLTriple_createWith("p", "http://www.w3.org/1999/xhtml", "");
  XMLAttributes_t *att = XMLAttributes_create();
  XMLNamespaces_t *xmlns = XMLNamespaces_create();
  XMLNamespaces_add(xmlns, "http://www.w3.org/1999/xhtml", "");
  
  XMLNode_t *p = XMLNode_createStartElementNS(triple, att, xmlns);
  XMLNode_addChild(p, text);
  
  XMLTriple_t *triple1 = XMLTriple_createWith("message", "", "");
  XMLAttributes_t *att1 = XMLAttributes_create();
  XMLNode_t *node = XMLNode_createStartElement(triple1, att1);

  XMLNode_addChild(node, p);

  Constraint_setMessage(C, node);

  fail_unless( Constraint_getMessage(C) != node );
  fail_unless( Constraint_isSetMessage(C) == 1);

  /* Reflexive case (pathological) */
  Constraint_setMessage(C, (XMLNode_t *) Constraint_getMessage(C));

  fail_unless( Constraint_getMessage(C) != node );

  char* str = Constraint_getMessageString(C) ;
  fail_unless( str != NULL );
  safe_free(str);

  Constraint_unsetMessage(C);
  fail_unless( !Constraint_isSetMessage(C) );

  if (Constraint_getMessage(C) != NULL)
  {
    fail("Constraint_unsetMessage(C) did not clear XMLNode.");
  }

  XMLNode_free(text);
  XMLTriple_free(triple);
  XMLAttributes_free(att);
  XMLNamespaces_free(xmlns);
  XMLNode_free(p);
  XMLTriple_free(triple1);
  XMLAttributes_free(att1);
  XMLNode_free(node);
}
END_TEST


START_TEST (test_XMLNode_removeChildren)
{
  XMLNode_t *node = XMLNode_create();
  XMLNode_t *node2 = XMLNode_create();
  XMLNode_t *node3 = XMLNode_create();

  XMLNode_addChild(node, node2);
  XMLNode_addChild(node, node3);

  fail_unless(XMLNode_getNumChildren(node) == 2);

  int i = XMLNode_removeChildren(node);
  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless(XMLNode_getNumChildren(node) == 0);

  XMLNode_free(node);
  XMLNode_free(node2);
  XMLNode_free(node3);
}
END_TEST


START_TEST (test_SyntaxChecker_validXHTML)
{
  SBMLNamespaces_t *NS24 = SBMLNamespaces_create(2,4);
  SBMLNamespaces_t *NS31 = SBMLNamespaces_create(3,1);

  XMLToken_t *toptoken;
  XMLNode_t *topnode;
  XMLTriple_t * toptriple = XMLTriple_createWith("notes", "", "");

  XMLToken_t *token;
  XMLNode_t *node;
  XMLTriple_t * triple = XMLTriple_createWith("p", "", "");
  XMLAttributes_t * att = XMLAttributes_create ();
  XMLNamespaces_t *ns = XMLNamespaces_create();
  XMLNamespaces_add(ns, "http://www.w3.org/1999/xhtml", "");
  XMLToken_t *tt = XMLToken_createWithText("This is my text");
  XMLNode_t *n1 = XMLNode_createFromToken(tt);


  toptoken = XMLToken_createWithTripleAttr(toptriple, att);
  topnode = XMLNode_createFromToken(toptoken);

  token = XMLToken_createWithTripleAttrNS(triple, att, ns);
  node = XMLNode_createFromToken(token);
  XMLNode_addChild(node, n1);
  XMLNode_addChild(topnode, node);

  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NULL) == 1 );
  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NS24) == 1 );
  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NS31) == 1 );

  triple = XMLTriple_createWith("html", "", "");
  token = XMLToken_createWithTripleAttrNS(triple, att, ns);
  node = XMLNode_createFromToken(token);
  XMLNode_addChild(node, n1);
  XMLNode_removeChild(topnode, 0);
  XMLNode_addChild(topnode, node);

  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NULL) == 1 );
  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NS24) == 0 );
  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NS31) == 1 );

  triple = XMLTriple_createWith("html", "", "");
  XMLNamespaces_clear(ns);

  token = XMLToken_createWithTripleAttrNS(triple, att, ns);
  node = XMLNode_createFromToken(token);
  XMLNode_addChild(node, n1);
  XMLNode_removeChild(topnode, 0);
  XMLNode_addChild(topnode, node);

  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NULL) == 0 );
  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NS24) == 0 );
  fail_unless( SyntaxChecker_hasExpectedXHTMLSyntax(topnode, NS31) == 0 );
}
CK_CPPSTART
#endif

START_TEST (test_XMLNode_addChild1)
{
  XMLNode_t *node = XMLNode_create();
  XMLNode_t *node2 = XMLNode_create();

  int i = XMLNode_addChild(node, node2);

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless(XMLNode_getNumChildren(node) == 1);

  XMLNode_free(node);
  XMLNode_free(node2);
}
END_TEST


START_TEST (test_XMLNode_addChild3)
{
  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLNode_t *node = XMLNode_createEndElement(triple);
  XMLNode_t *node2 = XMLNode_create();

  int i = XMLNode_addChild(node, node2);

  fail_unless( i == LIBSBML_INVALID_XML_OPERATION);
  fail_unless(XMLNode_getNumChildren(node) == 0);

  XMLTriple_free(triple);
  XMLNode_free(node);
  XMLNode_free(node2);
}
END_TEST


START_TEST (test_XMLNode_addChild2)
{
  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLNode_t *node = XMLNode_createStartElement(triple, attr);
  XMLNode_t *node2 = XMLNode_create();

  int i = XMLNode_addChild(node, node2);

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless(XMLNode_getNumChildren(node) == 1);

  XMLTriple_free(triple);
  XMLAttributes_free(attr);
  XMLNode_free(node);
  XMLNode_free(node2);
}
END_TEST

START_TEST(test_XMLNode_accessWithNULL)
{
  fail_unless( XMLNode_addAttr(NULL, NULL, NULL) == LIBSBML_INVALID_OBJECT );
  fail_unless( XMLNode_addAttrWithNS(NULL, NULL, NULL, NULL, NULL) 
    == LIBSBML_INVALID_OBJECT );
  fail_unless( XMLNode_addAttrWithTriple(NULL, NULL, NULL) == LIBSBML_INVALID_OBJECT );
  fail_unless( XMLNode_addChild(NULL, NULL) == LIBSBML_INVALID_OBJECT );
  fail_unless( XMLNode_addNamespace(NULL, NULL, NULL) == LIBSBML_INVALID_OBJECT );
  fail_unless( XMLNode_clearAttributes(NULL) == LIBSBML_INVALID_OBJECT );
  fail_unless( XMLNode_clearNamespaces(NULL) == LIBSBML_INVALID_OBJECT );
  fail_unless( XMLNode_clone(NULL) == NULL);
  fail_unless( XMLNode_convertStringToXMLNode(NULL, NULL) == NULL);
  fail_unless( XMLNode_convertXMLNodeToString(NULL) == NULL);
  fail_unless( XMLNode_createEndElement(NULL) == NULL);
  fail_unless( XMLNode_createFromToken(NULL) == NULL);
  fail_unless( XMLNode_createStartElement(NULL, NULL) == NULL);
  fail_unless( XMLNode_createStartElementNS(NULL, NULL, NULL) == NULL);
  fail_unless( XMLNode_equals(NULL, NULL) == 1);
  fail_unless( XMLNode_equals(NULL, XMLNode_createTextNode(NULL)) == 0);

  XMLNode_free(NULL);

  fail_unless( XMLNode_getAttributes(NULL) == NULL);
  fail_unless( XMLNode_getAttributesLength(NULL) == 0);
  fail_unless( XMLNode_getAttrIndex(NULL, NULL, NULL) == -1);
  fail_unless( XMLNode_getAttrIndexByTriple(NULL, NULL) == -1);
  fail_unless( XMLNode_getAttrName(NULL, 0) == NULL);
  fail_unless( XMLNode_getAttrPrefix(NULL, 0) == NULL);
  fail_unless( XMLNode_getAttrPrefixedName(NULL, 0) == NULL);
  fail_unless( XMLNode_getAttrURI(NULL, 0) == NULL);
  fail_unless( XMLNode_getAttrValue(NULL, 0) == NULL);
  fail_unless( XMLNode_getAttrValueByName(NULL, NULL) == NULL);
  fail_unless( XMLNode_getAttrValueByNS(NULL, NULL, NULL) == NULL);
  fail_unless( XMLNode_getAttrValueByTriple(NULL, NULL) == NULL);
  
  fail_unless( XMLNode_getCharacters(NULL) == NULL);
  
  fail_unless( XMLNode_getChild(NULL, 0) == NULL);
  fail_unless( XMLNode_getChildForName(NULL, NULL) == NULL);
  fail_unless( XMLNode_getChildForNameNC(NULL, NULL) == NULL);
  fail_unless( XMLNode_getChildNC(NULL, 0) == NULL);
  
  fail_unless( XMLNode_getIndex(NULL, NULL) == -1);
  fail_unless( XMLNode_getName(NULL) == NULL);
  
  fail_unless( XMLNode_getNamespaceIndex(NULL, NULL) == -1);
  fail_unless( XMLNode_getNamespaceIndexByPrefix(NULL, NULL) == -1);
  fail_unless( XMLNode_getNamespacePrefix(NULL, 0) == NULL);
  fail_unless( XMLNode_getNamespacePrefixByURI(NULL, NULL) == NULL);
  fail_unless( XMLNode_getNamespaces(NULL) == NULL);
  fail_unless( XMLNode_getNamespacesLength(NULL) == 0);
  fail_unless( XMLNode_getNamespaceURI(NULL, 0) == NULL);
  fail_unless( XMLNode_getNamespaceURIByPrefix(NULL, NULL) == NULL);
  
  fail_unless( XMLNode_getNumChildren(NULL) == 0);
  fail_unless( XMLNode_getPrefix(NULL) == NULL);  
  fail_unless( XMLNode_getURI(NULL) == NULL);
  
  fail_unless( XMLNode_hasAttr(NULL, 0) == 0);
  fail_unless( XMLNode_hasAttrWithName(NULL, NULL) == 0);
  fail_unless( XMLNode_hasAttrWithNS(NULL, NULL, NULL) == 0);
  fail_unless( XMLNode_hasAttrWithTriple(NULL, NULL) == 0);
  
  fail_unless( XMLNode_hasChild(NULL, NULL) == 0);
  fail_unless( XMLNode_hasNamespaceNS(NULL, NULL, NULL) == 0);
  fail_unless( XMLNode_hasNamespacePrefix(NULL, NULL) == 0);
  fail_unless( XMLNode_hasNamespaceURI(NULL, NULL) == 0);
  
  fail_unless( XMLNode_insertChild(NULL, 0, NULL) == NULL);
  
  fail_unless( XMLNode_isAttributesEmpty(NULL) == 0);
  fail_unless( XMLNode_isElement(NULL) == 0);
  fail_unless( XMLNode_isEnd(NULL) == 0);
  fail_unless( XMLNode_isEndFor(NULL, NULL) == 0);
  fail_unless( XMLNode_isEOF(NULL) == 0);
  fail_unless( XMLNode_isNamespacesEmpty(NULL) == 0);
  fail_unless( XMLNode_isStart(NULL) == 0);
  fail_unless( XMLNode_isText(NULL) == 0);
  
  fail_unless( XMLNode_removeAttr(NULL, 0) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_removeAttrByName(NULL, NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_removeAttrByNS(NULL, NULL, NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_removeAttrByTriple(NULL, NULL) == LIBSBML_INVALID_OBJECT);
  
  fail_unless( XMLNode_removeChild(NULL, 0) == NULL);
  fail_unless( XMLNode_removeChildren(NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_removeNamespace(NULL, 0) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_removeNamespaceByPrefix(NULL, NULL) == LIBSBML_INVALID_OBJECT);
  
  fail_unless( XMLNode_setAttributes(NULL, NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_setEnd(NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_setEOF(NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_setNamespaces(NULL, NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNode_setTriple(NULL, NULL) == LIBSBML_INVALID_OBJECT);
  
  fail_unless( XMLNode_toXMLString(NULL) == NULL);
  fail_unless( XMLNode_unsetEnd(NULL) == LIBSBML_INVALID_OBJECT);
  
    
}