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>
2014-05-15 16:04:47 +02:00
# include <boost/scoped_array.hpp>
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
{
SvFileStream aFileStream ( aTempFile . GetURL ( ) , STREAM_READ ) ;
return parseXmlStream ( & aFileStream ) ;
}
xmlDocPtr XmlTestTools : : parseXmlStream ( SvStream * pStream )
{
sal_Size nSize = pStream - > remainingSize ( ) ;
boost : : scoped_array < sal_uInt8 > pBuffer ( new sal_uInt8 [ nSize + 1 ] ) ;
pStream - > Read ( pBuffer . get ( ) , nSize ) ;
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 ( ) ) ) ;
OUString s ( OUString : : createFromAscii ( ( const char * ) prop ) ) ;
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-06-02 15:45:28 +02:00
OUString s ( OUString : : createFromAscii ( ( const char * ) ( ( pXmlNode - > children [ 0 ] ) . content ) ) ) ;
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 ) ;
}
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
}
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 )
{
if ( OUString : : createFromAscii ( ( const char * ) pChild - > name ) = = rChildName )
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: */