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 MetadataAssert method assertXmlEquals.
/**
* Parses two XML trees as DOM documents, and compares the nodes with the given tolerance
* threshold for numerical values. The inputs given to this method can be any of the types
* documented {@linkplain #assertXmlEquals(Object, Object, String[]) above}. This method
* will ignore comments and the optional attributes given in arguments as documented in the
* above method.
*
* @param expected the expected XML document.
* @param actual the XML document to compare.
* @param tolerance the tolerance threshold for comparison of numerical values.
* @param ignoredNodes the fully-qualified names of the nodes to ignore, or {@code null} if none.
* @param ignoredAttributes the fully-qualified names of attributes to ignore
* (typically {@code "xmlns:*"} and {@code "xsi:schemaLocation"}).
*
* @see DocumentComparator
*/
public static void assertXmlEquals(final Object expected, final Object actual, final double tolerance, final String[] ignoredNodes, final String[] ignoredAttributes) {
final DocumentComparator comparator;
try {
comparator = new DocumentComparator(expected, actual);
} catch (IOException | ParserConfigurationException | SAXException e) {
/*
* We don't throw directly those exceptions since failing to parse the XML file can
* be considered as part of test failures and the JUnit exception for such failures
* is AssertionError. Having no checked exception in "assert" methods allow us to
* declare the checked exceptions only for the library code being tested.
*/
throw new AssertionError(e);
}
comparator.tolerance = tolerance;
comparator.ignoreComments = true;
if (ignoredNodes != null) {
for (final String node : ignoredNodes) {
comparator.ignoredNodes.add(DocumentComparator.substitutePrefix(node));
}
}
if (ignoredAttributes != null) {
for (final String attribute : ignoredAttributes) {
comparator.ignoredAttributes.add(DocumentComparator.substitutePrefix(attribute));
}
}
comparator.compare();
}
use of org.apache.sis.test.xml.DocumentComparator in project sis by apache.
the class DocumentComparatorTest method testIgnore.
/**
* Tests the {@link DocumentComparator#ignoredAttributes} and {@link DocumentComparator#ignoredNodes} sets.
*
* @throws Exception if an error occurred while reading the XML.
*/
@Test
public void testIgnore() throws Exception {
final DocumentComparator cmp = new DocumentComparator("<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();
}
use of org.apache.sis.test.xml.DocumentComparator in project geotoolkit by Geomatys.
the class XMLBindingTest method marshallingTest.
@Test
public void marshallingTest() throws Exception {
// Unmarshalles the given XML file to objects
MarshallerPool pool = FeatureCatalogMarshallerPool.getInstance();
Marshaller marshaller = pool.acquireMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
String name = "Digital Geographic information Exchange Standard (DIGEST) Feature and Attribute Coding Catalogue (FACC)";
List<String> scopes = new ArrayList<>();
scopes.add("Hydrography");
scopes.add("Ports and Harbours");
scopes.add("Transportation Networks");
String versionNumber = "2.1";
Date versionDate = new Date(2000, 9, 30);
// producer
DefaultResponsibleParty producer = new DefaultResponsibleParty();
producer.setIndividualName("John Q.Public");
producer.setOrganisationName(new SimpleInternationalString("US National Geospatial-Intelligence Agency (NGA)"));
DefaultContact contact = new DefaultContact();
DefaultTelephone phone = new DefaultTelephone();
List<String> facsmiles = new ArrayList<>();
facsmiles.add("1 703 XXX XXX");
phone.setFacsimiles(facsmiles);
phone.setVoices(facsmiles);
contact.setPhone(phone);
DefaultAddress address = new DefaultAddress();
List<InternationalString> dps = new ArrayList<>();
dps.add(new SimpleInternationalString("12310 Sunrise Valley Drive"));
address.setDeliveryPoints(dps);
address.setCity(new SimpleInternationalString("Reston"));
address.setAdministrativeArea(new SimpleInternationalString("Virginia"));
address.setPostalCode("2091-3449");
List<String> ema = new ArrayList<>();
ema.add("PublicJQ@nga.mil");
address.setElectronicMailAddresses(ema);
contact.setAddress(address);
producer.setContactInfo(contact);
producer.setRole(Role.POINT_OF_CONTACT);
// definition source
List<DefinitionSource> defSources = new ArrayList<>();
DefaultCitation source = new DefaultCitation();
source.setTitle(new SimpleInternationalString("International Hydrographic Organization (IHO) Hydrographic Dictionnary, Part I, Volume I English"));
List<CitationDate> dates = new ArrayList<>();
DefaultCitationDate date = new DefaultCitationDate();
date.setDate(new Date(1994, 1, 1));
date.setDateType(DateType.PUBLICATION);
dates.add(date);
source.setDates(dates);
source.setEdition(new SimpleInternationalString("Fifth"));
List<ResponsibleParty> rps = new ArrayList<>();
DefaultResponsibleParty rp = new DefaultResponsibleParty();
rp.setOrganisationName(new SimpleInternationalString("International Hydrographic Bureau"));
rp.setRole(Role.PUBLISHER);
rps.add(rp);
source.setCitedResponsibleParties(rps);
source.setOtherCitationDetails(Collections.singleton(new SimpleInternationalString("Special publication n°32")));
DefinitionSource defSource = new DefinitionSourceImpl("ds-IHO", source);
defSources.add(defSource);
// featureTypes 1
List<FeatureType> ftypes = new ArrayList<>();
String def = "An excavation made in the earth for the purpose of extracting natural deposit. (see also AQ090)";
String code = "DEP";
List<org.opengis.util.LocalName> aliases = new ArrayList<>();
DefaultNameFactory factory = new DefaultNameFactory();
aliases.add(factory.createLocalName(null, "Extraction Mine"));
List<PropertyType> cof = new ArrayList<>();
// attribute constraint
Constraint constraint = new ConstraintImpl("Positive values represent distance below the refernce point from wixh the measurement is made");
List<Constraint> consts = new ArrayList<>();
consts.add(constraint);
// feature attribute 1 with constraint
FeatureAttributeImpl attr = new FeatureAttributeImpl("attribute-1", factory.createLocalName(null, "Depth"), "Distance measured from the highest", new Multiplicity(NumberRange.create(1, true, 1, true)), null, consts, code, null, factory.createTypeName(null, "Real"));
cof.add(attr);
// listed values
List<ListedValue> values = new ArrayList<>();
ListedValue v1 = new ListedValueImpl("0", "Unknown", "The attribute value is missing", null);
values.add(v1);
// definition reference
DefinitionReference defRef = new DefinitionReferenceImpl("3833, pier", defSource);
ListedValue v2 = new ListedValueImpl("1", "Pier", null, defRef);
values.add(v2);
DefinitionReference defRef2 = new DefinitionReferenceImpl("5985, wharf", defSource);
ListedValue v3 = new ListedValueImpl("2", "Wharf", null, defRef2);
values.add(v3);
DefinitionReference defRef3 = new DefinitionReferenceImpl("4125, quay", defSource);
ListedValue v4 = new ListedValueImpl("3", "Quay", null, defRef3);
values.add(v4);
ListedValue v5 = new ListedValueImpl("997", "Unpopulated", "The attribute value exist, but due to the policy considerations it cannot be given", null);
values.add(v5);
ListedValue v6 = new ListedValueImpl("998", "Not applicable", "No attribute value in the range of possible attribute values is applicable", null);
values.add(v6);
ListedValue v7 = new ListedValueImpl("999", "Other", "The attribute value cannot be given for some reason other than it is 'Multiple', 'Not applicable', 'Unknown', or 'Unpopulated'", null);
values.add(v7);
// feature attribute 2 with listed value
FeatureAttributeImpl attr2 = new FeatureAttributeImpl("attribute-2", factory.createLocalName(null, "Pier/Wharf/Quay classification"), "Classification of decked berthing structure, based on configuration and structure", new Multiplicity(NumberRange.create(1, true, 1, true)), null, null, code, values, factory.createTypeName(null, "Real"));
cof.add(attr2);
code = "AA010";
FeatureTypeImpl ft1 = new FeatureTypeImpl("mine-1", factory.createLocalName(null, "Mine"), def, code, false, aliases, null, cof);
attr.setFeatureType(ft1);
ftypes.add(ft1);
// field of application
List<String> foa = new ArrayList<>();
foa.add("Military Engineering");
foa.add("Marine Navigation");
FeatureCatalogueImpl catalogue = new FeatureCatalogueImpl("cat-1", name, scopes, producer, versionDate, versionNumber, ftypes, defSources, foa, "Gofer");
ft1.setFeatureCatalogue(catalogue);
// feature type 2
List<PropertyType> cof2 = new ArrayList<>();
FeatureTypeImpl ft2 = new FeatureTypeImpl("road-1", factory.createLocalName(null, "Road"), "An open way maintained for vehicular use.", "AP030", false, null, catalogue, cof2);
// feature type 3
List<PropertyType> cof3 = new ArrayList<>();
FeatureTypeImpl ft3 = new FeatureTypeImpl("bidge-1", factory.createLocalName(null, "Bidge"), "A man made structure spanning and providing passage over a body of water", "AQ040", false, null, catalogue, cof3);
// Assoication role
AssociationRoleImpl role1 = new AssociationRoleImpl("role-1", factory.createLocalName(null, "Over"), "Bridge whitch the road crosses over ...", new Multiplicity(NumberRange.createLeftBounded(1, true)), ft2, null, null, RoleType.ORDINARY, true, true, null, ft3);
cof2.add(role1);
// Assoication role
AssociationRoleImpl role2 = new AssociationRoleImpl("role-2", factory.createLocalName(null, "Under"), "Roads which cross this bridge.", new Multiplicity(NumberRange.create(0, true, 1, true)), ft3, null, null, RoleType.ORDINARY, false, true, null, ft2);
cof3.add(role2);
// Feature association
List<AssociationRole> roles = new ArrayList<>();
FeatureAssociationImpl fassoc = new FeatureAssociationImpl("stack-1", factory.createLocalName(null, "Stacked On"), "An object is over another object", "101", false, null, catalogue, null, roles);
roles.add(role1);
roles.add(role2);
role1.setRelation(fassoc);
role2.setRelation(fassoc);
// // inheritance relation
// feature type 4
FeatureTypeImpl ft4 = new FeatureTypeImpl("building-1", factory.createLocalName(null, "Building"), "A relatively permanent structure, roofed and usually walled and designed for some particular use.", "AL015", false, null, catalogue, null);
// feature type 5
FeatureTypeImpl ft5 = new FeatureTypeImpl("lh-1", factory.createLocalName(null, "Lighthouse"), "A distinctive structure exhibiting light(s) designed to serve as an aid to navigation.", "BC050", false, null, catalogue, null);
// inheritanceRelation
InheritanceRelationImpl inherit = new InheritanceRelationImpl("is a", "An object is classified as a specialization of another object", false, ft5, ft4);
inherit.setId("inherits-1");
ft4.getInheritsTo().add(inherit);
ft5.getInheritsFrom().add(inherit);
// feature operation
FeatureOperationImpl operation = new FeatureOperationImpl();
operation.setId("operation-1");
operation.setMemberName(factory.createLocalName(null, "Raise dam"));
operation.setDefinition("The action of raising the dam causes changes in the discharge from the dam....");
operation.setCardinality(new Multiplicity(NumberRange.create(1, true, 1, true)));
operation.setFeatureType(ft5);
operation.setSignature("damRaise((Dam) dam, (Real) newHeight) : Dam");
operation.setFormalDefinition(" damRaise(ConstructDam(d), h) = error " + " 'cannot raise height of a damunder construction'" + " damRaise(Operate (d,i,j), h)" + " | (h >i) && (h < maxHeight(d)) = Operate(d,h,j)" + " | otherwise = error 'illegal new height for dam' ");
BoundFeatureAttributeImpl bf1 = new BoundFeatureAttributeImpl();
bf1.setId("boundatt-1");
bf1.setAttribute(attr2);
bf1.setFeatureType(ft5);
operation.getObservesValuesOf().add(bf1);
StringWriter sw = new StringWriter();
marshaller.marshal(catalogue, sw);
String result = sw.toString();
Path p = IOUtilities.getResourceAsPath("org/geotoolkit/test/FeatureCatalogue.xml");
String expected = IOUtilities.toString(p);
DocumentComparator comparator = new DocumentComparator(expected, result);
comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
comparator.compare();
sw = new StringWriter();
marshaller.marshal(fassoc, sw);
result = sw.toString();
p = IOUtilities.getResourceAsPath("org/geotoolkit/test/FeatureAssociation.xml");
expected = IOUtilities.toString(p);
comparator = new DocumentComparator(expected, result);
comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
comparator.compare();
sw = new StringWriter();
marshaller.marshal(inherit, sw);
result = sw.toString();
p = IOUtilities.getResourceAsPath("org/geotoolkit/test/InheritanceRelation.xml");
expected = IOUtilities.toString(p);
comparator = new DocumentComparator(expected, result);
comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
comparator.compare();
sw = new StringWriter();
marshaller.marshal(operation, sw);
result = sw.toString();
p = IOUtilities.getResourceAsPath("org/geotoolkit/test/FeatureOperation.xml");
expected = IOUtilities.toString(p);
comparator = new DocumentComparator(expected, result);
comparator.ignoredAttributes.add("http://www.w3.org/2000/xmlns:*");
comparator.compare();
}
use of org.apache.sis.test.xml.DocumentComparator in project geotoolkit by Geomatys.
the class DiffXmlBindingTest method marshallingTest.
/**
* Test simple Record Marshalling.
*/
@Test
public void marshallingTest() throws Exception {
DIF dif = new DIF();
dif.setEntryID(new EntryIDType("dif-1", null));
dif.setEntryTitle("title");
dif.setSummary(new SummaryType("some abstract", "some purpose"));
dif.getMetadataAssociation().add(new MetadataAssociationType(new EntryIDType("id-1", null), MetadataAssociationTypeEnum.PARENT, null));
PersonnelType author = new PersonnelType();
author.getRole().add(PersonnelRoleEnum.METADATA_AUTHOR);
AddressType addr = new AddressType("12 rue blip", "Montpellier", "Occitanie", "34000", "France");
PhoneType phone = new PhoneType("0466441122", PhoneTypeEnum.DIRECT_LINE);
author.getContactGroup().add(new ContactGroupType("Geomatys", addr, phone, "contact@geomatys.com"));
dif.getPersonnel().add(author);
PersonnelType author2 = new PersonnelType();
author2.getRole().add(PersonnelRoleEnum.METADATA_AUTHOR);
AddressType addr2 = new AddressType("12 rue blip", "Montpellier", "Occitanie", "34000", "France");
PhoneType phone2 = new PhoneType("0466441122", PhoneTypeEnum.DIRECT_LINE);
author2.getContactPerson().add(new ContactPersonType("987-887", "jean", "pierre", "imbert", addr2, phone2, "contact@geomatys.com"));
dif.getPersonnel().add(author2);
PersonnelType tech = new PersonnelType();
tech.getRole().add(PersonnelRoleEnum.TECHNICAL_CONTACT);
AddressType addrTech = new AddressType("12 rue blip", "Montpellier", "Occitanie", "34000", "France");
PhoneType phoneTech = new PhoneType("0466441122", PhoneTypeEnum.DIRECT_LINE);
tech.getContactPerson().add(new ContactPersonType("1244110-777", "michel", "henry", "bel", addrTech, phoneTech, "tech-contact@geomatys.com"));
dif.getPersonnel().add(tech);
BoundingRectangleType rec = new BoundingRectangleType();
rec.setEasternmostLongitude("5.0");
rec.setWesternmostLongitude("5.0");
rec.setNorthernmostLatitude("5.0");
rec.setSouthernmostLatitude("5.0");
rec.setMaximumAltitude("10");
rec.setMinimumAltitude("3");
final Geometry geom = new Geometry(rec);
dif.setSpatialCoverage(new SpatialCoverageType(geom));
dif.setMetadataDates(new MetadataDatesType("2010-12-01T12:00Z"));
ContactPersonType distributor = new ContactPersonType("666-777", "pedro", "juan", "lopez", addrTech, phoneTech, "distributor@geomatys.com");
OrgPersonnelType distPers = new OrgPersonnelType(OrganizationPersonnelRoleEnum.DATA_CENTER_CONTACT, distributor, null);
dif.getOrganization().add(new OrganizationType(OrganizationTypeEnum.DISTRIBUTOR, new OrganizationNameType("Geom", "Geomatys"), "http://www.geomatys.com", distPers));
dif.getDatasetLanguage().add(DatasetLanguageEnum.FRENCH);
dif.setDatasetProgress(DatasetProgressEnum.COMPLETE);
dif.getLocation().add(new LocationType("GARD"));
dif.getISOTopicCategory().add(new ISOTopicCategoryType("economy"));
dif.getScienceKeywords().add(new ScienceKeywordsType("science category", "science topic", "science term"));
dif.getDataResolution().add(new DataResolutionType());
dif.getTemporalCoverage().add(new TemporalCoverageType(new RangeDateTimeType("2010-12-01T12:00Z", "2010-12-02T23:00Z")));
dif.getMultimediaSample().add(new MultimediaSampleType("http://localhost:8080/quicklook/SCENE_5_083-342_09_04_16_09_38_39_1_J.png", "quicklook", "image/png"));
dif.getAncillaryKeyword().add("anc kw1");
dif.getAncillaryKeyword().add("anc kw2");
dif.setAccessConstraints("some acces constraint");
dif.setUseConstraints(new UseConstraintsType("use csts"));
dif.getDistribution().add(new DistributionType("http://link.com", "3.12", "binary", "free"));
dif.getDistribution().add(new DistributionType("http://link/img", "6", "image/png", "10$"));
dif.getRelatedURL().add(new RelatedURLType(new URLContentType("GET DATA"), "http", Arrays.asList("https://gportal.jaxa.jp/"), "jaxa", new DisplayableTextType(" desc blma"), null));
StringWriter sw = new StringWriter();
marshaller.marshal(dif, sw);
// System.out.println(sw.toString());
final DocumentComparator comparator = new DocumentComparator(sw.toString(), getResourceAsStream("org/geotoolkit/dif/xml/v102/dif.xml"));
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();
}
Aggregations