001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.assertions;
014
015
016import java.io.*;
017import java.util.function.*;
018
019import org.apache.juneau.cp.*;
020import org.apache.juneau.internal.*;
021import org.apache.juneau.serializer.*;
022
023/**
024 * Used for fluent assertion calls against booleans.
025 *
026 * <h5 class='section'>Test Methods:</h5>
027 * <p>
028 * <ul class='javatree'>
029 *    <li class='jc'>{@link FluentBooleanAssertion}
030 *    <ul class='javatreec'>
031 *       <li class='jm'>{@link FluentBooleanAssertion#isTrue() isTrue()}
032 *       <li class='jm'>{@link FluentBooleanAssertion#isFalse() isFalse()}
033 *    </ul>
034 *    <li class='jc'>{@link FluentComparableAssertion}
035 *    <ul class='javatreec'>
036 *       <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)}
037 *       <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)}
038 *       <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)}
039 *       <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)}
040 *       <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)}
041  *   </ul>
042 *    <li class='jc'>{@link FluentObjectAssertion}
043 *    <ul class='javatreec'>
044 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
045 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
046 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
047 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
048 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
049 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
050 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
051 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
052 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
054 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
055 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
056 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
057 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
058 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
059 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
060 *    </ul>
061 * </ul>
062 *
063 * <h5 class='section'>Transform Methods:</h5>
064 * <p>
065 * <ul class='javatree'>
066 *    <li class='jc'>{@link FluentObjectAssertion}
067 *    <ul class='javatreec'>
068 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
069 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
070 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
071 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
072 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
073 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
074 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
075 * </ul>
076 * </ul>
077 *
078 * <h5 class='section'>Configuration Methods:</h5>
079 * <p>
080 * <ul class='javatree'>
081 *    <li class='jc'>{@link Assertion}
082 *    <ul class='javatreec'>
083 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
084 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
085 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
086 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
087 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
088 *    </ul>
089 * </ul>
090 *
091 * <h5 class='section'>See Also:</h5><ul>
092 *    <li class='link'><a class="doclink" href="../../../../index.html#ja.Overview">Overview &gt; juneau-assertions &gt; Overview</a>
093 * </ul>
094 *
095 * @param <R> The return type.
096 */
097@FluentSetters(returns="FluentBooleanAssertion<R>")
098public class FluentBooleanAssertion<R> extends FluentComparableAssertion<Boolean,R> {
099
100   //-----------------------------------------------------------------------------------------------------------------
101   // Static
102   //-----------------------------------------------------------------------------------------------------------------
103
104   private static final Messages MESSAGES = Messages.of(FluentBooleanAssertion.class, "Messages");
105   private static final String
106      MSG_valueWasFalse = MESSAGES.getString("valueWasFalse"),
107      MSG_valueWasTrue = MESSAGES.getString("valueWasTrue");
108
109   //-----------------------------------------------------------------------------------------------------------------
110   // Instance
111   //-----------------------------------------------------------------------------------------------------------------
112
113   /**
114    * Constructor.
115    *
116    * @param value
117    *    The object being tested.
118    *    <br>Can be <jk>null</jk>.
119    * @param returns
120    *    The object to return after a test method is called.
121    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
122    * used on the same assertion.
123    */
124   public FluentBooleanAssertion(Boolean value, R returns) {
125      this(null, value, returns);
126   }
127
128   /**
129    * Chained constructor.
130    *
131    * <p>
132    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
133    *
134    * @param creator
135    *    The assertion that created this assertion.
136    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
137    * @param value
138    *    The object being tested.
139    *    <br>Can be <jk>null</jk>.
140    * @param returns
141    *    The object to return after a test method is called.
142    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
143    * used on the same assertion.
144    */
145   public FluentBooleanAssertion(Assertion creator, Boolean value, R returns) {
146      super(creator, value, returns);
147   }
148
149   //-----------------------------------------------------------------------------------------------------------------
150   // Test methods
151   //-----------------------------------------------------------------------------------------------------------------
152
153   /**
154    * Asserts that the value is true.
155    *
156    * @return The fluent return object.
157    * @throws AssertionError If assertion failed.
158    */
159   public R isTrue() throws AssertionError {
160      if (! value())
161         throw error(MSG_valueWasFalse);
162      return returns();
163   }
164
165   /**
166    * Asserts that the value is false.
167    *
168    * @return The fluent return object.
169    * @throws AssertionError If assertion failed.
170    */
171   public R isFalse() throws AssertionError {
172      if (value())
173         throw error(MSG_valueWasTrue);
174      return returns();
175   }
176
177   //-----------------------------------------------------------------------------------------------------------------
178   // Fluent setters
179   //-----------------------------------------------------------------------------------------------------------------
180
181   // <FluentSetters>
182
183   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
184   public FluentBooleanAssertion<R> setMsg(String msg, Object...args) {
185      super.setMsg(msg, args);
186      return this;
187   }
188
189   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
190   public FluentBooleanAssertion<R> setOut(PrintStream value) {
191      super.setOut(value);
192      return this;
193   }
194
195   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
196   public FluentBooleanAssertion<R> setSilent() {
197      super.setSilent();
198      return this;
199   }
200
201   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
202   public FluentBooleanAssertion<R> setStdOut() {
203      super.setStdOut();
204      return this;
205   }
206
207   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
208   public FluentBooleanAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
209      super.setThrowable(value);
210      return this;
211   }
212
213   // </FluentSetters>
214}