LIBLAX_CPP_NAMESPACE_USE
CK_CPPSTART
#endif

START_TEST(test_XMLToken_newSetters_setAttributes1)
{
  /*-- setup --*/

  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLToken_t*      token  = XMLToken_createWithTripleAttr(triple, attr);
  XMLAttributes_t* nattr  = XMLAttributes_create();

  XMLTriple_t* xt1    = XMLTriple_createWith("name1", "http://name1.org/", "p1");

  XMLAttributes_addWithTriple(nattr, xt1, "val1");

  /*-- test of setting attributes -- */

  int i = XMLToken_setAttributes(token, nattr);

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless(XMLToken_isAttributesEmpty(token)   == 0 );

  /*-- teardown --*/

  XMLAttributes_free(nattr);
  XMLAttributes_free(attr);
  XMLTriple_free(triple);
  XMLToken_free(token);
  XMLTriple_free(xt1);
}
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_XMLToken_newSetters_removeNamespaces1)
{
  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLToken_t*      token  = XMLToken_createWithTripleAttr(triple, attr);

  XMLToken_addNamespace(token, "http://test1.org/", "test1"); 

  fail_unless( XMLToken_getNamespacesLength(token) == 1 );
  
  int i = XMLToken_removeNamespaceByPrefix(token, "test2");
  
  fail_unless( i == LIBSBML_INDEX_EXCEEDS_SIZE);
  fail_unless( XMLToken_getNamespacesLength(token) == 1 );
  
  i = XMLToken_removeNamespaceByPrefix(token, "test1");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLToken_getNamespacesLength(token) == 0 );

  XMLToken_free(token);
  XMLTriple_free(triple);
  XMLAttributes_free(attr);
}
END_TEST


START_TEST(test_XMLToken_newSetters_addNamespaces1)
{
  /*-- setup --*/

  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLToken_t*      token  = XMLToken_createWithTripleAttr(triple, attr);
  
  fail_unless( XMLToken_getNamespacesLength(token) == 0 );
  fail_unless( XMLToken_isNamespacesEmpty(token)   == 1 );  

  /*-- test of setting namespaces -- */
  int i =  XMLToken_addNamespace(token, "http://test1.org/", "test1");

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLToken_getNamespacesLength(token) == 1 );
  fail_unless( XMLToken_isNamespacesEmpty(token)   == 0 );  

  /*-- teardown --*/

  XMLAttributes_free(attr);
  XMLTriple_free(triple);
  XMLToken_free(token);
}
END_TEST


START_TEST(test_XMLNode_clearNamespaces)
{
  /*-- setup --*/

  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLNode_t*       node   = XMLNode_createStartElement(triple, attr);
  const XMLNamespaces_t* nms;

  int i = XMLNode_addNamespace(node, "http://test1.org/", "test1");
  
  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  nms = XMLNode_getNamespaces(node);
  fail_unless (XMLNamespaces_getLength(nms) == 1);

  i = XMLNode_addNamespace(node, "http://test2.org/", "test2");
  
  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  nms = XMLNode_getNamespaces(node);
  fail_unless (XMLNamespaces_getLength(nms) == 2);

  i = XMLNode_clearNamespaces(node);

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS );
  nms = XMLNode_getNamespaces(node);
  fail_unless (XMLNamespaces_getLength(nms) == 0);

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


