Search in sources :

Example 6 with NameFactory

use of org.opengis.util.NameFactory in project sis by apache.

the class DefaultAssociationRoleTest method testCyclicAssociation.

/**
 * Tests {@code DefaultFeatureType.isAssignableFrom(FeatureType)} and {@code DefaultFeatureType.equals(Object)}
 * on a feature type having a bidirectional association to an other feature. This test will fall in an infinite
 * loop if the implementation does not have proper guard against infinite recursivity.
 */
@Test
@DependsOnMethod("testBidirectionalAssociation")
public void testCyclicAssociation() {
    final NameFactory factory = DefaultFactories.forBuildin(NameFactory.class);
    final GenericName nameOfA = factory.createTypeName(null, "A");
    final GenericName nameOfB = factory.createTypeName(null, "B");
    final GenericName nameOfC = factory.createTypeName(null, "C");
    final GenericName nameOfD = factory.createTypeName(null, "D");
    /*
         * Associations defined only by the FeatureType name.
         */
    final DefaultAssociationRole toB = new DefaultAssociationRole(singletonMap(NAME_KEY, "toB"), nameOfB, 1, 1);
    final DefaultAssociationRole toC = new DefaultAssociationRole(singletonMap(NAME_KEY, "toC"), nameOfC, 1, 1);
    final DefaultAssociationRole toD = new DefaultAssociationRole(singletonMap(NAME_KEY, "toD"), nameOfD, 1, 1);
    final DefaultFeatureType typeA = createType(nameOfA, null, toB);
    final DefaultFeatureType typeB = createType(nameOfB, null, toC);
    final DefaultFeatureType typeC = createType(nameOfC, null, toD);
    /*
         * Association defined with real FeatureType instance, except for an association to itself.
         * Construction of this FeatureType shall cause the resolution of all above FeatureTypes.
         */
    final DefaultAssociationRole toAr = new DefaultAssociationRole(singletonMap(NAME_KEY, "toA"), typeA, 1, 1);
    final DefaultAssociationRole toBr = new DefaultAssociationRole(singletonMap(NAME_KEY, toB.getName()), typeB, 1, 1);
    final DefaultAssociationRole toCr = new DefaultAssociationRole(singletonMap(NAME_KEY, toC.getName()), typeC, 1, 1);
    final DefaultFeatureType typeD = createType(nameOfD, null, toAr, toBr, toCr, toD);
    /*
         * Verify the property given to the constructors. There is no reason for those properties
         * to change as they are not the instances to be replaced by the name resolutions, but we
         * verify them as a paranoiac check.
         */
    assertSame("A.properties", toB, getSingleton(typeA.getProperties(false)));
    assertSame("B.properties", toC, getSingleton(typeB.getProperties(false)));
    assertSame("C.properties", toD, getSingleton(typeC.getProperties(false)));
    assertSame("D.properties", toAr, typeD.getProperty("toA"));
    assertSame("D.properties", toBr, typeD.getProperty("toB"));
    assertSame("D.properties", toCr, typeD.getProperty("toC"));
    assertSame("D.properties", toD, typeD.getProperty("toD"));
    /*
         * CORE OF THIS TEST: verify that the values of toB, toC and toD have been replaced by the actual
         * FeatureType instances. Also verify that as a result, toB.equals(toBr) and toC.equals(toCr).
         */
    assertSame("toA", typeA, toAr.getValueType());
    assertSame("toB", typeB, toBr.getValueType());
    assertSame("toB", typeB, toB.getValueType());
    assertSame("toC", typeC, toCr.getValueType());
    assertSame("toC", typeC, toC.getValueType());
    assertSame("toD", typeD, toD.getValueType());
    assertEquals("toB", toB, toBr);
    assertEquals("toC", toC, toCr);
    /*
         * Other equality tests, mostly for verifying that we do not fall in an infinite loop here.
         */
    assertFalse("equals", typeA.equals(typeD));
    assertFalse("equals", typeD.equals(typeA));
    assertFalse("equals", typeB.equals(typeC));
    assertFalse("equals", typeC.equals(typeB));
    assertFalse("hashCode", typeA.hashCode() == typeB.hashCode());
    assertFalse("hashCode", typeC.hashCode() == typeD.hashCode());
}
Also used : GenericName(org.opengis.util.GenericName) NameFactory(org.opengis.util.NameFactory) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 7 with NameFactory

