001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.bct.assertions; 018 019import java.io.*; 020import java.util.function.*; 021 022 023/** 024 * Used for assertion calls against throwable objects. 025 * 026 * <h5 class='section'>Example:</h5> 027 * <p class='bjava'> 028 * <jc>// Validates the throwable message or one of the parent messages contain 'Foobar'.</jc> 029 * <jsm>assertThrowable</jsm>(<jv>throwable</jv>).contains(<js>"Foobar"</js>); 030 * </p> 031 * 032 * 033 * <h5 class='section'>Test Methods:</h5> 034 * <p> 035 * <ul class='javatree'> 036 * <li class='jc'>{@link FluentObjectAssertion} 037 * <ul class='javatreec'> 038 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 039 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 040 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 041 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 042 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 043 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 044 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 045 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 046 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 047 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 048 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 049 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 050 * </ul> 051 * </ul> 052 * 053 * <h5 class='section'>Transform Methods:</h5> 054 * <p> 055 * <ul class='javatree'> 056 * <li class='jc'>{@link FluentThrowableAssertion} 057 * <ul class='javatreec'> 058 * <li class='jm'>{@link FluentThrowableAssertion#asMessage() asMessage()} 059 * <li class='jm'>{@link FluentThrowableAssertion#asMessages() asMessages()} 060 * <li class='jm'>{@link FluentThrowableAssertion#asLocalizedMessage() asLocalizedMessage()} 061 * <li class='jm'>{@link FluentThrowableAssertion#asLocalizedMessages() asLocalizedMessages()} 062 * <li class='jm'>{@link FluentThrowableAssertion#asStackTrace() asStackTrace()} 063 * <li class='jm'>{@link FluentThrowableAssertion#asCausedBy() asCausedBy()} 064 * <li class='jm'>{@link FluentThrowableAssertion#asCausedBy(Class) asCausedBy(Class)} 065 * <li class='jm'>{@link FluentThrowableAssertion#asFind(Class) asFind(Class)} 066 * </ul> 067 * <li class='jc'>{@link FluentObjectAssertion} 068 * <ul class='javatreec'> 069 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 070 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 071 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 072 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 073 * </ul> 074 * </ul> 075 * 076 * <h5 class='section'>Configuration Methods:</h5> 077 * <p> 078 * <ul class='javatree'> 079 * <li class='jc'>{@link Assertion} 080 * <ul class='javatreec'> 081 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 082 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 083 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 084 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 085 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 086 * </ul> 087 * </ul> 088 * 089 * <h5 class='section'>See Also:</h5><ul> 090 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 091 * </ul> 092 * 093 * @param <T> The throwable type. 094 */ 095public class ThrowableAssertion<T extends Throwable> extends FluentThrowableAssertion<T,ThrowableAssertion<T>> { 096 097 /** 098 * Static creator. 099 * 100 * @param <X> The throwable type. 101 * @param value 102 * The object being tested. 103 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 104 * @return A new assertion object. 105 */ 106 public static <X extends Throwable> ThrowableAssertion<X> create(X value) { 107 return new ThrowableAssertion<>(value); 108 } 109 110 /** 111 * Constructor. 112 * 113 * @param value 114 * The object being tested. 115 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 116 */ 117 public ThrowableAssertion(T value) { 118 super(value, null); 119 } 120 121 @Override /* Overridden from Assertion */ 122 public ThrowableAssertion<T> setMsg(String msg, Object...args) { 123 super.setMsg(msg, args); 124 return this; 125 } 126 127 @Override /* Overridden from Assertion */ 128 public ThrowableAssertion<T> setOut(PrintStream value) { 129 super.setOut(value); 130 return this; 131 } 132 133 @Override /* Overridden from Assertion */ 134 public ThrowableAssertion<T> setSilent() { 135 super.setSilent(); 136 return this; 137 } 138 139 @Override /* Overridden from Assertion */ 140 public ThrowableAssertion<T> setStdOut() { 141 super.setStdOut(); 142 return this; 143 } 144 145 @Override /* Overridden from Assertion */ 146 public ThrowableAssertion<T> setThrowable(Class<? extends RuntimeException> value) { 147 super.setThrowable(value); 148 return this; 149 } 150}