Search in sources :

Example 1 with ElementNameQualifier

use of org.custommonkey.xmlunit.ElementNameQualifier in project ddf by codice.

the class TestCswFilterDelegate method testRelative.

@Test
public void testRelative() throws JAXBException, SAXException, IOException {
    long duration = 92000000000L;
    CswSourceConfiguration cswSourceConfiguration = initCswSourceConfiguration(CswAxisOrder.LAT_LON, false, CswConstants.CSW_TYPE, effectiveDateMapping, createdDateMapping, modifiedDateMapping, CswConstants.CSW_IDENTIFIER);
    CswFilterDelegate localCswFilterDelegate = createCswFilterDelegate(cswSourceConfiguration);
    Map<String, Object> propMap = new HashMap<>();
    propMap.put("extendedComparisonOp", "relative");
    propMap.put("duration", new Long(duration));
    String xml = getXmlProperty(localCswFilterDelegate, propertyNameModified, BETWEEN, testStartDate.toCalendar(null).getTime(), testEndDate.toCalendar(null).getTime(), propMap);
    String durationCompare = duringXml.replace(REPLACE_START_DATE, "").replace(REPLACE_END_DATE, "").replace(REPLACE_TEMPORAL_PROPERTY, modifiedDateMapping);
    String pattern = "(?i)(<ns.:Literal>)(.+?)(</ns.:Literal>)";
    String compareXml = xml.replaceAll(pattern, "<ogc:Literal xmlns:ogc=\"http://www.opengis.net/ogc\"></ogc:Literal>");
    Diff xmlDiff = new Diff(durationCompare, compareXml);
    xmlDiff.overrideElementQualifier(new ElementNameQualifier() {

        @Override
        protected boolean equalsNamespace(Node control, Node test) {
            return true;
        }
    });
    xmlDiff.overrideDifferenceListener(new DifferenceListener() {

        @Override
        public int differenceFound(Difference diff) {
            if (diff.getId() == DifferenceConstants.NAMESPACE_PREFIX_ID) {
                return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
            return RETURN_ACCEPT_DIFFERENCE;
        }

        @Override
        public void skippedComparison(Node arg0, Node arg1) {
        }
    });
    assertXMLIdentical(xmlDiff, true);
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) HashMap(java.util.HashMap) Diff(org.custommonkey.xmlunit.Diff) Node(org.w3c.dom.Node) Difference(org.custommonkey.xmlunit.Difference) DifferenceListener(org.custommonkey.xmlunit.DifferenceListener) ElementNameQualifier(org.custommonkey.xmlunit.ElementNameQualifier) Test(org.junit.Test)

Example 2 with ElementNameQualifier

use of org.custommonkey.xmlunit.ElementNameQualifier in project gate-core by GateNLP.

the class TestCreoleAnnotationHandler method testCreoleAnnotationHandler.

/**
 * Take a skeleton creole.xml file, process the annotations on the classes it
 * mentions and compare the resulting XML to the expected result.
 */
public void testCreoleAnnotationHandler() throws Exception {
    URL originalUrl = new URL(TestDocument.getTestServerName() + "tests/creole-annotation-handler/initial-creole.xml");
    org.jdom.Document creoleXml = jdomBuilder.build(originalUrl.openStream());
    CreoleAnnotationHandler processor = new CreoleAnnotationHandler(new Plugin.Directory(new URL(TestDocument.getTestServerName() + "tests/creole-annotation-handler/")));
    processor.processAnnotations(creoleXml);
    URL expectedURL = new URL(TestDocument.getTestServerName() + "tests/creole-annotation-handler/expected-creole.xml");
    // XMLUnit requires the expected and actual results as W3C DOM rather than
    // JDOM
    DocumentBuilder docBuilder = jaxpFactory.newDocumentBuilder();
    org.w3c.dom.Document targetXmlDOM = docBuilder.parse(expectedURL.openStream());
    org.w3c.dom.Document actualXmlDOM = jdom2dom.output(creoleXml);
    Diff diff = XMLUnit.compareXML(targetXmlDOM, actualXmlDOM);
    // compare parameter elements with the same NAME, resources with the same
    // CLASS, and all other elements that have the same element name
    diff.overrideElementQualifier(new ElementNameQualifier() {

        @Override
        public boolean qualifyForComparison(Element control, Element test) {
            if ("PARAMETER".equals(control.getTagName()) && "PARAMETER".equals(test.getTagName())) {
                return control.getAttribute("NAME").equals(test.getAttribute("NAME"));
            } else if ("RESOURCE".equals(control.getTagName()) && "RESOURCE".equals(test.getTagName())) {
                String controlClass = findClass(control);
                String testClass = findClass(test);
                return (controlClass == null) ? (testClass == null) : controlClass.equals(testClass);
            } else {
                return super.qualifyForComparison(control, test);
            }
        }

        private String findClass(Element resource) {
            Node node = resource.getFirstChild();
            while (node != null && !"CLASS".equals(node.getNodeName())) {
                node = node.getNextSibling();
            }
            if (node != null) {
                return node.getTextContent();
            } else {
                return null;
            }
        }
    });
    // do the comparison!
    boolean match = diff.similar();
    if (!match) {
        // if comparison failed, output the "actual" result document for
        // debugging purposes
        System.err.println("---------actual-----------");
        new XMLOutputter(Format.getPrettyFormat()).output(creoleXml, System.err);
    }
    assertTrue("XML produced by annotation handler does not match expected: " + diff.toString(), match);
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) Diff(org.custommonkey.xmlunit.Diff) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) URL(java.net.URL) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ElementNameQualifier(org.custommonkey.xmlunit.ElementNameQualifier)

