2014-04-29 15:07:54 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project .
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/.
*/
# include <test/xmltesttools.hxx>
2015-06-15 17:58:15 +09:00
# include <memory>
2014-05-15 16:04:47 +02:00
2014-12-18 12:30:30 +01:00
namespace {
OUString convert ( xmlChar const * string ) {
OUString s ;
CPPUNIT_ASSERT_MESSAGE (
" xmlChar string is not UTF-8 " ,
rtl_convertStringToUString (
& s . pData , reinterpret_cast < char const * > ( string ) , xmlStrlen ( string ) ,
RTL_TEXTENCODING_UTF8 ,
( RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
| RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
| RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR ) ) ) ;
return s ;
}
}
2014-04-29 15:07:54 +02:00
XmlTestTools : : XmlTestTools ( )
{ }
XmlTestTools : : ~ XmlTestTools ( )
{ }
2014-08-28 12:54:35 +02:00
xmlDocPtr XmlTestTools : : parseXml ( utl : : TempFile & aTempFile )
2014-05-15 16:04:47 +02:00
{
2015-01-07 09:28:42 +02:00
SvFileStream aFileStream ( aTempFile . GetURL ( ) , StreamMode : : READ ) ;
2014-05-15 16:04:47 +02:00
return parseXmlStream ( & aFileStream ) ;
}
xmlDocPtr XmlTestTools : : parseXmlStream ( SvStream * pStream )
{
2016-09-14 17:01:50 +02:00
std : : size_t nSize = pStream - > remainingSize ( ) ;
2015-06-15 17:58:15 +09:00
std : : unique_ptr < sal_uInt8 [ ] > pBuffer ( new sal_uInt8 [ nSize + 1 ] ) ;
2016-06-03 14:45:59 +02:00
pStream - > ReadBytes ( pBuffer . get ( ) , nSize ) ;
2014-05-15 16:04:47 +02:00
pBuffer [ nSize ] = 0 ;
return xmlParseDoc ( reinterpret_cast < xmlChar * > ( pBuffer . get ( ) ) ) ;
}
2014-06-02 15:45:28 +02:00
xmlXPathObjectPtr XmlTestTools : : getXPathNode ( xmlDocPtr pXmlDoc , const OString & rXPath )
2014-04-29 15:07:54 +02:00
{
xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext ( pXmlDoc ) ;
registerNamespaces ( pXmlXpathCtx ) ;
xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression ( BAD_CAST ( rXPath . getStr ( ) ) , pXmlXpathCtx ) ;
2014-06-02 15:45:28 +02:00
xmlXPathFreeContext ( pXmlXpathCtx ) ;
return pXmlXpathObj ;
2014-04-29 15:07:54 +02:00
}
void XmlTestTools : : registerNamespaces ( xmlXPathContextPtr & /*pXmlXpathCtx*/ )
{
}
OUString XmlTestTools : : getXPath ( xmlDocPtr pXmlDoc , const OString & rXPath , const OString & rAttribute )
{
2014-06-02 15:45:28 +02:00
xmlXPathObjectPtr pXmlObj = getXPathNode ( pXmlDoc , rXPath ) ;
xmlNodeSetPtr pXmlNodes = pXmlObj - > nodesetval ;
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' number of nodes is incorrect " ) . getStr ( ) ,
2014-04-29 15:07:54 +02:00
1 , xmlXPathNodeSetGetLength ( pXmlNodes ) ) ;
if ( rAttribute . isEmpty ( ) )
return OUString ( ) ;
xmlNodePtr pXmlNode = pXmlNodes - > nodeTab [ 0 ] ;
2014-06-02 15:45:28 +02:00
xmlChar * prop = xmlGetProp ( pXmlNode , BAD_CAST ( rAttribute . getStr ( ) ) ) ;
2014-12-18 12:30:30 +01:00
OUString s ( convert ( prop ) ) ;
2014-06-02 15:45:28 +02:00
xmlFree ( prop ) ;
xmlXPathFreeObject ( pXmlObj ) ;
return s ;
2014-04-29 15:07:54 +02:00
}
OUString XmlTestTools : : getXPathContent ( xmlDocPtr pXmlDoc , const OString & rXPath )
{
2014-06-02 15:45:28 +02:00
xmlXPathObjectPtr pXmlObj = getXPathNode ( pXmlDoc , rXPath ) ;
xmlNodeSetPtr pXmlNodes = pXmlObj - > nodesetval ;
2014-04-29 15:07:54 +02:00
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' not found " ) . getStr ( ) ,
2014-04-29 15:07:54 +02:00
xmlXPathNodeSetGetLength ( pXmlNodes ) > 0 ) ;
xmlNodePtr pXmlNode = pXmlNodes - > nodeTab [ 0 ] ;
2014-12-18 12:30:30 +01:00
OUString s ( convert ( ( pXmlNode - > children [ 0 ] ) . content ) ) ;
2014-06-02 15:45:28 +02:00
xmlXPathFreeObject ( pXmlObj ) ;
return s ;
2014-04-29 15:07:54 +02:00
}
void XmlTestTools : : assertXPath ( xmlDocPtr pXmlDoc , const OString & rXPath , const OString & rAttribute , const OUString & rExpectedValue )
{
OUString aValue = getXPath ( pXmlDoc , rXPath , rAttribute ) ;
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, attribute ' " + rAttribute + " ' of ' " + rXPath + " ' incorrect value. " ) . getStr ( ) ,
2014-04-29 15:07:54 +02:00
rExpectedValue , aValue ) ;
}
2017-01-09 23:46:25 +01:00
void XmlTestTools : : assertXPathAttrs ( xmlDocPtr pXmlDoc , const OString & rXPath , std : : vector < std : : pair < OString , OUString > > aPairVector )
{
for ( auto & rPair : aPairVector )
{
assertXPath ( pXmlDoc , rXPath , rPair . first , rPair . second ) ;
}
}
2014-04-29 15:07:54 +02:00
void XmlTestTools : : assertXPath ( xmlDocPtr pXmlDoc , const OString & rXPath , int nNumberOfNodes )
{
2014-06-02 15:45:28 +02:00
xmlXPathObjectPtr pXmlObj = getXPathNode ( pXmlDoc , rXPath ) ;
xmlNodeSetPtr pXmlNodes = pXmlObj - > nodesetval ;
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' number of nodes is incorrect " ) . getStr ( ) ,
2014-04-29 15:07:54 +02:00
nNumberOfNodes , xmlXPathNodeSetGetLength ( pXmlNodes ) ) ;
2014-06-02 15:45:28 +02:00
xmlXPathFreeObject ( pXmlObj ) ;
2014-04-29 15:07:54 +02:00
}
void XmlTestTools : : assertXPathContent ( xmlDocPtr pXmlDoc , const OString & rXPath , const OUString & rContent )
{
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath contents of child does not match " ) . getStr ( ) , rContent , getXPathContent ( pXmlDoc , rXPath ) ) ;
2014-04-29 15:07:54 +02:00
}
void XmlTestTools : : assertXPathChildren ( xmlDocPtr pXmlDoc , const OString & rXPath , int nNumberOfChildNodes )
{
2014-06-15 08:39:00 -04:00
# if LIBXML_VERSION >= 20703 /* xmlChildElementCount is only available in libxml2 >= 2.7.3 */
2014-06-02 15:45:28 +02:00
xmlXPathObjectPtr pXmlObj = getXPathNode ( pXmlDoc , rXPath ) ;
xmlNodeSetPtr pXmlNodes = pXmlObj - > nodesetval ;
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' number of nodes is incorrect " ) . getStr ( ) ,
2014-04-29 15:07:54 +02:00
1 , xmlXPathNodeSetGetLength ( pXmlNodes ) ) ;
xmlNodePtr pXmlNode = pXmlNodes - > nodeTab [ 0 ] ;
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' number of child-nodes is incorrect " ) . getStr ( ) ,
2014-04-29 15:07:54 +02:00
nNumberOfChildNodes , ( int ) xmlChildElementCount ( pXmlNode ) ) ;
2014-06-02 15:45:28 +02:00
xmlXPathFreeObject ( pXmlObj ) ;
2014-06-15 08:39:00 -04:00
# else
( void ) pXmlDoc ;
( void ) rXPath ;
( void ) nNumberOfChildNodes ;
# endif
2014-04-29 15:07:54 +02:00
}
2014-09-30 11:48:59 +02:00
void XmlTestTools : : assertXPathNoAttribute ( xmlDocPtr pXmlDoc , const OString & rXPath , const OString & rAttribute )
{
xmlXPathObjectPtr pXmlObj = getXPathNode ( pXmlDoc , rXPath ) ;
xmlNodeSetPtr pXmlNodes = pXmlObj - > nodesetval ;
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' number of nodes is incorrect " ) . getStr ( ) ,
1 , xmlXPathNodeSetGetLength ( pXmlNodes ) ) ;
xmlNodePtr pXmlNode = pXmlNodes - > nodeTab [ 0 ] ;
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' unexpected ' " + rAttribute + " ' attribute " ) . getStr ( ) ,
2015-11-10 10:26:03 +01:00
static_cast < xmlChar * > ( nullptr ) , xmlGetProp ( pXmlNode , BAD_CAST ( rAttribute . getStr ( ) ) ) ) ;
2014-09-30 11:48:59 +02:00
xmlXPathFreeObject ( pXmlObj ) ;
}
2014-04-29 15:07:54 +02:00
int XmlTestTools : : getXPathPosition ( xmlDocPtr pXmlDoc , const OString & rXPath , const OUString & rChildName )
{
2014-06-02 15:45:28 +02:00
xmlXPathObjectPtr pXmlObj = getXPathNode ( pXmlDoc , rXPath ) ;
xmlNodeSetPtr pXmlNodes = pXmlObj - > nodesetval ;
2014-09-23 11:19:23 +02:00
CPPUNIT_ASSERT_EQUAL_MESSAGE ( OString ( " In < " + OString ( pXmlDoc - > name ) + " >, XPath ' " + rXPath + " ' number of nodes is incorrect " ) . getStr ( ) ,
2014-04-29 15:07:54 +02:00
1 ,
xmlXPathNodeSetGetLength ( pXmlNodes ) ) ;
xmlNodePtr pXmlNode = pXmlNodes - > nodeTab [ 0 ] ;
int nRet = 0 ;
for ( xmlNodePtr pChild = pXmlNode - > children ; pChild ; pChild = pChild - > next )
{
2014-12-18 12:30:30 +01:00
if ( convert ( pChild - > name ) = = rChildName )
2014-04-29 15:07:54 +02:00
break ;
+ + nRet ;
}
2014-06-02 15:45:28 +02:00
xmlXPathFreeObject ( pXmlObj ) ;
2014-04-29 15:07:54 +02:00
return nRet ;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */