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 6548885 Assertions tests.
6548885 is described below
commit 65488857d1376ce91119e119ac9dfb91f7fc3b67
Author: JamesBognar <[email protected]>
AuthorDate: Fri Jul 10 11:56:39 2020 -0400
Assertions tests.
---
.../java/org/apache/juneau/ComboRoundTripTest.java | 4 +-
.../java/org/apache/juneau/ComboSerializeTest.java | 2 +-
.../apache/juneau/assertions/Assertions_Test.java | 4 +
.../juneau/assertions/DateAssertion_Test.java | 4 +-
.../assertions/FluentIntegerAssertion_Test.java | 16 +--
.../assertions/FluentLongAssertion_Test.java | 16 +--
.../juneau/assertions/ObjectAssertion_Test.java | 42 ++++++-
.../juneau/assertions/StringAssertion_Test.java | 132 +++++++++++++++++++++
.../juneau/assertions/ThrowableAssertion_Test.java | 86 ++++++++++++++
.../{Assertions_Test.java => Verify_Test.java} | 57 +++++----
.../juneau/serializer/UriResolutionTest.java | 2 +-
.../org/apache/juneau/assertions/Assertion.java | 28 ++---
.../assertions/FluentComparableAssertion.java | 12 +-
.../juneau/assertions/FluentDateAssertion.java | 8 +-
.../juneau/assertions/FluentObjectAssertion.java | 11 +-
.../juneau/assertions/FluentStringAssertion.java | 112 ++++++++---------
.../assertions/FluentThrowableAssertion.java | 39 ++++--
.../java/org/apache/juneau/assertions/Verify.java | 12 +-
.../rest/client2/RestClient_Logging_Test.java | 10 +-
19 files changed, 444 insertions(+), 153 deletions(-)
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboRoundTripTest.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboRoundTripTest.java
index af1eb88..5a6a2a2 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboRoundTripTest.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboRoundTripTest.java
@@ -116,7 +116,7 @@ public abstract class ComboRoundTripTest {
if (isRdf) {
Object[] args = { comboInput.label, testName };
- assertString(r).msg("{0}/{1} parse-normal
failed", args).isEqualAfterSort(expected);
+ assertString(r).msg("{0}/{1} parse-normal
failed", args).isEqualSortedLines(expected);
} else
assertString(r).msg("{0}/{1} parse-normal
failed", comboInput.label, testName).is(expected);
@@ -160,7 +160,7 @@ public abstract class ComboRoundTripTest {
if (isRdf) {
Object[] args = { comboInput.label, testName };
- assertString(r).msg("{0}/{1} parse-normal
failed", args).isEqualAfterSort(expected);
+ assertString(r).msg("{0}/{1} parse-normal
failed", args).isEqualSortedLines(expected);
} else
assertString(r).msg("{0}/{1} parse-normal
failed", comboInput.label, testName).is(expected);
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboSerializeTest.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboSerializeTest.java
index fefc6e7..edef2ec 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboSerializeTest.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ComboSerializeTest.java
@@ -96,7 +96,7 @@ public abstract class ComboSerializeTest {
if (isRdf) {
Object[] args = { comboInput.label, testName };
- assertString(r).msg("{0}/{1} serialize-normal
failed", args).isEqualAfterSort(expected);
+ assertString(r).msg("{0}/{1} serialize-normal
failed", args).isEqualSortedLines(expected);
} else
assertString(r).msg("{0}/{1} parse-normal
failed", comboInput.label, testName).is(expected);
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Assertions_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Assertions_Test.java
index 165760b..96aa301 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Assertions_Test.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Assertions_Test.java
@@ -47,4 +47,8 @@ public class Assertions_Test {
new Assertions();
}
+ @Test
+ public void a02_stdout_stderr() throws Exception {
+ assertThrown(()->assertObject(null).msg("Test
message").stderr().stdout().exists()).exists();
+ }
}
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/DateAssertion_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/DateAssertion_Test.java
index a23fd26..7572925 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/DateAssertion_Test.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/DateAssertion_Test.java
@@ -44,12 +44,12 @@ public class DateAssertion_Test {
assertThrown(()->assertDate(x2).isEqual(x1,
ChronoUnit.DAYS)).contains("Unexpected value.");
assertThrown(()->assertDate(null).isBefore(x1)).is("Value was
null.");
- assertThrown(()->assertDate(x1).isBefore(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertDate(x1).isBefore(null)).is("Parameter
'value' cannot be null.");
assertDate(x1).isBefore(x2);
assertThrown(()->assertDate(x2).isBefore(x1)).contains("Value
was not before expected.");
assertThrown(()->assertDate(null).isAfter(x1)).is("Value was
null.");
- assertThrown(()->assertDate(x1).isAfter(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertDate(x1).isAfter(null)).is("Parameter
'value' cannot be null.");
assertDate(x2).isAfter(x1);
assertThrown(()->assertDate(x1).isAfter(x2)).contains("Value
was not after expected.");
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentIntegerAssertion_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentIntegerAssertion_Test.java
index be56358..fbd8986 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentIntegerAssertion_Test.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentIntegerAssertion_Test.java
@@ -36,49 +36,49 @@ public class FluentIntegerAssertion_Test {
assertInteger(null).isEqual(null);
assertThrown(()->assertInteger(null).isGreaterThan(1)).is("Value was null.");
-
assertThrown(()->assertInteger(1).isGreaterThan(null)).is("Parameter cannot be
null.");
+
assertThrown(()->assertInteger(1).isGreaterThan(null)).is("Parameter 'value'
cannot be null.");
assertInteger(2).isGreaterThan(1);
assertThrown(()->assertInteger(1).isGreaterThan(2)).is("Value
was not greater than expected.\n\tExpected=[2]\n\tActual=[1]");
assertThrown(()->assertInteger(1).isGreaterThan(1)).is("Value
was not greater than expected.\n\tExpected=[1]\n\tActual=[1]");
assertThrown(()->assertInteger(null).isGt(1)).is("Value was
null.");
- assertThrown(()->assertInteger(1).isGt(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertInteger(1).isGt(null)).is("Parameter
'value' cannot be null.");
assertInteger(2).isGt(1);
assertThrown(()->assertInteger(1).isGt(2)).is("Value was not
greater than expected.\n\tExpected=[2]\n\tActual=[1]");
assertThrown(()->assertInteger(1).isGt(1)).is("Value was not
greater than expected.\n\tExpected=[1]\n\tActual=[1]");
assertThrown(()->assertInteger(null).isGreaterThanOrEqual(1)).is("Value was
null.");
-
assertThrown(()->assertInteger(1).isGreaterThanOrEqual(null)).is("Parameter
cannot be null.");
+
assertThrown(()->assertInteger(1).isGreaterThanOrEqual(null)).is("Parameter
'value' cannot be null.");
assertInteger(2).isGreaterThanOrEqual(1);
assertThrown(()->assertInteger(1).isGreaterThanOrEqual(2)).is("Value was not
greater than or equals to expected.\n\tExpected=[2]\n\tActual=[1]");
assertInteger(1).isGreaterThanOrEqual(1);
assertThrown(()->assertInteger(null).isGte(1)).is("Value was
null.");
- assertThrown(()->assertInteger(1).isGte(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertInteger(1).isGte(null)).is("Parameter
'value' cannot be null.");
assertInteger(2).isGte(1);
assertThrown(()->assertInteger(1).isGte(2)).is("Value was not
greater than or equals to expected.\n\tExpected=[2]\n\tActual=[1]");
assertInteger(1).isGte(1);
assertThrown(()->assertInteger(null).isLessThan(1)).is("Value
was null.");
-
assertThrown(()->assertInteger(1).isLessThan(null)).is("Parameter cannot be
null.");
+
assertThrown(()->assertInteger(1).isLessThan(null)).is("Parameter 'value'
cannot be null.");
assertInteger(1).isLessThan(2);
assertThrown(()->assertInteger(2).isLessThan(1)).is("Value was
not less than expected.\n\tExpected=[1]\n\tActual=[2]");
assertThrown(()->assertInteger(1).isLessThan(1)).is("Value was
not less than expected.\n\tExpected=[1]\n\tActual=[1]");
assertThrown(()->assertInteger(null).isLt(1)).is("Value was
null.");
- assertThrown(()->assertInteger(1).isLt(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertInteger(1).isLt(null)).is("Parameter
'value' cannot be null.");
assertInteger(1).isLt(2);
assertThrown(()->assertInteger(2).isLt(1)).is("Value was not
less than expected.\n\tExpected=[1]\n\tActual=[2]");
assertThrown(()->assertInteger(1).isLt(1)).is("Value was not
less than expected.\n\tExpected=[1]\n\tActual=[1]");
assertThrown(()->assertInteger(null).isLessThanOrEqual(1)).is("Value was
null.");
-
assertThrown(()->assertInteger(1).isLessThanOrEqual(null)).is("Parameter cannot
be null.");
+
assertThrown(()->assertInteger(1).isLessThanOrEqual(null)).is("Parameter
'value' cannot be null.");
assertInteger(1).isLessThanOrEqual(2);
assertThrown(()->assertInteger(2).isLessThanOrEqual(1)).is("Value was not less
than or equals to expected.\n\tExpected=[1]\n\tActual=[2]");
assertInteger(1).isLessThanOrEqual(1);
assertThrown(()->assertInteger(null).isLte(1)).is("Value was
null.");
- assertThrown(()->assertInteger(1).isLte(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertInteger(1).isLte(null)).is("Parameter
'value' cannot be null.");
assertInteger(1).isLte(2);
assertThrown(()->assertInteger(2).isLte(1)).is("Value was not
less than or equals to expected.\n\tExpected=[1]\n\tActual=[2]");
assertInteger(1).isLte(1);
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentLongAssertion_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentLongAssertion_Test.java
index 6ac4fb6..bb51397 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentLongAssertion_Test.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/FluentLongAssertion_Test.java
@@ -36,7 +36,7 @@ public class FluentLongAssertion_Test {
assertLong(null).isEqual(null);
assertThrown(()->assertLong(null).isGreaterThan(1)).is("Value
was null.");
-
assertThrown(()->assertLong(1l).isGreaterThan(null)).is("Parameter cannot be
null.");
+
assertThrown(()->assertLong(1l).isGreaterThan(null)).is("Parameter 'value'
cannot be null.");
assertLong(2l).isGreaterThan(1);
assertThrown(()->assertLong(1l).isGreaterThan(2l)).is("Value
was not greater than expected.\n\tExpected=[2]\n\tActual=[1]");
assertThrown(()->assertLong(1l).isGreaterThan(1l)).is("Value
was not greater than expected.\n\tExpected=[1]\n\tActual=[1]");
@@ -45,43 +45,43 @@ public class FluentLongAssertion_Test {
assertLong(null).integer().isNull();
assertThrown(()->assertLong(null).isGt(1l)).is("Value was
null.");
- assertThrown(()->assertLong(1l).isGt(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertLong(1l).isGt(null)).is("Parameter
'value' cannot be null.");
assertLong(2l).isGt(1);
assertThrown(()->assertLong(1l).isGt(2)).is("Value was not
greater than expected.\n\tExpected=[2]\n\tActual=[1]");
assertThrown(()->assertLong(1l).isGt(1)).is("Value was not
greater than expected.\n\tExpected=[1]\n\tActual=[1]");
assertThrown(()->assertLong(null).isGreaterThanOrEqual(1)).is("Value was
null.");
-
assertThrown(()->assertLong(1l).isGreaterThanOrEqual(null)).is("Parameter
cannot be null.");
+
assertThrown(()->assertLong(1l).isGreaterThanOrEqual(null)).is("Parameter
'value' cannot be null.");
assertLong(2l).isGreaterThanOrEqual(1);
assertThrown(()->assertLong(1l).isGreaterThanOrEqual(2)).is("Value was not
greater than or equals to expected.\n\tExpected=[2]\n\tActual=[1]");
assertLong(1l).isGreaterThanOrEqual(1);
assertThrown(()->assertLong(null).isGte(1)).is("Value was
null.");
- assertThrown(()->assertLong(1l).isGte(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertLong(1l).isGte(null)).is("Parameter
'value' cannot be null.");
assertLong(2l).isGte(1);
assertThrown(()->assertLong(1l).isGte(2l)).is("Value was not
greater than or equals to expected.\n\tExpected=[2]\n\tActual=[1]");
assertLong(1l).isGte(1l);
assertThrown(()->assertLong(null).isLessThan(1)).is("Value was
null.");
- assertThrown(()->assertLong(1l).isLessThan(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertLong(1l).isLessThan(null)).is("Parameter
'value' cannot be null.");
assertLong(1l).isLessThan(2l);
assertThrown(()->assertLong(2l).isLessThan(1)).is("Value was
not less than expected.\n\tExpected=[1]\n\tActual=[2]");
assertThrown(()->assertLong(1l).isLessThan(1)).is("Value was
not less than expected.\n\tExpected=[1]\n\tActual=[1]");
assertThrown(()->assertLong(null).isLt(1)).is("Value was
null.");
- assertThrown(()->assertLong(1l).isLt(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertLong(1l).isLt(null)).is("Parameter
'value' cannot be null.");
assertLong(1l).isLt(2);
assertThrown(()->assertLong(2l).isLt(1)).is("Value was not less
than expected.\n\tExpected=[1]\n\tActual=[2]");
assertThrown(()->assertLong(1l).isLt(1)).is("Value was not less
than expected.\n\tExpected=[1]\n\tActual=[1]");
assertThrown(()->assertLong(null).isLessThanOrEqual(1)).is("Value was null.");
-
assertThrown(()->assertLong(1l).isLessThanOrEqual(null)).is("Parameter cannot
be null.");
+
assertThrown(()->assertLong(1l).isLessThanOrEqual(null)).is("Parameter 'value'
cannot be null.");
assertLong(1l).isLessThanOrEqual(2);
assertThrown(()->assertLong(2l).isLessThanOrEqual(1)).is("Value
was not less than or equals to expected.\n\tExpected=[1]\n\tActual=[2]");
assertLong(1l).isLessThanOrEqual(1);
assertThrown(()->assertLong(null).isLte(1)).is("Value was
null.");
- assertThrown(()->assertLong(1l).isLte(null)).is("Parameter
cannot be null.");
+ assertThrown(()->assertLong(1l).isLte(null)).is("Parameter
'value' cannot be null.");
assertLong(1l).isLte(2);
assertThrown(()->assertLong(2l).isLte(1)).is("Value was not
less than or equals to expected.\n\tExpected=[1]\n\tActual=[2]");
assertLong(1l).isLte(1);
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/ObjectAssertion_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/ObjectAssertion_Test.java
index 2423ae3..b88b37f 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/ObjectAssertion_Test.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/ObjectAssertion_Test.java
@@ -15,6 +15,8 @@ package org.apache.juneau.assertions;
import static org.apache.juneau.assertions.Assertions.*;
import static org.junit.runners.MethodSorters.*;
+import java.util.*;
+
import org.apache.juneau.json.*;
import org.junit.*;
@@ -42,7 +44,7 @@ public class ObjectAssertion_Test {
assertObject(1).exists();
assertThrown(()->assertObject(null).isType(null)).is("Value was
null.");
-
assertThrown(()->assertObject("foo").isType(null)).is("Parameter cannot be
null.");
+
assertThrown(()->assertObject("foo").isType(null)).is("Parameter 'parent'
cannot be null.");
assertObject("foo").isType(String.class);
assertObject("foo").isType(CharSequence.class);
assertObject("foo").isType(Comparable.class);
@@ -57,6 +59,44 @@ public class ObjectAssertion_Test {
assertObject(null).serialized(JsonSerializer.DEFAULT).is("null");
assertObject(new A2()).jsonSorted().is("{bar:2,foo:1}");
+
+ int[] x1 = {1,2}, x2 = {2,1};
+ assertObject(x2).jsonSorted().is("[1,2]");
+
assertThrown(()->assertObject(x2).jsonSorted().is("[2,1]")).stderr().is("Unexpected
value.\n\tExpected=[[2,1]]\n\tActual=[[1,2]]");
+ assertObject(null).jsonSorted().is("null");
+
+ assertObject(x1).sameAs(x1);
+
assertThrown(()->assertObject(x1).sameAs(x2)).stderr().is("Unexpected
comparison.\n\tExpected=[[2,1]]\n\tActual=[[1,2]]");
+ assertObject(null).sameAs(null);
+ assertThrown(()->assertObject(new
A1()).sameAs(null)).contains("Could not call getValue() on property 'foo'");
+
+ assertObject(x1).sameAsSorted(x1);
+ assertObject(x1).sameAsSorted(x2);
+
assertThrown(()->assertObject(x1).sameAs(null)).stderr().is("Unexpected
comparison.\n\tExpected=[null]\n\tActual=[[1,2]]");
+ assertObject(null).sameAsSorted(null);
+
+ assertObject(x1).doesNotEqual(null);
+ assertObject(null).doesNotEqual(x1);
+ assertObject(x1).doesNotEqual(x2);
+
assertThrown(()->assertObject(null).doesNotEqual(null)).is("Unexpected
value.\n\tExpected not=[null]\n\tActual=[null]");
+
assertThrown(()->assertObject(x1).doesNotEqual(x1)).is("Unexpected
value.\n\tExpected not=[[1,2]]\n\tActual=[[1,2]]");
+
+ assertObject(x1).passes(x->x != null);
+ assertThrown(()->assertObject(x1).passes(x->x ==
null)).stderr().is("Value did not pass predicate test.\n\tValue=[[1,2]]");
+
+ assertObject(x1).passes(int[].class, x->x[0] == 1);
+ assertThrown(()->assertObject(x1).passes(int[].class,
x->x[0]==2)).stderr().is("Value did not pass predicate test.\n\tValue=[[1,2]]");
+
+ assertObject(x1).isNot(null);
+
+ assertObject(x1).isAny(x1,x2);
+
assertThrown(()->assertObject(x1).isAny(x2)).stderr().is("Expected value not
found.\n\tExpected=[[[2,1]]]\n\tActual=[[1,2]]");
+
+ assertObject(x1).isNotAny(x2);
+
assertThrown(()->assertObject(x1).isNotAny(x1,x2)).stderr().is("Unexpected
value found.\n\tUnexpected=[[1,2]]\n\tActual=[[1,2]]");
+
+ Date d1 = new Date(0), d2 = new Date(0);
+ assertObject(d1).is(d2);
}
@Test
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/StringAssertion_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/StringAssertion_Test.java
new file mode 100644
index 0000000..b5c428d
--- /dev/null
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/StringAssertion_Test.java
@@ -0,0 +1,132 @@
+//
***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright
ownership. The ASF licenses this file *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance *
+// * with the License. You may obtain a copy of the License at
*
+// *
*
+// * http://www.apache.org/licenses/LICENSE-2.0
*
+// *
*
+// * Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
+// * specific language governing permissions and limitations under the
License. *
+//
***************************************************************************************************************************
+package org.apache.juneau.assertions;
+
+import static org.apache.juneau.assertions.Assertions.*;
+import static org.junit.runners.MethodSorters.*;
+
+import org.junit.*;
+
+@FixMethodOrder(NAME_ASCENDING)
+public class StringAssertion_Test {
+
+ @Test
+ public void a01_basic() throws Exception {
+ assertString(null).doesNotExist();
+ assertString(1).exists();
+
+
assertString("foobar").replaceAll("fo+","bar").is("barbar").is("foobar");
+ assertString(null).replaceAll("fo+","bar").isNull();
+
assertThrown(()->assertString("foobar").replaceAll(null,"")).is("Parameter
'regex' cannot be null.");
+
assertThrown(()->assertString("foobar").replaceAll("",null)).is("Parameter
'replacement' cannot be null.");
+
+
assertString("foobar").replace("foo","bar").is("barbar").is("foobar");
+ assertString(null).replace("foo","bar").isNull();
+
assertThrown(()->assertString("").replace(null,"bar").isNull()).is("Parameter
'target' cannot be null.");
+
assertThrown(()->assertString("").replace("foo",null).isNull()).is("Parameter
'replacement' cannot be null.");
+
+ assertString("foo%20bar").urlDecode().is("foo
bar").is("foo%20bar");
+ assertString(null).urlDecode().isNull();
+
+
assertString("foo\nbar\nbaz").sort().is("bar\nbaz\nfoo").is("foo\nbar\nbaz");
+ assertString(null).sort().doesNotExist();
+
+ assertString("FOOBAR").lc().is("foobar").is("FOOBAR");
+ assertString(null).lc().isNull();
+
+ assertString("foobar").uc().is("FOOBAR").is("foobar");
+ assertString(null).uc().isNull();
+
+ assertString("foo\nbar\nbaz").isEqualLines("foo","bar","baz");
+
assertThrown(()->assertString(null).isEqualLines((String[])null)).is("Parameter
'lines' cannot be null.");
+
assertThrown(()->assertString(null).isEqualLines((String)null)).is("Text
differed at position -1.\n\tExpected=[]\n\tActual=[null]");
+
assertThrown(()->assertString("foo\nbar\nbaz").javaStrings().isEqualLines("foo","bar","bar")).stderr().is("Text
differed at position
10.\n\tExpected=[foo\\nbar\\nbar]\n\tActual=[foo\\nbar\\nbaz]");
+
+
assertString("foo\nbar\nbaz").isEqualSortedLines("bar","foo","baz");
+
assertThrown(()->assertString(null).isEqualSortedLines((String[])null)).is("Parameter
'lines' cannot be null.");
+ assertString("").isEqualSortedLines((String)null);
+
assertThrown(()->assertString(null).isEqualSortedLines()).is("Value was null.");
+
assertThrown(()->assertString("foo\nbar\nbaz").isEqualSortedLines("bar","foo","bar")).stderr().is("Expected
text had different values at line 2.\n\tExpected=[bar]\n\tActual=[baz]");
+
assertThrown(()->assertString("foo\nbar\nbaz").isEqualSortedLines("bar","foo")).stderr().is("Expected
text had different numbers of lines.\n\tExpected=[2]\n\tActual=[3]");
+
assertThrown(()->assertString(null).isEqualSortedLines("foo")).stderr().is("Value
was null.");
+
assertThrown(()->assertString("foo").isEqualSortedLines((String)null)).stderr().is("Expected
text had different values at line 1.\n\tExpected=[]\n\tActual=[foo]");
+
+ assertString("foo\nbar\nbaz").isEqualLines("foo","bar","baz");
+
+ assertString("foobar").isEqualIc("FOOBAR");
+ assertString(null).isEqualIc(null);
+
assertThrown(()->assertString("foobar").isEqualIc("FOOBAZ")).stderr().is("Text
differed at position 5.\n\tExpected=[FOOBAZ]\n\tActual=[foobar]");
+
assertThrown(()->assertString(null).isEqualIc("FOOBAZ")).stderr().is("Text
differed at position 0.\n\tExpected=[FOOBAZ]\n\tActual=[null]");
+
assertThrown(()->assertString("foobar").isEqualIc(null)).stderr().is("Text
differed at position 0.\n\tExpected=[null]\n\tActual=[foobar]");
+
+ assertString("foobar").doesNotEqual("foobaz");
+
assertThrown(()->assertString("foobar").doesNotEqual("foobar")).stderr().is("Text
equaled unexpected.\n\tText=[foobar]");
+
+
assertThrown(()->assertString("foobar").isEqual("foobaz")).stderr().is("Text
differed at position 5.\n\tExpected=[foobaz]\n\tActual=[foobar]");
+
+ assertString("foobar").isNot("foobaz");
+
assertThrown(()->assertString("foobar").isNot("foobar")).is("Text equaled
unexpected.\n\tText=[foobar]");
+ assertThrown(()->assertString(null).isNot(null)).is("Text
equaled unexpected.\n\tText=[null]");
+ assertString("foobar").isNot(null);
+ assertString(null).isNot("foobar");
+
+ assertString("foobar").doesNotEqualIc("foobaz");
+
assertThrown(()->assertString("foobar").doesNotEqualIc("Foobar")).is("Text
equaled unexpected.\n\tText=[foobar]");
+
assertThrown(()->assertString(null).doesNotEqualIc(null)).is("Text equaled
unexpected.\n\tText=[null]");
+ assertString("foobar").doesNotEqualIc(null);
+ assertString(null).doesNotEqualIc("foobar");
+
+ assertString("foobar").contains("foo","bar");
+
assertThrown(()->assertString("foobar").contains("foo","baz")).is("Text did not
contain expected substring.\n\tSubstring=[baz]\n\tText=[foobar]");
+ assertString(null).contains();
+
assertThrown(()->assertString("foobar").contains((String[])null)).is("Parameter
'values' cannot be null.");
+ assertString("foobar").contains((String)null);
+
assertThrown(()->assertString(null).contains("foobar")).is("Text did not
contain expected substring.\n\tSubstring=[foobar]\n\tText=[null]");
+
+ assertString("foobar").doesNotContain("baz","qux");
+
assertThrown(()->assertString("foobar").doesNotContain("foo","baz")).is("Text
contained unexpected substring.\n\tSubstring=[foo]\n\tText=[foobar]");
+ assertString(null).doesNotContain();
+
assertThrown(()->assertString("foobar").doesNotContain((String[])null)).is("Parameter
'values' cannot be null.");
+ assertString("foobar").doesNotContain((String)null);
+ assertString(null).doesNotContain("foobar");
+
+ assertString("").isEmpty();
+ assertString(null).isEmpty();
+ assertThrown(()->assertString("foo").isEmpty()).is("Text was
not empty.\n\tText=[foo]");
+
+ assertString("foo").isNotEmpty();
+ assertThrown(()->assertString("").isNotEmpty()).is("Text was
empty.");
+ assertThrown(()->assertString(null).isNotEmpty()).is("Text was
null.");
+
+ assertString("foo").matches("fo+");
+ assertThrown(()->assertString("foo").matches("bar")).is("Text
did not match expected pattern.\n\tPattern=[bar]\n\tText=[foo]");
+ assertThrown(()->assertString(null).matches("fo+")).is("Value
was null.");
+
assertThrown(()->assertString("").matches((String)null)).is("Parameter 'regex'
cannot be null.");
+
+ assertString("foo").matchesSimple("fo*");
+
assertThrown(()->assertString("foo").matchesSimple("b*")).stderr().is("Text did
not match expected pattern.\n\tPattern=[\\Qb\\E.*\\Q\\E]\n\tText=[foo]");
+
assertThrown(()->assertString(null).matchesSimple("b*")).is("Value was null.");
+
assertThrown(()->assertString("").matchesSimple(null)).is("Parameter
'searchPattern' cannot be null.");
+
+ assertString("foo").doesNotMatch("b.*");
+
assertThrown(()->assertString("foo").doesNotMatch("fo+")).is("Text matched
unexpected pattern.\n\tPattern=[fo+]\n\tText=[foo]");
+ assertString(null).doesNotMatch("fo+");
+
assertThrown(()->assertString("").doesNotMatch((String)null)).is("Parameter
'regex' cannot be null.");
+ }
+
+ @Test
+ public void a02_other() throws Exception {
+ assertThrown(()->StringAssertion.create(null).msg("Foo {0}",
1).exists()).is("Foo 1");
+ StringAssertion.create(null).stdout().stderr().javaStrings();
+ }
+}
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/ThrowableAssertion_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/ThrowableAssertion_Test.java
new file mode 100644
index 0000000..f6420eb
--- /dev/null
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/ThrowableAssertion_Test.java
@@ -0,0 +1,86 @@
+//
***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright
ownership. The ASF licenses this file *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance *
+// * with the License. You may obtain a copy of the License at
*
+// *
*
+// * http://www.apache.org/licenses/LICENSE-2.0
*
+// *
*
+// * Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
+// * specific language governing permissions and limitations under the
License. *
+//
***************************************************************************************************************************
+package org.apache.juneau.assertions;
+
+import static org.apache.juneau.assertions.Assertions.*;
+import static org.junit.runners.MethodSorters.*;
+
+import java.io.*;
+
+import org.junit.*;
+
+@FixMethodOrder(NAME_ASCENDING)
+public class ThrowableAssertion_Test {
+
+ @Test
+ public void a01_basic() throws Exception {
+ Exception x1 = new RuntimeException("foo");
+
+
assertThrowable(x1).isType(Exception.class).isType(RuntimeException.class);
+
assertThrown(()->assertThrowable(x1).isType(IOException.class)).is("Exception
was not expected
type.\n\tExpected=[java.io.IOException]\n\tActual=[java.lang.RuntimeException]");
+
assertThrown(()->assertThrowable(null).isType(IOException.class)).is("Exception
was not expected type.\n\tExpected=[java.io.IOException]\n\tActual=[null]");
+
assertThrown(()->assertThrowable(x1).isType(null)).is("Parameter 'type' cannot
be null.");
+
+ assertThrowable(x1).contains("foo");
+
assertThrown(()->assertThrowable(x1).contains("bar")).is("Exception message did
not contain expected substring.\n\tSubstring=[bar]\n\tText=[foo]");
+
assertThrown(()->assertThrowable(null).contains("foo")).is("Exception was not
thrown.");
+
assertThrown(()->assertThrowable(x1).contains((String[])null)).is("Parameter
'substrings' cannot be null.");
+ assertThrowable(x1).contains((String)null);
+
+ assertThrowable(null).doesNotExist();
+
assertThrown(()->assertThrowable(x1).doesNotExist()).is("Exception was
thrown.");
+
+ assertThrowable(x1).passes(x->x.getMessage().equals("foo"));
+
assertThrown(()->assertThrowable(x1).passes(x->x.getMessage().equals("bar"))).is("Value
did not pass predicate test.\n\tValue=[java.lang.RuntimeException: foo]");
+
+ assertThrowable(x1).passes(x->x.getMessage().equals("foo"));
+
assertThrown(()->assertThrowable(x1).passes(x->x.getMessage().equals("bar"))).is("Value
did not pass predicate test.\n\tValue=[java.lang.RuntimeException: foo]");
+
+ assertThrowable(x1).passes(x->x.getMessage().equals("foo"));
+
assertThrown(()->assertThrowable(x1).passes(x->x.getMessage().equals("bar"))).is("Value
did not pass predicate test.\n\tValue=[java.lang.RuntimeException: foo]");
+
+ assertThrowable(x1).passes(RuntimeException.class,
x->x.getMessage().equals("foo"));
+ assertThrown(()->assertThrowable(x1).passes(IOException.class,
x->x.getMessage().equals("foo"))).is("Exception was not expected
type.\n\tExpected=[java.io.IOException]\n\tActual=[java.lang.RuntimeException]");
+
assertThrown(()->assertThrowable(x1).passes(RuntimeException.class,
x->x.getMessage().equals("bar"))).is("Value did not pass predicate
test.\n\tValue=[java.lang.RuntimeException: foo]");
+
+ assertThrowable(x1).message().is("foo");
+ assertThrowable(new
RuntimeException()).message().doesNotExist();
+ assertThrowable(null).message().doesNotExist();
+
+ assertThrowable(x1).localizedMessage().is("foo");
+ assertThrowable(new
RuntimeException()).localizedMessage().doesNotExist();
+ assertThrowable(null).localizedMessage().doesNotExist();
+
+ assertThrowable(x1).stackTrace().contains("RuntimeException");
+ assertThrowable(new
RuntimeException()).stackTrace().contains("RuntimeException");
+ assertThrowable(null).stackTrace().doesNotExist();
+
+ assertThrowable(new
RuntimeException(x1)).causedBy().message().is("foo");
+ assertThrowable(new
RuntimeException()).message().doesNotExist();
+ assertThrowable(null).causedBy().message().doesNotExist();
+
+ assertThrowable(new RuntimeException(new
IOException())).find(RuntimeException.class).exists();
+ assertThrowable(new RuntimeException(new
IOException())).find(IOException.class).exists();
+ assertThrowable(new RuntimeException(new
IOException())).find(Exception.class).exists();
+ assertThrowable(new RuntimeException(new
IOException())).find(FileNotFoundException.class).doesNotExist();
+ assertThrowable(new
RuntimeException()).find(RuntimeException.class).exists();
+ assertThrowable(new
RuntimeException()).find(IOException.class).doesNotExist();
+
assertThrowable(null).find(RuntimeException.class).doesNotExist();
+ }
+
+ @Test
+ public void a02_other() throws Exception {
+ assertThrown(()->ThrowableAssertion.create(null).msg("Foo {0}",
1).exists()).is("Foo 1");
+ ThrowableAssertion.create(null).stdout().stderr();
+ }
+}
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Assertions_Test.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Verify_Test.java
similarity index 50%
copy from
juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Assertions_Test.java
copy to
juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Verify_Test.java
index 165760b..8b43a40 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Assertions_Test.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/assertions/Verify_Test.java
@@ -12,39 +12,48 @@
//
***************************************************************************************************************************
package org.apache.juneau.assertions;
+import static org.apache.juneau.assertions.Verify.*;
+
import static org.apache.juneau.assertions.Assertions.*;
import static org.junit.runners.MethodSorters.*;
-import java.io.*;
import java.util.*;
-import org.apache.juneau.collections.*;
import org.junit.*;
@FixMethodOrder(NAME_ASCENDING)
-public class Assertions_Test {
+public class Verify_Test {
@Test
- public void a01_basic() throws Exception {
- assertDate(new Date()).isAfter(new Date(0));
- assertInteger(2).isGt(1);
- assertLong(2l).isGt(1l);
- assertObject("foo").json().is("'foo'");
- assertString("foo").is("foo");
- assertThrowable(null).doesNotExist();
- assertArray(new String[0]).isEmpty();
- assertCollection(AList.of()).isEmpty();
- assertList(AList.of()).isEmpty();
- assertStream(new
ByteArrayInputStream("foo".getBytes())).string().is("foo");
- assertStream(null).string().doesNotExist();
- assertBytes("foo".getBytes()).string().is("foo");
- assertBytes(null).string().doesNotExist();
- assertReader(new StringReader("foo")).is("foo");
- assertReader(null).doesNotExist();
- assertThrown(()->{throw new
RuntimeException("foo");}).is("foo");
- assertThrown(()->{}).doesNotExist();
-
- new Assertions();
- }
+ public void a01_basic() {
+ Verify x1 = verify("foo"), x2 = verify(null), x3 = verify(new
Date(0));
+
+ assertString(x1.is("foo")).doesNotExist();
+ assertString(x1.is("bar")).is("Expected [bar] but was [foo].");
+ assertString(x1.is(null)).is("Expected [null] but was [foo].");
+ assertString(x2.is(null)).doesNotExist();
+ assertString(x2.is("foo")).is("Expected [foo] but was [null].");
+ assertString(x3.is(new Date(0))).doesNotExist();
+
+ assertString(x1.isType(String.class)).doesNotExist();
+ assertString(x1.isType(Integer.class)).is("Expected type
[java.lang.Integer] but was [java.lang.String].");
+ assertString(x2.isType(null)).doesNotExist();
+ assertString(x2.isType(String.class)).is("Expected type
[java.lang.String] but was [null].");
+ assertString(x1.isType(null)).is("Expected type [null] but was
[java.lang.String].");
+
+ assertString(verify(true).isTrue()).doesNotExist();
+ assertString(verify(false).isFalse()).doesNotExist();
+ assertString(verify(null).isTrue()).is("Expected [true] but was
[null].");
+ assertString(verify(null).isFalse()).is("Expected [false] but
was [null].");
+ assertString(verify(Boolean.TRUE).isTrue()).doesNotExist();
+ assertString(verify(Boolean.FALSE).isFalse()).doesNotExist();
+ assertString(x1.is("foo")).doesNotExist();
+
+ assertString(verify("foo").msg("bar{0}",
"baz").is("foo")).doesNotExist();
+ assertString(verify("foo").msg("bar{0}",
"baz").is("bar")).is("barbaz");
+ assertString(verify("foo").msg("bar{0}",
"baz").isType(Integer.class)).is("barbaz");
+ assertString(verify(null).msg("bar{0}",
"baz").is("bar")).is("barbaz");
+ assertString(verify("foo").msg("bar{0}",
"baz").is(null)).is("barbaz");
+ }
}
diff --git
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/serializer/UriResolutionTest.java
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/serializer/UriResolutionTest.java
index ed5b40f..ce70eae 100644
---
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/serializer/UriResolutionTest.java
+++
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/serializer/UriResolutionTest.java
@@ -566,7 +566,7 @@ public class UriResolutionTest {
if (isRdf) {
Object[] args = { label,
s.getClass().getSimpleName() };
- assertString(r).msg("{0}/{1} serialize-normal
failed", args).isEqualAfterSort(expected);
+ assertString(r).msg("{0}/{1} serialize-normal
failed", args).isEqualSortedLines(expected);
} else
assertString(r).msg("{0}/{1} serialize-normal
failed", label, s.getClass().getSimpleName()).is(expected);
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertion.java
index 28a588f..5df9d96 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertion.java
@@ -27,11 +27,6 @@ public class Assertion {
boolean stdout, stderr;
/**
- * No-arg constructor.
- */
- protected Assertion() {}
-
- /**
* Constructor used when this assertion is being created from within
another assertion.
* @param creator The creator of this assertion.
*/
@@ -117,28 +112,23 @@ public class Assertion {
* @param value The value to check.
* @param msg The message.
* @param args The message arguments.
+ * @return The value.
*/
- protected void assertNotNull(Object value, String msg, Object...args) {
+ protected Object assertNotNull(Object value, String msg, Object...args)
{
if (value == null)
throw new BasicAssertionError(format(msg, args));
+ return value;
}
-
/**
- * Casts the specified object to the specified type.
+ * Asserts the specified parameter is not null.
*
- * @param <T> The type to cast the object to.
- * @param c The type to cast the object to.
- * @param o The object to cast.
- * @return The same object cast to the specified object.s
+ * @param parameter The parameter name.
+ * @param value The value to check.
+ * @return The value.
*/
- @SuppressWarnings("unchecked")
- protected static <T> T cast(Class<T> c, Object o) {
- if (o == null)
- return null;
- if (c.isInstance(o))
- return (T)o;
- throw new BasicAssertionError("Object was not expected type.
Expected={0}, Actual={1}.", c.getName(), className(o));
+ protected Object assertNotNull(String parameter, Object value) {
+ return assertNotNull(value, "Parameter ''{0}'' cannot be
null.", parameter);
}
// <FluentSetters>
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentComparableAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentComparableAssertion.java
index f911dee..2fb403c 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentComparableAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentComparableAssertion.java
@@ -56,7 +56,7 @@ public class FluentComparableAssertion<R> extends
FluentObjectAssertion<R> {
*/
public R isGreaterThan(Comparable value) throws AssertionError {
exists();
- assertNotNull(value, "Parameter cannot be null.");
+ assertNotNull("value", value);
if (compareTo(value) <= 0)
throw error("Value was not greater than
expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
return returns();
@@ -85,7 +85,7 @@ public class FluentComparableAssertion<R> extends
FluentObjectAssertion<R> {
*/
public R isGreaterThanOrEqual(Comparable value) throws AssertionError {
exists();
- assertNotNull(value, "Parameter cannot be null.");
+ assertNotNull("value", value);
if (compareTo(value) < 0)
throw error("Value was not greater than or
equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
return returns();
@@ -114,7 +114,7 @@ public class FluentComparableAssertion<R> extends
FluentObjectAssertion<R> {
*/
public R isLessThan(Comparable value) throws AssertionError {
exists();
- assertNotNull(value, "Parameter cannot be null.");
+ assertNotNull("value", value);
if (compareTo(value) >= 0)
throw error("Value was not less than
expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
return returns();
@@ -143,7 +143,7 @@ public class FluentComparableAssertion<R> extends
FluentObjectAssertion<R> {
*/
public R isLessThanOrEqual(Comparable value) throws AssertionError {
exists();
- assertNotNull(value, "Parameter cannot be null.");
+ assertNotNull("value", value);
if (compareTo(value) > 0)
throw error("Value was not less than or equals
to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
return returns();
@@ -173,8 +173,8 @@ public class FluentComparableAssertion<R> extends
FluentObjectAssertion<R> {
*/
public R isBetween(Comparable lower, Comparable upper) throws
AssertionError {
exists();
- assertNotNull(lower, "Parameter 'lower' cannot be null.");
- assertNotNull(upper, "Parameter 'upper' cannot be null.");
+ assertNotNull("lower", lower);
+ assertNotNull("upper", upper);
isLessThanOrEqual(upper);
isGreaterThanOrEqual(lower);
return returns();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentDateAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentDateAssertion.java
index 06aa5d5..75a94c2 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentDateAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentDateAssertion.java
@@ -83,7 +83,7 @@ public class FluentDateAssertion<R> extends
FluentComparableAssertion<R> {
*/
public R isAfter(Date value) throws AssertionError {
exists();
- assertNotNull(value, "Parameter cannot be null.");
+ assertNotNull("value", value);
if (! (this.value.after(value)))
throw error("Value was not after
expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
return returns();
@@ -108,7 +108,7 @@ public class FluentDateAssertion<R> extends
FluentComparableAssertion<R> {
*/
public R isBefore(Date value) throws AssertionError {
exists();
- assertNotNull(value, "Parameter cannot be null.");
+ assertNotNull("value", value);
if (! (this.value.before(value)))
throw error("Value was not before
expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
return returns();
@@ -134,8 +134,8 @@ public class FluentDateAssertion<R> extends
FluentComparableAssertion<R> {
*/
public R isBetween(Date lower, Date upper) throws AssertionError {
exists();
- assertNotNull(lower, "Parameter 'lower' cannot be null.");
- assertNotNull(upper, "Parameter 'upper' cannot be null.");
+ assertNotNull("lower", lower);
+ assertNotNull("upper", upper);
isBefore(upper);
isAfter(lower);
return returns();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
index d32509f..5c464e8 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
@@ -78,7 +78,7 @@ public class FluentObjectAssertion<R> extends
FluentAssertion<R> {
*/
public R isType(Class<?> parent) throws AssertionError {
exists();
- assertNotNull(parent, "Parameter cannot be null.");
+ assertNotNull("parent", parent);
if (! ClassInfo.of(value).isChildOf(parent))
throw error("Unexpected
class.\n\tExpected=[{0}]\n\tActual=[{1}]", className(parent), className(value));
return returns();
@@ -149,6 +149,9 @@ public class FluentObjectAssertion<R> extends
FluentAssertion<R> {
/**
* Verifies that two objects are equivalent after converting them both
to sorted JSON.
*
+ * <p>
+ * Properties, maps, and collections are all sorted on both objects
before comparison.
+ *
* @param o The object to compare against.
* @return The response object (for method chaining).
* @throws AssertionError If assertion failed.
@@ -215,10 +218,9 @@ public class FluentObjectAssertion<R> extends
FluentAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R doesNotEqual(Object value) throws AssertionError {
- if (this.value != value)
+ if (this.value == null && value != null || this.value != null
&& value == null)
return returns();
- exists();
- if (this.value.equals(value))
+ if (this.value == null || this.value.equals(value))
throw error("Unexpected value.\n\tExpected
not=[{0}]\n\tActual=[{1}]", value, this.value);
return returns();
}
@@ -247,6 +249,7 @@ public class FluentObjectAssertion<R> extends
FluentAssertion<R> {
*/
@SuppressWarnings("unchecked")
public <T> R passes(Class<T> c, Predicate<T> test) throws
AssertionError {
+ isType(c);
if (! test.test((T)value))
throw error("Value did not pass predicate
test.\n\tValue=[{0}]", value);
return returns();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentStringAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentStringAssertion.java
index 135c5c0..31156e9 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentStringAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentStringAssertion.java
@@ -86,6 +86,8 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @return This object (for method chaining).
*/
public FluentStringAssertion<R> replaceAll(String regex, String
replacement) {
+ assertNotNull("regex", regex);
+ assertNotNull("replacement", replacement);
return apply(x -> x == null ? null : text.replaceAll(regex,
replacement));
}
@@ -97,6 +99,8 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @return This object (for method chaining).
*/
public FluentStringAssertion<R> replace(String target, String
replacement) {
+ assertNotNull("target", target);
+ assertNotNull("replacement", replacement);
return apply(x -> x == null ? null : text.replace(target,
replacement));
}
@@ -149,38 +153,55 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
/**
* Asserts that the text equals the specified value.
*
+ * <p>
+ * Similar to {@link #is(String)} except error message states diff
position.
+ *
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
* <jc>// Validates the response body of an HTTP call is the text
"OK".</jc>
* client
* .get(<jsf>URL</jsf>)
* .run()
- * .assertBody().equals(<js>"OK"</js>);
+ * .assertBody().isEquals(<js>"OK"</js>);
* </p>
*
- * <p>
- * Multiple values can be passed in to represent multiple lines of
output like so:
+ * @param value
+ * The value to check against.
+ * <br>If multiple values are specified, they are concatenated
with newlines.
+ * @return The response object (for method chaining).
+ * @throws AssertionError If assertion failed.
+ */
+ public R isEqual(String value) throws AssertionError {
+ if (! StringUtils.isEquals(value, text))
+ throw error("Text differed at position
{0}.\n\tExpected=[{1}]\n\tActual=[{2}]", diffPosition(value, text), fix(value),
fix(text));
+ return returns();
+ }
+
+ /**
+ * Asserts that the lines of text equals the specified value.
*
+ * <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
* <jc>// Validates the response body of an HTTP call is the text
"OK".</jc>
* client
* .get(<jsf>URL</jsf>)
* .run()
- * .assertBody().isEqual(
+ * .assertBody().isEqualLines(
* <js>"Line 1"</js>,
* <js>"Line 2"</js>,
* <js>"Line 3"</js>
* );
* </p>
*
- * @param value
+ * @param lines
* The value to check against.
* <br>If multiple values are specified, they are concatenated
with newlines.
* @return The response object (for method chaining).
* @throws AssertionError If assertion failed.
*/
- public R isEqual(String...value) throws AssertionError {
- String v = join(value, '\n');
+ public R isEqualLines(String...lines) throws AssertionError {
+ assertNotNull("lines", lines);
+ String v = join(lines, '\n');
if (! StringUtils.isEquals(v, text))
throw error("Text differed at position
{0}.\n\tExpected=[{1}]\n\tActual=[{2}]", diffPosition(v, text), fix(v),
fix(text));
return returns();
@@ -195,44 +216,35 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* client
* .get(<jsf>URL</jsf>)
* .run()
- * .assertBody().isEqualAfterSort(<js>"OK"</js>);
- * </p>
- *
- * <p>
- * Multiple values can be passed in to represent multiple lines of
output like so:
- *
- * <p class='bcode w800'>
- * <jc>// Validates the response body of an HTTP call is the text
"OK".</jc>
- * client
- * .get(<jsf>URL</jsf>)
- * .run()
- * .assertBody().isEqualAfterSort(
+ * .assertBody().isEqualSortedLines(
* <js>"Line 1"</js>,
* <js>"Line 2"</js>,
* <js>"Line 3"</js>
* );
* </p>
*
- * @param expected
+ * @param lines
* The value to check against.
* <br>If multiple values are specified, they are concatenated
with newlines.
* @return The response object (for method chaining).
* @throws AssertionError If assertion failed.
*/
- public R isEqualAfterSort(String...expected) {
+ public R isEqualSortedLines(String...lines) {
+ assertNotNull("lines", lines);
exists();
+
// Must work for windows too.
- String[] e = StringUtils.join(expected,
'\n').trim().split("[\r\n]+"), a = this.text.trim().split("[\r\n]+");
+ String[] e = StringUtils.join(lines,
'\n').trim().split("[\r\n]+"), a = this.text.trim().split("[\r\n]+");
if (e.length != a.length)
- throw error("Expected text had different numbers of
lines.\n\tExpected=[{0}]\n\tActual={1}]", e.length, a.length);
+ throw error("Expected text had different numbers of
lines.\n\tExpected=[{0}]\n\tActual=[{1}]", e.length, a.length);
Arrays.sort(e);
Arrays.sort(a);
for (int i = 0; i < e.length; i++)
if (! e[i].equals(a[i]))
- throw error("Expected text had different values
at line {0}.\n\tExpected=[{1}]\n\tActual=[{2}]", i, e[i], a[i]);
+ throw error("Expected text had different values
at line {0}.\n\tExpected=[{1}]\n\tActual=[{2}]", i+1, e[i], a[i]);
return returns();
}
@@ -241,7 +253,7 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* Asserts that the text equals the specified value.
*
* <p>
- * Equivalent to {@link #isEqual(String...)}.
+ * Similar to {@link #isEqual(String)} except error message doesn't
state diff position.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
@@ -252,28 +264,15 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* .assertBody().is(<js>"OK"</js>);
* </p>
*
- * <p>
- * Multiple values can be passed in to represent multiple lines of
output like so:
- *
- * <p class='bcode w800'>
- * <jc>// Validates the response body of an HTTP call is the text
"OK".</jc>
- * client
- * .get(<jsf>URL</jsf>)
- * .run()
- * .assertBody().is(
- * <js>"Line 1"</js>,
- * <js>"Line 2"</js>,
- * <js>"Line 3"</js>
- * );
- * </p>
- *
* @param value
* The value to check against.
* <br>If multiple values are specified, they are concatenated
with newlines.
* @return The response object (for method chaining).
* @throws AssertionError If assertion failed.
*/
- public R is(String...value) throws AssertionError {
+ public R is(String value) throws AssertionError {
+ if (! StringUtils.isEquals(value, text))
+ throw error("Unexpected
value.\n\tExpected=[{0}]\n\tActual=[{1}]", fix(value), fix(text));
return isEqual(value);
}
@@ -284,7 +283,7 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @return The response object (for method chaining).
* @throws AssertionError If assertion failed.
*/
- public R isEqualsIc(String value) throws AssertionError {
+ public R isEqualIc(String value) throws AssertionError {
if (! StringUtils.isEqualsIc(value, text))
throw error("Text differed at position
{0}.\n\tExpected=[{1}]\n\tActual=[{2}]", diffPositionIc(value, text),
fix(value), fix(text));
return returns();
@@ -299,7 +298,7 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
*/
public R doesNotEqual(String value) throws AssertionError {
if (StringUtils.isEquals(value, text))
- throw error("Text equaled unexpected.\n\tText=[{1}]",
fix(value), fix(text));
+ throw error("Text equaled unexpected.\n\tText=[{0}]",
fix(text));
return returns();
}
@@ -326,7 +325,7 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
*/
public R doesNotEqualIc(String value) throws AssertionError {
if (StringUtils.isEqualsIc(value, text))
- throw error("Text equaled unexpected.\n\tText=[{1}]",
fix(value));
+ throw error("Text equaled unexpected.\n\tText=[{0}]",
fix(text));
return returns();
}
@@ -338,8 +337,9 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R contains(String...values) throws AssertionError {
+ assertNotNull("values", values);
for (String substring : values)
- if (! StringUtils.contains(text, substring))
+ if (substring != null && ! StringUtils.contains(text,
substring))
throw error("Text did not contain expected
substring.\n\tSubstring=[{0}]\n\tText=[{1}]", fix(substring), fix(text));
return returns();
}
@@ -352,8 +352,9 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R doesNotContain(String...values) throws AssertionError {
+ assertNotNull("values", values);
for (String substring : values)
- if (StringUtils.contains(text, substring))
+ if (substring != null && StringUtils.contains(text,
substring))
throw error("Text contained unexpected
substring.\n\tSubstring=[{0}]\n\tText=[{1}]", fix(substring), fix(text));
return returns();
}
@@ -365,8 +366,8 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R isEmpty() throws AssertionError {
- if (! text.isEmpty())
- throw error("Text was not empty.");
+ if (text != null && ! text.isEmpty())
+ throw error("Text was not empty.\n\tText=[{0}]",
fix(text));
return returns();
}
@@ -406,6 +407,7 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R matchesSimple(String searchPattern) throws AssertionError {
+ assertNotNull("searchPattern", searchPattern);
return matches(getMatchPattern(searchPattern));
}
@@ -417,6 +419,7 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R doesNotMatch(String regex) throws AssertionError {
+ assertNotNull("regex", regex);
return doesNotMatch(regex, 0);
}
@@ -429,6 +432,8 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R matches(String regex, int flags) throws AssertionError {
+ assertNotNull("regex", regex);
+ exists();
Pattern p = Pattern.compile(regex, flags);
if (! p.matcher(text).matches())
throw error("Text did not match expected
pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(regex), fix(text));
@@ -444,10 +449,8 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R doesNotMatch(String regex, int flags) throws AssertionError {
- Pattern p = Pattern.compile(regex, flags);
- if (p.matcher(text).matches())
- throw error("Text matched unexpected
pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(regex), fix(text));
- return returns();
+ assertNotNull("regex", regex);
+ return doesNotMatch(Pattern.compile(regex, flags));
}
/**
@@ -458,6 +461,8 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R matches(Pattern pattern) throws AssertionError {
+ assertNotNull("pattern", pattern);
+ exists();
if (! pattern.matcher(text).matches())
throw error("Text did not match expected
pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(pattern.pattern()), fix(text));
return returns();
@@ -471,7 +476,8 @@ public class FluentStringAssertion<R> extends
FluentObjectAssertion<R> {
* @throws AssertionError If assertion failed.
*/
public R doesNotMatch(Pattern pattern) throws AssertionError {
- if (pattern.matcher(text).matches())
+ assertNotNull("pattern", pattern);
+ if (text != null && pattern.matcher(text).matches())
throw error("Text matched unexpected
pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(pattern.pattern()), fix(text));
return returns();
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
index 0b76594..7140276 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
@@ -62,6 +62,7 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @return This object (for method chaining).
*/
public R isType(Class<?> type) {
+ assertNotNull("type", type);
if (! type.isInstance(value))
throw error("Exception was not expected
type.\n\tExpected=[{0}]\n\tActual=[{1}]", className(type), className(value));
return returns();
@@ -80,16 +81,19 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @return This object (for method chaining).
*/
public R contains(String...substrings) {
+ assertNotNull("substrings", substrings);
exists();
for (String substring : substrings) {
- Throwable e2 = value;
- boolean found = false;
- while (e2 != null && ! found) {
- found |= StringUtils.contains(e2.getMessage(),
substring);
- e2 = e2.getCause();
- }
- if (! found) {
- throw error("Exception message did not contain
expected substring.\n\tSubstring=[{0}]\n\tText=[{1}]", substring,
value.getMessage());
+ if (substring != null) {
+ Throwable e2 = value;
+ boolean found = false;
+ while (e2 != null && ! found) {
+ found |=
StringUtils.contains(e2.getMessage(), substring);
+ e2 = e2.getCause();
+ }
+ if (! found) {
+ throw error("Exception message did not
contain expected substring.\n\tSubstring=[{0}]\n\tText=[{1}]", substring,
value.getMessage());
+ }
}
}
return returns();
@@ -152,13 +156,30 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @return The response object (for method chaining).
* @throws AssertionError If assertion failed.
*/
- public R passes(Predicate<Object> test) throws AssertionError {
+ public R passes(Predicate<Throwable> test) throws AssertionError {
if (! test.test(value))
throw error("Value did not pass predicate
test.\n\tValue=[{0}]", value);
return returns();
}
/**
+ * Asserts that the value passes the specified predicate test.
+ *
+ * @param c The class to cast to for the predicate.
+ * @param <T> The class to cast to for the predicate.
+ * @param test The predicate to use to test the value.
+ * @return The response object (for method chaining).
+ * @throws AssertionError If assertion failed.
+ */
+ @SuppressWarnings("unchecked")
+ public <T extends Throwable> R passes(Class<T> c, Predicate<T> test)
throws AssertionError {
+ isType(c);
+ if (! test.test((T) value))
+ throw error("Value did not pass predicate
test.\n\tValue=[{0}]", value);
+ return returns();
+ }
+
+ /**
* Returns an assertion against the throwable message.
*
* <h5 class='section'>Example:</h5>
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Verify.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Verify.java
index 9413e2d..16590be 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Verify.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Verify.java
@@ -74,11 +74,11 @@ public class Verify {
* @return An error message if the object is not of the specified type,
otherwise <jk>null</jk>.
*/
public String isType(Class<?> type) {
- if (type.isInstance(o))
+ if (type == null && o == null)
return null;
- if (msg == null)
- msg = () -> StringUtils.format("Expected type {0} but
was {1}", type, (o == null ? null : o.getClass()));
- return msg.get();
+ if (type != null && type.isInstance(o))
+ return null;
+ return msg != null ? msg.get() : StringUtils.format("Expected
type [{0}] but was [{1}].", type, (o == null ? null : o.getClass()));
}
/**
@@ -91,9 +91,9 @@ public class Verify {
if (expected == o)
return null;
if (expected == null || o == null)
- return StringUtils.format("Expected {0} but was {1}",
expected, o);
+ return msg != null ? msg.get() :
StringUtils.format("Expected [{0}] but was [{1}].", expected, o);
if (! expected.equals(o))
- return StringUtils.format("Expected {0} but was {1}",
expected, o);
+ return msg != null ? msg.get() :
StringUtils.format("Expected [{0}] but was [{1}].", expected, o);
return null;
}
diff --git
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Logging_Test.java
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Logging_Test.java
index 2bb20ef..976f847 100644
---
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Logging_Test.java
+++
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Logging_Test.java
@@ -70,7 +70,7 @@ public class RestClient_Logging_Test {
c.reset();
client().logRequests(DetailLevel.FULL,Level.SEVERE,null).logToConsole().logger(l).console(c).build().post("/bean",bean).complete();
- c.assertContents().is(
+ c.assertContents().isEqualLines(
"",
"=== HTTP Call (outgoing)
======================================================",
"=== REQUEST ===",
@@ -93,7 +93,7 @@ public class RestClient_Logging_Test {
c.reset();
client().logRequests(DetailLevel.FULL,Level.SEVERE,null).logToConsole().logger(l).console(c).build().get("/bean").complete();
- c.assertContents().is(
+ c.assertContents().isEqualLines(
"",
"=== HTTP Call (outgoing)
======================================================",
"=== REQUEST ===",
@@ -112,7 +112,7 @@ public class RestClient_Logging_Test {
c.reset();
clientPlain().logRequests(DetailLevel.FULL,Level.SEVERE,null).logToConsole().logger(l).console(c).build().post("/stream",new
InputStreamEntity(new ByteArrayInputStream("foo".getBytes()))).complete();
- c.assertContents().is(
+ c.assertContents().isEqualLines(
"",
"=== HTTP Call (outgoing)
======================================================",
"=== REQUEST ===",
@@ -154,7 +154,7 @@ public class RestClient_Logging_Test {
client().logger(l).logRequests(DetailLevel.FULL,Level.WARNING,null).build().post("/bean",bean).complete();
l.assertLastLevel(Level.WARNING);
- l.assertLastMessage().is(
+ l.assertLastMessage().isEqualLines(
"",
"=== HTTP Call (outgoing)
======================================================",
"=== REQUEST ===",
@@ -173,7 +173,7 @@ public class RestClient_Logging_Test {
"{f:1}",
"=== END
======================================================================="
);
- l.assertContents().stderr().javaStrings().is(
+ l.assertContents().stderr().javaStrings().isEqualLines(
"WARNING: ",
"=== HTTP Call (outgoing)
======================================================",
"=== REQUEST ===",