Example 3 with ElementNameQualifier

use of org.custommonkey.xmlunit.ElementNameQualifier in project ddf by codice.

the class CswFilterDelegateTest method testRelative.

@Test
public void testRelative() throws JAXBException, SAXException, IOException {
    long duration = 92000000000L;
    CswSourceConfiguration cswSourceConfiguration = initCswSourceConfiguration(CswAxisOrder.LAT_LON, false, CswConstants.CSW_TYPE, effectiveDateMapping, createdDateMapping, modifiedDateMapping, CswConstants.CSW_IDENTIFIER);
    CswFilterDelegate localCswFilterDelegate = createCswFilterDelegate(cswSourceConfiguration);
    Map<String, Object> propMap = new HashMap<>();
    propMap.put("extendedComparisonOp", "relative");
    propMap.put("duration", new Long(duration));
    String xml = getXmlProperty(localCswFilterDelegate, propertyNameModified, BETWEEN, testStartDate.toCalendar(null).getTime(), testEndDate.toCalendar(null).getTime(), propMap);
    String durationCompare = duringXml.replace(REPLACE_START_DATE, "").replace(REPLACE_END_DATE, "").replace(REPLACE_TEMPORAL_PROPERTY, modifiedDateMapping);
    String pattern = "(?i)(<ns.:Literal>)(.+?)(</ns.:Literal>)";
    String compareXml = xml.replaceAll(pattern, "<ogc:Literal xmlns:ogc=\"http://www.opengis.net/ogc\"></ogc:Literal>");
    Diff xmlDiff = new Diff(durationCompare, compareXml);
    xmlDiff.overrideElementQualifier(new ElementNameQualifier() {

        @Override
        protected boolean equalsNamespace(Node control, Node test) {
            return true;
        }
    });
    xmlDiff.overrideDifferenceListener(new DifferenceListener() {

        @Override
        public int differenceFound(Difference diff) {
            if (diff.getId() == DifferenceConstants.NAMESPACE_PREFIX_ID) {
                return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
            return RETURN_ACCEPT_DIFFERENCE;
        }

        @Override
        public void skippedComparison(Node arg0, Node arg1) {
        }
    });
    assertXMLIdentical(xmlDiff, true);
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) HashMap(java.util.HashMap) Diff(org.custommonkey.xmlunit.Diff) Node(org.w3c.dom.Node) Difference(org.custommonkey.xmlunit.Difference) DifferenceListener(org.custommonkey.xmlunit.DifferenceListener) ElementNameQualifier(org.custommonkey.xmlunit.ElementNameQualifier) Test(org.junit.Test)

Aggregations

Diff (org.custommonkey.xmlunit.Diff)3 ElementNameQualifier (org.custommonkey.xmlunit.ElementNameQualifier)3 Node (org.w3c.dom.Node)3 HashMap (java.util.HashMap)2 CswSourceConfiguration (org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration)2 Difference (org.custommonkey.xmlunit.Difference)2 DifferenceListener (org.custommonkey.xmlunit.DifferenceListener)2 Test (org.junit.Test)2 URL (java.net.URL)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 XMLOutputter (org.jdom.output.XMLOutputter)1 Element (org.w3c.dom.Element)1