From b1ec7d8bf0f139dd93494562edc21b69f897631c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jo=C5=84ski?= Date: Fri, 21 Oct 2016 18:09:53 +0200 Subject: [PATCH 1/5] #135. Typo in class name --- ...ctAssetion.java => AbstractAssertion.java} | 25 ++++--- .../pojo/tester/api/assertion/Assertions.java | 75 ++++++++++--------- ...Assetion.java => MultiClassAssertion.java} | 7 +- ...ssetion.java => SingleClassAssertion.java} | 5 +- ...onTest.java => AbstractAssertionTest.java} | 70 ++++++++--------- .../tester/api/assertion/AssertionsTest.java | 28 +++---- .../api/assertion/MultiClassAssetionTest.java | 2 +- .../assertion/SingleClassAssetionTest.java | 4 +- 8 files changed, 111 insertions(+), 105 deletions(-) rename src/main/java/pl/pojo/tester/api/assertion/{AbstractAssetion.java => AbstractAssertion.java} (85%) rename src/main/java/pl/pojo/tester/api/assertion/{MultiClassAssetion.java => MultiClassAssertion.java} (78%) rename src/main/java/pl/pojo/tester/api/assertion/{SingleClassAssetion.java => SingleClassAssertion.java} (74%) rename src/test/java/pl/pojo/tester/api/assertion/{AbstractAssetionTest.java => AbstractAssertionTest.java} (81%) diff --git a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssetion.java b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java similarity index 85% rename from src/main/java/pl/pojo/tester/api/assertion/AbstractAssetion.java rename to src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java index 4da68b9c..9581858c 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssetion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java @@ -23,7 +23,7 @@ * @author Piotr Joński * @since 0.1.0 */ -public abstract class AbstractAssetion { +public abstract class AbstractAssertion { private static final Set DEFAULT_TESTERS; @@ -47,7 +47,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; @@ -63,7 +63,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) @@ -80,7 +80,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(); @@ -119,9 +119,9 @@ 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, @@ -140,7 +140,8 @@ 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); @@ -161,9 +162,9 @@ 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, @@ -183,7 +184,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); diff --git a/src/main/java/pl/pojo/tester/api/assertion/Assertions.java b/src/main/java/pl/pojo/tester/api/assertion/Assertions.java index 8c01763f..287034d3 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/Assertions.java +++ b/src/main/java/pl/pojo/tester/api/assertion/Assertions.java @@ -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); @@ -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 predicateAcceptingAllFields = FieldPredicate.includeAllFields(clazz); @@ -71,12 +71,12 @@ 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 fieldPredicate) { + public static AbstractAssertion assertPojoMethodsFor(final String qualifiedClassName, + final Predicate fieldPredicate) { checkNotBlank("qualifiedClassName", qualifiedClassName); checkNotNull("fieldPredicate", fieldPredicate); @@ -92,11 +92,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 fieldPredicate) { + public static AbstractAssertion assertPojoMethodsFor(final Class clazz, final Predicate fieldPredicate) { checkNotNull("clazz", clazz); checkNotNull("fieldPredicate", fieldPredicate); @@ -113,15 +113,16 @@ 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... + public static AbstractAssertion assertPojoMethodsFor(final ClassAndFieldPredicatePair + baseClassAndFieldPredicatePair, + final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { checkNotNull("baseClassAndFieldPredicatePair", baseClassAndFieldPredicatePair); - return new SingleClassAssetion(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs); + return new SingleClassAssertion(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs); } /** @@ -131,11 +132,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) @@ -153,7 +154,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()); } @@ -165,11 +166,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) @@ -185,17 +186,17 @@ 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... + public static AbstractAssertion assertPojoMethodsForAll(final ClassAndFieldPredicatePair... classesAndFieldPredicatesPairs) { checkNotNull("classesAndFieldPredicatesPairs", classesAndFieldPredicatesPairs); final List classAndFieldPredicatePairs = Arrays.asList( classesAndFieldPredicatesPairs); - return new MultiClassAssetion(classAndFieldPredicatePairs); + return new MultiClassAssertion(classAndFieldPredicatePairs); } } diff --git a/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssetion.java b/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssertion.java similarity index 78% rename from src/main/java/pl/pojo/tester/api/assertion/MultiClassAssetion.java rename to src/main/java/pl/pojo/tester/api/assertion/MultiClassAssertion.java index 76d1f44a..80963315 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssetion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssertion.java @@ -1,13 +1,14 @@ 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 classAndFieldPredicatePairs; - MultiClassAssetion(final List classAndFieldPredicatePairs) { + MultiClassAssertion(final List classAndFieldPredicatePairs) { this.classAndFieldPredicatePairs = classAndFieldPredicatePairs; } diff --git a/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssetion.java b/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java similarity index 74% rename from src/main/java/pl/pojo/tester/api/assertion/SingleClassAssetion.java rename to src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java index 0c31e4f3..b6b22ad7 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssetion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java @@ -2,12 +2,13 @@ import pl.pojo.tester.api.ClassAndFieldPredicatePair; -class SingleClassAssetion extends AbstractAssetion { +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) { this.baseClassAndFieldPredicatePair = baseClassAndFieldPredicatePair; this.classAndFieldPredicatePairs = classAndFieldPredicatePairs; } diff --git a/src/test/java/pl/pojo/tester/api/assertion/AbstractAssetionTest.java b/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java similarity index 81% rename from src/test/java/pl/pojo/tester/api/assertion/AbstractAssetionTest.java rename to src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java index 97892738..73020dd4 100644 --- a/src/test/java/pl/pojo/tester/api/assertion/AbstractAssetionTest.java +++ b/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java @@ -25,17 +25,17 @@ import static org.powermock.reflect.Whitebox.setInternalState; @RunWith(JUnitPlatform.class) -public class AbstractAssetionTest { +public class AbstractAssertionTest { @Test public void Should_Set_Field_Value_Changer() { // given - final AbstractAssetion abstractAssetion = new AbstractAssetionImplementation(); + final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final AbstractFieldValueChanger expectedFieldsValuesChanger = DefaultFieldValueChanger.INSTANCE; // when - abstractAssetion.using(expectedFieldsValuesChanger); - final AbstractFieldValueChanger result = getInternalState(abstractAssetion, "abstractFieldValueChanger"); + abstractAssertion.using(expectedFieldsValuesChanger); + final AbstractFieldValueChanger result = getInternalState(abstractAssertion, "abstractFieldValueChanger"); // then assertThat(result).isEqualTo(expectedFieldsValuesChanger); @@ -44,30 +44,30 @@ public void Should_Set_Field_Value_Changer() { @Test public void Should_Add_Equals_Tester() { // given - final AbstractAssetion abstractAssetion = new AbstractAssetionImplementation(); + final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final EqualsTester expectedTester = new EqualsTester(); // when - abstractAssetion.testing(Method.EQUALS); + abstractAssertion.testing(Method.EQUALS); // then - assertThat(abstractAssetion.testers).usingRecursiveFieldByFieldElementComparator() - .containsExactly(expectedTester); + assertThat(abstractAssertion.testers).usingRecursiveFieldByFieldElementComparator() + .containsExactly(expectedTester); } @Test public void Should_Add_Equals_And_Hash_Code_Testers() { // given - final AbstractAssetion abstractAssetion = new AbstractAssetionImplementation(); + final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final EqualsTester expectedTester1 = new EqualsTester(); final HashCodeTester expectedTester2 = new HashCodeTester(); // when - abstractAssetion.testing(Method.EQUALS, Method.HASH_CODE); + abstractAssertion.testing(Method.EQUALS, Method.HASH_CODE); // then - assertThat(abstractAssetion.testers).usingRecursiveFieldByFieldElementComparator() - .containsExactly(expectedTester1, expectedTester2); + assertThat(abstractAssertion.testers).usingRecursiveFieldByFieldElementComparator() + .containsExactly(expectedTester1, expectedTester2); } @Test @@ -100,14 +100,14 @@ public void Should_Throw_Exception_When_Class_Has_Method_Implemented_In_Wrong_Wa @Test public void Should_Set_Field_Value_Changer_To_Testers() { // given - final AbstractAssetion abstractAssetion = new AbstractAssetionImplementation(); + final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final AbstractFieldValueChanger expectedFieldsValuesChanger = DefaultFieldValueChanger.INSTANCE; final EqualsTester equalsTester = mock(EqualsTester.class); - setInternalState(abstractAssetion, "testers", Sets.newHashSet(equalsTester)); - abstractAssetion.using(expectedFieldsValuesChanger); + setInternalState(abstractAssertion, "testers", Sets.newHashSet(equalsTester)); + abstractAssertion.using(expectedFieldsValuesChanger); // when - abstractAssetion.areWellImplemented(); + abstractAssertion.areWellImplemented(); // then verify(equalsTester, times(1)).setFieldValuesChanger(expectedFieldsValuesChanger); @@ -116,18 +116,18 @@ public void Should_Set_Field_Value_Changer_To_Testers() { @Test public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester() { // given - final AbstractAssetion abstractAssetion = new AbstractAssetionImplementation(); + final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final EqualsTester equalsTester = mock(EqualsTester.class); - setInternalState(abstractAssetion, "testers", Sets.newHashSet(equalsTester)); + setInternalState(abstractAssertion, "testers", Sets.newHashSet(equalsTester)); final Class expectedClass = String.class; final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); - abstractAssetion.create(expectedClass, expectedConstructorParameters); + abstractAssertion.create(expectedClass, expectedConstructorParameters); // when - abstractAssetion.areWellImplemented(); + abstractAssertion.areWellImplemented(); // then verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MapMatcher(expectedClass, @@ -137,38 +137,38 @@ public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester() @Test public void Should_Call_Next_Create_Method() { // given - final AbstractAssetion abstractAssetion = spy(new AbstractAssetionImplementation()); + final AbstractAssertion abstractAssertion = spy(new AbstractAssertionImplementation()); final EqualsTester equalsTester = mock(EqualsTester.class); - setInternalState(abstractAssetion, "testers", Sets.newHashSet(equalsTester)); + setInternalState(abstractAssertion, "testers", Sets.newHashSet(equalsTester)); final Class expectedClass = String.class; final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); - abstractAssetion.create(expectedClass, expectedArguments, expectedTypes); + abstractAssertion.create(expectedClass, expectedArguments, expectedTypes); // when - abstractAssetion.areWellImplemented(); + abstractAssertion.areWellImplemented(); // then - verify(abstractAssetion).create(eq(expectedClass), eq(expectedConstructorParameters)); + verify(abstractAssertion).create(eq(expectedClass), eq(expectedConstructorParameters)); } @Test public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester_Using_Class_Name() { // given - final AbstractAssetion abstractAssetion = new AbstractAssetionImplementation(); + final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final EqualsTester equalsTester = mock(EqualsTester.class); - setInternalState(abstractAssetion, "testers", Sets.newHashSet(equalsTester)); + setInternalState(abstractAssertion, "testers", Sets.newHashSet(equalsTester)); final Class expectedClass = String.class; final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); - abstractAssetion.create("java.lang.String", expectedConstructorParameters); + abstractAssertion.create("java.lang.String", expectedConstructorParameters); // when - abstractAssetion.areWellImplemented(); + abstractAssertion.areWellImplemented(); // then verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MapMatcher(expectedClass, @@ -178,24 +178,24 @@ public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester_Us @Test public void Should_Call_Next_Create_Method_Using_Class_Name() { // given - final AbstractAssetion abstractAssetion = spy(new AbstractAssetionImplementation()); + final AbstractAssertion abstractAssertion = spy(new AbstractAssertionImplementation()); final EqualsTester equalsTester = mock(EqualsTester.class); - setInternalState(abstractAssetion, "testers", Sets.newHashSet(equalsTester)); + setInternalState(abstractAssertion, "testers", Sets.newHashSet(equalsTester)); final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); final String expectedClassName = "java.lang.String"; - abstractAssetion.create(expectedClassName, expectedArguments, expectedTypes); + abstractAssertion.create(expectedClassName, expectedArguments, expectedTypes); // when - abstractAssetion.areWellImplemented(); + abstractAssertion.areWellImplemented(); // then - verify(abstractAssetion).create(eq(expectedClassName), eq(expectedConstructorParameters)); + verify(abstractAssertion).create(eq(expectedClassName), eq(expectedConstructorParameters)); } - private class AbstractAssetionImplementation extends AbstractAssetion { + private class AbstractAssertionImplementation extends AbstractAssertion { @Override protected void testImplementation() { diff --git a/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java b/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java index 4c9b42d7..b5d9ab30 100644 --- a/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java +++ b/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java @@ -31,7 +31,7 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class() { fieldName); // when - final SingleClassAssetion result = (SingleClassAssetion) Assertions.assertPojoMethodsFor(expectedClass); + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClass); final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, @@ -53,7 +53,7 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_Name() { fieldName); // when - final SingleClassAssetion result = (SingleClassAssetion) Assertions.assertPojoMethodsFor(expectedClassName); + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClassName); final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, @@ -76,8 +76,8 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_And_Field_ fieldName); // when - final SingleClassAssetion result = (SingleClassAssetion) Assertions.assertPojoMethodsFor(expectedClassName, - predicate); + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClassName, + predicate); final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, @@ -99,8 +99,8 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_Name_And_F fieldName); // when - final SingleClassAssetion result = (SingleClassAssetion) Assertions.assertPojoMethodsFor(expectedClass, - predicate); + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClass, + predicate); final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, @@ -118,7 +118,7 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_And_Field_ final ClassAndFieldPredicatePair expectedClassAndFieldPredicate = new ClassAndFieldPredicatePair(expectedClass); // when - final SingleClassAssetion result = (SingleClassAssetion) Assertions.assertPojoMethodsFor( + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor( expectedClassAndFieldPredicate, expectedClassAndFieldPredicate); final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, @@ -140,7 +140,7 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Package() { // when - final MultiClassAssetion result = (MultiClassAssetion) Assertions.assertPojoMethodsForAll(packageFilter); + final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(packageFilter); final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); @@ -158,8 +158,8 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Classes() { // when - final MultiClassAssetion result = (MultiClassAssetion) Assertions.assertPojoMethodsForAll(expectedClass1, - expectedClass2); + final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(expectedClass1, + expectedClass2); final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); @@ -177,8 +177,8 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Classes_Names() { // when - final MultiClassAssetion result = (MultiClassAssetion) Assertions.assertPojoMethodsForAll(expectedClass1Name, - expectedClass2Name); + final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(expectedClass1Name, + expectedClass2Name); final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); @@ -202,7 +202,9 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Class_And_Field_P // when - final MultiClassAssetion result = (MultiClassAssetion) Assertions.assertPojoMethodsForAll(pair1, pair2, pair2); + final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(pair1, + pair2, + pair2); final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); diff --git a/src/test/java/pl/pojo/tester/api/assertion/MultiClassAssetionTest.java b/src/test/java/pl/pojo/tester/api/assertion/MultiClassAssetionTest.java index 444a327c..43af4add 100644 --- a/src/test/java/pl/pojo/tester/api/assertion/MultiClassAssetionTest.java +++ b/src/test/java/pl/pojo/tester/api/assertion/MultiClassAssetionTest.java @@ -21,7 +21,7 @@ public class MultiClassAssetionTest { public void Should_Test_Against_Each_Tester() { // given final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair(A.class); - final MultiClassAssetion multiClassAssetion = new MultiClassAssetion(Lists.newArrayList( + final MultiClassAssertion multiClassAssetion = new MultiClassAssertion(Lists.newArrayList( classAndFieldPredicatePair)); final EqualsTester equalsTester1 = mock(EqualsTester.class); final EqualsTester equalsTester2 = mock(EqualsTester.class); diff --git a/src/test/java/pl/pojo/tester/api/assertion/SingleClassAssetionTest.java b/src/test/java/pl/pojo/tester/api/assertion/SingleClassAssetionTest.java index 2f30a58e..18b5275b 100644 --- a/src/test/java/pl/pojo/tester/api/assertion/SingleClassAssetionTest.java +++ b/src/test/java/pl/pojo/tester/api/assertion/SingleClassAssetionTest.java @@ -19,8 +19,8 @@ public void Should_Test_Against_Each_Tester() { // given final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair(A.class); final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = {classAndFieldPredicatePair}; - final SingleClassAssetion singleClassAssetion = new SingleClassAssetion(classAndFieldPredicatePair, - classAndFieldPredicatePairs); + final SingleClassAssertion singleClassAssetion = new SingleClassAssertion(classAndFieldPredicatePair, + classAndFieldPredicatePairs); final EqualsTester equalsTester1 = mock(EqualsTester.class); final EqualsTester equalsTester2 = mock(EqualsTester.class); setInternalState(singleClassAssetion, "testers", Sets.newHashSet(equalsTester1, equalsTester2)); From f8be8e388ff9d736a351dce36810e33c4a6f48df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jo=C5=84ski?= Date: Fri, 21 Oct 2016 19:52:47 +0200 Subject: [PATCH 2/5] #135. Changed Map to apache's multivalued map, so user can provide multiple constructor parameters for one class --- .../tester/api/ConstructorParameters.java | 10 +++- .../api/assertion/AbstractAssertion.java | 8 ++- .../pojo/tester/api/assertion/Assertions.java | 4 +- .../pl/pojo/tester/api/assertion/Method.java | 6 +- .../BestConstructorInstantiator.java | 19 +++++-- .../instantiator/CollectionInstantiator.java | 13 ++--- .../internal/instantiator/Instantiable.java | 5 +- .../instantiator/ObjectGenerator.java | 56 ++++++++++++------- .../UserDefinedConstructorInstantiator.java | 52 ++++++++++++----- .../internal/tester/AbstractTester.java | 11 ++-- .../internal/tester/ConstructorTester.java | 45 +++++++++++---- .../Constructors_First_Throws_Exception.java | 9 +++ .../java/helpers/MultiValuedMapMatcher.java | 34 +++++++++++ .../api/assertion/AbstractAssertionTest.java | 14 ++--- .../BestConstructorInstantiatorTest.java | 26 +++++++-- .../CollectionInstantiatorTest.java | 40 ++++++++++++- .../instantiator/InstantiableTest.java | 5 +- .../instantiator/ObjectGeneratorTest.java | 48 ++++++++++------ ...serDefinedConstructorInstantiatorTest.java | 12 ++-- .../internal/tester/AbstractTesterTest.java | 5 +- .../tester/ConstructorTesterTest.java | 12 ++-- 21 files changed, 314 insertions(+), 120 deletions(-) create mode 100644 src/test/java/classesForTest/Constructors_First_Throws_Exception.java create mode 100644 src/test/java/helpers/MultiValuedMapMatcher.java diff --git a/src/main/java/pl/pojo/tester/api/ConstructorParameters.java b/src/main/java/pl/pojo/tester/api/ConstructorParameters.java index e1cd33c4..c5336f12 100644 --- a/src/main/java/pl/pojo/tester/api/ConstructorParameters.java +++ b/src/main/java/pl/pojo/tester/api/ConstructorParameters.java @@ -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. *

