unoxml/qa/complex/unoxml/DOMTest.java | 263 ------------------------------- unoxml/qa/unit/domtest.cxx | 280 ++++++++++++++++++++++++++++++++++ 2 files changed, 280 insertions(+), 263 deletions(-)
New commits: commit 25ad0ee3c83925c39160d7d567e35d636c916809 Author: Xisco Fauli <xiscofa...@libreoffice.org> AuthorDate: Wed Oct 2 17:35:13 2024 +0200 Commit: Xisco Fauli <xiscofa...@libreoffice.org> CommitDate: Wed Oct 2 22:35:08 2024 +0200 unoxml: port testXXPathAPI from java to c++ Change-Id: Iaeb6713edc8d8b54066eaa5b8de2f600b1c10296 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174395 Tested-by: Jenkins Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org> diff --git a/unoxml/qa/complex/unoxml/DOMTest.java b/unoxml/qa/complex/unoxml/DOMTest.java index 06511faa0035..f61e448ba491 100644 --- a/unoxml/qa/complex/unoxml/DOMTest.java +++ b/unoxml/qa/complex/unoxml/DOMTest.java @@ -2378,158 +2378,6 @@ public class DOMTest assertSame("AttributesMap.getLength()", 2, xAttributes.getLength()); } - /* - @Test public void testXNamedNodeMap_EntitiesMap() throws Exception - { - XNamedNodeMap xEntities = FIXME - } - */ - - /* - @Test public void testXNamedNodeMap_NotationsMap() throws Exception - { - XNamedNodeMap xNotations = FIXME - } - */ - - @Test public void testXXPathAPI() throws Exception - { - XXPathAPI xXPathAPI = - UnoRuntime.queryInterface(XXPathAPI.class, - m_xMSF.createInstance("com.sun.star.xml.xpath.XPathAPI")); - XDocumentBuilder xBuilder = - UnoRuntime.queryInterface(XDocumentBuilder.class, - m_xMSF.createInstance("com.sun.star.xml.dom.DocumentBuilder")); - - String ns = "http://example.com/"; - - XDocument xDoc = xBuilder.newDocument(); - - XElement xRoot = xDoc.createElement("root"); - - XElement xFoo1 = xDoc.createElement("foo"); - XElement xFoo2 = xDoc.createElement("foo"); - XElement xFooNs = xDoc.createElementNS(ns, "ns:foo"); - XElement xBar = xDoc.createElement("bar"); - - xDoc.appendChild(xRoot); - xRoot.appendChild(xFoo1); - xFoo1.appendChild(xBar); - xBar.appendChild(xFoo2); - xRoot.appendChild(xFooNs); - - try { - xXPathAPI.eval(xRoot, "~/-$+&#_"); - fail("XXPathAPI.eval"); - } catch (XPathException e) { /* expected */ } - try { - xXPathAPI.evalNS(xRoot, "~/-$+&#_", xRoot); - fail("XXPathAPI.evalNS"); - } catch (XPathException e) { /* expected */ } - try { - xXPathAPI.selectNodeList(xRoot, "~/-$+&#_"); - fail("XXPathAPI.selectNodeList"); - } catch (XPathException e) { /* expected */ } - try { - xXPathAPI.selectNodeListNS(xRoot, "~/-$+&#_", xRoot); - fail("XXPathAPI.selectNodeListNS"); - } catch (XPathException e) { /* expected */ } - try { - xXPathAPI.selectSingleNode(xRoot, "~/-$+&#_"); - fail("XXPathAPI.selectSingleNode"); - } catch (XPathException e) { /* expected */ } - try { - xXPathAPI.selectSingleNodeNS(xRoot, "~/-$+&#_", xRoot); - fail("XXPathAPI.selectSingleNodeNS"); - } catch (XPathException e) { /* expected */ } - try { - xXPathAPI.eval(null, "child::foo"); - fail("XXPathAPI.eval(null)"); - } catch (Exception e) { /* expected */ } - try { - xXPathAPI.evalNS(null, "child::foo", xRoot); - fail("XXPathAPI.evalNS(null)"); - } catch (Exception e) { /* expected */ } - try { - xXPathAPI.selectNodeList(null, "child::foo"); - fail("XXPathAPI.selectNodeList(null)"); - } catch (Exception e) { /* expected */ } - try { - xXPathAPI.selectNodeListNS(null, "child::foo", xRoot); - fail("XXPathAPI.selectNodeListNS(null)"); - } catch (Exception e) { /* expected */ } - try { - xXPathAPI.selectSingleNode(null, "child::foo"); - fail("XXPathAPI.selectSingleNode(null)"); - } catch (Exception e) { /* expected */ } - try { - xXPathAPI.selectSingleNodeNS(null, "child::foo", xRoot); - fail("XXPathAPI.selectSingleNodeNS(null)"); - } catch (Exception e) { /* expected */ } - - { - XXPathObject xResult = xXPathAPI.eval(xRoot, "count(child::foo)"); - assertNotNull("XXPathAPI.eval", xResult); - assertEquals("XXPathAPI.eval", - XPATH_NUMBER, xResult.getObjectType()); - assertEquals("XXPathAPI.eval", 1, xResult.getLong()); - } - { - XXPathObject xResult = - xXPathAPI.evalNS(xRoot, "count(//ns:foo)", xFooNs); - assertNotNull("XXPathAPI.evalNS", xResult); - assertEquals("XXPathAPI.evalNS", - XPATH_NUMBER, xResult.getObjectType()); - assertEquals("XXPathAPI.evalNS", 1, xResult.getLong()); - } - { - XNodeList xResult = xXPathAPI.selectNodeList(xRoot, "child::foo"); - assertNotNull("XXPathAPI.selectNodeList", xResult); - assertEquals("XXPathAPI.selectNodeList", 1, xResult.getLength()); - assertEquals("XXPathAPI.selectNodeList", xFoo1, xResult.item(0)); - } - { - XNodeList xResult = - xXPathAPI.selectNodeListNS(xRoot, ".//ns:foo", xFooNs); - assertNotNull("XXPathAPI.selectNodeListNS", xResult); - assertEquals("XXPathAPI.selectNodeListNS", 1, xResult.getLength()); - assertEquals("XXPathAPI.selectNodeListNS", xFooNs, xResult.item(0)); - } - { - XNode xResult = xXPathAPI.selectSingleNode(xBar, "child::foo"); - assertNotNull("XXPathAPI.selectSingleNode", xResult); - assertEquals("XXPathAPI.selectSingleNode", xFoo2, xResult); - } - { - XNode xResult = - xXPathAPI.selectSingleNodeNS(xFoo2, "//ns:foo", xFooNs); - assertNotNull("XXPathAPI.selectSingleNodeNS", xResult); - assertEquals("XXPathAPI.selectSingleNodeNS", xFooNs, xResult); - } - - try { - xXPathAPI.selectSingleNode(xDoc, "//pre:foo"); - fail("XXPathAPI.selectSingleNode"); - } catch (XPathException e) { /* expected */ } - xXPathAPI.registerNS("pre", ns); - { - XNode xResult = xXPathAPI.selectSingleNode(xDoc, "//pre:foo"); - assertNotNull("XXPathAPI.registerNS", xResult); - assertEquals("XXPathAPI.registerNS", xFooNs, xResult); - } - - xXPathAPI.unregisterNS("pre", ns); - try { - xXPathAPI.selectSingleNode(xDoc, "//pre:foo"); - fail("XXPathAPI.unregisterNS"); - } catch (XPathException e) { /* expected */ } - - /* FIXME - registerExtension(""); - registerExtensionInstance(xExtension); - */ - } - @Test public void testXSAXSerialize() throws Exception { String file = diff --git a/unoxml/qa/unit/domtest.cxx b/unoxml/qa/unit/domtest.cxx index f749c19fcb61..d0c262123bf7 100644 --- a/unoxml/qa/unit/domtest.cxx +++ b/unoxml/qa/unit/domtest.cxx @@ -35,6 +35,7 @@ #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp> #include <com/sun/star/xml/sax/SAXParseException.hpp> #include <com/sun/star/xml/xpath/XPathAPI.hpp> +#include <com/sun/star/xml/xpath/XPathException.hpp> #include <com/sun/star/xml/xpath/XPathObjectType.hpp> #include <com/sun/star/xml/dom/XNodeList.hpp> @@ -317,6 +318,202 @@ public: mxDomBuilder->setErrorHandler(nullptr); } + void testXXPathAPI() + { + uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW ); + + Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument(); + CPPUNIT_ASSERT(xDocument); + + Reference< xml::dom::XElement > xRoot = xDocument->createElement(u"root"_ustr); + Reference< xml::dom::XElement > xFoo1 = xDocument->createElement(u"foo"_ustr); + Reference< xml::dom::XElement > xFoo2 = xDocument->createElement(u"foo"_ustr); + Reference< xml::dom::XElement > xFooNs = xDocument->createElementNS(u"http://example.com/"_ustr, u"ns:foo"_ustr); + Reference< xml::dom::XElement > xBar = xDocument->createElement(u"bar"_ustr); + + xDocument->appendChild(xRoot); + xRoot->appendChild(xFoo1); + xFoo1->appendChild(xBar); + xBar->appendChild(xFoo2); + xRoot->appendChild(xFooNs); + + try + { + xXPathAPI->eval(xRoot, u"~/-$+&#_"_ustr); + CPPUNIT_FAIL("XXPathAPI.eval"); + } + catch (xml::xpath::XPathException&) + { + } + + try + { + xXPathAPI->evalNS(xRoot, u"~/-$+&#_"_ustr, xRoot); + CPPUNIT_FAIL("XXPathAPI.evalNS"); + } + catch (xml::xpath::XPathException&) + { + } + + try + { + xXPathAPI->selectNodeList(xRoot, u"~/-$+&#_"_ustr); + CPPUNIT_FAIL("XXPathAPI.selectNodeList"); + } + catch (xml::xpath::XPathException&) + { + } + + try + { + xXPathAPI->selectNodeListNS(xRoot, u"~/-$+&#_"_ustr, xRoot); + CPPUNIT_FAIL("XXPathAPI.selectNodeListNS"); + } + catch (xml::xpath::XPathException&) + { + } + + try + { + xXPathAPI->selectSingleNode(xRoot, u"~/-$+&#_"_ustr); + CPPUNIT_FAIL("XXPathAPI.selectSingleNode"); + } + catch (xml::xpath::XPathException&) + { + } + + try + { + xXPathAPI->selectSingleNodeNS(xRoot, u"~/-$+&#_"_ustr, xRoot); + CPPUNIT_FAIL("XXPathAPI.selectSingleNodeNS"); + } + catch (xml::xpath::XPathException&) + { + } + + try + { + xXPathAPI->eval(nullptr, u"child::foo"_ustr); + CPPUNIT_FAIL("XXPathAPI.eval(null)"); + } + catch (uno::RuntimeException&) + { + } + + try + { + xXPathAPI->evalNS(nullptr, u"child::foo"_ustr, xRoot); + CPPUNIT_FAIL("XXPathAPI.evalNS(null)"); + } + catch (uno::RuntimeException&) + { + } + + try + { + xXPathAPI->selectNodeList(nullptr, u"child::foo"_ustr); + CPPUNIT_FAIL("XXPathAPI.selectNodeList(null)"); + } + catch (uno::RuntimeException&) + { + } + + try + { + xXPathAPI->selectNodeListNS(nullptr, u"child::foo"_ustr, xRoot); + CPPUNIT_FAIL("XXPathAPI.selectNodeListNS(null)"); + } + catch (uno::RuntimeException&) + { + } + + try + { + xXPathAPI->selectSingleNode(nullptr, u"child::foo"_ustr); + CPPUNIT_FAIL("XXPathAPI.selectSingleNode(null)"); + } + catch (uno::RuntimeException&) + { + } + + try + { + xXPathAPI->selectSingleNodeNS(nullptr, u"child::foo"_ustr, xRoot); + CPPUNIT_FAIL("XXPathAPI.selectSingleNodeNS(null)"); + } + catch (uno::RuntimeException&) + { + } + + { + uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"count(child::foo)"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NUMBER, xResult->getObjectType()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLong()); + } + { + uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->evalNS(xRoot, u"count(//ns:foo)"_ustr, xFooNs); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NUMBER, xResult->getObjectType()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLong()); + } + { + uno::Reference<xml::dom::XNodeList> xResult = xXPathAPI->selectNodeList(xRoot, u"child::foo"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLength()); + CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo1, uno::UNO_QUERY), + Reference< XInterface >(xResult->item(0), uno::UNO_QUERY)); + } + { + uno::Reference<xml::dom::XNodeList> xResult = xXPathAPI->selectNodeListNS(xRoot, u".//ns:foo"_ustr, xFooNs); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLength()); + CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFooNs, uno::UNO_QUERY), + Reference< XInterface >(xResult->item(0), uno::UNO_QUERY)); + } + { + uno::Reference<xml::dom::XNode> xResult = xXPathAPI->selectSingleNode(xBar, u"child::foo"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo2, uno::UNO_QUERY), + Reference< XInterface >(xResult, uno::UNO_QUERY)); + } + { + uno::Reference<xml::dom::XNode> xResult = xXPathAPI->selectSingleNodeNS(xFoo2, u"//ns:foo"_ustr, xFooNs); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFooNs, uno::UNO_QUERY), + Reference< XInterface >(xResult, uno::UNO_QUERY)); + } + + try + { + xXPathAPI->selectSingleNode(xDocument, u"//pre:foo"_ustr); + CPPUNIT_FAIL("XXPathAPI.selectSingleNode"); + } + catch (xml::xpath::XPathException&) + { + } + + xXPathAPI->registerNS(u"pre"_ustr, u"http://example.com/"_ustr); + + { + uno::Reference<xml::dom::XNode> xResult = xXPathAPI->selectSingleNode(xRoot, u"//pre:foo"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFooNs, uno::UNO_QUERY), + Reference< XInterface >(xResult, uno::UNO_QUERY)); + } + + xXPathAPI->unregisterNS(u"pre"_ustr, u"http://example.com/"_ustr); + + try + { + xXPathAPI->selectSingleNode(xDocument, u"//pre:foo"_ustr); + CPPUNIT_FAIL("XXPathAPI.unregisterNS"); + } + catch (xml::xpath::XPathException&) + { + } + } + void testXXPathObject() { uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW ); @@ -442,6 +639,7 @@ public: CPPUNIT_TEST(warningInputTest); CPPUNIT_TEST(errorInputTest); CPPUNIT_TEST(testXDocumentBuilder); + CPPUNIT_TEST(testXXPathAPI); CPPUNIT_TEST(testXXPathObject); CPPUNIT_TEST(testXNodeList_NodeList); CPPUNIT_TEST(serializerTest); commit d937166f5121a1b0c381d1991cdc932c45afcddb Author: Xisco Fauli <xiscofa...@libreoffice.org> AuthorDate: Wed Oct 2 16:39:01 2024 +0200 Commit: Xisco Fauli <xiscofa...@libreoffice.org> CommitDate: Wed Oct 2 22:35:01 2024 +0200 unoxml: port testXXPathObject from java to c++ Code for "count(//foo) = 2" was duplicated Same for "//foo" which is already tested in testXNodeList_NodeList Change-Id: I2144552f5b687e639fef66b82463aae3bf82f53c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174394 Tested-by: Jenkins Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org> diff --git a/unoxml/qa/complex/unoxml/DOMTest.java b/unoxml/qa/complex/unoxml/DOMTest.java index f4e82d065042..06511faa0035 100644 --- a/unoxml/qa/complex/unoxml/DOMTest.java +++ b/unoxml/qa/complex/unoxml/DOMTest.java @@ -2530,78 +2530,6 @@ public class DOMTest */ } - @Test public void testXXPathObject() throws Exception - { - XXPathAPI xXPathAPI = - UnoRuntime.queryInterface(XXPathAPI.class, - m_xMSF.createInstance("com.sun.star.xml.xpath.XPathAPI")); - XDocumentBuilder xBuilder = - UnoRuntime.queryInterface(XDocumentBuilder.class, - m_xMSF.createInstance("com.sun.star.xml.dom.DocumentBuilder")); - - String ns = "http://example.com/"; - - XDocument xDoc = xBuilder.newDocument(); - - XElement xRoot = xDoc.createElement("root"); - - XElement xFoo1 = xDoc.createElement("foo"); - XElement xFoo2 = xDoc.createElement("foo"); - XElement xFooNs = xDoc.createElementNS(ns, "ns:foo"); - XElement xBar = xDoc.createElement("bar"); - - xDoc.appendChild(xRoot); - xRoot.appendChild(xFoo1); - xFoo1.appendChild(xBar); - xBar.appendChild(xFoo2); - xRoot.appendChild(xFooNs); - - { - XXPathObject xResult = xXPathAPI.eval(xRoot, "count(//foo)"); - assertNotNull("XXPathAPI.eval", xResult); - assertEquals("XXPathObject.getObjectType", - XPATH_NUMBER, xResult.getObjectType()); - assertEquals("XXPathObject.getByte", 2, xResult.getByte()); - assertEquals("XXPathObject.getShort", 2, xResult.getShort()); - assertEquals("XXPathObject.getLong", 2, xResult.getLong()); - assertEquals("XXPathObject.getHyper", 2, xResult.getHyper()); - assertEquals("XXPathObject.getFloat", 2.0, xResult.getFloat(), 0.0); - assertEquals("XXPathObject.getDouble", - 2.0, xResult.getDouble(), 0.0); - assertEquals("XXPathObject.getString", "2", xResult.getString()); - } - { - XXPathObject xResult = xXPathAPI.eval(xRoot, "count(//foo) = 2"); - assertNotNull("XXPathAPI.eval", xResult); - assertEquals("XXPathObject.getObjectType", - XPATH_BOOLEAN, xResult.getObjectType()); - assertEquals("XXPathObject.getBoolean", true, xResult.getBoolean()); - assertEquals("XXPathObject.getString", "true", xResult.getString()); - } - { - XXPathObject xResult = xXPathAPI.eval(xRoot, "count(//foo) = 2"); - assertNotNull("XXPathAPI.eval", xResult); - assertEquals("XXPathObject.getObjectType", - XPATH_BOOLEAN, xResult.getObjectType()); - assertEquals("XXPathObject.getBoolean", true, xResult.getBoolean()); - assertEquals("XXPathObject.getString", "true", xResult.getString()); - } - { - XXPathObject xResult = xXPathAPI.eval(xRoot, "local-name(foo)"); - assertNotNull("XXPathAPI.eval", xResult); - assertEquals("XXPathObject.getObjectType", - XPATH_STRING, xResult.getObjectType()); - assertEquals("XXPathObject.getString", "foo", xResult.getString()); - } - { - XXPathObject xResult = xXPathAPI.eval(xRoot, "//foo"); - assertNotNull("XXPathAPI.eval", xResult); - assertEquals("XXPathObject.getObjectType", - XPATH_NODESET, xResult.getObjectType()); - assertNotNull("XXPathObject.getNodeList", xResult.getNodeList()); - } - } - @Test public void testXSAXSerialize() throws Exception { String file = diff --git a/unoxml/qa/unit/domtest.cxx b/unoxml/qa/unit/domtest.cxx index 12eb1991442e..f749c19fcb61 100644 --- a/unoxml/qa/unit/domtest.cxx +++ b/unoxml/qa/unit/domtest.cxx @@ -317,6 +317,52 @@ public: mxDomBuilder->setErrorHandler(nullptr); } + void testXXPathObject() + { + uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW ); + + Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument(); + CPPUNIT_ASSERT(xDocument); + + Reference< xml::dom::XElement > xRoot = xDocument->createElement(u"root"_ustr); + Reference< xml::dom::XElement > xFoo1 = xDocument->createElement(u"foo"_ustr); + Reference< xml::dom::XElement > xFoo2 = xDocument->createElement(u"foo"_ustr); + Reference< xml::dom::XElement > xFooNs = xDocument->createElementNS(u"http://example.com/"_ustr, u"ns:foo"_ustr); + Reference< xml::dom::XElement > xBar = xDocument->createElement(u"bar"_ustr); + + xDocument->appendChild(xRoot); + xRoot->appendChild(xFoo1); + xFoo1->appendChild(xBar); + xBar->appendChild(xFoo2); + xRoot->appendChild(xFooNs); + + { + uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"count(//foo)"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NUMBER, xResult->getObjectType()); + CPPUNIT_ASSERT_EQUAL(sal_Int8(2), xResult->getByte()); + CPPUNIT_ASSERT_EQUAL(sal_Int16(2), xResult->getShort()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xResult->getLong()); + CPPUNIT_ASSERT_EQUAL(sal_Int64(2), xResult->getHyper()); + CPPUNIT_ASSERT_EQUAL(float(2), xResult->getFloat()); + CPPUNIT_ASSERT_EQUAL(double(2), xResult->getDouble()); + CPPUNIT_ASSERT_EQUAL(u"2"_ustr, xResult->getString()); + } + { + uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"count(//foo) = 2"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_BOOLEAN, xResult->getObjectType()); + CPPUNIT_ASSERT_EQUAL(sal_True, xResult->getBoolean()); + CPPUNIT_ASSERT_EQUAL(u"true"_ustr, xResult->getString()); + } + { + uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"local-name(foo)"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_STRING, xResult->getObjectType()); + CPPUNIT_ASSERT_EQUAL(u"foo"_ustr, xResult->getString()); + } + } + void testXNodeList_NodeList() { uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW ); @@ -396,6 +442,7 @@ public: CPPUNIT_TEST(warningInputTest); CPPUNIT_TEST(errorInputTest); CPPUNIT_TEST(testXDocumentBuilder); + CPPUNIT_TEST(testXXPathObject); CPPUNIT_TEST(testXNodeList_NodeList); CPPUNIT_TEST(serializerTest); CPPUNIT_TEST_SUITE_END(); commit 466134863b5ceb76df743017fa175fdce2bbc597 Author: Xisco Fauli <xiscofa...@libreoffice.org> AuthorDate: Wed Oct 2 16:24:57 2024 +0200 Commit: Xisco Fauli <xiscofa...@libreoffice.org> CommitDate: Wed Oct 2 22:34:54 2024 +0200 unoxml: port testXNodeList_NodeList from java to c++ Change-Id: I1a059ecb13e35518828b395f9eb9863930a3d12e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174392 Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org> Tested-by: Jenkins diff --git a/unoxml/qa/complex/unoxml/DOMTest.java b/unoxml/qa/complex/unoxml/DOMTest.java index ce4fb4b17d48..f4e82d065042 100644 --- a/unoxml/qa/complex/unoxml/DOMTest.java +++ b/unoxml/qa/complex/unoxml/DOMTest.java @@ -2602,45 +2602,6 @@ public class DOMTest } } - @Test public void testXNodeList_NodeList() throws Exception - { - XXPathAPI xXPathAPI = - UnoRuntime.queryInterface(XXPathAPI.class, - m_xMSF.createInstance("com.sun.star.xml.xpath.XPathAPI")); - XDocumentBuilder xBuilder = - UnoRuntime.queryInterface(XDocumentBuilder.class, - m_xMSF.createInstance("com.sun.star.xml.dom.DocumentBuilder")); - - String ns = "http://example.com/"; - - XDocument xDoc = xBuilder.newDocument(); - - XElement xRoot = xDoc.createElement("root"); - - XElement xFoo1 = xDoc.createElement("foo"); - XElement xFoo2 = xDoc.createElement("foo"); - XElement xFooNs = xDoc.createElementNS(ns, "ns:foo"); - XElement xBar = xDoc.createElement("bar"); - - xDoc.appendChild(xRoot); - xRoot.appendChild(xFoo1); - xFoo1.appendChild(xBar); - xBar.appendChild(xFoo2); - xRoot.appendChild(xFooNs); - - { - XXPathObject xResult = xXPathAPI.eval(xRoot, "//foo"); - assertNotNull("XXPathAPI.eval", xResult); - assertEquals("XXPathObject.getObjectType", - XPATH_NODESET, xResult.getObjectType()); - XNodeList xNodeList = xResult.getNodeList(); - assertNotNull("XXPathObject.getNodeList", xNodeList); - assertEquals("NodeList.getLength", 2, xNodeList.getLength()); - assertEquals("NodeList.item", xFoo1, xNodeList.item(0)); - assertEquals("NodeList.item", xFoo2, xNodeList.item(1)); - } - } - @Test public void testXSAXSerialize() throws Exception { String file = diff --git a/unoxml/qa/unit/domtest.cxx b/unoxml/qa/unit/domtest.cxx index 324c4ca457fd..12eb1991442e 100644 --- a/unoxml/qa/unit/domtest.cxx +++ b/unoxml/qa/unit/domtest.cxx @@ -34,6 +34,9 @@ #include <com/sun/star/xml/sax/XSAXSerializable.hpp> #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp> #include <com/sun/star/xml/sax/SAXParseException.hpp> +#include <com/sun/star/xml/xpath/XPathAPI.hpp> +#include <com/sun/star/xml/xpath/XPathObjectType.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> using namespace ::comphelper; using namespace ::com::sun::star; @@ -314,6 +317,37 @@ public: mxDomBuilder->setErrorHandler(nullptr); } + void testXNodeList_NodeList() + { + uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW ); + + Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument(); + CPPUNIT_ASSERT(xDocument); + + Reference< xml::dom::XElement > xRoot = xDocument->createElement(u"root"_ustr); + Reference< xml::dom::XElement > xFoo1 = xDocument->createElement(u"foo"_ustr); + Reference< xml::dom::XElement > xFoo2 = xDocument->createElement(u"foo"_ustr); + Reference< xml::dom::XElement > xFooNs = xDocument->createElementNS(u"http://example.com/"_ustr, u"ns:foo"_ustr); + Reference< xml::dom::XElement > xBar = xDocument->createElement(u"bar"_ustr); + + xDocument->appendChild(xRoot); + xRoot->appendChild(xFoo1); + xFoo1->appendChild(xBar); + xBar->appendChild(xFoo2); + xRoot->appendChild(xFooNs); + + uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"//foo"_ustr); + CPPUNIT_ASSERT(xResult); + CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NODESET, xResult->getObjectType()); + uno::Reference<xml::dom::XNodeList> xNodeList = xResult->getNodeList(); + CPPUNIT_ASSERT(xNodeList); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xNodeList->getLength()); + CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo1, uno::UNO_QUERY), + Reference< XInterface >(xNodeList->item(0), uno::UNO_QUERY)); + CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo2, uno::UNO_QUERY), + Reference< XInterface >(xNodeList->item(1), uno::UNO_QUERY)); + } + void serializerTest () { rtl::Reference<DocumentHandler> xHandler = new DocumentHandler; @@ -362,6 +396,7 @@ public: CPPUNIT_TEST(warningInputTest); CPPUNIT_TEST(errorInputTest); CPPUNIT_TEST(testXDocumentBuilder); + CPPUNIT_TEST(testXNodeList_NodeList); CPPUNIT_TEST(serializerTest); CPPUNIT_TEST_SUITE_END(); };