dtest.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,677 行 · 第 1/5 页

CPP
1,677
字号
         DOMTest::testCommentNode = 0;         DOMTest::testDocumentNode = 0;         DOMTest::testDocumentTypeNode = 0;         DOMTest::testDocumentFragmentNode = 0;         DOMTest::testNotationNode = 0;        // we couldn't really test the user data handler call as the userhandler is already        // deleted when the release() is done, but still set it to test the code internally        d->setUserData(tempStr, (void*) tempStr, &userhandler);        d->release();        // Test baseURI. BaseURI is set on nodes at parse time so we        // cannot use the docBuilder document above        //Setup parser        XercesDOMParser *parser = new XercesDOMParser;        parser->setValidationScheme(XercesDOMParser::Val_Never);        parser->setDoNamespaces(true);        parser->setDoSchema(true);        parser->setCreateEntityReferenceNodes(true);        OK = test.testBaseURI(parser);        parser->setCreateEntityReferenceNodes(false);        OK = test.testBaseURI(parser);        parser->setDoNamespaces(false);        parser->setDoSchema(false);        OK = test.testBaseURI(parser);        parser->setCreateEntityReferenceNodes(true);        OK = test.testBaseURI(parser);        delete parser;    };    XMLPlatformUtils::Terminate();    if (!OK) {        printf("Test Failed\n");        return 4;    }    printf("Test Run Successfully\n");    return 0;};/** * This method tests DOMAttr* methods for the XML DOM implementation * @param document org.w3c.dom.DOMDocument * */bool DOMTest::testAttr(DOMDocument* document){    DOMNode* node;    DOMAttr* attributeNode;    bool T = true;    bool F = false;    bool OK = true;// For debugging*****   printf("\n          testAttr's outputs:\n\n");    XMLString::transcode("testAttribute", tempStr, 3999);    DOMAttr* testAttribute = document->createAttribute(tempStr);    XMLString::transcode("testAttribute's value", tempStr, 3999);    testAttribute->setValue(tempStr);    node = document->getDocumentElement(); // node gets first element    // ((DOMElement*)node)->setAttributeNode(testAttribute);    // attributeNode = ((DOMElement*)node)->getAttributeNode("testAttribute");    DOMElement* el = (DOMElement*)node;    DOMNode* rem = el->setAttributeNode(testAttribute);    if (rem)        rem->release();    XMLString::transcode("testAttribute", tempStr, 3999);    attributeNode = el->getAttributeNode(tempStr);    //Test compareTreePosition, the equivalent case here    XMLString::transcode("dFirstElementdFirstElement", tempStr2, 3999);    DOMAttr* docFirstElementAttr = el->getAttributeNode(tempStr2);    COMPARETREEPOSITIONTEST(docFirstElementAttr, attributeNode, DOMNode::TREE_POSITION_EQUIVALENT, __LINE__);    // Test the name and other data    if (XMLString::compareString(tempStr, attributeNode->getName()))    {        fprintf(stderr, "Warning!!! DOMAttr's 'getName' method failed to work properly!\n");        OK = false;    }    XMLString::transcode("testAttribute's value", tempStr, 3999);    if (XMLString::compareString(tempStr, attributeNode->getNodeValue()))    {        fprintf(stderr, "Warning!!! DOMAttr's 'getNodeValue' method failed to work properly!\n");        OK = false;    }    if (! T ==attributeNode->getSpecified())    {        fprintf(stderr, "Warning!!! DOMAttr's 'getSpecified' method failed to work properly!\n");        OK = false;    }    if (XMLString::compareString(tempStr, attributeNode->getValue()))    {        fprintf(stderr, "Warning!!! DOMAttr's 'getValue' method failed to work properly!\n");        OK = false;    }    XMLString::transcode("Reset Value", tempStr, 3999);    attributeNode->setNodeValue(tempStr);   /// LEAK!!!!!    if (XMLString::compareString(tempStr, attributeNode->getNodeValue()))    {        fprintf(stderr, "Warning!!! DOMAttr's 'setNodeValue' method failed to work properly!\n");        OK = false;    }    attributeNode->setValue(XMLUni::fgZeroLenString);    if (XMLString::compareString(XMLUni::fgZeroLenString, attributeNode->getValue()))    {        fprintf(stderr, "Warning!!! DOMAttr's 'setValue' to '0' method failed to work properly!\n");        OK = false;    }    XMLString::transcode("Another value ", tempStr, 3999);    attributeNode->setValue(tempStr);    if (XMLString::compareString(tempStr, attributeNode->getValue()))    {        fprintf(stderr, "Warning!!! DOMAttr's 'setValue' method failed to work properly!");        OK = false;    }    node = attributeNode->cloneNode(T);    // Check nodes for equality, both their name and value or lack thereof    bool cloneOK = true;    if (XMLString::compareString(node->getNodeName(), attributeNode->getNodeName()))        cloneOK = false;    if (node->getNodeValue() == 0 &&        attributeNode->getNodeValue() != 0)    {        cloneOK = false;    }    if (node->getNodeValue() != 0 && attributeNode->getNodeValue() == 0)    {        cloneOK = false;    };    if (node->getNodeValue() != 0 && attributeNode->getNodeValue() != 0)    {        if (XMLString::compareString(node->getNodeValue(),attributeNode->getNodeValue()))            cloneOK = false;    }/*    if (! (node->getNodeName(), attributeNode->getNodeName()) &&         // Compares node names for equality          (node->getNodeValue() != 0 && attributeNode->getNodeValue() != 0)  // Checks to make sure each node has a value node        ?  node->getNodeValue(), attributeNode->getNodeValue())          // If both have value nodes test those value nodes for equality        : (node->getNodeValue() == 0 && attributeNode->getNodeValue() == 0)))// If one node doesn't have a value node make sure both don't*/    if (cloneOK == false)        {            fprintf(stderr, "'cloneNode' did not clone the Attribute node correctly\n");            OK = false;        }        // Deep clone test comparison is in testNode & testDocument//************************************************* ERROR TESTS    DOMTest tests;//!! Throws HIERARCHY_REQUEST_ERR ****************    //  doc->getDocumentElement()->appendChild(attributeNode);//!! Throws a NOT_FOUND_ERR ********    //  attribute2 = doc->createAttribute("testAttribute2");    //  doc->getDocumentElement()->removeAttributeNode(attribute2);//!! Throws an INUSE_ATTRIBUTE_ERR ******    //  DOMElement* element = (DOMElement*)doc->getLastChild()->getLastChild();    //  element->setAttributeNode(testAttribute );// Tests setNamedItem which generates error through justSetNamedItem.    // Test the user data    // Test simple set and get    DOMAttr* userTest = testAttribute;    DOMElement*  userFirst = el;    XMLCh* userSecond = tempStr2;    XMLString::transcode("first", tempStr, 3999);    XMLString::transcode("document", tempStr2, 3999);    userTest->setUserData(tempStr2, (void*) document, 0);    void* mydocument = userTest->getUserData(tempStr2);    if (document != mydocument) {        fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);        OK = false;    }    userTest->setUserData(tempStr, (void*) userFirst, 0);    void* myFirst = userTest->getUserData(tempStr);    if (userFirst != myFirst) {        fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);        OK = false;    }    // Test overwrite    void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);    void* mySecond = userTest->getUserData(tempStr);    if (userSecond != mySecond) {        fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);        OK = false;    }    if (userFirst != myFirst2) {        fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);        OK = false;    }    if (userFirst == mySecond) {        fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);        OK = false;    }    // Test null    // test non-exist key    XMLString::transcode("not-exist", tempStr3, 3999);    if (userTest->getUserData(tempStr3)) {        fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);        OK = false;    }    // use a node that does not have user data set before    if (userFirst->getUserData(tempStr)) {        fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);        OK = false;    }    // Test reset    void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);    void* myNull = userTest->getUserData(tempStr);    if (userSecond != mySecond2) {        fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);        OK = false;    }    if (myNull) {        fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);        OK = false;    }    void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);    void* myNull2 = userTest->getUserData(tempStr2);    if (mydocument != mydocument2) {        fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);        OK = false;    }    if (myNull2) {        fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);        OK = false;    }    //the userTest user data table should be null now    if (userTest->getUserData(tempStr)) {        fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);        OK = false;    }    if (userTest->getUserData(tempStr2)) {        fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);        OK = false;    }    // Test DOMUserDataHandler    // test clone    userTest->setUserData(tempStr2, (void*) document, &userhandler);    DOMNode* mycloned = userTest->cloneNode(true);    USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);    // test import    document->setUserData(tempStr2, (void*) document, &userhandler);    DOMNode* myimport = document->importNode(userTest,true);    USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);    // test delete    myimport->setUserData(tempStr2, (void*) userTest, &userhandler);    myimport->release();    USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);    // Test isSameNode    if (!userTest->isSameNode(userTest)) {        fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);        OK = false;    }    if (userTest->isSameNode(userFirst)) {        fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);        OK = false;    }    // Test isEqualNode    if (!userTest->isEqualNode(mycloned)) {        fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);        OK = false;    }    if (!userTest->isEqualNode(userTest)) {        fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);        OK = false;    }    if (userTest->isEqualNode(userFirst)) {        fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);        OK = false;    }    // Test renameNode    XMLString::transcode("http://nsa", tempStr4, 3999);    XMLString::transcode("aa", tempStr5, 3999);    XMLString::transcode("pnsa:aa", tempStr3, 3999);    // create the attribute    DOMAttr* renameTestAttribute = document->createAttribute(tempStr5);    DOMAttr* renameTestAttributeNS = document->createAttributeNS(tempStr4, tempStr3);    // create the owner element and append the attribute node    DOMElement* renameTestElement = document->createElement(tempStr5);    renameTestElement->setAttributeNode(renameTestAttribute);    renameTestElement->setAttributeNode(renameTestAttributeNS);    // set up the userdata    renameTestAttribute->setUserData(tempStr5, (void*) document, &userhandler);    renameTestAttributeNS->setUserData(tempStr4, (void*) document, 0);    // set up the node value    renameTestAttribute->setNodeValue(tempStr5);    renameTestAttributeNS->setNodeValue(tempStr4);    XMLString::transcode("http://nsb", tempStr, 3999);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?