use of org.apache.sis.test.xml.DocumentComparator 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. Consequently 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>Pseudo Common Data Index record</gcol:CharacterString>", "<gmx:Anchor xlink:href=\"SDN:L231:3:CDI\">Pseudo Common Data Index record</gmx:Anchor>");
replace(xml, "<gcol:CharacterString>4326</gcol:CharacterString>", "<gmx:Anchor xlink:href=\"SDN:L101:2:4326\">4326</gmx:Anchor>");
replace(xml, "License", "Licence");
/*
* The <gmd:EX_TemporalExtent> block can not be marshalled es expected yet (need a "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 our test XML file,
* and those values may change in future SIS version.
*/
final DocumentComparator comparator = new DocumentComparator(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();
}
use of org.apache.sis.test.xml.DocumentComparator in project sis by apache.
the class DocumentComparatorTest 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 {
DocumentComparator cmp = new DocumentComparator("<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 DocumentComparator("<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);
}
use of org.apache.sis.test.xml.DocumentComparator in project geotoolkit by Geomatys.
the class XMLBindingTestBuilder method test.
public void test() throws JAXBException, IOException, ParserConfigurationException, SAXException {
ArgumentChecks.ensureNonNull("Input data to test", input);
ArgumentChecks.ensureNonNull("Marshaller pool", pool);
final Unmarshaller um = pool.acquireUnmarshaller();
um.setEventHandler(ERROR_LOGGER);
final Object unmarshalled;
try (final InputStream in = input.get()) {
final Object tmpObj = um.unmarshal(in);
if (tmpObj instanceof JAXBElement) {
unmarshalled = ((JAXBElement) tmpObj).getValue();
} else {
unmarshalled = tmpObj;
}
}
pool.recycle(um);
Assert.assertNotNull("Unmarshalled capabilities should not be null", unmarshalled);
if (expectedBindingType != null) {
Assert.assertTrue("Read capabilities is of invalid type. Expected: " + expectedBindingType + ", but was: " + unmarshalled.getClass(), expectedBindingType.isInstance(unmarshalled));
}
if (additionalTests != null) {
additionalTests.accept((T) unmarshalled);
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Marshaller marsh = pool.acquireMarshaller();
marsh.setEventHandler(ERROR_LOGGER);
marsh.marshal(unmarshalled, out);
pool.recycle(marsh);
final byte[] outputArray = out.toByteArray();
LOGGER.info(() -> "Generated Document:" + System.lineSeparator() + new String(outputArray, StandardCharsets.UTF_8));
final DocumentComparator comparator = new DocumentComparator(input.get(), new ByteArrayInputStream(outputArray));
comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
comparator.ignoredAttributes.add("http://www.w3.org/2001/XMLSchema-instance:schemaLocation");
comparator.ignoreComments = true;
comparator.compare();
}
use of org.apache.sis.test.xml.DocumentComparator in project geotoolkit by Geomatys.
the class DomCompare method compare.
/**
* convenient method to test XML comparison by ignoring irrelevant details
* like formatting, different attribute order, comments ...
*
* @param expected the expected structure (File,Stream,Document)
* @param result the obtained result (File,Stream,Document)
*/
public static void compare(final Object expected, final Object result) throws ParserConfigurationException, SAXException, IOException {
final DocumentComparator comparator = new DocumentComparator(expected, result);
comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
comparator.ignoredAttributes.add("http://www.w3.org/2001/XMLSchema-instance:schemaLocation");
comparator.compare();
}
use of org.apache.sis.test.xml.DocumentComparator in project geotoolkit by Geomatys.
the class ProvidersXmlTest method testMarshallingWithoutSLD.
/**
* Test for the marshalling process of a {@link MapContext}.
*
* @throws JAXBException
*/
@Test
public void testMarshallingWithoutSLD() throws Exception {
final List<MapItem> mapLayers2 = new ArrayList<>();
mapLayers2.add(new MapLayer(new DataReference("postgis_test:my_otherlayer"), new StyleReference("my_otherstyle")));
mapLayers2.add(new MapLayer(new DataReference("coverage:my_thirdlayer"), new StyleReference("my_newstyle")));
final List<MapItem> mapItems = new ArrayList<>();
mapItems.add(new MapItem(mapLayers2));
final MapLayer ml = new MapLayer(new DataReference("postgis_test:my_layer"), new StyleReference("my_style"));
mapItems.add(new MapItem());
mapItems.add(ml);
final MapItem mapItem = new MapItem(mapItems);
final MapContext mapContext = new MapContext(mapItem);
final StringWriter sw = new StringWriter();
marshaller.marshal(mapContext, sw);
final String result = sw.toString();
try {
sw.close();
} catch (IOException e) {
fail("Unable to close the writer");
}
assertNotNull(result);
assertFalse(result.isEmpty());
DocumentComparator comparator = new DocumentComparator(RESULT_MARSHALLING_WITHOUT_SLD, result.trim());
comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
comparator.compare();
}
Aggregations