Search in sources :

Example 1 with ValueConstraintFactoryDescriptor

use of eu.esdihumboldt.hale.common.schema.model.constraint.factory.extension.ValueConstraintFactoryDescriptor in project hale by halestudio.

the class GeometryMetadataFactoryTest method testAlias.

/**
 * Test if alias "jdbc.geometry" for geometry metadata constraint is
 * working.
 */
@Test
public void testAlias() {
    ValueConstraintFactoryDescriptor desc = ValueConstraintExtension.INSTANCE.get("jdbc.geometry");
    assertNotNull(desc);
    assertEquals(GeometryMetadata.class, desc.getConstraintType());
    assertTrue(desc.getFactory() instanceof GeometryMetadataFactory);
}
Also used : GeometryMetadataFactory(eu.esdihumboldt.hale.common.schema.model.constraint.type.factory.GeometryMetadataFactory) ValueConstraintFactoryDescriptor(eu.esdihumboldt.hale.common.schema.model.constraint.factory.extension.ValueConstraintFactoryDescriptor) Test(org.junit.Test)

Example 2 with ValueConstraintFactoryDescriptor

use of eu.esdihumboldt.hale.common.schema.model.constraint.factory.extension.ValueConstraintFactoryDescriptor in project hale by halestudio.

the class AbstractValueConstraintFactoryTest method storeRestoreTest.

/**
 * Test storing a constraint, restoring it and compare both.
 *
 * @param constraint the constraint to store
 * @param typeIndex the type index (as context for storing/restoring),
 *            <code>null</code> for an empty index
 * @param constraintDef the definition the constraint is associated to (as
 *            context for storing/restoring), for <code>null</code> the
 *            method will try to generate a default definition based on the
 *            constraint type
 * @throws Exception if an error occurs during storing, restoring or
 *             comparing the constraint
 */
@SuppressWarnings("unchecked")
protected void storeRestoreTest(T constraint, Map<TypeDefinition, Value> typeIndex, Definition<?> constraintDef) throws Exception {
    // conversion service may be needed for Value conversions
    TestUtil.startConversionService();
    ValueConstraintFactoryDescriptor desc = ValueConstraintExtension.INSTANCE.getForConstraint(constraint);
    // provide defaults for null parameters
    if (typeIndex == null) {
        typeIndex = new HashMap<>();
    }
    if (constraintDef == null) {
        constraintDef = getDefaultConstraintDefinition(constraint.getClass());
    }
    @SuppressWarnings("rawtypes") ValueConstraintFactory factory = desc.getFactory();
    Value val = factory.store(constraint, new MapTypeReferenceBuilder(typeIndex));
    T read;
    if (val != null) {
        // to DOM
        NSDOMBuilder builder = NSDOMBuilder.newBuilder(new HashMap<String, String>());
        Element elem = DOMValueUtil.valueTag(builder, "test", val);
        // from DOM
        Value res = DOMValueUtil.fromTag(elem);
        // bimap for reverse index
        BiMap<TypeDefinition, Value> types = HashBiMap.create(typeIndex);
        read = (T) factory.restore(res, constraintDef, new MapTypeResolver(types.inverse()), new OsgiClassResolver());
    } else {
        // fall back to default constraint
        Class<?> constraintType = ConstraintUtil.getConstraintType(constraint.getClass());
        read = (T) ConstraintUtil.getDefaultConstraint(constraintType, constraintDef);
    }
    compare(constraint, read);
}
Also used : NSDOMBuilder(eu.esdihumboldt.util.groovy.xml.NSDOMBuilder) Element(org.w3c.dom.Element) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) ValueConstraintFactoryDescriptor(eu.esdihumboldt.hale.common.schema.model.constraint.factory.extension.ValueConstraintFactoryDescriptor) Value(eu.esdihumboldt.hale.common.core.io.Value)

Aggregations

ValueConstraintFactoryDescriptor (eu.esdihumboldt.hale.common.schema.model.constraint.factory.extension.ValueConstraintFactoryDescriptor)2 Value (eu.esdihumboldt.hale.common.core.io.Value)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 GeometryMetadataFactory (eu.esdihumboldt.hale.common.schema.model.constraint.type.factory.GeometryMetadataFactory)1 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)1 NSDOMBuilder (eu.esdihumboldt.util.groovy.xml.NSDOMBuilder)1 Test (org.junit.Test)1 Element (org.w3c.dom.Element)1