📄 elementtest.cpp
字号:
//// ElementTest.cpp//// $Id: //poco/1.2/XML/testsuite/src/ElementTest.cpp#1 $//// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.// and Contributors.//// Permission is hereby granted, free of charge, to any person or organization// obtaining a copy of the software and accompanying documentation covered by// this license (the "Software") to use, reproduce, display, distribute,// execute, and transmit the Software, and to prepare derivative works of the// Software, and to permit third-parties to whom the Software is furnished to// do so, all subject to the following:// // The copyright notices in the Software and this entire statement, including// the above license grant, this restriction and the following disclaimer,// must be included in all copies of the Software, in whole or in part, and// all derivative works of the Software, unless such copies or derivative// works are solely in the form of machine-executable object code generated by// a source language processor.// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER// DEALINGS IN THE SOFTWARE.//#include "ElementTest.h"#include "CppUnit/TestCaller.h"#include "CppUnit/TestSuite.h"#include "Poco/DOM/Document.h"#include "Poco/DOM/Element.h"#include "Poco/DOM/Attr.h"#include "Poco/DOM/Text.h"#include "Poco/DOM/NamedNodeMap.h"#include "Poco/DOM/NodeList.h"#include "Poco/DOM/AutoPtr.h"using Poco::XML::Element;using Poco::XML::Document;using Poco::XML::Attr;using Poco::XML::Text;using Poco::XML::Node;using Poco::XML::NamedNodeMap;using Poco::XML::NodeList;using Poco::XML::AutoPtr;using Poco::XML::XMLString;ElementTest::ElementTest(const std::string& name): CppUnit::TestCase(name){}ElementTest::~ElementTest(){}void ElementTest::testAttributes(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pElem = pDoc->createElement("elem"); assert (!pElem->hasAttributes()); pElem->setAttribute("a1", "v1"); assert (pElem->hasAttributes()); assert (pElem->hasAttribute("a1")); assert (pElem->getAttribute("a1") == "v1"); Attr* pAttr1 = pElem->getAttributeNode("a1"); assert (pAttr1 != 0); assert (pAttr1->name() == "a1"); assert (pAttr1->nodeName() == "a1"); assert (pAttr1->value() == "v1"); assert (pAttr1->nodeValue() == "v1"); assert (pAttr1->ownerElement() == pElem); assert (pAttr1->ownerDocument() == pDoc); assert (pAttr1->innerText() == "v1"); assert (pAttr1->previousSibling() == 0); assert (pAttr1->nextSibling() == 0); pAttr1->setValue("V1"); assert (pElem->getAttribute("a1") == "V1"); pElem->setAttribute("a2", "v2"); assert (pElem->hasAttribute("a1")); assert (pElem->getAttribute("a1") == "V1"); assert (pElem->hasAttribute("a2")); assert (pElem->getAttribute("a2") == "v2"); Attr* pAttr2 = pElem->getAttributeNode("a2"); assert (pAttr2 != 0); assert (pAttr2->name() == "a2"); assert (pAttr2->value() == "v2"); assert (pAttr2->ownerElement() == pElem); assert (pAttr1->previousSibling() == 0); assert (pAttr1->nextSibling() == pAttr2); assert (pAttr2->previousSibling() == pAttr1); assert (pAttr2->nextSibling() == 0); Attr* pAttr3 = pElem->getAttributeNode("a3"); assert (pAttr3 == 0); pAttr3 = pDoc->createAttribute("a3"); pAttr3->setValue("v3"); pElem->setAttributeNode(pAttr3); pAttr3->release(); assert (pElem->hasAttribute("a1")); assert (pElem->getAttribute("a1") == "V1"); assert (pElem->hasAttribute("a2")); assert (pElem->getAttribute("a2") == "v2"); assert (pElem->hasAttribute("a3")); assert (pElem->getAttribute("a3") == "v3"); assert (pAttr1->previousSibling() == 0); assert (pAttr1->nextSibling() == pAttr2); assert (pAttr2->previousSibling() == pAttr1); assert (pAttr2->nextSibling() == pAttr3); assert (pAttr3->previousSibling() == pAttr2); assert (pAttr3->nextSibling() == 0); pAttr2 = pDoc->createAttribute("a2"); pAttr2->setValue("V2"); pElem->setAttributeNode(pAttr2); pAttr2->release(); assert (pElem->hasAttribute("a1")); assert (pElem->getAttribute("a1") == "V1"); assert (pElem->hasAttribute("a2")); assert (pElem->getAttribute("a2") == "V2"); assert (pElem->hasAttribute("a3")); assert (pElem->getAttribute("a3") == "v3"); pAttr1 = pDoc->createAttribute("a1"); pAttr1->setValue("v1"); pElem->setAttributeNode(pAttr1); pAttr1->release(); assert (pElem->hasAttribute("a1")); assert (pElem->getAttribute("a1") == "v1"); assert (pElem->hasAttribute("a2")); assert (pElem->getAttribute("a2") == "V2"); assert (pElem->hasAttribute("a3")); assert (pElem->getAttribute("a3") == "v3"); pAttr3 = pDoc->createAttribute("a3"); pAttr3->setValue("V3"); pElem->setAttributeNode(pAttr3); pAttr3->release(); assert (pElem->hasAttribute("a1")); assert (pElem->getAttribute("a1") == "v1"); assert (pElem->hasAttribute("a2")); assert (pElem->getAttribute("a2") == "V2"); assert (pElem->hasAttribute("a3")); assert (pElem->getAttribute("a3") == "V3"); pElem->removeAttributeNode(pAttr3); assert (!pElem->hasAttribute("a3")); pElem->removeAttribute("a1"); assert (!pElem->hasAttribute("a1")); pElem->removeAttribute("a2"); assert (!pElem->hasAttribute("a2")); assert (!pElem->hasAttributes());}void ElementTest::testAttributesNS(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pElem = pDoc->createElementNS("urn:ns1", "p:elem"); assert (pElem->namespaceURI() == "urn:ns1"); assert (pElem->prefix() == "p"); assert (pElem->tagName() == "p:elem"); assert (pElem->localName() == "elem"); assert (!pElem->hasAttributes()); pElem->setAttributeNS("urn:ns1", "a1", "v1"); assert (pElem->hasAttributes()); assert (pElem->hasAttributeNS("urn:ns1", "a1")); assert (pElem->getAttributeNS("urn:ns1", "a1") == "v1"); Attr* pAttr1 = pElem->getAttributeNodeNS("urn:ns1", "a1"); assert (pAttr1 != 0); assert (pAttr1->name() == "a1"); assert (pAttr1->namespaceURI() == "urn:ns1"); assert (pAttr1->prefix().empty()); assert (pAttr1->localName() == "a1"); assert (pAttr1->nodeName() == "a1"); assert (pAttr1->value() == "v1"); assert (pAttr1->nodeValue() == "v1"); assert (pAttr1->ownerElement() == pElem); pAttr1->setValue("V1"); assert (pElem->getAttributeNS("urn:ns1", "a1") == "V1"); pElem->setAttributeNS("urn:ns1", "a2", "v2"); assert (pElem->hasAttributeNS("urn:ns1", "a1")); assert (pElem->getAttributeNS("urn:ns1", "a1") == "V1"); assert (pElem->hasAttributeNS("urn:ns1", "a2")); assert (pElem->getAttributeNS("urn:ns1", "a2") == "v2"); Attr* pAttr2 = pElem->getAttributeNodeNS("urn:ns1", "a2"); assert (pAttr2 != 0); assert (pAttr2->name() == "a2"); assert (pAttr2->namespaceURI() == "urn:ns1"); assert (pAttr2->prefix().empty()); assert (pAttr2->localName() == "a2"); assert (pAttr2->value() == "v2"); assert (pAttr2->ownerElement() == pElem); Attr* pAttr3 = pElem->getAttributeNodeNS("urn:ns2", "p:a3"); assert (pAttr3 == 0); pAttr3 = pDoc->createAttributeNS("urn:ns2", "p:a3"); pAttr3->setValue("v3"); pElem->setAttributeNodeNS(pAttr3); pAttr3->release(); assert (pElem->hasAttributeNS("urn:ns1", "a1")); assert (pElem->getAttributeNS("urn:ns1", "a1") == "V1"); assert (pElem->hasAttributeNS("urn:ns1", "a2")); assert (pElem->getAttributeNS("urn:ns1", "a2") == "v2"); assert (pElem->hasAttributeNS("urn:ns2", "a3")); assert (pElem->getAttributeNS("urn:ns2", "a3") == "v3"); pAttr2 = pDoc->createAttributeNS("urn:ns1", "a2"); pAttr2->setValue("V2"); pElem->setAttributeNodeNS(pAttr2); pAttr2->release(); assert (pElem->hasAttributeNS("urn:ns1", "a1")); assert (pElem->getAttributeNS("urn:ns1", "a1") == "V1"); assert (pElem->hasAttributeNS("urn:ns1", "a2")); assert (pElem->getAttributeNS("urn:ns1", "a2") == "V2"); assert (pElem->hasAttributeNS("urn:ns2", "a3")); assert (pElem->getAttributeNS("urn:ns2", "a3") == "v3"); pAttr1 = pDoc->createAttributeNS("urn:ns1", "a1"); pAttr1->setValue("v1"); pElem->setAttributeNodeNS(pAttr1); pAttr1->release(); assert (pElem->hasAttributeNS("urn:ns1", "a1")); assert (pElem->getAttributeNS("urn:ns1", "a1") == "v1"); assert (pElem->hasAttributeNS("urn:ns1", "a2")); assert (pElem->getAttributeNS("urn:ns1", "a2") == "V2"); assert (pElem->hasAttributeNS("urn:ns2", "a3")); assert (pElem->getAttributeNS("urn:ns2", "a3") == "v3"); pAttr3 = pDoc->createAttributeNS("urn:ns2", "q:a3"); pAttr3->setValue("V3"); pElem->setAttributeNodeNS(pAttr3); pAttr3->release(); assert (pElem->hasAttributeNS("urn:ns1", "a1")); assert (pElem->getAttributeNS("urn:ns1", "a1") == "v1"); assert (pElem->hasAttributeNS("urn:ns1", "a2")); assert (pElem->getAttributeNS("urn:ns1", "a2") == "V2"); assert (pElem->hasAttributeNS("urn:ns2", "a3")); assert (pElem->getAttributeNS("urn:ns2", "a3") == "V3"); pElem->removeAttributeNode(pAttr3); assert (!pElem->hasAttributeNS("urn:ns2", "a3")); pElem->removeAttributeNS("urn:ns1", "a1"); assert (!pElem->hasAttributeNS("urn:ns1", "a1")); pElem->removeAttributeNS("urn:ns1", "a2"); assert (!pElem->hasAttributeNS("urn:ns1", "a2")); assert (!pElem->hasAttributes());}void ElementTest::testAttrMap(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pElem = pDoc->createElement("elem"); AutoPtr<NamedNodeMap> pNNM = pElem->attributes(); assert (pNNM->length() == 0); pElem->setAttribute("a1", "v1"); assert (pNNM->length() == 1); assert (pNNM->item(0)->nodeName() == "a1"); assert (pNNM->getNamedItem("a1")->nodeName() == "a1"); pElem->setAttribute("a2", "v2"); assert (pNNM->length() == 2); assert (pNNM->item(0)->nodeName() == "a1"); assert (pNNM->getNamedItem("a1")->nodeName() == "a1"); assert (pNNM->item(1)->nodeName() == "a2"); assert (pNNM->getNamedItem("a2")->nodeName() == "a2"); Attr* pAttr = pDoc->createAttribute("a3"); pNNM->setNamedItem(pAttr); pAttr->release(); assert (pNNM->length() == 3); assert (pNNM->item(0)->nodeName() == "a1"); assert (pNNM->getNamedItem("a1")->nodeName() == "a1"); assert (pNNM->item(1)->nodeName() == "a2"); assert (pNNM->getNamedItem("a2")->nodeName() == "a2"); assert (pNNM->item(2)->nodeName() == "a3"); assert (pNNM->getNamedItem("a3")->nodeName() == "a3"); pNNM->removeNamedItem("a2"); assert (pNNM->length() == 2); assert (!pElem->hasAttribute("a2")); pNNM->removeNamedItem("a3");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -