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