![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.assertions.FluentBeanAssertion 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 java.util.stream.Collectors.*;
import static org.apache.juneau.common.internal.StringUtils.*;
import static java.util.Arrays.*;
import java.io.*;
import java.util.function.*;
import org.apache.juneau.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.serializer.*;
/**
* Used for fluent assertion calls against Java beans.
*
* Test Methods:
*
*
* - {@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 FluentBeanAssertion}
*
* - {@link FluentBeanAssertion#asPropertyMap(String...) asPropertyMap(String...)}
*
- {@link FluentBeanAssertion#asProperty(String) asProperty(String)}
*
- {@link FluentBeanAssertion#asProperties(String...) asProperties(String...)}
*
* - {@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 bean type.
* @param The return type.
*/
@FluentSetters(returns="FluentBeanAssertion")
public class FluentBeanAssertion extends FluentObjectAssertion {
//-----------------------------------------------------------------------------------------------------------------
// 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 FluentBeanAssertion(T 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 FluentBeanAssertion(Assertion creator, T value, R returns) {
super(creator, value, returns);
}
//-----------------------------------------------------------------------------------------------------------------
// Transform methods
//-----------------------------------------------------------------------------------------------------------------
@Override /* FluentObjectAssertion */
public FluentBeanAssertion asTransformed(Function function) {
return new FluentBeanAssertion<>(this, function.apply(orElse(null)), returns());
}
/**
* Extracts the specified fields of this bean into a simple map of key/value pairs and returns it as
* a new {@link MapAssertion}.
*
* @param names The fields to extract. Can also pass in comma-delimited lists.
* @return This object.
*/
public FluentMapAssertion asPropertyMap(String...names) {
return new FluentMapAssertion<>(this, toBeanMap().getProperties(split(names, ',')), returns());
}
/**
* Extracts the specified property as an {@link FluentAnyAssertion}.
*
* @param name The property to extract. Can also pass in comma-delimited lists.
* @return An assertion of the property value.
*/
public FluentAnyAssertion