From d835b2917bb65ebada3c0397856a705f50338df4 Mon Sep 17 00:00:00 2001 From: Belal Jafri Date: Tue, 5 Nov 2019 16:13:14 +0000 Subject: [PATCH] Fix bug #22 --- build.gradle | 2 +- .../verifier/tostring/ToStringVerifier.java | 49 ++++++++++++++++-- .../tostring/ToStringVerifierTest.java | 51 +++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 104e628..8bcca4b 100755 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ repositories { } dependencies { - compile 'com.jparams:object-builder:2.1.1' + compile 'com.jparams:object-builder:2.2.1' testCompile 'junit:junit:4.12' testCompile 'org.assertj:assertj-core:3.11.1' testCompile 'eu.codearte.catch-exception:catch-exception:1.4.4' diff --git a/src/main/java/com/jparams/verifier/tostring/ToStringVerifier.java b/src/main/java/com/jparams/verifier/tostring/ToStringVerifier.java index 44b1a58..1613e2a 100644 --- a/src/main/java/com/jparams/verifier/tostring/ToStringVerifier.java +++ b/src/main/java/com/jparams/verifier/tostring/ToStringVerifier.java @@ -14,6 +14,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.function.Function; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -22,7 +23,10 @@ import com.jparams.object.builder.Build; import com.jparams.object.builder.BuildStrategy; import com.jparams.object.builder.Configuration; +import com.jparams.object.builder.Context; import com.jparams.object.builder.ObjectBuilder; +import com.jparams.object.builder.path.Path; +import com.jparams.object.builder.provider.Provider; import com.jparams.object.builder.type.Type; import com.jparams.verifier.tostring.error.ClassNameVerificationError; import com.jparams.verifier.tostring.error.ErrorMessageGenerator; @@ -286,14 +290,53 @@ public ToStringVerifier withFailOnExcludedFields(final boolean failOnExcludedFie /** * Adds prefabricated values for instance fields of classes that ToStringVerifier cannot instantiate by itself. * - * @param type The class of the prefabricated values + * @param clazz The class of the prefabricated values * @param prefabValue An instance of {@code S}. * @param The class of the prefabricated values. * @return verifier */ - public ToStringVerifier withPrefabValue(final Class type, final S prefabValue) + public ToStringVerifier withPrefabValue(final Class clazz, final S prefabValue) { - this.configuration.withPrefabValue(Type.forClass(type), prefabValue); + this.configuration.withPrefabValue(Type.forClass(clazz), prefabValue); + return this; + } + + /** + * Adds value provider values for instance fields of classes that ToStringVerifier cannot instantiate by itself. + * + * @param clazz The class of the value to being supplied + * @param valueSupplier value supplier + * @param The class of the prefabricated values. + * @return verifier + */ + public ToStringVerifier withValueProvider(final Class clazz, final Function valueSupplier) + { + this.configuration.withProvider(new Provider() + { + @Override + public boolean supports(final Type type) + { + return type.getJavaType().equals(clazz); + } + + @Override + public Object provide(final Context context) + { + return valueSupplier.apply(context.getPath()); + } + }); + return this; + } + + /** + * Adds value provider values for instance fields of classes that ToStringVerifier cannot instantiate by itself. + * + * @param provider Value provider + * @return verifier + */ + public ToStringVerifier withValueProvider(final Provider provider) + { + this.configuration.withProvider(provider); return this; } diff --git a/src/test/java/com/jparams/verifier/tostring/ToStringVerifierTest.java b/src/test/java/com/jparams/verifier/tostring/ToStringVerifierTest.java index cfd2e19..534cb67 100644 --- a/src/test/java/com/jparams/verifier/tostring/ToStringVerifierTest.java +++ b/src/test/java/com/jparams/verifier/tostring/ToStringVerifierTest.java @@ -6,6 +6,9 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import com.jparams.object.builder.Context; +import com.jparams.object.builder.provider.Provider; +import com.jparams.object.builder.type.Type; import com.jparams.verifier.tostring.error.ClassNameVerificationError; import com.jparams.verifier.tostring.error.ErrorMessageGenerator; import com.jparams.verifier.tostring.error.FieldValue; @@ -187,6 +190,54 @@ public void testWithPrefabValues() .verify(); } + @Test + public void testWithValueProviderFunction() + { + Person.setStringValue("Person{id=1, firstName='A', lastName='A'}"); + + subject.withValueProvider(Integer.class, path -> 1) + .withValueProvider(String.class, path -> "A") + .verify(); + } + + @Test + public void testWithValueProvider() + { + Person.setStringValue("Person{id=1, firstName='A', lastName='A'}"); + + subject.withValueProvider(new Provider() + { + @Override + public boolean supports(final Type type) + { + return Integer.class.equals(type.getJavaType()); + } + + @Override + public Object provide(final Context context) + { + return 1; + } + }); + + subject.withValueProvider(new Provider() + { + @Override + public boolean supports(final Type type) + { + return String.class.equals(type.getJavaType()); + } + + @Override + public Object provide(final Context context) + { + return "A"; + } + }); + + subject.verify(); + } + @Test(expected = IllegalArgumentException.class) public void testWithOnlyTheseFieldsNullList() {