![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.assertions.FluentThrowableAssertion 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.Collections.*;
import static org.apache.juneau.common.internal.ArgUtils.*;
import static org.apache.juneau.common.internal.ThrowableUtils.*;
import static org.apache.juneau.internal.CollectionUtils.list;
import java.io.*;
import java.util.*;
import java.util.function.*;
import org.apache.juneau.cp.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.serializer.*;
/**
* Used for fluent assertion calls against throwables.
*
* 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 FluentThrowableAssertion}
*
* - {@link FluentThrowableAssertion#asMessage() asMessage()}
*
- {@link FluentThrowableAssertion#asMessages() asMessages()}
*
- {@link FluentThrowableAssertion#asLocalizedMessage() asLocalizedMessage()}
*
- {@link FluentThrowableAssertion#asLocalizedMessages() asLocalizedMessages()}
*
- {@link FluentThrowableAssertion#asStackTrace() asStackTrace()}
*
- {@link FluentThrowableAssertion#asCausedBy() asCausedBy()}
*
- {@link FluentThrowableAssertion#asCausedBy(Class) asCausedBy(Class)}
*
- {@link FluentThrowableAssertion#asFind(Class) asFind(Class)}
*
* - {@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 throwable type.
* @param The return type.
*/
@FluentSetters(returns="FluentThrowableAssertion")
public class FluentThrowableAssertion extends FluentObjectAssertion {
//-----------------------------------------------------------------------------------------------------------------
// Static
//-----------------------------------------------------------------------------------------------------------------
private static final Messages MESSAGES = Messages.of(FluentThrowableAssertion.class, "Messages");
private static final String
MSG_exceptionWasNotExpectedType = MESSAGES.getString("exceptionWasNotExpectedType"),
MSG_exceptionWasNotThrown = MESSAGES.getString("exceptionWasNotThrown"),
MSG_causedByExceptionNotExpectedType = MESSAGES.getString("causedByExceptionNotExpectedType");
//-----------------------------------------------------------------------------------------------------------------
// 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 FluentThrowableAssertion(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 FluentThrowableAssertion(Assertion creator, T value, R returns) {
super(creator, value, returns);
}
//-----------------------------------------------------------------------------------------------------------------
// Transform methods
//-----------------------------------------------------------------------------------------------------------------
@Override /* FluentObjectAssertion */
public FluentThrowableAssertion asTransformed(Function function) {
return new FluentThrowableAssertion<>(this, function.apply(orElse(null)), returns());
}
/**
* Returns an assertion against the throwable message.
*
* Example:
*
* // Asserts that the specified method throws an exception
* // with 'foobar' somewhere in the messages.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .asMessage()
* .isPattern(".*foobar.*" );
*
*
* @return An assertion against the throwable message. Never null .
*/
public FluentStringAssertion asMessage() {
return new FluentStringAssertion<>(this, map(Throwable::getMessage).orElse(null), returns());
}
/**
* Returns an assertion against the throwable message and all caused-by messages.
*
* Example:
*
* // Asserts that the specified method throws an exception with
* // 'foobar' somewhere in the messages.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .asMessages()
* .isPattern(".*foobar.*" );
*
*
* @return An assertion against the throwable message. Never null .
*/
public FluentListAssertion asMessages() {
List l = null;
Throwable t = orElse(null);
if (t != null) {
if (t.getCause() == null)
l = singletonList(t.getMessage());
else {
l = list();
while (t != null) {
l.add(t.getMessage());
t = t.getCause();
}
}
}
return new FluentListAssertion<>(this, l, returns());
}
/**
* Returns an assertion against the throwable localized message.
*
* Example:
*
* // Asserts that the specified method throws an exception with
* // 'foobar' somewhere in the localized messages.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .asLocalizedMessage()
* .isPattern(".*foobar.*" );
*
*
* @return An assertion against the throwable localized message. Never null .
*/
public FluentStringAssertion asLocalizedMessage() {
return new FluentStringAssertion<>(this, map(Throwable::getLocalizedMessage).orElse(null), returns());
}
/**
* Returns an assertion against the throwable message and all caused-by messages.
*
* Example:
*
* // Asserts that the specified method throws an exception with
* // 'foobar' somewhere in the messages.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .asLocalizedMessages()
* .isPattern(".*foobar.*" );
*
*
* @return An assertion against the throwable message. Never null .
*/
public FluentListAssertion asLocalizedMessages() {
List l = null;
Throwable t = orElse(null);
if (t != null) {
if (t.getCause() == null)
l = singletonList(t.getMessage());
else {
l = list();
while (t != null) {
l.add(t.getLocalizedMessage());
t = t.getCause();
}
}
}
return new FluentListAssertion<>(this, l, returns());
}
/**
* Returns an assertion against the throwable localized message.
*
* Example:
*
* // Asserts that the specified method throws an exception with
* // 'foobar' somewhere in the stack trace.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .asStackTrace()
* .isPattern("foobar" );
*
*
* @return An assertion against the throwable stacktrace. Never null .
*/
public FluentStringListAssertion asStackTrace() {
return new FluentStringListAssertion<>(this, valueIsNull() ? null : Arrays.asList(getStackTrace(value())), returns());
}
/**
* Returns an assertion against the caused-by throwable.
*
* Example:
*
* // Asserts that the specified method throws an exception whose
* // caused-by message contains 'foobar'.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .asCausedBy()
* .asMessage()
* .isPattern("foobar" );
*
*
* @return An assertion against the caused-by. Never null .
*/
public FluentThrowableAssertion asCausedBy() {
return asCausedBy(Throwable.class);
}
/**
* Returns an assertion against the caused-by throwable.
*
* Example:
*
* // Asserts that the specified method throws an exception whose
* // caused-by message contains 'foobar'.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .asCausedBy(RuntimeException.class )
* .asMessage()
* .isPattern("foobar" );
*
*
* @param The throwable type.
* @param type The expected exception type.
* @return An assertion against the caused-by. Never null .
*/
public FluentThrowableAssertion asCausedBy(Class type) {
Throwable t = map(Throwable::getCause).orElse(null);
if (t == null || type.isInstance(t))
return new FluentThrowableAssertion<>(this, type.cast(t), returns());
throw error(MSG_causedByExceptionNotExpectedType, type, t.getClass());
}
/**
* Returns an assertion against the throwable localized message.
*
* Example:
*
* // Asserts that the specified method throws an exception with a
* // caused-by RuntimeException containing 'foobar'
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .findCausedBy(RuntimeException.class )
* .isExists()
* .asMessage()
* .isPattern("foobar" );
*
*
* @param The throwable type.
* @param throwableClass The class type to search for in the caused-by chain.
* @return An assertion against the caused-by throwable. Never null .
*/
public FluentThrowableAssertion asFind(Class throwableClass) {
Throwable t = orElse(null);
while (t != null) {
if (throwableClass.isInstance(t))
return new FluentThrowableAssertion<>(this, throwableClass.cast(t), returns());
t = t.getCause();
}
return new FluentThrowableAssertion<>(this, (X)null, returns());
}
//-----------------------------------------------------------------------------------------------------------------
// Test methods
//-----------------------------------------------------------------------------------------------------------------
/**
* Asserts that this throwable is of the specified type.
*
* Example:
*
* // Asserts that the specified method throws a RuntimeException.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .isType(RuntimeException.class );
*
*
* @param parent The type.
* @return The fluent return object.
*/
@Override
public R isType(Class> parent) {
assertArgNotNull("parent", parent);
if (! parent.isInstance(value()))
throw error(MSG_exceptionWasNotExpectedType, className(parent), className(value()));
return returns();
}
/**
* Asserts that this throwable is exactly the specified type.
*
* Example:
*
* // Asserts that the specified method throws a RuntimeException.
* ThrowableAssertion.assertThrown (() -> foo .getBar())
* .isExactType(RuntimeException.class );
*
*
* @param type The type.
* @return The fluent return object.
*/
@Override
public R isExactType(Class> type) {
assertArgNotNull("type", type);
if (type != value().getClass())
throw error(MSG_exceptionWasNotExpectedType, className(type), className(value()));
return returns();
}
/**
* Asserts that this throwable exists.
*
* Example:
*
* // Asserts that the specified method throws any exception.
* ThrowableAssertion.assertThrown (() -> foo .getBar()).isExists();
*
*
* @return The fluent return object.
*/
@Override
public R isExists() {
if (valueIsNull())
throw error(MSG_exceptionWasNotThrown);
return returns();
}
//-----------------------------------------------------------------------------------------------------------------
// Fluent setters
//-----------------------------------------------------------------------------------------------------------------
//
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentThrowableAssertion setMsg(String msg, Object...args) {
super.setMsg(msg, args);
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentThrowableAssertion setOut(PrintStream value) {
super.setOut(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentThrowableAssertion setSilent() {
super.setSilent();
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentThrowableAssertion setStdOut() {
super.setStdOut();
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentThrowableAssertion setThrowable(Class extends java.lang.RuntimeException> value) {
super.setThrowable(value);
return this;
}
//
//-----------------------------------------------------------------------------------------------------------------
// Utility methods
//-----------------------------------------------------------------------------------------------------------------
@Override
protected boolean equals(Object o1, Object o2) {
if (o1 instanceof Throwable && o2 instanceof Throwable)
return ObjectUtils.eq((Throwable)o1, (Throwable)o2, (x,y)->ObjectUtils.eq(x.getClass(),y.getClass()) && ObjectUtils.eq(x.getMessage(),y.getMessage()));
return super.equals(o1, o2);
}
}