START_TEST(test_XMLToken_newSetters_addAttributes1)
{
  /*-- setup --*/

  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLToken_t*      token  = XMLToken_createWithTripleAttr(triple, attr);

  XMLTriple_t* xt2    = XMLTriple_createWith("name3", 
                                             "http://name3.org/", "p3");
  /*-- test of adding attributes --*/

  int i = XMLToken_addAttr(token, "name1", "val1");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless( XMLToken_getAttributesLength(token) == 1 );
  fail_unless( XMLToken_isAttributesEmpty(token)   == 0 );

  fail_unless( strcmp(XMLToken_getAttrName  (token, 0), "name1") == 0 );
  fail_unless( strcmp(XMLToken_getAttrValue (token, 0), "val1" ) == 0 );

  i = XMLToken_addAttrWithNS(token, "name2", "val2", 
                                             "http://name1.org/", "p1");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless( XMLToken_getAttributesLength(token) == 2 );
  fail_unless( XMLToken_isAttributesEmpty(token)   == 0 );

  fail_unless( strcmp(XMLToken_getAttrName  (token, 1), "name2") == 0 );
  fail_unless( strcmp(XMLToken_getAttrValue (token, 1), "val2" ) == 0 );
  fail_unless( strcmp(XMLToken_getAttrURI   (token, 1), "http://name1.org/") == 0 );
  fail_unless( strcmp(XMLToken_getAttrPrefix(token, 1), "p1"   ) == 0 );

  i = XMLToken_addAttrWithTriple(token, xt2, "val2");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless( XMLToken_getAttributesLength(token) == 3 );
  fail_unless( XMLToken_isAttributesEmpty(token)   == 0 );
  fail_unless( strcmp(XMLToken_getAttrName  (token, 2), "name3") == 0 );
  fail_unless( strcmp(XMLToken_getAttrValue (token, 2), "val2" ) == 0 );
  fail_unless( strcmp(XMLToken_getAttrURI   (token, 2), "http://name3.org/") == 0 );
  fail_unless( strcmp(XMLToken_getAttrPrefix(token, 2), "p3"   ) == 0 );

  /*-- teardown --*/

  XMLTriple_free(xt2);
  XMLTriple_free(triple);
  XMLAttributes_free(attr);
  XMLToken_free(token);
}
END_TEST


START_TEST(test_XMLNode_clearAttributes)
{
  /*-- setup --*/

  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLNode_t*      node  = XMLNode_createStartElement(triple, attr);

  XMLTriple_t* xt2    = XMLTriple_createWith("name3", 
                                             "http://name3.org/", "p3");
  XMLTriple_t* xt1    = XMLTriple_createWith("name5", 
                                             "http://name5.org/", "p5");
  int i = XMLNode_addAttr(node, "name1", "val1");
  
  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  fail_unless (XMLAttributes_getLength(XMLNode_getAttributes(node)) == 1);

  i = XMLNode_addAttrWithNS(node, "name2", "val2", 
                                             "http://name1.org/", "p1");
  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  fail_unless (XMLAttributes_getLength(XMLNode_getAttributes(node)) == 2);

  i = XMLNode_addAttrWithTriple(node, xt2, "val2");
  
  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  fail_unless (XMLAttributes_getLength(XMLNode_getAttributes(node)) == 3);

  i = XMLNode_addAttr(node, "name4", "val4");

  fail_unless(i == LIBSBML_OPERATION_SUCCESS);
  fail_unless (XMLAttributes_getLength(XMLNode_getAttributes(node)) == 4);

  i = XMLNode_clearAttributes(node);

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless (XMLAttributes_getLength(XMLNode_getAttributes(node)) == 0);

  /*-- teardown --*/

  XMLTriple_free(xt1);
  XMLTriple_free(xt2);
  XMLTriple_free(triple);
  XMLAttributes_free(attr);
  XMLNode_free(node);
}
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_XMLToken_newSetters_setAttributes2)
{
  int i ;
  /*-- setup --*/

  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLToken_t*      token  = XMLToken_createWithTriple(triple);
  XMLAttributes_t* nattr  = XMLAttributes_create();

  XMLTriple_t* xt1    = XMLTriple_createWith("name1", "http://name1.org/", "p1");

  XMLAttributes_addWithTriple(nattr, xt1, "val1");

  /*-- test of setting attributes with NULL value -- */
  i = XMLToken_setAttributes(token, NULL);

  fail_unless ( i == LIBSBML_INVALID_OBJECT);

  /*-- test of setting attributes -- */

  i = XMLToken_setAttributes(token, nattr);

  fail_unless ( i == LIBSBML_INVALID_XML_OPERATION);
  fail_unless(XMLToken_isAttributesEmpty(token)   == 1 );


  /*-- teardown --*/

  XMLAttributes_free(nattr);
  XMLTriple_free(triple);
  XMLToken_free(token);
  XMLTriple_free(xt1);
}
END_TEST


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

  XMLTriple_t * toptriple = XMLTriple_createWith("notes", "", "");
  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);
  XMLToken_t *toptoken = XMLToken_createWithTripleAttr(toptriple, att);
  XMLNode_t *topnode = XMLNode_createFromToken(toptoken);
  XMLToken_t *token = XMLToken_createWithTripleAttrNS(triple, att, ns);
  XMLNode_t *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 );

  XMLTriple_free(triple);
  XMLToken_free(token);
  XMLNode_free(node);
  triple = XMLTriple_createWith("html", "", "");
  token = XMLToken_createWithTripleAttrNS(triple, att, ns);
  node = XMLNode_createFromToken(token);
  XMLNode_addChild(node, n1);
  XMLNode_free(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 );

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

  XMLToken_free(token);
  XMLNode_free(node);
  token = XMLToken_createWithTripleAttrNS(triple, att, ns);
  node = XMLNode_createFromToken(token);
  XMLNode_addChild(node, n1);
  XMLNode_free(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 );

  SBMLNamespaces_free(NS24);
  SBMLNamespaces_free(NS31);
  XMLTriple_free(toptriple);
  XMLTriple_free(triple);
  XMLAttributes_free(att);
  XMLNamespaces_free(ns);
  XMLToken_free(tt);
  XMLNode_free(n1);
  XMLToken_free(toptoken);
  XMLNode_free(topnode);
  XMLToken_free(token);
  XMLNode_free(node);
}
END_TEST


