Search in sources :

Example 1 with XMLComparator

use of org.apache.sis.test.XMLComparator in project sis by apache.

the class XMLComparatorTest method testIgnore.

/**
 * Tests the {@link XMLComparator#ignoredAttributes} and {@link XMLComparator#ignoredNodes} sets.
 *
 * @throws Exception if an error occurred while reading the XML.
 */
@Test
public void testIgnore() throws Exception {
    final XMLComparator cmp = new XMLComparator("<body>\n" + "  <form id=\"MyForm\">\n" + "    <table cellpading=\"1\">\n" + "      <tr><td>foo</td></tr>\n" + "    </table>\n" + "  </form>\n" + "</body>", "<body>\n" + "  <form id=\"MyForm\">\n" + "    <table cellpading=\"2\">\n" + "      <tr><td>foo</td></tr>\n" + "    </table>\n" + "  </form>\n" + "</body>");
    assertFail("Shall fail because the \"cellpading\" attribute value is different.", cmp);
    // Following comparison should not fail anymore.
    cmp.ignoredAttributes.add("cellpading");
    cmp.compare();
    cmp.ignoredAttributes.clear();
    cmp.ignoredAttributes.add("bgcolor");
    assertFail("The \"cellpading\" attribute should not be ignored anymore.", cmp);
    // Ignore the table node, which contains the faulty attribute.
    cmp.ignoredNodes.add("table");
    cmp.compare();
    // Ignore the form node and all its children.
    cmp.ignoredNodes.clear();
    cmp.ignoredNodes.add("form");
    cmp.compare();
}
Also used : XMLComparator(org.apache.sis.test.XMLComparator) Test(org.junit.Test)

Example 2 with XMLComparator

use of org.apache.sis.test.XMLComparator in project sis by apache.

the class XMLComparatorTest method testNamespaceAware.

/**
 * Verifies that comparisons of XML documents compare the namespace URIs, not the prefixes.
 *
 * @see javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean)
 *
 * @throws Exception if an error occurred while reading the XML.
 */
@Test
public void testNamespaceAware() throws Exception {
    XMLComparator cmp = new XMLComparator("<ns1:body xmlns:ns1=\"http://myns1\" xmlns:ns2=\"http://myns2\">\n" + "  <ns1:table ns2:cellpading=\"1\"/>\n" + "</ns1:body>", "<ns4:body xmlns:ns4=\"http://myns1\" xmlns:ns3=\"http://myns2\">\n" + "  <ns4:table ns3:cellpading=\"1\"/>\n" + "</ns4:body>");
    // Following comparison should not fail anymore.
    cmp.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
    cmp.compare();
    /*
         * Opposite case: same prefix, but different URL.
         * The XML comparison is expected to fail.
         */
    cmp = new XMLComparator("<ns1:body xmlns:ns1=\"http://myns1\" xmlns:ns2=\"http://myns2\">\n" + "  <ns1:table ns2:cellpading=\"1\"/>\n" + "</ns1:body>", "<ns1:body xmlns:ns1=\"http://myns1\" xmlns:ns2=\"http://myns3\">\n" + "  <ns1:table ns2:cellpading=\"1\"/>\n" + "</ns1:body>");
    // Following comparison should not fail anymore.
    cmp.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
    assertFail("Shall fail because the \"cellpading\" attribute has a different namespace.", cmp);
}
Also used : XMLComparator(org.apache.sis.test.XMLComparator) Test(org.junit.Test)

Example 3 with XMLComparator

use of org.apache.sis.test.XMLComparator in project sis by apache.

the class MetadataTest method testMarshalling.

/**
 * Tests marshalling of a XML document.
 *
 * @throws Exception if an error occurred during marshalling.
 */
@Test
public void testMarshalling() throws Exception {
    final MarshallerPool pool = getMarshallerPool();
    final Marshaller ms = pool.acquireMarshaller();
    final StringWriter writer = new StringWriter(25000);
    ms.setProperty(XML.METADATA_VERSION, VERSION_2007);
    ms.marshal(createHardCoded(), writer);
    pool.recycle(ms);
    /*
         * Apache SIS can marshal CharSequence as Anchor only if the property type is InternationalString.
         * But the 'Metadata.hierarchyLevelName' and 'Identifier.code' properties are String, which we can
         * not subclass. Concequently SIS currently marshals them as plain string. Replace those strings
         * by the anchor version so we can compare the XML with the "Metadata.xml" file content.
         */
    final StringBuffer xml = writer.getBuffer();
    replace(xml, "<gcol:CharacterString>Common Data Index record</gcol:CharacterString>", "<gmx:Anchor xlink:href=\"SDN:L231:3:CDI\">Common Data Index record</gmx:Anchor>");
    replace(xml, "<gcol:CharacterString>EPSG:4326</gcol:CharacterString>", "<gmx:Anchor xlink:href=\"SDN:L101:2:4326\">EPSG:4326</gmx:Anchor>");
    replace(xml, "License", "Licence");
    /*
         * The <gmd:EX_TemporalExtent> block can not be marshalled yet, since it requires the sis-temporal module.
         * We need to instruct the XML comparator to ignore this block during the comparison. We also ignore for
         * now the "gml:id" attribute since SIS generates different values than the ones in oyr test XML file,
         * and those values may change in future SIS version.
         */
    final XMLComparator comparator = new XMLComparator(getResource(), xml.toString());
    comparator.ignoredNodes.add(LegacyNamespaces.GMD + ":temporalElement");
    comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
    comparator.ignoredAttributes.add(Namespaces.XSI + ":schemaLocation");
    comparator.ignoredAttributes.add(Namespaces.GML + ":id");
    comparator.ignoreComments = true;
    comparator.compare();
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) XMLComparator(org.apache.sis.test.XMLComparator) MarshallerPool(org.apache.sis.xml.MarshallerPool) Test(org.junit.Test)

Aggregations

XMLComparator (org.apache.sis.test.XMLComparator)3 Test (org.junit.Test)3 StringWriter (java.io.StringWriter)1 Marshaller (javax.xml.bind.Marshaller)1 MarshallerPool (org.apache.sis.xml.MarshallerPool)1