![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.assertions.FluentCollectionAssertion Maven / Gradle / Ivy
Show all versions of juneau-assertions Show documentation
// ***************************************************************************************************************************
// * 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.internal.ObjectUtils.*;
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import org.apache.juneau.common.internal.StringUtils;
import org.apache.juneau.cp.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.serializer.*;
/**
* Used for fluent assertion calls against collections objects.
*
* Test Methods:
*
*
* - {@link FluentCollectionAssertion}
*
* - {@link FluentCollectionAssertion#isEmpty() isEmpty()}
*
- {@link FluentCollectionAssertion#isNotEmpty() isNotEmpty()}
*
- {@link FluentCollectionAssertion#isContains(Object) isContains(Object)}
*
- {@link FluentCollectionAssertion#isNotContains(Object) isNotContains(Object)}
*
- {@link FluentCollectionAssertion#isAny(Predicate) isAny(Predicate)}
*
- {@link FluentCollectionAssertion#isAll(Predicate) isAll(Predicate)}
*
- {@link FluentCollectionAssertion#isSize(int size) isSize(int size)}
*
* - {@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 FluentCollectionAssertion}
*
* - {@link FluentCollectionAssertion#asStrings() asStrings()}
*
- {@link FluentCollectionAssertion#asSize() asSize()}
*
* - {@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 element type.
* @param The return type.
*/
@FluentSetters(returns="FluentCollectionAssertion")
public class FluentCollectionAssertion extends FluentObjectAssertion,R> {
//-----------------------------------------------------------------------------------------------------------------
// Static
//-----------------------------------------------------------------------------------------------------------------
private static final Messages MESSAGES = Messages.of(FluentCollectionAssertion.class, "Messages");
private static final String
MSG_collectionWasNotEmpty = MESSAGES.getString("collectionWasNotEmpty"),
MSG_collectionDidNotContainExpectedValue = MESSAGES.getString("collectionDidNotContainExpectedValue"),
MSG_collectionDidNotContainTestedValue = MESSAGES.getString("collectionDidNotContainTestedValue"),
MSG_collectionContainedUnexpectedValue = MESSAGES.getString("collectionContainedUnexpectedValue"),
MSG_collectionWasEmpty = MESSAGES.getString("collectionWasEmpty"),
MSG_collectionDidNotHaveExpectedSize = MESSAGES.getString("collectionDidNotHaveExpectedSize");
//-----------------------------------------------------------------------------------------------------------------
// 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 FluentCollectionAssertion(Collection 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 FluentCollectionAssertion(Assertion creator, Collection value, R returns) {
super(creator, value, returns);
}
//-----------------------------------------------------------------------------------------------------------------
// Transform methods
//-----------------------------------------------------------------------------------------------------------------
@Override /* FluentObjectAssertion */
public FluentCollectionAssertion asTransformed(Function,Collection> function) {
return new FluentCollectionAssertion<>(this, function.apply(orElse(null)), 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 : value().stream().map(StringUtils::stringify).collect(Collectors.toList()), returns());
}
/**
* Returns an integer assertion on the size of this collection.
*
*
* If the collection is null , the returned assertion is a null assertion
* (meaning {@link FluentIntegerAssertion#isExists()} returns false ).
*
* @return A new assertion.
*/
public FluentIntegerAssertion asSize() {
return new FluentIntegerAssertion<>(this, valueIsNull() ? null : value().size(), returns());
}
//-----------------------------------------------------------------------------------------------------------------
// Test methods
//-----------------------------------------------------------------------------------------------------------------
/**
* Asserts that the collection exists and is empty.
*
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isEmpty() throws AssertionError {
if (! value().isEmpty())
throw error(MSG_collectionWasNotEmpty);
return returns();
}
/**
* Asserts that the collection exists and is not empty.
*
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isNotEmpty() throws AssertionError {
if (value().isEmpty())
throw error(MSG_collectionWasEmpty);
return returns();
}
/**
* Asserts that the collection contains the expected value.
*
* @param entry The value to check for.
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isContains(E entry) throws AssertionError {
for (Object v : value())
if (eq(v, entry))
return returns();
throw error(MSG_collectionDidNotContainExpectedValue, entry, value());
}
/**
* Asserts that the collection contains the expected value.
*
* @param entry The value to check for.
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isNotContains(E entry) throws AssertionError {
value().forEach(x -> {
if (eq(x, entry))
throw error(MSG_collectionContainedUnexpectedValue, entry, value());
});
return returns();
}
/**
* Asserts that at least one value in the collection 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 {
if (test == null)
return returns();
for (E v : value())
if (test.test(v))
return returns();
throw error(MSG_collectionDidNotContainTestedValue, value());
}
/**
* Asserts that all values in the collection pass 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 {
if (test == null)
return returns();
value().forEach(x -> {
if (! test.test(x))
throw error(MSG_collectionDidNotContainTestedValue, value());
});
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 or value was null .
*/
public R isSize(int size) throws AssertionError {
if (getSize() != size)
throw error(MSG_collectionDidNotHaveExpectedSize, size, getSize());
return returns();
}
//-----------------------------------------------------------------------------------------------------------------
// Fluent setters
//-----------------------------------------------------------------------------------------------------------------
//
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentCollectionAssertion setMsg(String msg, Object...args) {
super.setMsg(msg, args);
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentCollectionAssertion setOut(PrintStream value) {
super.setOut(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentCollectionAssertion setSilent() {
super.setSilent();
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentCollectionAssertion setStdOut() {
super.setStdOut();
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentCollectionAssertion setThrowable(Class extends java.lang.RuntimeException> value) {
super.setThrowable(value);
return this;
}
//
//-----------------------------------------------------------------------------------------------------------------
// Utility methods
//-----------------------------------------------------------------------------------------------------------------
/**
* Returns the size of this collection if it is not null .
*
* @return the size of this collection if it is not null .
* @throws AssertionError If value was null .
*/
protected int getSize() throws AssertionError {
return value().size();
}
}