1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <cppunit/extensions/HelperMacros.h>
11 #include <test/bootstrapfixture.hxx>
12 #include <rtl/ustring.hxx>
13 #include <tools/stream.hxx>
14 #include <tools/XmlWalker.hxx>
18 class XmlWalkerTest
: public test::BootstrapFixture
24 : BootstrapFixture(true, false)
28 virtual void setUp() override
{ maBasePath
= m_directories
.getURLFromSrc(u
"/tools/qa/data/"); }
32 CPPUNIT_TEST_SUITE(XmlWalkerTest
);
33 CPPUNIT_TEST(testReadXML
);
34 CPPUNIT_TEST_SUITE_END();
37 void XmlWalkerTest::testReadXML()
39 OUString aXmlFilePath
= maBasePath
+ "test.xml";
41 tools::XmlWalker aWalker
;
42 SvFileStream
aFileStream(aXmlFilePath
, StreamMode::READ
);
43 CPPUNIT_ASSERT(aWalker
.open(&aFileStream
));
44 CPPUNIT_ASSERT_EQUAL(std::string_view("root"), aWalker
.name());
45 CPPUNIT_ASSERT_EQUAL("Hello World"_ostr
, aWalker
.attribute("root-attr"_ostr
));
47 int nNumberOfChildNodes
= 0;
50 while (aWalker
.isValid())
52 if (aWalker
.name() == "child")
54 nNumberOfChildNodes
++;
56 CPPUNIT_ASSERT_EQUAL(OString(OString::number(nNumberOfChildNodes
)),
57 aWalker
.attribute("number"_ostr
));
59 if (nNumberOfChildNodes
== 1) // only the first node has the attribute
60 CPPUNIT_ASSERT_EQUAL("123"_ostr
, aWalker
.attribute("attribute"_ostr
));
62 CPPUNIT_ASSERT_EQUAL(OString(), aWalker
.attribute("attribute"_ostr
));
65 while (aWalker
.isValid())
67 if (aWalker
.name() == "grandchild")
69 CPPUNIT_ASSERT_EQUAL("ABC"_ostr
, aWalker
.attribute("attribute1"_ostr
));
70 CPPUNIT_ASSERT_EQUAL("CDE"_ostr
, aWalker
.attribute("attribute2"_ostr
));
71 CPPUNIT_ASSERT_EQUAL("Content"_ostr
, aWalker
.content());
77 else if (aWalker
.name() == "with-namespace")
79 CPPUNIT_ASSERT_EQUAL(std::string_view("adobe:ns:meta/"), aWalker
.namespaceHref());
80 CPPUNIT_ASSERT_EQUAL(std::string_view("xx"), aWalker
.namespacePrefix());
86 CPPUNIT_ASSERT_EQUAL(3, nNumberOfChildNodes
);
89 CPPUNIT_TEST_SUITE_REGISTRATION(XmlWalkerTest
);
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */