Skip to content

Commit

Permalink
Update to latest object builder (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jparams committed Sep 13, 2018
1 parent 43303b5 commit 2b17d5f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 105 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ apply plugin: 'maven'
apply from: 'publish.gradle'

group = 'com.jparams'
version = '1.4.2'
version = '1.4.3'

sourceCompatibility = 1.8

Expand All @@ -24,7 +24,7 @@ repositories {
}

dependencies {
compile 'com.jparams:object-builder:1.0.9'
compile 'com.jparams:object-builder:2.0.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
86 changes: 0 additions & 86 deletions src/main/java/com/jparams/verifier/tostring/SubjectBuilder.java

This file was deleted.

53 changes: 36 additions & 17 deletions src/main/java/com/jparams/verifier/tostring/ToStringVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -18,6 +19,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.jparams.object.builder.Build;
import com.jparams.object.builder.BuildStrategy;
import com.jparams.object.builder.Configuration;
import com.jparams.object.builder.ObjectBuilder;
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 All @@ -41,7 +47,7 @@ public final class ToStringVerifier
private static final int TEST_REPEAT_COUNT = 3;
private static final Formatter<Object> DEFAULT_FORMATTER = String::valueOf;

private final SubjectBuilder builder;
private final Configuration configuration;
private final List<Class<?>> classes;
private final Map<Class<?>, Formatter<Object>> formatterMap = new HashMap<>();
private NameStyle nameStyle = null;
Expand All @@ -53,8 +59,9 @@ public final class ToStringVerifier

private ToStringVerifier(final Collection<Class<?>> classes)
{
this.builder = new SubjectBuilder();
this.configuration = Configuration.defaultConfiguration().withDefaultBuildStrategy(BuildStrategy.AUTO).withFailOnError(false).withFailOnWarning(false);
this.classes = classes.stream().filter(ToStringVerifier::isTestableClass).collect(Collectors.toList());
this.classes.forEach(clazz -> configuration.withBuildStrategy(Type.forClass(clazz).build(), BuildStrategy.FIELD_INJECTION));

if (this.classes.isEmpty())
{
Expand Down Expand Up @@ -247,31 +254,31 @@ public ToStringVerifier withMatchingFields(final String regex)
}

/**
* Define how the verifier should behave if the {@link Object#toString()} output contains a field that has been explicitly excluded by
* calling {@link #withIgnoredFields(String...)} or implicitly excluded by calling {@link #withOnlyTheseFields(String...)} or
* {@link #withMatchingFields(String)}. By default, the verifier will not test for the presence of the excluded fields and will not fail
* if they exist in the {@link Object#toString()} output when they should not. You can change this behaviour by setting <code>failOnExcludedFields</code>
* to true.
* If specified, this tester will only assert that field matching this filter criteria are present in the {@link Object#toString()} output.
*
* @param failOnExcludedFields fail if ignored fields are found in the {@link Object#toString()} output
* @param fields filter criteria
* @return verifier
*/
public ToStringVerifier withFailOnExcludedFields(final boolean failOnExcludedFields)
public ToStringVerifier withMatchingFields(final FieldFilter fields)
{
this.failOnExcludedFields = failOnExcludedFields;
assertNotNull(fields);
this.fieldFilter = fields;
return this;
}

/**
* If specified, this tester will only assert that field matching this filter criteria are present in the {@link Object#toString()} output.
* Define how the verifier should behave if the {@link Object#toString()} output contains a field that has been explicitly excluded by
* calling {@link #withIgnoredFields(String...)} or implicitly excluded by calling {@link #withOnlyTheseFields(String...)} or
* {@link #withMatchingFields(String)}. By default, the verifier will not test for the presence of the excluded fields and will not fail
* if they exist in the {@link Object#toString()} output when they should not. You can change this behaviour by setting <code>failOnExcludedFields</code>
* to true.
*
* @param fields filter criteria
* @param failOnExcludedFields fail if ignored fields are found in the {@link Object#toString()} output
* @return verifier
*/
public ToStringVerifier withMatchingFields(final FieldFilter fields)
public ToStringVerifier withFailOnExcludedFields(final boolean failOnExcludedFields)
{
assertNotNull(fields);
this.fieldFilter = fields;
this.failOnExcludedFields = failOnExcludedFields;
return this;
}

Expand All @@ -285,7 +292,7 @@ public ToStringVerifier withMatchingFields(final FieldFilter fields)
*/
public <S> ToStringVerifier withPrefabValue(final Class<S> type, final S prefabValue)
{
this.builder.setPrefabValue(type, prefabValue);
this.configuration.withPrefabValue(Type.forClass(type).build(), prefabValue);
return this;
}

Expand Down Expand Up @@ -345,7 +352,14 @@ public void verify()

private Optional<String> verify(final Class<?> clazz)
{
final Object subject = builder.build(clazz);
final Build<?> build = ObjectBuilder.withConfiguration(configuration).buildInstanceOf(clazz);
final Object subject = build.get();

if (subject == null)
{
throw new AssertionError("Failed to create instance of " + clazz + ". Failed with error:\n" + build.getErrors());
}

final String stringValue = subject.toString();

if (stringValue == null)
Expand Down Expand Up @@ -444,6 +458,11 @@ private List<String> getFieldValues(final Object subject, final Field field)
return Collections.singletonList(nullValue);
}

if (Proxy.isProxyClass(value.getClass()))
{
return Collections.singletonList(value.toString());
}

if (value.getClass().isArray())
{
return Stream.of((Object[]) value).map(this::formatValue).collect(Collectors.toList());
Expand Down

0 comments on commit 2b17d5f

Please sign in to comment.