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