@@ -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; @@ -60,4 +62,8 @@ public int hashCode() { .append(constructorParametersTypes) .toHashCode(); } + + public boolean matches(final Class[] parameterTypes) { + return Arrays.equals(constructorParametersTypes, parameterTypes); + } } diff --git a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java index 9581858c..5cc7d1a0 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java @@ -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; @@ -34,10 +34,12 @@ public abstract class AbstractAssertion { .forEach(DEFAULT_TESTERS::add); } - private final Map, ConstructorParameters> constructorParameters = new HashMap<>(); + private final MultiValuedMap, ConstructorParameters> constructorParameters = new + ArrayListValuedHashMap<>(); Set testers = new HashSet<>(); private AbstractFieldValueChanger abstractFieldValueChanger; + /** * Specifies what field values changer will be used for testing. * diff --git a/src/main/java/pl/pojo/tester/api/assertion/Assertions.java b/src/main/java/pl/pojo/tester/api/assertion/Assertions.java index 287034d3..98ca07ea 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/Assertions.java +++ b/src/main/java/pl/pojo/tester/api/assertion/Assertions.java @@ -120,7 +120,7 @@ public static AbstractAssertion assertPojoMethodsFor(final Class clazz, final public static AbstractAssertion assertPojoMethodsFor(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... - classAndFieldPredicatePairs) { + classAndFieldPredicatePairs) { checkNotNull("baseClassAndFieldPredicatePair", baseClassAndFieldPredicatePair); return new SingleClassAssertion(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs); } @@ -191,7 +191,7 @@ public static AbstractAssertion assertPojoMethodsForAll(final Class... classes) * @see SingleClassAssertion */ public static AbstractAssertion assertPojoMethodsForAll(final ClassAndFieldPredicatePair... - classesAndFieldPredicatesPairs) { + classesAndFieldPredicatesPairs) { checkNotNull("classesAndFieldPredicatesPairs", classesAndFieldPredicatesPairs); final List classAndFieldPredicatePairs = Arrays.asList( diff --git a/src/main/java/pl/pojo/tester/api/assertion/Method.java b/src/main/java/pl/pojo/tester/api/assertion/Method.java index 13ee1e96..f257c9ee 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/Method.java +++ b/src/main/java/pl/pojo/tester/api/assertion/Method.java @@ -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; @@ -17,7 +16,6 @@ * @author Piotr Joński * @since 0.1.0 */ -@Getter public enum Method { EQUALS(new EqualsTester()), HASH_CODE(new HashCodeTester()), @@ -31,4 +29,8 @@ public enum Method { Method(final AbstractTester tester) { this.tester = tester; } + + public AbstractTester getTester() { + return tester; + } } diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java index f1e5e5b9..2b856d3d 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java @@ -1,17 +1,20 @@ 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, ConstructorParameters> constructorParameters; + private final MultiValuedMap, ConstructorParameters> constructorParameters; - BestConstructorInstantiator(final Class clazz, final Map, ConstructorParameters> constructorParameters) { + BestConstructorInstantiator(final Class clazz, + final MultiValuedMap, ConstructorParameters> constructorParameters) { super(clazz); this.constructorParameters = constructorParameters; } @@ -25,9 +28,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) { diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java index 7eec4c45..90efc52b 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java @@ -23,7 +23,6 @@ import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; -import java.util.function.Supplier; import java.util.stream.Stream; class CollectionInstantiator extends ObjectInstantiator { @@ -67,7 +66,7 @@ public Object instantiate() { .filter(this::clazzCanBeAssigned) .map(Map.Entry::getValue) .findFirst() - .orElseThrow(createObjectInstantiationExceptionSupplier()); + .orElseThrow(this::createObjectInstantiationException); } private boolean clazzCanBeAssigned(final Map.Entry, Object> entry) { @@ -75,10 +74,10 @@ private boolean clazzCanBeAssigned(final Map.Entry, Object> entry) { .isAssignableFrom(clazz); } - private Supplier createObjectInstantiationExceptionSupplier() { - return () -> new ObjectInstantiationException(clazz, - "There is no declared object for that class. " - + "Please report an issue at " - + "https://github.com/sta-szek/pojo-tester"); + private ObjectInstantiationException createObjectInstantiationException() { + return new ObjectInstantiationException(clazz, "There is no declared object for that class. Please report an issue at " + + "https://github.com/sta-szek/pojo-tester"); + } + } diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java b/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java index fecb45f9..cc61da15 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java @@ -1,6 +1,7 @@ package pl.pojo.tester.internal.instantiator; +import org.apache.commons.collections4.MultiValuedMap; import pl.pojo.tester.api.ConstructorParameters; import java.lang.reflect.Constructor; @@ -15,7 +16,7 @@ public final class Instantiable { private Instantiable() {} static ObjectInstantiator forClass(final Class clazz, - final Map, ConstructorParameters> constructorParameters) { + final MultiValuedMap, ConstructorParameters> constructorParameters) { if (userDefinedConstructorParametersFor(clazz, constructorParameters)) { return new UserDefinedConstructorInstantiator(clazz, constructorParameters); } @@ -59,7 +60,7 @@ private static boolean isKindOfCollectionClass(final Class clazz) { } private static boolean userDefinedConstructorParametersFor(final Class clazz, - final Map, ConstructorParameters> + final MultiValuedMap, ConstructorParameters> constructorParameters) { return constructorParameters.containsKey(clazz); } diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java index 0a8fc5b4..2372803f 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java @@ -1,6 +1,12 @@ package pl.pojo.tester.internal.instantiator; +import org.apache.commons.collections4.MultiValuedMap; +import pl.pojo.tester.api.ClassAndFieldPredicatePair; +import pl.pojo.tester.api.ConstructorParameters; +import pl.pojo.tester.internal.field.AbstractFieldValueChanger; +import pl.pojo.tester.internal.utils.FieldUtils; + import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; @@ -12,17 +18,14 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; -import pl.pojo.tester.api.ClassAndFieldPredicatePair; -import pl.pojo.tester.api.ConstructorParameters; -import pl.pojo.tester.internal.field.AbstractFieldValueChanger; -import pl.pojo.tester.internal.utils.FieldUtils; public class ObjectGenerator { private final AbstractFieldValueChanger abstractFieldValueChanger; - private final Map, ConstructorParameters> constructorParameters; + private final MultiValuedMap, ConstructorParameters> constructorParameters; - public ObjectGenerator(final AbstractFieldValueChanger abstractFieldValueChanger, final Map, ConstructorParameters> constructorParameters) { + public ObjectGenerator(final AbstractFieldValueChanger abstractFieldValueChanger, + final MultiValuedMap, ConstructorParameters> constructorParameters) { this.abstractFieldValueChanger = abstractFieldValueChanger; this.constructorParameters = constructorParameters; } @@ -42,7 +45,8 @@ public Object generateSameInstance(final Object object) { public List generateDifferentObjects(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { - final Map, Predicate> userDefinedClassAndFieldPredicatePairsMap = convertToMap(classAndFieldPredicatePairs); + final Map, Predicate> userDefinedClassAndFieldPredicatePairsMap = convertToMap( + classAndFieldPredicatePairs); final Map, List> dejaVu = new HashMap<>(); final Class baseClass = baseClassAndFieldPredicatePair.getClazz(); @@ -51,7 +55,8 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba final List baseClassFieldsToChange = FieldUtils.getFields(baseClass, baseClassFieldPredicate); userDefinedClassAndFieldPredicatePairsMap.put(baseClass, baseClassFieldPredicate); - final Map, List> userDefinedClassAndFieldToChangePairsMap = convertToClassAndFieldsToChange(userDefinedClassAndFieldPredicatePairsMap); + final Map, List> userDefinedClassAndFieldToChangePairsMap = convertToClassAndFieldsToChange( + userDefinedClassAndFieldPredicatePairsMap); final List> baseObjectFieldsPermutations = FieldUtils.permutations(baseClassFieldsToChange); @@ -68,7 +73,8 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba for (final Field permutationField : eachBaseObjectFieldsPermutation) { final Class permutationFieldType = permutationField.getType(); - final List nestedFieldsToChangeInFieldType = userDefinedClassAndFieldToChangePairsMap.get(permutationFieldType); + final List nestedFieldsToChangeInFieldType = userDefinedClassAndFieldToChangePairsMap.get( + permutationFieldType); if (nestedFieldsToChangeInFieldType == null || permutationFieldType.equals(baseClass)) { Object newFieldTypeInstance = createNewInstance(permutationFieldType); @@ -82,12 +88,17 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba if (dejaVu.containsKey(permutationFieldType)) { nestedObjectsOfFieldType = new ArrayList<>(dejaVu.get(permutationFieldType)); } else { - final Predicate fieldPredicate = userDefinedClassAndFieldPredicatePairsMap.get(permutationFieldType); + final Predicate fieldPredicate = userDefinedClassAndFieldPredicatePairsMap.get( + permutationFieldType); final List fieldClassFields = FieldUtils.getFields(permutationFieldType, fieldPredicate); if (hasNestedFieldsToChange(fieldClassFields, userDefinedClassAndFieldPredicatePairsMap)) { - final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair(permutationFieldType, fieldPredicate); - nestedObjectsOfFieldType = generateDifferentObjects(classAndFieldPredicatePair, classAndFieldPredicatePairs); + final ClassAndFieldPredicatePair classAndFieldPredicatePair = new + ClassAndFieldPredicatePair( + permutationFieldType, + fieldPredicate); + nestedObjectsOfFieldType = generateDifferentObjects(classAndFieldPredicatePair, + classAndFieldPredicatePairs); } else { nestedObjectsOfFieldType = generateDifferentObjects(permutationFieldType, fieldClassFields); } @@ -98,7 +109,8 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba } partialResult.add(baseObjectCopy); - for (final Map.Entry> nestedObjectsToSet : nestedObjectsThatAreWaitingForSetInBaseObjectCopy.entrySet()) { + for (final Map.Entry> nestedObjectsToSet : + nestedObjectsThatAreWaitingForSetInBaseObjectCopy.entrySet()) { partialResult = createCopiesAndFillThem(partialResult, nestedObjectsToSet); } result.addAll(partialResult); @@ -119,13 +131,15 @@ private List generateDifferentObjects(final Class clazz, final List generateInstanceWithDifferentFieldValues(fieldObject, fields)) + .map(fields -> generateInstanceWithDifferentFieldValues(fieldObject, + fields)) .collect(Collectors.toList()); differentObjects.add(0, fieldObject); return differentObjects; } - private List createCopiesAndFillThem(final List baseObjects, final Map.Entry> nestedObjectsToSet) { + private List createCopiesAndFillThem(final List baseObjects, + final Map.Entry> nestedObjectsToSet) { final List result = new ArrayList<>(); final Field fieldToFill = nestedObjectsToSet.getKey(); final List objectsToFillWith = nestedObjectsToSet.getValue(); @@ -159,16 +173,20 @@ private List createCopies(final Object baseObject, final int size) { .collect(Collectors.toList()); } - private Map, List> convertToClassAndFieldsToChange(final Map, Predicate> classAndFieldPredicatePairMap) { + private Map, List> convertToClassAndFieldsToChange(final Map, Predicate> + classAndFieldPredicatePairMap) { return classAndFieldPredicatePairMap.entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, - entry -> FieldUtils.getFields(entry.getKey(), entry.getValue()))); + entry -> FieldUtils.getFields(entry.getKey(), + entry.getValue()))); } - private Map, Predicate> convertToMap(final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) { + private Map, Predicate> convertToMap(final ClassAndFieldPredicatePair[] + classAndFieldPredicatePairs) { return Stream.of(classAndFieldPredicatePairs) - .collect(Collectors.toMap(ClassAndFieldPredicatePair::getClazz, ClassAndFieldPredicatePair::getFieldsPredicate)); + .collect(Collectors.toMap(ClassAndFieldPredicatePair::getClazz, + ClassAndFieldPredicatePair::getFieldsPredicate)); } private Object makeThemEqual(final Object object, final Object newInstance) { diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java index fe9682ba..07a1fa90 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java @@ -1,56 +1,82 @@ 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.lang.reflect.Modifier; import java.util.Arrays; -import java.util.Map; +import java.util.Objects; import java.util.stream.Stream; -import pl.pojo.tester.api.ConstructorParameters; class UserDefinedConstructorInstantiator extends ObjectInstantiator { - private final Map, ConstructorParameters> constructorParameters; + private final MultiValuedMap, ConstructorParameters> constructorParameters; - UserDefinedConstructorInstantiator(final Class clazz, final Map, ConstructorParameters> constructorParameters) { + UserDefinedConstructorInstantiator(final Class clazz, + final MultiValuedMap, ConstructorParameters> constructorParameters) { super(clazz); this.constructorParameters = constructorParameters; } @Override public Object instantiate() { + return constructorParameters.get(clazz) + .stream() + .map(this::createObjectUsingConstructorParameters) + .filter(Objects::nonNull) + .findAny() + .orElseThrow(this::createObjectInstantiationException); + } + + private ObjectInstantiationException createObjectInstantiationException() { + return new ObjectInstantiationException(clazz, + "Could not instantiate object by any user defined constructor " + + "types and parameters."); + } + + private Object createObjectUsingConstructorParameters(final ConstructorParameters constructorParameters) { try { - final ConstructorParameters constructorParameters = this.constructorParameters.get(clazz); Class[] constructorParametersTypes = constructorParameters.getConstructorParametersTypes(); Object[] arguments = constructorParameters.getConstructorParameters(); if (isInnerClass()) { - constructorParametersTypes = putEnclosingClassAsFirstParameterType(clazz.getEnclosingClass(), constructorParametersTypes); + constructorParametersTypes = putEnclosingClassAsFirstParameterType(clazz.getEnclosingClass(), + constructorParametersTypes); final Object enclosingClassInstance = instantiateEnclosingClass(); - arguments = putEnclosingClassInstanceAsFirstParameter(enclosingClassInstance, arguments); + arguments = putEnclosingClassInstanceAsFirstParameter(enclosingClassInstance, + arguments); } - final Constructor constructor = clazz.getDeclaredConstructor(constructorParametersTypes); + final Constructor constructor = clazz.getDeclaredConstructor( + constructorParametersTypes); constructor.setAccessible(true); return constructor.newInstance(arguments); - } catch (final NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { - throw new ObjectInstantiationException(clazz, e.getMessage(), e); + } catch (final NoSuchMethodException + | InstantiationException + | InvocationTargetException + | IllegalAccessException + | IllegalArgumentException e) { + return null; } } + private Object instantiateEnclosingClass() { final Class enclosingClass = clazz.getEnclosingClass(); return Instantiable.forClass(enclosingClass, constructorParameters) .instantiate(); } - private Object[] putEnclosingClassInstanceAsFirstParameter(final Object enclosingClassInstance, final Object[] arguments) { + private Object[] putEnclosingClassInstanceAsFirstParameter(final Object enclosingClassInstance, + final Object[] arguments) { return Stream.concat(Stream.of(enclosingClassInstance), Arrays.stream(arguments)) .toArray(Object[]::new); } - - private Class[] putEnclosingClassAsFirstParameterType(final Class enclosingClass, final Class[] constructorParametersTypes) { + private Class[] putEnclosingClassAsFirstParameterType(final Class enclosingClass, + final Class[] constructorParametersTypes) { return Stream.concat(Stream.of(enclosingClass), Arrays.stream(constructorParametersTypes)) .toArray(Class[]::new); } diff --git a/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java b/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java index bfa5e673..9324deab 100644 --- a/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java +++ b/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java @@ -1,5 +1,7 @@ package pl.pojo.tester.internal.tester; +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import pl.pojo.tester.api.ClassAndFieldPredicatePair; @@ -11,9 +13,7 @@ import pl.pojo.tester.internal.instantiator.ObjectGenerator; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.Predicate; @@ -21,7 +21,7 @@ public abstract class AbstractTester { ObjectGenerator objectGenerator; TestAssertions testAssertions = new TestAssertions(); - private Map, ConstructorParameters> constructorParameters = new HashMap<>(); + private MultiValuedMap, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>(); private AbstractFieldValueChanger fieldValuesChanger = DefaultFieldValueChanger.INSTANCE; @@ -66,7 +66,8 @@ public void setFieldValuesChanger(final AbstractFieldValueChanger fieldValuesCha objectGenerator = new ObjectGenerator(fieldValuesChanger, constructorParameters); } - public void setUserDefinedConstructors(final Map, ConstructorParameters> constructorParameters) { + public void setUserDefinedConstructors(final MultiValuedMap, ConstructorParameters> + constructorParameters) { this.constructorParameters = constructorParameters; objectGenerator = new ObjectGenerator(fieldValuesChanger, constructorParameters); } @@ -99,7 +100,7 @@ public int hashCode() { .toHashCode(); } - protected Map, ConstructorParameters> getConstructorParameters() { + protected MultiValuedMap, ConstructorParameters> getConstructorParameters() { return constructorParameters; } } diff --git a/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java b/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java index ca9513f6..29a01b16 100644 --- a/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java +++ b/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java @@ -1,16 +1,23 @@ package pl.pojo.tester.internal.tester; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import pl.pojo.tester.api.ClassAndFieldPredicatePair; +import pl.pojo.tester.api.ConstructorParameters; import pl.pojo.tester.internal.field.AbstractFieldValueChanger; import java.lang.reflect.Constructor; import java.util.Arrays; +import java.util.Collection; import java.util.List; +import java.util.function.Predicate; import java.util.stream.Collectors; public class ConstructorTester extends AbstractTester { + private static final Logger LOGGER = LoggerFactory.getLogger(ConstructorTester.class); + public ConstructorTester() { super(); } @@ -23,37 +30,52 @@ public ConstructorTester(final AbstractFieldValueChanger abstractFieldValueChang public void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { final Class testedClass = baseClassAndFieldPredicatePair.getClazz(); - final List> declaredConstructors = getUserDefinedConstructors(testedClass); + final List> declaredConstructors = getNotSyntheticConstructorFromClass(testedClass); declaredConstructors.forEach(this::tryInstantiate); } - private List> getUserDefinedConstructors(final Class testedClass) { + private List> getNotSyntheticConstructorFromClass(final Class testedClass) { return Arrays.stream(testedClass.getDeclaredConstructors()) .filter(this::isNotSynthetic) .collect(Collectors.toList()); - } private boolean isNotSynthetic(final Constructor constructor) { return !constructor.isSynthetic(); } - private void tryInstantiate(final Constructor constructor) { - final Object[] constructorParameters; - if (userDefinedParameters(constructor)) { - constructorParameters = getConstructorParameters().get(constructor.getDeclaringClass()) - .getConstructorParameters(); + final Object[] parameters; + final Predicate typesMatches = ctr -> ctr.matches(constructor.getParameterTypes()); + + if (constructorParametersAreProvided(constructor)) { + final Collection constructorParameters = getConstructorParameters(constructor); + parameters = constructorParameters.stream() + .filter(typesMatches) + .map(ConstructorParameters::getConstructorParameters) + .findFirst() + .orElseGet(() -> logAndTryToCreateOwnParameters(constructor)); } else { - constructorParameters = createConstructorParameters(constructor); + parameters = createConstructorParameters(constructor); } testAssertions.assertThatConstructor(constructor) - .willInstantiateClassUsing(constructorParameters); + .willInstantiateClassUsing(parameters); + } + + private Object[] logAndTryToCreateOwnParameters(final Constructor constructor) { + LOGGER.warn(String.format("Class '%s' could not be created by constructor '%s' and any user defined parameters", + constructor.getDeclaringClass(), + constructor)); + return createConstructorParameters(constructor); + } + + private Collection getConstructorParameters(final Constructor constructor) { + return getConstructorParameters().get(constructor.getDeclaringClass()); } - private boolean userDefinedParameters(final Constructor constructor) { + private boolean constructorParametersAreProvided(final Constructor constructor) { final Class declaringClass = constructor.getDeclaringClass(); return getConstructorParameters().containsKey(declaringClass); } @@ -64,5 +86,4 @@ private Object[] createConstructorParameters(final Constructor constructor) { .toArray(); } - } diff --git a/src/test/java/classesForTest/Constructors_First_Throws_Exception.java b/src/test/java/classesForTest/Constructors_First_Throws_Exception.java new file mode 100644 index 00000000..b2084609 --- /dev/null +++ b/src/test/java/classesForTest/Constructors_First_Throws_Exception.java @@ -0,0 +1,9 @@ +package classesForTest; + +public class Constructors_First_Throws_Exception { + private Constructors_First_Throws_Exception() { + throw new NullPointerException("test"); + } + + private Constructors_First_Throws_Exception(final Object o) { } +} \ No newline at end of file diff --git a/src/test/java/helpers/MultiValuedMapMatcher.java b/src/test/java/helpers/MultiValuedMapMatcher.java new file mode 100644 index 00000000..3ddd1f24 --- /dev/null +++ b/src/test/java/helpers/MultiValuedMapMatcher.java @@ -0,0 +1,34 @@ +package helpers; + +import org.apache.commons.collections4.MultiValuedMap; +import org.mockito.ArgumentMatcher; +import pl.pojo.tester.api.ConstructorParameters; + +import java.util.Arrays; + +public class MultiValuedMapMatcher implements ArgumentMatcher, ConstructorParameters>> { + + private final Class expectedClass; + private final ConstructorParameters expectedArguments; + + public MultiValuedMapMatcher(final Class expectedClass, final ConstructorParameters expectedArguments) { + this.expectedClass = expectedClass; + this.expectedArguments = expectedArguments; + } + + + @Override + public boolean matches(final MultiValuedMap, ConstructorParameters> argument) { + if (!argument.containsKey(expectedClass)) { + return false; + } + return argument.get(expectedClass) + .stream() + .map(actualArgument -> Arrays.equals(actualArgument.getConstructorParameters(), + expectedArguments.getConstructorParameters()) + && Arrays.equals(actualArgument.getConstructorParametersTypes(), + expectedArguments.getConstructorParametersTypes())) + .findAny() + .isPresent(); + } +} diff --git a/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java b/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java index 73020dd4..05f29690 100644 --- a/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java +++ b/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java @@ -2,7 +2,7 @@ import classesForTest.fields.TestEnum1; import com.google.common.collect.Sets; -import helpers.MapMatcher; +import helpers.MultiValuedMapMatcher; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; @@ -114,7 +114,7 @@ public void Should_Set_Field_Value_Changer_To_Testers() { } @Test - public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester() { + public void Should_Set_User_Defined_Class_And_Constructor_Parameters_To_Tester() { // given final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final EqualsTester equalsTester = mock(EqualsTester.class); @@ -130,8 +130,8 @@ public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester() abstractAssertion.areWellImplemented(); // then - verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MapMatcher(expectedClass, - expectedConstructorParameters))); + verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MultiValuedMapMatcher(expectedClass, + expectedConstructorParameters))); } @Test @@ -155,7 +155,7 @@ public void Should_Call_Next_Create_Method() { } @Test - public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester_Using_Class_Name() { + public void Should_Set_User_Defined_Class_And_Constructor_Parameters_To_Tester_Using_Class_Name() { // given final AbstractAssertion abstractAssertion = new AbstractAssertionImplementation(); final EqualsTester equalsTester = mock(EqualsTester.class); @@ -171,8 +171,8 @@ public void Should_Set_User_Defined_Class_And_Constructor_Paramters_To_Tester_Us abstractAssertion.areWellImplemented(); // then - verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MapMatcher(expectedClass, - expectedConstructorParameters))); + verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MultiValuedMapMatcher(expectedClass, + expectedConstructorParameters))); } @Test diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiatorTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiatorTest.java index 6411efd0..5bfac9f2 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiatorTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiatorTest.java @@ -3,13 +3,13 @@ import classesForTest.ClassContainingStaticClasses; import classesForTest.Constructor_Stream; import classesForTest.Constructor_Thread; +import classesForTest.Constructors_First_Throws_Exception; import classesForTest.PackageConstructor; import classesForTest.Person; import classesForTest.PrivateConstructor; import classesForTest.ProtectedConstructor; -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Stream; +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; @@ -21,6 +21,8 @@ import pl.pojo.tester.internal.utils.FieldUtils; import pl.pojo.tester.internal.utils.MethodUtils; +import java.util.stream.Stream; + import static helpers.TestHelper.getDefaultDisplayName; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; @@ -29,7 +31,8 @@ @RunWith(JUnitPlatform.class) public class BestConstructorInstantiatorTest { - private final Map, ConstructorParameters> constructorParameters = new HashMap<>(); + private final MultiValuedMap, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>(); + @TestFactory public Stream Should_Instantiate_Non_Public_Classes() { @@ -149,7 +152,20 @@ public Executable Should_Throw_Exception_When_Cannot_Instantiate_Class(final Cla @Test public void Should_Create_Object_Using_Private_Constructor() { // given - final Class classToInstantiate = PrivateConstructor.class; + final Class classToInstantiate = PrivateConstructor.class; + final BestConstructorInstantiator instantiator = new BestConstructorInstantiator(classToInstantiate, constructorParameters); + + // when + final Object result = instantiator.instantiate(); + + // then + assertThat(result).isInstanceOf(classToInstantiate); + } + + @Test + public void Should_Create_Object_With_Second_Constructor_If_First_Threw_exception() { + // given + final Class classToInstantiate = Constructors_First_Throws_Exception.class; final BestConstructorInstantiator instantiator = new BestConstructorInstantiator(classToInstantiate, constructorParameters); // when diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/CollectionInstantiatorTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/CollectionInstantiatorTest.java index afacee64..d2ff6b17 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/CollectionInstantiatorTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/CollectionInstantiatorTest.java @@ -1,15 +1,39 @@ package pl.pojo.tester.internal.instantiator; -import java.util.*; -import java.util.stream.Stream; import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.function.Executable; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.NavigableSet; +import java.util.Queue; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.Stack; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.Vector; +import java.util.stream.Stream; + import static helpers.TestHelper.getDefaultDisplayName; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; import static org.junit.jupiter.api.DynamicTest.dynamicTest; @RunWith(JUnitPlatform.class) @@ -57,4 +81,16 @@ public Executable Should_Return_Expected_Collection_Object(final Class classT }; } + @Test + public void Should_Throws_Exception_When_Prepared_Objects_Do_Not_Contain_Expected_Class() { + // given + final CollectionInstantiator instantiator = new CollectionInstantiator(String.class); + + // when + final Throwable result = catchThrowable(instantiator::instantiate); + + // then + assertThat(result).isInstanceOf(ObjectInstantiationException.class); + } + } diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/InstantiableTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/InstantiableTest.java index 0ce71f50..1af518e7 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/InstantiableTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/InstantiableTest.java @@ -6,6 +6,8 @@ import classesForTest.Constructor_Thread; import classesForTest.EmptyEnum; import lombok.AllArgsConstructor; +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; @@ -45,7 +47,8 @@ @RunWith(JUnitPlatform.class) public class InstantiableTest { - private static final Map, ConstructorParameters> CLASS_AND_CONSTRUCTOR_PARAMETERS = new HashMap<>(); + + private static final MultiValuedMap, ConstructorParameters> CLASS_AND_CONSTRUCTOR_PARAMETERS = new ArrayListValuedHashMap<>(); @BeforeAll private static void beforeAll() { diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java index aa435b63..a5231cac 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java @@ -7,13 +7,10 @@ import classesForTest.fields.TestEnum1; import classesForTest.fields.collections.collection.Collections; import classesForTest.fields.collections.map.Maps; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.stream.Stream; import lombok.AllArgsConstructor; import lombok.Data; +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; @@ -28,6 +25,10 @@ import pl.pojo.tester.internal.field.AbstractFieldValueChanger; import pl.pojo.tester.internal.field.DefaultFieldValueChanger; +import java.util.List; +import java.util.Random; +import java.util.stream.Stream; + import static helpers.TestHelper.getDefaultDisplayName; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.DynamicTest.dynamicTest; @@ -36,13 +37,15 @@ public class ObjectGeneratorTest { private final AbstractFieldValueChanger abstractFieldValueChanger = DefaultFieldValueChanger.INSTANCE; - private final Map, ConstructorParameters> constructorParameters = new HashMap<>(); + private final MultiValuedMap, ConstructorParameters> constructorParameters = new + ArrayListValuedHashMap<>(); @Test public void Should_Generate_Different_Objects_For_Class_Containing_Boolean_Type() { // given final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, constructorParameters); - final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair(ClassWithBooleanField.class); + final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair( + ClassWithBooleanField.class); // when final List result = objectGenerator.generateDifferentObjects(classAndFieldPredicatePair); @@ -77,7 +80,8 @@ public Stream Should_Create_Same_Instance() { public Executable Should_Create_Same_Instance(final Object objectToCreateSameInstance) { return () -> { // given - final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, constructorParameters); + final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, + constructorParameters); // when final Object result = objectGenerator.generateSameInstance(objectToCreateSameInstance); @@ -105,7 +109,8 @@ public Stream Should_Generate_Different_Objects() { public Executable Should_Generate_Different_Objects(final DifferentObjectTestCase testCase) { return () -> { // given - final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, constructorParameters); + final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, + constructorParameters); final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair(testCase.clazz); // when @@ -121,30 +126,37 @@ public Executable Should_Generate_Different_Objects(final DifferentObjectTestCas public Stream Should_Generate_Different_Objects_Recursively() throws IllegalAccessException { final RecursivelyDifferentObjectTestCase case1 = new RecursivelyDifferentObjectTestCase(18, pair(D.class), - new ClassAndFieldPredicatePair[]{pair(E.class), pair(F.class)}); + new ClassAndFieldPredicatePair[]{ + pair(E.class), + pair(F.class)}); final RecursivelyDifferentObjectTestCase case2 = new RecursivelyDifferentObjectTestCase(6, pair(G.class), - new ClassAndFieldPredicatePair[]{pair(F.class)}); + new ClassAndFieldPredicatePair[]{ + pair(F.class)}); final RecursivelyDifferentObjectTestCase case3 = new RecursivelyDifferentObjectTestCase(945, pair(H.class), - new ClassAndFieldPredicatePair[]{pair(A.class), - pair(B.class), - pair(F.class), - pair(G.class)}); + new ClassAndFieldPredicatePair[]{ + pair(A.class), + pair(B.class), + pair(F.class), + pair(G.class)}); return Stream.of(case1, case2, case3) - .map(value -> dynamicTest(getDefaultDisplayName(value), Should_Generate_Different_Objects_Recursively(value))); + .map(value -> dynamicTest(getDefaultDisplayName(value), + Should_Generate_Different_Objects_Recursively(value))); } public Executable Should_Generate_Different_Objects_Recursively(final RecursivelyDifferentObjectTestCase testCase) { return () -> { // given - final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, constructorParameters); + final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, + constructorParameters); // when - final List result = objectGenerator.generateDifferentObjects(testCase.baseClass, testCase.otherClasses); + final List result = objectGenerator.generateDifferentObjects(testCase.baseClass, + testCase.otherClasses); // then assertThat(result).hasSize(testCase.expectedSize) diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java index 9add45bc..1f895e8f 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java @@ -1,10 +1,9 @@ package pl.pojo.tester.internal.instantiator; -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Stream; import lombok.AllArgsConstructor; import lombok.Data; +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; @@ -13,6 +12,8 @@ import org.junit.runner.RunWith; import pl.pojo.tester.api.ConstructorParameters; +import java.util.stream.Stream; + import static helpers.TestHelper.getDefaultDisplayName; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; @@ -21,7 +22,7 @@ @RunWith(JUnitPlatform.class) public class UserDefinedConstructorInstantiatorTest { - private final Map, ConstructorParameters> constructorParameters = new HashMap<>(); + private final MultiValuedMap, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>(); { constructorParameters.put(UserDefinedClass.class, new ConstructorParameters(new Object[]{1, 2}, new Class[]{int.class, int.class})); @@ -82,8 +83,7 @@ public Stream Should_Throw_Exception_When_Cannot_Instantiate_Class( public Executable Should_Throw_Exception_When_Cannot_Instantiate_Class(final Class classToInstantiate) { return () -> { // given - final UserDefinedConstructorInstantiator instantiator = new UserDefinedConstructorInstantiator(classToInstantiate, - constructorParameters); + final UserDefinedConstructorInstantiator instantiator = new UserDefinedConstructorInstantiator(classToInstantiate, constructorParameters); // when final Throwable result = catchThrowable(instantiator::instantiate); diff --git a/src/test/java/pl/pojo/tester/internal/tester/AbstractTesterTest.java b/src/test/java/pl/pojo/tester/internal/tester/AbstractTesterTest.java index e08644d4..0c0a1e2c 100644 --- a/src/test/java/pl/pojo/tester/internal/tester/AbstractTesterTest.java +++ b/src/test/java/pl/pojo/tester/internal/tester/AbstractTesterTest.java @@ -4,6 +4,7 @@ import helpers.RecursivelyEqualArgumentMatcher; import helpers.StringPredicateArgumentMatcher; import lombok.Data; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @@ -12,7 +13,6 @@ import pl.pojo.tester.internal.field.DefaultFieldValueChanger; import pl.pojo.tester.internal.instantiator.ObjectGenerator; -import java.util.HashMap; import java.util.function.Predicate; import static org.assertj.core.api.Assertions.assertThat; @@ -109,7 +109,8 @@ public void Should_Create_New_Object_Generator_When_User_Defined_Class_And_Const final ObjectGenerator beforeChange = abstractTester.objectGenerator; // when - abstractTester.setUserDefinedConstructors(new HashMap<>()); + abstractTester.setUserDefinedConstructors(new ArrayListValuedHashMap<>()); + final ObjectGenerator afterChange = abstractTester.objectGenerator; // then diff --git a/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java b/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java index 8812fbe0..d564e134 100644 --- a/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java +++ b/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java @@ -1,6 +1,8 @@ package pl.pojo.tester.internal.tester; import classesForTest.ClassWithSyntheticConstructor; +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @@ -13,11 +15,10 @@ import pl.pojo.tester.internal.instantiator.Instantiable; import pl.pojo.tester.internal.preconditions.ParameterPreconditions; -import java.util.HashMap; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; @RunWith(JUnitPlatform.class) public class ConstructorTesterTest { @@ -61,9 +62,8 @@ public void Should_Use_User_Constructor_Parameters() { final ConstructorParameters parameters = new ConstructorParameters(new Object[]{"string"}, new Class[]{String.class}); - final HashMap, ConstructorParameters> constructorParameters = mock(HashMap.class); - when(constructorParameters.get(ClassWithSyntheticConstructor.class)).thenReturn(parameters); - when(constructorParameters.containsKey(ClassWithSyntheticConstructor.class)).thenReturn(true); + final MultiValuedMap, ConstructorParameters> constructorParameters = spy(new ArrayListValuedHashMap<>()); + constructorParameters.put(ClassWithSyntheticConstructor.class, parameters); final ConstructorTester constructorTester = new ConstructorTester(); constructorTester.setUserDefinedConstructors(constructorParameters); From caf8b85e625736e54c82d8cf0fc0dc1e19fd10d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jo=C5=84ski?= Date: Fri, 21 Oct 2016 20:04:55 +0200 Subject: [PATCH 3/5] #135. Refactoring --- .../api/assertion/AbstractAssertion.java | 14 ++----- .../pojo/tester/api/assertion/Assertions.java | 12 ++---- .../api/assertion/SingleClassAssertion.java | 3 +- .../BestConstructorInstantiator.java | 3 +- .../instantiator/ObjectGenerator.java | 39 ++++++------------- .../UserDefinedConstructorInstantiator.java | 13 ++----- .../internal/tester/AbstractTester.java | 6 +-- .../java/helpers/MultiValuedMapMatcher.java | 7 ++-- .../tester/api/ConstructorParametersTest.java | 27 +++++++++++++ .../instantiator/ObjectGeneratorTest.java | 25 +++++------- ...serDefinedConstructorInstantiatorTest.java | 3 +- 11 files changed, 68 insertions(+), 84 deletions(-) diff --git a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java index 5cc7d1a0..ff5065fb 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java @@ -34,8 +34,7 @@ public abstract class AbstractAssertion { .forEach(DEFAULT_TESTERS::add); } - private final MultiValuedMap, ConstructorParameters> constructorParameters = new - ArrayListValuedHashMap<>(); + private final MultiValuedMap, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>(); Set testers = new HashSet<>(); private AbstractFieldValueChanger abstractFieldValueChanger; @@ -121,9 +120,7 @@ public void areWellImplemented() { * * @see ConstructorParameters */ - public AbstractAssertion 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, @@ -142,8 +139,7 @@ public AbstractAssertion create(final String qualifiedClassName, * * @see ConstructorParameters */ - public AbstractAssertion create(final String qualifiedClassName, - final ConstructorParameters constructorParameters) { + public AbstractAssertion create(final String qualifiedClassName, final ConstructorParameters constructorParameters) { checkNotBlank("qualifiedClassName", qualifiedClassName); checkNotNull("constructorParameters", constructorParameters); @@ -164,9 +160,7 @@ public AbstractAssertion create(final String qualifiedClassName, * * @see ConstructorParameters */ - public AbstractAssertion 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, diff --git a/src/main/java/pl/pojo/tester/api/assertion/Assertions.java b/src/main/java/pl/pojo/tester/api/assertion/Assertions.java index 98ca07ea..1cbf4d27 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/Assertions.java +++ b/src/main/java/pl/pojo/tester/api/assertion/Assertions.java @@ -75,8 +75,7 @@ public static AbstractAssertion assertPojoMethodsFor(final Class clazz) { * @see MultiClassAssertion * @see SingleClassAssertion */ - public static AbstractAssertion assertPojoMethodsFor(final String qualifiedClassName, - final Predicate fieldPredicate) { + public static AbstractAssertion assertPojoMethodsFor(final String qualifiedClassName, final Predicate fieldPredicate) { checkNotBlank("qualifiedClassName", qualifiedClassName); checkNotNull("fieldPredicate", fieldPredicate); @@ -117,10 +116,8 @@ public static AbstractAssertion assertPojoMethodsFor(final Class clazz, final * @see MultiClassAssertion * @see SingleClassAssertion */ - public static AbstractAssertion assertPojoMethodsFor(final ClassAndFieldPredicatePair - baseClassAndFieldPredicatePair, - final ClassAndFieldPredicatePair... - classAndFieldPredicatePairs) { + public static AbstractAssertion assertPojoMethodsFor(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, + final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { checkNotNull("baseClassAndFieldPredicatePair", baseClassAndFieldPredicatePair); return new SingleClassAssertion(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs); } @@ -190,8 +187,7 @@ public static AbstractAssertion assertPojoMethodsForAll(final Class... classes) * @see MultiClassAssertion * @see SingleClassAssertion */ - public static AbstractAssertion assertPojoMethodsForAll(final ClassAndFieldPredicatePair... - classesAndFieldPredicatesPairs) { + public static AbstractAssertion assertPojoMethodsForAll(final ClassAndFieldPredicatePair... classesAndFieldPredicatesPairs) { checkNotNull("classesAndFieldPredicatesPairs", classesAndFieldPredicatesPairs); final List classAndFieldPredicatePairs = Arrays.asList( diff --git a/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java b/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java index b6b22ad7..0da2ab82 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java @@ -7,8 +7,7 @@ class SingleClassAssertion extends AbstractAssertion { private final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair; private final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs; - SingleClassAssertion(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, - final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) { + SingleClassAssertion(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) { this.baseClassAndFieldPredicatePair = baseClassAndFieldPredicatePair; this.classAndFieldPredicatePairs = classAndFieldPredicatePairs; } diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java index 2b856d3d..07a3400d 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/BestConstructorInstantiator.java @@ -13,8 +13,7 @@ class BestConstructorInstantiator extends ObjectInstantiator { private final MultiValuedMap, ConstructorParameters> constructorParameters; - BestConstructorInstantiator(final Class clazz, - final MultiValuedMap, ConstructorParameters> constructorParameters) { + BestConstructorInstantiator(final Class clazz, final MultiValuedMap, ConstructorParameters> constructorParameters) { super(clazz); this.constructorParameters = constructorParameters; } diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java index 2372803f..7b904262 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java @@ -45,8 +45,7 @@ public Object generateSameInstance(final Object object) { public List generateDifferentObjects(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { - final Map, Predicate> userDefinedClassAndFieldPredicatePairsMap = convertToMap( - classAndFieldPredicatePairs); + final Map, Predicate> userDefinedClassAndFieldPredicatePairsMap = convertToMap(classAndFieldPredicatePairs); final Map, List> dejaVu = new HashMap<>(); final Class baseClass = baseClassAndFieldPredicatePair.getClazz(); @@ -55,8 +54,7 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba final List baseClassFieldsToChange = FieldUtils.getFields(baseClass, baseClassFieldPredicate); userDefinedClassAndFieldPredicatePairsMap.put(baseClass, baseClassFieldPredicate); - final Map, List> userDefinedClassAndFieldToChangePairsMap = convertToClassAndFieldsToChange( - userDefinedClassAndFieldPredicatePairsMap); + final Map, List> userDefinedClassAndFieldToChangePairsMap = convertToClassAndFieldsToChange(userDefinedClassAndFieldPredicatePairsMap); final List> baseObjectFieldsPermutations = FieldUtils.permutations(baseClassFieldsToChange); @@ -73,8 +71,7 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba for (final Field permutationField : eachBaseObjectFieldsPermutation) { final Class permutationFieldType = permutationField.getType(); - final List nestedFieldsToChangeInFieldType = userDefinedClassAndFieldToChangePairsMap.get( - permutationFieldType); + final List nestedFieldsToChangeInFieldType = userDefinedClassAndFieldToChangePairsMap.get(permutationFieldType); if (nestedFieldsToChangeInFieldType == null || permutationFieldType.equals(baseClass)) { Object newFieldTypeInstance = createNewInstance(permutationFieldType); @@ -88,17 +85,12 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba if (dejaVu.containsKey(permutationFieldType)) { nestedObjectsOfFieldType = new ArrayList<>(dejaVu.get(permutationFieldType)); } else { - final Predicate fieldPredicate = userDefinedClassAndFieldPredicatePairsMap.get( - permutationFieldType); + final Predicate fieldPredicate = userDefinedClassAndFieldPredicatePairsMap.get(permutationFieldType); final List fieldClassFields = FieldUtils.getFields(permutationFieldType, fieldPredicate); if (hasNestedFieldsToChange(fieldClassFields, userDefinedClassAndFieldPredicatePairsMap)) { - final ClassAndFieldPredicatePair classAndFieldPredicatePair = new - ClassAndFieldPredicatePair( - permutationFieldType, - fieldPredicate); - nestedObjectsOfFieldType = generateDifferentObjects(classAndFieldPredicatePair, - classAndFieldPredicatePairs); + final ClassAndFieldPredicatePair classAndFieldPredicatePair = new ClassAndFieldPredicatePair(permutationFieldType, fieldPredicate); + nestedObjectsOfFieldType = generateDifferentObjects(classAndFieldPredicatePair, classAndFieldPredicatePairs); } else { nestedObjectsOfFieldType = generateDifferentObjects(permutationFieldType, fieldClassFields); } @@ -109,8 +101,7 @@ public List generateDifferentObjects(final ClassAndFieldPredicatePair ba } partialResult.add(baseObjectCopy); - for (final Map.Entry> nestedObjectsToSet : - nestedObjectsThatAreWaitingForSetInBaseObjectCopy.entrySet()) { + for (final Map.Entry> nestedObjectsToSet : nestedObjectsThatAreWaitingForSetInBaseObjectCopy.entrySet()) { partialResult = createCopiesAndFillThem(partialResult, nestedObjectsToSet); } result.addAll(partialResult); @@ -131,15 +122,13 @@ private List generateDifferentObjects(final Class clazz, final List generateInstanceWithDifferentFieldValues(fieldObject, - fields)) + .map(fields -> generateInstanceWithDifferentFieldValues(fieldObject, fields)) .collect(Collectors.toList()); differentObjects.add(0, fieldObject); return differentObjects; } - private List createCopiesAndFillThem(final List baseObjects, - final Map.Entry> nestedObjectsToSet) { + private List createCopiesAndFillThem(final List baseObjects, final Map.Entry> nestedObjectsToSet) { final List result = new ArrayList<>(); final Field fieldToFill = nestedObjectsToSet.getKey(); final List objectsToFillWith = nestedObjectsToSet.getValue(); @@ -173,17 +162,13 @@ private List createCopies(final Object baseObject, final int size) { .collect(Collectors.toList()); } - private Map, List> convertToClassAndFieldsToChange(final Map, Predicate> - classAndFieldPredicatePairMap) { + private Map, List> convertToClassAndFieldsToChange(final Map, Predicate> classAndFieldPredicatePairMap) { return classAndFieldPredicatePairMap.entrySet() .stream() - .collect(Collectors.toMap(Map.Entry::getKey, - entry -> FieldUtils.getFields(entry.getKey(), - entry.getValue()))); + .collect(Collectors.toMap(Map.Entry::getKey, entry -> FieldUtils.getFields(entry.getKey(), entry.getValue()))); } - private Map, Predicate> convertToMap(final ClassAndFieldPredicatePair[] - classAndFieldPredicatePairs) { + private Map, Predicate> convertToMap(final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) { return Stream.of(classAndFieldPredicatePairs) .collect(Collectors.toMap(ClassAndFieldPredicatePair::getClazz, ClassAndFieldPredicatePair::getFieldsPredicate)); diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java index 07a1fa90..197da363 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java @@ -14,8 +14,7 @@ class UserDefinedConstructorInstantiator extends ObjectInstantiator { private final MultiValuedMap, ConstructorParameters> constructorParameters; - UserDefinedConstructorInstantiator(final Class clazz, - final MultiValuedMap, ConstructorParameters> constructorParameters) { + UserDefinedConstructorInstantiator(final Class clazz, final MultiValuedMap, ConstructorParameters> constructorParameters) { super(clazz); this.constructorParameters = constructorParameters; } @@ -31,9 +30,7 @@ public Object instantiate() { } private ObjectInstantiationException createObjectInstantiationException() { - return new ObjectInstantiationException(clazz, - "Could not instantiate object by any user defined constructor " + - "types and parameters."); + return new ObjectInstantiationException(clazz, "Could not instantiate object by any user defined constructor types and parameters."); } private Object createObjectUsingConstructorParameters(final ConstructorParameters constructorParameters) { @@ -69,14 +66,12 @@ private Object instantiateEnclosingClass() { .instantiate(); } - private Object[] putEnclosingClassInstanceAsFirstParameter(final Object enclosingClassInstance, - final Object[] arguments) { + private Object[] putEnclosingClassInstanceAsFirstParameter(final Object enclosingClassInstance, final Object[] arguments) { return Stream.concat(Stream.of(enclosingClassInstance), Arrays.stream(arguments)) .toArray(Object[]::new); } - private Class[] putEnclosingClassAsFirstParameterType(final Class enclosingClass, - final Class[] constructorParametersTypes) { + private Class[] putEnclosingClassAsFirstParameterType(final Class enclosingClass, final Class[] constructorParametersTypes) { return Stream.concat(Stream.of(enclosingClass), Arrays.stream(constructorParametersTypes)) .toArray(Class[]::new); } diff --git a/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java b/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java index 9324deab..54600e37 100644 --- a/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java +++ b/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java @@ -44,8 +44,7 @@ public void test(final Class clazz, final Predicate fieldPredicate) { test(classAndFieldPredicatePair); } - public abstract void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, - final ClassAndFieldPredicatePair... classAndFieldPredicatePairs); + public abstract void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... classAndFieldPredicatePairs); public void testAll(final Class... classes) { final ClassAndFieldPredicatePair[] classesAndFieldPredicatesPairs = Arrays.stream(classes) @@ -66,8 +65,7 @@ public void setFieldValuesChanger(final AbstractFieldValueChanger fieldValuesCha objectGenerator = new ObjectGenerator(fieldValuesChanger, constructorParameters); } - public void setUserDefinedConstructors(final MultiValuedMap, ConstructorParameters> - constructorParameters) { + public void setUserDefinedConstructors(final MultiValuedMap, ConstructorParameters> constructorParameters) { this.constructorParameters = constructorParameters; objectGenerator = new ObjectGenerator(fieldValuesChanger, constructorParameters); } diff --git a/src/test/java/helpers/MultiValuedMapMatcher.java b/src/test/java/helpers/MultiValuedMapMatcher.java index 3ddd1f24..de709ea4 100644 --- a/src/test/java/helpers/MultiValuedMapMatcher.java +++ b/src/test/java/helpers/MultiValuedMapMatcher.java @@ -24,10 +24,9 @@ public boolean matches(final MultiValuedMap, ConstructorParameters> arg } return argument.get(expectedClass) .stream() - .map(actualArgument -> Arrays.equals(actualArgument.getConstructorParameters(), - expectedArguments.getConstructorParameters()) - && Arrays.equals(actualArgument.getConstructorParametersTypes(), - expectedArguments.getConstructorParametersTypes())) + .map(actualArgument -> Arrays.equals(actualArgument.getConstructorParameters(), expectedArguments.getConstructorParameters()) + && + Arrays.equals(actualArgument.getConstructorParametersTypes(), expectedArguments.getConstructorParametersTypes())) .findAny() .isPresent(); } diff --git a/src/test/java/pl/pojo/tester/api/ConstructorParametersTest.java b/src/test/java/pl/pojo/tester/api/ConstructorParametersTest.java index e3f6c0cc..4811d557 100644 --- a/src/test/java/pl/pojo/tester/api/ConstructorParametersTest.java +++ b/src/test/java/pl/pojo/tester/api/ConstructorParametersTest.java @@ -98,4 +98,31 @@ public void Should_Generate_Same_Hash_Codes_For_Equal_Objects() { assertThat(result1).isEqualTo(result2); } + @Test + public void Should_Return_True_If_Contains_Matching_Parameter_Types() { + // given + final Class[] expectedToContain = {String.class}; + final ConstructorParameters constructorParameters = new ConstructorParameters(new Object[0], expectedToContain); + + // when + final boolean result = constructorParameters.matches(expectedToContain); + + // then + assertThat(result).isTrue(); + } + + @Test + public void Should_Return_False_If_Does_Not_Contain_Matching_Parameter_Types() { + // given + final Class[] constructorParametersTypes = {String.class}; + final Class[] expectedNotToContain = {Object.class}; + final ConstructorParameters constructorParameters = new ConstructorParameters(new Object[0], constructorParametersTypes); + + // when + final boolean result = constructorParameters.matches(expectedNotToContain); + + // then + assertThat(result).isFalse(); + } + } diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java index a5231cac..85a37370 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java @@ -126,37 +126,30 @@ public Executable Should_Generate_Different_Objects(final DifferentObjectTestCas public Stream Should_Generate_Different_Objects_Recursively() throws IllegalAccessException { final RecursivelyDifferentObjectTestCase case1 = new RecursivelyDifferentObjectTestCase(18, pair(D.class), - new ClassAndFieldPredicatePair[]{ - pair(E.class), - pair(F.class)}); + new ClassAndFieldPredicatePair[]{pair(E.class), pair(F.class)}); final RecursivelyDifferentObjectTestCase case2 = new RecursivelyDifferentObjectTestCase(6, pair(G.class), - new ClassAndFieldPredicatePair[]{ - pair(F.class)}); + new ClassAndFieldPredicatePair[]{pair(F.class)}); final RecursivelyDifferentObjectTestCase case3 = new RecursivelyDifferentObjectTestCase(945, pair(H.class), - new ClassAndFieldPredicatePair[]{ - pair(A.class), - pair(B.class), - pair(F.class), - pair(G.class)}); + new ClassAndFieldPredicatePair[]{pair(A.class), + pair(B.class), + pair(F.class), + pair(G.class)}); return Stream.of(case1, case2, case3) - .map(value -> dynamicTest(getDefaultDisplayName(value), - Should_Generate_Different_Objects_Recursively(value))); + .map(value -> dynamicTest(getDefaultDisplayName(value), Should_Generate_Different_Objects_Recursively(value))); } public Executable Should_Generate_Different_Objects_Recursively(final RecursivelyDifferentObjectTestCase testCase) { return () -> { // given - final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, - constructorParameters); + final ObjectGenerator objectGenerator = new ObjectGenerator(abstractFieldValueChanger, constructorParameters); // when - final List result = objectGenerator.generateDifferentObjects(testCase.baseClass, - testCase.otherClasses); + final List result = objectGenerator.generateDifferentObjects(testCase.baseClass, testCase.otherClasses); // then assertThat(result).hasSize(testCase.expectedSize) diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java index 1f895e8f..b37000dc 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiatorTest.java @@ -76,8 +76,7 @@ public void Should_Create_Object_For_Nested_Class() { public Stream Should_Throw_Exception_When_Cannot_Instantiate_Class() { return Stream.of(One_Arg_Constructor_Throws_NPE.class, No_Args_Constructor_Throws_NPE.class) - .map(value -> dynamicTest(getDefaultDisplayName(value.getName()), - Should_Throw_Exception_When_Cannot_Instantiate_Class(value))); + .map(value -> dynamicTest(getDefaultDisplayName(value.getName()), Should_Throw_Exception_When_Cannot_Instantiate_Class(value))); } public Executable Should_Throw_Exception_When_Cannot_Instantiate_Class(final Class classToInstantiate) { From bd3de528da48efc5879f54d9659cb11e07c0a063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jo=C5=84ski?= Date: Fri, 21 Oct 2016 20:12:36 +0200 Subject: [PATCH 4/5] #135. Formatting --- .../internal/instantiator/Instantiable.java | 3 +- .../instantiator/ObjectGenerator.java | 3 +- .../UserDefinedConstructorInstantiator.java | 15 +-- .../api/assertion/AbstractAssertionTest.java | 18 ++-- .../tester/api/assertion/AssertionsTest.java | 95 ++++++------------- .../assertion/equals/EqualAssertionsTest.java | 9 +- 6 files changed, 44 insertions(+), 99 deletions(-) diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java b/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java index cc61da15..8aff1461 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java @@ -60,8 +60,7 @@ private static boolean isKindOfCollectionClass(final Class clazz) { } private static boolean userDefinedConstructorParametersFor(final Class clazz, - final MultiValuedMap, ConstructorParameters> - constructorParameters) { + final MultiValuedMap, ConstructorParameters> constructorParameters) { return constructorParameters.containsKey(clazz); } diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java index 7b904262..d9fa1719 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java @@ -170,8 +170,7 @@ private Map, List> convertToClassAndFieldsToChange(final Map, Predicate> convertToMap(final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs) { return Stream.of(classAndFieldPredicatePairs) - .collect(Collectors.toMap(ClassAndFieldPredicatePair::getClazz, - ClassAndFieldPredicatePair::getFieldsPredicate)); + .collect(Collectors.toMap(ClassAndFieldPredicatePair::getClazz, ClassAndFieldPredicatePair::getFieldsPredicate)); } private Object makeThemEqual(final Object object, final Object newInstance) { diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java index 197da363..4de5f23e 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/UserDefinedConstructorInstantiator.java @@ -39,22 +39,15 @@ private Object createObjectUsingConstructorParameters(final ConstructorParameter Object[] arguments = constructorParameters.getConstructorParameters(); if (isInnerClass()) { - constructorParametersTypes = putEnclosingClassAsFirstParameterType(clazz.getEnclosingClass(), - constructorParametersTypes); + constructorParametersTypes = putEnclosingClassAsFirstParameterType(clazz.getEnclosingClass(), constructorParametersTypes); final Object enclosingClassInstance = instantiateEnclosingClass(); - arguments = putEnclosingClassInstanceAsFirstParameter(enclosingClassInstance, - arguments); + arguments = putEnclosingClassInstanceAsFirstParameter(enclosingClassInstance, arguments); } - final Constructor constructor = clazz.getDeclaredConstructor( - constructorParametersTypes); + final Constructor constructor = clazz.getDeclaredConstructor(constructorParametersTypes); constructor.setAccessible(true); return constructor.newInstance(arguments); - } catch (final NoSuchMethodException - | InstantiationException - | InvocationTargetException - | IllegalAccessException - | IllegalArgumentException e) { + } catch (final NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { return null; } } diff --git a/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java b/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java index 05f29690..ce1e2181 100644 --- a/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java +++ b/src/test/java/pl/pojo/tester/api/assertion/AbstractAssertionTest.java @@ -122,16 +122,14 @@ public void Should_Set_User_Defined_Class_And_Constructor_Parameters_To_Tester() final Class expectedClass = String.class; final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; - final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, - expectedTypes); + final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); abstractAssertion.create(expectedClass, expectedConstructorParameters); // when abstractAssertion.areWellImplemented(); // then - verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MultiValuedMapMatcher(expectedClass, - expectedConstructorParameters))); + verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MultiValuedMapMatcher(expectedClass, expectedConstructorParameters))); } @Test @@ -143,8 +141,7 @@ public void Should_Call_Next_Create_Method() { final Class expectedClass = String.class; final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; - final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, - expectedTypes); + final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); abstractAssertion.create(expectedClass, expectedArguments, expectedTypes); // when @@ -163,16 +160,14 @@ public void Should_Set_User_Defined_Class_And_Constructor_Parameters_To_Tester_U final Class expectedClass = String.class; final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; - final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, - expectedTypes); + final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); abstractAssertion.create("java.lang.String", expectedConstructorParameters); // when abstractAssertion.areWellImplemented(); // then - verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MultiValuedMapMatcher(expectedClass, - expectedConstructorParameters))); + verify(equalsTester, times(1)).setUserDefinedConstructors(argThat(new MultiValuedMapMatcher(expectedClass, expectedConstructorParameters))); } @Test @@ -183,8 +178,7 @@ public void Should_Call_Next_Create_Method_Using_Class_Name() { setInternalState(abstractAssertion, "testers", Sets.newHashSet(equalsTester)); final Object[] expectedArguments = {'c', 'h', 'a', 'r'}; final Class[] expectedTypes = {char.class, char.class, char.class, char.class}; - final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, - expectedTypes); + final ConstructorParameters expectedConstructorParameters = new ConstructorParameters(expectedArguments, expectedTypes); final String expectedClassName = "java.lang.String"; abstractAssertion.create(expectedClassName, expectedArguments, expectedTypes); diff --git a/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java b/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java index b5d9ab30..3b844169 100644 --- a/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java +++ b/src/test/java/pl/pojo/tester/api/assertion/AssertionsTest.java @@ -26,16 +26,12 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class() { // given final Class expectedClass = A.class; final String fieldName = "a"; - final ClassAndFieldPredicatePairCondition conditionToMatch = new ClassAndFieldPredicatePairCondition( - expectedClass, - fieldName); + final ClassAndFieldPredicatePairCondition conditionToMatch = new ClassAndFieldPredicatePairCondition(expectedClass, fieldName); // when final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClass); - final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, - "baseClassAndFieldPredicatePair"); - final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); + final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(baseClassAndFieldPredicatePair).is(conditionToMatch); @@ -48,16 +44,12 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_Name() { final Class expectedClass = A.class; final String fieldName = "a"; final String expectedClassName = expectedClass.getName(); - final ClassNameAndFieldPredicatePairCondition conditionToMatch = new ClassNameAndFieldPredicatePairCondition( - expectedClassName, - fieldName); + final ClassNameAndFieldPredicatePairCondition conditionToMatch = new ClassNameAndFieldPredicatePairCondition(expectedClassName, fieldName); // when final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClassName); - final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, - "baseClassAndFieldPredicatePair"); - final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); + final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(baseClassAndFieldPredicatePair).is(conditionToMatch); @@ -71,17 +63,12 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_And_Field_ final String fieldName = "a"; final Predicate predicate = name -> name.equals(fieldName); final String expectedClassName = expectedClass.getName(); - final ClassNameAndFieldPredicatePairCondition conditionToMatch = new ClassNameAndFieldPredicatePairCondition( - expectedClassName, - fieldName); + final ClassNameAndFieldPredicatePairCondition conditionToMatch = new ClassNameAndFieldPredicatePairCondition(expectedClassName, fieldName); // when - final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClassName, - predicate); - final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, - "baseClassAndFieldPredicatePair"); - final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClassName, predicate); + final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); + final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(baseClassAndFieldPredicatePair).is(conditionToMatch); @@ -94,17 +81,12 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_Name_And_F final Class expectedClass = A.class; final String fieldName = "a"; final Predicate predicate = name -> name.equals(fieldName); - final ClassAndFieldPredicatePairCondition conditionToMatch = new ClassAndFieldPredicatePairCondition( - expectedClass, - fieldName); + final ClassAndFieldPredicatePairCondition conditionToMatch = new ClassAndFieldPredicatePairCondition(expectedClass, fieldName); // when - final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClass, - predicate); - final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, - "baseClassAndFieldPredicatePair"); - final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClass, predicate); + final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); + final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(baseClassAndFieldPredicatePair).is(conditionToMatch); @@ -118,13 +100,10 @@ public void Should_Create_Expected_Single_Class_Assertion_Using_Class_And_Field_ final ClassAndFieldPredicatePair expectedClassAndFieldPredicate = new ClassAndFieldPredicatePair(expectedClass); // when - final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor( - expectedClassAndFieldPredicate, - expectedClassAndFieldPredicate); - final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, - "baseClassAndFieldPredicatePair"); - final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final SingleClassAssertion result = (SingleClassAssertion) Assertions.assertPojoMethodsFor(expectedClassAndFieldPredicate, + expectedClassAndFieldPredicate); + final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair = getInternalState(result, "baseClassAndFieldPredicatePair"); + final ClassAndFieldPredicatePair[] classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(baseClassAndFieldPredicatePair).isEqualTo(expectedClassAndFieldPredicate); @@ -141,8 +120,7 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Package() { // when final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(packageFilter); - final List classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(classAndFieldPredicatePairs).hasSize(2); @@ -158,10 +136,8 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Classes() { // when - final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(expectedClass1, - expectedClass2); - final List classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(expectedClass1, expectedClass2); + final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(classAndFieldPredicatePairs).hasSize(2); @@ -177,19 +153,13 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Classes_Names() { // when - final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(expectedClass1Name, - expectedClass2Name); - final List classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(expectedClass1Name, expectedClass2Name); + final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(classAndFieldPredicatePairs).hasSize(2); - assertThat(classAndFieldPredicatePairs.get(0)).is(new ClassNameAndFieldPredicatePairCondition - (expectedClass1Name, - "a")); - assertThat(classAndFieldPredicatePairs.get(1)).is(new ClassNameAndFieldPredicatePairCondition - (expectedClass2Name, - "b")); + assertThat(classAndFieldPredicatePairs.get(0)).is(new ClassNameAndFieldPredicatePairCondition(expectedClass1Name, "a")); + assertThat(classAndFieldPredicatePairs.get(1)).is(new ClassNameAndFieldPredicatePairCondition(expectedClass2Name, "b")); } @Test @@ -202,11 +172,8 @@ public void Should_Create_Expected_Multi_Class_Assertion_Using_Class_And_Field_P // when - final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(pair1, - pair2, - pair2); - final List classAndFieldPredicatePairs = getInternalState(result, - "classAndFieldPredicatePairs"); + final MultiClassAssertion result = (MultiClassAssertion) Assertions.assertPojoMethodsForAll(pair1, pair2, pair2); + final List classAndFieldPredicatePairs = getInternalState(result, "classAndFieldPredicatePairs"); // then assertThat(classAndFieldPredicatePairs).hasSize(3); @@ -229,9 +196,7 @@ private class ClassAndFieldPredicatePairCondition extends Condition expectedClass, final String stringToMatchPredicate) { - this.classAndFieldPredicatePairArgumentMatcher = new ClassAndFieldPredicatePairArgumentMatcher - (expectedClass, - stringToMatchPredicate); + this.classAndFieldPredicatePairArgumentMatcher = new ClassAndFieldPredicatePairArgumentMatcher(expectedClass, stringToMatchPredicate); } @Override @@ -244,9 +209,7 @@ private class ClassNameAndFieldPredicatePairCondition extends Condition equalAssertions.isTransitive(objectUnderAssert, - objectUnderAssert)); + final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, objectUnderAssert)); // then assertThat(result).isInstanceOf(TransitiveEqualsAssertionError.class); @@ -90,8 +89,7 @@ public void Should_Throw_Exception_When_Equals_Method_Is_Not_Transitive_Between_ final EqualAssertions equalAssertions = new EqualAssertions(objectUnderAssert); // when - final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, - objectUnderAssert)); + final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, objectUnderAssert)); // then assertThat(result).isInstanceOf(TransitiveEqualsAssertionError.class); @@ -197,8 +195,7 @@ public void Should_Not_Throw_Exception_When_Equals_Method_Is_Transitive() { final EqualAssertions equalAssertions = new EqualAssertions(objectUnderAssert); // when - final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, - objectUnderAssert)); + final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, objectUnderAssert)); // then assertThat(result).isNull(); From 1bbbf4d2023ebb884b99f8ee9c9d65e6504db989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jo=C5=84ski?= Date: Mon, 24 Oct 2016 20:01:29 +0200 Subject: [PATCH 5/5] #135. More tests, refactoring --- .../api/assertion/MultiClassAssertion.java | 1 + .../api/assertion/SingleClassAssertion.java | 5 ++- .../instantiator/CollectionInstantiator.java | 4 +- .../internal/tester/ConstructorTester.java | 9 ++-- .../tester/ConstructorTesterTest.java | 42 ++++++++++++++++++- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssertion.java b/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssertion.java index 80963315..a6342207 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssertion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/MultiClassAssertion.java @@ -9,6 +9,7 @@ class MultiClassAssertion extends AbstractAssertion { private final List classAndFieldPredicatePairs; MultiClassAssertion(final List classAndFieldPredicatePairs) { + super(); this.classAndFieldPredicatePairs = classAndFieldPredicatePairs; } diff --git a/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java b/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java index 0da2ab82..deecda1e 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/SingleClassAssertion.java @@ -2,14 +2,17 @@ import pl.pojo.tester.api.ClassAndFieldPredicatePair; +import java.util.Arrays; + class SingleClassAssertion extends AbstractAssertion { private final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair; private 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 diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java index 90efc52b..78d5c066 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/CollectionInstantiator.java @@ -75,8 +75,8 @@ private boolean clazzCanBeAssigned(final Map.Entry, Object> entry) { } private ObjectInstantiationException createObjectInstantiationException() { - return new ObjectInstantiationException(clazz, "There is no declared object for that class. Please report an issue at " + - "https://github.com/sta-szek/pojo-tester"); + return new ObjectInstantiationException(clazz, "There is no declared object for that class. Please report an issue at " + + "https://github.com/sta-szek/pojo-tester"); } diff --git a/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java b/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java index 29a01b16..b094abbb 100644 --- a/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java +++ b/src/main/java/pl/pojo/tester/internal/tester/ConstructorTester.java @@ -27,8 +27,7 @@ public ConstructorTester(final AbstractFieldValueChanger abstractFieldValueChang } @Override - public void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, - final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { + public void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) { final Class testedClass = baseClassAndFieldPredicatePair.getClazz(); final List> declaredConstructors = getNotSyntheticConstructorFromClass(testedClass); @@ -47,12 +46,12 @@ private boolean isNotSynthetic(final Constructor constructor) { private void tryInstantiate(final Constructor constructor) { final Object[] parameters; - final Predicate typesMatches = ctr -> ctr.matches(constructor.getParameterTypes()); + final Predicate matchingConstructorParameterTypes = ctr -> ctr.matches(constructor.getParameterTypes()); if (constructorParametersAreProvided(constructor)) { final Collection constructorParameters = getConstructorParameters(constructor); parameters = constructorParameters.stream() - .filter(typesMatches) + .filter(matchingConstructorParameterTypes) .map(ConstructorParameters::getConstructorParameters) .findFirst() .orElseGet(() -> logAndTryToCreateOwnParameters(constructor)); @@ -65,7 +64,7 @@ private void tryInstantiate(final Constructor constructor) { } private Object[] logAndTryToCreateOwnParameters(final Constructor constructor) { - LOGGER.warn(String.format("Class '%s' could not be created by constructor '%s' and any user defined parameters", + LOGGER.warn(String.format("Class '%s' could not be created by constructor '%s' and any user defined parameters.", constructor.getDeclaringClass(), constructor)); return createConstructorParameters(constructor); diff --git a/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java b/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java index d564e134..7b8cb263 100644 --- a/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java +++ b/src/test/java/pl/pojo/tester/internal/tester/ConstructorTesterTest.java @@ -17,8 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; @RunWith(JUnitPlatform.class) public class ConstructorTesterTest { @@ -76,6 +75,45 @@ public void Should_Use_User_Constructor_Parameters() { verify(constructorParameters).get(ClassWithSyntheticConstructor.class); } + @Test + public void Should_Create_Constructor_Parameters_When_Parameters_Are_Not_Provided() { + // given + final Class[] classesToTest = {ClassWithSyntheticConstructor.class}; + + final MultiValuedMap, ConstructorParameters> constructorParameters = spy(new ArrayListValuedHashMap<>()); + + final ConstructorTester constructorTester = new ConstructorTester(); + constructorTester.setUserDefinedConstructors(constructorParameters); + + // when + final Throwable result = catchThrowable(() -> constructorTester.testAll(classesToTest)); + + // then + assertThat(result).isNull(); + verify(constructorParameters, never()).get(ClassWithSyntheticConstructor.class); + } + + @Test + public void Should_Create_Constructor_Parameters_When_Could_Not_Find_Matching_Constructor_Parameters_Types() { + // given + final Class[] classesToTest = {ClassWithSyntheticConstructor.class}; + + final ConstructorParameters parameters = spy(new ConstructorParameters(new Object[]{"to", "many", "parameters"}, + new Class[]{String.class, String.class, String.class})); + final MultiValuedMap, ConstructorParameters> constructorParameters = spy(new ArrayListValuedHashMap<>()); + constructorParameters.put(ClassWithSyntheticConstructor.class, parameters); + + final ConstructorTester constructorTester = new ConstructorTester(); + constructorTester.setUserDefinedConstructors(constructorParameters); + + // when + final Throwable result = catchThrowable(() -> constructorTester.testAll(classesToTest)); + + // then + assertThat(result).isNull(); + verify(parameters, never()).getConstructorParameters(); + } + private static class Pojo { public Pojo() { }