there is already a shared XPath test class
Change-Id: I3c94d1f8302aba52164fb27f40832415eb435a85
This commit is contained in:
@@ -40,77 +40,4 @@ xmlDocPtr XPathHelper::parseExport(ScDocShell* pShell, uno::Reference<lang::XMul
|
||||
return xmlParseMemory((const char*)aDocument.getStr(), aDocument.getLength());
|
||||
}
|
||||
|
||||
xmlNodeSetPtr XPathHelper::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath)
|
||||
{
|
||||
struct { xmlChar* pPrefix; xmlChar* pURI; } aNamespaces[] =
|
||||
{
|
||||
{ BAD_CAST("w"), BAD_CAST("http://schemas.openxmlformats.org/wordprocessingml/2006/main") },
|
||||
{ BAD_CAST("v"), BAD_CAST("urn:schemas-microsoft-com:vml") },
|
||||
{ BAD_CAST("c"), BAD_CAST("http://schemas.openxmlformats.org/drawingml/2006/chart") },
|
||||
{ BAD_CAST("a"), BAD_CAST("http://schemas.openxmlformats.org/drawingml/2006/main") },
|
||||
{ BAD_CAST("mc"), BAD_CAST("http://schemas.openxmlformats.org/markup-compatibility/2006") },
|
||||
{ BAD_CAST("wps"), BAD_CAST("http://schemas.microsoft.com/office/word/2010/wordprocessingShape") },
|
||||
{ BAD_CAST("wpg"), BAD_CAST("http://schemas.microsoft.com/office/word/2010/wordprocessingGroup") },
|
||||
{ BAD_CAST("wp"), BAD_CAST("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing") },
|
||||
{ BAD_CAST("office"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:office:1.0") },
|
||||
{ BAD_CAST("table"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:table:1.0") },
|
||||
{ BAD_CAST("text"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:text:1.0") },
|
||||
{ BAD_CAST("xlink"), BAD_CAST("http://www.w3c.org/1999/xlink") }
|
||||
};
|
||||
xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc);
|
||||
for(size_t i = 0; i < SAL_N_ELEMENTS(aNamespaces); ++i)
|
||||
{
|
||||
xmlXPathRegisterNs(pXmlXpathCtx, aNamespaces[i].pPrefix, aNamespaces[i].pURI );
|
||||
}
|
||||
|
||||
xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx);
|
||||
return pXmlXpathObj->nodesetval;
|
||||
}
|
||||
|
||||
void XPathHelper::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue)
|
||||
{
|
||||
OUString aValue = getXPath(pXmlDoc, rXPath, rAttribute);
|
||||
CPPUNIT_ASSERT_EQUAL_MESSAGE(
|
||||
OString("Attribute '" + rAttribute + "' of '" + rXPath + "' incorrect value.").getStr(),
|
||||
rExpectedValue, aValue);
|
||||
}
|
||||
|
||||
void XPathHelper::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes)
|
||||
{
|
||||
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
|
||||
CPPUNIT_ASSERT_EQUAL_MESSAGE(
|
||||
OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
|
||||
nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes));
|
||||
}
|
||||
|
||||
void XPathHelper::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent)
|
||||
{
|
||||
CPPUNIT_ASSERT_EQUAL_MESSAGE("XPath contents of child does not match", rContent, getXPathContent(pXmlDoc, rXPath));
|
||||
}
|
||||
|
||||
OUString XPathHelper::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath)
|
||||
{
|
||||
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
|
||||
|
||||
CPPUNIT_ASSERT_MESSAGE(OString("XPath '" + rXPath + "' not found").getStr(),
|
||||
xmlXPathNodeSetGetLength(pXmlNodes) > 0);
|
||||
|
||||
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
|
||||
|
||||
_xmlNode *pNode = &(pXmlNode->children[0]);
|
||||
return pNode ? OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)) : OUString();
|
||||
}
|
||||
|
||||
OUString XPathHelper::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
|
||||
{
|
||||
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath);
|
||||
CPPUNIT_ASSERT_EQUAL_MESSAGE(
|
||||
OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
|
||||
1, xmlXPathNodeSetGetLength(pXmlNodes));
|
||||
if (rAttribute.isEmpty())
|
||||
return OUString();
|
||||
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
|
||||
return OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr())));
|
||||
}
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||
|
@@ -25,7 +25,6 @@
|
||||
#endif
|
||||
|
||||
#include "docsh.hxx"
|
||||
#include "scdllapi.h"
|
||||
|
||||
using namespace com::sun::star;
|
||||
|
||||
@@ -39,37 +38,6 @@ namespace XPathHelper
|
||||
*/
|
||||
SCQAHELPER_DLLPUBLIC xmlDocPtr parseExport(ScDocShell* pShell, uno::Reference< lang::XMultiServiceFactory> xSFactory,
|
||||
const OUString& rFile, sal_Int32 nFormat);
|
||||
|
||||
/**
|
||||
* Helper method to return nodes represented by rXPath.
|
||||
*/
|
||||
SCQAHELPER_DLLPUBLIC xmlNodeSetPtr getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath);
|
||||
|
||||
/**
|
||||
* Assert that rXPath exists, and returns exactly one node.
|
||||
* In case rAttribute is provided, the rXPath's attribute's value must
|
||||
* equal to the rExpected value.
|
||||
*/
|
||||
SCQAHELPER_DLLPUBLIC void assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute = OString(), const OUString& rExpectedValue = OUString());
|
||||
|
||||
/**
|
||||
* Assert that rXPath exists, and returns exactly nNumberOfNodes nodes.
|
||||
* Useful for checking that we do _not_ export some node (nNumberOfNodes == 0).
|
||||
*/
|
||||
SCQAHELPER_DLLPUBLIC void assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes);
|
||||
|
||||
/**
|
||||
* Same as the assertXPath(), but don't assert: return the string instead.
|
||||
*/
|
||||
SCQAHELPER_DLLPUBLIC OUString getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute);
|
||||
/**
|
||||
Assert that rXPath exists, and its content equals rContent.
|
||||
*/
|
||||
SCQAHELPER_DLLPUBLIC void assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent);
|
||||
/**
|
||||
Same as the assertXPathContent(), but don't assert: return the string instead.
|
||||
*/
|
||||
SCQAHELPER_DLLPUBLIC OUString getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -50,13 +50,17 @@
|
||||
#include <editeng/udlnitem.hxx>
|
||||
#include <formula/grammar.hxx>
|
||||
|
||||
#include <test/xmltesttools.hxx>
|
||||
|
||||
#include <com/sun/star/table/BorderLineStyle.hpp>
|
||||
|
||||
using namespace ::com::sun::star;
|
||||
using namespace ::com::sun::star::uno;
|
||||
|
||||
class ScExportTest : public ScBootstrapFixture
|
||||
class ScExportTest : public ScBootstrapFixture, XmlTestTools
|
||||
{
|
||||
protected:
|
||||
virtual void registerNamespaces(xmlXPathContextPtr& pXmlXPathCtx);
|
||||
public:
|
||||
ScExportTest();
|
||||
|
||||
@@ -161,6 +165,29 @@ private:
|
||||
|
||||
};
|
||||
|
||||
void ScExportTest::registerNamespaces(xmlXPathContextPtr& pXmlXPathCtx)
|
||||
{
|
||||
struct { xmlChar* pPrefix; xmlChar* pURI; } aNamespaces[] =
|
||||
{
|
||||
{ BAD_CAST("w"), BAD_CAST("http://schemas.openxmlformats.org/wordprocessingml/2006/main") },
|
||||
{ BAD_CAST("v"), BAD_CAST("urn:schemas-microsoft-com:vml") },
|
||||
{ BAD_CAST("c"), BAD_CAST("http://schemas.openxmlformats.org/drawingml/2006/chart") },
|
||||
{ BAD_CAST("a"), BAD_CAST("http://schemas.openxmlformats.org/drawingml/2006/main") },
|
||||
{ BAD_CAST("mc"), BAD_CAST("http://schemas.openxmlformats.org/markup-compatibility/2006") },
|
||||
{ BAD_CAST("wps"), BAD_CAST("http://schemas.microsoft.com/office/word/2010/wordprocessingShape") },
|
||||
{ BAD_CAST("wpg"), BAD_CAST("http://schemas.microsoft.com/office/word/2010/wordprocessingGroup") },
|
||||
{ BAD_CAST("wp"), BAD_CAST("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing") },
|
||||
{ BAD_CAST("office"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:office:1.0") },
|
||||
{ BAD_CAST("table"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:table:1.0") },
|
||||
{ BAD_CAST("text"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:text:1.0") },
|
||||
{ BAD_CAST("xlink"), BAD_CAST("http://www.w3c.org/1999/xlink") }
|
||||
};
|
||||
for(size_t i = 0; i < SAL_N_ELEMENTS(aNamespaces); ++i)
|
||||
{
|
||||
xmlXPathRegisterNs(pXmlXPathCtx, aNamespaces[i].pPrefix, aNamespaces[i].pURI );
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined MACOSX && !defined DRAGONFLY
|
||||
ScDocShellRef ScExportTest::saveAndReloadPassword(ScDocShell* pShell, const OUString &rFilter,
|
||||
const OUString &rUserData, const OUString& rTypeName, sal_uLong nFormatType)
|
||||
@@ -1832,7 +1859,7 @@ void ScExportTest::testRelativePaths()
|
||||
|
||||
xmlDocPtr pDoc = XPathHelper::parseExport(&(*xDocSh), m_xSFactory, "content.xml", ODS);
|
||||
CPPUNIT_ASSERT(pDoc);
|
||||
OUString aURL = XPathHelper::getXPath(pDoc,
|
||||
OUString aURL = getXPath(pDoc,
|
||||
"/office:document-content/office:body/office:spreadsheet/table:table/table:table-row[2]/table:table-cell[2]/text:p/text:a", "href");
|
||||
// make sure that the URL is relative
|
||||
CPPUNIT_ASSERT(aURL.startsWith(".."));
|
||||
|
Reference in New Issue
Block a user