Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.1.1 #1

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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.0.1'
version = '1.1.1'

sourceCompatibility = 1.8

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/jparams/verifier/tostring/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jparams.verifier.tostring;

/**
* Formatter for a data type
*
* @param <T> type
*/
public interface Formatter<T>
{
/**
* Format the item to string
*
* @param item item
* @return string value
*/
String format(T item);
}
33 changes: 13 additions & 20 deletions src/main/java/com/jparams/verifier/tostring/SubjectBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,14 @@

/**
* Builds the subject of the test
*
* @param <T> subject type
*/
class SubjectBuilder<T>
class SubjectBuilder
{
private final Class<T> clazz;
private final Configuration configuration;

SubjectBuilder(final Class<T> clazz)
{
this.clazz = clazz;
this.configuration = new Configuration().withDefaultProviders()
.withMaxDepth(2)
.withFailOnWarning(false)
.withFailOnError(false)
.withCaching(false);
}
private final Configuration configuration = new Configuration().withDefaultProviders()
.withMaxDepth(2)
.withFailOnWarning(false)
.withFailOnError(false)
.withCaching(false);

/**
* Set a prefabricated value
Expand Down Expand Up @@ -55,20 +46,22 @@ public Object provide(final Context context)
/**
* Build test subject
*
* @param clazz type
* @param <T> type
* @return test subject
*/
T build()
<T> T build(final Class<T> clazz)
{
// attempt to build with field injection to bypass any validation that may exist in a constructor
final Build<T> builtWithFieldInjection = buildWithFieldInjection();
final Build<T> builtWithFieldInjection = buildWithFieldInjection(clazz);

if (builtWithFieldInjection.get() != null)
{
return builtWithFieldInjection.get();
}

// if unable to build, attempt to build with constructor injection
final Build<T> builtWithConstructorInjection = buildWithConstructorInjection();
final Build<T> builtWithConstructorInjection = buildWithConstructorInjection(clazz);

if (builtWithConstructorInjection.get() != null)
{
Expand All @@ -79,13 +72,13 @@ T build()
throw new AssertionError("Failed to create instance of " + clazz + ". Failed with error: " + builtWithConstructorInjection.getErrors());
}

private Build<T> buildWithFieldInjection()
private <T> Build<T> buildWithFieldInjection(final Class<T> clazz)
{
return ObjectBuilder.withConfiguration(configuration.withDefaultProviders(InjectionStrategy.FIELD_INJECTION))
.buildInstanceOf(clazz);
}

private Build<T> buildWithConstructorInjection()
private <T> Build<T> buildWithConstructorInjection(final Class<T> clazz)
{
return ObjectBuilder.withConfiguration(configuration.withDefaultProviders(InjectionStrategy.CONSTRUCTOR_INJECTION))
.buildInstanceOf(clazz);
Expand Down
Loading