Skip to content

Commit

Permalink
Fix bug #22
Browse files Browse the repository at this point in the history
  • Loading branch information
Belal Jafri authored and Belal Jafri committed Nov 5, 2019
1 parent e557c6d commit d835b29
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
49 changes: 46 additions & 3 deletions src/main/java/com/jparams/verifier/tostring/ToStringVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 <S> The class of the prefabricated values.
* @return verifier
*/
public <S> ToStringVerifier withPrefabValue(final Class<S> type, final S prefabValue)
public <S> ToStringVerifier withPrefabValue(final Class<S> 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 <S> The class of the prefabricated values.
* @return verifier
*/
public <S> ToStringVerifier withValueProvider(final Class<S> clazz, final Function<Path, S> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit d835b29

Please sign in to comment.