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 <test/xmltesttools.hxx>
12 #include <boost/scoped_array.hpp>
16 OUString
convert(xmlChar
const * string
) {
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
)));
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
);
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
);
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())
73 xmlNodePtr pXmlNode
= pXmlNodes
->nodeTab
[0];
74 xmlChar
* prop
= xmlGetProp(pXmlNode
, BAD_CAST(rAttribute
.getStr()));
75 OUString
s(convert(prop
));
77 xmlXPathFreeObject(pXmlObj
);
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
);
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
);
130 (void)nNumberOfChildNodes
;
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];
155 for (xmlNodePtr pChild
= pXmlNode
->children
; pChild
; pChild
= pChild
->next
)
157 if (convert(pChild
->name
) == rChildName
)
161 xmlXPathFreeObject(pXmlObj
);
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */