fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / test / source / xmltesttools.cxx
blob749e87e89cf277795d467ffe4bff289975197e7d
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/.
8 */
10 #include <test/xmltesttools.hxx>
12 #include <boost/scoped_array.hpp>
14 namespace {
16 OUString convert(xmlChar const * string) {
17 OUString s;
18 CPPUNIT_ASSERT_MESSAGE(
19 "xmlChar string is not UTF-8",
20 rtl_convertStringToUString(
21 &s.pData, reinterpret_cast<char const *>(string), xmlStrlen(string),
22 RTL_TEXTENCODING_UTF8,
23 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
24 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
25 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
26 return s;
31 XmlTestTools::XmlTestTools()
34 XmlTestTools::~XmlTestTools()
37 xmlDocPtr XmlTestTools::parseXml(utl::TempFile& aTempFile)
39 SvFileStream aFileStream(aTempFile.GetURL(), StreamMode::READ);
40 return parseXmlStream(&aFileStream);
43 xmlDocPtr XmlTestTools::parseXmlStream(SvStream* pStream)
45 sal_Size nSize = pStream->remainingSize();
46 boost::scoped_array<sal_uInt8> pBuffer(new sal_uInt8[nSize + 1]);
47 pStream->Read(pBuffer.get(), nSize);
48 pBuffer[nSize] = 0;
49 return xmlParseDoc(reinterpret_cast<xmlChar*>(pBuffer.get()));
52 xmlXPathObjectPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath)
54 xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc);
55 registerNamespaces(pXmlXpathCtx);
56 xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx);
57 xmlXPathFreeContext(pXmlXpathCtx);
58 return pXmlXpathObj;
61 void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/)
65 OUString XmlTestTools::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
67 xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
68 xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
69 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
70 1, xmlXPathNodeSetGetLength(pXmlNodes));
71 if (rAttribute.isEmpty())
72 return OUString();
73 xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
74 xmlChar * prop = xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr()));
75 OUString s(convert(prop));
76 xmlFree(prop);
77 xmlXPathFreeObject(pXmlObj);
78 return s;
81 OUString XmlTestTools::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath)
83 xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
84 xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
86 CPPUNIT_ASSERT_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' not found").getStr(),
87 xmlXPathNodeSetGetLength(pXmlNodes) > 0);
89 xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
90 OUString s(convert((pXmlNode->children[0]).content));
91 xmlXPathFreeObject(pXmlObj);
92 return s;
95 void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue)
97 OUString aValue = getXPath(pXmlDoc, rXPath, rAttribute);
98 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, attribute '" + rAttribute + "' of '" + rXPath + "' incorrect value.").getStr(),
99 rExpectedValue, aValue);
102 void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes)
104 xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
105 xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
106 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
107 nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes));
108 xmlXPathFreeObject(pXmlObj);
111 void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent)
113 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath contents of child does not match").getStr(), rContent, getXPathContent(pXmlDoc, rXPath));
116 void XmlTestTools::assertXPathChildren(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfChildNodes)
118 #if LIBXML_VERSION >= 20703 /* xmlChildElementCount is only available in libxml2 >= 2.7.3 */
119 xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
120 xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
121 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
122 1, xmlXPathNodeSetGetLength(pXmlNodes));
123 xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
124 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of child-nodes is incorrect").getStr(),
125 nNumberOfChildNodes, (int)xmlChildElementCount(pXmlNode));
126 xmlXPathFreeObject(pXmlObj);
127 #else
128 (void)pXmlDoc;
129 (void)rXPath;
130 (void)nNumberOfChildNodes;
131 #endif
134 void XmlTestTools::assertXPathNoAttribute(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
136 xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
137 xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
138 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
139 1, xmlXPathNodeSetGetLength(pXmlNodes));
140 xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
141 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' unexpected '" + rAttribute + "' attribute").getStr(),
142 static_cast<xmlChar*>(0), xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr())));
143 xmlXPathFreeObject(pXmlObj);
146 int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName)
148 xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
149 xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
150 CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
152 xmlXPathNodeSetGetLength(pXmlNodes));
153 xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
154 int nRet = 0;
155 for (xmlNodePtr pChild = pXmlNode->children; pChild; pChild = pChild->next)
157 if (convert(pChild->name) == rChildName)
158 break;
159 ++nRet;
161 xmlXPathFreeObject(pXmlObj);
162 return nRet;
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */