From 8fbfc6d4ffbe690738a267a202fec3d4d7b31e74 Mon Sep 17 00:00:00 2001 From: Jim Blake Date: Sat, 6 Jan 2018 14:09:03 -0500 Subject: [PATCH] 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. --- .../utils/configuration/WrappedInstance.java | 17 +++---- .../ConfigurationBeanLoaderTest.java | 46 +++++++++---------- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/WrappedInstance.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/WrappedInstance.java index 344016863..ba4a2ab75 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/WrappedInstance.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/WrappedInstance.java @@ -70,7 +70,8 @@ public class WrappedInstance { */ public void checkCardinality(Set propertyStatements) throws CardinalityException { - Map statementCounts = countPropertyStatementsByPredicateUri(propertyStatements); + Map statementCounts = countPropertyStatementsByPredicateUri( + propertyStatements); for (PropertyMethod pm : propertyMethods.values()) { Integer c = statementCounts.get(pm.getPropertyUri()); int count = (c == null) ? 0 : c; @@ -109,12 +110,11 @@ public class WrappedInstance { */ public void setProperties(ConfigurationBeanLoader loader, Collection propertyStatements) - throws PropertyTypeException, NoSuchPropertyMethodException, - ConfigurationBeanLoaderException { + throws PropertyTypeException, ConfigurationBeanLoaderException { for (PropertyStatement ps : propertyStatements) { PropertyMethod pm = propertyMethods.get(ps.getPredicateUri()); if (pm == null) { - throw new NoSuchPropertyMethodException(ps); + continue; // No method for this property? Ignore it. } pm.confirmCompatible(ps); @@ -141,7 +141,8 @@ public class WrappedInstance { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new ValidationFailedException( - "Error executing validation method '" + method + "'", e); + "Error executing validation method '" + method + "'", + e); } } } @@ -165,12 +166,6 @@ public class WrappedInstance { } } - public static class NoSuchPropertyMethodException extends Exception { - public NoSuchPropertyMethodException(PropertyStatement ps) { - super("No property method for '" + ps.getPredicateUri() + "'"); - } - } - public static class CardinalityException extends Exception { public CardinalityException(String message) { super(message); diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/ConfigurationBeanLoaderTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/ConfigurationBeanLoaderTest.java index 17e9f2751..a0ebfa672 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/ConfigurationBeanLoaderTest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/utils/configuration/ConfigurationBeanLoaderTest.java @@ -2,12 +2,12 @@ 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.objectProperty; import static edu.cornell.mannlib.vitro.testing.ModelUtilitiesTestHelper.typeStatement; 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.assertNotNull; import static org.junit.Assert.assertTrue; @@ -19,18 +19,16 @@ import java.util.Collections; import java.util.HashSet; 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.Statement; +import org.junit.Ignore; +import org.junit.Test; import edu.cornell.mannlib.vitro.webapp.modelaccess.ContextModelAccess; 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.InstanceWrapper.InstanceWrapperException; 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; /** @@ -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 public void valueTypeDoesNotMatchArgumentOfPropertyMethod_throwsException() throws ConfigurationBeanLoaderException { @@ -395,10 +375,26 @@ public class ConfigurationBeanLoaderTest extends 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 { // Nothing of interest. } - + // -------------------------------------------- /**