use of org.opengis.util.NameFactory in project sis by apache.

the class MetadataReader method addSampleDimension.

/**
 * Adds metadata about a sample dimension (or band) from the given variable.
 * This is the {@code <gmd:dimension>} element in XML.
 *
 * @param  variable  the netCDF variable.
 */
private void addSampleDimension(final Variable variable) {
    newSampleDimension();
    final String name = trim(variable.getName());
    if (name != null) {
        if (nameFactory == null) {
            nameFactory = DefaultFactories.forBuildin(NameFactory.class, DefaultNameFactory.class);
        // Real dependency injection to be used in a future version.
        }
        setBandIdentifier(nameFactory.createMemberName(null, name, nameFactory.createTypeName(null, variable.getDataTypeName())));
    }
    Object[] v = variable.getAttributeValues(CF.STANDARD_NAME, false);
    final String id = (v.length == 1) ? trim((String) v[0]) : null;
    if (id != null && !id.equals(name)) {
        v = variable.getAttributeValues(ACDD.standard_name_vocabulary, false);
        addBandName(v.length == 1 ? (String) v[0] : null, id);
    }
    final String description = trim(variable.getDescription());
    if (description != null && !description.equals(name) && !description.equals(id)) {
        addBandDescription(description);
    }
    final String units = variable.getUnitsString();
    if (units != null)
        try {
            setSampleUnits(Units.valueOf(units));
        } catch (ClassCastException | ParserException e) {
            decoder.listeners.warning(errors().getString(Errors.Keys.CanNotAssignUnitToDimension_2, name, units), e);
        }
    double scale = Double.NaN;
    double offset = Double.NaN;
    v = variable.getAttributeValues(CDM.SCALE_FACTOR, true);
    if (v.length == 1)
        scale = ((Number) v[0]).doubleValue();
    v = variable.getAttributeValues(CDM.ADD_OFFSET, true);
    if (v.length == 1)
        offset = ((Number) v[0]).doubleValue();
    setTransferFunction(scale, offset);
    addContentType(forCodeName(CoverageContentType.class, stringValue(ACDD.coverage_content_type)));
}
Also used : DefaultNameFactory(org.apache.sis.util.iso.DefaultNameFactory) InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) NameFactory(org.opengis.util.NameFactory) DefaultNameFactory(org.apache.sis.util.iso.DefaultNameFactory)

Example 8 with NameFactory

use of org.opengis.util.NameFactory in project sis by apache.

the class DefaultServiceIdentificationTest method create.

/**
 * Creates the service identification to use for testing purpose.
 */
private static DefaultServiceIdentification create() {
    final NameFactory factory = DefaultFactories.forBuildin(NameFactory.class);
    final DefaultCoupledResource resource = DefaultCoupledResourceTest.create(factory);
    resource.setResourceReferences(singleton(new DefaultCitation("WMS specification")));
    final DefaultServiceIdentification id = new DefaultServiceIdentification(// serviceType
    factory.createGenericName(null, "Web Map Server"), // citation
    NilReason.MISSING.createNilObject(Citation.class), // abstract
    "A dummy service for testing purpose.");
    id.setServiceTypeVersions(singleton("1.0"));
    id.setCoupledResources(singleton(resource));
    id.setCouplingType(UnsupportedCodeList.valueOf("LOOSE"));
    id.setContainsOperations(singleton(resource.getOperation()));
    return id;
}
Also used : DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) Citation(org.opengis.metadata.citation.Citation) DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) NameFactory(org.opengis.util.NameFactory)

Example 9 with NameFactory

use of org.opengis.util.NameFactory in project sis by apache.

the class DefaultAssociationRoleTest method twinTown.

/**
 * Creates an association to a twin town. We arbitrarily fix the maximum number
 * of occurrences to 1, even if in reality some cities have many twin towns.
 *
 * @param  cyclic  {@code true} if in addition to the association from <var>A</var> to <var>B</var>,
 *                 we also want an association from <var>B</var> to <var>A</var>, thus creating a cycle.
 * @return the association to use for testing purpose.
 */
