Skip to content

Commit

Permalink
Merge pull request #140 from sta-szek/#135-Create-class-that-will-enc…
Browse files Browse the repository at this point in the history
…apsulate-constructorParamters-in-AbstractTester

#135 create class that will encapsulate constructor paramters in abstract tester
  • Loading branch information
Piotr Joński authored Oct 24, 2016
2 parents 111279d + 1bbbf4d commit fb25da2
Show file tree
Hide file tree
Showing 28 changed files with 444 additions and 274 deletions.
10 changes: 8 additions & 2 deletions src/main/java/pl/pojo/tester/api/ConstructorParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.Arrays;

/**
* Defines constructor parameters and constructor parameter's types.
* <p>
Expand All @@ -23,8 +25,8 @@ public class ConstructorParameters {
/**
* Instantaites {@code ConstructorParameters} with given constructor parameters and constructor parameter's types.
*
* @param constructorParameters constructor paramters
* @param constructorParametersTypes constructor paramter's types
* @param constructorParameters constructor parameters
* @param constructorParametersTypes constructor parameter's types
*/
public ConstructorParameters(final Object[] constructorParameters, final Class<?>[] constructorParametersTypes) {
this.constructorParameters = constructorParameters;
Expand Down Expand Up @@ -60,4 +62,8 @@ public int hashCode() {
.append(constructorParametersTypes)
.toHashCode();
}

public boolean matches(final Class<?>[] parameterTypes) {
return Arrays.equals(constructorParametersTypes, parameterTypes);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package pl.pojo.tester.api.assertion;

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import pl.pojo.tester.api.ConstructorParameters;
import pl.pojo.tester.internal.field.AbstractFieldValueChanger;
import pl.pojo.tester.internal.instantiator.ClassLoader;
import pl.pojo.tester.internal.tester.AbstractTester;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static pl.pojo.tester.internal.preconditions.ParameterPreconditions.checkNotBlank;
Expand All @@ -23,7 +23,7 @@
* @author Piotr Joński
* @since 0.1.0
*/
public abstract class AbstractAssetion {
public abstract class AbstractAssertion {

private static final Set<AbstractTester> DEFAULT_TESTERS;

Expand All @@ -34,10 +34,11 @@ public abstract class AbstractAssetion {
.forEach(DEFAULT_TESTERS::add);
}

private final Map<Class<?>, ConstructorParameters> constructorParameters = new HashMap<>();
private final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>();
Set<AbstractTester> testers = new HashSet<>();
private AbstractFieldValueChanger abstractFieldValueChanger;


/**
* Specifies what field values changer will be used for testing.
*
Expand All @@ -47,7 +48,7 @@ public abstract class AbstractAssetion {
*
* @see AbstractFieldValueChanger
*/
public AbstractAssetion using(final AbstractFieldValueChanger abstractFieldValueChanger) {
public AbstractAssertion using(final AbstractFieldValueChanger abstractFieldValueChanger) {
checkNotNull("abstractFieldValueChanger", abstractFieldValueChanger);

this.abstractFieldValueChanger = abstractFieldValueChanger;
Expand All @@ -63,7 +64,7 @@ public AbstractAssetion using(final AbstractFieldValueChanger abstractFieldValue
*
* @see Method
*/
public AbstractAssetion testing(final Method... methods) {
public AbstractAssertion testing(final Method... methods) {
checkNotNull("methods", methods);

Arrays.asList(methods)
Expand All @@ -80,7 +81,7 @@ public AbstractAssetion testing(final Method... methods) {
*
* @see Method
*/
public AbstractAssetion testing(final Method method) {
public AbstractAssertion testing(final Method method) {
checkNotNull("method", method);

final AbstractTester tester = method.getTester();
Expand Down Expand Up @@ -119,9 +120,7 @@ public void areWellImplemented() {
*
* @see ConstructorParameters
*/
public AbstractAssetion create(final String qualifiedClassName,
final Object[] constructorParameters,
final Class<?>[] constructorParameterTypes) {
public AbstractAssertion create(final String qualifiedClassName, final Object[] constructorParameters, final Class<?>[] constructorParameterTypes) {
checkNotBlank("qualifiedClassName", qualifiedClassName);

final ConstructorParameters constructorParameter = new ConstructorParameters(constructorParameters,
Expand All @@ -140,7 +139,7 @@ public AbstractAssetion create(final String qualifiedClassName,
*
* @see ConstructorParameters
*/
public AbstractAssetion create(final String qualifiedClassName, final ConstructorParameters constructorParameters) {
public AbstractAssertion create(final String qualifiedClassName, final ConstructorParameters constructorParameters) {
checkNotBlank("qualifiedClassName", qualifiedClassName);
checkNotNull("constructorParameters", constructorParameters);

Expand All @@ -161,9 +160,7 @@ public AbstractAssetion create(final String qualifiedClassName, final Constructo
*
* @see ConstructorParameters
*/
public AbstractAssetion create(final Class<?> clazz,
final Object[] constructorParameters,
final Class<?>[] constructorParameterTypes) {
public AbstractAssertion create(final Class<?> clazz, final Object[] constructorParameters, final Class<?>[] constructorParameterTypes) {
checkNotNull("clazz", clazz);

final ConstructorParameters constructorParameter = new ConstructorParameters(constructorParameters,
Expand All @@ -183,7 +180,7 @@ public AbstractAssetion create(final Class<?> clazz,
*
* @see ConstructorParameters
*/
public AbstractAssetion create(final Class<?> clazz, final ConstructorParameters constructorParameters) {
public AbstractAssertion create(final Class<?> clazz, final ConstructorParameters constructorParameters) {
checkNotNull("clazz", clazz);
checkNotNull("constructorParameters", constructorParameters);

Expand Down
75 changes: 36 additions & 39 deletions src/main/java/pl/pojo/tester/api/assertion/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ private Assertions() {}
*
* @return assertion for given class
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsFor(final String qualifiedClassName) {
public static AbstractAssertion assertPojoMethodsFor(final String qualifiedClassName) {
checkNotBlank("qualifiedClassName", qualifiedClassName);

final Class<?> clazz = ClassLoader.loadClass(qualifiedClassName);
Expand All @@ -52,11 +52,11 @@ public static AbstractAssetion assertPojoMethodsFor(final String qualifiedClassN
*
* @return assertion for given class
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsFor(final Class<?> clazz) {
public static AbstractAssertion assertPojoMethodsFor(final Class<?> clazz) {
checkNotNull("clazz", clazz);

final Predicate<String> predicateAcceptingAllFields = FieldPredicate.includeAllFields(clazz);
Expand All @@ -71,12 +71,11 @@ public static AbstractAssetion assertPojoMethodsFor(final Class<?> clazz) {
*
* @return assertion for given class
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsFor(final String qualifiedClassName,
final Predicate<String> fieldPredicate) {
public static AbstractAssertion assertPojoMethodsFor(final String qualifiedClassName, final Predicate<String> fieldPredicate) {
checkNotBlank("qualifiedClassName", qualifiedClassName);
checkNotNull("fieldPredicate", fieldPredicate);

Expand All @@ -92,11 +91,11 @@ public static AbstractAssetion assertPojoMethodsFor(final String qualifiedClassN
*
* @return assertion for given class
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsFor(final Class<?> clazz, final Predicate<String> fieldPredicate) {
public static AbstractAssertion assertPojoMethodsFor(final Class<?> clazz, final Predicate<String> fieldPredicate) {
checkNotNull("clazz", clazz);
checkNotNull("fieldPredicate", fieldPredicate);

Expand All @@ -113,15 +112,14 @@ public static AbstractAssetion assertPojoMethodsFor(final Class<?> clazz, final
*
* @return assertion for given base class
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsFor(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair,
final ClassAndFieldPredicatePair...
classAndFieldPredicatePairs) {
public static AbstractAssertion assertPojoMethodsFor(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair,
final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) {
checkNotNull("baseClassAndFieldPredicatePair", baseClassAndFieldPredicatePair);
return new SingleClassAssetion(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs);
return new SingleClassAssertion(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs);
}

/**
Expand All @@ -131,11 +129,11 @@ public static AbstractAssetion assertPojoMethodsFor(final ClassAndFieldPredicate
*
* @return assertion for all classes
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsForAll(final String... qualifiedClassNames) {
public static AbstractAssertion assertPojoMethodsForAll(final String... qualifiedClassNames) {
checkNotBlank("qualifiedClassNames", qualifiedClassNames);

final Class<?>[] classesAndFieldPredicatesPairs = Arrays.stream(qualifiedClassNames)
Expand All @@ -153,7 +151,7 @@ public static AbstractAssetion assertPojoMethodsForAll(final String... qualified
*
* @see PackageFilter
*/
public static AbstractAssetion assertPojoMethodsForAll(final PackageFilter packageFilter) {
public static AbstractAssertion assertPojoMethodsForAll(final PackageFilter packageFilter) {
checkNotNull("packageFilter", packageFilter);
return assertPojoMethodsForAll(packageFilter.getClasses());
}
Expand All @@ -165,11 +163,11 @@ public static AbstractAssetion assertPojoMethodsForAll(final PackageFilter packa
*
* @return assertion for all classes
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsForAll(final Class... classes) {
public static AbstractAssertion assertPojoMethodsForAll(final Class... classes) {
checkNotNull("classes", classes);

final ClassAndFieldPredicatePair[] classesAndFieldPredicatesPairs = Arrays.stream(classes)
Expand All @@ -185,17 +183,16 @@ public static AbstractAssetion assertPojoMethodsForAll(final Class... classes) {
*
* @return assertion for all classes
*
* @see AbstractAssetion
* @see MultiClassAssetion
* @see SingleClassAssetion
* @see AbstractAssertion
* @see MultiClassAssertion
* @see SingleClassAssertion
*/
public static AbstractAssetion assertPojoMethodsForAll(final ClassAndFieldPredicatePair...
classesAndFieldPredicatesPairs) {
public static AbstractAssertion assertPojoMethodsForAll(final ClassAndFieldPredicatePair... classesAndFieldPredicatesPairs) {
checkNotNull("classesAndFieldPredicatesPairs", classesAndFieldPredicatesPairs);

final List<ClassAndFieldPredicatePair> classAndFieldPredicatePairs = Arrays.asList(
classesAndFieldPredicatesPairs);
return new MultiClassAssetion(classAndFieldPredicatePairs);
return new MultiClassAssertion(classAndFieldPredicatePairs);
}

}
6 changes: 4 additions & 2 deletions src/main/java/pl/pojo/tester/api/assertion/Method.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pl.pojo.tester.api.assertion;

import lombok.Getter;
import pl.pojo.tester.internal.tester.AbstractTester;
import pl.pojo.tester.internal.tester.ConstructorTester;
import pl.pojo.tester.internal.tester.EqualsTester;
Expand All @@ -17,7 +16,6 @@
* @author Piotr Joński
* @since 0.1.0
*/
@Getter
public enum Method {
EQUALS(new EqualsTester()),
HASH_CODE(new HashCodeTester()),
Expand All @@ -31,4 +29,8 @@ public enum Method {
Method(final AbstractTester tester) {
this.tester = tester;
}

public AbstractTester getTester() {
return tester;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package pl.pojo.tester.api.assertion;

import java.util.List;
import pl.pojo.tester.api.ClassAndFieldPredicatePair;

class MultiClassAssetion extends AbstractAssetion {
import java.util.List;

class MultiClassAssertion extends AbstractAssertion {

private final List<ClassAndFieldPredicatePair> classAndFieldPredicatePairs;

MultiClassAssetion(final List<ClassAndFieldPredicatePair> classAndFieldPredicatePairs) {
MultiClassAssertion(final List<ClassAndFieldPredicatePair> classAndFieldPredicatePairs) {
super();
this.classAndFieldPredicatePairs = classAndFieldPredicatePairs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import pl.pojo.tester.api.ClassAndFieldPredicatePair;

class SingleClassAssetion extends AbstractAssetion {
import java.util.Arrays;

class SingleClassAssertion extends AbstractAssertion {

private final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair;
private final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs;

SingleClassAssetion(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) {
SingleClassAssertion(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) {
super();
this.baseClassAndFieldPredicatePair = baseClassAndFieldPredicatePair;
this.classAndFieldPredicatePairs = classAndFieldPredicatePairs;
this.classAndFieldPredicatePairs = Arrays.copyOf(classAndFieldPredicatePairs, classAndFieldPredicatePairs.length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package pl.pojo.tester.internal.instantiator;


import org.apache.commons.collections4.MultiValuedMap;
import pl.pojo.tester.api.ConstructorParameters;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Map;
import pl.pojo.tester.api.ConstructorParameters;
import java.util.Objects;

class BestConstructorInstantiator extends ObjectInstantiator {

private final Map<Class<?>, ConstructorParameters> constructorParameters;
private final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters;

BestConstructorInstantiator(final Class<?> clazz, final Map<Class<?>, ConstructorParameters> constructorParameters) {
BestConstructorInstantiator(final Class<?> clazz, final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters) {
super(clazz);
this.constructorParameters = constructorParameters;
}
Expand All @@ -25,9 +27,13 @@ private Object createFindingBestConstructor(final Class<?> clazz) {
final Constructor<?>[] constructors = clazz.getDeclaredConstructors();
return Arrays.stream(constructors)
.map(this::createObjectFromConstructor)
.filter(object -> object != null)
.filter(Objects::nonNull)
.findAny()
.orElseThrow(() -> new ObjectInstantiationException(clazz, "Class could not be created by any constructor."));
.orElseThrow(this::createObjectInstantiationException);
}

private ObjectInstantiationException createObjectInstantiationException() {
return new ObjectInstantiationException(clazz, "Class could not be created by any constructor.");
}

private Object createObjectFromConstructor(final Constructor<?> constructor) {
Expand Down
Loading

0 comments on commit fb25da2

Please sign in to comment.