START_TEST(test_XMLToken_newSetters_removeAttributes1)
{
  /*-- setup --*/

  XMLTriple_t*     triple = XMLTriple_createWith("test","","");
  XMLAttributes_t* attr   = XMLAttributes_create();
  XMLToken_t*      token  = XMLToken_createWithTripleAttr(triple, attr);

  XMLTriple_t* xt2    = XMLTriple_createWith("name3", 
                                             "http://name3.org/", "p3");
  XMLTriple_t* xt1    = XMLTriple_createWith("name5", 
                                             "http://name5.org/", "p5");
  int i = XMLToken_addAttr(token, "name1", "val1");
  i = XMLToken_addAttrWithNS(token, "name2", "val2", 
                                             "http://name1.org/", "p1");
  i = XMLToken_addAttrWithTriple(token, xt2, "val2");
  i = XMLToken_addAttr(token, "name4", "val4");

  fail_unless (XMLAttributes_getLength(XMLToken_getAttributes(token)) == 4);

  i = XMLToken_removeAttr(token, 7);

  fail_unless ( i == LIBSBML_INDEX_EXCEEDS_SIZE );

  i = XMLToken_removeAttrByName(token, "name7");

  fail_unless ( i == LIBSBML_INDEX_EXCEEDS_SIZE );

  i = XMLToken_removeAttrByNS(token, "name7", "namespaces7");

  fail_unless ( i == LIBSBML_INDEX_EXCEEDS_SIZE );

  i = XMLToken_removeAttrByTriple(token, xt1);

  fail_unless ( i == LIBSBML_INDEX_EXCEEDS_SIZE );
  fail_unless (XMLAttributes_getLength(XMLToken_getAttributes(token)) == 4);

  i = XMLToken_removeAttr(token, 3);

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless (XMLAttributes_getLength(XMLToken_getAttributes(token)) == 3);

  i = XMLToken_removeAttrByName(token, "name1");

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless (XMLAttributes_getLength(XMLToken_getAttributes(token)) == 2);

  i = XMLToken_removeAttrByNS(token, "name2", "http://name1.org/");

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless (XMLAttributes_getLength(XMLToken_getAttributes(token)) == 1);

  i = XMLToken_removeAttrByTriple(token, xt2);

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS );
  fail_unless (XMLAttributes_getLength(XMLToken_getAttributes(token)) == 0);

  /*-- teardown --*/

  XMLTriple_free(xt1);
  XMLTriple_free(xt2);
  XMLTriple_free(triple);
  XMLAttributes_free(attr);
  XMLToken_free(token);
}