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
019import java.io.*;
020import java.util.function.*;
021
022import org.apache.juneau.common.utils.*;
023import org.apache.juneau.cp.*;
024import org.apache.juneau.internal.*;
025import org.apache.juneau.serializer.*;
026
027/**
028 * Used for fluent assertion calls against comparable objects.
029 *
030 * <h5 class='section'>Test Methods:</h5>
031 * <p>
032 * <ul class='javatree'>
033 *    <li class='jc'>{@link FluentComparableAssertion}
034 *    <ul class='javatreec'>
035 *       <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)}
036 *       <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)}
037 *       <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)}
038 *       <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)}
039 *       <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)}
040 *    </ul>
041 *    <li class='jc'>{@link FluentObjectAssertion}
042 *    <ul class='javatreec'>
043 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
044 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
045 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
046 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
047 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
048 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
049 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
050 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
051 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
052 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
054 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
055 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
056 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
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(WriterSerializer) asString(WriterSerializer)}
069 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
070 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
071 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
072 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
073 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
074 * </ul>
075 * </ul>
076 *
077 * <h5 class='section'>Configuration Methods:</h5>
078 * <p>
079 * <ul class='javatree'>
080 *    <li class='jc'>{@link Assertion}
081 *    <ul class='javatreec'>
082 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
083 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
084 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
085 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
086 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
087 *    </ul>
088 * </ul>
089 *
090 * <h5 class='section'>See Also:</h5><ul>
091 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem Overview</a>
092 * </ul>
093 *
094 * @param <T> The value type
095 * @param <R> The return type.
096 */
097@SuppressWarnings("rawtypes")
098public class FluentComparableAssertion<T extends Comparable,R> extends FluentObjectAssertion<T,R> {
099
100   //-----------------------------------------------------------------------------------------------------------------
101   // Static
102   //-----------------------------------------------------------------------------------------------------------------
103
104   private static final Messages MESSAGES = Messages.of(FluentComparableAssertion.class, "Messages");
105   private static final String
106      MSG_valueWasNotGreaterThanExpected = MESSAGES.getString("valueWasNotGreaterThanExpected"),
107      MSG_valueWasNotGreaterOrEqualsToExpected = MESSAGES.getString("valueWasNotGreaterOrEqualsToExpected"),
108      MSG_valueWasNotLessThanExpected = MESSAGES.getString("valueWasNotLessThanExpected"),
109      MSG_valueWasNotLessOrEqualsToExpected = MESSAGES.getString("valueWasNotLessOrEqualsToExpected");
110
111   //-----------------------------------------------------------------------------------------------------------------
112   // Instance
113   //-----------------------------------------------------------------------------------------------------------------
114
115   /**
116    * Constructor.
117    *
118    * @param value
119    *    The object being tested.
120    *    <br>Can be <jk>null</jk>.
121    * @param returns
122    *    The object to return after a test method is called.
123    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
124    * used on the same assertion.
125    */
126   public FluentComparableAssertion(T value, R returns) {
127      this(null, value, returns);
128   }
129
130   /**
131    * Chained constructor.
132    *
133    * <p>
134    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
135    *
136    * @param creator
137    *    The assertion that created this assertion.
138    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
139    * @param value
140    *    The object being tested.
141    *    <br>Can be <jk>null</jk>.
142    * @param returns
143    *    The object to return after a test method is called.
144    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
145    * used on the same assertion.
146    */
147   public FluentComparableAssertion(Assertion creator, T value, R returns) {
148      super(creator, value, returns);
149   }
150
151   //-----------------------------------------------------------------------------------------------------------------
152   // Transform methods
153   //-----------------------------------------------------------------------------------------------------------------
154
155   @Override /* FluentObjectAssertion */
156   public FluentComparableAssertion<T,R> asTransformed(Function<T,T> function) {  // NOSONAR - Intentional.
157      return new FluentComparableAssertion<>(this, function.apply(orElse(null)), returns());
158   }
159
160   //-----------------------------------------------------------------------------------------------------------------
161   // Test methods
162   //-----------------------------------------------------------------------------------------------------------------
163
164   /**
165    * Asserts that the value is greater than the specified value.
166    *
167    * @param value The values to check against.
168    * @return The fluent return object.
169    * @throws AssertionError If assertion failed.
170    */
171   public R isGt(Comparable value) throws AssertionError {
172      Utils.assertArgNotNull("value", value);
173      if (value().compareTo(value) <= 0)
174         throw error(MSG_valueWasNotGreaterThanExpected, value, value());
175      return returns();
176   }
177
178   /**
179    * Asserts that the value is greater than or equal to the specified value.
180    *
181    * @param value The values to check against.
182    * @return The fluent return object.
183    * @throws AssertionError If assertion failed.
184    */
185   public R isGte(Comparable value) throws AssertionError {
186      Utils.assertArgNotNull("value", value);
187      if (value().compareTo(value) < 0)
188            throw error(MSG_valueWasNotGreaterOrEqualsToExpected, value, value());
189      return returns();
190   }
191
192   /**
193    * Asserts that the value is less than the specified value.
194    *
195    * @param value The values to check against.
196    * @return The fluent return object.
197    * @throws AssertionError If assertion failed.
198    */
199   public R isLt(Comparable value) throws AssertionError {
200      Utils.assertArgNotNull("value", value);
201      if (value().compareTo(value) >= 0)
202            throw error(MSG_valueWasNotLessThanExpected, value, value());
203      return returns();
204   }
205
206   /**
207    * Asserts that the value is less than or equals to the specified value.
208    *
209    * @param value The values to check against.
210    * @return The fluent return object.
211    * @throws AssertionError If assertion failed.
212    */
213   public R isLte(Comparable value) throws AssertionError {
214      Utils.assertArgNotNull("value", value);
215      if (value().compareTo(value) > 0)
216            throw error(MSG_valueWasNotLessOrEqualsToExpected, value, value());
217      return returns();
218   }
219
220   /**
221    * Asserts that the value is between (inclusive) the specified upper and lower values.
222    *
223    * @param lower The lower value to check against.
224    * @param upper The upper value to check against.
225    * @return The fluent return object.
226    * @throws AssertionError If assertion failed.
227    */
228   public R isBetween(Comparable lower, Comparable upper) throws AssertionError {
229      isExists();
230      Utils.assertArgNotNull("lower", lower);
231      Utils.assertArgNotNull("upper", upper);
232      isLte(upper);
233      isGte(lower);
234      return returns();
235   }
236
237   //-----------------------------------------------------------------------------------------------------------------
238   // Fluent setters
239   //-----------------------------------------------------------------------------------------------------------------
240   @Override /* Overridden from Assertion */
241   public FluentComparableAssertion<T,R> setMsg(String msg, Object...args) {
242      super.setMsg(msg, args);
243      return this;
244   }
245
246   @Override /* Overridden from Assertion */
247   public FluentComparableAssertion<T,R> setOut(PrintStream value) {
248      super.setOut(value);
249      return this;
250   }
251
252   @Override /* Overridden from Assertion */
253   public FluentComparableAssertion<T,R> setSilent() {
254      super.setSilent();
255      return this;
256   }
257
258   @Override /* Overridden from Assertion */
259   public FluentComparableAssertion<T,R> setStdOut() {
260      super.setStdOut();
261      return this;
262   }
263
264   @Override /* Overridden from Assertion */
265   public FluentComparableAssertion<T,R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
266      super.setThrowable(value);
267      return this;
268   }
269}