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.IoUtils.*; 020 021import java.io.*; 022import java.time.*; 023import java.util.*; 024import java.util.stream.*; 025 026import org.apache.juneau.commons.function.*; 027import org.apache.juneau.commons.lang.*; 028 029/** 030 * Main class for creation of assertions for stand-alone testing. 031 * 032 * <p> 033 * Provides assertions for various common POJO types. 034 * 035 * <h5 class='section'>Example:</h5> 036 * <p class='bjava'> 037 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 038 * 039 * <jc>// Assert string is greater than 100 characters and contains "foo".</jc> 040 * <jsm>assertString</jsm>(<jv>myString</jv>) 041 * .length().isGt(100) 042 * .contains(<js>"foo"</js>); 043 * </p> 044 * 045 * <p> 046 * Provides simple testing that {@link Throwable Throwables} are being thrown correctly. 047 * 048 * <h5 class='section'>Example:</h5> 049 * <p class='bjava'> 050 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 051 * 052 * <jc>// Assert that calling doBadCall() causes a RuntimeException.</jc> 053 * <jsm>assertThrown</jsm>(() -> <jv>myPojo</jv>.doBadCall()) 054 * .isType(RuntimeException.<jk>class</jk>) 055 * .message().contains(<js>"Bad thing happened."</js>); 056 * </p> 057 * 058 * <h5 class='section'>See Also:</h5> 059 * <ul> 060 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 061 * </ul> 062 */ 063public class Assertions { 064 065 /** 066 * Performs an assertion on an arbitrary POJO. 067 * 068 * <p> 069 * The distinction between {@link ObjectAssertion} and {@link AnyAssertion} is that the latter supports all 070 * the operations of the former, but adds various transform methods for conversion to specific assertion types. 071 * 072 * <p> 073 * Various transform methods such as {@link FluentListAssertion#asItem(int)} and {@link FluentBeanAssertion#asProperty(String)} 074 * return generic any-assertions so that they can be easily transformed into other assertion types. 075 * 076 * <h5 class='section'>Example:</h5> 077 * <p class='bjava'> 078 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 079 * 080 * <jc>// Asserts that the property 'foo' of a bean is 'bar'.</jc> 081 * <jsm>assertAny</jsm>(<jv>myPojo</jv>) <jc>// Start with AnyAssertion.</jc> 082 * .asBean(MyBean.<jk>class</jk>) <jc>// Transform to BeanAssertion.</jc> 083 * .property(<js>"foo"</js>).is(<js>"bar"</js>); 084 * </p> 085 * 086 * <p> 087 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link AnyAssertion} for supported operations on this type. 088 * 089 * @param <T> The value type. 090 * @param value 091 * The object being tested. 092 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 093 * @return 094 * A new assertion object. 095 * <br>Never <jk>null</jk>. 096 */ 097 public static final <T> AnyAssertion<T> assertAny(T value) { 098 return AnyAssertion.create(value); 099 } 100 101 /** 102 * Performs an assertion on an array of POJOs. 103 * 104 * <h5 class='section'>Example:</h5> 105 * <p class='bjava'> 106 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 107 * 108 * <jc>// Asserts that an Integer array contains [1,2,3].</jc> 109 * Integer[] <jv>array</jv> = {...}; 110 * <jsm>assertArray</jsm>(<jv>array</jv>) 111 * .asJson().is(<js>"[1,2,3]"</js>); 112 * </p> 113 * 114 * <p> 115 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ArrayAssertion} for supported operations on this type. 116 * 117 * @param <E> The value element type. 118 * @param value 119 * The object being tested. 120 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 121 * @return 122 * A new assertion object. 123 * <br>Never <jk>null</jk>. 124 */ 125 public static final <E> ArrayAssertion<E> assertArray(E[] value) { 126 return ArrayAssertion.create(value); 127 } 128 129 /** 130 * Performs an assertion on a Java bean. 131 * 132 * <h5 class='section'>Example:</h5> 133 * <p class='bjava'> 134 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 135 * 136 * <jc>// Asserts that the 'foo' and 'bar' properties of a bean are 1 and 2 respectively.</jc> 137 * <jsm>assertBean</jsm>(<jv>myBean</jv>) 138 * .isType(MyBean.<jk>class</jk>) 139 * .extract(<js>"foo,bar"</js>) 140 * .asJson().is(<js>"{foo:1,bar:2}"</js>); 141 * </p> 142 * 143 * <p> 144 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link BeanAssertion} for supported operations on this type. 145 * 146 * @param <T> The value type. 147 * @param value 148 * The object being tested. 149 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 150 * @return 151 * A new assertion object. 152 * <br>Never <jk>null</jk>. 153 */ 154 public static final <T> BeanAssertion<T> assertBean(T value) { 155 return BeanAssertion.create(value); 156 } 157 158 /** 159 * Performs an assertion on a list of Java beans. 160 * 161 * <h5 class='section'>Example:</h5> 162 * <p class='bjava'> 163 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 164 * 165 * <jc>// Asserts that a bean list has 3 entries with 'foo' property values of 'bar','baz','qux'.</jc> 166 * <jsm>assertBeanList</jsm>(<jv>myListOfBeans</jv>) 167 * .isSize(3) 168 * .property(<js>"foo"</js>) 169 * .is(<js>"bar"</js>,<js>"baz"</js>,<js>"qux"</js>); 170 * </p> 171 * 172 * <p> 173 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link BeanListAssertion} for supported operations on this type. 174 * 175 * @param <E> The element type. 176 * @param value 177 * The object being tested. 178 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 179 * @return 180 * A new assertion object. 181 * <br>Never <jk>null</jk>. 182 */ 183 public static final <E> BeanListAssertion<E> assertBeanList(List<E> value) { 184 return BeanListAssertion.create(value); 185 } 186 187 /** 188 * Performs an assertion on a Boolean. 189 * 190 * <h5 class='section'>Example:</h5> 191 * <p class='bjava'> 192 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 193 * 194 * <jc>// Asserts that a Boolean is not null and TRUE.</jc> 195 * <jsm>assertBoolean</jsm>(<jv>myBoolean</jv>) 196 * .isTrue(); 197 * </p> 198 * 199 * <p> 200 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link BooleanAssertion} for supported operations on this type. 201 * 202 * @param value 203 * The object being tested. 204 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 205 * @return 206 * A new assertion object. 207 * <br>Never <jk>null</jk>. 208 */ 209 public static final BooleanAssertion assertBoolean(Boolean value) { 210 return BooleanAssertion.create(value); 211 } 212 213 /** 214 * Performs an assertion on a boolean array. 215 * 216 * <h5 class='section'>Example:</h5> 217 * <p class='bjava'> 218 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 219 * 220 * <jc>// Asserts that a Boolean array has size of 3 and all entries are TRUE.</jc> 221 * <jsm>assertBooleanArray</jsm>(<jv>myBooleanArray</jv>) 222 * .isSize(3) 223 * .all(<jv>x</jv> -> <jv>x</jv> == <jk>true</jk>); 224 * </p> 225 * 226 * <p> 227 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 228 * 229 * @param value 230 * The object being tested. 231 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 232 * @return 233 * A new assertion object. 234 * <br>Never <jk>null</jk>. 235 */ 236 public static final PrimitiveArrayAssertion<Boolean,boolean[]> assertBooleanArray(boolean[] value) { 237 return PrimitiveArrayAssertion.create(value); 238 } 239 240 /** 241 * Performs an assertion on a byte array. 242 * 243 * <p> 244 * The distinction between {@link #assertByteArray} and {@link #assertBytes} is that the former returns an assertion 245 * more tied to general byte arrays and the latter returns an assertion more tied to dealing with binary streams 246 * that can be decoded or transformed into a string. 247 * 248 * <h5 class='section'>Example:</h5> 249 * <p class='bjava'> 250 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 251 * 252 * <jc>// Asserts that a byte array has size of 3 and all bytes are larger than 10.</jc> 253 * <jsm>assertByteArray</jsm>(<jv>myByteArray</jv>) 254 * .isSize(3) 255 * .all(<jv>x</jv> -> <jv>x</jv> > 10); 256 * </p> 257 * 258 * <p> 259 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 260 * 261 * @param value 262 * The object being tested. 263 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 264 * @return 265 * A new assertion object. 266 * <br>Never <jk>null</jk>. 267 */ 268 public static final PrimitiveArrayAssertion<Byte,byte[]> assertByteArray(byte[] value) { 269 return PrimitiveArrayAssertion.create(value); 270 } 271 272 /** 273 * Performs an assertion on a byte array. 274 * 275 * <p> 276 * The distinction between {@link #assertByteArray} and {@link #assertBytes} is that the former returns an assertion 277 * more tied to general byte arrays and the latter returns an assertion more tied to dealing with binary streams 278 * that can be decoded or transformed into a string. 279 * 280 * <h5 class='section'>Example:</h5> 281 * <p class='bjava'> 282 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 283 * 284 * <jc>// Asserts that the byte array contains the string "foo".</jc> 285 * <jsm>assertBytes</jsm>(<jv>myBytes</jv>) 286 * .asHex().is(<js>"666F6F"</js>); 287 * </p> 288 * 289 * <p> 290 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ByteArrayAssertion} for supported operations on this type. 291 * 292 * @param value 293 * The object being tested. 294 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 295 * @return 296 * A new assertion object. 297 * <br>Never <jk>null</jk>. 298 */ 299 public static final ByteArrayAssertion assertBytes(byte[] value) { 300 return ByteArrayAssertion.create(value); 301 } 302 303 /** 304 * Performs an assertion on the contents of an input stream. 305 * 306 * <h5 class='section'>Example:</h5> 307 * <p class='bjava'> 308 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 309 * 310 * <jc>// Asserts that the stream contains the string "foo".</jc> 311 * <jsm>assertBytes</jsm>(<jv>myStream</jv>) 312 * .asHex().is(<js>"666F6F"</js>); 313 * </p> 314 * 315 * <p> 316 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ByteArrayAssertion} for supported operations on this type. 317 * 318 * @param value 319 * The object being tested. 320 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 321 * <br>Stream is automatically closed. 322 * @return 323 * A new assertion object. 324 * <br>Never <jk>null</jk>. 325 * @throws IOException If thrown while reading contents from stream. 326 */ 327 public static final ByteArrayAssertion assertBytes(InputStream value) throws IOException { 328 return assertBytes(value == null ? null : readBytes(value)); 329 } 330 331 /** 332 * Performs an assertion on a char array. 333 * 334 * <h5 class='section'>Example:</h5> 335 * <p class='bjava'> 336 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 337 * 338 * <jc>// Asserts that the char array contains the string "foo".</jc> 339 * <jsm>assertCharArray</jsm>(<jv>myCharArray</jv>) 340 * .asString().is(<js>"foo"</js>); 341 * </p> 342 * 343 * <p> 344 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 345 * 346 * @param value 347 * The object being tested. 348 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 349 * @return 350 * A new assertion object. 351 * <br>Never <jk>null</jk>. 352 */ 353 public static final PrimitiveArrayAssertion<Character,char[]> assertCharArray(char[] value) { 354 return PrimitiveArrayAssertion.create(value); 355 } 356 357 /** 358 * Performs an assertion on a collection of POJOs. 359 * 360 * <h5 class='section'>Example:</h5> 361 * <p class='bjava'> 362 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 363 * 364 * <jc>// Asserts that a collection of strings has only one entry of 'foo'.</jc> 365 * <jsm>assertCollection</jsm>(<jv>myCollectionOfStrings</jv>) 366 * .isSize(1) 367 * .contains(<js>"foo"</js>); 368 * </p> 369 * 370 * <p> 371 * In general, use {@link #assertList(List)} if you're performing an assertion on a list since {@link ListAssertion} 372 * provides more functionality than {@link CollectionAssertion}. 373 * 374 * <p> 375 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link CollectionAssertion} for supported operations on this type. 376 * 377 * @param <E> The element type. 378 * @param value 379 * The object being tested. 380 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 381 * @return 382 * A new assertion object. 383 * <br>Never <jk>null</jk>. 384 */ 385 public static final <E> CollectionAssertion<E> assertCollection(Collection<E> value) { 386 return CollectionAssertion.create(value); 387 } 388 389 /** 390 * Performs an assertion on a Comparable. 391 * 392 * <h5 class='section'>Example:</h5> 393 * <p class='bjava'> 394 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 395 * 396 * <jc>// Asserts a comparable is less than another comparable.</jc> 397 * <jsm>assertComparable</jsm>(<jv>myComparable</jv>) 398 * .isLt(<jv>anotherComparable</jv>); 399 * </p> 400 * 401 * <p> 402 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ComparableAssertion} for supported operations on this type. 403 * 404 * @param <T> The value type. 405 * @param value 406 * The object being tested. 407 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 408 * @return 409 * A new assertion object. 410 * <br>Never <jk>null</jk>. 411 */ 412 public static final <T extends Comparable<T>> ComparableAssertion<T> assertComparable(T value) { 413 return ComparableAssertion.create(value); 414 } 415 416 /** 417 * Performs an assertion on a Date. 418 * 419 * <h5 class='section'>Example:</h5> 420 * <p class='bjava'> 421 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 422 * 423 * <jc>// Asserts the specified date is after the current date.</jc> 424 * <jsm>assertDate</jsm>(<jv>myDate</jv>) 425 * .isAfterNow(); 426 * </p> 427 * 428 * <p> 429 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link DateAssertion} for supported operations on this type. 430 * 431 * @param value 432 * The object being tested. 433 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 434 * @return 435 * A new assertion object. 436 * <br>Never <jk>null</jk>. 437 */ 438 public static final DateAssertion assertDate(Date value) { 439 return DateAssertion.create(value); 440 } 441 442 /** 443 * Performs an assertion on a double array. 444 * 445 * <h5 class='section'>Example:</h5> 446 * <p class='bjava'> 447 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 448 * 449 * <jc>// Asserts that a double array is at least size 100 and all values are greater than 1000.</jc> 450 * <jsm>assertDoubleArray</jsm>(<jv>myDoubleArray</jv>) 451 * .size().isGte(100f) 452 * .all(<jv>x</jv> -> <jv>x</jv> > 1000f); 453 * </p> 454 * 455 * <p> 456 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 457 * 458 * @param value 459 * The object being tested. 460 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 461 * @return 462 * A new assertion object. 463 * <br>Never <jk>null</jk>. 464 */ 465 public static final PrimitiveArrayAssertion<Double,double[]> assertDoubleArray(double[] value) { 466 return PrimitiveArrayAssertion.create(value); 467 } 468 469 /** 470 * Performs an assertion on a float array. 471 * 472 * <h5 class='section'>Example:</h5> 473 * <p class='bjava'> 474 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 475 * 476 * <jc>// Asserts that a float array is at least size 100 and all values are greater than 1000.</jc> 477 * <jsm>assertFloatArray</jsm>(<jv>myFloatArray</jv>) 478 * .size().isGte(100f) 479 * .all(<jv>x</jv> -> <jv>x</jv> > 1000f); 480 * </p> 481 * 482 * <p> 483 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 484 * 485 * @param value 486 * The object being tested. 487 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 488 * @return 489 * A new assertion object. 490 * <br>Never <jk>null</jk>. 491 */ 492 public static final PrimitiveArrayAssertion<Float,float[]> assertFloatArray(float[] value) { 493 return PrimitiveArrayAssertion.create(value); 494 } 495 496 /** 497 * Performs an assertion on an int array. 498 * 499 * <h5 class='section'>Example:</h5> 500 * <p class='bjava'> 501 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 502 * 503 * <jc>// Asserts that a double array is at least size 100 and all values are greater than 1000.</jc> 504 * <jsm>assertIntArray</jsm>(<jv>myIntArray</jv>) 505 * .size().isGte(100) 506 * .all(<jv>x</jv> -> <jv>x</jv> > 1000); 507 * </p> 508 * 509 * <p> 510 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 511 * 512 * @param value 513 * The object being tested. 514 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 515 * @return 516 * A new assertion object. 517 * <br>Never <jk>null</jk>. 518 */ 519 public static final PrimitiveArrayAssertion<Integer,int[]> assertIntArray(int[] value) { 520 return PrimitiveArrayAssertion.create(value); 521 } 522 523 /** 524 * Performs an assertion on an Integer. 525 * 526 * <h5 class='section'>Example:</h5> 527 * <p class='bjava'> 528 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 529 * 530 * <jc>// Assert that an HTTP response status code is 200 or 404.</jc> 531 * <jsm>assertInteger</jsm>(<jv>httpReponse</jv>) 532 * .isAny(200,404); 533 * </p> 534 * 535 * <p> 536 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link IntegerAssertion} for supported operations on this type. 537 * 538 * @param value 539 * The object being tested. 540 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 541 * @return 542 * A new assertion object. 543 * <br>Never <jk>null</jk>. 544 */ 545 public static final IntegerAssertion assertInteger(Integer value) { 546 return IntegerAssertion.create(value); 547 } 548 549 /** 550 * Performs an assertion on a list of POJOs. 551 * 552 * <h5 class='section'>Example:</h5> 553 * <p class='bjava'> 554 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 555 * 556 * <jc>// Assert that the first entry in a list is "{foo:'bar'}" when serialized to simplified JSON.</jc> 557 * <jsm>assertList</jsm>(<jv>myList</jv>) 558 * .item(0) 559 * .asJson().is(<js>"{foo:'bar'}"</js>); 560 * </p> 561 * 562 * <p> 563 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ListAssertion} for supported operations on this type. 564 * 565 * @param <E> The element type. 566 * @param value 567 * The object being tested. 568 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 569 * @return 570 * A new assertion object. 571 * <br>Never <jk>null</jk>. 572 */ 573 public static final <E> ListAssertion<E> assertList(List<E> value) { 574 return ListAssertion.create(value); 575 } 576 577 /** 578 * Performs an assertion on a stream of POJOs. 579 * 580 * <h5 class='section'>Example:</h5> 581 * <p class='bjava'> 582 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 583 * 584 * <jc>// Assert that the first entry in a list is "{foo:'bar'}" when serialized to simplified JSON.</jc> 585 * <jsm>assertList</jsm>(<jv>myStream</jv>) 586 * .item(0) 587 * .asJson().is(<js>"{foo:'bar'}"</js>); 588 * </p> 589 * 590 * <p> 591 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ListAssertion} for supported operations on this type. 592 * 593 * @param <E> The element type. 594 * @param value 595 * The object being tested. 596 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 597 * @return 598 * A new assertion object. 599 * <br>Never <jk>null</jk>. 600 */ 601 public static final <E> ListAssertion<E> assertList(Stream<E> value) { 602 return ListAssertion.create(value); 603 } 604 605 /** 606 * Performs an assertion on a Long. 607 * 608 * <h5 class='section'>Example:</h5> 609 * <p class='bjava'> 610 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 611 * 612 * <jc>// Throw a BadReqest if an HTTP response length is greater than 100k.</jc> 613 * <jsm>assertLong</jsm>(<jv>responseLength</jv>) 614 * .throwable(BadRequest.<jk>class</jk>) 615 * .msg(<js>"Request is too large"</js>) 616 * .isLt(100000); 617 * </p> 618 * 619 * <p> 620 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link LongAssertion} for supported operations on this type. 621 * 622 * @param value 623 * The object being tested. 624 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 625 * @return 626 * A new assertion object. 627 * <br>Never <jk>null</jk>. 628 */ 629 public static final LongAssertion assertLong(Long value) { 630 return LongAssertion.create(value); 631 } 632 633 /** 634 * Performs an assertion on a long array. 635 * 636 * <h5 class='section'>Example:</h5> 637 * <p class='bjava'> 638 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 639 * 640 * <jc>// Asserts that a long array is at least size 100 and all values are greater than 1000.</jc> 641 * <jsm>assertLongArray</jsm>(<jv>myLongArray</jv>) 642 * .size().isGte(100) 643 * .all(<jv>x</jv> -> <jv>x</jv> > 1000); 644 * </p> 645 * 646 * <p> 647 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 648 * 649 * @param value 650 * The object being tested. 651 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 652 * @return 653 * A new assertion object. 654 * <br>Never <jk>null</jk>. 655 */ 656 public static final PrimitiveArrayAssertion<Long,long[]> assertLongArray(long[] value) { 657 return PrimitiveArrayAssertion.create(value); 658 } 659 660 /** 661 * Performs an assertion on a map. 662 * 663 * <h5 class='section'>Example:</h5> 664 * <p class='bjava'> 665 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 666 * 667 * <jc>// Assert the specified map is a HashMap and contains the key "foo".</jc> 668 * <jsm>assertMap</jsm>(<jv>myMap</jv>) 669 * .isType(HashMap.<jk>class</jk>) 670 * .containsKey(<js>"foo"</js>); 671 * </p> 672 * 673 * <p> 674 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link MapAssertion} for supported operations on this type. 675 * 676 * @param <K> The key type. 677 * @param <V> The value type. 678 * @param value 679 * The object being tested. 680 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 681 * @return 682 * A new assertion object. 683 * <br>Never <jk>null</jk>. 684 */ 685 public static final <K,V> MapAssertion<K,V> assertMap(Map<K,V> value) { 686 return MapAssertion.create(value); 687 } 688 689 /** 690 * Performs an assertion on a Java Object. 691 * 692 * <h5 class='section'>Example:</h5> 693 * <p class='bjava'> 694 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 695 * 696 * <jc>// Asserts the specified POJO is of type MyBean and is "{foo:'bar'}" </jc> 697 * <jc>// when serialized to Simplified JSON.</jc> 698 * <jsm>assertObject</jsm>(<jv>myPojo</jv>) 699 * .isType(MyBean.<jk>class</jk>) 700 * .asJson().is(<js>"{foo:'bar'}"</js>); 701 * </p> 702 * 703 * <p> 704 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ObjectAssertion} for supported operations on this type. 705 * 706 * @param <T> The value type. 707 * @param value 708 * The object being tested. 709 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 710 * @return 711 * A new assertion object. 712 * <br>Never <jk>null</jk>. 713 */ 714 public static final <T> ObjectAssertion<T> assertObject(T value) { 715 return ObjectAssertion.create(value); 716 } 717 718 /** 719 * Performs an assertion on a Java Object wrapped in an Optional. 720 * 721 * <h5 class='section'>Example:</h5> 722 * <p class='bjava'> 723 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 724 * 725 * <jc>// Asserts the specified POJO is of type MyBean and is "{foo:'bar'}" </jc> 726 * <jc>// when serialized to Simplified JSON.</jc> 727 * <jsm>assertOptional</jsm>(<jv>opt</jv>) 728 * .isType(MyBean.<jk>class</jk>) 729 * .asJson().is(<js>"{foo:'bar'}"</js>); 730 * </p> 731 * 732 * <p> 733 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link AnyAssertion} for supported operations on this type. 734 * 735 * @param <T> The value type. 736 * @param value 737 * The object being tested. 738 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 739 * @return 740 * A new assertion object. 741 * <br>Never <jk>null</jk>. 742 */ 743 public static final <T> AnyAssertion<T> assertOptional(Optional<T> value) { 744 return AnyAssertion.create(value.orElse(null)); 745 } 746 747 /** 748 * Performs an assertion on the contents of a Reader. 749 * 750 * <h5 class='section'>Example:</h5> 751 * <p class='bjava'> 752 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 753 * 754 * <jc>// Asserts the contents of the Reader contains "foo".</jc> 755 * <jsm>assertReader</jsm>(<jv>myReader</jv>) 756 * .contains(<js>"foo"</js>); 757 * </p> 758 * 759 * <p> 760 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link StringAssertion} for supported operations on this type. 761 * 762 * @param value 763 * The object being tested. 764 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 765 * <br>Reader is automatically closed. 766 * @return 767 * A new assertion object. 768 * <br>Never <jk>null</jk>. 769 * @throws IOException If thrown while reading contents from reader. 770 */ 771 public static final StringAssertion assertReader(Reader value) throws IOException { 772 return assertString(read(value)); 773 } 774 775 /** 776 * Performs an assertion on a short array. 777 * 778 * <h5 class='section'>Example:</h5> 779 * <p class='bjava'> 780 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 781 * 782 * <jc>// Asserts that a float array is at least size 10 and all values are greater than 100.</jc> 783 * <jsm>assertShortArray</jsm>(<jv>myShortArray</jv>) 784 * .size().isGte(10) 785 * .all(<jv>x</jv> -> <jv>x</jv> > 100); 786 * </p> 787 * 788 * <p> 789 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link PrimitiveArrayAssertion} for supported operations on this type. 790 * 791 * @param value 792 * The object being tested. 793 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 794 * @return 795 * A new assertion object. 796 * <br>Never <jk>null</jk>. 797 */ 798 public static final PrimitiveArrayAssertion<Short,short[]> assertShortArray(short[] value) { 799 return PrimitiveArrayAssertion.create(value); 800 } 801 802 /** 803 * Performs an assertion on a String. 804 * 805 * <h5 class='section'>Example:</h5> 806 * <p class='bjava'> 807 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 808 * 809 * <jc>// Asserts a string is at least 100 characters long and contains "foo".</jc> 810 * <jsm>assertString</jsm>(<jv>myString</jv>) 811 * .size().isGte(100) 812 * .contains(<js>"foo"</js>); 813 * </p> 814 * 815 * <p> 816 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link StringAssertion} for supported operations on this type. 817 * 818 * @param value 819 * The object being tested. 820 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 821 * @return 822 * A new assertion object. 823 * <br>Never <jk>null</jk>. 824 */ 825 public static final StringAssertion assertString(Object value) { 826 if (value instanceof Optional<?> value2) 827 value = value2.orElse(null); 828 return StringAssertion.create(value); 829 } 830 831 /** 832 * Performs an assertion on a list of Strings. 833 * 834 * <h5 class='section'>Example:</h5> 835 * <p class='bjava'> 836 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 837 * 838 * <jc>// Asserts a list of strings contain "foo,bar,baz" after trimming all and joining.</jc> 839 * <jsm>assertStringList</jsm>(<jv>myListOfStrings</jv>) 840 * .isSize(3) 841 * .trim() 842 * .join(<js>","</js>) 843 * .is(<js>"foo,bar,baz"</js>); 844 * </p> 845 * 846 * <p> 847 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link StringListAssertion} for supported operations on this type. 848 * 849 * @param value 850 * The object being tested. 851 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 852 * @return 853 * A new assertion object. 854 * <br>Never <jk>null</jk>. 855 */ 856 public static final StringListAssertion assertStringList(List<String> value) { 857 return StringListAssertion.create(value); 858 } 859 860 /** 861 * Performs an assertion on a Throwable. 862 * 863 * <h5 class='section'>Example:</h5> 864 * <p class='bjava'> 865 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 866 * 867 * <jc>// Asserts a throwable is a RuntimeException containing 'foobar' in the message.</jc> 868 * <jsm>assertThrowable</jsm>(<jv>throwable</jv>) 869 * .isExactType(RuntimeException.<jk>class</jk>) 870 * .message().contains(<js>"foobar"</js>); 871 * </p> 872 * 873 * <p> 874 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ThrowableAssertion} for supported operations on this type. 875 * 876 * @param <T> The value type. 877 * @param value 878 * The object being tested. 879 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 880 * @return 881 * A new assertion object. 882 * <br>Never <jk>null</jk>. 883 */ 884 public static final <T extends Throwable> ThrowableAssertion<T> assertThrowable(T value) { 885 return ThrowableAssertion.create(value); 886 } 887 888 /** 889 * Executes an arbitrary snippet of code and captures anything thrown from it as a Throwable assertion. 890 * 891 * <h5 class='section'>Example:</h5> 892 * <p class='bjava'> 893 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 894 * 895 * <jc>// Asserts that the specified method throws a RuntimeException containing "foobar" in the message. </jc> 896 * <jsm>assertThrown</jsm>(()-><jv>foo</jv>.getBar()) 897 * .isType(RuntimeException.<jk>class</jk>) 898 * .message().contains(<js>"foobar"</js>); 899 * </p> 900 * 901 * @param snippet The snippet of code to execute. 902 * @return A new assertion object. Never <jk>null</jk>. 903 */ 904 @SuppressWarnings({ 905 "java:S1181" // Need to catch Throwable to handle all exception types including Error 906 }) 907 public static final ThrowableAssertion<Throwable> assertThrown(Snippet snippet) { 908 try { 909 snippet.run(); 910 } catch (Throwable e) { 911 return assertThrowable(e); 912 } 913 return assertThrowable(null); 914 } 915 916 /** 917 * Performs an assertion on a Version. 918 * 919 * <h5 class='section'>Example:</h5> 920 * <p class='bjava'> 921 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 922 * 923 * <jc>// Asserts the specified major version is at least 2.</jc> 924 * <jsm>assertVersion</jsm>(<jv>version</jv>) 925 * .major().isGte(2); 926 * </p> 927 * 928 * <p> 929 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link VersionAssertion} for supported operations on this type. 930 * 931 * @param value 932 * The object being tested. 933 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 934 * @return 935 * A new assertion object. 936 * <br>Never <jk>null</jk>. 937 */ 938 public static final VersionAssertion assertVersion(Version value) { 939 return VersionAssertion.create(value); 940 } 941 942 /** 943 * Performs an assertion on a ZonedDateTime. 944 * 945 * <h5 class='section'>Example:</h5> 946 * <p class='bjava'> 947 * <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*; 948 * 949 * <jc>// Asserts the specified date is after the current date.</jc> 950 * <jsm>assertZonedDateTime</jsm>(<jv>myZonedDateTime</jv>) 951 * .isAfterNow(); 952 * </p> 953 * 954 * <p> 955 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> for general assertion usage and {@link ZonedDateTimeAssertion} for supported operations on this type. 956 * 957 * @param value 958 * The object being tested. 959 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 960 * @return 961 * A new assertion object. 962 * <br>Never <jk>null</jk>. 963 */ 964 public static final ZonedDateTimeAssertion assertZonedDateTime(ZonedDateTime value) { 965 return ZonedDateTimeAssertion.create(value); 966 } 967 968 /** 969 * Constructor. 970 */ 971 protected Assertions() {} // HTT - utility class; instantiation via subclass is theoretical 972}