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 static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.io.*;
022import java.util.function.*;
023
024import org.apache.juneau.commons.function.*;
025
026
027/**
028 * Used for fluent assertion calls against comparable objects.
029 *
030 * <h2 class='section'>Test Methods:</h2>
031 * <ul class='javatree'>
032 *    <li class='jc'>{@link FluentComparableAssertion}
033 *    <ul class='javatreec'>
034 *       <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)}
035 *       <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)}
036 *       <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)}
037 *       <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)}
038 *       <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)}
039 *    </ul>
040 *    <li class='jc'>{@link FluentObjectAssertion}
041 *    <ul class='javatreec'>
042 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
043 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
044 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
045 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
046 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
047 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
048 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
049 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
050 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
051 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
052 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
054 *    </ul>
055 * </ul>
056 *
057 * <h2 class='section'>Transform Methods:</h2>
058 * <ul class='javatree'>
059 *    <li class='jc'>{@link FluentObjectAssertion}
060 *    <ul class='javatreec'>
061 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
062 *       <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)}
063 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)}
064 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
065 * </ul>
066 * </ul>
067 *
068 * <h2 class='section'>Configuration Methods:</h2>
069 * <ul class='javatree'>
070 *    <li class='jc'>{@link Assertion}
071 *    <ul class='javatreec'>
072 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
073 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
074 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
075 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
076 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
077 *    </ul>
078 * </ul>
079 *
080 * <h2 class='section'>See Also:</h2><ul>
081 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a>
082 * </ul>
083 *
084 * @param <T> The value type
085 * @param <R> The return type.
086 */
087@SuppressWarnings({
088   "java:S115", // Constants use UPPER_snakeCase naming convention
089   "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants
090   "rawtypes", // Raw Comparable type necessary for flexible comparison API without self-referencing type constraints
091   "unchecked" // Type erasure requires cast for comparable comparison
092})
093public class FluentComparableAssertion<T extends Comparable,R> extends FluentObjectAssertion<T,R> {
094
095   private static final Messages MESSAGES = Messages.of(FluentComparableAssertion.class, "Messages");
096   private static final String MSG_valueWasNotGreaterThanExpected = MESSAGES.getString("valueWasNotGreaterThanExpected");
097   private static final String MSG_valueWasNotGreaterOrEqualsToExpected = MESSAGES.getString("valueWasNotGreaterOrEqualsToExpected");
098   private static final String MSG_valueWasNotLessThanExpected = MESSAGES.getString("valueWasNotLessThanExpected");
099   private static final String MSG_valueWasNotLessOrEqualsToExpected = MESSAGES.getString("valueWasNotLessOrEqualsToExpected");
100
101   /**
102    * Chained constructor.
103    *
104    * <p>
105    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
106    *
107    * @param creator
108    *    The assertion that created this assertion.
109    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
110    * @param value
111    *    The object being tested.
112    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
113    * @param returns
114    *    The object to return after a test method is called.
115    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
116    * used on the same assertion.
117    */
118   public FluentComparableAssertion(Assertion creator, T value, R returns) {
119      super(creator, value, returns);
120   }
121
122   /**
123    * Constructor.
124    *
125    * @param value
126    *    The object being tested.
127    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
128    * @param returns
129    *    The object to return after a test method is called.
130    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
131    * used on the same assertion.
132    */
133   public FluentComparableAssertion(T value, R returns) {
134      this(null, value, returns);
135   }
136
137   @Override /* Overridden from FluentObjectAssertion */
138   public FluentComparableAssertion<T,R> asTransformed(UnaryOperator<T> function) {
139      return new FluentComparableAssertion<>(this, function.apply(orElse(null)), returns());
140   }
141
142   /**
143    * Asserts that the value is between (inclusive) the specified upper and lower values.
144    *
145    * @param lower The lower value to check against.
146    * @param upper The upper value to check against.
147    * @return The fluent return object.
148    * @throws AssertionError If assertion failed.
149    */
150   public R isBetween(Comparable lower, Comparable upper) throws AssertionError {
151      isExists();
152      reqnn("lower", lower);
153      reqnn("upper", upper);
154      isLte(upper);
155      isGte(lower);
156      return returns();
157   }
158
159   /**
160    * Asserts that the value is greater than the specified value.
161    *
162    * @param value The values to check against.
163    * @return The fluent return object.
164    * @throws AssertionError If assertion failed.
165    */
166   public R isGt(Comparable value) throws AssertionError {
167      reqnn("value", value);
168      if (value().compareTo(value) <= 0)
169         throw error(MSG_valueWasNotGreaterThanExpected, r(value), r(value()));
170      return returns();
171   }
172
173   /**
174    * Asserts that the value is greater than or equal to the specified value.
175    *
176    * @param value The values to check against.
177    * @return The fluent return object.
178    * @throws AssertionError If assertion failed.
179    */
180   public R isGte(Comparable value) throws AssertionError {
181      reqnn("value", value);
182      if (value().compareTo(value) < 0)
183         throw error(MSG_valueWasNotGreaterOrEqualsToExpected, r(value), r(value()));
184      return returns();
185   }
186
187   /**
188    * Asserts that the value is less than the specified value.
189    *
190    * @param value The values to check against.
191    * @return The fluent return object.
192    * @throws AssertionError If assertion failed.
193    */
194   public R isLt(Comparable value) throws AssertionError {
195      reqnn("value", value);
196      if (value().compareTo(value) >= 0)
197         throw error(MSG_valueWasNotLessThanExpected, r(value), r(value()));
198      return returns();
199   }
200
201   /**
202    * Asserts that the value is less than or equals to the specified value.
203    *
204    * @param value The values to check against.
205    * @return The fluent return object.
206    * @throws AssertionError If assertion failed.
207    */
208   public R isLte(Comparable value) throws AssertionError {
209      reqnn("value", value);
210      if (value().compareTo(value) > 0)
211         throw error(MSG_valueWasNotLessOrEqualsToExpected, r(value), r(value()));
212      return returns();
213   }
214
215   @Override /* Overridden from Assertion */
216   public FluentComparableAssertion<T,R> setMsg(String msg, Object...args) {
217      super.setMsg(msg, args);
218      return this;
219   }
220
221   @Override /* Overridden from Assertion */
222   public FluentComparableAssertion<T,R> setOut(PrintStream value) {
223      super.setOut(value);
224      return this;
225   }
226
227   @Override /* Overridden from Assertion */
228   public FluentComparableAssertion<T,R> setSilent() {
229      super.setSilent();
230      return this;
231   }
232
233   @Override /* Overridden from Assertion */
234   public FluentComparableAssertion<T,R> setStdOut() {
235      super.setStdOut();
236      return this;
237   }
238
239   @Override /* Overridden from Assertion */
240   public FluentComparableAssertion<T,R> setThrowable(Class<? extends RuntimeException> value) {
241      super.setThrowable(value);
242      return this;
243   }
244}