diff --git a/README.md b/README.md index d7f1e4c..0da55d8 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ To install, you can simply include the dependency from Maven Central: com.redfin validity - 3.0.0 + 4.0.0 ``` @@ -39,18 +39,18 @@ Be careful when using primitive boolean validation that they return the given su For example, `validate.that(false).isFalse()` will return `false`, not true. If the validation were to fail then it wouldn't return false, but would rather throw an exception. -For best effect, you should statically import the two static `Validity` method entry points. +For best effect, you should statically import the static `Validity` method entry point. ```java import static com.redfin.validity.Validity.validate; -import static com.redfin.validity.Validity.withMessage; ``` ## Customization The verifiable types are implemented with generics so that if a company or project wants to use the library but have different behavior than the default, they can. -A static class (like the `Validity` class itself) can be created that returns e a `VerifiableFactory` class with different `FailedValidationExecutor` implementations that handle the creation and throwing of Throwable's on failure. Implementations of that interface are where the stack trimming portions of the library are implemented. -The `VerifiableFactory` class can be sub-classed to add new, custom, verifiable types or to customize the entry point for specific throwable types on validation failure. +A static class (like the `Validity` class itself) can be created that returns a sub-class of the `AbstractVerifiableFactory` class with different `FailedValidationExecutor` implementations that handle the creation and throwing of Throwable's on failure. +Implementations of that interface are where the stack trimming portions of the library are implemented. +The `AbstractVerifiableFactory` class can be sub-classed to add new, custom, verifiable types or to customize the entry point for specific throwable types on validation failure. ## Descriptive Predicates @@ -70,7 +70,6 @@ t -> null != t ### example ```java - import static com.redfin.validity.Validity.validate; public final class Foo { @@ -78,7 +77,8 @@ public final class Foo { private final int i; public Foo(int i) { - this.i = validate().that(i).isStrictlyPositive(); + this.i = validate().that(i) + .isStrictlyPositive(); } } @@ -103,3 +103,21 @@ java.lang.IllegalArgumentException: Subject failed validation at java.lang.reflect.Method.invoke(Method.java:498) ... more lines below, truncated for space ``` + +### example 2 (custom message prefix) +You can add in a custom prefix to the exception which would replace the "Subject failed validation" in the stack trace above. +You do this by adding the `withMessage` message to the factory instance you get from Validity before adding the subject that is to be validated. +```java +import static com.redfin.validity.Validity.validate; + +public final class Foo { + + private final int i; + + public Foo(int i) { + this.i = validate().withMessage("A Foo instance needs a positive integer") + .that(i) + .isStrictlyPositive(); + } +} +``` \ No newline at end of file diff --git a/pom.xml b/pom.xml index 718e69c..d22924a 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ com.redfin validity - 3.0.0 + 4.0.0 4.0.0 jar @@ -69,80 +69,10 @@ 1.8 - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - - release - - - - org.apache.maven.plugins - maven-source-plugin - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-gpg-plugin - - - sign-artifacts - verify - - sign - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - true - - ossrh - https://oss.sonatype.org/ - true - - - - - - + - - org.junit.jupiter @@ -152,6 +82,16 @@ + + + + + org.junit.jupiter + junit-jupiter-api + test + + + @@ -293,13 +233,73 @@ - + - - - org.junit.jupiter - junit-jupiter-api - test - - + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + release + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + true + + ossrh + https://oss.sonatype.org/ + true + + + + + + diff --git a/src/main/java/com/redfin/validity/VerifiableFactory.java b/src/main/java/com/redfin/validity/AbstractVerifiableFactory.java similarity index 87% rename from src/main/java/com/redfin/validity/VerifiableFactory.java rename to src/main/java/com/redfin/validity/AbstractVerifiableFactory.java index ff794b4..9cf1649 100644 --- a/src/main/java/com/redfin/validity/VerifiableFactory.java +++ b/src/main/java/com/redfin/validity/AbstractVerifiableFactory.java @@ -56,8 +56,12 @@ * An abstract class that acts as a factory. It holds the information passed in * until a test subject for validation is given. At that point the correct * type of verifiable is returned depending upon the type of the subject. + * + * @param the type to be thrown from failed validation. + * @param the type of the implementing sub-class. */ -public abstract class VerifiableFactory { +public abstract class AbstractVerifiableFactory> { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Fields @@ -71,7 +75,7 @@ public abstract class VerifiableFactory { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** - * Create a new {@link VerifiableFactory} instance with the given message and failed validation + * Create a new {@link AbstractVerifiableFactory} instance with the given message and failed validation * executor. * * @param message the String message to pre-pend the failure message with, if necessary. @@ -79,9 +83,10 @@ public abstract class VerifiableFactory { * @param failedValidationExecutor the {@link FailedValidationExecutor} to use in case * of failed validation. * May not be null. + * * @throws NullPointerException if failedValidationExecutor is null. */ - public VerifiableFactory(String message, FailedValidationExecutor failedValidationExecutor) { + public AbstractVerifiableFactory(String message, FailedValidationExecutor failedValidationExecutor) { this.message = message; if (null == failedValidationExecutor) { throw new NullPointerException(ValidityUtils.nullArgumentMessage("failedValidationExecutor")); @@ -89,6 +94,33 @@ public VerifiableFactory(String message, FailedValidationExecutor failedValid this.failedValidationExecutor = failedValidationExecutor; } + /** + * @param message the String message prefix for the verifiable factory that is + * to be returned. + * + * @return a new instance of the verifiable factory with the given message and the + * current failed validation executor. + */ + public F withMessage(String message) { + return getFactory(message, failedValidationExecutor); + } + + /** + * Get an instance of the verifiable factory with the given message + * and failed validation executor. It is not required that it be a + * new instance, but it must have the given message and validation executor. + * + * @param message the String message for the verifiable factory. + * @param failedValidationExecutor the failed validation executor for the factory. + * May not be null. + * @return an instance of the implementing subclass verifiable factory with + * the given parameters. + * + * @throws IllegalArgumentException if failedValidationExecutor is null. + */ + protected abstract F getFactory(String message, + FailedValidationExecutor failedValidationExecutor); + /** * @return the message for this verifiable factory. */ @@ -118,7 +150,8 @@ protected FailedValidationExecutor getFailedValidationExecutor() { /** * @param subject the object to perform validation on. - * @param the type of the elements in the array. + * @param the type of the elements in the array. + * * @return a {@link VerifiableArray} instance for the given subject. */ public VerifiableArray that(E[] subject) { @@ -127,6 +160,7 @@ public VerifiableArray that(E[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableBooleanArray} instance for the given subject. */ public VerifiableBooleanArray that(boolean[] subject) { @@ -135,6 +169,7 @@ public VerifiableBooleanArray that(boolean[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableByteArray} instance for the given subject. */ public VerifiableByteArray that(byte[] subject) { @@ -143,6 +178,7 @@ public VerifiableByteArray that(byte[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableCharArray} instance for the given subject. */ public VerifiableCharArray that(char[] subject) { @@ -151,6 +187,7 @@ public VerifiableCharArray that(char[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableDoubleArray} instance for the given subject. */ public VerifiableDoubleArray that(double[] subject) { @@ -159,6 +196,7 @@ public VerifiableDoubleArray that(double[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableFloatArray} instance for the given subject. */ public VerifiableFloatArray that(float[] subject) { @@ -167,6 +205,7 @@ public VerifiableFloatArray that(float[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableIntArray} instance for the given subject. */ public VerifiableIntArray that(int[] subject) { @@ -175,6 +214,7 @@ public VerifiableIntArray that(int[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableLongArray} instance for the given subject. */ public VerifiableLongArray that(long[] subject) { @@ -183,6 +223,7 @@ public VerifiableLongArray that(long[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableShortArray} instance for the given subject. */ public VerifiableShortArray that(short[] subject) { @@ -195,6 +236,7 @@ public VerifiableShortArray that(short[] subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveBoolean} instance for the given subject. */ public VerifiablePrimitiveBoolean that(boolean subject) { @@ -203,6 +245,7 @@ public VerifiablePrimitiveBoolean that(boolean subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveByte} instance for the given subject. */ public VerifiablePrimitiveByte that(byte subject) { @@ -211,6 +254,7 @@ public VerifiablePrimitiveByte that(byte subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveChar} instance for the given subject. */ public VerifiablePrimitiveChar that(char subject) { @@ -219,6 +263,7 @@ public VerifiablePrimitiveChar that(char subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveDouble} instance for the given subject. */ public VerifiablePrimitiveDouble that(double subject) { @@ -227,6 +272,7 @@ public VerifiablePrimitiveDouble that(double subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveFloat} instance for the given subject. */ public VerifiablePrimitiveFloat that(float subject) { @@ -235,6 +281,7 @@ public VerifiablePrimitiveFloat that(float subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveInt} instance for the given subject. */ public VerifiablePrimitiveInt that(int subject) { @@ -243,6 +290,7 @@ public VerifiablePrimitiveInt that(int subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveLong} instance for the given subject. */ public VerifiablePrimitiveLong that(long subject) { @@ -251,6 +299,7 @@ public VerifiablePrimitiveLong that(long subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiablePrimitiveShort} instance for the given subject. */ public VerifiablePrimitiveShort that(short subject) { @@ -267,6 +316,7 @@ public VerifiablePrimitiveShort that(short subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableBoolean} instance for the given subject. */ public VerifiableBoolean that(Boolean subject) { @@ -275,6 +325,7 @@ public VerifiableBoolean that(Boolean subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableByte} instance for the given subject. */ public VerifiableByte that(Byte subject) { @@ -283,6 +334,7 @@ public VerifiableByte that(Byte subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableCharacter} instance for the given subject. */ public VerifiableCharacter that(Character subject) { @@ -291,6 +343,7 @@ public VerifiableCharacter that(Character subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableDouble} instance for the given subject. */ public VerifiableDouble that(Double subject) { @@ -299,6 +352,7 @@ public VerifiableDouble that(Double subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableFloat} instance for the given subject. */ public VerifiableFloat that(Float subject) { @@ -307,6 +361,7 @@ public VerifiableFloat that(Float subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableInteger} instance for the given subject. */ public VerifiableInteger that(Integer subject) { @@ -315,6 +370,7 @@ public VerifiableInteger that(Integer subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableLong} instance for the given subject. */ public VerifiableLong that(Long subject) { @@ -323,6 +379,7 @@ public VerifiableLong that(Long subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableShort} instance for the given subject. */ public VerifiableShort that(Short subject) { @@ -335,6 +392,7 @@ public VerifiableShort that(Short subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableDuration} instance for the given subject. */ public VerifiableDuration that(Duration subject) { @@ -343,6 +401,7 @@ public VerifiableDuration that(Duration subject) { /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableInstant} instance for the given subject. */ public VerifiableInstant that(Instant subject) { @@ -355,7 +414,8 @@ public VerifiableInstant that(Instant subject) { /** * @param subject the object to perform validation on. - * @param the class being validated. + * @param the class being validated. + * * @return a {@link VerifiableClass} instance for the given subject. */ public VerifiableClass that(Class subject) { @@ -364,8 +424,9 @@ public VerifiableClass that(Class subject) { /** * @param subject the object to perform validation on. - * @param the type of the objects in the collection. - * @param the type of the Collection (e.g. list, map, etc). + * @param the type of the objects in the collection. + * @param the type of the Collection (e.g. list, map, etc). + * * @return a {@link VerifiableCollection} instance for the given subject. */ public > VerifiableCollection that(T subject) { @@ -374,6 +435,7 @@ public > VerifiableCollection that(T subject /** * @param subject the object to perform validation on. + * * @return a {@link VerifiableString} instance for the given subject. */ public VerifiableString that(String subject) { @@ -386,6 +448,7 @@ public VerifiableString that(String subject) { * * @param subject the object to perform validation on. * @param the type of the subject. + * * @return a {@link VerifiableObject} instance for the given subject. */ public VerifiableObject that(T subject) { @@ -398,7 +461,7 @@ public VerifiableObject that(T subject) { /** * @throws UnsupportedOperationException always. - * @deprecated VerifiableFactory objects cannot be tested for equality. + * @deprecated AbstractVerifiableFactory objects cannot be tested for equality. */ @Deprecated @Override @@ -408,7 +471,7 @@ public final boolean equals(Object obj) { /** * @throws UnsupportedOperationException always. - * @deprecated VerifiableFactory objects cannot be hashed. + * @deprecated AbstractVerifiableFactory objects cannot be hashed. */ @Deprecated @Override diff --git a/src/main/java/com/redfin/validity/Validity.java b/src/main/java/com/redfin/validity/Validity.java index 0154d8f..700ded3 100644 --- a/src/main/java/com/redfin/validity/Validity.java +++ b/src/main/java/com/redfin/validity/Validity.java @@ -17,11 +17,9 @@ package com.redfin.validity; /** - * The entry point for the Validity library. This is a specific sub-class - * of the {@link VerifiableFactory} that throws {@link IllegalArgumentException}s on - * validation failure. + * The entry point for the Validity library. */ -public final class Validity extends VerifiableFactory { +public final class Validity { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Constants @@ -34,15 +32,12 @@ public final class Validity extends VerifiableFactory * message. If a message is, given, though, a new instance is required. */ - private static final FailedValidationExecutor VERIFY_FAILURE = new DefaultValidityFailedValidationExecutor<>(IllegalArgumentException::new); - private static final Validity NO_MESSAGE_INSTANCE = new Validity(null); + private static final FailedValidationExecutor VERIFY_FAILURE; + private static final ValidityVerifiableFactory NO_MESSAGE_INSTANCE; - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // Instance Methods - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - private Validity(String message) { - super(message, VERIFY_FAILURE); + static { + VERIFY_FAILURE = new DefaultValidityFailedValidationExecutor<>(IllegalArgumentException::new); + NO_MESSAGE_INSTANCE = new ValidityVerifiableFactory(null, VERIFY_FAILURE); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -53,43 +48,19 @@ private Validity(String message) { * @return a {@link Validity} instance with the default message * prefix. */ - public static Validity validate() { + public static ValidityVerifiableFactory validate() { return NO_MESSAGE_INSTANCE; } - /** - * @param message the String message to use as a prefix for the validation. - * May be null. - * - * @return a {@link ValidityBuilder} instance with the given message prefix. - */ - public static ValidityBuilder withMessage(String message) { - return new ValidityBuilder(message); - } + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Instance Methods + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - /** - * Helper class to store the given message used when generating the - * {@link Validity} instance. + /* + * Make sure you cannot instantiate the static Validity class even with reflection. */ - public static final class ValidityBuilder { - - private final String message; - - /** - * Create a new {@link ValidityBuilder} instance with the given string message. - * - * @param message the String message prefix for the build Validity instance. - */ - public ValidityBuilder(String message) { - this.message = message; - } - /** - * @return a {@link Validity} instance with the message given when this builder - * was created. - */ - public Validity validate() { - return new Validity(message); - } + private Validity() { + throw new AssertionError(ValidityUtils.nonInstantiableMessage()); } } diff --git a/src/main/java/com/redfin/validity/ValidityVerifiableFactory.java b/src/main/java/com/redfin/validity/ValidityVerifiableFactory.java new file mode 100644 index 0000000..c97d149 --- /dev/null +++ b/src/main/java/com/redfin/validity/ValidityVerifiableFactory.java @@ -0,0 +1,48 @@ +/* + * Copyright: (c) 2016 Redfin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.redfin.validity; + +/** + * The default implementation of the {@link AbstractVerifiableFactory} for the Validity library. + */ +public final class ValidityVerifiableFactory + extends AbstractVerifiableFactory { + + /** + * Create a new {@link AbstractVerifiableFactory} instance with the given message and failed validation + * executor. + * + * @param message the String message to pre-pend the failure message with, if necessary. + * May be null. + * @param failedValidationExecutor the {@link FailedValidationExecutor} to use in case + * of failed validation. + * May not be null. + * + * @throws NullPointerException if failedValidationExecutor is null. + */ + public ValidityVerifiableFactory(String message, + FailedValidationExecutor failedValidationExecutor) { + super(message, failedValidationExecutor); + } + + @Override + protected ValidityVerifiableFactory getFactory(String message, + FailedValidationExecutor failedValidationExecutor) { + return new ValidityVerifiableFactory(message, + failedValidationExecutor); + } +} diff --git a/src/test/java/com/redfin/validity/VerifiableFactoryContract.java b/src/test/java/com/redfin/validity/AbstractVerifiableFactoryContract.java similarity index 66% rename from src/test/java/com/redfin/validity/VerifiableFactoryContract.java rename to src/test/java/com/redfin/validity/AbstractVerifiableFactoryContract.java index 0994adc..18e16fd 100644 --- a/src/test/java/com/redfin/validity/VerifiableFactoryContract.java +++ b/src/test/java/com/redfin/validity/AbstractVerifiableFactoryContract.java @@ -55,12 +55,37 @@ import java.util.Collection; @SuppressWarnings("ConstantConditions") -interface VerifiableFactoryContract extends NotValueTypeContract> { +interface AbstractVerifiableFactoryContract> + extends NotValueTypeContract> { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test cases // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + @Test + default void testWithMessageReturnsDifferentInstance() { + String message = "hello"; + Assertions.assertTrue(getNotValueTypeInstance().withMessage(message) != getNotValueTypeInstance().withMessage(message), + "AbstractVerifiableFactory should return a new instance for withMessage(String)"); + } + + @Test + default void testWithMessageFactoryReturnsGivenMessage() { + String message = "world"; + Assertions.assertEquals(message, + getNotValueTypeInstance().withMessage(message).getMessage(), + "AbstractVerifiableFactory should return given message."); + } + + @Test + default void testAbstractVerifiableFactoryWithMessageReturnsFactoryWithGivenExecutor() { + AbstractVerifiableFactory factory = getNotValueTypeInstance(); + FailedValidationExecutor executor = factory.getFailedValidationExecutor(); + Assertions.assertTrue(executor == factory.withMessage("hello").getFailedValidationExecutor(), + "AbstractVerifiableFactory withMessage(String) should return factory with same validation executor."); + } + // -------------------------------------------------------------- // Factory method tests // -------------------------------------------------------------- @@ -68,7 +93,7 @@ interface VerifiableFactoryContract extends NotValueTypeCon /* * The following tests have some redundancy built-in to them. That is on purpose * to quickly catch any ambiguity issues with the method overloading done in the - * VerifiableFactory class. + * AbstractVerifiableFactory class. */ // - - - - - - - - - - - - - - - - - - - - - - @@ -79,126 +104,126 @@ interface VerifiableFactoryContract extends NotValueTypeCon default void testReturnsExpectedVerifiableArray() { Integer[] subject = new Integer[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableArrayForNullSubject() { Integer[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableBooleanArray() { boolean[] subject = new boolean[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableBooleanArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableBooleanArrayForNullSubject() { boolean[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableBooleanArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableByteArray() { byte[] subject = new byte[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableByteArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableByteArrayForNullSubject() { byte[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableByteArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableCharArray() { char[] subject = new char[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableCharArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableCharArrayForNullSubject() { char[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableCharArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableDoubleArray() { double[] subject = new double[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableDoubleArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableDoubleArrayForNullSubject() { double[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableDoubleArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableFloatArray() { float[] subject = new float[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableFloatArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableFloatArrayForNullSubject() { float[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableFloatArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableIntArray() { int[] subject = new int[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableIntArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableIntArrayForNullSubject() { int[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableIntArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableLongArray() { long[] subject = new long[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableLongArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableLongArrayForNullSubject() { long[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableLongArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableShortArray() { short[] subject = new short[0]; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableShortArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableShortArrayForNullSubject() { short[] subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableShortArray, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } // - - - - - - - - - - - - - - - - - - - - - - @@ -209,56 +234,56 @@ default void testReturnsExpectedVerifiableShortArrayForNullSubject() { default void testReturnsExpectedVerifiablePrimitiveBoolean() { boolean subject = true; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveBoolean, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiablePrimitiveByte() { byte subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveByte, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiablePrimitiveChar() { char subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveChar, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiablePrimitiveDouble() { double subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveDouble, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiablePrimitiveFloat() { float subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveFloat, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiablePrimitiveInt() { int subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveInt, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiablePrimitiveLong() { long subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveLong, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiablePrimitiveShort() { short subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiablePrimitiveShort, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } // - - - - - - - - - - - - - - - - - - - - - - @@ -269,188 +294,188 @@ default void testReturnsExpectedVerifiablePrimitiveShort() { default void testReturnsExpectedVerifiableBoolean() { Boolean subject = true; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableBoolean, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableBooleanForNullSubject() { Boolean subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableBoolean, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableByte() { Byte subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableByte, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableByteForNullSubject() { Byte subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableByte, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableCharacter() { Character subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableCharacter, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableCharacterForNullSubject() { Character subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableCharacter, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableClass() { - Class subject = VerifiableFactoryContract.class; + Class subject = AbstractVerifiableFactoryContract.class; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableClass, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableClassForNullSubject() { Class subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableClass, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableCollections() { Collection subject = new ArrayList<>(); Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableCollection, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableCollectionsForNullSubject() { Collection subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableCollection, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableDouble() { Double subject = 0D; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableDouble, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableDoubleForNullSubject() { Double subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableDouble, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableFloat() { Float subject = 0F; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableFloat, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableInteger() { Integer subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableInteger, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableIntegerForNullSubject() { Integer subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableInteger, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableLong() { Long subject = 0L; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableLong, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableLongForNullSubject() { Long subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableLong, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableObject() { Object subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableObject, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableObjectForNullSubject() { Object subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableObject, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableShort() { Short subject = 0; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableShort, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableShortForNullSubject() { Short subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableShort, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableDuration() { Duration subject = Duration.ZERO; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableDuration, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableDurationForNullSubject() { Duration subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableDuration, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableInstant() { Instant subject = Instant.now(); Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableInstant, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableInstantForNullSubject() { Instant subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableInstant, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableString() { String subject = ""; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableString, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } @Test default void testReturnsExpectedVerifiableStringForNullSubject() { String subject = null; Assertions.assertTrue(getNotValueTypeInstance().that(subject) instanceof VerifiableString, - "VerifiableFactory should have returned a non-null object of the expected type."); + "AbstractVerifiableFactory should have returned a non-null object of the expected type."); } } diff --git a/src/test/java/com/redfin/validity/VerifiableFactoryTest.java b/src/test/java/com/redfin/validity/AbstractVerifiableFactoryTest.java similarity index 67% rename from src/test/java/com/redfin/validity/VerifiableFactoryTest.java rename to src/test/java/com/redfin/validity/AbstractVerifiableFactoryTest.java index d2e4630..ea1f189 100644 --- a/src/test/java/com/redfin/validity/VerifiableFactoryTest.java +++ b/src/test/java/com/redfin/validity/AbstractVerifiableFactoryTest.java @@ -19,7 +19,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -final class VerifiableFactoryTest { +final class AbstractVerifiableFactoryTest { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test requirements @@ -33,10 +33,19 @@ public void fail(String expected, T subject, String message) throws IllegalA } }; - private static final class TestVerifiableFactory extends VerifiableFactory { - private TestVerifiableFactory(String message, FailedValidationExecutor failedValidationExecutor) { + private static final class TestAbstractVerifiableFactory + extends AbstractVerifiableFactory { + + private TestAbstractVerifiableFactory(String message, + FailedValidationExecutor failedValidationExecutor) { super(message, failedValidationExecutor); } + + @Override + protected TestAbstractVerifiableFactory getFactory(String message, + FailedValidationExecutor failedValidationExecutor) { + return new TestAbstractVerifiableFactory(message, failedValidationExecutor); + } } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -46,39 +55,39 @@ private TestVerifiableFactory(String message, FailedValidationExecutor new TestVerifiableFactory(VALID_MESSAGE, null)); + () -> new TestAbstractVerifiableFactory(VALID_MESSAGE, null)); Assertions.assertEquals(ValidityUtils.nullArgumentMessage("failedValidationExecutor"), exception.getMessage(), - "Should not be able to instantiate a VerifiableFactory with a null failed validation executor."); + "Should not be able to instantiate a AbstractVerifiableFactory with a null failed validation executor."); } @Test void testReturnsGivenMessage() { - Assertions.assertTrue( VALID_MESSAGE.equals(new TestVerifiableFactory(VALID_MESSAGE, VALIDATION_EXECUTOR).getMessage()), + Assertions.assertTrue( VALID_MESSAGE.equals(new TestAbstractVerifiableFactory(VALID_MESSAGE, VALIDATION_EXECUTOR).getMessage()), "A verifiable factory should return the same string message instance it is given"); } @Test void testReturnsGivenExecutor() { - Assertions.assertTrue( VALIDATION_EXECUTOR.equals(new TestVerifiableFactory(VALID_MESSAGE, VALIDATION_EXECUTOR).getFailedValidationExecutor()), + Assertions.assertTrue( VALIDATION_EXECUTOR.equals(new TestAbstractVerifiableFactory(VALID_MESSAGE, VALIDATION_EXECUTOR).getFailedValidationExecutor()), "A verifiable factory should return the same FailedValidationExecutor instance it is given"); } } diff --git a/src/test/java/com/redfin/validity/ValidityTest.java b/src/test/java/com/redfin/validity/ValidityTest.java index 72e8064..d8e80a7 100644 --- a/src/test/java/com/redfin/validity/ValidityTest.java +++ b/src/test/java/com/redfin/validity/ValidityTest.java @@ -19,15 +19,15 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -final class ValidityTest implements VerifiableFactoryContract { +final class ValidityTest implements NonInstantiableContract { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Test values & contract implementations // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @Override - public Validity getNotValueTypeInstance() { - return Validity.validate(); + public Class getNonInstantiableClassObject() { + return Validity.class; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -41,33 +41,14 @@ void testRequireReturnsNonNull() { } @Test - void testRequireWithMessageReturnsNonNull() { - Assertions.assertNotNull(Validity.withMessage("hello").validate(), - "Validity validate with message should return a non null object."); - } - - @Test - void testRequireWithNullMessageReturnsNonNullValidityBuilder() { - Assertions.assertNotNull(Validity.withMessage(null), - "Validity with null message should return a non null object."); - } - - @Test - void testRequireWithNullMessageReturnsNonNull() { - Assertions.assertNotNull(Validity.withMessage(null).validate(), - "Validity validate with null message should return a non null object."); - } - - @Test - void testRepeatedAssertsReturnsSameInstance() { + void testRepeatedCallsToValidateReturnTheSameInstance() { Assertions.assertTrue(Validity.validate() == Validity.validate(), "Repeated calls to validate should return the same instance."); } @Test - void testRepeatedAssertsWithMessagesReturnDifferentInstances() { - String message = "hello"; - Assertions.assertFalse(Validity.withMessage(message).validate() == Validity.withMessage(message).validate(), - "Repeated calls to validate with message should return different instances."); + void testValidateReturnsFactoryWithNullMessage() { + Assertions.assertNull(Validity.validate().getMessage(), + "Validity validate should return a factory with a null message."); } } diff --git a/src/test/java/com/redfin/validity/ValidityVerifiableFactoryTest.java b/src/test/java/com/redfin/validity/ValidityVerifiableFactoryTest.java new file mode 100644 index 0000000..e849ad4 --- /dev/null +++ b/src/test/java/com/redfin/validity/ValidityVerifiableFactoryTest.java @@ -0,0 +1,110 @@ +/* + * Copyright: (c) 2016 Redfin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.redfin.validity; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +final class ValidityVerifiableFactoryTest + implements AbstractVerifiableFactoryContract { + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test requirements + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + private static final String VALID_MESSAGE = "hello"; + + private static final FailedValidationExecutor VALIDATION_EXECUTOR = new FailedValidationExecutor() { + @Override + public void fail(String expected, T subject, String message) throws IllegalArgumentException { + throw new IllegalArgumentException(expected + subject + message); + } + }; + + private ValidityVerifiableFactory getInstance() { + return new ValidityVerifiableFactory(VALID_MESSAGE, VALIDATION_EXECUTOR); + } + + @Override + public AbstractVerifiableFactory getNotValueTypeInstance() { + return getInstance(); + } + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Test cases + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + @Test + void testCanInstantiateWithValidArguments() { + try { + getInstance(); + } catch (Throwable thrown) { + throw new RuntimeException("Should be able to instantiate a ValidityVerifiableFactory with valid arguments", thrown); + } + } + + @Test + void testCanInstantiateWithNullMessage() { + try { + new ValidityVerifiableFactory(null, VALIDATION_EXECUTOR); + } catch (Throwable thrown) { + throw new RuntimeException("Should be able to instantiate a ValidityVerifiableFactory with a null message", thrown); + } + } + + @Test + void testConstructorThrowsExceptionForNullValidationExecutor() { + NullPointerException exception = Assertions.assertThrows(NullPointerException.class, + () -> new ValidityVerifiableFactory(VALID_MESSAGE, null)); + Assertions.assertEquals(ValidityUtils.nullArgumentMessage("failedValidationExecutor"), + exception.getMessage(), + "Should not be able to instantiate a ValidityVerifiableFactory with a null failed validation executor."); + } + + @Test + void testReturnsGivenMessage() { + Assertions.assertTrue( VALID_MESSAGE.equals(getInstance().getMessage()), + "A verifiable factory should return the same string message instance it is given"); + } + + @Test + void testReturnsGivenExecutor() { + Assertions.assertTrue( VALIDATION_EXECUTOR.equals(getInstance().getFailedValidationExecutor()), + "A verifiable factory should return the same FailedValidationExecutor instance it is given"); + } + + @Test + void testGetFactoryReturnsNonNUllInstance() { + Assertions.assertNotNull(getInstance().getFactory(VALID_MESSAGE, VALIDATION_EXECUTOR), + "The getFactory method of the verifiable factory should return a non-null instance."); + } + + @Test + void testGetFactoryReturnsGivenMessage() { + Assertions.assertTrue( VALID_MESSAGE.equals(getInstance().getFactory(VALID_MESSAGE, VALIDATION_EXECUTOR) + .getMessage()), + "A verifiable factory should return the same string message instance it is given"); + } + + @Test + void testGetFactoryReturnsGivenExecutor() { + Assertions.assertTrue( VALIDATION_EXECUTOR.equals(getInstance().getFactory(VALID_MESSAGE, VALIDATION_EXECUTOR) + .getFailedValidationExecutor()), + "A verifiable factory should return the same FailedValidationExecutor instance it is given"); + } +}