diff --git a/CHANGELOG.md b/CHANGELOG.md index 4079ad5..4cc5cdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Version 3.1-SNAPSHOT ------------- +ADDED: + +- new class ContractVerifier created that can verify a given class the contracts of the methods equals, hashcode and toString + CHANGED: - update of dependency jobject-clone version to 3 diff --git a/pom.xml b/pom.xml index c986869..bc70f81 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ jobj-contract-verifier 3.1-SNAPSHOT - + ${project.artifactId} Verify if java objects fulfill the contracts of equals, hashcode and toString @@ -20,12 +20,14 @@ https://github.com/astrapi69/${project.artifactId} - + 3 5.1 1.9.3 + + 5.8 @@ -80,6 +82,12 @@ test-objects ${test-objects.version} + + + de.alpharogroup + randomizer-core + ${randomizer-core.version} + @@ -96,7 +104,12 @@ io.github.benas random-beans - + + + de.alpharogroup + randomizer-core + + commons-beanutils commons-beanutils @@ -115,5 +128,5 @@ - + diff --git a/src/main/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheck.java b/src/main/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheck.java index 80092b0..4cb2d0f 100644 --- a/src/main/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheck.java +++ b/src/main/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheck.java @@ -29,6 +29,7 @@ import de.alpharogroup.evaluate.object.api.ContractViolation; import de.alpharogroup.evaluate.object.enums.EqualsHashcodeContractViolation; import de.alpharogroup.evaluate.object.enums.ToStringContractViolation; +import de.alpharogroup.random.RandomObjectFactory; import io.github.benas.randombeans.EnhancedRandomBuilder; import lombok.experimental.UtilityClass; @@ -244,10 +245,12 @@ public static Optional equalsAndHashcodeUnequality(final * Signals that an I/O exception has occurred * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader + * @throws NoSuchFieldException + * is thrown if no such field exists */ public static Optional equalsHashcodeAndToString(Class cls) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, - InstantiationException, IOException, ClassNotFoundException + InstantiationException, IOException, ClassNotFoundException, NoSuchFieldException { Function, T> function = new EnhancedRandomBuilder().build()::nextObject; return equalsHashcodeAndToString(cls, function); @@ -278,11 +281,14 @@ public static Optional equalsHashcodeAndToString(Class * Signals that an I/O exception has occurred * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader + * @throws NoSuchFieldException + * is thrown if no such field exists */ @SuppressWarnings("unchecked") public static Optional equalsHashcodeAndToString(Class cls, - Function, T> function) throws NoSuchMethodException, IllegalAccessException, - InvocationTargetException, InstantiationException, IOException, ClassNotFoundException + Function, T> function) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, + InstantiationException, IOException, ClassNotFoundException, NoSuchFieldException { if (cls == null) { @@ -292,12 +298,16 @@ public static Optional equalsHashcodeAndToString(Class T second = null; do { - if(second != null) { - + if (second == null) + { + second = function.apply(cls); + } + else + { + second = RandomObjectFactory.newRandomObject(cls); } - second = function.apply(cls); } - while(second.equals(first)); + while (second.equals(first)); final T third = (T)CloneObjectExtensions.cloneObject(first); final T fourth = (T)CloneObjectExtensions.cloneObject(third); diff --git a/src/main/java/de/alpharogroup/evaluate/object/enums/VerificationType.java b/src/main/java/de/alpharogroup/evaluate/object/enums/VerificationType.java index 8dfcb92..d004d3b 100644 --- a/src/main/java/de/alpharogroup/evaluate/object/enums/VerificationType.java +++ b/src/main/java/de/alpharogroup/evaluate/object/enums/VerificationType.java @@ -1,7 +1,27 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ package de.alpharogroup.evaluate.object.enums; /** - * The class {@link VerificationConfiguration} holds the data how to verify of an object + * The class {@link VerificationType} holds the data how to verify of an object */ public enum VerificationType { diff --git a/src/main/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluator.java b/src/main/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluator.java index da6c985..735a034 100644 --- a/src/main/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluator.java +++ b/src/main/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluator.java @@ -171,10 +171,12 @@ public static boolean evaluateEqualsAndHashcodeUnequality(final T object, * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader + * @throws NoSuchFieldException + * is thrown if no such field exists */ public static boolean evaluateEqualsHashcodeAndToString(Class cls) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, - InstantiationException, IOException, ClassNotFoundException + InstantiationException, IOException, ClassNotFoundException, NoSuchFieldException { return !EqualsHashCodeAndToStringCheck.equalsHashcodeAndToString(cls).isPresent(); } @@ -203,10 +205,13 @@ public static boolean evaluateEqualsHashcodeAndToString(Class cls) * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader + * @throws NoSuchFieldException + * is thrown if no such field exists */ public static boolean evaluateEqualsHashcodeAndToString(Class cls, - Function, T> function) throws NoSuchMethodException, IllegalAccessException, - InvocationTargetException, InstantiationException, IOException, ClassNotFoundException + Function, T> function) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, + InstantiationException, IOException, ClassNotFoundException, NoSuchFieldException { return !EqualsHashCodeAndToStringCheck.equalsHashcodeAndToString(cls, function).isPresent(); } diff --git a/src/main/java/de/alpharogroup/evaluate/object/verifier/ContractVerifier.java b/src/main/java/de/alpharogroup/evaluate/object/verifier/ContractVerifier.java index 4cc7191..2ec0a94 100644 --- a/src/main/java/de/alpharogroup/evaluate/object/verifier/ContractVerifier.java +++ b/src/main/java/de/alpharogroup/evaluate/object/verifier/ContractVerifier.java @@ -1,3 +1,23 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ package de.alpharogroup.evaluate.object.verifier; import java.io.IOException; @@ -22,24 +42,49 @@ @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public final class ContractVerifier { + + /** + * Factory method for create a new {@link ContractVerifier} object from the given class + * + * @param + * the generic type + * @param type + * the class + * @return the contract verifier + */ public static ContractVerifier of(final @NonNull Class type) { return new ContractVerifier<>(type); } + /** The cls. */ Class cls; + /** The factory function. */ @NonFinal Function, T> factoryFunction; + /** The verification type. */ @NonFinal VerificationType verificationType = VerificationType.EQUALS_HASHCODE_AND_TO_STRING; - ContractVerifier(final @NonNull Class cls) + /** + * Private constructor + * + * @param cls + * the class + */ + private ContractVerifier(final @NonNull Class cls) { this.cls = cls; } + /** + * Verify the given class and returns an {@link Optional} that is empty if the verification was + * successful + * + * @return an empty {@link Optional} object if the verification was successful + */ public Optional verify() { Optional verificationResult = Optional.empty(); @@ -47,8 +92,11 @@ public Optional verify() { if (factoryFunction != null) { - verificationResult = EqualsHashCodeAndToStringCheck.equalsHashcodeAndToString(cls, factoryFunction); - } else { + verificationResult = EqualsHashCodeAndToStringCheck.equalsHashcodeAndToString(cls, + factoryFunction); + } + else + { verificationResult = EqualsHashCodeAndToStringCheck.equalsHashcodeAndToString(cls); } } @@ -76,16 +124,34 @@ public Optional verify() { throw new AssertionError(e.getMessage(), e); } + catch (NoSuchFieldException e) + { + throw new AssertionError(e.getMessage(), e); + } return verificationResult; } + /** + * Builder method that sets the verification type + * {@link VerificationType#EQUALS_HASHCODE_AND_TO_STRING} + * + * @return this {@link ContractVerifier} + */ public ContractVerifier withEqualsHashcodeAndToString() { - verificationType = VerificationType.EQUALS_HASHCODE_AND_TO_STRING; + verificationType = VerificationType.EQUALS_HASHCODE_AND_TO_STRING; return this; } - public ContractVerifier withFactoryFunction(final @NonNull Function, T> factoryFunction) + /** + * Builder method that sets the factory function + * + * @param factoryFunction + * the factory function + * @return this {@link ContractVerifier} + */ + public ContractVerifier withFactoryFunction( + final @NonNull Function, T> factoryFunction) { this.factoryFunction = factoryFunction; return this; diff --git a/src/main/resources/LICENSE.txt b/src/main/resources/LICENSE.txt new file mode 100644 index 0000000..ce28436 --- /dev/null +++ b/src/main/resources/LICENSE.txt @@ -0,0 +1,18 @@ +The MIT License + +Copyright (C) ${year} ${owner} + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/src/test/java/de/alpharogroup/evaluate/object/BaseEnhancedRandomExtensions.java b/src/test/java/de/alpharogroup/evaluate/object/BaseEnhancedRandomExtensions.java index 0f66779..eb76e30 100644 --- a/src/test/java/de/alpharogroup/evaluate/object/BaseEnhancedRandomExtensions.java +++ b/src/test/java/de/alpharogroup/evaluate/object/BaseEnhancedRandomExtensions.java @@ -1,3 +1,23 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ package de.alpharogroup.evaluate.object; import io.github.benas.randombeans.EnhancedRandomBuilder; diff --git a/src/test/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheckTest.java b/src/test/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheckTest.java index b23a10d..74f5791 100644 --- a/src/test/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheckTest.java +++ b/src/test/java/de/alpharogroup/evaluate/object/checkers/EqualsHashCodeAndToStringCheckTest.java @@ -216,11 +216,13 @@ public void testEqualsHashcodeAndToString() * Signals that an I/O exception has occurred * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader + * @throws NoSuchFieldException + * is thrown if no such field exists */ @Test(enabled = true) public void testEqualsHashcodeAndToStringClass() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, - InstantiationException, IOException, ClassNotFoundException + InstantiationException, IOException, ClassNotFoundException, NoSuchFieldException { actual = EqualsHashCodeAndToStringCheck.equalsHashcodeAndToString(null); expected = Optional.of(ToStringContractViolation.CLASS_NULL_ARGUMENT); diff --git a/src/test/java/de/alpharogroup/evaluate/object/evaluators/ComparableEvaluatorTest.java b/src/test/java/de/alpharogroup/evaluate/object/evaluators/ComparableEvaluatorTest.java index 720e44c..8eb1e4b 100644 --- a/src/test/java/de/alpharogroup/evaluate/object/evaluators/ComparableEvaluatorTest.java +++ b/src/test/java/de/alpharogroup/evaluate/object/evaluators/ComparableEvaluatorTest.java @@ -29,7 +29,7 @@ import org.testng.annotations.Test; /** - * The unit test class for the class {@link ComparableEvaluator}. + * The unit test class for the class {@link ComparableEvaluator} */ public class ComparableEvaluatorTest { diff --git a/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsEvaluatorTest.java b/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsEvaluatorTest.java index 2395603..f18ef94 100644 --- a/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsEvaluatorTest.java +++ b/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsEvaluatorTest.java @@ -28,9 +28,6 @@ import org.meanbean.test.BeanTester; import org.testng.annotations.Test; -import de.alpharogroup.evaluate.object.BaseEnhancedRandomExtensions; -import de.alpharogroup.evaluate.object.Person; - /** * The unit test class for the class {@link EqualsEvaluator}. */ @@ -65,22 +62,6 @@ public void testEvaluateConsistency() expected = false; assertEquals(expected, actual); - actual = EqualsEvaluator.evaluateConsistency(new Person() - { - @Override - public boolean equals(Object o) - { - return BaseEnhancedRandomExtensions.nextObject(boolean.class); - } - - @Override - public int hashCode() - { - return BaseEnhancedRandomExtensions.nextObject(Integer.class); - } - }, Person.builder().build()); - expected = false; - assertEquals(expected, actual); } /** diff --git a/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluatorTest.java b/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluatorTest.java index 83b4913..d237825 100644 --- a/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluatorTest.java +++ b/src/test/java/de/alpharogroup/evaluate/object/evaluators/EqualsHashCodeAndToStringEvaluatorTest.java @@ -166,11 +166,13 @@ public void testEvaluateEqualsHashcodeAndToString() * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader + * @throws NoSuchFieldException + * is thrown if no such field exists */ @Test(enabled = true) public void testEvaluateEqualsHashcodeAndToStringClass() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, - InstantiationException, IOException, ClassNotFoundException + InstantiationException, IOException, ClassNotFoundException, NoSuchFieldException { boolean expected; boolean actual; @@ -207,11 +209,13 @@ public void testEvaluateEqualsHashcodeAndToStringClass() * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * occurs if a given class cannot be located by the specified class loader + * @throws NoSuchFieldException + * is thrown if no such field exists */ @Test(enabled = true) public void testEvaluateEqualsHashcodeAndToStringClassAndFuction() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, - InstantiationException, IOException, ClassNotFoundException + InstantiationException, IOException, ClassNotFoundException, NoSuchFieldException { boolean expected; boolean actual; diff --git a/src/test/java/de/alpharogroup/evaluate/object/verifier/ContractVerifierTest.java b/src/test/java/de/alpharogroup/evaluate/object/verifier/ContractVerifierTest.java index f710224..2f9e4e0 100644 --- a/src/test/java/de/alpharogroup/evaluate/object/verifier/ContractVerifierTest.java +++ b/src/test/java/de/alpharogroup/evaluate/object/verifier/ContractVerifierTest.java @@ -1,27 +1,43 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ package de.alpharogroup.evaluate.object.verifier; -import java.util.function.Function; - import org.testng.annotations.Test; -import de.alpharogroup.evaluate.object.BaseEnhancedRandomExtensions; import de.alpharogroup.test.objects.Member; import de.alpharogroup.test.objects.Person; +/** + * The unit test class for the class {@link ContractVerifier} + */ public class ContractVerifierTest { + /** + * Test method for {@link ContractVerifier#verify()} + */ @Test public void testOfVerify() { - Function, Person> factoryFunction = clazz -> Person.builder() - .name(BaseEnhancedRandomExtensions.nextObject(String.class)) - .about(BaseEnhancedRandomExtensions.nextObject(String.class)) - .married(BaseEnhancedRandomExtensions.nextObject(Boolean.class)) - .nickname(BaseEnhancedRandomExtensions.nextObject(String.class)).build(); - ContractVerifier.of(Person.class) - .withFactoryFunction(factoryFunction) - .verify(); + ContractVerifier.of(Person.class).verify(); ContractVerifier.of(Member.class).verify(); } }