![JAR search and dependency download from the Maven repository](/logo.png)
fr.landel.utils.assertor.utils.AssertorThrowable Maven / Gradle / Ivy
/*-
* #%L
* utils-assertor
* %%
* Copyright (C) 2016 - 2018 Gilles Landel
* %%
* 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.
* #L%
*/
package fr.landel.utils.assertor.utils;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import fr.landel.utils.assertor.StepAssertor;
import fr.landel.utils.assertor.commons.ConstantsAssertor;
import fr.landel.utils.assertor.commons.MessageAssertor;
import fr.landel.utils.assertor.commons.ParameterAssertor;
import fr.landel.utils.assertor.enums.EnumType;
/**
* Utility class to prepare the check of {@link Throwable}
*
* @since Mar 21, 2017
* @author Gilles
*
*/
public class AssertorThrowable extends ConstantsAssertor {
/**
* Prepare the next step to validate if the {@link Throwable} is an instance
* of {@code type} and has the specified message
*
*
* precondition: throwable and type cannot be null
*
*
* @param step
* the current step
* @param type
* the exception type
* @param exceptionMessage
* the exception message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor isInstanceOf(final StepAssertor step, final Class> type,
final CharSequence exceptionMessage, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && type != null;
final BiPredicate checker = (throwable, not) -> type.isInstance(throwable)
&& Objects.equals(exceptionMessage, throwable.getMessage());
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.INSTANCE, false,
new ParameterAssertor<>(type, EnumType.CLASS), new ParameterAssertor<>(exceptionMessage, EnumType.CHAR_SEQUENCE));
}
/**
* Prepare the next step to validate if the {@link Throwable} is an instance
* of {@code type} and matches the specified pattern
*
*
* precondition: throwable, type and pattern cannot be null
*
*
* @param step
* the current step
* @param type
* the exception type
* @param pattern
* the pattern to check the message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor isInstanceOf(final StepAssertor step, final Class> type, final Pattern pattern,
final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && type != null && pattern != null;
final BiPredicate checker = (throwable, not) -> type.isInstance(throwable) && throwable.getMessage() != null
&& pattern.matcher(throwable.getMessage()).matches();
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.INSTANCE_PATTERN, false,
new ParameterAssertor<>(type, EnumType.CLASS), new ParameterAssertor<>(pattern, EnumType.UNKNOWN));
}
/**
* Prepare the next step to validate if the {@link Throwable} is assignable
* from the {@code type} and has the specified message
*
*
* precondition: throwable and type cannot be null
*
*
* @param step
* the current step
* @param type
* the exception type
* @param exceptionMessage
* the exception message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor isAssignableFrom(final StepAssertor step, final Class> type,
final CharSequence exceptionMessage, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && type != null;
final BiPredicate checker = (throwable, not) -> type.isAssignableFrom(throwable.getClass())
&& Objects.equals(exceptionMessage, throwable.getMessage());
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.ASSIGNABLE, false,
new ParameterAssertor<>(type, EnumType.CLASS), new ParameterAssertor<>(exceptionMessage, EnumType.CHAR_SEQUENCE));
}
/**
* Prepare the next step to validate if the {@link Throwable} is assignable
* from the {@code type} and matches the specified pattern
*
*
* precondition: throwable, type and pattern cannot be null
*
*
* @param step
* the current step
* @param type
* the exception type
* @param pattern
* the pattern to check the message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor isAssignableFrom(final StepAssertor step, final Class> type,
final Pattern pattern, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && type != null && pattern != null;
final BiPredicate checker = (throwable, not) -> type.isAssignableFrom(throwable.getClass())
&& throwable.getMessage() != null && pattern.matcher(throwable.getMessage()).matches();
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.ASSIGNABLE_PATTERN, false,
new ParameterAssertor<>(type, EnumType.CLASS), new ParameterAssertor<>(pattern, EnumType.UNKNOWN));
}
/**
* Prepare the next step to validate if the {@link Throwable} has no cause
*
*
* precondition: throwable cannot be null
*
*
* @param step
* the current step
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseNull(final StepAssertor step, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null;
final BiPredicate checker = (throwable, not) -> throwable.getCause() == null;
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE, false);
}
/**
* Prepare the next step to validate if the {@link Throwable} has a cause
*
*
* precondition: throwable cannot be null
*
*
* @param step
* the current step
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseNotNull(final StepAssertor step, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null;
final BiPredicate checker = (throwable, not) -> throwable.getCause() != null;
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE, true);
}
/**
* Prepare the next step to validate if the {@link Throwable} has cause with
* instance of {@code causeType}
*
*
* precondition: throwable and causeType cannot be null
*
*
* @param step
* the current step
* @param causeType
* the exception type of cause
* @param resursively
* if true, sub causes are checked until a cause has the type and
* the specified message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseInstanceOf(final StepAssertor step, final Class> causeType,
final boolean resursively, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && causeType != null;
final BiPredicate checker = check(resursively, t -> causeType.isInstance(t));
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE_INSTANCE, false,
new ParameterAssertor<>(causeType, EnumType.CLASS), new ParameterAssertor<>(resursively, EnumType.BOOLEAN));
}
/**
* Prepare the next step to validate if the {@link Throwable} has cause with
* instance of {@code causeType} and has the specified message
*
*
* precondition: throwable and causeType cannot be null
*
*
* @param step
* the current step
* @param causeType
* the exception type of cause
* @param exceptionMessage
* the exception message
* @param resursively
* if true, sub causes are checked until a cause has the type and
* the specified message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseInstanceOf(final StepAssertor step, final Class> causeType,
final CharSequence exceptionMessage, final boolean resursively, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && causeType != null;
final BiPredicate checker = check(resursively,
t -> causeType.isInstance(t) && Objects.equals(exceptionMessage, t.getMessage()));
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE_INSTANCE_MESSAGE, false,
new ParameterAssertor<>(causeType, EnumType.CLASS), new ParameterAssertor<>(exceptionMessage, EnumType.CHAR_SEQUENCE),
new ParameterAssertor<>(resursively, EnumType.BOOLEAN));
}
/**
* Prepare the next step to validate if the {@link Throwable} has cause with
* instance of {@code causeType} and matches the specified pattern
*
*
* precondition: throwable, causeType and pattern cannot be null
*
*
* @param step
* the current step
* @param causeType
* the exception type of cause
* @param pattern
* the pattern to check the message
* @param resursively
* if true, sub causes are checked until a cause has the type and
* matches the pattern
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseInstanceOf(final StepAssertor step, final Class> causeType,
final Pattern pattern, final boolean resursively, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && causeType != null && pattern != null;
final BiPredicate checker = check(resursively, throwable -> causeType.isInstance(throwable)
&& throwable.getMessage() != null && pattern.matcher(throwable.getMessage()).matches());
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE_INSTANCE_PATTERN, false,
new ParameterAssertor<>(causeType, EnumType.CLASS), new ParameterAssertor<>(pattern, EnumType.UNKNOWN),
new ParameterAssertor<>(resursively, EnumType.BOOLEAN));
}
/**
* Prepare the next step to validate if the {@link Throwable} has cause
* assignable from {@code causeType}
*
*
* precondition: throwable and causeType cannot be null
*
*
* @param step
* the current step
* @param causeType
* the exception type of cause
* @param resursively
* if true, sub causes are checked until a cause has the type and
* the specified message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseAssignableFrom(final StepAssertor step, final Class> causeType,
final boolean resursively, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && causeType != null;
final BiPredicate checker = check(resursively, throwable -> {
return causeType.isAssignableFrom(throwable.getClass());
});
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE_ASSIGNABLE, false,
new ParameterAssertor<>(causeType, EnumType.CLASS), new ParameterAssertor<>(resursively, EnumType.BOOLEAN));
}
/**
* Prepare the next step to validate if the {@link Throwable} has cause
* assignable from {@code causeType} and has the specified message
*
*
* precondition: throwable and causeType cannot be null
*
*
* @param step
* the current step
* @param causeType
* the exception type of cause
* @param exceptionMessage
* the exception message
* @param resursively
* if true, sub causes are checked until a cause has the type and
* the specified message
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseAssignableFrom(final StepAssertor step, final Class> causeType,
final CharSequence exceptionMessage, final boolean resursively, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && causeType != null;
final BiPredicate checker = check(resursively,
throwable -> causeType.isAssignableFrom(throwable.getClass()) && Objects.equals(exceptionMessage, throwable.getMessage()));
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE_ASSIGNABLE_MESSAGE, false,
new ParameterAssertor<>(causeType, EnumType.CLASS), new ParameterAssertor<>(exceptionMessage, EnumType.CHAR_SEQUENCE),
new ParameterAssertor<>(resursively, EnumType.BOOLEAN));
}
/**
* Prepare the next step to validate if the {@link Throwable} has cause
* assignable from {@code causeType} and matches the specified pattern
*
*
* precondition: throwable, causeType and pattern cannot be null
*
*
* @param step
* the current step
* @param causeType
* the exception type of cause
* @param pattern
* the pattern to check the message
* @param resursively
* if true, sub causes are checked until a cause has the type and
* matches the pattern
* @param message
* the message if invalid
* @param
* the class type
* @return the next step
*/
public static StepAssertor hasCauseAssignableFrom(final StepAssertor step, final Class> causeType,
final Pattern pattern, final boolean resursively, final MessageAssertor message) {
final Predicate preChecker = (throwable) -> throwable != null && causeType != null && pattern != null;
final BiPredicate checker = check(resursively, throwable -> causeType.isAssignableFrom(throwable.getClass())
&& throwable.getMessage() != null && pattern.matcher(throwable.getMessage()).matches());
return new StepAssertor<>(step, preChecker, checker, false, message, MSG.THROWABLE.CAUSE_ASSIGNABLE_PATTERN, false,
new ParameterAssertor<>(causeType, EnumType.CLASS), new ParameterAssertor<>(pattern, EnumType.UNKNOWN),
new ParameterAssertor<>(resursively, EnumType.BOOLEAN));
}
private static BiPredicate check(final boolean resursively, final Predicate predicate) {
final BiPredicate checker;
if (resursively) {
checker = (throwable, not) -> {
Throwable t = throwable;
while ((t = t.getCause()) != null) {
if (predicate.test(t)) {
return true;
}
}
return false;
};
} else {
checker = (throwable, not) -> predicate.test(throwable.getCause());
}
return checker;
}
}