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