Fix memory leaks in test code

Change-Id: Ifa756a5da636e0b37654ad83900115872bc40f0e
This commit is contained in:
Stephan Bergmann 2014-06-02 15:45:28 +02:00
parent 7e4a51ee50
commit f87859b88f
4 changed files with 48 additions and 23 deletions

View file

@ -36,7 +36,8 @@ protected:
virtual void registerNamespaces(xmlXPathContextPtr& pXmlXpathCtx); virtual void registerNamespaces(xmlXPathContextPtr& pXmlXpathCtx);
xmlNodeSetPtr getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath); // Caller must call xmlXPathFreeObject:
xmlXPathObjectPtr getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath);
/** /**
* Same as the assertXPath(), but don't assert: return the string instead. * Same as the assertXPath(), but don't assert: return the string instead.
*/ */

View file

@ -1541,7 +1541,8 @@ DECLARE_OOXMLEXPORT_TEST(testFontNameIsEmpty, "font-name-is-empty.docx")
xmlDocPtr pXmlFontTable = parseExport("word/fontTable.xml"); xmlDocPtr pXmlFontTable = parseExport("word/fontTable.xml");
if (!pXmlFontTable) if (!pXmlFontTable)
return; return;
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlFontTable, "/w:fonts/w:font"); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlFontTable, "/w:fonts/w:font");
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
sal_Int32 length = xmlXPathNodeSetGetLength(pXmlNodes); sal_Int32 length = xmlXPathNodeSetGetLength(pXmlNodes);
for(sal_Int32 index = 0; index < length; index++){ for(sal_Int32 index = 0; index < length; index++){
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[index]; xmlNodePtr pXmlNode = pXmlNodes->nodeTab[index];
@ -1550,6 +1551,7 @@ DECLARE_OOXMLEXPORT_TEST(testFontNameIsEmpty, "font-name-is-empty.docx")
CPPUNIT_FAIL("Font name is empty."); CPPUNIT_FAIL("Font name is empty.");
} }
} }
xmlXPathFreeObject(pXmlObj);
} }
DECLARE_OOXMLEXPORT_TEST(testMultiColumnLineSeparator, "multi-column-line-separator-SAVED.docx") DECLARE_OOXMLEXPORT_TEST(testMultiColumnLineSeparator, "multi-column-line-separator-SAVED.docx")
@ -2816,10 +2818,12 @@ DECLARE_OOXMLEXPORT_TEST(testGenericTextField, "Unsupportedtextfields.docx")
xmlDocPtr pXmlDoc = parseExport(); xmlDocPtr pXmlDoc = parseExport();
if (!pXmlDoc) if (!pXmlDoc)
return; return;
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc,"/w:document/w:body/w:p[2]/w:r[2]/w:instrText"); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc,"/w:document/w:body/w:p[2]/w:r[2]/w:instrText");
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
OUString contents = OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)); OUString contents = OUString::createFromAscii((const char*)((pXmlNode->children[0]).content));
CPPUNIT_ASSERT(contents.match("PRINTDATE \\* MERGEFORMAT")); CPPUNIT_ASSERT(contents.match("PRINTDATE \\* MERGEFORMAT"));
xmlXPathFreeObject(pXmlObj);
} }
DECLARE_OOXMLEXPORT_TEST(testDateControl, "date-control.docx") DECLARE_OOXMLEXPORT_TEST(testDateControl, "date-control.docx")
@ -2966,12 +2970,14 @@ DECLARE_OOXMLEXPORT_TEST(testCitation,"FDO74775.docx")
xmlDocPtr pXmlDoc = parseExport(); xmlDocPtr pXmlDoc = parseExport();
if (!pXmlDoc) if (!pXmlDoc)
return; return;
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc,"/w:document/w:body/w:sdt/w:sdtContent/w:p[1]/w:r[3]/w:instrText"); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc,"/w:document/w:body/w:sdt/w:sdtContent/w:p[1]/w:r[3]/w:instrText");
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
CPPUNIT_ASSERT(pXmlNodes != 0); CPPUNIT_ASSERT(pXmlNodes != 0);
CPPUNIT_ASSERT(pXmlNodes->nodeNr > 0); CPPUNIT_ASSERT(pXmlNodes->nodeNr > 0);
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
OUString contents = OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)); OUString contents = OUString::createFromAscii((const char*)((pXmlNode->children[0]).content));
CPPUNIT_ASSERT(contents.match(" CITATION [Kra06]")); CPPUNIT_ASSERT(contents.match(" CITATION [Kra06]"));
xmlXPathFreeObject(pXmlObj);
} }
DECLARE_OOXMLEXPORT_TEST(testFdo76016, "fdo76016.docx") DECLARE_OOXMLEXPORT_TEST(testFdo76016, "fdo76016.docx")