static DefaultAssociationRole twinTown(final boolean cyclic) {
    final Map<String, ?> properties = singletonMap(NAME_KEY, "twin town");
    if (cyclic) {
        final NameFactory factory = DefaultFactories.forBuildin(NameFactory.class);
        final GenericName valueType = factory.createTypeName(null, "Twin town");
        return new DefaultAssociationRole(properties, valueType, 0, 1);
    } else {
        final DefaultFeatureType valueType = DefaultFeatureTypeTest.city();
        return new DefaultAssociationRole(properties, valueType, 0, 1);
    }
}
Also used : GenericName(org.opengis.util.GenericName) NameFactory(org.opengis.util.NameFactory)

Example 10 with NameFactory

use of org.opengis.util.NameFactory in project sis by apache.

the class DefaultFeatureTypeTest method testQualifiedNames.

/**
 * Same than {@link #testNameCollision()}, but resolving collisions with usage of names
 * of the form {@code "head:tip"}.
 *
 * @since 0.6
 */
@Test
@DependsOnMethod("testNameCollision")
public void testQualifiedNames() {
    final NameFactory factory = DefaultFactories.forBuildin(NameFactory.class);
    final DefaultAttributeType<String> city = new DefaultAttributeType<>(name(factory.createGenericName(null, "ns1", "name")), String.class, 1, 1, null);
    final DefaultAttributeType<Integer> cityId = new DefaultAttributeType<>(name(factory.createGenericName(null, "ns2", "name")), Integer.class, 1, 1, null);
    final DefaultAttributeType<Integer> population = new DefaultAttributeType<>(name(factory.createGenericName(null, "ns1", "population")), Integer.class, 1, 1, null);
    final DefaultFeatureType feature = new DefaultFeatureType(name("City"), false, null, city, cityId, population);
    final Iterator<AbstractIdentifiedType> it = feature.getProperties(false).iterator();
    assertSame("properties[0]", city, it.next());
    assertSame("properties[1]", cityId, it.next());
    assertSame("properties[2]", population, it.next());
    assertFalse(it.hasNext());
    assertSame("Shall get from fully qualified name.", city, feature.getProperty("ns1:name"));
    assertSame("Shall get from fully qualified name.", cityId, feature.getProperty("ns2:name"));
    assertSame("Shall get from fully qualified name.", population, feature.getProperty("ns1:population"));
    assertSame("Shall get from short alias.", population, feature.getProperty("population"));
    try {
        feature.getProperty("name");
        fail("Expected no alias because of ambiguity.");
    } catch (IllegalArgumentException e) {
        final String message = e.getMessage();
        // Property name.
        assertTrue(message, message.contains("name"));
        // Ambiguity 1.
        assertTrue(message, message.contains("ns1:name"));
        // Ambiguity 2.
        assertTrue(message, message.contains("ns2:name"));
    }
    try {
        feature.getProperty("other");
        fail("Expected no property.");
    } catch (IllegalArgumentException e) {
        final String message = e.getMessage();
        // Property name.
        assertTrue(message, message.contains("other"));
        // Feature name.
        assertTrue(message, message.contains("City"));
    }
}
Also used : InternationalString(org.opengis.util.InternationalString) NameFactory(org.opengis.util.NameFactory) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Aggregations

NameFactory (org.opengis.util.NameFactory)20 Test (org.junit.Test)13 DependsOnMethod (org.apache.sis.test.DependsOnMethod)6 GenericName (org.opengis.util.GenericName)5 LocalName (org.opengis.util.LocalName)5 InternationalString (org.opengis.util.InternationalString)4 SimpleInternationalString (org.apache.sis.util.iso.SimpleInternationalString)2 NameSpace (org.opengis.util.NameSpace)2 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 DefaultCitation (org.apache.sis.metadata.iso.citation.DefaultCitation)1 IdentifiedObjectMock (org.apache.sis.test.mock.IdentifiedObjectMock)1 DefaultNameFactory (org.apache.sis.util.iso.DefaultNameFactory)1 Citation (org.opengis.metadata.citation.Citation)1 ResponsibleParty (org.opengis.metadata.citation.ResponsibleParty)1 Constraints (org.opengis.metadata.constraint.Constraints)1 Format (org.opengis.metadata.distribution.Format)1 Extent (org.opengis.metadata.extent.Extent)1 MaintenanceInformation (org.opengis.metadata.maintenance.MaintenanceInformation)1