ConfigurationBeanLoader will ignore unexpected properties on instances.

If no method has been annotated to accept that property, don't complain,
don't throw an exception, just continue.
This commit is contained in:
Jim Blake 2018-01-06 14:09:03 -05:00
parent 9e3ac4fb54
commit 8fbfc6d4ff
2 changed files with 27 additions and 36 deletions

View file

@ -70,7 +70,8 @@ public class WrappedInstance<T> {
*/ */
public void checkCardinality(Set<PropertyStatement> propertyStatements) public void checkCardinality(Set<PropertyStatement> propertyStatements)
throws CardinalityException { throws CardinalityException {
Map<String, Integer> statementCounts = countPropertyStatementsByPredicateUri(propertyStatements); Map<String, Integer> statementCounts = countPropertyStatementsByPredicateUri(
propertyStatements);
for (PropertyMethod pm : propertyMethods.values()) { for (PropertyMethod pm : propertyMethods.values()) {
Integer c = statementCounts.get(pm.getPropertyUri()); Integer c = statementCounts.get(pm.getPropertyUri());
int count = (c == null) ? 0 : c; int count = (c == null) ? 0 : c;
@ -109,12 +110,11 @@ public class WrappedInstance<T> {
*/ */
public void setProperties(ConfigurationBeanLoader loader, public void setProperties(ConfigurationBeanLoader loader,
Collection<PropertyStatement> propertyStatements) Collection<PropertyStatement> propertyStatements)
throws PropertyTypeException, NoSuchPropertyMethodException, throws PropertyTypeException, ConfigurationBeanLoaderException {
ConfigurationBeanLoaderException {
for (PropertyStatement ps : propertyStatements) { for (PropertyStatement ps : propertyStatements) {
PropertyMethod pm = propertyMethods.get(ps.getPredicateUri()); PropertyMethod pm = propertyMethods.get(ps.getPredicateUri());
if (pm == null) { if (pm == null) {
throw new NoSuchPropertyMethodException(ps); continue; // No method for this property? Ignore it.
} }
pm.confirmCompatible(ps); pm.confirmCompatible(ps);
@ -141,7 +141,8 @@ public class WrappedInstance<T> {
} catch (IllegalAccessException | IllegalArgumentException } catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) { | InvocationTargetException e) {
throw new ValidationFailedException( throw new ValidationFailedException(
"Error executing validation method '" + method + "'", e); "Error executing validation method '" + method + "'",
e);
} }
} }
} }
@ -165,12 +166,6 @@ public class WrappedInstance<T> {
} }
} }
public static class NoSuchPropertyMethodException extends Exception {
public NoSuchPropertyMethodException(PropertyStatement ps) {
super("No property method for '" + ps.getPredicateUri() + "'");
}
}
public static class CardinalityException extends Exception { public static class CardinalityException extends Exception {
public CardinalityException(String message) { public CardinalityException(String message) {
super(message); super(message);

View file

@ -2,12 +2,12 @@
package edu.cornell.mannlib.vitro.webapp.utils.configuration; package edu.cornell.mannlib.vitro.webapp.utils.configuration;
import static org.apache.jena.datatypes.xsd.XSDDatatype.XSDfloat;
import static org.apache.jena.datatypes.xsd.XSDDatatype.XSDstring;
import static edu.cornell.mannlib.vitro.testing.ModelUtilitiesTestHelper.dataProperty; import static edu.cornell.mannlib.vitro.testing.ModelUtilitiesTestHelper.dataProperty;
import static edu.cornell.mannlib.vitro.testing.ModelUtilitiesTestHelper.objectProperty; import static edu.cornell.mannlib.vitro.testing.ModelUtilitiesTestHelper.objectProperty;
import static edu.cornell.mannlib.vitro.testing.ModelUtilitiesTestHelper.typeStatement; import static edu.cornell.mannlib.vitro.testing.ModelUtilitiesTestHelper.typeStatement;
import static edu.cornell.mannlib.vitro.webapp.utils.configuration.ConfigurationBeanLoader.toJavaUri; import static edu.cornell.mannlib.vitro.webapp.utils.configuration.ConfigurationBeanLoader.toJavaUri;
import static org.apache.jena.datatypes.xsd.XSDDatatype.XSDfloat;
import static org.apache.jena.datatypes.xsd.XSDDatatype.XSDstring;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -19,18 +19,16 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Statement; import org.apache.jena.rdf.model.Statement;
import org.junit.Ignore;
import org.junit.Test;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ContextModelAccess; import edu.cornell.mannlib.vitro.webapp.modelaccess.ContextModelAccess;
import edu.cornell.mannlib.vitro.webapp.modelaccess.RequestModelAccess; import edu.cornell.mannlib.vitro.webapp.modelaccess.RequestModelAccess;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.ConfigurationRdfParser.InvalidConfigurationRdfException; import edu.cornell.mannlib.vitro.webapp.utils.configuration.ConfigurationRdfParser.InvalidConfigurationRdfException;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.InstanceWrapper.InstanceWrapperException; import edu.cornell.mannlib.vitro.webapp.utils.configuration.InstanceWrapper.InstanceWrapperException;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.PropertyType.PropertyTypeException; import edu.cornell.mannlib.vitro.webapp.utils.configuration.PropertyType.PropertyTypeException;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.WrappedInstance.NoSuchPropertyMethodException;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.WrappedInstance.ResourceUnavailableException; import edu.cornell.mannlib.vitro.webapp.utils.configuration.WrappedInstance.ResourceUnavailableException;
/** /**
@ -309,24 +307,6 @@ public class ConfigurationBeanLoaderTest extends
// -------------------------------------------- // --------------------------------------------
@Test
public void tripleHasUnrecognizedProperty_throwsException()
throws ConfigurationBeanLoaderException {
model.add(typeStatement(GENERIC_INSTANCE_URI,
toJavaUri(SimpleSuccess.class)));
model.add(dataProperty(GENERIC_INSTANCE_URI,
"http://bogus.property/name", "No place to put it."));
expectSimpleFailure(
SimpleSuccess.class,
throwable(ConfigurationBeanLoaderException.class,
"Failed to load"),
throwable(NoSuchPropertyMethodException.class,
"No property method"));
}
// --------------------------------------------
@Test @Test
public void valueTypeDoesNotMatchArgumentOfPropertyMethod_throwsException() public void valueTypeDoesNotMatchArgumentOfPropertyMethod_throwsException()
throws ConfigurationBeanLoaderException { throws ConfigurationBeanLoaderException {
@ -395,6 +375,22 @@ public class ConfigurationBeanLoaderTest extends
assertNotNull(instance); assertNotNull(instance);
} }
/**
* Ignores unexpected properties
*/
@Test
public void simpleSuccessIgnoringExtraProperties() throws ConfigurationBeanLoaderException {
model.add(typeStatement(SIMPLE_SUCCESS_INSTANCE_URI,
toJavaUri(SimpleSuccess.class)));
model.add(dataProperty(SIMPLE_SUCCESS_INSTANCE_URI,
"http://surprise.property/name", "No matching method."));
SimpleSuccess instance = loader.loadInstance(
SIMPLE_SUCCESS_INSTANCE_URI, SimpleSuccess.class);
assertNotNull(instance);
}
public static class SimpleSuccess { public static class SimpleSuccess {
// Nothing of interest. // Nothing of interest.
} }