View file

@ -1519,8 +1519,10 @@ DECLARE_OOXMLEXPORT_TEST(testFdo76101, "fdo76101.docx")
if (!pXmlDoc) if (!pXmlDoc)
return; return;
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, "/w:styles/w:style"); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, "/w:styles/w:style");
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
CPPUNIT_ASSERT(4091 >= xmlXPathNodeSetGetLength(pXmlNodes)); CPPUNIT_ASSERT(4091 >= xmlXPathNodeSetGetLength(pXmlNodes));
xmlXPathFreeObject(pXmlObj);
} }
DECLARE_OOXMLEXPORT_TEST(testSdtAndShapeOverlapping,"ShapeOverlappingWithSdt.docx") DECLARE_OOXMLEXPORT_TEST(testSdtAndShapeOverlapping,"ShapeOverlappingWithSdt.docx")
@ -1561,22 +1563,23 @@ DECLARE_OOXMLEXPORT_TEST(testAbsolutePositionOffsetValue,"fdo78432.docx")
sal_Int32 IntMax = SAL_MAX_INT32; sal_Int32 IntMax = SAL_MAX_INT32;
xmlNodeSetPtr pXmlNodes[6]; xmlXPathObjectPtr pXmlObjs[6];
pXmlNodes[0] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[1]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionH[1]/wp:posOffset[1]"); pXmlObjs[0] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[1]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionH[1]/wp:posOffset[1]");
pXmlNodes[1] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[1]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionV[1]/wp:posOffset[1]"); pXmlObjs[1] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[1]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionV[1]/wp:posOffset[1]");
pXmlNodes[2] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[2]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionH[1]/wp:posOffset[1]"); pXmlObjs[2] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[2]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionH[1]/wp:posOffset[1]");
pXmlNodes[3] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[2]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionV[1]/wp:posOffset[1]"); pXmlObjs[3] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[2]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionV[1]/wp:posOffset[1]");
pXmlNodes[4] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[3]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionH[1]/wp:posOffset[1]"); pXmlObjs[4] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[3]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionH[1]/wp:posOffset[1]");
pXmlNodes[5] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[3]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionV[1]/wp:posOffset[1]"); pXmlObjs[5] = getXPathNode(pXmlDoc,"/w:document[1]/w:body[1]/w:p[1]/w:r[1]/mc:AlternateContent[3]/mc:Choice[1]/w:drawing[1]/wp:anchor[1]/wp:positionV[1]/wp:posOffset[1]");
for(sal_Int32 index = 0; index<6; ++index) for(sal_Int32 index = 0; index<6; ++index)
{ {
CPPUNIT_ASSERT(pXmlNodes[index] != 0); CPPUNIT_ASSERT(pXmlObjs[index]->nodesetval != 0);
xmlNodePtr pXmlNode = pXmlNodes[index]->nodeTab[0]; xmlNodePtr pXmlNode = pXmlObjs[index]->nodesetval->nodeTab[0];
OUString contents = OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)); OUString contents = OUString::createFromAscii((const char*)((pXmlNode->children[0]).content));
CPPUNIT_ASSERT( contents.toInt64() <= IntMax ); CPPUNIT_ASSERT( contents.toInt64() <= IntMax );
xmlXPathFreeObject(pXmlObjs[index]);
} }
} }

View file

