-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piotr Joński
committed
Sep 8, 2016
1 parent
b5ca2c7
commit 4e0bd87
Showing
19 changed files
with
707 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.pojo.tester; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.pojo.tester.field.AbstractFieldValueChanger; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public abstract class AbstractAssetion { | ||
|
||
private static final Set<AbstractTester> DEFAULT_TESTERS; | ||
|
||
static { | ||
DEFAULT_TESTERS = new HashSet<>(); | ||
Arrays.stream(Method.values()) | ||
.map(Method::getTester) | ||
.forEach(DEFAULT_TESTERS::add); | ||
} | ||
|
||
Set<AbstractTester> testers = new HashSet<>(); | ||
private AbstractFieldValueChanger abstractFieldValueChanger; | ||
|
||
public AbstractAssetion using(final AbstractFieldValueChanger abstractFieldValueChanger) { | ||
this.abstractFieldValueChanger = abstractFieldValueChanger; | ||
return this; | ||
} | ||
|
||
public AbstractAssetion testing(final Method... methods) { | ||
Arrays.asList(methods) | ||
.forEach(this::testing); | ||
return this; | ||
} | ||
|
||
public AbstractAssetion testing(final Method method) { | ||
final AbstractTester tester = method.getTester(); | ||
this.testers.add(tester); | ||
return this; | ||
} | ||
|
||
public void areWellImplemented() { | ||
if (testers.isEmpty()) { | ||
testers = DEFAULT_TESTERS; | ||
} | ||
if (abstractFieldValueChanger != null) { | ||
testers.forEach(tester -> tester.setFieldValuesChanger(abstractFieldValueChanger)); | ||
} | ||
|
||
testImplementation(); | ||
} | ||
|
||
protected abstract void testImplementation(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.pojo.tester; | ||
|
||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
|
||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public abstract class Assetions { | ||
|
||
public static AbstractAssetion assertPojoMethodsFor(final Class<?> clazz) { | ||
final Predicate<String> predicateAcceptingAllFields = FieldPredicate.includeAllFields(clazz); | ||
return assertPojoMethodsFor(clazz, predicateAcceptingAllFields); | ||
} | ||
|
||
public static AbstractAssetion assertPojoMethodsFor(final Class<?> clazz, final Predicate<String> fieldPredicate) { | ||
final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair(clazz, fieldPredicate); | ||
return assertPojoMethodsFor(classAndFieldPredicatePair); | ||
} | ||
|
||
public static AbstractAssetion assertPojoMethodsFor(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, | ||
final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { | ||
return new SingleClassAssetion(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs); | ||
} | ||
|
||
public static AbstractAssetion assertPojoMethodsForAll(final Class... classes) { | ||
final ClassAndFieldPredicatePair[] classesAndFieldPredicatesPairs = Arrays.stream(classes) | ||
.map(ClassAndFieldPredicatePair::new) | ||
.toArray(ClassAndFieldPredicatePair[]::new); | ||
return assertPojoMethodsForAll(classesAndFieldPredicatesPairs); | ||
} | ||
|
||
public static AbstractAssetion assertPojoMethodsForAll(final ClassAndFieldPredicatePair... classesAndFieldPredicatesPairs) { | ||
final List<ClassAndFieldPredicatePair> classAndFieldPredicatePairs = Arrays.asList(classesAndFieldPredicatesPairs); | ||
return new MultiClassAssetion(classAndFieldPredicatePairs); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.pojo.tester; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum Method { | ||
EQUALS(new EqualsTester()), | ||
HASH_CODE(new HashCodeTester()), | ||
SETTERS_AND_GETTERS(new SetterGetterTester()), | ||
TO_STRING(new ToStringTester()); | ||
|
||
private final AbstractTester tester; | ||
|
||
Method(final AbstractTester tester) { | ||
this.tester = tester; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.pojo.tester; | ||
|
||
import java.util.List; | ||
|
||
class MultiClassAssetion extends AbstractAssetion { | ||
|
||
private final List<ClassAndFieldPredicatePair> classAndFieldPredicatePairs; | ||
|
||
MultiClassAssetion(final List<ClassAndFieldPredicatePair> classAndFieldPredicatePairs) { | ||
this.classAndFieldPredicatePairs = classAndFieldPredicatePairs; | ||
} | ||
|
||
@Override | ||
public void testImplementation() { | ||
final ClassAndFieldPredicatePair[] classes = classAndFieldPredicatePairs.toArray(new ClassAndFieldPredicatePair[classAndFieldPredicatePairs.size()]); | ||
testers.forEach(tester -> tester.testAll(classes)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.pojo.tester; | ||
|
||
class SingleClassAssetion extends AbstractAssetion { | ||
|
||
private final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair; | ||
private final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs; | ||
|
||
SingleClassAssetion(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) { | ||
this.baseClassAndFieldPredicatePair = baseClassAndFieldPredicatePair; | ||
this.classAndFieldPredicatePairs = classAndFieldPredicatePairs; | ||
} | ||
|
||
@Override | ||
public void testImplementation() { | ||
testers.forEach(tester -> tester.test(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/test/java/matchers/ClassAndFieldPredicatePairArgumentMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package matchers; | ||
|
||
import org.mockito.ArgumentMatcher; | ||
import org.pojo.tester.ClassAndFieldPredicatePair; | ||
|
||
public class ClassAndFieldPredicatePairArgumentMatcher extends ArgumentMatcher<ClassAndFieldPredicatePair> { | ||
private final Class<?> clazz; | ||
private final String fieldName; | ||
|
||
public ClassAndFieldPredicatePairArgumentMatcher(final Class<?> clazz, final String fieldName) { | ||
this.clazz = clazz; | ||
this.fieldName = fieldName; | ||
} | ||
|
||
@Override | ||
public boolean matches(final Object argument) { | ||
final ClassAndFieldPredicatePair classAndFieldPredicatePair = (ClassAndFieldPredicatePair) argument; | ||
|
||
final boolean classesMatches = classAndFieldPredicatePair.getClazz() | ||
.equals(clazz); | ||
|
||
final boolean predicateMatches = classAndFieldPredicatePair.getFieldsPredicate() | ||
.test(fieldName); | ||
return classesMatches && predicateMatches; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/matchers/RecursivelyEqualArgumentMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package matchers; | ||
|
||
import org.mockito.ArgumentMatcher; | ||
import org.pojo.tester.ClassAndFieldPredicatePair; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class RecursivelyEqualArgumentMatcher extends ArgumentMatcher<ClassAndFieldPredicatePair> { | ||
private final ClassAndFieldPredicatePair expectedParameter; | ||
|
||
public RecursivelyEqualArgumentMatcher(final ClassAndFieldPredicatePair expectedParameter) { | ||
this.expectedParameter = expectedParameter; | ||
} | ||
|
||
@Override | ||
public boolean matches(final Object argument) { | ||
final ClassAndFieldPredicatePair classAndFieldPredicatePair = (ClassAndFieldPredicatePair) argument; | ||
assertThat(classAndFieldPredicatePair).isEqualToComparingFieldByFieldRecursively(expectedParameter); | ||
return true; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/matchers/StringPredicateArgumentMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package matchers; | ||
|
||
import java.util.function.Predicate; | ||
import org.mockito.ArgumentMatcher; | ||
|
||
public class StringPredicateArgumentMatcher extends ArgumentMatcher<Predicate<String>> { | ||
|
||
@Override | ||
public boolean matches(final Object argument) { | ||
final Predicate<String> stringPredicate = (Predicate<String>) argument; | ||
return stringPredicate.test("a"); | ||
} | ||
} |
Oops, something went wrong.