diff --git a/src/main/java/com/github/fge/jsonschema/keyword/validator/KeywordValidatorFactory.java b/src/main/java/com/github/fge/jsonschema/keyword/validator/KeywordValidatorFactory.java new file mode 100755 index 000000000..db84c642e --- /dev/null +++ b/src/main/java/com/github/fge/jsonschema/keyword/validator/KeywordValidatorFactory.java @@ -0,0 +1,20 @@ +package com.github.fge.jsonschema.keyword.validator; + +import com.fasterxml.jackson.databind.JsonNode; +import com.github.fge.jsonschema.core.exceptions.ProcessingException; + +/** + * Interface for a keyword validator factory + */ +public interface KeywordValidatorFactory +{ + /** + * Create a validator for the instance + * + * @param node the instance to validate + * @return a validator for the given instance + * @throws ProcessingException an error occurs creating the validator + */ + KeywordValidator getKeywordValidator(JsonNode node) + throws ProcessingException; +} diff --git a/src/main/java/com/github/fge/jsonschema/keyword/validator/ReflectionKeywordValidatorFactory.java b/src/main/java/com/github/fge/jsonschema/keyword/validator/ReflectionKeywordValidatorFactory.java new file mode 100755 index 000000000..54e394c1c --- /dev/null +++ b/src/main/java/com/github/fge/jsonschema/keyword/validator/ReflectionKeywordValidatorFactory.java @@ -0,0 +1,52 @@ +package com.github.fge.jsonschema.keyword.validator; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.github.fge.jsonschema.core.exceptions.ProcessingException; +import com.github.fge.jsonschema.messages.JsonSchemaConfigurationBundle; +import com.github.fge.msgsimple.bundle.MessageBundle; +import com.github.fge.msgsimple.load.MessageBundles; + +/** + * A validator factory that uses reflection to create an instance of the + * specified KeywordValidator class + */ +public class ReflectionKeywordValidatorFactory + implements KeywordValidatorFactory +{ + private static final String ERRMSG = "failed to build keyword validator"; + private static final MessageBundle BUNDLE + = MessageBundles.getBundle(JsonSchemaConfigurationBundle.class); + + private final Constructor constructor; + + public ReflectionKeywordValidatorFactory(String name, + Class clazz) + { + try { + constructor = clazz.getConstructor(JsonNode.class); + } catch (NoSuchMethodException ignored) { + throw new IllegalArgumentException(BUNDLE.printf( + "noAppropriateConstructor", name, clazz.getCanonicalName() + )); + } + } + + @Override + public KeywordValidator getKeywordValidator(JsonNode node) + throws ProcessingException + { + try { + return constructor.newInstance(node); + } catch (InstantiationException e) { + throw new ProcessingException(ERRMSG, e); + } catch (IllegalAccessException e) { + throw new ProcessingException(ERRMSG, e); + } catch (InvocationTargetException e) { + throw new ProcessingException(ERRMSG, e); + } + } + +} diff --git a/src/main/java/com/github/fge/jsonschema/library/Keyword.java b/src/main/java/com/github/fge/jsonschema/library/Keyword.java index b9a6d8ea1..adace7d33 100644 --- a/src/main/java/com/github/fge/jsonschema/library/Keyword.java +++ b/src/main/java/com/github/fge/jsonschema/library/Keyword.java @@ -22,9 +22,7 @@ import com.github.fge.Frozen; import com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxChecker; import com.github.fge.jsonschema.keyword.digest.Digester; -import com.github.fge.jsonschema.keyword.validator.KeywordValidator; - -import java.lang.reflect.Constructor; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; /** @@ -51,9 +49,9 @@ public final class Keyword final Digester digester; /** - * {@link KeywordValidator} constructor + * Validator factory */ - final Constructor constructor; + final KeywordValidatorFactory validatorFactory; /** * Instantiate a new keyword builder @@ -79,7 +77,7 @@ public static KeywordBuilder newBuilder(final String name) name = builder.name; syntaxChecker = builder.syntaxChecker; digester = builder.digester; - constructor = builder.constructor; + validatorFactory = builder.validatorFactory; } /** diff --git a/src/main/java/com/github/fge/jsonschema/library/KeywordBuilder.java b/src/main/java/com/github/fge/jsonschema/library/KeywordBuilder.java index fd1f9b330..553b1f8a0 100644 --- a/src/main/java/com/github/fge/jsonschema/library/KeywordBuilder.java +++ b/src/main/java/com/github/fge/jsonschema/library/KeywordBuilder.java @@ -19,7 +19,6 @@ package com.github.fge.jsonschema.library; -import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.Thawed; import com.github.fge.jackson.NodeType; import com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxChecker; @@ -27,12 +26,12 @@ import com.github.fge.jsonschema.keyword.digest.helpers.IdentityDigester; import com.github.fge.jsonschema.keyword.digest.helpers.SimpleDigester; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; +import com.github.fge.jsonschema.keyword.validator.ReflectionKeywordValidatorFactory; import com.github.fge.jsonschema.messages.JsonSchemaConfigurationBundle; import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; -import java.lang.reflect.Constructor; - /** * A keyword builder -- the thawed version of a {@link Keyword} * @@ -48,7 +47,7 @@ public final class KeywordBuilder final String name; SyntaxChecker syntaxChecker; Digester digester; - Constructor constructor; + KeywordValidatorFactory validatorFactory; /** * Create a new, empty keyword builder @@ -74,7 +73,7 @@ public final class KeywordBuilder name = keyword.name; syntaxChecker = keyword.syntaxChecker; digester = keyword.digester; - constructor = keyword.constructor; + validatorFactory = keyword.validatorFactory; } /** @@ -149,7 +148,20 @@ public KeywordBuilder withSimpleDigester(final NodeType first, public KeywordBuilder withValidatorClass( final Class c) { - constructor = getConstructor(name, c); + validatorFactory = new ReflectionKeywordValidatorFactory(name, c); + return this; + } + + /** + * Set the validator factory for this keyword + * + * @param factory the factory + * @return this + */ + public KeywordBuilder withValidatorFactory( + KeywordValidatorFactory factory) + { + validatorFactory = factory; return this; } @@ -169,24 +181,11 @@ public Keyword freeze() * We can have a keyword without a validator; however, if there is one, * there must be a digester. */ - BUNDLE.checkArgumentPrintf(constructor == null || digester != null, + BUNDLE.checkArgumentPrintf(validatorFactory == null || digester != null, "malformedKeyword", name); return new Keyword(this); } - - private static Constructor getConstructor( - final String name, final Class c) - { - try { - return c.getConstructor(JsonNode.class); - } catch (NoSuchMethodException ignored) { - throw new IllegalArgumentException(BUNDLE.printf( - "noAppropriateConstructor", name, c.getCanonicalName() - )); - } - } - private static NodeType checkType(final NodeType type) { return BUNDLE.checkNotNull(type, "nullType"); diff --git a/src/main/java/com/github/fge/jsonschema/library/Library.java b/src/main/java/com/github/fge/jsonschema/library/Library.java index 74aa566ce..24087f1a6 100644 --- a/src/main/java/com/github/fge/jsonschema/library/Library.java +++ b/src/main/java/com/github/fge/jsonschema/library/Library.java @@ -26,9 +26,7 @@ import com.github.fge.jsonschema.core.util.Dictionary; import com.github.fge.jsonschema.format.FormatAttribute; import com.github.fge.jsonschema.keyword.digest.Digester; -import com.github.fge.jsonschema.keyword.validator.KeywordValidator; - -import java.lang.reflect.Constructor; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; /** * A schema keyword library @@ -54,9 +52,9 @@ public final class Library final Dictionary digesters; /** - * Dictionary of keyword validator constructors + * Dictionary of keyword validator factories */ - final Dictionary> validators; + final Dictionary validators; /** * Dictionary of format attributes @@ -97,7 +95,7 @@ public static LibraryBuilder newBuilder() */ Library(final Dictionary syntaxCheckers, final Dictionary digesters, - final Dictionary> validators, + final Dictionary validators, final Dictionary formatAttributes) { this.syntaxCheckers = syntaxCheckers; @@ -131,7 +129,7 @@ public Dictionary getDigesters() * * @return a dictionary */ - public Dictionary> getValidators() + public Dictionary getValidators() { return validators; } diff --git a/src/main/java/com/github/fge/jsonschema/library/LibraryBuilder.java b/src/main/java/com/github/fge/jsonschema/library/LibraryBuilder.java index b65f64264..93d2a170a 100644 --- a/src/main/java/com/github/fge/jsonschema/library/LibraryBuilder.java +++ b/src/main/java/com/github/fge/jsonschema/library/LibraryBuilder.java @@ -25,13 +25,11 @@ import com.github.fge.jsonschema.core.util.DictionaryBuilder; import com.github.fge.jsonschema.format.FormatAttribute; import com.github.fge.jsonschema.keyword.digest.Digester; -import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; import com.github.fge.jsonschema.messages.JsonSchemaConfigurationBundle; import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; -import java.lang.reflect.Constructor; - /** * Mutable version of a library * @@ -60,7 +58,7 @@ public final class LibraryBuilder /** * Dictionary builder of keyword validator constructors */ - final DictionaryBuilder> validators; + final DictionaryBuilder validators; /** * Dictionary builder of format attributes @@ -107,9 +105,9 @@ public LibraryBuilder addKeyword(final Keyword keyword) syntaxCheckers.addEntry(name, keyword.syntaxChecker); - if (keyword.constructor != null) { + if (keyword.validatorFactory != null) { digesters.addEntry(name, keyword.digester); - validators.addEntry(name, keyword.constructor); + validators.addEntry(name, keyword.validatorFactory); } return this; } diff --git a/src/main/java/com/github/fge/jsonschema/library/validator/CommonValidatorDictionary.java b/src/main/java/com/github/fge/jsonschema/library/validator/CommonValidatorDictionary.java index 20ade1257..b96141b72 100644 --- a/src/main/java/com/github/fge/jsonschema/library/validator/CommonValidatorDictionary.java +++ b/src/main/java/com/github/fge/jsonschema/library/validator/CommonValidatorDictionary.java @@ -19,10 +19,11 @@ package com.github.fge.jsonschema.library.validator; -import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jsonschema.core.util.Dictionary; import com.github.fge.jsonschema.core.util.DictionaryBuilder; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; +import com.github.fge.jsonschema.keyword.validator.ReflectionKeywordValidatorFactory; import com.github.fge.jsonschema.keyword.validator.common.AdditionalItemsValidator; import com.github.fge.jsonschema.keyword.validator.common.AdditionalPropertiesValidator; import com.github.fge.jsonschema.keyword.validator.common.EnumValidator; @@ -35,27 +36,25 @@ import com.github.fge.jsonschema.keyword.validator.common.PatternValidator; import com.github.fge.jsonschema.keyword.validator.common.UniqueItemsValidator; -import java.lang.reflect.Constructor; - /** * Keyword validator constructors common to draft v4 and v3 */ public final class CommonValidatorDictionary { - private static final Dictionary> + private static final Dictionary DICTIONARY; private CommonValidatorDictionary() { } - public static Dictionary> get() + public static Dictionary get() { return DICTIONARY; } static { - final DictionaryBuilder> + final DictionaryBuilder builder = Dictionary.newBuilder(); String keyword; @@ -66,67 +65,63 @@ public static Dictionary> get() */ keyword = "additionalItems"; c = AdditionalItemsValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "minItems"; c = MinItemsValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "maxItems"; c = MaxItemsValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "uniqueItems"; c = UniqueItemsValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); /* * Numbers and integers */ keyword = "minimum"; c = MinimumValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "maximum"; c = MaximumValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); /* * Objects */ keyword = "additionalProperties"; c = AdditionalPropertiesValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); /* * Strings */ keyword = "minLength"; c = MinLengthValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "maxLength"; c = MaxLengthValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "pattern"; c = PatternValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "enum"; c = EnumValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); DICTIONARY = builder.freeze(); } - private static Constructor constructor( + private static KeywordValidatorFactory factory(String name, final Class c) { - try { - return c.getConstructor(JsonNode.class); - } catch (NoSuchMethodException e) { - throw new RuntimeException("No appropriate constructor", e); - } + return new ReflectionKeywordValidatorFactory(name, c); } } diff --git a/src/main/java/com/github/fge/jsonschema/library/validator/DraftV3ValidatorDictionary.java b/src/main/java/com/github/fge/jsonschema/library/validator/DraftV3ValidatorDictionary.java index a6d648c11..ecf982b1f 100644 --- a/src/main/java/com/github/fge/jsonschema/library/validator/DraftV3ValidatorDictionary.java +++ b/src/main/java/com/github/fge/jsonschema/library/validator/DraftV3ValidatorDictionary.java @@ -19,10 +19,11 @@ package com.github.fge.jsonschema.library.validator; -import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jsonschema.core.util.Dictionary; import com.github.fge.jsonschema.core.util.DictionaryBuilder; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; +import com.github.fge.jsonschema.keyword.validator.ReflectionKeywordValidatorFactory; import com.github.fge.jsonschema.keyword.validator.common.DependenciesValidator; import com.github.fge.jsonschema.keyword.validator.draftv3.DisallowKeywordValidator; import com.github.fge.jsonschema.keyword.validator.draftv3.DivisibleByValidator; @@ -30,27 +31,25 @@ import com.github.fge.jsonschema.keyword.validator.draftv3.ExtendsValidator; import com.github.fge.jsonschema.keyword.validator.draftv3.PropertiesValidator; -import java.lang.reflect.Constructor; - /** * Draft v3 specific keyword validator constructors */ public final class DraftV3ValidatorDictionary { - private static final Dictionary> + private static final Dictionary DICTIONARY; private DraftV3ValidatorDictionary() { } - public static Dictionary> get() + public static Dictionary get() { return DICTIONARY; } static { - final DictionaryBuilder> + final DictionaryBuilder builder = Dictionary.newBuilder(); String keyword; @@ -63,41 +62,37 @@ public static Dictionary> get() */ keyword = "divisibleBy"; c = DivisibleByValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); /* * Object */ keyword = "properties"; c = PropertiesValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "dependencies"; c = DependenciesValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "type"; c = DraftV3TypeValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "disallow"; c = DisallowKeywordValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "extends"; c = ExtendsValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); DICTIONARY = builder.freeze(); } - private static Constructor constructor( + private static KeywordValidatorFactory factory(String name, final Class c) { - try { - return c.getConstructor(JsonNode.class); - } catch (NoSuchMethodException e) { - throw new RuntimeException("No appropriate constructor found", e); - } + return new ReflectionKeywordValidatorFactory(name, c); } } diff --git a/src/main/java/com/github/fge/jsonschema/library/validator/DraftV4ValidatorDictionary.java b/src/main/java/com/github/fge/jsonschema/library/validator/DraftV4ValidatorDictionary.java index 6e517c7c9..a82a74b49 100644 --- a/src/main/java/com/github/fge/jsonschema/library/validator/DraftV4ValidatorDictionary.java +++ b/src/main/java/com/github/fge/jsonschema/library/validator/DraftV4ValidatorDictionary.java @@ -19,10 +19,11 @@ package com.github.fge.jsonschema.library.validator; -import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jsonschema.core.util.Dictionary; import com.github.fge.jsonschema.core.util.DictionaryBuilder; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; +import com.github.fge.jsonschema.keyword.validator.ReflectionKeywordValidatorFactory; import com.github.fge.jsonschema.keyword.validator.common.DependenciesValidator; import com.github.fge.jsonschema.keyword.validator.draftv4.AllOfValidator; import com.github.fge.jsonschema.keyword.validator.draftv4.AnyOfValidator; @@ -34,27 +35,25 @@ import com.github.fge.jsonschema.keyword.validator.draftv4.OneOfValidator; import com.github.fge.jsonschema.keyword.validator.draftv4.RequiredKeywordValidator; -import java.lang.reflect.Constructor; - /** * Draft v4 specific keyword validator constructors */ public final class DraftV4ValidatorDictionary { - private static final Dictionary> + private static final Dictionary DICTIONARY; private DraftV4ValidatorDictionary() { } - public static Dictionary> get() + public static Dictionary get() { return DICTIONARY; } static { - final DictionaryBuilder> + final DictionaryBuilder builder = Dictionary.newBuilder(); String keyword; @@ -67,60 +66,56 @@ public static Dictionary> get() */ keyword = "multipleOf"; c = MultipleOfValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); /* * Object */ keyword = "minProperties"; c = MinPropertiesValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "maxProperties"; c = MaxPropertiesValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "required"; c = RequiredKeywordValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "dependencies"; c = DependenciesValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); /* * All */ keyword = "anyOf"; c = AnyOfValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "allOf"; c = AllOfValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "oneOf"; c = OneOfValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "not"; c = NotValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); keyword = "type"; c = DraftV4TypeValidator.class; - builder.addEntry(keyword, constructor(c)); + builder.addEntry(keyword, factory(keyword, c)); DICTIONARY = builder.freeze(); } - private static Constructor constructor( + private static KeywordValidatorFactory factory(String name, final Class c) { - try { - return c.getConstructor(JsonNode.class); - } catch (NoSuchMethodException e) { - throw new RuntimeException("No appropriate constructor found", e); - } + return new ReflectionKeywordValidatorFactory(name, c); } } diff --git a/src/main/java/com/github/fge/jsonschema/processors/build/ValidatorBuilder.java b/src/main/java/com/github/fge/jsonschema/processors/build/ValidatorBuilder.java index f8fba5171..27409c83b 100644 --- a/src/main/java/com/github/fge/jsonschema/processors/build/ValidatorBuilder.java +++ b/src/main/java/com/github/fge/jsonschema/processors/build/ValidatorBuilder.java @@ -19,23 +19,22 @@ package com.github.fge.jsonschema.processors.build; +import java.util.Map; +import java.util.SortedMap; + import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jsonschema.core.exceptions.ProcessingException; import com.github.fge.jsonschema.core.processing.Processor; import com.github.fge.jsonschema.core.report.ProcessingReport; import com.github.fge.jsonschema.core.util.Dictionary; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; import com.github.fge.jsonschema.library.Library; import com.github.fge.jsonschema.processors.data.SchemaDigest; import com.github.fge.jsonschema.processors.data.ValidatorList; import com.github.fge.jsonschema.processors.validation.ValidationProcessor; import com.google.common.collect.Maps; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.Map; -import java.util.SortedMap; - /** * Keyword builder processor * @@ -48,20 +47,18 @@ public final class ValidatorBuilder implements Processor { - private static final String ERRMSG = "failed to build keyword validator"; - - private final Map> - constructors; + private final Map + factories; public ValidatorBuilder(final Library library) { - constructors = library.getValidators().entries(); + factories = library.getValidators().entries(); } public ValidatorBuilder( - final Dictionary> dict) + final Dictionary dict) { - constructors = dict.entries(); + factories = dict.entries(); } /** @@ -82,35 +79,19 @@ public ValidatorList process(final ProcessingReport report, String keyword; JsonNode digest; KeywordValidator validator; - Constructor constructor; + KeywordValidatorFactory factory; for (final Map.Entry entry: input.getDigests().entrySet()) { keyword = entry.getKey(); digest = entry.getValue(); - constructor = constructors.get(keyword); - validator = buildKeyword(constructor, digest); + factory = factories.get(keyword); + validator = factory.getKeywordValidator(digest); map.put(keyword, validator); } return new ValidatorList(input.getContext(), map.values()); } - private static KeywordValidator buildKeyword( - final Constructor constructor, - final JsonNode node) - throws ProcessingException - { - try { - return constructor.newInstance(node); - } catch (InstantiationException e) { - throw new ProcessingException(ERRMSG, e); - } catch (IllegalAccessException e) { - throw new ProcessingException(ERRMSG, e); - } catch (InvocationTargetException e) { - throw new ProcessingException(ERRMSG, e); - } - } - @Override public String toString() { diff --git a/src/test/java/com/github/fge/jsonschema/keyword/special/ExtendsKeywordTest.java b/src/test/java/com/github/fge/jsonschema/keyword/special/ExtendsKeywordTest.java index 36bcee0c9..32fab2b55 100644 --- a/src/test/java/com/github/fge/jsonschema/keyword/special/ExtendsKeywordTest.java +++ b/src/test/java/com/github/fge/jsonschema/keyword/special/ExtendsKeywordTest.java @@ -19,6 +19,19 @@ package com.github.fge.jsonschema.keyword.special; +import static com.github.fge.jsonschema.TestUtils.anyMessage; +import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.assertMessage; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.fail; + +import org.mockito.ArgumentCaptor; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.github.fge.jackson.JacksonUtils; @@ -33,22 +46,12 @@ import com.github.fge.jsonschema.core.tree.SimpleJsonTree; import com.github.fge.jsonschema.core.tree.key.SchemaKey; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; import com.github.fge.jsonschema.library.validator.DraftV3ValidatorDictionary; import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle; import com.github.fge.jsonschema.processors.data.FullData; import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; -import org.mockito.ArgumentCaptor; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -import static com.github.fge.jsonschema.TestUtils.*; -import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.*; -import static org.mockito.Mockito.*; -import static org.testng.Assert.*; public final class ExtendsKeywordTest { @@ -65,13 +68,12 @@ public final class ExtendsKeywordTest private ProcessingMessage msg; public ExtendsKeywordTest() - throws IllegalAccessException, InvocationTargetException, - InstantiationException + throws ProcessingException { - final Constructor constructor + final KeywordValidatorFactory factory = DraftV3ValidatorDictionary.get().entries().get("extends"); - validator = constructor == null ? null - : constructor.newInstance(FACTORY.nullNode()); + validator = factory == null ? null + : factory.getKeywordValidator(FACTORY.nullNode()); } @BeforeMethod diff --git a/src/test/java/com/github/fge/jsonschema/keyword/special/NotKeywordTest.java b/src/test/java/com/github/fge/jsonschema/keyword/special/NotKeywordTest.java index 3270529a3..243f1ddf8 100644 --- a/src/test/java/com/github/fge/jsonschema/keyword/special/NotKeywordTest.java +++ b/src/test/java/com/github/fge/jsonschema/keyword/special/NotKeywordTest.java @@ -19,6 +19,20 @@ package com.github.fge.jsonschema.keyword.special; +import static com.github.fge.jsonschema.TestUtils.anyMessage; +import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.assertMessage; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.fail; + +import org.mockito.ArgumentCaptor; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.github.fge.jackson.JacksonUtils; @@ -34,22 +48,12 @@ import com.github.fge.jsonschema.core.tree.SimpleJsonTree; import com.github.fge.jsonschema.core.tree.key.SchemaKey; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; import com.github.fge.jsonschema.library.validator.DraftV4ValidatorDictionary; import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle; import com.github.fge.jsonschema.processors.data.FullData; import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; -import org.mockito.ArgumentCaptor; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -import static com.github.fge.jsonschema.TestUtils.*; -import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.*; -import static org.mockito.Mockito.*; -import static org.testng.Assert.*; public final class NotKeywordTest { @@ -65,13 +69,12 @@ public final class NotKeywordTest private ProcessingReport report; public NotKeywordTest() - throws IllegalAccessException, InvocationTargetException, - InstantiationException + throws ProcessingException { - final Constructor constructor + final KeywordValidatorFactory factory = DraftV4ValidatorDictionary.get().entries().get("not"); - validator = constructor == null ? null - : constructor.newInstance(FACTORY.nullNode()); + validator = factory == null ? null + : factory.getKeywordValidator(FACTORY.nullNode()); } @BeforeMethod diff --git a/src/test/java/com/github/fge/jsonschema/keyword/special/PatternKeywordTest.java b/src/test/java/com/github/fge/jsonschema/keyword/special/PatternKeywordTest.java index 6ff655ae0..873041061 100644 --- a/src/test/java/com/github/fge/jsonschema/keyword/special/PatternKeywordTest.java +++ b/src/test/java/com/github/fge/jsonschema/keyword/special/PatternKeywordTest.java @@ -19,6 +19,22 @@ package com.github.fge.jsonschema.keyword.special; +import static com.github.fge.jsonschema.TestUtils.anyMessage; +import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.assertMessage; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.testng.Assert.assertNotNull; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Iterator; +import java.util.List; + +import org.mockito.ArgumentCaptor; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.github.fge.jackson.JsonLoader; @@ -33,26 +49,13 @@ import com.github.fge.jsonschema.core.tree.SimpleJsonTree; import com.github.fge.jsonschema.core.tree.key.SchemaKey; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; import com.github.fge.jsonschema.library.validator.CommonValidatorDictionary; import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle; import com.github.fge.jsonschema.processors.data.FullData; import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; import com.google.common.collect.Lists; -import org.mockito.ArgumentCaptor; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.Iterator; -import java.util.List; - -import static com.github.fge.jsonschema.TestUtils.*; -import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.*; -import static org.mockito.Mockito.*; -import static org.testng.Assert.*; public final class PatternKeywordTest { @@ -65,20 +68,20 @@ public final class PatternKeywordTest private static final MessageBundle BUNDLE = MessageBundles.getBundle(JsonSchemaValidationBundle.class); - private final Constructor constructor; + private final KeywordValidatorFactory factory; private final JsonNode testData; public PatternKeywordTest() throws IOException { - constructor = CommonValidatorDictionary.get().entries().get("pattern"); + factory = CommonValidatorDictionary.get().entries().get("pattern"); testData = JsonLoader.fromResource("/keyword/special/pattern.json"); } @Test public void keywordExists() { - assertNotNull(constructor, "no support for pattern??"); + assertNotNull(factory, "no support for pattern??"); } @DataProvider @@ -120,7 +123,7 @@ public void instancesAreValidatedCorrectly(final JsonNode schema, // It is a null node which is ignored by the constructor, so we can // do that - final KeywordValidator validator = constructor.newInstance(schema); + final KeywordValidator validator = factory.getKeywordValidator(schema); validator.validate(processor, report, BUNDLE, data); if (valid) { diff --git a/src/test/java/com/github/fge/jsonschema/keyword/validator/AbstractKeywordValidatorTest.java b/src/test/java/com/github/fge/jsonschema/keyword/validator/AbstractKeywordValidatorTest.java index aa69bcec9..9916d8a8d 100644 --- a/src/test/java/com/github/fge/jsonschema/keyword/validator/AbstractKeywordValidatorTest.java +++ b/src/test/java/com/github/fge/jsonschema/keyword/validator/AbstractKeywordValidatorTest.java @@ -19,6 +19,22 @@ package com.github.fge.jsonschema.keyword.validator; +import static com.github.fge.jsonschema.TestUtils.anyMessage; +import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.assertMessage; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.testng.Assert.assertNotNull; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Iterator; +import java.util.List; + +import org.mockito.ArgumentCaptor; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.github.fge.jackson.JsonLoader; @@ -38,20 +54,6 @@ import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; import com.google.common.collect.Lists; -import org.mockito.ArgumentCaptor; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.Iterator; -import java.util.List; - -import static com.github.fge.jsonschema.TestUtils.*; -import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.*; -import static org.mockito.Mockito.*; -import static org.testng.Assert.*; @Test public abstract class AbstractKeywordValidatorTest @@ -60,16 +62,16 @@ public abstract class AbstractKeywordValidatorTest = MessageBundles.getBundle(JsonSchemaValidationBundle.class); private final String keyword; - private final Constructor constructor; + private final KeywordValidatorFactory factory; private final JsonNode testNode; protected AbstractKeywordValidatorTest( - final Dictionary> dict, + final Dictionary dict, final String prefix, final String keyword) throws IOException { this.keyword = keyword; - constructor = dict.entries().get(keyword); + factory = dict.entries().get(keyword); final String resourceName = String.format("/keyword/validators/%s/%s.json", prefix, keyword); testNode = JsonLoader.fromResource(resourceName); @@ -78,7 +80,7 @@ protected AbstractKeywordValidatorTest( @Test public final void keywordExists() { - assertNotNull(constructor, "no support for " + keyword + "??"); + assertNotNull(factory, "no support for " + keyword + "??"); } @DataProvider @@ -120,7 +122,7 @@ public final void instancesAreValidatedCorrectly(final JsonNode digest, @SuppressWarnings("unchecked") final Processor processor = mock(Processor.class); - final KeywordValidator validator = constructor.newInstance(digest); + final KeywordValidator validator = factory.getKeywordValidator(digest); validator.validate(processor, report, BUNDLE, data); if (valid) { diff --git a/src/test/java/com/github/fge/jsonschema/keyword/validator/callback/CallbackValidatorTest.java b/src/test/java/com/github/fge/jsonschema/keyword/validator/callback/CallbackValidatorTest.java index 07ba21ec8..d5d936e94 100644 --- a/src/test/java/com/github/fge/jsonschema/keyword/validator/callback/CallbackValidatorTest.java +++ b/src/test/java/com/github/fge/jsonschema/keyword/validator/callback/CallbackValidatorTest.java @@ -19,6 +19,21 @@ package com.github.fge.jsonschema.keyword.validator.callback; +import static com.github.fge.jsonschema.TestUtils.anyReport; +import static com.github.fge.jsonschema.TestUtils.onlyOnce; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.fail; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -36,19 +51,11 @@ import com.github.fge.jsonschema.core.tree.key.SchemaKey; import com.github.fge.jsonschema.core.util.Dictionary; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle; import com.github.fge.jsonschema.processors.data.FullData; import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -import static com.github.fge.jsonschema.TestUtils.*; -import static org.mockito.Mockito.*; -import static org.testng.Assert.*; @Test public abstract class CallbackValidatorTest @@ -62,7 +69,7 @@ public abstract class CallbackValidatorTest protected static final ObjectNode sub2 = FACTORY.objectNode(); protected final String keyword; - private final Constructor constructor; + private final KeywordValidatorFactory factory; protected final JsonPointer ptr1; protected final JsonPointer ptr2; @@ -72,21 +79,20 @@ public abstract class CallbackValidatorTest private KeywordValidator validator; protected CallbackValidatorTest( - final Dictionary> dict, + final Dictionary dict, final String keyword, final JsonPointer ptr1, final JsonPointer ptr2) { this.keyword = keyword; - constructor = dict.entries().get(keyword); + factory = dict.entries().get(keyword); this.ptr1 = ptr1; this.ptr2 = ptr2; } @BeforeMethod protected final void initEnvironment() - throws IllegalAccessException, InvocationTargetException, - InstantiationException + throws ProcessingException { - if (constructor == null) + if (factory == null) return; final SchemaTree tree = new CanonicalSchemaTree( @@ -95,13 +101,13 @@ protected final void initEnvironment() data = new FullData(tree, instance); report = mock(ProcessingReport.class); when(report.getLogLevel()).thenReturn(LogLevel.DEBUG); - validator = constructor.newInstance(generateDigest()); + validator = factory.getKeywordValidator(generateDigest()); } @Test public final void keywordExists() { - assertNotNull(constructor, "no support for " + keyword + "??"); + assertNotNull(factory, "no support for " + keyword + "??"); } @Test(dependsOnMethods = "keywordExists") diff --git a/src/test/java/com/github/fge/jsonschema/processors/build/ValidatorBuilderTest.java b/src/test/java/com/github/fge/jsonschema/processors/build/ValidatorBuilderTest.java index 4887af34a..8f038c38c 100644 --- a/src/test/java/com/github/fge/jsonschema/processors/build/ValidatorBuilderTest.java +++ b/src/test/java/com/github/fge/jsonschema/processors/build/ValidatorBuilderTest.java @@ -19,6 +19,16 @@ package com.github.fge.jsonschema.processors.build; +import static org.mockito.Mockito.mock; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertSame; +import static org.testng.Assert.fail; + +import java.util.List; +import java.util.Map; + +import org.testng.annotations.Test; + import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jackson.JacksonUtils; import com.github.fge.jsonschema.core.exceptions.ProcessingException; @@ -27,20 +37,14 @@ import com.github.fge.jsonschema.core.util.Dictionary; import com.github.fge.jsonschema.core.util.DictionaryBuilder; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; +import com.github.fge.jsonschema.keyword.validator.KeywordValidatorFactory; +import com.github.fge.jsonschema.keyword.validator.ReflectionKeywordValidatorFactory; import com.github.fge.jsonschema.processors.data.FullData; import com.github.fge.jsonschema.processors.data.SchemaDigest; import com.github.fge.jsonschema.processors.data.ValidatorList; import com.github.fge.msgsimple.bundle.MessageBundle; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.testng.annotations.Test; - -import java.lang.reflect.Constructor; -import java.util.List; -import java.util.Map; - -import static org.mockito.Mockito.*; -import static org.testng.Assert.*; public final class ValidatorBuilderTest { @@ -53,17 +57,17 @@ public final class ValidatorBuilderTest public ValidatorBuilderTest() throws NoSuchMethodException { - final DictionaryBuilder> + final DictionaryBuilder builder = Dictionary.newBuilder(); - Constructor constructor; + KeywordValidatorFactory factory; - constructor = Keyword1.class .getConstructor(JsonNode.class); - builder.addEntry(K1, constructor); - constructor = Keyword2.class.getConstructor(JsonNode.class); - builder.addEntry(K2, constructor); - constructor = Challenged.class.getConstructor(JsonNode.class); - builder.addEntry(CHALLENGED, constructor); + factory = new ReflectionKeywordValidatorFactory(K1, Keyword1.class); + builder.addEntry(K1, factory); + factory = new ReflectionKeywordValidatorFactory(K2, Keyword2.class); + builder.addEntry(K2, factory); + factory = new ReflectionKeywordValidatorFactory(CHALLENGED, Challenged.class); + builder.addEntry(CHALLENGED, factory); validatorBuilder = new ValidatorBuilder(builder.freeze()); }