dommemtest.cpp

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

CPP
1,486
字号
    //    //  importNode quick test    //    {        DOMDocument*   doc1 = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMDocument*   doc2 = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   el1  = doc1->createElement(X("abc"));        doc1->appendChild(el1);        TASSERT(el1->getParentNode() != 0);        DOMNode*        el2  = doc2->importNode(el1, true);        TASSERT(el2->getParentNode() == 0);        const XMLCh*       tagName = el2->getNodeName();        TASSERT(!XMLString::compareString(tagName, X("abc")));        TASSERT(el2->getOwnerDocument() == doc2);        TASSERT(doc1 != doc2);        doc1->release();        doc2->release();    }    //    //  getLength() tests.  Both Node CharacterData and NodeList implement    //                  getLength().  Early versions of the DOM had a clash    //                  between the two, originating in the implementation class    //                  hirearchy, which has NodeList as a (distant) base class    //                  of CharacterData.  This is a regression test to verify    //                  that the problem stays fixed.    //    {        DOMDocument*    doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMText*          tx = doc->createTextNode(X("Hello"));        DOMElement*     el = doc->createElement(X("abc"));        el->appendChild(tx);        int     textLength = tx->getLength();        TASSERT(textLength == 5);        DOMNodeList*      nl = tx->getChildNodes();        int      nodeListLen = nl->getLength();        TASSERT(nodeListLen == 0);        nl = el->getChildNodes();        nodeListLen = nl->getLength();        TASSERT(nodeListLen == 1);        doc->release();    }    //    //  NodeList - comparison operators, basic operation.    //    {        DOMNodeList*    nl = 0;        DOMNodeList*    nl2 = 0;        TASSERT(nl == 0);        TASSERT(!(nl != 0));        TASSERT(nl == nl2);        DOMDocument*       doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        nl = doc->getChildNodes();    // Should be non-null, but empty        TASSERT(nl != 0);        int len = nl->getLength();        TASSERT(len == 0);        DOMElement* el = doc->createElement(X("NodeList01"));        doc->appendChild(el);        len = nl->getLength();        TASSERT(len == 1);        TASSERT(nl != nl2);        nl2 = nl;        TASSERT(nl == nl2);        doc->release();    }    //    //  Name validity checking.    //    {         DOMDocument*       doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();         try         {             DOMElement* el = doc->createElement(X("!@@ bad element name"));             TASSERT(false);  // Exception above should prevent us reaching here.         }         catch ( DOMException e)         {             TASSERT(e.code == DOMException::INVALID_CHARACTER_ERR);         }         catch (...)         {             TASSERT(false);  // Wrong exception thrown.         }         doc->release();    }    //    //  Assignment ops return value    //    {        DOMDocument*       doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement* el = doc->createElement(X("NodeList01"));        doc->appendChild(el);        DOMElement* n1, *n2, *n3;        n1 = n2 = n3 = el;        TASSERT(n1 == n2);        TASSERT(n1 == n3);        TASSERT(n1 == el);        TASSERT(n1 != 0);        n1 = n2 = n3 = 0;        TASSERT(n1 == 0);        doc->release();    }    //    //  Cloning of a node with attributes. Regression test for a ref counting    //  bug in attributes of cloned nodes that occured when the "owned" flag    //  was not set in the clone.    //    {        DOMDocument*   doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMElement*   root = doc->createElement(X("CTestRoot"));        root->setAttribute(X("CTestAttr"), X("CTestAttrValue"));        const XMLCh* s = root->getAttribute(X("CTestAttr"));        TASSERT(!XMLString::compareString(s, X("CTestAttrValue")));        DOMNode* abc2 = root->cloneNode(true);        DOMElement*   cloned = (DOMElement*) abc2;        DOMAttr* a = cloned->getAttributeNode(X("CTestAttr"));        TASSERT(a != 0);        s = a->getValue();        TASSERT(!XMLString::compareString(s, X("CTestAttrValue")));        a = 0;        a = cloned->getAttributeNode(X("CTestAttr"));        TASSERT(a != 0);        s = a->getValue();        TASSERT(!XMLString::compareString(s, X("CTestAttrValue")));        doc->release();    }    //    //  splitText()    //     Regression test for a bug from Tinny Ng    //    {        DOMDocument* doc;        doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        DOMText* tn, *tn1, *tn2;        tn = doc->createTextNode (X("0123456789"));        tn1 = tn->splitText(5);        TASSERT(!XMLString::compareString(tn->getNodeValue(), X("01234")));        TASSERT(!XMLString::compareString(tn1->getNodeValue(), X("56789")));        tn2 = tn->splitText(5);        TASSERT(!XMLString::compareString(tn->getNodeValue(), X("01234")));        TASSERT(!XMLString::compareString(tn2->getNodeValue(), XMLUni::fgZeroLenString));        EXCEPTION_TEST(tn->splitText(6), DOMException::INDEX_SIZE_ERR);        doc->release();    }}//---------------------------------------------------------------------------------------////   DOMNSTests    DOM Name Space tests////---------------------------------------------------------------------------------------void DOMNSTests(){    //    //  DOM Level 2 tests.  These should be split out as a separate test.    //    //    // hasFeature.  The set of supported options tested here is for Xerces 1.1    //              Note: because the implementation lazily creates some of the comprison    //                    strings within the implementation, this test must be pre-flighted    //                    outside of the TESPROLOG/ macros to avoid spurious    //                    reports of memory leaks.    //    // Also test the case-insensitive    //    {        DOMImplementation*  impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));        TASSERT(impl->hasFeature(X("XmL"), X("2.0")) == true);        TASSERT(impl->hasFeature(X("xML"), 0) == true);        TASSERT(impl->hasFeature(X("xML"), XMLUni::fgZeroLenString) == true);        TASSERT(impl->hasFeature(X("XMl"), X("1.0"))  == true);        TASSERT(impl->hasFeature(X("xMl"), X("3.0"))  == false);        TASSERT(impl->hasFeature(X("TrAveRsal"), 0)  == true);        TASSERT(impl->hasFeature(X("TrAveRsal"), XMLUni::fgZeroLenString)  == true);    }    {        DOMImplementation*  impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));        TASSERT(impl->hasFeature(X("XmL"), 0)   == true);        TASSERT(impl->hasFeature(X("XmL"), X("1.0"))    == true);        TASSERT(impl->hasFeature(X("XmL"), X("2.0"))   == true);        TASSERT(impl->hasFeature(X("XmL"), X("3.0"))   == false);        TASSERT(impl->hasFeature(X("Core"), 0)   == true);        TASSERT(impl->hasFeature(X("coRe"), X("1.0"))    == true);        TASSERT(impl->hasFeature(X("core"), X("2.0"))   == true);        TASSERT(impl->hasFeature(X("cORe"), X("3.0"))   == true);        TASSERT(impl->hasFeature(X("cORe"), X("4.0"))   == false);        TASSERT(impl->hasFeature(X("Traversal"), XMLUni::fgZeroLenString)   == true);        TASSERT(impl->hasFeature(X("traversal"), X("1.0"))    == false);        TASSERT(impl->hasFeature(X("TraVersal"), X("2.0"))   == true);        TASSERT(impl->hasFeature(X("Range"), 0)   == true);        TASSERT(impl->hasFeature(X("raNge"), X("1.0"))    == false);        TASSERT(impl->hasFeature(X("RaNge"), X("2.0"))   == true);        TASSERT(impl->hasFeature(X("HTML"), 0)   == false);        TASSERT(impl->hasFeature(X("Views"), XMLUni::fgZeroLenString)   == false);        TASSERT(impl->hasFeature(X("StyleSheets"), 0)   == false);        TASSERT(impl->hasFeature(X("CSS"), XMLUni::fgZeroLenString)   == false);        TASSERT(impl->hasFeature(X("CSS2"), 0)   == false);        TASSERT(impl->hasFeature(X("Events"), 0)   == false);        TASSERT(impl->hasFeature(X("UIEvents"), 0)   == false);        TASSERT(impl->hasFeature(X("MouseEvents"), 0)   == false);        TASSERT(impl->hasFeature(X("MutationEvents"), 0)   == false);        TASSERT(impl->hasFeature(X("HTMLEvents"), 0)   == false);    }    //    // isSupported test (similar to hasFeature)    //    {        DOMDocument* doc;        doc = DOMImplementationRegistry::getDOMImplementation(X("Core"))->createDocument();        TASSERT(doc->isSupported(X("XmL"), 0)   == true);        TASSERT(doc->isSupported(X("XmL"), X("1.0"))    == true);        TASSERT(doc->isSupported(X("XmL"), X("2.0"))   == true);        TASSERT(doc->isSupported(X("XmL"), X("3.0"))   == false);        TASSERT(doc->isSupported(X("Core"), 0)   == true);        TASSERT(doc->isSupported(X("Core"), XMLUni::fgZeroLenString)   == true);        TASSERT(doc->isSupported(X("coRe"), X("1.0"))    == true);        TASSERT(doc->isSupported(X("core"), X("2.0"))   == true);        TASSERT(doc->isSupported(X("cORe"), X("3.0"))   == true);        TASSERT(doc->isSupported(X("cORe"), X("4.0"))   == false);        TASSERT(doc->isSupported(X("Traversal"), 0)   == true);        TASSERT(doc->isSupported(X("traversal"), X("1.0"))    == false);        TASSERT(doc->isSupported(X("TraVersal"), X("2.0"))   == true);        TASSERT(doc->isSupported(X("Range"), XMLUni::fgZeroLenString)   == true);        TASSERT(doc->isSupported(X("raNge"), X("1.0"))    == false);        TASSERT(doc->isSupported(X("RaNge"), X("2.0"))   == true);        TASSERT(doc->isSupported(X("HTML"), 0)   == false);        TASSERT(doc->isSupported(X("Views"), 0)   == false);        TASSERT(doc->isSupported(X("StyleSheets"), 0)   == false);        TASSERT(doc->isSupported(X("CSS"), 0)   == false);        TASSERT(doc->isSupported(X("CSS2"), XMLUni::fgZeroLenString)   == false);        TASSERT(doc->isSupported(X("Events"), 0)   == false);        TASSERT(doc->isSupported(X("UIEvents"), 0)   == false);        TASSERT(doc->isSupported(X("MouseEvents"), 0)   == false);        TASSERT(doc->isSupported(X("MutationEvents"), XMLUni::fgZeroLenString)   == false);        TASSERT(doc->isSupported(X("HTMLEvents"), 0)   == false);        doc->release();    }    //    // CreateDocumentType    //    {        DOMImplementation*  impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));        DOMDocumentType* dt = impl->createDocumentType(X("foo:docName"), X("pubId"), X("http://sysId"));        TASSERT(dt != 0);        TASSERT(dt->getNodeType() == DOMNode::DOCUMENT_TYPE_NODE);        TASSERT(!XMLString::compareString(dt->getNodeName(), X("foo:docName")));

⌨️ 快捷键说明

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