Skip to content
Snippets Groups Projects
DTest.cpp 77.5 KiB
Newer Older
Ted Leung's avatar
Ted Leung committed
/*
 * The Apache Software License, Version 1.1
Rahul Jain's avatar
Rahul Jain committed
 * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
Ted Leung's avatar
Ted Leung committed
 * reserved.
Ted Leung's avatar
Ted Leung committed
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Ted Leung's avatar
Ted Leung committed
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
Ted Leung's avatar
Ted Leung committed
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
Ted Leung's avatar
Ted Leung committed
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
Ted Leung's avatar
Ted Leung committed
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
Ted Leung's avatar
Ted Leung committed
 * 4. The names "Xerces" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
Ted Leung's avatar
Ted Leung committed
 *    permission, please contact apache\@apache.org.
Ted Leung's avatar
Ted Leung committed
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
Ted Leung's avatar
Ted Leung committed
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
Ted Leung's avatar
Ted Leung committed
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation, and was
 * originally based on software copyright (c) 1999, International
 * Business Machines, Inc., http://www.ibm.com .  For more information
 * on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

Ted Leung's avatar
Ted Leung committed
 * $Log$
 * Revision 1.14  2001/11/23 20:44:10  tng
 * Solaris Forte C++ core dump when handling this long condition check; so break it into smaller check to bypass this compiler problem.
 *
 * Revision 1.13  2000/08/30 22:21:44  andyh
 * Unix Build script fixes.  Clean up some UNIX compiler warnings.
 *
 * Revision 1.12  2000/05/15 23:08:59  andyh
 * Remove entity tests, which failed because entity nodes are (correctly)
 * read-only.
 *
 * Revision 1.11  2000/04/19 02:25:18  aruna1
 * Full support for DOM_EntityReference, DOM_Entity and DOM_DocumentType introduced
 *
 * Revision 1.10  2000/03/11 03:00:24  chchou
 * Fix bug # 18, remove set method of readonly attributes.
 * As a result, remove related test cases
 *
 * Revision 1.9  2000/03/02 19:55:45  roddey
 * This checkin includes many changes done while waiting for the
 * 1.1.0 code to be finished. I can't list them all here, but a list is
 * available elsewhere.
 *
 * Revision 1.8  2000/02/29 02:19:13  rahulj
 * No more compilation errors under HPUX 11.0. We do not build
 * DOMMemTest as it crashes the aCC compiler.
 *
Rahul Jain's avatar
Rahul Jain committed
 * Revision 1.7  2000/02/06 07:48:36  rahulj
 * Year 2K copyright swat.
 *
 * Revision 1.6  2000/02/04 05:46:31  andyh
 * Change offsets and lengths form signed to unsigned
 *
 * Revision 1.5  2000/02/04 01:50:13  aruna1
 * TreeWalker and NodeIterator changes
 *
 * Revision 1.4  2000/01/29 00:39:09  andyh
 * Redo synchronization in DOMStringHandle allocator.  There
 * was a bug in the use of Compare and Swap.  Switched to mutexes.
 *
 * Changed a few plain deletes to delete [].
 *
 * Revision 1.3  2000/01/19 21:40:58  andyh
 * Remove a few remaining dependencies on the (now defunct)
 * XML StdOut stream.
 *
 * Revision 1.2  1999/12/03 00:02:25  andyh
 * DOM tests updated to catch DOMException by ref.
 * Added tests of DOMString::transcode() and append()
 *
 * Revision 1.1.1.1  1999/11/09 01:02:34  twl
 * Initial checkin
Ted Leung's avatar
Ted Leung committed
 *
 * Revision 1.2  1999/11/08 20:42:24  rahul
 * Swat for adding in Product name and CVS comment log variable.
 *
 */



/**
 * This class tests methods for XML DOM implementation
 * version 2.0 10/12/98
Ted Leung's avatar
Ted Leung committed
 * DOM_DOMException errors are tested by calls to DOMExceptionsTest from: Main, docBuilder...
 *
 */
Ted Leung's avatar
Ted Leung committed
#include <stdio.h>
#include "DTest.h"
#include <util/PlatformUtils.hpp>
#include <util/XMLException.hpp>
#include <util/XMLString.hpp>
Ted Leung's avatar
Ted Leung committed


