LCOV - code coverage report
Current view: top level - test/source - xmltesttools.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 97 97 100.0 %
Date: 2015-06-13 12:38:46 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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             :  */
       9             : 
      10             : #include <test/xmltesttools.hxx>
      11             : 
      12             : #include <boost/scoped_array.hpp>
      13             : 
      14             : namespace {
      15             : 
      16        1084 : OUString convert(xmlChar const * string) {
      17        1084 :     OUString s;
      18        2168 :     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        1084 :              | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
      26        1084 :     return s;
      27             : }
      28             : 
      29             : }
      30             : 
      31        1769 : XmlTestTools::XmlTestTools()
      32        1769 : {}
      33             : 
      34        1769 : XmlTestTools::~XmlTestTools()
      35        1769 : {}
      36             : 
      37           2 : xmlDocPtr XmlTestTools::parseXml(utl::TempFile& aTempFile)
      38             : {
      39           2 :     SvFileStream aFileStream(aTempFile.GetURL(), StreamMode::READ);
      40           2 :     return parseXmlStream(&aFileStream);
      41             : }
      42             : 
      43         374 : xmlDocPtr XmlTestTools::parseXmlStream(SvStream* pStream)
      44             : {
      45         374 :     sal_Size nSize = pStream->remainingSize();
      46         374 :     boost::scoped_array<sal_uInt8> pBuffer(new sal_uInt8[nSize + 1]);
      47         374 :     pStream->Read(pBuffer.get(), nSize);
      48         374 :     pBuffer[nSize] = 0;
      49         374 :     return xmlParseDoc(reinterpret_cast<xmlChar*>(pBuffer.get()));
      50             : }
      51             : 
      52        1329 : xmlXPathObjectPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath)
      53             : {
      54        1329 :     xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc);
      55        1329 :     registerNamespaces(pXmlXpathCtx);
      56        1329 :     xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx);
      57        1329 :     xmlXPathFreeContext(pXmlXpathCtx);
      58        1329 :     return pXmlXpathObj;
      59             : }
      60             : 
      61          57 : void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/)
      62             : {
      63          57 : }
      64             : 
      65        1002 : OUString XmlTestTools::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
      66             : {
      67        1002 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
      68        1002 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
      69        2004 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
      70        1002 :                                  1, xmlXPathNodeSetGetLength(pXmlNodes));
      71        1002 :     if (rAttribute.isEmpty())
      72          17 :         return OUString();
      73         985 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
      74         985 :     xmlChar * prop = xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr()));
      75         985 :     OUString s(convert(prop));
      76         985 :     xmlFree(prop);
      77         985 :     xmlXPathFreeObject(pXmlObj);
      78         985 :     return s;
      79             : }
      80             : 
      81          55 : OUString XmlTestTools::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath)
      82             : {
      83          55 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
      84          55 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
      85             : 
      86         110 :     CPPUNIT_ASSERT_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' not found").getStr(),
      87          55 :             xmlXPathNodeSetGetLength(pXmlNodes) > 0);
      88             : 
      89          55 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
      90          55 :     OUString s(convert((pXmlNode->children[0]).content));
      91          55 :     xmlXPathFreeObject(pXmlObj);
      92          55 :     return s;
      93             : }
      94             : 
      95         881 : void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue)
      96             : {
      97         881 :     OUString aValue = getXPath(pXmlDoc, rXPath, rAttribute);
      98        1762 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, attribute '" + rAttribute + "' of '" + rXPath + "' incorrect value.").getStr(),
      99        1762 :                                  rExpectedValue, aValue);
     100         881 : }
     101             : 
     102         241 : void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes)
     103             : {
     104         241 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
     105         241 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
     106         482 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
     107         241 :                                  nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes));
     108         241 :     xmlXPathFreeObject(pXmlObj);
     109         241 : }
     110             : 
     111          47 : void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent)
     112             : {
     113          47 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath contents of child does not match").getStr(), rContent, getXPathContent(pXmlDoc, rXPath));
     114          47 : }
     115             : 
     116           1 : void XmlTestTools::assertXPathChildren(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfChildNodes)
     117             : {
     118             : #if LIBXML_VERSION >= 20703 /* xmlChildElementCount is only available in libxml2 >= 2.7.3 */
     119           1 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
     120           1 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
     121           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
     122           1 :                                  1, xmlXPathNodeSetGetLength(pXmlNodes));
     123           1 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
     124           2 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of child-nodes is incorrect").getStr(),
     125           1 :                                  nNumberOfChildNodes, (int)xmlChildElementCount(pXmlNode));
     126           1 :     xmlXPathFreeObject(pXmlObj);
     127             : #else
     128             :     (void)pXmlDoc;
     129             :     (void)rXPath;
     130             :     (void)nNumberOfChildNodes;
     131             : #endif
     132           1 : }
     133             : 
     134           8 : void XmlTestTools::assertXPathNoAttribute(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
     135             : {
     136           8 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
     137           8 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
     138          16 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
     139           8 :                                  1, xmlXPathNodeSetGetLength(pXmlNodes));
     140           8 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
     141          16 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' unexpected '" + rAttribute + "' attribute").getStr(),
     142           8 :                                  static_cast<xmlChar*>(0), xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr())));
     143           8 :     xmlXPathFreeObject(pXmlObj);
     144           8 : }
     145             : 
     146          11 : int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName)
     147             : {
     148          11 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
     149          11 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
     150          22 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
     151             :                                  1,
     152          11 :                                  xmlXPathNodeSetGetLength(pXmlNodes));
     153          11 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
     154          11 :     int nRet = 0;
     155          44 :     for (xmlNodePtr pChild = pXmlNode->children; pChild; pChild = pChild->next)
     156             :     {
     157          44 :         if (convert(pChild->name) == rChildName)
     158          11 :             break;
     159          33 :         ++nRet;
     160             :     }
     161          11 :     xmlXPathFreeObject(pXmlObj);
     162          11 :     return nRet;
     163         372 : }
     164             : 
     165             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11