def test_mergeXMLDocs(): # test without a namespace parentXML = libxml2.parseDoc("<a><b/></a>") childXML = libxml2.parseDoc("<c>Hello World</c>") expectedXML = libxml2.parseDoc("<a><b><c>Hello World</c></b></a>") xpath = "//b" foxml = fedoraObject(fedoraObject.inactiveState, "test:99", "test import object", "VITAL2") mergedXML = foxml.mergeXMLDocs(parentXML, childXML, xpath, None) assert diff_util.sameXml(mergedXML.serialize(), expectedXML.serialize()) #Test with non ascii characters parentXML = libxml2.parseDoc("<a><b/></a>") unicodeXML = libxml2.parseFile("../xml_data/umlaut.xml") expectedXML = libxml2.parseFile("../xml_data/merged-umlaut.xml") mergedXML = foxml.mergeXMLDocs(parentXML, unicodeXML, xpath, None) assert diff_util.sameXml(mergedXML.serialize(), expectedXML.serialize()) #Merge changes the parent doc! #assert diff_util.sameXml(parentXML.serialize(), expectedXML.serialize()) # test with a namespace foxml = fedoraObject(fedoraObject.inactiveState, "test:99", "test import object", "VITAL2") parentXML = libxml2.parseDoc(foxml.serialize()) xpathContext = parentXML.xpathNewContext() xpathContext.xpathRegisterNs( "foxml", "http://www.fedora.info/resources/faq.shtml#rdfproperties") childXML = libxml2.parseFile("../xml_data/rubric_DC.xml") expectedXML = libxml2.parseFile("../xml_data/rubric_mergeXML_test.xml") expectedXML = libxml2.parseDoc(xml_normalize.normalizeXml(expectedXML)) xpath = "//foxml:digitalObject" mergedXML = foxml.mergeXMLDocs(parentXML, childXML, xpath, xpathContext) assert diff_util.sameXml(mergedXML.serialize(), expectedXML.serialize())
def test_bodyHtmlToXML(): targetBodyFile = open("xml_data/body_item.xml", 'r') targetBody = targetBodyFile.read() htmlFile2 = open("html_data/thompson-index.html", 'r') bodyHtml = htmlFile2.read() testThis2 = xml_util.xml(targetBody) resultBody = bodyHtmlToXML(bodyHtml) assert diff_util.sameXml(targetBody,resultBody)
def test_htmlToXML(): targetFile = open("xml_data/meta_lowercase.xml", 'r') target = targetFile.read() htmlFile = open("html_data/thompson-index.html", 'r') html = htmlFile.read() testThis = xml_util.xml(target) result = htmlToXML(html) assert diff_util.sameXml(target, result)
def test_createDCXML(): htmlFile3 = open("html_data/thompson-index.html", 'r') fullHtml = htmlFile3.read() dcXML = createDCXML(fullHtml) targetFileDC = open("xml_data/DCMurdoch.xml", 'r') targetDC = targetFileDC.read() #testThis = xml_util.xml(targetDC) assert diff_util.sameXml(targetDC,dcXML)
def test_addDublinCore(): foxml = fedoraObject(fedoraObject.inactiveState, "test:99", "test import object", "VITAL2") dublinCore = libxml2.parseFile("../xml_data/rubric_DC.xml") #testxml = libxml2.parseFile("../xml_data/rubric_with_DC_A.xml") #testxml = libxml2.parseFile("../xml_data/rubric_with_DC_B.xml") testxml = libxml2.parseFile("../xml_data/rubric_with_DC_C.xml") foxml.addDublinCore(dublinCore, fedoraObject.inactiveState) assert diff_util.sameXml(foxml.serialize(), testxml.serialize())
def test_htmlToXML(): targetFile = open("../xml_data/meta_lowercase.xml", 'r') target = targetFile.read() htmlFile = open("../html_data/thompson-index.html", 'r') html = htmlFile.read() testThis = libxml2.parseDoc(target) result = htmlToXML(html, "PhD Doctorate") print result assert diff_util.sameXml(target, result)
def test_addCreator(): testXml = """<?xml version="1.0" encoding="UTF-8"?> <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd"> <dc:creator>James Bond</dc:creator> </oai_dc:dc> """ testXmlDoc = libxml2.parseDoc(testXml) testVitalDublinCore = dublin_core.vitalDublinCore() testVitalDublinCore.addCreator("James Bond") assert diff_util.sameXml(testVitalDublinCore.serialize(), testXmlDoc.serialize())
def test_addExternalDataStream(): foxml = fedoraObject(fedoraObject.inactiveState, "test:99", "test import object", "VITAL2") inlineXML = libxml2.parseFile("../xml_data/rubric_inlineXML.xml") dublinCore = libxml2.parseFile("../xml_data/rubric_DC.xml") testxml = libxml2.parseFile( "../xml_data/rubric_with_external_datastream.xml") foxml.addDublinCore(dublinCore, fedoraObject.inactiveState) foxml.addInlineXML("rubric", "rubric-inline", inlineXML, fedoraObject.inactiveState) foxml.addExternalDataStream("rubric-external", "application/pdf", "rubric-test-external-pdf", "http://localhost/test.pdf", fedoraObject.activeState) assert diff_util.sameXml(foxml.serialize(), testxml.serialize())
def test_Unicode_characters(): foxml = fedoraObject(fedoraObject.inactiveState, "test:99", "test import object", "VITAL") inlineXML = libxml2.parseFile("../xml_data/unicode_input.xml") dublinCore = libxml2.parseFile("../xml_data/rubric_vital_DC.xml") testxml = libxml2.parseFile("../xml_data/unicode_output.xml") foxml.addDublinCore(dublinCore, fedoraObject.inactiveState) foxml.addInlineXML("MARC", "MARCXML", inlineXML, fedoraObject.inactiveState) foxml.addManagedDataStream("DS1", "application/pdf", "Journal Article", "http://localhost/managed.pdf", fedoraObject.activeState) foxml.writeFile("/tmp/foxml_test_output.xml") assert diff_util.sameXml(foxml.serialize(), testxml.serialize()) assert os.path.exists("/tmp/foxml_test_output.xml")
def test_addAllNodes(): testXmlDoc = libxml2.parseFile("../xml_data/test_dublin_core.xml") testVitalDublinCore = dublin_core.vitalDublinCore() testVitalDublinCore.addTitle("This is a test title") testVitalDublinCore.addCreator("This is a test author") testVitalDublinCore.addSubject("This is a test subject") testVitalDublinCore.addDescription("This is a test description") testVitalDublinCore.addPublisher("This is a test publisher") testVitalDublinCore.addContributor("This is a test contributor") testVitalDublinCore.addDate("This is a test date") testVitalDublinCore.addType("This is a test type") testVitalDublinCore.addFormat("This is a test format") testVitalDublinCore.addIdentifier("This is a test identifier") testVitalDublinCore.addSource("This is a test source") testVitalDublinCore.addLanguage("This is a test language") testVitalDublinCore.addRelation("This is a test relation") testVitalDublinCore.addCoverage("This is a test coverage") testVitalDublinCore.addRights("This is a test right") assert diff_util.sameXml(testVitalDublinCore.serialize(), testXmlDoc.serialize())
def processTest(name, xsltSrc, xmlSrc, expectedXml): print "--- Test: '%s' ---" % name xslt = xslt_util.xslt(xsltSrc) r = "<root" for ns in nsList: r += " xmlns:" + ns[0] + '="' + ns[1] + '"\n' r += ">\n" r += xmlSrc + "\n</root>" xml = IceCommon.Xml(r, nsList) newXml = xml.applyXslt(xslt) xslt.close() xml.close() expected = expectedXml.replace("utfx:expected", "body").replace(' validate="no"', "") html = """<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"></meta> <title> Untitled </title> <style type="text/css" xml:space="preserve">table {border-spacing: 0;empty-cells: show;} </style> </head> <body> </body> </html>""" xml = IceCommon.Xml(expected, nsList) import diff_util same = diff_util.sameXml(newXml.getNode("//body"), xml.getNode("//body"), False) print "Same=", same if same is False: for node in newXml.getNode("//body").getChildren(): if node.getType() != "text": print node print for node in xml.getNode("//body").getChildren(): if node.getType() != "text": print node newXml.close() xml.close() print "--- Done ---\n"
def test_fedoraObject(): foxml = fedoraObject(fedoraObject.inactiveState, "test:99", "test import object", "VITAL2") testxml = libxml2.parseFile("../xml_data/rubric_base_foxml.xml") assert diff_util.sameXml(foxml.serialize(), testxml.serialize())