@ -32,12 +32,13 @@ xmlDocPtr XmlTestTools::parseXmlStream(SvStream* pStream)
return xmlParseDoc(reinterpret_cast<xmlChar*>(pBuffer.get())); return xmlParseDoc(reinterpret_cast<xmlChar*>(pBuffer.get()));
} }
xmlNodeSetPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath) xmlXPathObjectPtr XmlTestTools::getXPathNode(xmlDocPtr pXmlDoc, const OString& rXPath)
{ {
xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc); xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc);
registerNamespaces(pXmlXpathCtx); registerNamespaces(pXmlXpathCtx);
xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx); xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(rXPath.getStr()), pXmlXpathCtx);
return pXmlXpathObj->nodesetval; xmlXPathFreeContext(pXmlXpathCtx);
return pXmlXpathObj;
} }
void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/) void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/)
@ -46,24 +47,32 @@ void XmlTestTools::registerNamespaces(xmlXPathContextPtr& /*pXmlXpathCtx*/)
OUString XmlTestTools::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute) OUString XmlTestTools::getXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute)
{ {
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
1, xmlXPathNodeSetGetLength(pXmlNodes)); 1, xmlXPathNodeSetGetLength(pXmlNodes));
if (rAttribute.isEmpty()) if (rAttribute.isEmpty())
return OUString(); return OUString();
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
return OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr()))); xmlChar * prop = xmlGetProp(pXmlNode, BAD_CAST(rAttribute.getStr()));
OUString s(OUString::createFromAscii((const char*)prop));
xmlFree(prop);
xmlXPathFreeObject(pXmlObj);
return s;
} }
OUString XmlTestTools::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath) OUString XmlTestTools::getXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath)
{ {
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
CPPUNIT_ASSERT_MESSAGE(OString("XPath '" + rXPath + "' not found").getStr(), CPPUNIT_ASSERT_MESSAGE(OString("XPath '" + rXPath + "' not found").getStr(),
xmlXPathNodeSetGetLength(pXmlNodes) > 0); xmlXPathNodeSetGetLength(pXmlNodes) > 0);
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
return OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)); OUString s(OUString::createFromAscii((const char*)((pXmlNode->children[0]).content)));
xmlXPathFreeObject(pXmlObj);
return s;
} }
void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue) void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const OString& rAttribute, const OUString& rExpectedValue)
@ -75,9 +84,11 @@ void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, const O
void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes) void XmlTestTools::assertXPath(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfNodes)
{ {
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes)); nNumberOfNodes, xmlXPathNodeSetGetLength(pXmlNodes));
xmlXPathFreeObject(pXmlObj);
} }
void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent) void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rContent)
@ -87,17 +98,20 @@ void XmlTestTools::assertXPathContent(xmlDocPtr pXmlDoc, const OString& rXPath,
void XmlTestTools::assertXPathChildren(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfChildNodes) void XmlTestTools::assertXPathChildren(xmlDocPtr pXmlDoc, const OString& rXPath, int nNumberOfChildNodes)
{ {
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
1, xmlXPathNodeSetGetLength(pXmlNodes)); 1, xmlXPathNodeSetGetLength(pXmlNodes));
xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of child-nodes is incorrect").getStr(), CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of child-nodes is incorrect").getStr(),
nNumberOfChildNodes, (int)xmlChildElementCount(pXmlNode)); nNumberOfChildNodes, (int)xmlChildElementCount(pXmlNode));
xmlXPathFreeObject(pXmlObj);
} }
int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName) int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, const OUString& rChildName)
{ {
xmlNodeSetPtr pXmlNodes = getXPathNode(pXmlDoc, rXPath); xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc, rXPath);
xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(), CPPUNIT_ASSERT_EQUAL_MESSAGE(OString("XPath '" + rXPath + "' number of nodes is incorrect").getStr(),
1, 1,
xmlXPathNodeSetGetLength(pXmlNodes)); xmlXPathNodeSetGetLength(pXmlNodes));
@ -109,6 +123,7 @@ int XmlTestTools::getXPathPosition(xmlDocPtr pXmlDoc, const OString& rXPath, con
break; break;
++nRet; ++nRet;
} }
xmlXPathFreeObject(pXmlObj);
return nRet; return nRet;
} }