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
022import org.apache.juneau.commons.function.*;
023import org.apache.juneau.commons.utils.*;
024
025
026/**
027 * Used for fluent assertion calls against booleans.
028 *
029 * <h5 class='section'>Test Methods:</h5>
030 * <p>
031 * <ul class='javatree'>
032 *    <li class='jc'>{@link FluentBooleanAssertion}
033 *    <ul class='javatreec'>
034 *       <li class='jm'>{@link FluentBooleanAssertion#isTrue() isTrue()}
035 *       <li class='jm'>{@link FluentBooleanAssertion#isFalse() isFalse()}
036 *    </ul>
037 *    <li class='jc'>{@link FluentComparableAssertion}
038 *    <ul class='javatreec'>
039 *       <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)}
040 *       <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)}
041 *       <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)}
042 *       <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)}
043 *       <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)}
044  *   </ul>
045 *    <li class='jc'>{@link FluentObjectAssertion}
046 *    <ul class='javatreec'>
047 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
048 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
049 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
050 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
051 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
052 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
054 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
055 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
056 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
057 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
058 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
059 *    </ul>
060 * </ul>
061 *
062 * <h5 class='section'>Transform Methods:</h5>
063 * <p>
064 * <ul class='javatree'>
065 *    <li class='jc'>{@link FluentObjectAssertion}
066 *    <ul class='javatreec'>
067 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
068 *       <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)}
069 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)}
070 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
071 * </ul>
072 * </ul>
073 *
074 * <h5 class='section'>Configuration Methods:</h5>
075 * <p>
076 * <ul class='javatree'>
077 *    <li class='jc'>{@link Assertion}
078 *    <ul class='javatreec'>
079 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
080 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
081 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
082 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
083 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
084 *    </ul>
085 * </ul>
086 *
087 * <h5 class='section'>See Also:</h5><ul>
088 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a>
089 * </ul>
090 *
091 * @param <R> The return type.
092 */
093@SuppressWarnings({
094   "java:S115" // Constants use UPPER_snakeCase convention (e.g., MSG_valueWasFalse)
095})
096public class FluentBooleanAssertion<R> extends FluentComparableAssertion<Boolean,R> {
097
098   private static final Messages MESSAGES = Messages.of(FluentBooleanAssertion.class, "Messages");
099   private static final String MSG_valueWasFalse = MESSAGES.getString("valueWasFalse");
100   private static final String MSG_valueWasTrue = MESSAGES.getString("valueWasTrue");
101
102   /**
103    * Chained constructor.
104    *
105    * <p>
106    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
107    *
108    * @param creator
109    *    The assertion that created this assertion.
110    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
111    * @param value
112    *    The object being tested.
113    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
114    * @param returns
115    *    The object to return after a test method is called.
116    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
117    * used on the same assertion.
118    */
119   public FluentBooleanAssertion(Assertion creator, Boolean value, R returns) {
120      super(creator, value, returns);
121   }
122
123   /**
124    * Constructor.
125    *
126    * @param value
127    *    The object being tested.
128    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
129    * @param returns
130    *    The object to return after a test method is called.
131    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
132    * used on the same assertion.
133    */
134   public FluentBooleanAssertion(Boolean value, R returns) {
135      this(null, value, returns);
136   }
137
138   /**
139    * Asserts that the value is false.
140    *
141    * @return The fluent return object.
142    * @throws AssertionError If assertion failed.
143    */
144   public R isFalse() throws AssertionError {
145      if (ObjectUtils.isTrue(value()))
146         throw error(MSG_valueWasTrue);
147      return returns();
148   }
149
150   /**
151    * Asserts that the value is true.
152    *
153    * @return The fluent return object.
154    * @throws AssertionError If assertion failed.
155    */
156   public R isTrue() throws AssertionError {
157      if (ObjectUtils.isFalse(value()))
158         throw error(MSG_valueWasFalse);
159      return returns();
160   }
161
162   @Override /* Overridden from Assertion */
163   public FluentBooleanAssertion<R> setMsg(String msg, Object...args) {
164      super.setMsg(msg, args);
165      return this;
166   }
167
168   @Override /* Overridden from Assertion */
169   public FluentBooleanAssertion<R> setOut(PrintStream value) {
170      super.setOut(value);
171      return this;
172   }
173
174   @Override /* Overridden from Assertion */
175   public FluentBooleanAssertion<R> setSilent() {
176      super.setSilent();
177      return this;
178   }
179
180   @Override /* Overridden from Assertion */
181   public FluentBooleanAssertion<R> setStdOut() {
182      super.setStdOut();
183      return this;
184   }
185
186   @Override /* Overridden from Assertion */
187   public FluentBooleanAssertion<R> setThrowable(Class<? extends RuntimeException> value) {
188      super.setThrowable(value);
189      return this;
190   }
191}