tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / unoxml / qa / unit / domtest.cxx
blob8d872d765f7e0aeb0d1d97079c19f20c640c5b8e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/ref.hxx>
21 #include <sal/log.hxx>
22 #include <sax/fastattribs.hxx>
23 #include <comphelper/seqstream.hxx>
24 #include <cppuhelper/implbase.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <cppunit/extensions/HelperMacros.h>
27 #include <cppunit/plugin/TestPlugIn.h>
28 #include <test/bootstrapfixture.hxx>
30 #include <com/sun/star/ucb/ContentCreationException.hpp>
31 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
32 #include <com/sun/star/xml/dom/DOMException.hpp>
33 #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
34 #include <com/sun/star/xml/sax/FastToken.hpp>
35 #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
36 #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
37 #include <com/sun/star/xml/sax/SAXParseException.hpp>
38 #include <com/sun/star/xml/xpath/XPathAPI.hpp>
39 #include <com/sun/star/xml/xpath/XPathException.hpp>
40 #include <com/sun/star/xml/xpath/XPathObjectType.hpp>
41 #include <com/sun/star/xml/dom/XNodeList.hpp>
43 using namespace ::comphelper;
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using css::xml::dom::XDocumentBuilder;
48 namespace
50 // valid xml
51 const char validTestFile[] =
52 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
53 <office:document-content \
54 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
55 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
56 office:version=\"1.0\"> \
57 <office:scripts/> \
58 <xlink:test/> \
59 <office:automatic-styles teststyle=\"test\"/> \
60 <moretest/> \
61 some text \303\266\303\244\303\274 \
62 </office:document-content> \
65 // generates a warning: unknown xml:space
66 // value
67 const char warningTestFile[] =
68 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
69 <office:document-content \
70 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
71 xml:space=\"blah\" \
72 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
73 office:version=\"1.0\"> \
74 <office:scripts/> \
75 <xlink:test/> \
76 <office:automatic-styles teststyle=\"test\"/> \
77 <moretest/> \
78 some text \303\266\303\244\303\274 \
79 </office:document-content> \
82 // <?xml not at start of file
83 const char errorTestFile[] =
84 " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
85 <office:document-content \
86 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
87 office:version=\"1.0\"> \
88 <office:scripts/> \
89 <office:automatic-styles/> \
90 </office:document-content> \
93 struct ErrorHandler
94 : public ::cppu::WeakImplHelper< xml::sax::XErrorHandler >
96 sal_uInt32 mnErrCount;
97 // sal_uInt32 mnFatalCount; // No fatal error counter, as lib2xml doesn't distinguish between error and fatal error
98 // (See xmlFatalErrMsg from lib2xml/parse.c and __xmlRaiseError from lib2xml/error.c)
99 sal_uInt32 mnWarnCount;
101 bool noErrors() const { return !mnErrCount /*&& !mnFatalCount*/ && !mnWarnCount; }
103 ErrorHandler() : mnErrCount(0), /*mnFatalCount(0),*/ mnWarnCount(0)
106 virtual void SAL_CALL error( const uno::Any& ) override
108 ++mnErrCount;
111 // Just implement FatalError function as it is in XErrorHandler
112 // This function is never used, as lib2xml doesn't distinguish between error and fatalerror and calls error functions in both cases
113 virtual void SAL_CALL fatalError( const uno::Any& ) override
115 //++mnFatalCount;
118 virtual void SAL_CALL warning( const uno::Any& ) override
120 ++mnWarnCount;
124 struct DocumentHandler
125 : public ::cppu::WeakImplHelper< xml::sax::XFastDocumentHandler >
127 // XFastContextHandler
128 virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const uno::Reference< xml::sax::XFastAttributeList >& ) override
130 SAL_INFO(
131 "unoxml",
132 "Seen element: " << (Element & 0xFFFF) << " with namespace "
133 << (Element & 0xFFFF0000));
136 virtual void SAL_CALL startUnknownElement( const OUString& , const OUString& , const uno::Reference< xml::sax::XFastAttributeList >& ) override
140 virtual void SAL_CALL endFastElement( ::sal_Int32 ) override
144 virtual void SAL_CALL endUnknownElement( const OUString&, const OUString& ) override
148 virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 , const uno::Reference< xml::sax::XFastAttributeList >& ) override
150 return this;
153 virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& , const OUString& , const uno::Reference< xml::sax::XFastAttributeList >& ) override
155 return this;
158 virtual void SAL_CALL characters( const OUString& ) override
162 // XFastDocumentHandler
163 virtual void SAL_CALL startDocument( ) override
167 virtual void SAL_CALL endDocument( ) override
171 virtual void SAL_CALL processingInstruction( const OUString& /*rTarget*/, const OUString& /*rData*/ ) override
175 virtual void SAL_CALL setDocumentLocator( const uno::Reference< xml::sax::XLocator >& ) override
180 struct TokenHandler : public sax_fastparser::FastTokenHandlerBase
182 virtual ::sal_Int32 SAL_CALL getTokenFromUTF8( const uno::Sequence< ::sal_Int8 >& Identifier ) override
184 return Identifier.hasElements() ? Identifier[0] : 0;
187 virtual uno::Sequence< ::sal_Int8 > SAL_CALL getUTF8Identifier( ::sal_Int32 ) override
189 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
190 false );
191 return uno::Sequence<sal_Int8>();
194 virtual sal_Int32 getTokenDirect( std::string_view /* token */ ) const override
196 return -1;
200 class BasicTest : public test::BootstrapFixture
202 protected:
203 uno::Reference<XDocumentBuilder> mxDomBuilder;
204 rtl::Reference<ErrorHandler> mxErrHandler;
205 rtl::Reference<SequenceInputStream> mxValidInStream;
206 rtl::Reference<SequenceInputStream> mxWarningInStream;
207 rtl::Reference<SequenceInputStream> mxErrorInStream;
209 public:
211 BasicTest() {}
213 void setUp() override
215 test::BootstrapFixture::setUp();
217 mxErrHandler.set( new ErrorHandler() );
218 uno::Reference<XDocumentBuilder> xDB( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.dom.DocumentBuilder"_ustr), uno::UNO_QUERY_THROW );
219 mxDomBuilder.set( xDB );
220 mxValidInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(validTestFile), std::size(validTestFile) - 1)) );
221 mxWarningInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(warningTestFile), std::size(warningTestFile) - 1)) );
222 mxErrorInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(errorTestFile), std::size(errorTestFile) - 1)) );
223 mxDomBuilder->setErrorHandler(mxErrHandler);
226 void validInputTest()
230 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #1",
231 mxDomBuilder->parse(mxValidInStream).is());
232 CPPUNIT_ASSERT_MESSAGE("Valid input file resulted in parse errors",
233 mxErrHandler->noErrors());
235 catch (const css::xml::sax::SAXParseException&)
237 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument (exception thrown)", false);
241 void warningInputTest()
245 // We DON'T expect exception here, as mxWarningInStream is valid XML Doc
246 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #2",
247 mxDomBuilder->parse(mxWarningInStream).is());
249 catch (const css::xml::sax::SAXParseException& )
251 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #2 (exception thrown)", false);
253 CPPUNIT_ASSERT_MESSAGE("No parse warnings in unclean input file",
254 mxErrHandler->mnWarnCount);
255 CPPUNIT_ASSERT_MESSAGE("No parse warnings in unclean input file",
256 !mxErrHandler->mnErrCount /*&& !mxErrHandler->mnFatalCount*/);
259 void errorInputTest()
263 // We expect exception here, as mxErrorInStream is invalid XML Doc
264 CPPUNIT_ASSERT_MESSAGE("Invalid input file result in XDocument #2!",
265 !mxDomBuilder->parse(mxErrorInStream).is());
266 CPPUNIT_ASSERT_MESSAGE("No exception is thrown in unclean input file", false);
268 catch (const css::xml::sax::SAXParseException&)
270 // It's OK to catch an exception here as we parse incorrect XML file
272 CPPUNIT_ASSERT_MESSAGE("No parse errors in unclean input file",
273 !mxErrHandler->mnWarnCount);
274 CPPUNIT_ASSERT_MESSAGE("No parse errors in unclean input file",
275 mxErrHandler->mnErrCount /*&& !mxErrHandler->mnFatalCount*/);
278 void testXDocumentBuilder()
280 mxDomBuilder->isNamespaceAware();
281 mxDomBuilder->isValidating();
283 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
284 CPPUNIT_ASSERT(xDocument);
288 mxDomBuilder->parse(nullptr);
289 CPPUNIT_FAIL("XDocumentBuilder.parse(null)");
291 catch (css::uno::RuntimeException&)
295 uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext(), css::uno::UNO_SET_THROW);
296 const uno::Reference<css::ucb::XSimpleFileAccess> xFileAccess(
297 xContext->getServiceManager()->createInstanceWithContext(
298 u"com.sun.star.ucb.SimpleFileAccess"_ustr, xContext),
299 uno::UNO_QUERY_THROW);
300 uno::Reference<io::XInputStream> xInputStream(xFileAccess->openFileRead(m_directories.getURLFromSrc(u"/unoxml/qa/unit/data/example.rdf")),
301 uno::UNO_SET_THROW);
303 xDocument = mxDomBuilder->parse(xInputStream);
304 CPPUNIT_ASSERT(xDocument);
308 mxDomBuilder->parseURI(u""_ustr);
309 CPPUNIT_FAIL("XDocumentBuilder.parseURI(\"\")");
311 catch (css::ucb::ContentCreationException&)
315 xDocument = mxDomBuilder->parseURI(m_directories.getURLFromSrc(u"/unoxml/qa/unit/data/example.rdf"));
316 CPPUNIT_ASSERT(xDocument);
318 mxDomBuilder->setEntityResolver(nullptr);
319 mxDomBuilder->setErrorHandler(nullptr);
322 void testXXPathAPI()
324 uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW );
326 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
327 CPPUNIT_ASSERT(xDocument);
329 Reference< xml::dom::XElement > xRoot = xDocument->createElement(u"root"_ustr);
330 Reference< xml::dom::XElement > xFoo1 = xDocument->createElement(u"foo"_ustr);
331 Reference< xml::dom::XElement > xFoo2 = xDocument->createElement(u"foo"_ustr);
332 Reference< xml::dom::XElement > xFooNs = xDocument->createElementNS(u"http://example.com/"_ustr, u"ns:foo"_ustr);
333 Reference< xml::dom::XElement > xBar = xDocument->createElement(u"bar"_ustr);
335 xDocument->appendChild(xRoot);
336 xRoot->appendChild(xFoo1);
337 xFoo1->appendChild(xBar);
338 xBar->appendChild(xFoo2);
339 xRoot->appendChild(xFooNs);
343 xXPathAPI->eval(xRoot, u"~/-$+&#_"_ustr);
344 CPPUNIT_FAIL("XXPathAPI.eval");
346 catch (xml::xpath::XPathException&)
352 xXPathAPI->evalNS(xRoot, u"~/-$+&#_"_ustr, xRoot);
353 CPPUNIT_FAIL("XXPathAPI.evalNS");
355 catch (xml::xpath::XPathException&)
361 xXPathAPI->selectNodeList(xRoot, u"~/-$+&#_"_ustr);
362 CPPUNIT_FAIL("XXPathAPI.selectNodeList");
364 catch (xml::xpath::XPathException&)
370 xXPathAPI->selectNodeListNS(xRoot, u"~/-$+&#_"_ustr, xRoot);
371 CPPUNIT_FAIL("XXPathAPI.selectNodeListNS");
373 catch (xml::xpath::XPathException&)
379 xXPathAPI->selectSingleNode(xRoot, u"~/-$+&#_"_ustr);
380 CPPUNIT_FAIL("XXPathAPI.selectSingleNode");
382 catch (xml::xpath::XPathException&)
388 xXPathAPI->selectSingleNodeNS(xRoot, u"~/-$+&#_"_ustr, xRoot);
389 CPPUNIT_FAIL("XXPathAPI.selectSingleNodeNS");
391 catch (xml::xpath::XPathException&)
397 xXPathAPI->eval(nullptr, u"child::foo"_ustr);
398 CPPUNIT_FAIL("XXPathAPI.eval(null)");
400 catch (uno::RuntimeException&)
406 xXPathAPI->evalNS(nullptr, u"child::foo"_ustr, xRoot);
407 CPPUNIT_FAIL("XXPathAPI.evalNS(null)");
409 catch (uno::RuntimeException&)
415 xXPathAPI->selectNodeList(nullptr, u"child::foo"_ustr);
416 CPPUNIT_FAIL("XXPathAPI.selectNodeList(null)");
418 catch (uno::RuntimeException&)
424 xXPathAPI->selectNodeListNS(nullptr, u"child::foo"_ustr, xRoot);
425 CPPUNIT_FAIL("XXPathAPI.selectNodeListNS(null)");
427 catch (uno::RuntimeException&)
433 xXPathAPI->selectSingleNode(nullptr, u"child::foo"_ustr);
434 CPPUNIT_FAIL("XXPathAPI.selectSingleNode(null)");
436 catch (uno::RuntimeException&)
442 xXPathAPI->selectSingleNodeNS(nullptr, u"child::foo"_ustr, xRoot);
443 CPPUNIT_FAIL("XXPathAPI.selectSingleNodeNS(null)");
445 catch (uno::RuntimeException&)
450 uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"count(child::foo)"_ustr);
451 CPPUNIT_ASSERT(xResult);
452 CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NUMBER, xResult->getObjectType());
453 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLong());
456 uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->evalNS(xRoot, u"count(//ns:foo)"_ustr, xFooNs);
457 CPPUNIT_ASSERT(xResult);
458 CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NUMBER, xResult->getObjectType());
459 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLong());
462 uno::Reference<xml::dom::XNodeList> xResult = xXPathAPI->selectNodeList(xRoot, u"child::foo"_ustr);
463 CPPUNIT_ASSERT(xResult);
464 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLength());
465 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo1, uno::UNO_QUERY),
466 Reference< XInterface >(xResult->item(0), uno::UNO_QUERY));
469 uno::Reference<xml::dom::XNodeList> xResult = xXPathAPI->selectNodeListNS(xRoot, u".//ns:foo"_ustr, xFooNs);
470 CPPUNIT_ASSERT(xResult);
471 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xResult->getLength());
472 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFooNs, uno::UNO_QUERY),
473 Reference< XInterface >(xResult->item(0), uno::UNO_QUERY));
476 uno::Reference<xml::dom::XNode> xResult = xXPathAPI->selectSingleNode(xBar, u"child::foo"_ustr);
477 CPPUNIT_ASSERT(xResult);
478 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo2, uno::UNO_QUERY),
479 Reference< XInterface >(xResult, uno::UNO_QUERY));
482 uno::Reference<xml::dom::XNode> xResult = xXPathAPI->selectSingleNodeNS(xFoo2, u"//ns:foo"_ustr, xFooNs);
483 CPPUNIT_ASSERT(xResult);
484 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFooNs, uno::UNO_QUERY),
485 Reference< XInterface >(xResult, uno::UNO_QUERY));
490 xXPathAPI->selectSingleNode(xDocument, u"//pre:foo"_ustr);
491 CPPUNIT_FAIL("XXPathAPI.selectSingleNode");
493 catch (xml::xpath::XPathException&)
497 xXPathAPI->registerNS(u"pre"_ustr, u"http://example.com/"_ustr);
500 uno::Reference<xml::dom::XNode> xResult = xXPathAPI->selectSingleNode(xRoot, u"//pre:foo"_ustr);
501 CPPUNIT_ASSERT(xResult);
502 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFooNs, uno::UNO_QUERY),
503 Reference< XInterface >(xResult, uno::UNO_QUERY));
506 xXPathAPI->unregisterNS(u"pre"_ustr, u"http://example.com/"_ustr);
510 xXPathAPI->selectSingleNode(xDocument, u"//pre:foo"_ustr);
511 CPPUNIT_FAIL("XXPathAPI.unregisterNS");
513 catch (xml::xpath::XPathException&)
518 void testXXPathObject()
520 uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW );
522 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
523 CPPUNIT_ASSERT(xDocument);
525 Reference< xml::dom::XElement > xRoot = xDocument->createElement(u"root"_ustr);
526 Reference< xml::dom::XElement > xFoo1 = xDocument->createElement(u"foo"_ustr);
527 Reference< xml::dom::XElement > xFoo2 = xDocument->createElement(u"foo"_ustr);
528 Reference< xml::dom::XElement > xFooNs = xDocument->createElementNS(u"http://example.com/"_ustr, u"ns:foo"_ustr);
529 Reference< xml::dom::XElement > xBar = xDocument->createElement(u"bar"_ustr);
531 xDocument->appendChild(xRoot);
532 xRoot->appendChild(xFoo1);
533 xFoo1->appendChild(xBar);
534 xBar->appendChild(xFoo2);
535 xRoot->appendChild(xFooNs);
538 uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"count(//foo)"_ustr);
539 CPPUNIT_ASSERT(xResult);
540 CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NUMBER, xResult->getObjectType());
541 CPPUNIT_ASSERT_EQUAL(sal_Int8(2), xResult->getByte());
542 CPPUNIT_ASSERT_EQUAL(sal_Int16(2), xResult->getShort());
543 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xResult->getLong());
544 CPPUNIT_ASSERT_EQUAL(sal_Int64(2), xResult->getHyper());
545 CPPUNIT_ASSERT_EQUAL(float(2), xResult->getFloat());
546 CPPUNIT_ASSERT_EQUAL(double(2), xResult->getDouble());
547 CPPUNIT_ASSERT_EQUAL(u"2"_ustr, xResult->getString());
550 uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"count(//foo) = 2"_ustr);
551 CPPUNIT_ASSERT(xResult);
552 CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_BOOLEAN, xResult->getObjectType());
553 CPPUNIT_ASSERT_EQUAL(sal_True, xResult->getBoolean());
554 CPPUNIT_ASSERT_EQUAL(u"true"_ustr, xResult->getString());
557 uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"local-name(foo)"_ustr);
558 CPPUNIT_ASSERT(xResult);
559 CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_STRING, xResult->getObjectType());
560 CPPUNIT_ASSERT_EQUAL(u"foo"_ustr, xResult->getString());
564 void testXText()
566 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
567 CPPUNIT_ASSERT(xDocument);
568 uno::Reference<xml::dom::XText> xText = xDocument->createTextNode(u"foobar"_ustr);
569 CPPUNIT_ASSERT(xText);
571 CPPUNIT_ASSERT_EQUAL(u"foobar"_ustr, xText->getData());
572 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), xText->getLength());
574 xText->setData(u"foo"_ustr);
576 xText->appendData(u"baz"_ustr);
577 CPPUNIT_ASSERT_EQUAL(u"foobaz"_ustr, xText->getData());
581 xText->deleteData(999, 999);
582 CPPUNIT_FAIL("XText.deleteData(999,999)");
584 catch (xml::dom::DOMException& e)
586 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_INDEX_SIZE_ERR, e.Code);
589 xText->deleteData(0, 3);
590 CPPUNIT_ASSERT_EQUAL(u"baz"_ustr, xText->getData());
594 xText->insertData(999, u"blah"_ustr);
595 CPPUNIT_FAIL("XText.insertData(999,\"blah\")");
597 catch (xml::dom::DOMException& e)
599 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_INDEX_SIZE_ERR, e.Code);
602 xText->insertData(1, u"arb"_ustr);
603 CPPUNIT_ASSERT_EQUAL(u"barbaz"_ustr, xText->getData());
607 xText->replaceData(999, 999, u"x"_ustr);
608 CPPUNIT_FAIL("XText.replaceData(999, 999, \"x\")");
610 catch (xml::dom::DOMException& e)
612 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_INDEX_SIZE_ERR, e.Code);
615 xText->replaceData(3, 3, u"foo"_ustr);
616 CPPUNIT_ASSERT_EQUAL(u"barfoo"_ustr, xText->getData());
618 xText->setData(u"quux"_ustr);
619 CPPUNIT_ASSERT_EQUAL(u"quux"_ustr, xText->getData());
623 xText->subStringData(999, 999);
624 CPPUNIT_FAIL("XText.subStringData(999, 999)");
626 catch (xml::dom::DOMException& e)
628 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_INDEX_SIZE_ERR, e.Code);
631 CPPUNIT_ASSERT_EQUAL(u"x"_ustr, xText->subStringData(3, 1));
633 // XNode
635 uno::Reference<xml::dom::XNode> xTextCloneN = xText->cloneNode(false);
636 CPPUNIT_ASSERT(xTextCloneN);
637 uno::Reference<xml::dom::XText> xTextClone(xTextCloneN, uno::UNO_QUERY_THROW);
638 CPPUNIT_ASSERT(xTextClone);
639 CPPUNIT_ASSERT(!xTextClone->hasChildNodes());
642 uno::Reference<xml::dom::XNode> xTextCloneN = xText->cloneNode(true);
643 CPPUNIT_ASSERT(xTextCloneN);
644 uno::Reference<xml::dom::XText> xTextClone(xTextCloneN, uno::UNO_QUERY_THROW);
645 CPPUNIT_ASSERT(xTextClone);
646 CPPUNIT_ASSERT(!xTextClone->hasChildNodes());
649 CPPUNIT_ASSERT(!xText->getAttributes());
651 uno::Reference<xml::dom::XNodeList> xChildList = xText->getChildNodes();
652 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xChildList->getLength());
654 CPPUNIT_ASSERT_EQUAL(u""_ustr, xText->getLocalName());
655 CPPUNIT_ASSERT_EQUAL(u""_ustr, xText->getNamespaceURI());
656 CPPUNIT_ASSERT(!xText->getNextSibling());
657 CPPUNIT_ASSERT_EQUAL(u"#text"_ustr, xText->getNodeName());
658 CPPUNIT_ASSERT_EQUAL(xml::dom::NodeType::NodeType_TEXT_NODE, xText->getNodeType());
659 CPPUNIT_ASSERT_EQUAL(u"quux"_ustr, xText->getNodeValue());
660 CPPUNIT_ASSERT_EQUAL(xDocument, xText->getOwnerDocument());
661 CPPUNIT_ASSERT(!xText->getParentNode());
662 CPPUNIT_ASSERT_EQUAL(u""_ustr, xText->getPrefix());
663 CPPUNIT_ASSERT(!xText->getPreviousSibling());
664 CPPUNIT_ASSERT(!xText->hasAttributes());
665 CPPUNIT_ASSERT(!xText->hasChildNodes());
666 CPPUNIT_ASSERT(!xText->isSupported(u"frobnication"_ustr, u"v99.33.0.0.0.1"_ustr));
668 xText->normalize();
669 xText->setNodeValue(u"42"_ustr);
670 CPPUNIT_ASSERT_EQUAL(u"42"_ustr, xText->getNodeValue());
674 xText->setPrefix(u"foo"_ustr);
675 CPPUNIT_FAIL("XText.setPrefix()");
677 catch (xml::dom::DOMException& e)
679 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_NO_MODIFICATION_ALLOWED_ERR, e.Code);
682 uno::Reference<xml::dom::XText> xText2 = xDocument->createTextNode(u"foobar"_ustr);
683 uno::Reference<xml::dom::XText> xText3 = xDocument->createTextNode(u"foobar"_ustr);
687 xText->appendChild(nullptr);
688 CPPUNIT_FAIL("XText.appendChild(null)");
690 catch (css::uno::RuntimeException&)
696 xText->appendChild(xText2);
697 CPPUNIT_FAIL("XText.appendChild(xText2)");
699 catch (xml::dom::DOMException& e)
701 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_HIERARCHY_REQUEST_ERR, e.Code);
706 xText->insertBefore(xText2, xText3);
707 CPPUNIT_FAIL("XText.insertBefore");
709 catch (xml::dom::DOMException&)
715 xText->replaceChild(xText2, xText3);
716 CPPUNIT_FAIL("XText.replaceChild");
718 catch (xml::dom::DOMException&)
724 xText->removeChild(nullptr);
725 CPPUNIT_FAIL("XText.removeChild(null)");
727 catch (css::uno::RuntimeException&)
733 xText->removeChild(xText2);
734 CPPUNIT_FAIL("XText.removeChild");
736 catch (xml::dom::DOMException& e)
738 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_HIERARCHY_REQUEST_ERR, e.Code);
743 void testXProcessingInstruction()
745 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
746 CPPUNIT_ASSERT(xDocument);
747 Reference< xml::dom::XProcessingInstruction > xPI = xDocument->createProcessingInstruction(u"foo"_ustr, u"bar"_ustr);
748 CPPUNIT_ASSERT(xPI);
750 CPPUNIT_ASSERT_EQUAL(u"foo"_ustr, xPI->getTarget());
751 CPPUNIT_ASSERT_EQUAL(u"bar"_ustr, xPI->getData());
753 xPI->setData(u"baz"_ustr);
754 CPPUNIT_ASSERT_EQUAL(u"baz"_ustr, xPI->getData());
757 uno::Reference< xml::dom::XProcessingInstruction> xPIClone( xPI->cloneNode(false), uno::UNO_QUERY_THROW );
758 CPPUNIT_ASSERT(xPIClone);
759 CPPUNIT_ASSERT(!xPIClone->hasChildNodes());
762 uno::Reference< xml::dom::XProcessingInstruction> xPIClone( xPI->cloneNode(true), uno::UNO_QUERY_THROW );
763 CPPUNIT_ASSERT(xPIClone);
764 CPPUNIT_ASSERT(!xPIClone->hasChildNodes());
767 CPPUNIT_ASSERT(!xPI->getAttributes());
769 uno::Reference<xml::dom::XNodeList> xChildList = xPI->getChildNodes();
770 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xChildList->getLength());
772 CPPUNIT_ASSERT_EQUAL(u""_ustr, xPI->getLocalName());
773 CPPUNIT_ASSERT_EQUAL(u""_ustr, xPI->getNamespaceURI());
774 CPPUNIT_ASSERT(!xPI->getNextSibling());
775 CPPUNIT_ASSERT_EQUAL(u"foo"_ustr, xPI->getNodeName());
776 CPPUNIT_ASSERT_EQUAL(xml::dom::NodeType::NodeType_PROCESSING_INSTRUCTION_NODE, xPI->getNodeType());
777 CPPUNIT_ASSERT_EQUAL(u"baz"_ustr, xPI->getNodeValue());
778 CPPUNIT_ASSERT_EQUAL(xDocument, xPI->getOwnerDocument());
779 CPPUNIT_ASSERT(!xPI->getParentNode());
780 CPPUNIT_ASSERT_EQUAL(u""_ustr, xPI->getPrefix());
781 CPPUNIT_ASSERT(!xPI->getPreviousSibling());
782 CPPUNIT_ASSERT(!xPI->hasAttributes());
783 CPPUNIT_ASSERT(!xPI->hasChildNodes());
784 CPPUNIT_ASSERT(!xPI->isSupported(u"frobnication"_ustr, u"v99.33.0.0.0.1"_ustr));
786 xPI->normalize();
787 xPI->setNodeValue(u"42"_ustr);
788 CPPUNIT_ASSERT_EQUAL(u"42"_ustr, xPI->getNodeValue());
792 xPI->setPrefix(u"foo"_ustr);
793 CPPUNIT_FAIL("XProcessingInstruction.setPrefix()");
795 catch (xml::dom::DOMException& e)
797 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_NO_MODIFICATION_ALLOWED_ERR, e.Code);
800 uno::Reference<xml::dom::XText> xText2 = xDocument->createTextNode(u"foobar"_ustr);
801 uno::Reference<xml::dom::XText> xText3 = xDocument->createTextNode(u"foobar"_ustr);
805 xPI->appendChild(nullptr);
806 CPPUNIT_FAIL("XProcessingInstruction.appendChild(null)");
808 catch (css::uno::RuntimeException&)
814 xPI->appendChild(xText2);
815 CPPUNIT_FAIL("XProcessingInstruction.appendChild(xText2)");
817 catch (xml::dom::DOMException& e)
819 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_HIERARCHY_REQUEST_ERR, e.Code);
824 xPI->insertBefore(xText2, xText3);
825 CPPUNIT_FAIL("XProcessingInstruction.insertBefore");
827 catch (xml::dom::DOMException&)
833 xPI->replaceChild(xText2, xText3);
834 CPPUNIT_FAIL("XProcessingInstruction.replaceChild");
836 catch (xml::dom::DOMException&)
842 xPI->removeChild(nullptr);
843 CPPUNIT_FAIL("XProcessingInstruction.removeChild(null)");
845 catch (css::uno::RuntimeException&)
851 xPI->removeChild(xText2);
852 CPPUNIT_FAIL("XProcessingInstruction.removeChild");
854 catch (xml::dom::DOMException& e)
856 CPPUNIT_ASSERT_EQUAL(xml::dom::DOMExceptionType::DOMExceptionType_HIERARCHY_REQUEST_ERR, e.Code);
860 void testXNamedNodeMap_AttributesMap()
862 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
863 CPPUNIT_ASSERT(xDocument);
865 Reference< xml::dom::XElement > xElem = xDocument->createElement(u"foo"_ustr);
866 Reference< xml::dom::XNamedNodeMap > xAttributes = xElem->getAttributes();
867 CPPUNIT_ASSERT(xAttributes);
868 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xAttributes->getLength());
870 // Should it throw an exception ?
871 CPPUNIT_ASSERT(!xAttributes->item(4));
873 xElem->setAttribute(u"bar"_ustr, u"42"_ustr);
874 Reference< xml::dom::XAttr > xAttBar = xElem->getAttributeNode(u"bar"_ustr);
875 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xAttributes->getLength());
876 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
877 Reference< XInterface >(xAttributes->item(0), uno::UNO_QUERY));
878 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
879 Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
881 OUString aNS(u"http://example.com/"_ustr);
883 xElem->setAttributeNS(aNS, u"n:bar"_ustr, u"43"_ustr);
884 Reference< xml::dom::XAttr > xAttBarNS = xElem->getAttributeNodeNS(aNS, u"bar"_ustr);
885 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xAttributes->getLength());
886 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
887 Reference< XInterface >(xAttributes->item(0), uno::UNO_QUERY));
888 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBarNS, uno::UNO_QUERY),
889 Reference< XInterface >(xAttributes->item(1), uno::UNO_QUERY));
890 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
891 Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
892 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBarNS, uno::UNO_QUERY),
893 Reference< XInterface >(xAttributes->getNamedItemNS(aNS, u"bar"_ustr), uno::UNO_QUERY));
895 uno::Reference<xml::dom::XNode> xAttrBarNsRem = xAttributes->removeNamedItemNS(aNS, u"bar"_ustr);
896 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xAttributes->getLength());
897 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
898 Reference< XInterface >(xAttributes->item(0), uno::UNO_QUERY));
899 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttBar, uno::UNO_QUERY),
900 Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
901 CPPUNIT_ASSERT(!xAttrBarNsRem->getParentNode());
903 uno::Reference<xml::dom::XNode> xAttrBarRem = xAttributes->removeNamedItem(u"bar"_ustr);
904 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xAttributes->getLength());
905 CPPUNIT_ASSERT(!xAttrBarRem->getParentNode());
907 uno::Reference<xml::dom::XNode> xAttrBarSet = xAttributes->setNamedItem(xAttrBarRem);
908 CPPUNIT_ASSERT(xAttrBarSet);
909 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttrBarSet, uno::UNO_QUERY),
910 Reference< XInterface >(xAttributes->getNamedItem(u"bar"_ustr), uno::UNO_QUERY));
912 uno::Reference<xml::dom::XNode> xAttrBarNsSet = xAttributes->setNamedItemNS(xAttrBarNsRem);
913 CPPUNIT_ASSERT(xAttrBarNsSet);
914 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xAttrBarNsSet, uno::UNO_QUERY),
915 Reference< XInterface >(xAttributes->getNamedItemNS(aNS, u"bar"_ustr), uno::UNO_QUERY));
916 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xAttributes->getLength());
919 void testXNodeList_ChildList()
921 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
922 CPPUNIT_ASSERT(xDocument);
924 Reference< xml::dom::XElement > xRoot = xDocument->createElement(u"root"_ustr);
925 Reference< xml::dom::XElement > xFoo = xDocument->createElement(u"foo"_ustr);
926 Reference< xml::dom::XElement > xBar = xDocument->createElement(u"bar"_ustr);
927 Reference< xml::dom::XElement > xBaz = xDocument->createElement(u"baz"_ustr);
929 xDocument->appendChild(xRoot);
931 uno::Reference<xml::dom::XNodeList> xChildList = xRoot->getChildNodes();
932 CPPUNIT_ASSERT(xChildList);
933 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xChildList->getLength());
935 // Should it throw an exception ?
936 CPPUNIT_ASSERT(!xChildList->item(4));
938 xRoot->appendChild(xFoo);
939 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xChildList->getLength());
940 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo, uno::UNO_QUERY),
941 Reference< XInterface >(xChildList->item(0), uno::UNO_QUERY));
943 xRoot->appendChild(xBar);
944 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xChildList->getLength());
945 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo, uno::UNO_QUERY),
946 Reference< XInterface >(xChildList->item(0), uno::UNO_QUERY));
947 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xBar, uno::UNO_QUERY),
948 Reference< XInterface >(xChildList->item(1), uno::UNO_QUERY));
950 xRoot->appendChild(xBaz);
951 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), xChildList->getLength());
952 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo, uno::UNO_QUERY),
953 Reference< XInterface >(xChildList->item(0), uno::UNO_QUERY));
954 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xBar, uno::UNO_QUERY),
955 Reference< XInterface >(xChildList->item(1), uno::UNO_QUERY));
956 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xBaz, uno::UNO_QUERY),
957 Reference< XInterface >(xChildList->item(2), uno::UNO_QUERY));
959 xRoot->removeChild(xBar);
960 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xChildList->getLength());
961 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo, uno::UNO_QUERY),
962 Reference< XInterface >(xChildList->item(0), uno::UNO_QUERY));
963 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xBaz, uno::UNO_QUERY),
964 Reference< XInterface >(xChildList->item(1), uno::UNO_QUERY));
967 void testXNodeList_NodeList()
969 uno::Reference<xml::xpath::XXPathAPI> xXPathAPI( getMultiServiceFactory()->createInstance(u"com.sun.star.xml.xpath.XPathAPI"_ustr), uno::UNO_QUERY_THROW );
971 Reference< xml::dom::XDocument > xDocument = mxDomBuilder->newDocument();
972 CPPUNIT_ASSERT(xDocument);
974 Reference< xml::dom::XElement > xRoot = xDocument->createElement(u"root"_ustr);
975 Reference< xml::dom::XElement > xFoo1 = xDocument->createElement(u"foo"_ustr);
976 Reference< xml::dom::XElement > xFoo2 = xDocument->createElement(u"foo"_ustr);
977 Reference< xml::dom::XElement > xFooNs = xDocument->createElementNS(u"http://example.com/"_ustr, u"ns:foo"_ustr);
978 Reference< xml::dom::XElement > xBar = xDocument->createElement(u"bar"_ustr);
980 xDocument->appendChild(xRoot);
981 xRoot->appendChild(xFoo1);
982 xFoo1->appendChild(xBar);
983 xBar->appendChild(xFoo2);
984 xRoot->appendChild(xFooNs);
986 uno::Reference<xml::xpath::XXPathObject> xResult = xXPathAPI->eval(xRoot, u"//foo"_ustr);
987 CPPUNIT_ASSERT(xResult);
988 CPPUNIT_ASSERT_EQUAL(xml::xpath::XPathObjectType_XPATH_NODESET, xResult->getObjectType());
989 uno::Reference<xml::dom::XNodeList> xNodeList = xResult->getNodeList();
990 CPPUNIT_ASSERT(xNodeList);
991 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xNodeList->getLength());
992 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo1, uno::UNO_QUERY),
993 Reference< XInterface >(xNodeList->item(0), uno::UNO_QUERY));
994 CPPUNIT_ASSERT_EQUAL(Reference< XInterface >(xFoo2, uno::UNO_QUERY),
995 Reference< XInterface >(xNodeList->item(1), uno::UNO_QUERY));
998 void serializerTest ()
1000 rtl::Reference<DocumentHandler> xHandler = new DocumentHandler;
1001 rtl::Reference<TokenHandler> xTokHandler = new TokenHandler;
1002 uno::Sequence< beans::Pair< OUString, sal_Int32 > > aRegisteredNamespaces = {
1003 beans::make_Pair(
1004 u"urn:oasis:names:tc:opendocument:xmlns:office:1.0"_ustr,
1005 xml::sax::FastToken::NAMESPACE),
1006 beans::make_Pair(
1007 u"http://www.w3.org/1999/xlink"_ustr,
1008 2*xml::sax::FastToken::NAMESPACE)
1013 uno::Reference< xml::dom::XDocument > xDoc =
1014 mxDomBuilder->parse(mxValidInStream);
1015 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument",
1016 xDoc.is());
1017 CPPUNIT_ASSERT_MESSAGE("Valid input file resulted in parse errors",
1018 mxErrHandler->noErrors());
1020 uno::Reference< xml::sax::XSAXSerializable > xSaxSerializer(
1021 xDoc, uno::UNO_QUERY);
1022 CPPUNIT_ASSERT_MESSAGE("XSAXSerializable not supported",
1023 xSaxSerializer.is());
1025 uno::Reference< xml::sax::XFastSAXSerializable > xFastSaxSerializer(
1026 xDoc, uno::UNO_QUERY);
1027 CPPUNIT_ASSERT_MESSAGE("XFastSAXSerializable not supported",
1028 xSaxSerializer.is());
1030 xFastSaxSerializer->fastSerialize(xHandler,
1031 xTokHandler,
1032 uno::Sequence< beans::StringPair >(),
1033 aRegisteredNamespaces);
1035 catch (const css::xml::sax::SAXParseException&)
1037 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument (exception thrown)", false);
1041 CPPUNIT_TEST_SUITE(BasicTest);
1042 CPPUNIT_TEST(validInputTest);
1043 CPPUNIT_TEST(warningInputTest);
1044 CPPUNIT_TEST(errorInputTest);
1045 CPPUNIT_TEST(testXDocumentBuilder);
1046 CPPUNIT_TEST(testXXPathAPI);
1047 CPPUNIT_TEST(testXXPathObject);
1048 CPPUNIT_TEST(testXText);
1049 CPPUNIT_TEST(testXProcessingInstruction);
1050 CPPUNIT_TEST(testXNamedNodeMap_AttributesMap);
1051 CPPUNIT_TEST(testXNodeList_ChildList);
1052 CPPUNIT_TEST(testXNodeList_NodeList);
1053 CPPUNIT_TEST(serializerTest);
1054 CPPUNIT_TEST_SUITE_END();
1057 CPPUNIT_TEST_SUITE_REGISTRATION(BasicTest);
1060 CPPUNIT_PLUGIN_IMPLEMENT();
1062 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */