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

org.apache.juneau.assertions.FluentArrayAssertion 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.common.internal.ArgUtils.*;
import static org.apache.juneau.common.internal.StringUtils.*;
import static org.apache.juneau.internal.CollectionUtils.*;
import static org.apache.juneau.internal.ObjectUtils.*;
import static java.util.Arrays.*;

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

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

/**
 * Used for fluent assertion calls against array objects.
 *
 * 
Test Methods:
*

*

    *
  • {@link FluentArrayAssertion} *
      *
    • {@link FluentArrayAssertion#isHas(Object[]) isHas(Object[])} *
    • {@link FluentArrayAssertion#is(Predicate) is(Predicate)} *
    • {@link FluentArrayAssertion#isAny(Predicate) isAny(Predicate)} *
    • {@link FluentArrayAssertion#isAll(Predicate) isAll(Predicate)} *
    • {@link FluentArrayAssertion#isEmpty() isEmpty()} *
    • {@link FluentArrayAssertion#isNotEmpty() isNotEmpty()} *
    • {@link FluentArrayAssertion#isSize(int size) isSize(int size)} *
    • {@link FluentArrayAssertion#isContains(Object) isContains(Object)} *
    • {@link FluentArrayAssertion#isNotContains(Object) isNotContains(Object)} *
    *
  • {@link FluentObjectAssertion} *
      *
    • {@link FluentObjectAssertion#isExists() isExists()} *
    • {@link FluentObjectAssertion#is(Object) is(Object)} *
    • {@link FluentObjectAssertion#is(Predicate) is(Predicate)} *
    • {@link FluentObjectAssertion#isNot(Object) isNot(Object)} *
    • {@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} *
    • {@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} *
    • {@link FluentObjectAssertion#isNull() isNull()} *
    • {@link FluentObjectAssertion#isNotNull() isNotNull()} *
    • {@link FluentObjectAssertion#isString(String) isString(String)} *
    • {@link FluentObjectAssertion#isJson(String) isJson(String)} *
    • {@link FluentObjectAssertion#isSame(Object) isSame(Object)} *
    • {@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)} *
    • {@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)} *
    • {@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)} *
    • {@link FluentObjectAssertion#isType(Class) isType(Class)} *
    • {@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} *
    *
* *
Transform Methods:
*

*

    *
  • {@link FluentArrayAssertion} *
      *
    • {@link FluentArrayAssertion#asStrings() asStrings()} *
    • {@link FluentArrayAssertion#asStrings(Function) asStrings(Function)} *
    • {@link FluentArrayAssertion#asCdl() asCdl()} *
    • {@link FluentArrayAssertion#asCdl(Function) asCdl(Function)} *
    • {@link FluentArrayAssertion#asBeanList() asBeanList()} *
    • {@link FluentArrayAssertion#asItem(int) asItem(int)} *
    • {@link FluentArrayAssertion#asSorted() asSorted()} *
    • {@link FluentArrayAssertion#asSorted(Comparator) asSorted(Comparator)} *
    *
  • {@link FluentObjectAssertion} *
      *
    • {@link FluentObjectAssertion#asString() asString()} *
    • {@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)} *
    • {@link FluentObjectAssertion#asString(Function) asString(Function)} *
    • {@link FluentObjectAssertion#asJson() asJson()} *
    • {@link FluentObjectAssertion#asJsonSorted() asJsonSorted()} *
    • {@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)} *
    • {@link FluentObjectAssertion#asAny() asAny()} *
    *
* *
Configuration Methods:
*

*

    *
  • {@link Assertion} *
      *
    • {@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} *
    • {@link Assertion#setOut(PrintStream) setOut(PrintStream)} *
    • {@link Assertion#setSilent() setSilent()} *
    • {@link Assertion#setStdOut() setStdOut()} *
    • {@link Assertion#setThrowable(Class) setThrowable(Class)} *
    *
* *
See Also:
* * @param The entry type. * @param The return type. */ @FluentSetters(returns="FluentArrayAssertion") public class FluentArrayAssertion extends FluentObjectAssertion { //----------------------------------------------------------------------------------------------------------------- // Static //----------------------------------------------------------------------------------------------------------------- private static final Messages MESSAGES = Messages.of(FluentArrayAssertion.class, "Messages"); private static final String MSG_arrayWasNotEmpty = MESSAGES.getString("arrayWasNotEmpty"), MSG_arrayWasEmpty = MESSAGES.getString("arrayWasEmpty"), MSG_arrayUnexpectedSize = MESSAGES.getString("arrayUnexpectedSize"), MSG_arrayDidNotContainExpectedValue = MESSAGES.getString("arrayDidNotContainExpectedValue"), MSG_arrayContainedUnexpectedValue = MESSAGES.getString("arrayContainedUnexpectedValue"), MSG_arrayDidNotContainExpectedValueAt = MESSAGES.getString("arrayDidNotContainExpectedValueAt"), MSG_arrayDidntContainAnyMatchingValue = MESSAGES.getString("arrayDidntContainAnyMatchingValue"), MSG_arrayContainedNonMatchingValueAt = MESSAGES.getString("arrayContainedNonMatchingValueAt"); //----------------------------------------------------------------------------------------------------------------- // Instance //----------------------------------------------------------------------------------------------------------------- /** * Constructor. * * @param value * The object being tested. *
Can be null. * @param returns * The object to return after a test method is called. *
If null, the test method returns this object allowing multiple test method calls to be * used on the same assertion. */ public FluentArrayAssertion(E[] value, R returns) { this(null, value, returns); } /** * Chained constructor. * *

* Used when transforming one assertion into another so that the assertion config can be used by the new assertion. * * @param creator * The assertion that created this assertion. *
Should be null if this is the top-level assertion. * @param value * The object being tested. *
Can be null. * @param returns * The object to return after a test method is called. *
If null, the test method returns this object allowing multiple test method calls to be * used on the same assertion. */ public FluentArrayAssertion(Assertion creator, E[] value, R returns) { super(creator, value, returns); } //----------------------------------------------------------------------------------------------------------------- // Transform methods //----------------------------------------------------------------------------------------------------------------- @Override /* FluentObjectAssertion */ public FluentArrayAssertion asTransformed(Function function) { return new FluentArrayAssertion<>(this, function.apply(orElse(null)), returns()); } @Override /* FluentBaseAssertion */ public FluentStringAssertion asString() { return new FluentStringAssertion<>(this, toString(), returns()); } /** * Converts this assertion into a {@link FluentListAssertion} of strings. * * @return A new fluent string assertion. */ public FluentStringListAssertion asStrings() { return new FluentStringListAssertion<>(this, valueIsNull() ? null : stream(value()).map(StringUtils::stringify).collect(Collectors.toList()), returns()); } /** * Runs the stringify function against all values in this list and returns it as a fluent string list assertion. * * @param function The function to apply to all values in this list. * @return A new fluent string list assertion. Never null. */ public FluentStringListAssertion asStrings(Function function) { List l = valueIsNull() ? null : stream(value()).map(x -> function.apply(x)).collect(Collectors.toList()); return new FluentStringListAssertion<>(this, l, returns()); } /** * Converts the entries in this list to a simple comma-delimited list and returns the value as a fluent string assertion. * * @return A fluent string assertion. Never null. */ public FluentStringAssertion asCdl() { return new FluentStringAssertion<>(this, valueIsNull() ? null : StringUtils.join(value(), ','), returns()); } /** * Converts the entries to strings using the specified stringify function, combines them into a simple comma-delimited list, and returns the value as a fluent string assertion. * * @param function The function to apply to all values in this list. * @return A fluent string assertion. Never null. */ public FluentStringAssertion asCdl(Function function) { List l = valueIsNull() ? null : stream(value()).map(x -> function.apply(x)).collect(Collectors.toList()); return new FluentStringAssertion<>(this, join(l, ','), returns()); } /** * Converts this assertion into a {@link FluentBeanListAssertion}. * *

Example:
*

* // Extracts the 'foo' property from an array of beans and validates their values.. * assertObject(myArrayOfBeans) * .asBeanList() * .asProperty("foo") * .asSorted() * .equals("value1","value2","value3"); *

* * @return A new fluent string assertion. */ public FluentBeanListAssertion asBeanList() { return new FluentBeanListAssertion<>(this, toList(), returns()); } /** * Returns an object assertion on the item specified at the specified index. * *

* If the array is null or the index is out-of-bounds, the returned assertion is a null assertion * (meaning {@link FluentAnyAssertion#isExists()} returns false). * * @param index The index of the item to retrieve from the array. * @return A new assertion. */ public FluentAnyAssertion asItem(int index) { return new FluentAnyAssertion<>(this, at(index), returns()); } /** * Sorts the entries in this list. * * @return A new list assertion. The contents of the original list remain unchanged. */ public FluentListAssertion asSorted() { return new FluentListAssertion<>(this, toSortedList(null), returns()); } /** * Sorts the entries in this list using the specified comparator. * * @param comparator The comparator to use to sort the list. * @return A new list assertion. The contents of the original list remain unchanged. */ public FluentListAssertion asSorted(Comparator comparator) { return new FluentListAssertion<>(this, toSortedList(comparator), returns()); } //----------------------------------------------------------------------------------------------------------------- // Test methods //----------------------------------------------------------------------------------------------------------------- /** * Asserts that the contents of this list contain the specified values. * * @param entries The expected entries in this list. * @return This object. * @throws AssertionError If assertion failed. */ @SuppressWarnings("unchecked") public R isHas(E...entries) throws AssertionError { assertArgNotNull("entries", entries); Predicate[] p = stream(entries).map(AssertionPredicates::eq).toArray(Predicate[]::new); return is(p); } /** * Asserts that the contents of this list pass the specified tests. * * @param tests The tests to run. null entries are ignored. * @return This object. * @throws AssertionError If assertion failed. */ @SafeVarargs public final R is(Predicate...tests) throws AssertionError { isSize(tests.length); for (int i = 0, j = length(); i < j; i++) { Predicate t = tests[i]; if (t != null) if (! t.test(at(i))) throw error(MSG_arrayDidNotContainExpectedValueAt, i, getFailureMessage(t, at(i))); } return returns(); } /** * Asserts that at least one value in the array passes the specified test. * * @param test The predicate test. * @return The fluent return object. * @throws AssertionError If assertion failed or value was null. */ public R isAny(Predicate test) throws AssertionError { assertArgNotNull("test", test); for (E v : value()) if (test.test(v)) return returns(); throw error(MSG_arrayDidntContainAnyMatchingValue, (Object)value()); } /** * Asserts that all values in the array passes the specified test. * * @param test The predicate test. * @return The fluent return object. * @throws AssertionError If assertion failed or value was null. */ public R isAll(Predicate test) throws AssertionError { assertArgNotNull("test", test); for (int i = 0, j = length(); i < j; i++) if (! test.test(at(i))) throw error(MSG_arrayContainedNonMatchingValueAt, i, getFailureMessage(test, at(i))); return returns(); } /** * Asserts that the collection exists and is empty. * * @return The fluent return object. * @throws AssertionError If assertion failed. */ public R isEmpty() throws AssertionError { if (length() != 0) throw error(MSG_arrayWasNotEmpty); return returns(); } /** * Asserts that the collection exists and is not empty. * * @return The fluent return object. * @throws AssertionError If assertion failed. */ public R isNotEmpty() throws AssertionError { if (length() == 0) throw error(MSG_arrayWasEmpty); return returns(); } /** * Asserts that the collection exists and is the specified size. * * @param size The expected size. * @return The fluent return object. * @throws AssertionError If assertion failed. */ public R isSize(int size) throws AssertionError { if (length() != size) throw error(MSG_arrayUnexpectedSize, size, length()); return returns(); } /** * Asserts that the array contains the expected value. * * @param entry The value to check for. * @return The fluent return object. * @throws AssertionError If assertion failed. */ public R isContains(E entry) throws AssertionError { for (int i = 0, j = length(); i < j; i++) if (eq(at(i), entry)) return returns(); throw error(MSG_arrayDidNotContainExpectedValue, entry, toString()); } /** * Asserts that the array does not contain the expected value. * * @param entry The value to check for. * @return The fluent return object. * @throws AssertionError If assertion failed. */ public R isNotContains(E entry) throws AssertionError { for (int i = 0, j = length(); i < j; i++) if (eq(at(i), entry)) throw error(MSG_arrayContainedUnexpectedValue, entry, toString()); return returns(); } //----------------------------------------------------------------------------------------------------------------- // Fluent setters //----------------------------------------------------------------------------------------------------------------- // @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentArrayAssertion setMsg(String msg, Object...args) { super.setMsg(msg, args); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentArrayAssertion setOut(PrintStream value) { super.setOut(value); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentArrayAssertion setSilent() { super.setSilent(); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentArrayAssertion setStdOut() { super.setStdOut(); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentArrayAssertion setThrowable(Class value) { super.setThrowable(value); return this; } // //----------------------------------------------------------------------------------------------------------------- // Utility methods //----------------------------------------------------------------------------------------------------------------- private int length() { return value().length; } private List toList() { return valueIsNull() ? null : list(value()); } private List toSortedList(Comparator comparator) { return valueIsNull() ? null : sortedList(comparator, value()); } private E at(int index) { return valueIsNull() || index >= length() || index < 0 ? null : value()[index]; } @Override public String toString() { return valueIsNull() ? null : Arrays.toString(value()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy