unoxml: port testXNamedNodeMap_AttributesMap from java to c++

Change-Id: I1a37c7a63622d38cd8d9148589ba999901453611
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174612
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
This commit is contained in:
Xisco Fauli 2024-10-07 17:09:11 +02:00
parent 388ec7f609
commit 9526206b54
2 changed files with 61 additions and 68 deletions

View file

@ -2267,74 +2267,6 @@ public class DOMTest
assertEquals("ElementList.item", xFoo3, xElementList.item(0));
}
@Test public void testXNamedNodeMap_AttributesMap() throws Exception
{
XDocumentBuilder xBuilder =
UnoRuntime.queryInterface(XDocumentBuilder.class,
m_xMSF.createInstance("com.sun.star.xml.dom.DocumentBuilder"));
XDocument xDoc = xBuilder.newDocument();
String ns = "http://example.com/";
XElement xElem = xDoc.createElement("foo");
XNamedNodeMap xAttributes = xElem.getAttributes();
assertNotNull(xAttributes);
assertSame("AttributesMap.getLength()", 0, xAttributes.getLength());
try {
xAttributes.item(4);
} catch (Exception e) { /* expected */ }
xElem.setAttribute("bar", "42");
XAttr xAttrBar = xElem.getAttributeNode("bar");
assertSame("AttributesMap.getLength()", 1, xAttributes.getLength());
assertEquals("AttributesMap.item", xAttrBar, xAttributes.item(0));
assertEquals("AttributesMap.getNamedItem",
xAttrBar, xAttributes.getNamedItem("bar"));
xElem.setAttributeNS(ns, "n:bar", "43");
XAttr xAttrBarNs = xElem.getAttributeNodeNS(ns, "bar");
assertSame("AttributesMap.getLength()", 2, xAttributes.getLength());
assertEquals("AttributesMap.item", xAttrBar, xAttributes.item(0));
assertEquals("AttributesMap.item", xAttrBarNs, xAttributes.item(1));
assertEquals("AttributesMap.getNamedItem",
xAttrBar, xAttributes.getNamedItem("bar"));
assertEquals("AttributesMap.getNamedItemNS",
xAttrBarNs, xAttributes.getNamedItemNS(ns, "bar"));
XNode xAttrBarNsRem = xAttributes.removeNamedItemNS(ns, "bar");
assertSame("AttributesMap.getLength()", 1, xAttributes.getLength());
assertEquals("AttributesMap.removeNamedItemNS",
xAttrBar, xAttributes.item(0));
assertEquals("AttributesMap.removeNamedItemNS",
xAttrBar, xAttributes.getNamedItem("bar"));
assertNull("AttributesMap.removeNamedItemNS",
xAttrBarNsRem.getParentNode());
XNode xAttrBarRem = xAttributes.removeNamedItem("bar");
assertSame("AttributesMap.getLength()", 0, xAttributes.getLength());
assertNull("AttributesMap.removeNamedItem",
xAttrBarRem.getParentNode());
XNode xAttrBarSetN = xAttributes.setNamedItem(xAttrBarRem);
assertNotNull("AttributesMap.setNamedItem", xAttrBarSetN);
XAttr xAttrBarSet =
UnoRuntime.queryInterface(XAttr.class, xAttrBarSetN);
assertNotNull("AttributesMap.setNamedItem", xAttrBarSet);
assertEquals("AttributesMap.setNamedItem",
xAttrBarSet, xAttributes.getNamedItem("bar"));
XNode xAttrBarNsSetN = xAttributes.setNamedItemNS(xAttrBarNsRem);
assertNotNull("AttributesMap.setNamedItemNS", xAttrBarNsSetN);
XAttr xAttrBarNsSet =
UnoRuntime.queryInterface(XAttr.class, xAttrBarNsSetN);
assertNotNull("AttributesMap.setNamedItemNS", xAttrBarNsSet);
assertEquals("AttributesMap.setNamedItemNS",
xAttrBarNsSet, xAttributes.getNamedItemNS(ns, "bar"));
assertSame("AttributesMap.getLength()", 2, xAttributes.getLength());
}
@Test public void testXSAXSerialize() throws Exception
{
String file =

View file

@ -560,6 +560,65 @@ public:
}
}
void testXNamedNodeMap_AttributesMap()
{
Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
CPPUNIT_ASSERT(xDocument);
Reference< xml::dom::XElement > xElem = xDocument->createElement(u"foo"_ustr);
Reference< xml::dom::XNamedNodeMap > xAttributes = xElem->getAttributes();
CPPUNIT_ASSERT(xAttributes);
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xAttributes->getLength());
// Should it throw an exception ?
CPPUNIT_ASSERT(!xAttributes->item(4));
xElem->setAttribute(u"bar"_ustr, u"42"_ustr);
Reference< xml::dom::XAttr > xAttBar = xElem->getAttributeNode(u"bar"_ustr);
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xAttributes->getLength());
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->item(0), uno::UNO_QUERY));
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
OUString aNS(u"http://example.com/"_ustr);
xElem->setAttributeNS(aNS, u"n:bar"_ustr, u"43"_ustr);
Reference< xml::dom::XAttr > xAttBarNS = xElem->getAttributeNodeNS(aNS, u"bar"_ustr);
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xAttributes->getLength());
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->item(0), uno::UNO_QUERY));
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBarNS, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->item(1), uno::UNO_QUERY));
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBarNS, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->getNamedItemNS(aNS, u"bar"_ustr), uno::UNO_QUERY));
uno::Reference<xml::dom::XNode> xAttrBarNsRem = xAttributes->removeNamedItemNS(aNS, u"bar"_ustr);
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xAttributes->getLength());
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->item(0), uno::UNO_QUERY));
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
CPPUNIT_ASSERT(!xAttrBarNsRem->getParentNode());
uno::Reference<xml::dom::XNode> xAttrBarRem = xAttributes->removeNamedItem(u"bar"_ustr);
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xAttributes->getLength());
CPPUNIT_ASSERT(!xAttrBarRem->getParentNode());
uno::Reference<xml::dom::XNode> xAttrBarSet = xAttributes->setNamedItem(xAttrBarRem);
CPPUNIT_ASSERT(xAttrBarSet);
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttrBarSet, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
uno::Reference<xml::dom::XNode> xAttrBarNsSet = xAttributes->setNamedItemNS(xAttrBarNsRem);
CPPUNIT_ASSERT(xAttrBarNsSet);
CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttrBarNsSet, uno::UNO_QUERY),
Reference< XInterface >(xAttributes->getNamedItemNS(aNS, u"bar"_ustr), uno::UNO_QUERY));
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xAttributes->getLength());
}
void testXNodeList_ChildList()
{
Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
@ -576,6 +635,7 @@ public:
CPPUNIT_ASSERT(xChildList);
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xChildList->getLength());
// Should it throw an exception ?
CPPUNIT_ASSERT(!xChildList->item(4));
xRoot->appendChild(xFoo);
@ -688,6 +748,7 @@ public:
CPPUNIT_TEST(testXDocumentBuilder);
CPPUNIT_TEST(testXXPathAPI);
CPPUNIT_TEST(testXXPathObject);
CPPUNIT_TEST(testXNamedNodeMap_AttributesMap);
CPPUNIT_TEST(testXNodeList_ChildList);
CPPUNIT_TEST(testXNodeList_NodeList);
CPPUNIT_TEST(serializerTest);