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
015import static org.apache.juneau.common.internal.ArgUtils.*;
016import static org.apache.juneau.internal.ObjectUtils.*;
017import static java.util.Arrays.*;
018
019import java.io.*;
020import java.lang.reflect.*;
021import java.util.*;
022import java.util.function.*;
023
024import org.apache.juneau.*;
025import org.apache.juneau.cp.*;
026import org.apache.juneau.internal.*;
027import org.apache.juneau.serializer.*;
028
029/**
030 * Used for fluent assertion calls against primitive array objects (e.g. <c><jk>int</jk>[]</c>).
031 *
032 * <h5 class='section'>Test Methods:</h5>
033 * <p>
034 * <ul class='javatree'>
035 *    <li class='jc'>{@link FluentPrimitiveArrayAssertion}
036 *    <ul class='javatreec'>
037 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isHas(Object...) isHas(Object...)}
038 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#is(Predicate) is(Predicate)}
039 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAny(Predicate) isAny(Predicate)}
040 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAll(Predicate) isAll(Predicate)}
041 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isEmpty() isEmpty()}
042 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotEmpty() isNotEmpty()}
043 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isSize(int) isSize(int)}
044 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isContains(Object) isContains(Object)}
045 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotContains(Object) isNotContains(Object)}
046 *    </ul>
047 *    <li class='jc'>{@link FluentObjectAssertion}
048 *    <ul class='javatreec'>
049 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
050 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
051 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
052 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
054 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
055 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
056 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
057 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
058 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
059 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
060 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
061 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
062 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
063 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
064 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
065 *    </ul>
066 * </ul>
067 *
068 * <h5 class='section'>Transform Methods:</h5>
069 * <p>
070 * <ul class='javatree'>
071 *    <li class='jc'>{@link FluentPrimitiveArrayAssertion}
072 *    <ul class='javatreec'>
073 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#asItem(int) asItem(int)}
074 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#asLength() asLength()}
075 *    </ul>
076 *    <li class='jc'>{@link FluentObjectAssertion}
077 *    <ul class='javatreec'>
078 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
079 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
080 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
081 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
082 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
083 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
084 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
085 * </ul>
086 * </ul>
087 *
088 * <h5 class='section'>Configuration Methods:</h5>
089 * <p>
090 * <ul class='javatree'>
091 *    <li class='jc'>{@link Assertion}
092 *    <ul class='javatreec'>
093 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
094 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
095 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
096 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
097 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
098 *    </ul>
099 * </ul>
100 *
101 * <h5 class='section'>See Also:</h5><ul>
102 *    <li class='link'><a class="doclink" href="../../../../index.html#ja.Overview">Overview &gt; juneau-assertions &gt; Overview</a>
103 * </ul>
104 *
105 * @param <E> The array element type.
106 * @param <T> The array type.
107 * @param <R> The return type.
108 */
109@FluentSetters(returns="FluentPrimitiveArrayAssertion<E,T,R>")
110public class FluentPrimitiveArrayAssertion<E,T,R> extends FluentObjectAssertion<T,R> {
111
112   //-----------------------------------------------------------------------------------------------------------------
113   // Static
114   //-----------------------------------------------------------------------------------------------------------------
115
116   private static final Map<Class<?>,Function<Object,String>> STRINGIFIERS = new HashMap<>();
117   static {
118      STRINGIFIERS.put(boolean.class, x -> Arrays.toString((boolean[])x));
119      STRINGIFIERS.put(byte.class, x -> Arrays.toString((byte[])x));
120      STRINGIFIERS.put(char.class, x -> Arrays.toString((char[])x));
121      STRINGIFIERS.put(double.class, x -> Arrays.toString((double[])x));
122      STRINGIFIERS.put(float.class, x -> Arrays.toString((float[])x));
123      STRINGIFIERS.put(int.class, x -> Arrays.toString((int[])x));
124      STRINGIFIERS.put(long.class, x -> Arrays.toString((long[])x));
125      STRINGIFIERS.put(short.class, x -> Arrays.toString((short[])x));
126   }
127
128   private static final Messages MESSAGES = Messages.of(FluentPrimitiveArrayAssertion.class, "Messages");
129   static final String
130      MSG_objectWasNotAnArray = MESSAGES.getString("objectWasNotAnArray"),
131      MSG_arrayWasNotEmpty = MESSAGES.getString("arrayWasNotEmpty"),
132      MSG_arrayWasEmpty = MESSAGES.getString("arrayWasEmpty"),
133      MSG_arrayDidNotHaveExpectedSize = MESSAGES.getString("arrayDidNotHaveExpectedSize"),
134      MSG_arrayDidNotContainExpectedValue = MESSAGES.getString("arrayDidNotContainExpectedValue"),
135      MSG_arrayDidNotContainExpectedValueAt = MESSAGES.getString("arrayDidNotContainExpectedValueAt"),
136      MSG_arrayContainedUnexpectedValue = MESSAGES.getString("arrayContainedUnexpectedValue"),
137      MSG_arrayDidntContainAnyMatchingValue = MESSAGES.getString("arrayDidntContainAnyMatchingValue"),
138      MSG_arrayContainedNonMatchingValueAt = MESSAGES.getString("arrayContainedNonMatchingValueAt");
139
140   //-----------------------------------------------------------------------------------------------------------------
141   // Instance
142   //-----------------------------------------------------------------------------------------------------------------
143
144   /**
145    * Constructor.
146    *
147    * @param value
148    *    The object being tested.
149    *    <br>Can be <jk>null</jk>.
150    * @param returns
151    *    The object to return after a test method is called.
152    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
153    * used on the same assertion.
154    */
155   public FluentPrimitiveArrayAssertion(T value, R returns) {
156      this(null, value, returns);
157   }
158
159   /**
160    * Chained constructor.
161    *
162    * <p>
163    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
164    *
165    * @param creator
166    *    The assertion that created this assertion.
167    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
168    * @param value
169    *    The object being tested.
170    *    <br>Can be <jk>null</jk>.
171    * @param returns
172    *    The object to return after a test method is called.
173    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
174    * used on the same assertion.
175    */
176   public FluentPrimitiveArrayAssertion(Assertion creator, T value, R returns) {
177      super(creator, value, returns);
178      if (value != null) {
179         Class<?> c = value.getClass();
180         if (! (c.isArray() && c.getComponentType().isPrimitive()))
181            throw new BasicAssertionError(MSG_objectWasNotAnArray, value.getClass());
182      }
183   }
184
185   //-----------------------------------------------------------------------------------------------------------------
186   // Transform methods
187   //-----------------------------------------------------------------------------------------------------------------
188
189   @Override /* FluentObjectAssertion */
190   public FluentPrimitiveArrayAssertion<E,T,R> asTransformed(Function<T,T> function) {
191      return new FluentPrimitiveArrayAssertion<>(this, function.apply(orElse(null)), returns());
192   }
193
194   @Override /* FluentBaseAssertion */
195   public FluentStringAssertion<R> asString() {
196      return new FluentStringAssertion<>(this, toString(), returns());
197   }
198
199   /**
200    * Returns an object assertion on the item specified at the specified index.
201    *
202    * <p>
203    * If the array is <jk>null</jk> or the index is out-of-bounds, the returned assertion is a null assertion
204    * (meaning {@link FluentAnyAssertion#isExists()} returns <jk>false</jk>).
205    *
206    * @param index The index of the item to retrieve from the array.
207    * @return A new assertion.
208    */
209   public FluentAnyAssertion<E,R> asItem(int index) {
210      return new FluentAnyAssertion<>(this, at(index), returns());
211   }
212
213   /**
214    * Returns an integer assertion on the length of this array.
215    *
216    * <p>
217    * If the array is <jk>null</jk> or the index is out-of-bounds, the returned assertion is a null assertion
218    * (meaning {@link FluentIntegerAssertion#isExists()} returns <jk>false</jk>).
219    *
220    * @return A new assertion.
221    */
222   public FluentIntegerAssertion<R> asLength() {
223      return new FluentIntegerAssertion<>(this, valueIsNull() ? null : Array.getLength(value()), returns());
224   }
225
226   //-----------------------------------------------------------------------------------------------------------------
227   // Test methods
228   //-----------------------------------------------------------------------------------------------------------------
229
230   /**
231    * Asserts that the contents of this list contain the specified values.
232    *
233    * @param entries The expected entries in this list.
234    * @return This object.
235    * @throws AssertionError If assertion failed.
236    */
237   @SuppressWarnings("unchecked")
238   public R isHas(E...entries) throws AssertionError {
239      assertArgNotNull("entries", entries);
240      Predicate<E>[] p = stream(entries).map(AssertionPredicates::eq).toArray(Predicate[]::new);
241      return is(p);
242   }
243
244   /**
245    * Asserts that the contents of this list pass the specified tests.
246    *
247    * @param tests The tests to run.  <jk>null</jk> entries are ignored.
248    * @return This object.
249    * @throws AssertionError If assertion failed.
250    */
251   @SafeVarargs
252   public final R is(Predicate<E>...tests) throws AssertionError {
253      isSize(tests.length);
254      for (int i = 0, j = length2(); i < j; i++) {
255         Predicate<E> t = tests[i];
256         if (t != null)
257            if (! t.test(at(i)))
258               throw error(MSG_arrayDidNotContainExpectedValueAt, i, getFailureMessage(t, at(i)));
259      }
260      return returns();
261   }
262
263   /**
264    * Asserts that at least one value in the array passes the specified test.
265    *
266    * @param test The predicate test.
267    * @return The fluent return object.
268    * @throws AssertionError If assertion failed or value was <jk>null</jk>.
269    */
270   public R isAny(Predicate<E> test) throws AssertionError {
271      assertArgNotNull("test", test);
272      for (int i = 0, j = length2(); i < j; i++)
273         if (test.test(at(i)))
274            return returns();
275      throw error(MSG_arrayDidntContainAnyMatchingValue, value());
276   }
277
278   /**
279    * Asserts that all values in the array pass the specified test.
280    *
281    * @param test The predicate test.
282    * @return The fluent return object.
283    * @throws AssertionError If assertion failed or value was <jk>null</jk>.
284    */
285   public R isAll(Predicate<E> test) throws AssertionError {
286      assertArgNotNull("test", test);
287      for (int i = 0, j = length2(); i < j; i++)
288         if (! test.test(at(i)))
289            throw error(MSG_arrayContainedNonMatchingValueAt, i, getFailureMessage(test, at(i)));
290      return returns();
291   }
292
293   /**
294    * Asserts that the collection exists and is empty.
295    *
296    * @return The fluent return object.
297    * @throws AssertionError If assertion failed.
298    */
299   public R isEmpty() throws AssertionError {
300      if (length2() != 0)
301         throw error(MSG_arrayWasNotEmpty);
302      return returns();
303   }
304
305   /**
306    * Asserts that the collection exists and is not empty.
307    *
308    * @return The fluent return object.
309    * @throws AssertionError If assertion failed.
310    */
311   public R isNotEmpty() throws AssertionError {
312      if (length2() == 0)
313         throw error(MSG_arrayWasEmpty);
314      return returns();
315   }
316
317   /**
318    * Asserts that the collection exists and is the specified size.
319    *
320    * @param size The expected size.
321    * @return The fluent return object.
322    * @throws AssertionError If assertion failed.
323    */
324   public R isSize(int size) throws AssertionError {
325      if (length2() != size)
326         throw error(MSG_arrayDidNotHaveExpectedSize, size, asLength());
327      return returns();
328   }
329
330   /**
331    * Asserts that the array contains the expected entry.
332    *
333    * @param entry The value to check for.
334    * @return The fluent return object.
335    * @throws AssertionError If assertion failed.
336    */
337   public R isContains(E entry) throws AssertionError {
338      for (int i = 0, j = length2(); i < j; i++)
339         if (eq(at(i), entry))
340            return returns();
341      throw error(MSG_arrayDidNotContainExpectedValue, entry, value());
342   }
343
344   /**
345    * Asserts that the array does not contain the expected value.
346    *
347    * @param entry The value to check for.
348    * @return The fluent return object.
349    * @throws AssertionError If assertion failed.
350    */
351   public R isNotContains(E entry) throws AssertionError {
352      for (int i = 0; i < length2(); i++)
353         if (eq(at(i), entry))
354            throw error(MSG_arrayContainedUnexpectedValue, entry, value());
355      return returns();
356   }
357
358   //-----------------------------------------------------------------------------------------------------------------
359   // Fluent setters
360   //-----------------------------------------------------------------------------------------------------------------
361
362   // <FluentSetters>
363
364   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
365   public FluentPrimitiveArrayAssertion<E,T,R> setMsg(String msg, Object...args) {
366      super.setMsg(msg, args);
367      return this;
368   }
369
370   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
371   public FluentPrimitiveArrayAssertion<E,T,R> setOut(PrintStream value) {
372      super.setOut(value);
373      return this;
374   }
375
376   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
377   public FluentPrimitiveArrayAssertion<E,T,R> setSilent() {
378      super.setSilent();
379      return this;
380   }
381
382   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
383   public FluentPrimitiveArrayAssertion<E,T,R> setStdOut() {
384      super.setStdOut();
385      return this;
386   }
387
388   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
389   public FluentPrimitiveArrayAssertion<E,T,R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
390      super.setThrowable(value);
391      return this;
392   }
393
394   // </FluentSetters>
395
396   //-----------------------------------------------------------------------------------------------------------------
397   // Utility methods
398   //-----------------------------------------------------------------------------------------------------------------
399
400   @SuppressWarnings("unchecked")
401   private E at(int index) {
402      return valueIsNull() || index < 0 || index >= length2() ? null : (E)Array.get(value(), index);
403   }
404
405   private int length2() {
406      return Array.getLength(value());
407   }
408
409   @Override
410   public String toString() {
411      if (valueIsNull())
412         return null;
413      return STRINGIFIERS.get(value().getClass().getComponentType()).apply(value());
414   }
415}