All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.assertions.AssertionPredicates Maven / Gradle / Ivy

The newest version!
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you 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 org.apache.juneau.assertions;

import static org.apache.juneau.assertions.AssertionPredicate.*;
import static org.apache.juneau.common.internal.ArgUtils.*;
import static org.apache.juneau.common.internal.StringUtils.*;

import java.text.*;
import java.util.*;
import java.util.function.*;
import java.util.regex.*;

import org.apache.juneau.common.internal.*;
import org.apache.juneau.cp.*;

/**
 * Predefined {@link AssertionPredicate} objects.
 *
 * 

* Typically used wherever predicates are allowed for testing of {@link Assertion} objects such as... *

    *
  • {@link FluentObjectAssertion#is(Predicate)} *
  • {@link FluentArrayAssertion#is(Predicate...)} *
  • {@link FluentPrimitiveArrayAssertion#is(Predicate...)} *
  • {@link FluentListAssertion#isEach(Predicate...)} *
* *
Example:
*

* // Asserts that a list contains te specified values. * List<Object> myList = AList.of(...); * assertList(myList) * .is(eq("foo"), any(), match("bar*")); *

* *
See Also:
* */ public class AssertionPredicates { private static Function TYPENAME = x -> x == null ? null : x.getClass().getName(); private static final Messages MESSAGES = Messages.of(AssertionPredicates.class, "Messages"); private static final String MSG_valueWasNull = MESSAGES.getString("valueWasNull"), MSG_valueWasNotNull = MESSAGES.getString("valueWasNotNull"), MSG_valueDidNotMatchExpected = MESSAGES.getString("valueDidNotMatchExpected"), MSG_valueDidNotContainExpected = MESSAGES.getString("valueDidNotContainExpected"), MSG_valueUnexpectedlyMatched = MESSAGES.getString("valueUnexpectedlyMatched"), MSG_valueWasNotExpectedType = MESSAGES.getString("valueWasNotExpectedType"), MSG_valueDidNotMatchPattern = MESSAGES.getString("valueDidNotMatchPattern"); /** * Predicate that always returns true. * *

* Note that this typically has the same affect as a null predicate. * * @param The object type being tested. * @return A new predicate. */ public static final AssertionPredicate any() { return test(x -> true, null); } /** * Predicate that returns true if the tested value is not null. * *

* Assertion error message is "Value was null.". * * @param The object type being tested. * @return A new predicate. */ public static final AssertionPredicate notNull() { return test(x -> x != null, MSG_valueWasNull); } /** * Predicate that returns true if the tested value is null. * *

* Assertion error message is "Value was not null.". * * @param The object type being tested. * @return A new predicate. */ public static final AssertionPredicate isNull() { return test(x -> x == null, MSG_valueWasNotNull); } /** * Predicate that returns true if the tested value equals the specified value. * *

* Uses standard Java equality for testing. * *

* Assertion error message is "Value did not match expected. Expected='{0}', Actual='{1}'.". * * @param The object type being tested. * @param value The specified value. * @return A new predicate. */ public static final AssertionPredicate eq(Object value) { return test(x -> Objects.equals(x, value), MSG_valueDidNotMatchExpected, value, VALUE); } /** * Predicate that returns true if the tested value converted to a string matches the specified value. * *

* Assertion error message is "Value did not match expected. Expected='{0}', Actual='{1}'.". * * @param The object type being tested. * @param value The specified value. * @return A new predicate. */ public static final AssertionPredicate eq(String value) { return test(x -> Objects.equals(stringify(x), value), MSG_valueDidNotMatchExpected, value, VALUE); } /** * Predicate that returns true if the tested value does not match the specified value. * *

* Assertion error message is "Value unexpectedly matched. Value='{0}'.". * * @param The object type being tested. * @param value The specified value. * @return A new predicate. */ public static final AssertionPredicate ne(Object value) { return test(x -> ! Objects.equals(x, value), MSG_valueUnexpectedlyMatched, VALUE); } /** * Predicate that returns true if the tested value converted to a string does not match the specified value. * *

* Assertion error message is "Value unexpectedly matched. Value='{0}'.". * * @param The object type being tested. * @param value The specified value. * @return A new predicate. */ public static final AssertionPredicate ne(String value) { return test(x -> ! Objects.equals(stringify(x), value), MSG_valueUnexpectedlyMatched, VALUE); } /** * Predicate that returns true if the tested value converted to a string does not match the specified value ignoring case. * *

* Assertion error message is "Value did not match expected. Expected='{0}', Actual='{1}'.". * * @param The object type being tested. * @param value The specified value. * @return A new predicate. */ public static final AssertionPredicate eqic(String value) { return test(x -> StringUtils.eqic(stringify(x), value), MSG_valueDidNotMatchExpected, value, VALUE); } /** * Predicate that returns true if the tested value converted to a string contains the specified substring. * *

* Assertion error message is "Value did not contain expected. Expected='{0}', Actual='{1}'.". * * @param The object type being tested. * @param value The specified value. * @return A new predicate. */ public static final AssertionPredicate contains(String value) { return test(x -> StringUtils.contains(stringify(x), value), MSG_valueDidNotContainExpected, value, VALUE); } /** * Predicate that returns true if the tested value is the specified or child type. * *

* Assertion error message is "Value was not expected type. Expected='{0}', Actual='{1}'.". * * @param The object type being tested. * @param type The specified type. * @return A new predicate. */ public static final AssertionPredicate type(Class type) { assertArgNotNull("type", type); return test(x -> x != null && type.isAssignableFrom(x.getClass()), MSG_valueWasNotExpectedType, type, TYPENAME); } /** * Predicate that returns true if the tested value is exactly specified type. * *

* Assertion error message is "Value was not expected type. Expected='{0}', Actual='{1}'.". * * @param The object type being tested. * @param type The specified type. * @return A new predicate. */ public static final AssertionPredicate exactType(Class type) { assertArgNotNull("type", type); return test(x -> x != null && x.getClass().equals(type), MSG_valueWasNotExpectedType, type, TYPENAME); } /** * Predicate that returns true if the tested value converted to a string matches the specified match pattern. * *

* Match pattern can contain the "*" meta-character. * *

* Assertion error message is "Value did not match pattern. Pattern='{0}', Actual='{1}'.". * * @param The object type being tested. * @param value The specified value. * @return A new predicate. */ public static final AssertionPredicate match(String value) { assertArgNotNull("value", value); Pattern p = StringUtils.getMatchPattern(value); return test(x -> x != null && p.matcher(stringify(x)).matches(), MSG_valueDidNotMatchPattern, value, VALUE); } /** * Predicate that returns true if the tested value converted to a string matches the specified regular expression. * *

* Assertion error message is "Value did not match pattern. Pattern='{0}', Actual='{1}'.". * * @param The object type being tested. * @param expression The regular expression to match. * @return A new predicate. */ public static final AssertionPredicate regex(String expression) { assertArgNotNull("expression", expression); Pattern p = Pattern.compile(expression); return test(x -> x != null && p.matcher(stringify(x)).matches(), MSG_valueDidNotMatchPattern, expression, VALUE); } /** * Predicate that returns true if the tested value converted to a string matches the specified regular expression. * *

* Assertion error message is "Value did not match pattern. Pattern='{0}', Actual='{1}'.". * * @param The object type being tested. * @param expression The regular expression to match. * @param flags Match flags, a bit mask that may include: *

    *
  • {@link Pattern#CASE_INSENSITIVE CASE_INSENSITIVE} *
  • {@link Pattern#MULTILINE MULTILINE} *
  • {@link Pattern#DOTALL DOTALL} *
  • {@link Pattern#UNICODE_CASE UNICODE_CASE} *
  • {@link Pattern#CANON_EQ CANON_EQ} *
  • {@link Pattern#UNIX_LINES UNIX_LINES} *
  • {@link Pattern#LITERAL LITERAL} *
  • {@link Pattern#UNICODE_CHARACTER_CLASS UNICODE_CHARACTER_CLASS} *
  • {@link Pattern#COMMENTS COMMENTS} * @return A new predicate. */ public static final AssertionPredicate regex(String expression, int flags) { assertArgNotNull("expression", expression); Pattern p = Pattern.compile(expression, flags); return test(x -> x != null && p.matcher(stringify(x)).matches(), MSG_valueDidNotMatchPattern, expression, VALUE); } /** * Predicate that returns true if the tested value converted to a string matches the specified regular expression. * *

    * Assertion error message is "Value did not match pattern. Pattern='{0}', Actual='{1}'.". * * @param The object type being tested. * @param value The regular expression to match. * @return A new predicate. */ public static final AssertionPredicate regex(Pattern value) { assertArgNotNull("value", value); return test(x -> x != null && value.matcher(stringify(x)).matches(), MSG_valueDidNotMatchPattern, value.pattern(), VALUE); } /** * Predicate that wraps another predicate. * *

    * If the predicate extends from {@link AssertionPredicate}, the assertion error * message is "Value did not pass test." followed by the inner assertion error. * Otherwise the message is "Value did not pass test. Value='{0}'." * * @param The object type being tested. * @param predicate The predicate to run. * @return A new predicate. */ public static final AssertionPredicate test(Predicate predicate) { return new AssertionPredicate<>(predicate, null); } /** * Predicate that wraps another predicate. * *

    * If the message specified is null and the predicate extends from {@link AssertionPredicate}, the assertion error * message is "Value did not pass test." followed by the inner assertion error. * Otherwise the message is "Value did not pass test. Value='{0}'." * * @param The object type being tested. * @param predicate The predicate to run. * @param msg * The error message if predicate fails. *
    Supports {@link MessageFormat}-style arguments. * @param args * Optional message arguments. *
    Can contain {@code #VALUE} to specify the value itself as an argument. *
    Can contain {@link Function functions} to apply to the tested value. * @return A new predicate. */ public static final AssertionPredicate test(Predicate predicate, String msg, Object...args) { return new AssertionPredicate<>(predicate, msg, args); } /** * Combines the specified predicates into a singled AND'ed predicate. * *

    * Assertion error message is "Predicate test #x failed." followed by * the inner failed message if the failed predicate extends from {@link AssertionPredicate}. * * @param The predicate type. * @param predicates The predicates to combine. * @return The combined predicates. */ @SafeVarargs public static final AssertionPredicate and(Predicate...predicates) { return new AssertionPredicate.And<>(predicates); } /** * Combines the specified predicates into a singled OR'ed predicate. * *

    * Assertion error message is "No predicate tests passed.". * * @param The predicate type. * @param predicates The predicates to combine. * @return The combined predicates. */ @SafeVarargs public static final AssertionPredicate or(Predicate...predicates) { return new AssertionPredicate.Or<>(predicates); } /** * Negates the specified predicate. * *

    * Assertion error message is "Predicate test unexpectedly passed.". * * @param The predicate type. * @param predicate The predicate to negate. * @return The combined predicates. */ public static final AssertionPredicate not(Predicate predicate) { return new AssertionPredicate.Not<>(predicate); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy