This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 19e8032 JUnit tests.
19e8032 is described below
commit 19e80327b3cb3120a93ea364476f8a3f6b8ec0d7
Author: JamesBognar <[email protected]>
AuthorDate: Fri Jul 16 14:51:14 2021 -0400
JUnit tests.
---
.../org/apache/juneau/assertions/Assertions.java | 316 +++++++++++----------
1 file changed, 162 insertions(+), 154 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
index 1eb0b9c..4b05b84 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
@@ -32,234 +32,256 @@ public class Assertions {
MSG_argumentCannotBeNull =
MESSAGES.getString("argumentCannotBeNull"),
MSG_exceptionNotOfExpectedType =
MESSAGES.getString("exceptionNotOfExpectedType");
+
//-----------------------------------------------------------------------------------------------------------------
+ // Fluent assertions
+
//-----------------------------------------------------------------------------------------------------------------
+
/**
- * Used for assertion calls against {@link Date} objects.
+ * Used for assertion calls against Java object arrays.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified date is after the current
date.</jc>
- * <jsm>assertDate</jsm>(<jv>myDate</jv>).isAfterNow();
+ * String[] <jv>array</jv> = {<js>"foo"</js>};
+ * <jsm>assertArray</jsm>(<jv>array</jv>).isSize(1);
* </p>
*
- * @param value The date being wrapped.
- * @return A new {@link DateAssertion} object. Never <jk>null</jk>.
+ * @param value The object being wrapped.
+ * @return A new {@link ArrayAssertion} object. Never <jk>null</jk>.
*/
- public static final DateAssertion assertDate(Date value) {
- return DateAssertion.create(value);
+ public static final <E> ArrayAssertion<E> assertArray(E[] value) {
+ return ArrayAssertion.create(value);
}
/**
- * Used for assertion calls against {@link Version} objects.
+ * Used for assertion calls against Java beans.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified major version is greater than
2.</jc>
- *
<jsm>assertVersion</jsm>(<jv>version</jv>).major().isGreaterThan(2);
+ * <jc>// Validates the specified POJO is the specified type and
serializes to the specified value.</jc>
+ *
<jsm>assertBean</jsm>(<jv>myBean</jv>).isType(MyBean.<jk>class</jk>).fields(<js>"foo"</js>).asJson().is(<js>"{foo:'bar'}"</js>);
* </p>
*
- * @param value The version object being wrapped.
- * @return A new {@link VersionAssertion} object. Never <jk>null</jk>.
+ * @param value The object being wrapped.
+ * @return A new {@link BeanAssertion} object. Never <jk>null</jk>.
*/
- public static final VersionAssertion assertVersion(Version value) {
- return VersionAssertion.create(value);
+ public static final <V> BeanAssertion<V> assertBean(V value) {
+ return BeanAssertion.create(value);
}
/**
- * Used for assertion calls against {@link ZonedDateTime} objects.
+ * Used for assertion calls against lists of Java beans.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified date is after the current
date.</jc>
- * <jsm>assertZonedDateTime</jsm>(<jv>byZdt</jv>).isAfterNow();
+ * <jc>// Validates the specified list contains 3 beans with the
specified values for the 'foo' property.</jc>
+ * <jsm>assertBeanList</jsm>(<jv>myBeanList</jv>)
+ * .property(<js>"foo"</js>)
+ * .is(<js>"bar"</js>,<js>"baz"</js>,<js>"qux"</js>);
* </p>
*
- * @param value The date being wrapped.
- * @return A new {@link ZonedDateTimeAssertion} object. Never
<jk>null</jk>.
+ * @param value The object being wrapped.
+ * @return A new {@link BeanListAssertion} object. Never <jk>null</jk>.
*/
- public static final ZonedDateTimeAssertion
assertZonedDateTime(ZonedDateTime value) {
- return ZonedDateTimeAssertion.create(value);
+ public static final <E> BeanListAssertion<E> assertBeanList(List<E>
value) {
+ return BeanListAssertion.create(value);
}
/**
- * Used for assertion calls against integers.
+ * Used for assertion calls against boolean objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the response status code is 200 or 404.</jc>
- * <jsm>assertInteger</jsm>(<jv>httpReponse<jv>).isAny(200,404);
+ * <jc>// Validates that the specified boolean object exists and
is true.</jc>
+ * <jsm>assertBoolean</jsm>(<jv>myBoolean</jv>).exists().isTrue();
* </p>
*
- * @param value The object being wrapped.
- * @return A new {@link IntegerAssertion} object. Never <jk>null</jk>.
+ * @param value The boolean being wrapped.
+ * @return A new {@link BooleanAssertion} object. Never <jk>null</jk>.
*/
- public static final IntegerAssertion assertInteger(Integer value) {
- return IntegerAssertion.create(value);
+ public static final BooleanAssertion assertBoolean(Boolean value) {
+ return BooleanAssertion.create(value);
}
/**
- * Used for assertion calls against longs.
+ * Used for assertion calls against byte arrays.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the response length isn't too long.</jc>
- *
<jsm>assertLong</jsm>(<jv>responseLength</jv>).isLessThan(100000);
+ * <jc>// Validates that the byte array contains the string
"foo".</jc>
+ *
<jsm>assertBytes</jsm>(<jv>myBytes</jv>).asHex().is(<js>"666F6F"</js>);
* </p>
*
- * @param value The object being wrapped.
- * @return A new {@link LongAssertion} object. Never <jk>null</jk>.
+ * @param value The byte array being wrapped.
+ * @return A new {@link ByteArrayAssertion} object. Never
<jk>null</jk>.
*/
- public static final LongAssertion assertLong(Long value) {
- return LongAssertion.create(value);
+ public static final ByteArrayAssertion assertBytes(byte[] value) {
+ return ByteArrayAssertion.create(value);
}
/**
- * Used for assertion calls against longs.
+ * Used for assertion calls against the contents of input streams.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the response length isn't too long.</jc>
- *
<jsm>assertLong</jsm>(<jv>responseLength</jv>).isLessThan(100000);
+ * <jc>// Validates that the stream contains the string "foo".</jc>
+ *
<jsm>assertStream</jsm>(<jv>myStream</jv>).asHex().is(<js>"666F6F"</js>);
* </p>
*
- * @param value The object being wrapped.
- * @return A new {@link LongAssertion} object. Never <jk>null</jk>.
+ * @param value
+ * The input stream being wrapped.
+ * <br>Can be <jk>null</jk>.
+ * <br>Stream is automatically closed.
+ * @return A new {@link ByteArrayAssertion} object. Never
<jk>null</jk>.
+ * @throws IOException If thrown while reading contents from stream.
*/
- public static final <T extends Comparable<T>> ComparableAssertion<T>
assertComparable(T value) {
- return ComparableAssertion.create(value);
+ public static final ByteArrayAssertion assertStream(InputStream value)
throws IOException {
+ return assertBytes(value == null ? null : readBytes(value));
}
/**
- * Used for assertion calls against arbitrary POJOs.
+ * Used for assertion calls against {@link Collection} objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified POJO is the specified type and
serializes to the specified value.</jc>
- *
<jsm>assertObject</jsm>(<jv>myPojo</jv>).isType(MyBean.<jk>class</jk>).asJson().is(<js>"{foo:'bar'}"</js>);
+ * List=<String> <jv>list</jv> =
AList.<jsm>of</jsm>(<js>"foo"</js>);
+ * <jsm>assertCollection</jsm>(<jv>list</jv>).isNotEmpty();
* </p>
*
* @param value The object being wrapped.
- * @return A new {@link ObjectAssertion} object. Never <jk>null</jk>.
+ * @return A new {@link CollectionAssertion} object. Never
<jk>null</jk>.
*/
- public static final <T> ObjectAssertion<T> assertObject(T value) {
- if (value instanceof Optional)
- throw new RuntimeException("XXX");
- return ObjectAssertion.create(value);
+ public static final <E> CollectionAssertion<E>
assertCollection(Collection<E> value) {
+ return CollectionAssertion.create(value);
}
/**
- * Used for assertion calls against {@link Optional Optionals}.
+ * Used for assertion calls against longs.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified POJO is the specified type and
serializes to the specified value.</jc>
- *
<jsm>assertOptional</jsm>(<jv>opt</jv>).isType(MyBean.<jk>class</jk>).asJson().is(<js>"{foo:'bar'}"</js>);
+ * <jc>// Validates the response length isn't too long.</jc>
+ *
<jsm>assertLong</jsm>(<jv>responseLength</jv>).isLessThan(100000);
* </p>
*
* @param value The object being wrapped.
- * @return A new {@link ObjectAssertion} object. Never <jk>null</jk>.
+ * @return A new {@link LongAssertion} object. Never <jk>null</jk>.
*/
- public static final <T> ObjectAssertion<T> assertOptional(Optional<T>
value) {
- return ObjectAssertion.create(value.orElse(null));
+ public static final <T extends Comparable<T>> ComparableAssertion<T>
assertComparable(T value) {
+ return ComparableAssertion.create(value);
}
/**
- * Used for assertion calls against Java beans.
+ * Used for assertion calls against {@link Date} objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified POJO is the specified type and
serializes to the specified value.</jc>
- *
<jsm>assertBean</jsm>(<jv>myBean</jv>).isType(MyBean.<jk>class</jk>).fields(<js>"foo"</js>).asJson().is(<js>"{foo:'bar'}"</js>);
+ * <jc>// Validates the specified date is after the current
date.</jc>
+ * <jsm>assertDate</jsm>(<jv>myDate</jv>).isAfterNow();
+ * </p>
+ *
+ * @param value The date being wrapped.
+ * @return A new {@link DateAssertion} object. Never <jk>null</jk>.
+ */
+ public static final DateAssertion assertDate(Date value) {
+ return DateAssertion.create(value);
+ }
+
+ /**
+ * Used for assertion calls against integers.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates the response status code is 200 or 404.</jc>
+ * <jsm>assertInteger</jsm>(<jv>httpReponse<jv>).isAny(200,404);
* </p>
*
* @param value The object being wrapped.
- * @return A new {@link BeanAssertion} object. Never <jk>null</jk>.
+ * @return A new {@link IntegerAssertion} object. Never <jk>null</jk>.
*/
- public static final <V> BeanAssertion<V> assertBean(V value) {
- return BeanAssertion.create(value);
+ public static final IntegerAssertion assertInteger(Integer value) {
+ return IntegerAssertion.create(value);
}
/**
- * Used for assertion calls against lists of Java beans.
+ * Used for assertion calls against {@link Collection} objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified list contains 3 beans with the
specified values for the 'foo' property.</jc>
- * <jsm>assertBeanList</jsm>(<jv>myBeanList</jv>)
- * .property(<js>"foo"</js>)
- * .is(<js>"bar"</js>,<js>"baz"</js>,<js>"qux"</js>);
+ * List=<String> <jv>list</jv> =
AList.<jsm>of</jsm>(<js>"foo"</js>);
+ * <jsm>assertList</jsm>(<jv>list</jv>).item(0).is(<js>"foo"</js>);
* </p>
*
* @param value The object being wrapped.
- * @return A new {@link BeanListAssertion} object. Never <jk>null</jk>.
+ * @return A new {@link ListAssertion} object. Never <jk>null</jk>.
*/
- public static final <E> BeanListAssertion<E> assertBeanList(List<E>
value) {
- return BeanListAssertion.create(value);
+ public static final <E> ListAssertion<E> assertList(List<E> value) {
+ return ListAssertion.create(value);
}
/**
- * Used for assertion calls against string objects.
+ * Used for assertion calls against longs.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the response body of an HTTP call is the text
"OK".</jc>
- * <jsm>assertString</jsm>(<jv>httpBody</jv>).is(<js>"OK"</js>);
+ * <jc>// Validates the response length isn't too long.</jc>
+ *
<jsm>assertLong</jsm>(<jv>responseLength</jv>).isLessThan(100000);
* </p>
*
- * @param value The string being wrapped.
- * @return A new {@link StringAssertion} object. Never <jk>null</jk>.
+ * @param value The object being wrapped.
+ * @return A new {@link LongAssertion} object. Never <jk>null</jk>.
*/
- public static final StringAssertion assertString(Object value) {
- if (value instanceof Optional)
- value = ((Optional<?>)value).orElse(null);
- return StringAssertion.create(value);
+ public static final LongAssertion assertLong(Long value) {
+ return LongAssertion.create(value);
}
/**
- * Used for assertion calls against boolean objects.
+ * Used for assertion calls against maps.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates that the specified boolean object exists and
is true.</jc>
- * <jsm>assertBoolean</jsm>(<jv>myBoolean</jv>).exists().isTrue();
+ * <jc>// Validates the specified POJO is the specified type and
contains the specified key.</jc>
+ *
<jsm>assertMap</jsm>(<jv>myMap</jv>).isType(HashMap.<jk>class</jk>).containsKey(<js>"foo"</js>);
* </p>
*
- * @param value The boolean being wrapped.
- * @return A new {@link BooleanAssertion} object. Never <jk>null</jk>.
+ * @param value The object being wrapped.
+ * @return A new {@link MapAssertion} object. Never <jk>null</jk>.
*/
- public static final BooleanAssertion assertBoolean(Boolean value) {
- return BooleanAssertion.create(value);
+ public static final <K,V> MapAssertion<K,V> assertMap(Map<K,V> value) {
+ return MapAssertion.create(value);
}
/**
- * Used for assertion calls against throwable objects.
+ * Used for assertion calls against arbitrary POJOs.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the throwable message or one of the parent
messages contain 'Foobar'.</jc>
- *
<jsm>assertThrowable</jsm>(<jv>throwable</jv>).contains(<js>"Foobar"</js>);
+ * <jc>// Validates the specified POJO is the specified type and
serializes to the specified value.</jc>
+ *
<jsm>assertObject</jsm>(<jv>myPojo</jv>).isType(MyBean.<jk>class</jk>).asJson().is(<js>"{foo:'bar'}"</js>);
* </p>
*
- * @param value The throwable being wrapped.
- * @return A new {@link ThrowableAssertion} object. Never
<jk>null</jk>.
+ * @param value The object being wrapped.
+ * @return A new {@link ObjectAssertion} object. Never <jk>null</jk>.
*/
- public static final <V extends Throwable> ThrowableAssertion<V>
assertThrowable(V value) {
- return ThrowableAssertion.create(value);
+ public static final <T> ObjectAssertion<T> assertObject(T value) {
+ if (value instanceof Optional)
+ throw new RuntimeException("XXX");
+ return ObjectAssertion.create(value);
}
/**
- * Used for assertion calls against Java object arrays.
+ * Used for assertion calls against {@link Optional Optionals}.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * String[] <jv>array</jv> = {<js>"foo"</js>};
- * <jsm>assertArray</jsm>(<jv>array</jv>).isSize(1);
+ * <jc>// Validates the specified POJO is the specified type and
serializes to the specified value.</jc>
+ *
<jsm>assertOptional</jsm>(<jv>opt</jv>).isType(MyBean.<jk>class</jk>).asJson().is(<js>"{foo:'bar'}"</js>);
* </p>
*
* @param value The object being wrapped.
- * @return A new {@link ArrayAssertion} object. Never <jk>null</jk>.
+ * @return A new {@link ObjectAssertion} object. Never <jk>null</jk>.
*/
- public static final <E> ArrayAssertion<E> assertArray(E[] value) {
- return ArrayAssertion.create(value);
+ public static final <T> ObjectAssertion<T> assertOptional(Optional<T>
value) {
+ return ObjectAssertion.create(value.orElse(null));
}
/**
@@ -279,51 +301,54 @@ public class Assertions {
}
/**
- * Used for assertion calls against {@link Collection} objects.
+ * Used for assertion calls against string objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * List=<String> <jv>list</jv> =
AList.<jsm>of</jsm>(<js>"foo"</js>);
- * <jsm>assertCollection</jsm>(<jv>list</jv>).isNotEmpty();
+ * <jc>// Validates the response body of an HTTP call is the text
"OK".</jc>
+ * <jsm>assertString</jsm>(<jv>httpBody</jv>).is(<js>"OK"</js>);
* </p>
*
- * @param value The object being wrapped.
- * @return A new {@link CollectionAssertion} object. Never
<jk>null</jk>.
+ * @param value The string being wrapped.
+ * @return A new {@link StringAssertion} object. Never <jk>null</jk>.
*/
- public static final <E> CollectionAssertion<E>
assertCollection(Collection<E> value) {
- return CollectionAssertion.create(value);
+ public static final StringAssertion assertString(Object value) {
+ if (value instanceof Optional)
+ value = ((Optional<?>)value).orElse(null);
+ return StringAssertion.create(value);
}
/**
- * Used for assertion calls against {@link Collection} objects.
+ * Used for assertion calls against the contents of readers.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * List=<String> <jv>list</jv> =
AList.<jsm>of</jsm>(<js>"foo"</js>);
- * <jsm>assertList</jsm>(<jv>list</jv>).item(0).is(<js>"foo"</js>);
+ * <jc>// Validates the throwable message or one of the parent
messages contain 'Foobar'.</jc>
+ * <jsm>assertReader</jsm>(<jv>myReader</jv>).is(<js>"foo"</js>);
* </p>
*
- * @param value The object being wrapped.
- * @return A new {@link ListAssertion} object. Never <jk>null</jk>.
+ * @param value The reader being wrapped.
+ * @return A new {@link StringAssertion} object. Never <jk>null</jk>.
+ * @throws IOException If thrown while reading contents from reader.
*/
- public static final <E> ListAssertion<E> assertList(List<E> value) {
- return ListAssertion.create(value);
+ public static final StringAssertion assertReader(Reader value) throws
IOException {
+ return assertString(read(value));
}
/**
- * Used for assertion calls against maps.
+ * Used for assertion calls against throwable objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates the specified POJO is the specified type and
contains the specified key.</jc>
- *
<jsm>assertMap</jsm>(<jv>myMap</jv>).isType(HashMap.<jk>class</jk>).containsKey(<js>"foo"</js>);
+ * <jc>// Validates the throwable message or one of the parent
messages contain 'Foobar'.</jc>
+ *
<jsm>assertThrowable</jsm>(<jv>throwable</jv>).contains(<js>"Foobar"</js>);
* </p>
*
- * @param value The object being wrapped.
- * @return A new {@link MapAssertion} object. Never <jk>null</jk>.
+ * @param value The throwable being wrapped.
+ * @return A new {@link ThrowableAssertion} object. Never
<jk>null</jk>.
*/
- public static final <K,V> MapAssertion<K,V> assertMap(Map<K,V> value) {
- return MapAssertion.create(value);
+ public static final <V extends Throwable> ThrowableAssertion<V>
assertThrowable(V value) {
+ return ThrowableAssertion.create(value);
}
/**
@@ -374,57 +399,40 @@ public class Assertions {
}
/**
- * Used for assertion calls against the contents of input streams.
+ * Used for assertion calls against {@link Version} objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates that the stream contains the string "foo".</jc>
- *
<jsm>assertStream</jsm>(<jv>myStream</jv>).asHex().is(<js>"666F6F"</js>);
+ * <jc>// Validates the specified major version is greater than
2.</jc>
+ *
<jsm>assertVersion</jsm>(<jv>version</jv>).major().isGreaterThan(2);
* </p>
*
- * @param value
- * The input stream being wrapped.
- * <br>Can be <jk>null</jk>.
- * <br>Stream is automatically closed.
- * @return A new {@link ByteArrayAssertion} object. Never
<jk>null</jk>.
- * @throws IOException If thrown while reading contents from stream.
+ * @param value The version object being wrapped.
+ * @return A new {@link VersionAssertion} object. Never <jk>null</jk>.
*/
- public static final ByteArrayAssertion assertStream(InputStream value)
throws IOException {
- return assertBytes(value == null ? null : readBytes(value));
+ public static final VersionAssertion assertVersion(Version value) {
+ return VersionAssertion.create(value);
}
/**
- * Used for assertion calls against byte arrays.
+ * Used for assertion calls against {@link ZonedDateTime} objects.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
- * <jc>// Validates that the byte array contains the string
"foo".</jc>
- *
<jsm>assertBytes</jsm>(<jv>myBytes</jv>).asHex().is(<js>"666F6F"</js>);
+ * <jc>// Validates the specified date is after the current
date.</jc>
+ * <jsm>assertZonedDateTime</jsm>(<jv>byZdt</jv>).isAfterNow();
* </p>
*
- * @param value The byte array being wrapped.
- * @return A new {@link ByteArrayAssertion} object. Never
<jk>null</jk>.
+ * @param value The date being wrapped.
+ * @return A new {@link ZonedDateTimeAssertion} object. Never
<jk>null</jk>.
*/
- public static final ByteArrayAssertion assertBytes(byte[] value) {
- return ByteArrayAssertion.create(value);
+ public static final ZonedDateTimeAssertion
assertZonedDateTime(ZonedDateTime value) {
+ return ZonedDateTimeAssertion.create(value);
}
- /**
- * Used for assertion calls against the contents of readers.
- *
- * <h5 class='section'>Example:</h5>
- * <p class='bcode w800'>
- * <jc>// Validates the throwable message or one of the parent
messages contain 'Foobar'.</jc>
- * <jsm>assertReader</jsm>(<jv>myReader</jv>).is(<js>"foo"</js>);
- * </p>
- *
- * @param value The reader being wrapped.
- * @return A new {@link StringAssertion} object. Never <jk>null</jk>.
- * @throws IOException If thrown while reading contents from reader.
- */
- public static final StringAssertion assertReader(Reader value) throws
IOException {
- return assertString(read(value));
- }
+
//-----------------------------------------------------------------------------------------------------------------
+ // Other assertions
+
//-----------------------------------------------------------------------------------------------------------------
/**
* Throws an {@link IllegalArgumentException} if the specified argument
is <jk>null</jk>.