#define EXCEPTIONSTEST(operation, expectedException, resultFlag, testNum) \
    {                                                               \
        try                                                         \
        {                                                           \
            operation;                                              \
            fprintf(stderr, "Exceptions Test # %d: no Exception thrown.\n", testNum); \
        }                                                           \
        catch (DOM_DOMException &e)                                 \
Ted Leung's avatar
Ted Leung committed
        {                                                           \
            if (e.code != expectedException) {                      \
Ted Leung's avatar
Ted Leung committed
                fprintf(stderr, "Exceptions Test # %d: wrong DOM_DOMException thrown.\n", \
                    testNum);                                       \
                resultFlag = false;                                 \
            }                                                       \
        }                                                           \
        catch (...)                                                 \
        {                                                           \
            fprintf(stderr, "Exceptions Test # %d: unknown exception thrown.\n",    \
                testNum);                                           \
            resultFlag = false;                                     \
        }                                                           \
Ted Leung's avatar
Ted Leung committed


#define LEAKTEST(operation)                             \
{                                                       \
    operation;   /* Precondition  */                    \
    DomMemDebug entryMemState;                          \
    {                                                   \
        operation;                                      \
    }                                                   \
    DomMemDebug exitMemState;                           \
    if (entryMemState != exitMemState)                  \
    {                                                   \
        printf("Memory Leak at line %d\n", __LINE__);   \
        exitMemState.printDifference(entryMemState);    \
    }                                                   \
}




DOM_Element                 DTest::testElementNode;
DOM_Attr                    DTest::testAttributeNode;
DOM_Text                    DTest::testTextNode;
DOM_CDATASection            DTest::testCDATASectionNode;
DOM_EntityReference         DTest::testEntityReferenceNode;
DOM_Entity                  DTest::testEntityNode;
DOM_ProcessingInstruction   DTest::testProcessingInstructionNode;
DOM_Comment                 DTest::testCommentNode;
DOM_Document                DTest::testDocumentNode;
DOM_DocumentType            DTest::testDocumentTypeNode;
DOM_DocumentFragment        DTest::testDocumentFragmentNode;
DOM_Notation                DTest::testNotationNode;

/**
Ted Leung's avatar
Ted Leung committed
 * version 2.0 10/12/98
 *
 */
Ted Leung's avatar
Ted Leung committed
DTest::DTest()
{
};


/**
 * version 3.0 01/25/99
Ted Leung's avatar
Ted Leung committed
 * @return DOM_Document
 *
 */
DOM_Document DTest::createDocument() {
    return DOM_Document::createDocument();
};


/**
 * version 3.0 01/25/99
Ted Leung's avatar
Ted Leung committed
 * @return DOM_DocumentType
 * @param name DOMString
 *
 */
DOM_DocumentType DTest::createDocumentType(DOM_Document doc, DOMString name) {
    return doc.createDocumentType(name);    //Replace with a DOM_DocumentType creator
};


/**
 * version 3.0 01/25/99
Ted Leung's avatar
Ted Leung committed
 * @return org.w3c.dom.DOM_Entity
 * @param doc org.w3c.dom.DOM_Document
 * @param name DOMString
 *
 */
DOM_Entity DTest::createEntity(DOM_Document doc, DOMString name) {
    return doc.createEntity(name);
};



/**
 * version 3.0 01/25/99
Ted Leung's avatar
Ted Leung committed
 * @return org.w3c.dom.DOM_Notation
 * @param doc org.w3c.dom.DOM_Document
 * @param name DOMString
 *
 */
DOM_Notation DTest::createNotation(DOM_Document doc, DOMString name) {
    return doc.createNotation(name);
};


/**
 * This method builds test documents for the XML DOM implementation
 * version 2.0 10/12/98
 * @param document org.w3c.dom.DOM_Document
 * @param name document's name
 * @param type document's type
 *
 */
void DTest::docBuilder(DOM_Document document, DOMString name)
{
    DOM_Document doc = document;
    bool OK = true;
Ted Leung's avatar
Ted Leung committed
    DOM_Element docFirstElement = doc.createElement(name + "FirstElement");
    doc.appendChild(docFirstElement);
    docFirstElement.setAttribute(name + "FirstElement", name + "firstElement");
Ted Leung's avatar
Ted Leung committed
    DOM_ProcessingInstruction docProcessingInstruction = doc.createProcessingInstruction(name +
                    "TargetProcessorChannel", DOMString("This is ") + doc.getNodeName() + "'s processing instruction");
    docFirstElement.appendChild(docProcessingInstruction);
Ted Leung's avatar
Ted Leung committed
    DOM_Element docBody = doc.createElement(name + "TestBody");
    docFirstElement.appendChild(docBody);
Ted Leung's avatar
Ted Leung committed
    DOM_Element docBodyLevel21 = doc.createElement(name + "BodyLevel21");
    DOM_Element docBodyLevel22 = doc.createElement(name + "BodyLevel22");
    DOM_Element docBodyLevel23 = doc.createElement(name + "BodyLevel23");
    DOM_Element docBodyLevel24 = doc.createElement(name + "BodyLevel24");
    docBody.appendChild(docBodyLevel21);
    docBody.appendChild(docBodyLevel22);
    docBody.appendChild(docBodyLevel23);
    docBody.appendChild(docBodyLevel24);
Ted Leung's avatar
Ted Leung committed
    DOM_Element docBodyLevel31 = doc.createElement(name + "BodyLevel31");
    DOM_Element docBodyLevel32 = doc.createElement(name + "BodyLevel32");
    DOM_Element docBodyLevel33 = doc.createElement(name + "BodyLevel33");
    DOM_Element docBodyLevel34 = doc.createElement(name + "BodyLevel34");
    docBodyLevel21.appendChild(docBodyLevel31);
    docBodyLevel21.appendChild(docBodyLevel32);
    docBodyLevel22.appendChild(docBodyLevel33);
    docBodyLevel22.appendChild(docBodyLevel34);
Ted Leung's avatar
Ted Leung committed
    DOM_Text docTextNode11 = doc.createTextNode(name + "BodyLevel31'sChildTextNode11");
    DOM_Text docTextNode12 = doc.createTextNode(name + "BodyLevel31'sChildTextNode12");
    DOM_Text docTextNode13 = doc.createTextNode(name + "BodyLevel31'sChildTextNode13");
    DOM_Text docTextNode2 = doc.createTextNode(name + "TextNode2");
    DOM_Text docTextNode3 = doc.createTextNode(name + "TextNode3");
    DOM_Text docTextNode4 = doc.createTextNode(name + "TextNode4");
    docBodyLevel31.appendChild(docTextNode11);
    docBodyLevel31.appendChild(docTextNode12);
    docBodyLevel31.appendChild(docTextNode13);
    docBodyLevel32.appendChild(docTextNode2);
    docBodyLevel33.appendChild(docTextNode3);
    docBodyLevel34.appendChild(docTextNode4);
Ted Leung's avatar
Ted Leung committed
    DOM_CDATASection docCDATASection = doc.createCDATASection("<![CDATA[<greeting>Hello, world!</greeting>]]>");
    docBodyLevel23.appendChild(docCDATASection);
Ted Leung's avatar
Ted Leung committed
    DOM_Comment docComment = doc.createComment("This should be a comment of some kind ");
    docBodyLevel23.appendChild(docComment);
Ted Leung's avatar
Ted Leung committed
    DOM_EntityReference docReferenceEntity = doc.createEntityReference("ourEntityNode");
    docBodyLevel24.appendChild(docReferenceEntity);

    DTest make;
    DOM_Notation docNotation = make.createNotation(doc, "ourNotationNode");
    DOM_Node abc1 = doc.getFirstChild();
    DOM_DocumentType docType = (DOM_DocumentType &) abc1;
Ted Leung's avatar
Ted Leung committed
    docType.getNotations().setNamedItem(docNotation);
Ted Leung's avatar
Ted Leung committed
    DOM_DocumentFragment docDocFragment = doc.createDocumentFragment();
Ted Leung's avatar
Ted Leung committed
//  printf("This document's first element name is " + docFirstElement.getTagName() + "\n");


//***********Following are for errorTests
    DOM_Text docNode3 = doc.createTextNode(name + "docTextNode3");
    DOM_Text docNode4 = doc.createTextNode(name + "docTextNode4");
    DOM_Node  abc2 =  doc.getDoctype().getEntities().getNamedItem("ourEntityNode"); // Get the DOM_Entity node
    DOM_Entity docEntity = (DOM_Entity &) abc2;
    DOM_Node  abc3 = doc.getFirstChild(); // Get the DOM_DocumentType node
    DOM_DocumentType docDocType = (DOM_DocumentType &) abc3;
    DOM_Node  abc4 = doc.getLastChild().getLastChild().getLastChild().getFirstChild();
    DOM_EntityReference & entityReferenceText = (DOM_EntityReference &) abc4;
Ted Leung's avatar
Ted Leung committed
    DOM_Text entityReferenceText2 = doc.createTextNode("entityReferenceText information");
//************************************************* ERROR TESTS
    DTest tests;

    EXCEPTIONSTEST(document.appendChild(docBody), DOM_DOMException::HIERARCHY_REQUEST_ERR, OK,  1);

    EXCEPTIONSTEST(document.appendChild(docBody), DOM_DOMException::HIERARCHY_REQUEST_ERR, OK, 2);
    EXCEPTIONSTEST(docNode3.appendChild(docNode4), DOM_DOMException::HIERARCHY_REQUEST_ERR, OK, 3);
    // EXCEPTIONSTEST(doc.insertBefore(docEntity, docFirstElement), DOM_DOMException::HIERARCHY_REQUEST_ERR, OK, 4);
    EXCEPTIONSTEST(doc.replaceChild(docCDATASection, docFirstElement), DOM_DOMException::HIERARCHY_REQUEST_ERR, OK, 5);
    EXCEPTIONSTEST(docFirstElement.setNodeValue("This shouldn't work!" ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 6);
Ted Leung's avatar
Ted Leung committed
    EXCEPTIONSTEST(docReferenceEntity.setNodeValue("This shouldn't work!" ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 7);
    // EXCEPTIONSTEST(docEntity.setNodeValue("This shouldn't work!" ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 8);
Ted Leung's avatar
Ted Leung committed
    EXCEPTIONSTEST(doc.setNodeValue("This shouldn't work!" ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 9);
    EXCEPTIONSTEST(docDocType.setNodeValue("This shouldn't work!" ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 10);
    EXCEPTIONSTEST(docDocFragment.setNodeValue("This shouldn't work!" ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 11);
    EXCEPTIONSTEST(docNotation.setNodeValue("This shouldn't work!" ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 12);
    EXCEPTIONSTEST(docReferenceEntity.appendChild(entityReferenceText2 ), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR , OK, 13);
    EXCEPTIONSTEST(docBodyLevel32.insertBefore(docTextNode11,docBody ), DOM_DOMException::NOT_FOUND_ERR, OK, 14);
    EXCEPTIONSTEST(docBodyLevel32.removeChild(docFirstElement), DOM_DOMException::NOT_FOUND_ERR, OK, 15);
    EXCEPTIONSTEST(docBodyLevel32.replaceChild(docTextNode11,docFirstElement ), DOM_DOMException::NOT_FOUND_ERR, OK, 16);


//!! Throws a NOT_FOUND_ERR ********
Ted Leung's avatar
Ted Leung committed
     // docBodyLevel32.getAttributes().removeNamedItem(testAttribute.getName());    16  // To test removeNamedItem
Ted Leung's avatar
Ted Leung committed
};  //END OF DOCBUILDER



/**
 * @param document org.w3c.dom.DOM_Document
 */
 void DTest::findTestNodes(DOM_Document document) {
    DOM_Node node = document;
    int nodeCount = 0;

    // Walk the tree until you find and assign all node types needed that exist.
    while (node != null && nodeCount < 12)
    {

        switch (node.getNodeType())
    {
        case DOM_Node::ELEMENT_NODE :
            if (testElementNode == null) {testElementNode = (DOM_Element &)node; nodeCount++;}
            break;
        case DOM_Node::ATTRIBUTE_NODE :
            if (testAttributeNode == null) {testAttributeNode = (DOM_Attr &)node; nodeCount++;}
            break;
        case DOM_Node::TEXT_NODE :
            if (testTextNode == null) {testTextNode = (DOM_Text &)node; nodeCount++;}
            break;
        case DOM_Node::CDATA_SECTION_NODE :
            if (testCDATASectionNode == null) {testCDATASectionNode = (DOM_CDATASection &)node; nodeCount++;}
            break;
        case DOM_Node::ENTITY_REFERENCE_NODE :
            if (testEntityReferenceNode == null) {testEntityReferenceNode = (DOM_EntityReference &)node; nodeCount++;}
            break;
        case DOM_Node::ENTITY_NODE :
            if (testEntityNode == null) {testEntityNode = (DOM_Entity &)node; nodeCount++;}
            break;
        case DOM_Node::PROCESSING_INSTRUCTION_NODE :
            if (testProcessingInstructionNode == null) {testProcessingInstructionNode = (DOM_ProcessingInstruction &)node; nodeCount++;}
            break;
        case DOM_Node::COMMENT_NODE :
            if (testCommentNode == null) {testCommentNode = (DOM_Comment &)node; nodeCount++;}
            break;
        case DOM_Node::DOCUMENT_TYPE_NODE :
            if (testDocumentTypeNode == null) {testDocumentTypeNode = (DOM_DocumentType &)node; nodeCount++;}
            break;
        case DOM_Node::DOCUMENT_FRAGMENT_NODE :
            if (testDocumentFragmentNode == null) {testDocumentFragmentNode = (DOM_DocumentFragment &)node; nodeCount++;}
            break;
        case DOM_Node::NOTATION_NODE :
            if (testNotationNode == null) {testNotationNode = (DOM_Notation &)node; nodeCount++;}
            break;
        case DOM_Node::DOCUMENT_NODE :
            if (testDocumentNode == null) {testDocumentNode = (DOM_Document &)node; nodeCount++;}
            break;
        default:
            ;
    }// End of switch

    }   // End of while
};


/**
 * @param document org.w3c.dom.DOM_Document
 */
 void DTest::findTestNodes(DOM_Node node) {
    DTest test;
    DOM_Node kid;
    // Walk the tree until you find and assign all node types needed that exist.
Ted Leung's avatar
Ted Leung committed
    if (node.getFirstChild() != null)
    {
        kid = node.getFirstChild();
        test.findTestNodes(kid);
    }
Ted Leung's avatar
Ted Leung committed
    if (node.getNextSibling() != null)
    {
        kid = node.getNextSibling();
        test.findTestNodes(kid);
    }

Ted Leung's avatar
Ted Leung committed
    switch (node.getNodeType())
    {
        case DOM_Node::ELEMENT_NODE :
            if (testElementNode == null) {testElementNode = (DOM_Element &)node; }
            break;
        case DOM_Node::ATTRIBUTE_NODE :
            if (testAttributeNode == null) {testAttributeNode = (DOM_Attr &)node; }
            break;
        case DOM_Node::TEXT_NODE :
            if (testTextNode == null) {testTextNode = (DOM_Text &)node; }
            break;
        case DOM_Node::CDATA_SECTION_NODE :
            if (testCDATASectionNode == null) {testCDATASectionNode = (DOM_CDATASection &)node; }
            break;
        case DOM_Node::ENTITY_REFERENCE_NODE :
            if (testEntityReferenceNode == null) {testEntityReferenceNode = (DOM_EntityReference &)node;}
            break;
        case DOM_Node::ENTITY_NODE :
            if (testEntityNode == null) {testEntityNode = (DOM_Entity &)node;}
            break;
        case DOM_Node::PROCESSING_INSTRUCTION_NODE :
            if (testProcessingInstructionNode == null) {testProcessingInstructionNode = (DOM_ProcessingInstruction &)node;}
            break;
        case DOM_Node::COMMENT_NODE :
            if (testCommentNode == null) {testCommentNode = (DOM_Comment &)node;}
            break;
        case DOM_Node::DOCUMENT_TYPE_NODE :
            if (testDocumentTypeNode == null) {testDocumentTypeNode = (DOM_DocumentType &)node; }
            break;
        case DOM_Node::DOCUMENT_FRAGMENT_NODE :
            if (testDocumentFragmentNode == null) {testDocumentFragmentNode = (DOM_DocumentFragment &)node;}
            break;
        case DOM_Node::NOTATION_NODE :
            if (testNotationNode == null) {testNotationNode = (DOM_Notation &)node;}
            break;
        case DOM_Node::DOCUMENT_NODE :
            if (testDocumentNode == null) {testDocumentNode = (DOM_Document &)node;}
            break;
        default:
            ;
    }// End of switch
};//End of class

/**
Ted Leung's avatar
Ted Leung committed
 * version 2.0 10/12/98
 *
 */
 int main(int argc, char **argv)
Ted Leung's avatar
Ted Leung committed
 {
Ted Leung's avatar
Ted Leung committed
     {
         //  Nest entire test in an inner block.
         //     Reference counting should recover all document
         //     storage when this block exits.
Ted Leung's avatar
Ted Leung committed
         DTest test;
         try {
             XMLPlatformUtils::Initialize();
         }
         catch (const XMLException& toCatch) {
             char *pMessage = XMLString::transcode(toCatch.getMessage());
             fprintf(stderr, "Error during initialization! \n  %s \n", pMessage);
Ted Leung's avatar
Ted Leung committed
         long avgTime = 0;
         bool OK = true;
         long startTime = 0;//****************Time the whole thing for efficiency of DOM implementation
Ted Leung's avatar
Ted Leung committed
         // for (int i=0; i< 1000; i++)
Ted Leung's avatar
Ted Leung committed
         // AH Revisit  //  startTime = System.currentTimeMillis();
         //     if(!OK)
         //     break;
Ted Leung's avatar
Ted Leung committed
         DOM_Document d = test.createDocument();
         // DOM_Document z = test.createDocument();
Ted Leung's avatar
Ted Leung committed
         DOM_DocumentType docDocType = test.createDocumentType(d,"testDocument1");
         d.appendChild(docDocType);
Ted Leung's avatar
Ted Leung committed
         DOM_Entity docEntity = test.createEntity( d, "ourEntityNode");
         //Build a branch for entityReference tests
         // DOM_Text entityChildText = d.createTextNode("entityChildText information"); //
         // docEntity.appendChild(entityChildText);
         // docDocType.getEntities().setNamedItem(docEntity);
Ted Leung's avatar
Ted Leung committed
         test.docBuilder(d, "d");
Ted Leung's avatar
Ted Leung committed
         test.findTestNodes((DOM_Node)d);
         // test.docBuilder(z, "z");
         // try {
         LEAKTEST(test.testAttr(d););
         LEAKTEST(test.testCDATASection(d););
         LEAKTEST(test.testCharacterData(d););
         LEAKTEST(test.testChildNodeList(d););
         LEAKTEST(test.testComment(d););
         LEAKTEST(test.testDeepNodeList(d););
         LEAKTEST(test.testDocument(d););
         LEAKTEST(test.testDocumentFragment(d););
Ted Leung's avatar
Ted Leung committed
         LEAKTEST(test.testDOMImplementation(d););
         LEAKTEST(test.testElement(d););
//       LEAKTEST(test.testEntity(d););      // Can not test entities;  only parser can create them.
Ted Leung's avatar
Ted Leung committed
         LEAKTEST(test.testEntityReference(d););
         LEAKTEST(test.testNode(d););
         LEAKTEST(test.testNotation(d););
         LEAKTEST(test.testPI(d););
         LEAKTEST(test.testText(d););
Ted Leung's avatar
Ted Leung committed
         LEAKTEST(test.testDOMerrors(d););
Ted Leung's avatar
Ted Leung committed
         //!! Throws WRONG_DOCUMENT_ERR **********
Ted Leung's avatar
Ted Leung committed
         // z.appendChild(d.createComment("Test doc d comment"));// Tries to append z document with document d comment
         // d.getDocumentElement().appendChild(z.createElement("newZdocElement"));// Tries to append d document with document z DOM_Element
         // d.getLastChild().getLastChild().insertBefore(z.createElement("newZdocElement"),d.getLastChild().getLastChild().getFirstChild());// Tries to insert into d document with document z DOM_Element
         // d.replaceChild(z.createElement("newZdocElement"),d.getLastChild().getLastChild().getFirstChild());  // Tries to replace in d document with document z DOM_Element
Ted Leung's avatar
Ted Leung committed
         /* Attribute newAttribute = d.createAttribute("newAttribute");
         d.getDocumentElement().setAttributeNode(newAttribute);
         d.getDocumentElement().getAttributes().setNamedItem(z.createAttribute("newzAttribute"));
         */
Ted Leung's avatar
Ted Leung committed
         //!! Throws INVALID_CHARACTER_ERR **********
         // ******This just gets us through each method. JKess has a comprehensive test of Invalid Names******
         // d.createAttribute("Invalid Name"); // Name with blank space
         // d.createElement("5InvalidName"); // Name starts with numeric
         // d.createProcessingInstruction("This is the target processor channel","InvalidName>");// Name ends with >
         // d.getDocumentElement().setAttribute("Invalid%Name",""); // Name contains %
Ted Leung's avatar
Ted Leung committed
         //!!   ******** NO_DATA_ALLOWED_ERR ********No cases to test as of 9/15
Ted Leung's avatar
Ted Leung committed
         //!!   ******** NO_MODIFICATION_ALLOWED_ERR ******** When read only exists
         /*
Ted Leung's avatar
Ted Leung committed
               // **** FOR DOM_Element when read only exists********
               .removeAttribute("aString");        // ***** Not until read only exists.
               .removeAttributeNode(Attribute);        // ***** Not until read only exists.
               .setAttribute("aString", "anotherString"); // ***** Not until read only exists.
Ted Leung's avatar
Ted Leung committed
                   // **** FOR DOM_Node when read only exists********
                   .appendChild(aNode);         // ***** Not until read only exists.
                   .insertBefore(aNode, AnotherNode);   // ***** Not until read only exists.
                   .removeChild(aNode);         // ***** Not until read only exists.
                   .replaceChild(aNode);            // ***** Not until read only exists.
Ted Leung's avatar
Ted Leung committed
                     .splitText(2); // ***** Not until read only exists.
Ted Leung's avatar
Ted Leung committed
                       .setNamedItem(DOM_Node); // ***** Not until read only exists.
         */
Ted Leung's avatar
Ted Leung committed
         //!!******** NOT_SUPPORTED_ERR ********For HTML when implemented
         /*
         .createCDATASection("String stuff");
         .createEntityReference("String stuff");
         .createProcessingInstruction("String stuff", "Some more String stuff");
         */
Ted Leung's avatar
Ted Leung committed
         // } catch (DOM_DOMException &e) {
         //     fprintf(stderr, "Unexpected DOM Exception caught.  Code is: %d\n", e.code);
         //     OK = false;
         // }
         //    catch (...) {
         //        fprintf(stderr, "Unexpected non-DOM exception caught.");
         //    };
Ted Leung's avatar
Ted Leung committed
         //System.err.println("Elapsed time (measured in seconds): " +   ((System.currentTimeMillis() - startTime) / 1000.0));
         // AH revisit avgTime += System.currentTimeMillis() - startTime;
         // }//END OF FOR
Ted Leung's avatar
Ted Leung committed
         // System.err.println("Elapsed time (measured in seconds): " +
         //                    ((System.currentTimeMillis() - startTime) / 1000.0));
         //       System.err.println("Elapsed time (measured in mili-seconds): " +
         //                    ((System.currentTimeMillis() - startTime)));
Ted Leung's avatar
Ted Leung committed
         // System.err.println("Average Elapsed time (measured in seconds): " + (avgTime/10000000.0) );

         // Null out the static object references in class DTest,
         // which will recover their storage.  Part of testing for memory leaks.
         DTest::testElementNode = null;
         DTest::testAttributeNode = null;
         DTest::testTextNode = null;
         DTest::testCDATASectionNode = null;
         DTest::testEntityReferenceNode = null;
         DTest::testEntityNode = null;
         DTest::testProcessingInstructionNode = null;
         DTest::testCommentNode = null;
         DTest::testDocumentNode = null;
         DTest::testDocumentTypeNode = null;
         DTest::testDocumentFragmentNode = null;
         DTest::testNotationNode = null;
Ted Leung's avatar
Ted Leung committed
    };
Ted Leung's avatar
Ted Leung committed
    DomMemDebug().print();
Ted Leung's avatar
Ted Leung committed
};


/**
 * This method tests DOM_Attr methods for the XML DOM implementation
 * version 2.0 10/12/98
 * @param document org.w3c.dom.DOM_Document
 *
 */
void DTest::testAttr(DOM_Document document)
{
Ted Leung's avatar
Ted Leung committed
    DOM_Attr attributeNode, attribute2;
    DOMString compare;
    bool T = true;
    bool F = false;
    bool OK = true;
// For debugging*****   printf("\n          testAttr's outputs:\n\n");

    DOM_Attr testAttribute = document.createAttribute("testAttribute");
    testAttribute.setValue("testAttribute's value");
    node = document.getDocumentElement(); // node gets first element

    // ((DOM_Element &)node).setAttributeNode(testAttribute);
    // attributeNode = ((DOM_Element &)node).getAttributeNode("testAttribute");
    DOM_Element el = (DOM_Element &)node;
Ted Leung's avatar
Ted Leung committed
    attributeNode = el.getAttributeNode("testAttribute");
Ted Leung's avatar
Ted Leung committed
    compare = "testAttribute";
    if (!compare.equals(attributeNode.getName()))
    {
        printf("Warning!!! DOM_Attr's 'getName' method failed to work properly!\n");
        OK = false;
    }
    compare = "testAttribute's value";
    if (!compare.equals(attributeNode.getNodeValue()))
    {
        printf("Warning!!! DOM_Attr's 'getNodeValue' method failed to work properly!\n");
        OK = false;
    }
    if (! T ==attributeNode.getSpecified())
    {
        printf("Warning!!! DOM_Attr's 'getSpecified' method failed to work properly!\n");
        OK = false;
    }
Ted Leung's avatar
Ted Leung committed
    if (!compare.equals(attributeNode.getValue()))
    {
        printf("Warning!!! DOM_Attr's 'getValue' method failed to work properly!\n");
        OK = false;
    }


    attributeNode.setNodeValue("Reset Value");   /// LEAK!!!!!
    compare = "Reset Value";
    if (!compare.equals(attributeNode.getNodeValue()))
    {
        printf("Warning!!! DOM_Attr's 'setNodeValue' method failed to work properly!\n");
        OK = false;
    }

    attributeNode.setValue(null);
    if (! attributeNode.getValue().equals(""))
    {
        printf("Warning!!! DOM_Attr's 'setValue' to 'null' method failed to work properly!\n");
        OK = false;
    }
Ted Leung's avatar
Ted Leung committed
    attributeNode.setValue("Another value ");
    compare = "Another value ";
    if (!compare.equals(attributeNode.getValue()))
    {
        printf("Warning!!! DOM_Attr's 'setValue' method failed to work properly!");
        OK = false;
    }

    node = attributeNode.cloneNode(T);
Ted Leung's avatar
Ted Leung committed
    // Check nodes for equality, both their name and value or lack thereof
    bool cloneOK = true;
    if (!(node.getNodeName().equals(attributeNode.getNodeName())))
        cloneOK = false;
    if (node.getNodeValue() == null &&
        attributeNode.getNodeValue() != null)
    {
        attributeNode.getNodeValue().print();
        cloneOK = false;
    }

    if (node.getNodeValue() != null && attributeNode.getNodeValue() == null)
    {
        cloneOK = false;
        node.getNodeValue().print();
    };

    if (node.getNodeValue() != null && attributeNode.getNodeValue() != null)
    {
        if (!(node.getNodeValue().equals(attributeNode.getNodeValue())))
            cloneOK = false;
    }


/*
    if (! (node.getNodeName().equals(attributeNode.getNodeName()) &&         // Compares node names for equality
          (node.getNodeValue() != null && attributeNode.getNodeValue() != null)  // Checks to make sure each node has a value node
        ?  node.getNodeValue().equals(attributeNode.getNodeValue())          // If both have value nodes test those value nodes for equality
        : (node.getNodeValue() == null && attributeNode.getNodeValue() == null)))// If one node doesn't have a value node make sure both don't
*/
    if (cloneOK == false)
Ted Leung's avatar
Ted Leung committed
            printf("'cloneNode' did not clone the Attribute node correctly\n");
            OK = false;
        }
        // Deep clone test comparison is in testNode & testDocument

//************************************************* ERROR TESTS
    DTest tests;
//!! Throws HIERARCHY_REQUEST_ERR ****************
Ted Leung's avatar
Ted Leung committed
    //  doc.getDocumentElement().appendChild(attributeNode);

//!! Throws a NOT_FOUND_ERR ********
    //  attribute2 = doc.createAttribute("testAttribute2");
    //  doc.getDocumentElement().removeAttributeNode(attribute2);

//!! Throws an INUSE_ATTRIBUTE_ERR ******
    //  DOM_Element element = (DOM_Element &)doc.getLastChild().getLastChild();
    //  element.setAttributeNode(testAttribute );// Tests setNamedItem which generates error through justSetNamedItem.
Ted Leung's avatar
Ted Leung committed
// For debugging*****       printf("All DOM_Attr method calls worked correctly.\n");
    if (! OK)
        printf("\n*****The DOM_Attr method calls listed above failed, all others worked correctly.*****\n");
//  printf("");

};




/**
 * This method tests DOM_CDATASection methods for the XML DOM implementation
 * version 2.0 10/12/98
 * @param document org.w3c.dom.DOM_Document
 *
 */
void DTest::testCDATASection(DOM_Document document)
{
    DOM_Node node, node2;
    bool T = true;
    bool OK = true;
// For debugging*****   printf("\n          testCDATASection's outputs:\n");
    node = document.getDocumentElement().getElementsByTagName("dBodyLevel23").item(0).getFirstChild(); // node gets DOM_CDATASection node

    node2 = node.cloneNode(T);//*****?
    // Check nodes for equality, both their name and value or lack thereof
    if (! (node.getNodeName().equals(node2.getNodeName()) &&        // Compares node names for equality
          (node.getNodeValue() != null && node2.getNodeValue() != null)     // Checks to make sure each node has a value node
        ?  node.getNodeValue().equals(node2.getNodeValue())         // If both have value nodes test those value nodes for equality
        : (node.getNodeValue() == null && node2.getNodeValue() == null)))   // If one node doesn't have a value node make sure both don't
    {
        printf("'cloneNode' did not clone the DOM_CDATASection node correctly\n");
        OK = false;
    }
    // Deep clone test comparison is in testNode & testDocument
Ted Leung's avatar
Ted Leung committed
// For debugging*****   printf("All DOM_CDATASection method calls worked correctly.\n");
Ted Leung's avatar
Ted Leung committed
    if (! OK)
        printf("\n*****The DOM_CDATASection method calls listed above failed, all others worked correctly.*****\n");
//  printf("");
};



/**
 * This method tests DOM_CharacterData methods for the XML DOM implementation
 * version 2.0 10/12/98
 * @param document org.w3c.dom.DOM_Document
 *
 */
void DTest::testCharacterData(DOM_Document document)
{
    DOM_CharacterData charData;
    DOMString compareData, newData, resetData;
    bool OK = true;
// For debugging*****   printf("\n          testCharacterData's outputs:\n");
    DOM_Node abc1 = document.getDocumentElement().getElementsByTagName("dBodyLevel31").item(0).getFirstChild(); // charData gets textNode11
    charData = (DOM_CharacterData &) abc1;
Ted Leung's avatar
Ted Leung committed
    compareData = "dBodyLevel31'sChildTextNode11";
    if (!compareData.equals(charData.getData()))
    {
        printf("Warning!!! DOM_CharacterData's 'getData' failed to work properly!\n This may corrupt other DOM_CharacterData tests!!!*****\n");
        OK = false;
Ted Leung's avatar
Ted Leung committed
    resetData = charData.getData();
    //  printf("This node's original data is: " + charData.getData());

    newData = " This is new data for this node";
    compareData = charData.getData() + newData;
    charData.appendData(newData);
    if (!compareData.equals(charData.getData()))
    {
        printf("Warning!!! DOM_CharacterData's 'appendData' failed to work properly!\n");
        OK = false;
    }
    //  printf("This node's appended data is: " + charData.getData());

    compareData = "dBodyLevel";
    charData.deleteData(10, 100);
    if (!compareData.equals(charData.getData()))
    {
        printf("Warning!!! DOM_CharacterData's 'deleteData' failed to work properly!\n");
        OK = false;
    }
    //  printf("This node's partially deleted data is: " + charData.getData());

    unsigned int length = 10;
Ted Leung's avatar
Ted Leung committed
    if (!(length == charData.getLength()))
    {
        printf("Warning!!! DOM_CharacterData's 'getLength' failed to work properly!\n");
        OK = false;
    }
    //  printf("This node's data length is: " + charData.getLength());

    compareData = "dBody' This is data inserted into this node'Level";
    charData.insertData(5, "' This is data inserted into this node'");
    if (!compareData.equals(charData.getData()))
    {
        printf("Warning!!! DOM_CharacterData's 'insertData' failed to work properly!\n");
        OK = false;
    }
    //  printf("This node's updated with insert data is: " + charData.getData());

    compareData = "dBody' This is ' replacement data'ted into this node'Level";
    charData.replaceData(15, 10, "' replacement data'");
    if (!compareData.equals(charData.getData()))
    {
        printf("Warning!!! DOM_CharacterData's 'replaceData' failed to work properly!\n");
        OK = false;
    }
    //  printf("This node's updated with replacement data is: " +charData.getData());

    compareData = "New data A123456789B123456789C123456789D123456789E123456789";
    charData.setData("New data A123456789B123456789C123456789D123456789E123456789");
    if (!compareData.equals(charData.getData()))
    {
        printf("Warning!!! DOM_CharacterData's 'setData' failed to work properly!");
        OK = false;
    }
    //  printf("This node's new data via setData: " + charData.getData());

    compareData = "123456789D123456789E123456789";
    if (!compareData.equals(charData.substringData(30, 30)))
    {
        printf("Warning!!! DOM_CharacterData's 'substringData' failed to work properly!\n");
        OK = false;
    }
    //  printf("Using subString 30,30 you get:");  charData.substringData(30,30)).print();

    compareData = "New data A123456789B12345";
    if (!compareData.equals(charData.substringData(0, 25)))
    {
        printf("Warning!!! DOM_CharacterData's 'substringData' failed to work properly!\n");
        OK = false;
    }
    //  printf("Using subString 0,25 you get: ");   charData.substringData(0,25)).print();

//************************************************* ERROR TESTS
    DTest tests;   // What is this for?  'tests' is never used.

//!! Throws INDEX_SIZE_ERR ********************
    EXCEPTIONSTEST(charData.deleteData(-1, 5), DOM_DOMException::INDEX_SIZE_ERR, OK, 101 );
    // Test 102 is not an error because the -1 parameter is an unsigned value, and counts
    //   that exceed the length of the string are allowed.
//    EXCEPTIONSTEST(charData.deleteData(2, -1), DOM_DOMException::INDEX_SIZE_ERR, OK, 102 );
Ted Leung's avatar
Ted Leung committed
    EXCEPTIONSTEST(charData.deleteData(100, 5), DOM_DOMException::INDEX_SIZE_ERR, OK,103 );

//can't set negative unsigned int in c++ compiler
  //  EXCEPTIONSTEST(charData.insertData(-1, "Stuff inserted"), DOM_DOMException::INDEX_SIZE_ERR, OK, 104 );
Ted Leung's avatar
Ted Leung committed
    EXCEPTIONSTEST(charData.insertData(100,"Stuff inserted"), DOM_DOMException::INDEX_SIZE_ERR, OK, 105 );
  //  EXCEPTIONSTEST(charData.replaceData(-1, 5, "Replacement stuff") , DOM_DOMException::INDEX_SIZE_ERR, OK, 106 );
Ted Leung's avatar
Ted Leung committed
    EXCEPTIONSTEST(charData.replaceData(100, 5 ,"Replacement stuff"), DOM_DOMException::INDEX_SIZE_ERR, OK, 107 );
  //  EXCEPTIONSTEST(charData.replaceData(2, -1, "Replacement stuff"), DOM_DOMException::INDEX_SIZE_ERR,  OK, 108 );
Ted Leung's avatar
Ted Leung committed
    EXCEPTIONSTEST(charData.substringData(-1, 5), DOM_DOMException::INDEX_SIZE_ERR, OK, 109 );
    EXCEPTIONSTEST(charData.substringData(100, 5), DOM_DOMException::INDEX_SIZE_ERR, OK, 110 );
 //   EXCEPTIONSTEST(charData.substringData(2, -1), DOM_DOMException::INDEX_SIZE_ERR, OK, 111 );
Ted Leung's avatar
Ted Leung committed


//!! Throws NO_MODIFICATION_ALLOWED_ERR ********
// Since user cannot add children to Entity and EntityReference Nodes this test can't be done.
#if 0

    DOM_Node abc8 = document.getDocumentElement().getElementsByTagName("dBodyLevel24").
Ted Leung's avatar
Ted Leung committed
        item(0).getFirstChild().getChildNodes().item(0); // node gets ourEntityReference node's child text
    DOM_Text node = (DOM_Text &) abc8;
Ted Leung's avatar
Ted Leung committed

    EXCEPTIONSTEST(node.appendData("new data"), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 112 );
    EXCEPTIONSTEST(node.deleteData(5, 10), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 113 );
    EXCEPTIONSTEST(node.insertData(5, "Stuff inserted"), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 114 );
    EXCEPTIONSTEST(node.replaceData(5, 10, "Replacementstuff"), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 115 );
    EXCEPTIONSTEST(node.setData("New setdata stuff"), DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 116 );
Ted Leung's avatar
Ted Leung committed
// For debugging*****       printf("All DOM_CharacterData method calls worked correctly.\n");
    if (!OK)
        printf("\n*****The DOM_CharacterData method calls listed above failed, all others worked correctly.*****\n");
    charData.setData(resetData); // reset node to original data
//  printf(""\n);
};




Ted Leung's avatar
Ted Leung committed
 * This method tests ChildNodeList methods for the XML DOM implementation
 * version 2.0 10/12/98
 * @param document org.w3c.dom.DOM_Document
 *
 */
void DTest::testChildNodeList(DOM_Document document)
{
    DOM_Node node, node2;
    bool OK = true;
// For debugging*****   printf("\n          testChildNodeList's outputs:\n");
    node = document.getDocumentElement().getLastChild(); // node gets doc's testBody element
Ted Leung's avatar
Ted Leung committed
    if (!(node.getChildNodes().getLength()== 4))
        OK = false;
    node2 = node.getChildNodes().item(2);
    if (! node2.getNodeName().equals("dBodyLevel23"))
        OK = false;
Ted Leung's avatar
Ted Leung committed
// For debugging*****       printf("All ChildNodeList method calls worked correctly.\n");
    if (!OK)
        printf("\n*****The ChildNodeList method calls listed above failed, all others worked correctly.*****\n");
Ted Leung's avatar
Ted Leung committed
//  printf("");
};



/**
 * This method tests DOM_Comment methods for the XML DOM implementation
 * version 1.0 10/12/98