LCOV - code coverage report
Current view: top level - test/source - xmltesttools.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 89 92 96.7 %
Date: 2014-11-03 Functions: 15 17 88.2 %
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        2970 : XmlTestTools::XmlTestTools()
      15        2970 : {}
      16             : 
      17        2970 : XmlTestTools::~XmlTestTools()
      18        2970 : {}
      19             : 
      20           0 : xmlDocPtr XmlTestTools::parseXml(utl::TempFile& aTempFile)
      21             : {
      22           0 :     SvFileStream aFileStream(aTempFile.GetURL(), STREAM_READ);
      23           0 :     return parseXmlStream(&aFileStream);
      24             : }
      25             : 
      26         624 : xmlDocPtr XmlTestTools::parseXmlStream(SvStream* pStream)
      27             : {
      28         624 :     sal_Size nSize = pStream->remainingSize();
      29         624 :     boost::scoped_array<sal_uInt8> pBuffer(new sal_uInt8[nSize + 1]);
      30         624 :     pStream->Read(pBuffer.get(), nSize);
      31         624 :     pBuffer[nSize] = 0;
      32         624 :     return xmlParseDoc(reinterpret_cast<xmlChar*>(pBuffer.get()));
      33             : }
      34             : 
      35        2448 : xmlXPathObjectPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath)
      36             : {
      37        2448 :     xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc);
      38        2448 :     registerNamespaces(pXmlXpathCtx);
      39        2448 :     xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx);
      40        2448 :     xmlXPathFreeContext(pXmlXpathCtx);
      41        2448 :     return pXmlXpathObj;
      42             : }
      43             : 
      44         128 : void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/)
      45             : {
      46         128 : }
      47             : 
      48        1850 : OUString XmlTestTools::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
      49             : {
      50        1850 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
      51        1850 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
      52        3700 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
      53        1850 :                                  1, xmlXPathNodeSetGetLength(pXmlNodes));
      54        1850 :     if (rAttribute.isEmpty())
      55          34 :         return OUString();
      56        1816 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
      57        1816 :     xmlChar * prop = xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr()));
      58        1816 :     OUString s(OUString::createFromAscii((const char*)prop));
      59        1816 :     xmlFree(prop);
      60        1816 :     xmlXPathFreeObject(pXmlObj);
      61        1816 :     return s;
      62             : }
      63             : 
      64         100 : OUString XmlTestTools::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath)
      65             : {
      66         100 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
      67         100 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
      68             : 
      69         200 :     CPPUNIT_ASSERT_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' not found").getStr(),
      70         100 :             xmlXPathNodeSetGetLength(pXmlNodes) > 0);
      71             : 
      72         100 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
      73         100 :     OUString s(OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)));
      74         100 :     xmlXPathFreeObject(pXmlObj);
      75         100 :     return s;
      76             : }
      77             : 
      78        1640 : void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue)
      79             : {
      80        1640 :     OUString aValue = getXPath(pXmlDoc, rXPath, rAttribute);
      81        3280 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, attribute '" + rAttribute + "' of '" + rXPath + "' incorrect value.").getStr(),
      82        3280 :                                  rExpectedValue, aValue);
      83        1640 : }
      84             : 
      85         444 : void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes)
      86             : {
      87         444 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
      88         444 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
      89         888 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
      90         444 :                                  nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes));
      91         444 :     xmlXPathFreeObject(pXmlObj);
      92         444 : }
      93             : 
      94          84 : void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent)
      95             : {
      96          84 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath contents of child does not match").getStr(), rContent, getXPathContent(pXmlDoc, rXPath));
      97          84 : }
      98             : 
      99           2 : void XmlTestTools::assertXPathChildren(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfChildNodes)
     100             : {
     101             : #if LIBXML_VERSION >= 20703 /* xmlChildElementCount is only available in libxml2 >= 2.7.3 */
     102           2 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
     103           2 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
     104           4 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
     105           2 :                                  1, xmlXPathNodeSetGetLength(pXmlNodes));
     106           2 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
     107           4 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of child-nodes is incorrect").getStr(),
     108           2 :                                  nNumberOfChildNodes, (int)xmlChildElementCount(pXmlNode));
     109           2 :     xmlXPathFreeObject(pXmlObj);
     110             : #else
     111             :     (void)pXmlDoc;
     112             :     (void)rXPath;
     113             :     (void)nNumberOfChildNodes;
     114             : #endif
     115           2 : }
     116             : 
     117          10 : void XmlTestTools::assertXPathNoAttribute(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
     118             : {
     119          10 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
     120          10 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
     121          20 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
     122          10 :                                  1, xmlXPathNodeSetGetLength(pXmlNodes));
     123          10 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
     124          20 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' unexpected '" + rAttribute + "' attribute").getStr(),
     125          10 :                                  static_cast<xmlChar*>(0), xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr())));
     126          10 :     xmlXPathFreeObject(pXmlObj);
     127          10 : }
     128             : 
     129          22 : int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName)
     130             : {
     131          22 :     xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
     132          22 :     xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
     133          44 :     CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("In <" + OString(pXmlDoc->name) + ">, XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
     134             :                                  1,
     135          22 :                                  xmlXPathNodeSetGetLength(pXmlNodes));
     136          22 :     xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
     137          22 :     int nRet = 0;
     138          88 :     for (xmlNodePtr pChild = pXmlNode->children; pChild; pChild = pChild->next)
     139             :     {
     140          88 :         if (OUString::createFromAscii((const char*)pChild->name) == rChildName)
     141          22 :             break;
     142          66 :         ++nRet;
     143             :     }
     144          22 :     xmlXPathFreeObject(pXmlObj);
     145          22 :     return nRet;
     146         642 : }
     147             : 
     148             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10