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 6152565 RestClient tests 6152565 is described below commit 61525655c23fd7c43fa001961ad1fe8280c4e439 Author: JamesBognar <james.bog...@salesforce.com> AuthorDate: Thu Jun 11 09:41:03 2020 -0400 RestClient tests --- .../apache/juneau/assertions/FluentAssertion.java | 64 ++++++++ .../juneau/assertions/FluentDateAssertion.java | 39 ++++- .../juneau/assertions/FluentIntegerAssertion.java | 47 ++++-- .../juneau/assertions/FluentLongAssertion.java | 47 ++++-- .../juneau/assertions/FluentStringAssertion.java | 166 +++++++++++---------- .../FluentSetters.java} | 35 ++--- .../main/ConfigurablePropertyCodeGenerator.java | 16 +- .../apache/juneau/rest/client2/RestClientTest.java | 74 ++++++--- 8 files changed, 333 insertions(+), 155 deletions(-) diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentAssertion.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentAssertion.java index 515bd7a..cb13360 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentAssertion.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentAssertion.java @@ -12,6 +12,9 @@ // *************************************************************************************************************************** package org.apache.juneau.assertions; +import org.apache.juneau.*; +import org.apache.juneau.internal.*; + /** * Parent class of all fluent assertion calls. * @@ -20,6 +23,8 @@ package org.apache.juneau.assertions; public abstract class FluentAssertion<R> { private final R returns; + private String msg; + private boolean stdout, stderr; /** * Constructor. @@ -38,4 +43,63 @@ public abstract class FluentAssertion<R> { protected R returns() { return returns; } + + /** + * Allows to to specify the assertion failure message. + * + * <p> + * String can contain <js>"{msg}"</js> to represent the original message. + * + * @param msg The assertion failure message. + * @return This object (for method chaining). + */ + @FluentSetter + public FluentAssertion<R> msg(String msg) { + this.msg = msg; + return this; + } + + /** + * If an error occurs, send the error message to STDOUT. + * + * @return This object (for method chaining). + */ + @FluentSetter + public FluentAssertion<R> stdout() { + this.stdout = true; + return this; + } + + /** + * If an error occurs, send the error message to STDERR. + * + * @return This object (for method chaining). + */ + @FluentSetter + public FluentAssertion<R> stderr() { + this.stderr = true; + return this; + } + + /** + * Creates a new {@link BasicAssertionError}. + * + * @param msg The message. + * @param args The message arguments. + * @return A new {@link BasicAssertionError}. + */ + protected BasicAssertionError error(String msg, Object...args) { + msg = StringUtils.format(msg, args); + if (this.msg != null) { + if (this.msg.contains("{msg}")) + msg = this.msg.replace("{msg}", msg); + else + msg = this.msg; + } + if (stdout) + System.out.println(msg); + if (stderr) + System.err.println(msg); + return new BasicAssertionError(msg); + } } 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 2a84ddc..27cf562 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 @@ -16,7 +16,7 @@ package org.apache.juneau.assertions; import java.util.*; import java.util.function.*; -import org.apache.juneau.*; +import org.apache.juneau.internal.*; /** * Used for fluent assertion calls. @@ -31,6 +31,7 @@ import org.apache.juneau.*; * </p> * @param <R> The return type. */ +@FluentSetters(returns="FluentDateAssertion<R>") public class FluentDateAssertion<R> extends FluentAssertion<R> { private final Date value; @@ -55,7 +56,7 @@ public class FluentDateAssertion<R> extends FluentAssertion<R> { */ public R equals(Date value) throws AssertionError { if (! this.value.equals(value)) - throw new BasicAssertionError("Unexpected value.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Unexpected value.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -82,7 +83,7 @@ public class FluentDateAssertion<R> extends FluentAssertion<R> { */ public R doesNotEqual(Date value) throws AssertionError { if (this.value.equals(value)) - throw new BasicAssertionError("Unexpected value.\n\tExpected not=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Unexpected value.\n\tExpected not=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -134,7 +135,7 @@ public class FluentDateAssertion<R> extends FluentAssertion<R> { */ public R isNull() throws AssertionError { if (value != null) - throw new BasicAssertionError("Value was not null."); + throw error("Value was not null."); return returns(); } @@ -146,7 +147,7 @@ public class FluentDateAssertion<R> extends FluentAssertion<R> { */ public R isNotNull() throws AssertionError { if (value == null) - throw new BasicAssertionError("Value was null."); + throw error("Value was null."); return returns(); } @@ -159,7 +160,7 @@ public class FluentDateAssertion<R> extends FluentAssertion<R> { */ public R isAfter(Date value) throws AssertionError { if (! (this.value.after(value))) - throw new BasicAssertionError("Value was not greater than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not greater than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -172,7 +173,7 @@ public class FluentDateAssertion<R> extends FluentAssertion<R> { */ public R isBefore(Date value) throws AssertionError { if (! (this.value.before(value))) - throw new BasicAssertionError("Value was not less than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not less than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -199,7 +200,29 @@ public class FluentDateAssertion<R> extends FluentAssertion<R> { */ public R passes(Predicate<Date> test) throws AssertionError { if (! test.test(value)) - throw new BasicAssertionError("Value did not pass predicate test.\n\tValue=[{0}]", value); + throw error("Value did not pass predicate test.\n\tValue=[{0}]", value); return returns(); } + + // <FluentSetters> + + @Override /* GENERATED - FluentAssertion */ + public FluentDateAssertion<R> msg(String msg) { + super.msg(msg); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentDateAssertion<R> stderr() { + super.stderr(); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentDateAssertion<R> stdout() { + super.stdout(); + return this; + } + + // </FluentSetters> } diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentIntegerAssertion.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentIntegerAssertion.java index ccdc88d..f87facc 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentIntegerAssertion.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentIntegerAssertion.java @@ -15,7 +15,7 @@ package org.apache.juneau.assertions; import java.util.function.*; -import org.apache.juneau.*; +import org.apache.juneau.internal.*; import org.apache.juneau.marshall.*; /** @@ -31,6 +31,7 @@ import org.apache.juneau.marshall.*; * </p> * @param <R> The return type. */ +@FluentSetters(returns="FluentIntegerAssertion<R>") public class FluentIntegerAssertion<R> extends FluentAssertion<R> { private final Integer value; @@ -58,7 +59,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { return returns(); exists(); if (! this.value.equals(value)) - throw new BasicAssertionError("Unexpected value.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Unexpected value.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -88,7 +89,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { return returns(); exists(); if (this.value.equals(value)) - throw new BasicAssertionError("Unexpected value.\n\tExpected not=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Unexpected value.\n\tExpected not=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -140,7 +141,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { */ public R isNull() throws AssertionError { if (value != null) - throw new BasicAssertionError("Value was not null."); + throw error("Value was not null."); return returns(); } @@ -152,7 +153,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { */ public R isNotNull() throws AssertionError { if (value == null) - throw new BasicAssertionError("Value was null."); + throw error("Value was null."); return returns(); } @@ -168,7 +169,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { for (Integer v : values) if (this.value.equals(v)) return returns(); - throw new BasicAssertionError("Expected value not found.\n\tExpected=[{0}]\n\tActual=[{1}]", SimpleJson.DEFAULT.toString(values), value); + throw error("Expected value not found.\n\tExpected=[{0}]\n\tActual=[{1}]", SimpleJson.DEFAULT.toString(values), value); } /** @@ -182,7 +183,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { exists(); for (Integer v : values) if (this.value.equals(v)) - throw new BasicAssertionError("Unexpected value found.\n\tUnexpected=[{0}]\n\tActual=[{1}]", v, value); + throw error("Unexpected value found.\n\tUnexpected=[{0}]\n\tActual=[{1}]", v, value); return returns(); } @@ -196,7 +197,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { public R isGreaterThan(Integer value) throws AssertionError { exists(); if (! (this.value > value)) - throw new BasicAssertionError("Value was not greater than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not greater than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -224,7 +225,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { public R isGreaterThanOrEquals(Integer value) throws AssertionError { exists(); if (! (this.value >= value)) - throw new BasicAssertionError("Value was not greater than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not greater than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -252,7 +253,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { public R isLessThan(Integer value) throws AssertionError { exists(); if (! (this.value < value)) - throw new BasicAssertionError("Value was not less than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not less than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -280,7 +281,7 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { public R isLessThanOrEquals(Integer value) throws AssertionError { exists(); if (! (this.value <= value)) - throw new BasicAssertionError("Value was not less than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not less than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -321,7 +322,29 @@ public class FluentIntegerAssertion<R> extends FluentAssertion<R> { */ public R passes(Predicate<Integer> test) throws AssertionError { if (! test.test(value)) - throw new BasicAssertionError("Value did not pass predicate test.\n\tValue=[{0}]", value); + throw error("Value did not pass predicate test.\n\tValue=[{0}]", value); return returns(); } + + // <FluentSetters> + + @Override /* GENERATED - FluentAssertion */ + public FluentIntegerAssertion<R> msg(String msg) { + super.msg(msg); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentIntegerAssertion<R> stderr() { + super.stderr(); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentIntegerAssertion<R> stdout() { + super.stdout(); + return this; + } + + // </FluentSetters> } diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java index 47b2db0..5a80917 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java @@ -15,7 +15,7 @@ package org.apache.juneau.assertions; import java.util.function.*; -import org.apache.juneau.*; +import org.apache.juneau.internal.*; import org.apache.juneau.marshall.*; /** @@ -31,6 +31,7 @@ import org.apache.juneau.marshall.*; * </p> * @param <R> The return type. */ +@FluentSetters(returns="FluentLongAssertion<R>") public class FluentLongAssertion<R> extends FluentAssertion<R> { private final Long value; @@ -58,7 +59,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { return returns(); exists(); if (! this.value.equals(value)) - throw new BasicAssertionError("Unexpected value.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Unexpected value.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -88,7 +89,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { return returns(); exists(); if (this.value.equals(value)) - throw new BasicAssertionError("Unexpected value.\n\tExpected not=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Unexpected value.\n\tExpected not=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -140,7 +141,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { */ public R isNull() throws AssertionError { if (value != null) - throw new BasicAssertionError("Value was not null."); + throw error("Value was not null."); return returns(); } @@ -152,7 +153,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { */ public R isNotNull() throws AssertionError { if (value == null) - throw new BasicAssertionError("Value was null."); + throw error("Value was null."); return returns(); } @@ -168,7 +169,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { for (Long v : values) if (this.value.equals(v)) return returns(); - throw new BasicAssertionError("Expected value not found.\n\tExpected=[{0}]\n\tActual=[{1}]", SimpleJson.DEFAULT.toString(values), value); + throw error("Expected value not found.\n\tExpected=[{0}]\n\tActual=[{1}]", SimpleJson.DEFAULT.toString(values), value); } /** @@ -182,7 +183,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { exists(); for (Long v : values) if (this.value.equals(v)) - throw new BasicAssertionError("Unexpected value found.\n\tUnexpected=[{0}]\n\tActual=[{1}]", v, value); + throw error("Unexpected value found.\n\tUnexpected=[{0}]\n\tActual=[{1}]", v, value); return returns(); } @@ -196,7 +197,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { public R isGreaterThan(Long value) throws AssertionError { exists(); if (! (this.value > value)) - throw new BasicAssertionError("Value was not greater than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not greater than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -224,7 +225,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { public R isGreaterThanOrEquals(Long value) throws AssertionError { exists(); if (! (this.value >= value)) - throw new BasicAssertionError("Value was not greater than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not greater than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -252,7 +253,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { public R isLessThan(Long value) throws AssertionError { exists(); if (! (this.value < value)) - throw new BasicAssertionError("Value was not less than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not less than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -280,7 +281,7 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { public R isLessThanOrEquals(Long value) throws AssertionError { exists(); if (! (this.value <= value)) - throw new BasicAssertionError("Value was not less than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); + throw error("Value was not less than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value); return returns(); } @@ -321,7 +322,29 @@ public class FluentLongAssertion<R> extends FluentAssertion<R> { */ public R passes(Predicate<Long> test) throws AssertionError { if (! test.test(value)) - throw new BasicAssertionError("Value did not pass predicate test.\n\tValue=[{0}]", value); + throw error("Value did not pass predicate test.\n\tValue=[{0}]", value); return returns(); } + + // <FluentSetters> + + @Override /* GENERATED - FluentAssertion */ + public FluentLongAssertion<R> msg(String msg) { + super.msg(msg); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentLongAssertion<R> stderr() { + super.stderr(); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentLongAssertion<R> stdout() { + super.stdout(); + return this; + } + + // </FluentSetters> } 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 d42386c..01a847b 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 @@ -17,7 +17,6 @@ import static org.apache.juneau.internal.StringUtils.*; import java.util.function.*; import java.util.regex.*; -import org.apache.juneau.*; import org.apache.juneau.internal.*; /** @@ -33,9 +32,11 @@ import org.apache.juneau.internal.*; * </p> * @param <R> The return type. */ +@FluentSetters(returns="FluentStringAssertion<R>") public class FluentStringAssertion<R> extends FluentAssertion<R> { private final String text; + private boolean javaStrings; /** * Constructor. @@ -49,6 +50,21 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { } /** + * When enabled, text in the message is converted to valid Java strings. + * + * <p class='bcode w800'> + * value.replaceAll(<js>"\\\\"</js>, <js>"\\\\\\\\"</js>).replaceAll(<js>"\n"</js>, <js>"\\\\n"</js>).replaceAll(<js>"\t"</js>, <js>"\\\\t"</js>); + * </p> + * + * @return This object (for method chaining). + */ + @FluentSetter + public FluentStringAssertion<R> javaStrings() { + this.javaStrings = true; + return this; + } + + /** * Asserts that the text equals the specified value. * * @param value The value to check against. @@ -56,16 +72,8 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { * @throws AssertionError If assertion failed. */ public R equals(String value) throws AssertionError { - if (! isEquals(value, text)) { - if (value != null && value.startsWith("x")) { - StringBuilder sb = new StringBuilder(); - sb.append("Text did not equal expected."); - sb.append("\nExpected: [").append(value.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - sb.append("\nActual : [").append(text == null ? null : text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - System.err.println(sb.toString()); - } - throw new BasicAssertionError("Text did not equal expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, text); - } + if (! isEquals(value, text)) + throw error("Text did not equal expected.\n\tExpected=[{0}]\n\tActual=[{1}]", fix(value), fix(text)); return returns(); } @@ -92,16 +100,8 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R urlDecodedIs(String value) throws AssertionError { String t = urlDecode(text); - if (! isEqualsIc(value, t)) { - if (value != null && value.startsWith("x")) { - StringBuilder sb = new StringBuilder(); - sb.append("Text did not equal expected."); - sb.append("\nExpected: [").append(value.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - sb.append("\nActual : [").append(t.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - System.err.println(sb.toString()); - } - throw new BasicAssertionError("Text did not equal expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, t); - } + if (! isEqualsIc(value, t)) + throw error("Text did not equal expected.\n\tExpected=[{0}]\n\tActual=[{1}]", fix(value), fix(t)); return returns(); } @@ -113,16 +113,8 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { * @throws AssertionError If assertion failed. */ public R equalsIc(String value) throws AssertionError { - if (! isEqualsIc(value, text)) { - if (value != null && value.startsWith("x")) { - StringBuilder sb = new StringBuilder(); - sb.append("Text did not equal expected."); - sb.append("\nExpected: [").append(value.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - sb.append("\nActual : [").append(text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - System.err.println(sb.toString()); - } - throw new BasicAssertionError("Text did not equal expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, text); - } + if (! isEqualsIc(value, text)) + throw error("Text did not equal expected.\n\tExpected=[{0}]\n\tActual=[{1}]", fix(value), fix(text)); return returns(); } @@ -134,15 +126,8 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { * @throws AssertionError If assertion failed. */ public R doesNotEqual(String value) throws AssertionError { - if (isEquals(value, text)) { - if (value != null && value.startsWith("x")) { - StringBuilder sb = new StringBuilder(); - sb.append("Text equaled unexpected."); - sb.append("\nText: [").append(value.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - System.err.println(sb.toString()); - } - throw new BasicAssertionError("Text equaled unexpected.\n\tText=[{1}]", value, text); - } + if (isEquals(value, text)) + throw error("Text equaled unexpected.\n\tText=[{1}]", fix(value), fix(text)); return returns(); } @@ -168,15 +153,8 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { * @throws AssertionError If assertion failed. */ public R doesNotEqualIc(String value) throws AssertionError { - if (isEqualsIc(value, text)) { - if (value != null && value.startsWith("x")) { - StringBuilder sb = new StringBuilder(); - sb.append("Text equaled unexpected."); - sb.append("\nText: [").append(value.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - System.err.println(sb.toString()); - } - throw new BasicAssertionError("Text equaled unexpected.\n\tText=[{1}]", value, text); - } + if (isEqualsIc(value, text)) + throw error("Text equaled unexpected.\n\tText=[{1}]", fix(value)); return returns(); } @@ -189,16 +167,8 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R contains(String...values) throws AssertionError { for (String substring : values) - if (! StringUtils.contains(text, substring)) { - if (substring.startsWith("x")) { - StringBuilder sb = new StringBuilder(); - sb.append("Text did not contain expected substring."); - sb.append("\nSubstring: [").append(substring.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - sb.append("\nText : [").append(text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - System.err.println(sb.toString()); - } - throw new BasicAssertionError("Text did not contain expected substring.\n\tExpected=[{0}]\n\tActual=[{1}]", substring, text); - } + if (! StringUtils.contains(text, substring)) + throw error("Text did not contain expected substring.\n\tSubstring=[{0}]\n\tText=[{1}]", fix(substring), fix(text)); return returns(); } @@ -211,16 +181,8 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R doesNotContain(String...values) throws AssertionError { for (String substring : values) - if (StringUtils.contains(text, substring)) { - if (substring.startsWith("x")) { - StringBuilder sb = new StringBuilder(); - sb.append("Text contained unexpected substring."); - sb.append("\nSubstring: [").append(substring.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - sb.append("\nText : [").append(text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t")).append("]"); - System.err.println(sb.toString()); - } - throw new BasicAssertionError("Text contained unexpected substring.\n\tExpected=[{0}]\n\tActual=[{1}]", substring, text); - } + if (StringUtils.contains(text, substring)) + throw error("Text contained unexpected substring.\n\tSubstring=[{0}]\n\tText=[{1}]", fix(substring), fix(text)); return returns(); } @@ -258,7 +220,7 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R isNull() throws AssertionError { if (text != null) - throw new BasicAssertionError("Text was not null. Text=["+text+"]"); + throw error("Text was not null. Text=[{0}]", fix(text)); return returns(); } @@ -270,7 +232,7 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R isNotNull() throws AssertionError { if (text == null) - throw new BasicAssertionError("Text was null."); + throw error("Text was null."); return returns(); } @@ -282,7 +244,7 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R isEmpty() throws AssertionError { if (! text.isEmpty()) - throw new BasicAssertionError("Text was not empty."); + throw error("Text was not empty."); return returns(); } @@ -294,9 +256,9 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R isNotEmpty() throws AssertionError { if (text == null) - throw new BasicAssertionError("Text was null."); + throw error("Text was null."); if (text.isEmpty()) - throw new BasicAssertionError("Text was empty."); + throw error("Text was empty."); return returns(); } @@ -309,7 +271,7 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R passes(Predicate<String> test) throws AssertionError { if (! test.test(text)) - throw new BasicAssertionError("Text did not pass predicate test.\n\tText=[{0}]", text); + throw error("Text did not pass predicate test.\n\tText=[{0}]", fix(text)); return returns(); } @@ -325,6 +287,20 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { } /** + * Asserts that the text matches the specified pattern containing <js>"*"</js> meta characters. + * + * <p> + * The <js>"*"</js> meta character can be used to represent zero or more characters.. + * + * @param searchPattern The search pattern. + * @return The response object (for method chaining). + * @throws AssertionError If assertion failed. + */ + public R matchesSimple(String searchPattern) throws AssertionError { + return matches(getMatchPattern(searchPattern)); + } + + /** * Asserts that the text doesn't match the specified regular expression. * * @param regex The pattern to test for. @@ -346,7 +322,7 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { public R matches(String regex, int flags) throws AssertionError { Pattern p = Pattern.compile(regex, flags); if (! p.matcher(text).matches()) - throw new BasicAssertionError("Text did not match expected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", regex, text); + throw error("Text did not match expected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(regex), fix(text)); return returns(); } @@ -361,7 +337,7 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { public R doesNotMatch(String regex, int flags) throws AssertionError { Pattern p = Pattern.compile(regex, flags); if (p.matcher(text).matches()) - throw new BasicAssertionError("Text matched unexpected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", regex, text); + throw error("Text matched unexpected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(regex), fix(text)); return returns(); } @@ -374,7 +350,7 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R matches(Pattern pattern) throws AssertionError { if (! pattern.matcher(text).matches()) - throw new BasicAssertionError("Text did not match expected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", pattern.pattern(), text); + throw error("Text did not match expected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(pattern.pattern()), fix(text)); return returns(); } @@ -387,7 +363,39 @@ public class FluentStringAssertion<R> extends FluentAssertion<R> { */ public R doesNotMatch(Pattern pattern) throws AssertionError { if (pattern.matcher(text).matches()) - throw new BasicAssertionError("Text matched unexpected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", pattern.pattern(), text); + throw error("Text matched unexpected pattern.\n\tPattern=[{0}]\n\tText=[{1}]", fix(pattern.pattern()), fix(text)); return returns(); } + + //------------------------------------------------------------------------------------------------------------------ + // Utility methods + //------------------------------------------------------------------------------------------------------------------ + + private String fix(String text) { + if (javaStrings) + text = text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t"); + return text; + } + + // <FluentSetters> + + @Override /* GENERATED - FluentAssertion */ + public FluentStringAssertion<R> msg(String msg) { + super.msg(msg); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentStringAssertion<R> stderr() { + super.stderr(); + return this; + } + + @Override /* GENERATED - FluentAssertion */ + public FluentStringAssertion<R> stdout() { + super.stdout(); + return this; + } + + // </FluentSetters> } diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentAssertion.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/FluentSetters.java similarity index 75% copy from juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentAssertion.java copy to juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/FluentSetters.java index 515bd7a..cdbf2ed 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentAssertion.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/FluentSetters.java @@ -10,32 +10,23 @@ // * "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; +package org.apache.juneau.internal; -/** - * Parent class of all fluent assertion calls. - * - * @param <R> The return type. - */ -public abstract class FluentAssertion<R> { +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; - private final R returns; +import java.lang.annotation.*; - /** - * Constructor. - * - * @param returns The object to return after the test. - */ - protected FluentAssertion(R returns) { - this.returns = returns; - } +/** + * Used in conjunction with the ConfigurablePropertyCodeGenerator class to synchronize and copy fluent setters from + * parent classes to child classes. + */ +@Target(TYPE) +@Retention(RUNTIME) +public @interface FluentSetters { /** - * Returns the object that the fluent methods on this class should return. - * - * @return The response object. + * Overrides the return type on the child methods. */ - protected R returns() { - return returns; - } + String returns() default ""; } diff --git a/juneau-releng/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java b/juneau-releng/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java index b2cbfa3..fcf84d7 100644 --- a/juneau-releng/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java +++ b/juneau-releng/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java @@ -17,6 +17,7 @@ import java.util.*; import java.util.stream.*; import org.apache.juneau.*; +import org.apache.juneau.assertions.*; import org.apache.juneau.config.*; import org.apache.juneau.config.store.*; import org.apache.juneau.csv.*; @@ -157,7 +158,12 @@ public class ConfigurablePropertyCodeGenerator { SessionArgs.class, BeanSessionArgs.class, SerializerSessionArgs.class, - ParserSessionArgs.class + ParserSessionArgs.class, + FluentAssertion.class, + FluentDateAssertion.class, + FluentIntegerAssertion.class, + FluentLongAssertion.class, + FluentStringAssertion.class, }; private static String[] SOURCE_PATHS = { @@ -204,6 +210,12 @@ public class ConfigurablePropertyCodeGenerator { .collect(Collectors.toMap(x -> ("\n\tpublic"+x[1].substring(0, x[1].indexOf("\n"))), x -> ("\t"+x[0]+"*/\n"))); StringBuilder sb = new StringBuilder(); + ClassInfo ci = ClassInfo.of(c); + String cName = ci.getSimpleName(); + for (FluentSetters fs : ci.getAnnotations(FluentSetters.class)) + if (! fs.returns().isEmpty()) + cName = fs.returns(); + for (ClassInfo pc : ClassInfo.of(c).getParentsParentFirst()) { Class<?> pcc = pc.inner(); if (pcc != c) { @@ -219,7 +231,7 @@ public class ConfigurablePropertyCodeGenerator { sigLine.append("\n\tpublic "); if (m.getTypeParameters().length > 0) sigLine.append("<").append(Arrays.asList(m.getTypeParameters()).stream().map(x -> x.getName()).collect(Collectors.joining(", "))).append("> "); - sigLine.append(c.getSimpleName()).append(" ").append(m.getName()).append("(").append(getArgs(m)).append(") "); + sigLine.append(cName).append(" ").append(m.getName()).append("(").append(getArgs(m)).append(") "); if ( m.getExceptionTypes().length > 0) sigLine.append("throws ").append(Arrays.asList(m.getExceptionTypes()).stream().map(x -> x.getSimpleName()).collect(Collectors.joining(", "))); sigLine.append("{"); diff --git a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java index 1e12e17..c4280c2 100644 --- a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java +++ b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java @@ -143,6 +143,10 @@ public class RestClientTest { public String[] getHeader(org.apache.juneau.rest.RestRequest req) { return req.getHeaders().get(req.getHeader("Check")); } + @RestMethod(path="/checkHeader") + public String[] postHeader(org.apache.juneau.rest.RestRequest req) { + return req.getHeaders().get(req.getHeader("Check")); + } @RestMethod(path="/checkQuery") public Reader getQuery(org.apache.juneau.rest.RestRequest req) { return new StringReader(req.getQuery().asQueryString()); @@ -566,26 +570,56 @@ public class RestClientTest { } } - @Test - public void a23_basicCalls_formPost_exhaustiveBodyTypes() throws Exception { - List<Object> bodies = AList.of( - bean, - NameValuePairs.of("f","1"), - new NameValuePair[]{BasicNameValuePair.of("f","1")}, - new StringEntity("{f:1}", org.apache.http.entity.ContentType.APPLICATION_JSON), - BasicNameValuePair.of("f","1") - ); - - for (Object body : bodies) { - MockRestClient - .create(A.class) - .build() - .formPost("/bean", body) - .accept("application/json+simple") - .run() - .assertBody().is("{f:1}"); - } - } +// @Test +// public void a23_basicCalls_formPost_exhaustiveBodyTypes() throws Exception { +// +// +// List<Object> bodies = AList.of( +// bean, +// NameValuePairs.of("f","1"), +// new NameValuePair[]{BasicNameValuePair.of("f","1")}, +// new StringEntity("{f:1}", org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED), +// new StringEntity("{f:1}", (org.apache.http.entity.ContentType)null), +// BasicNameValuePair.of("f","1") +// ); +// +// for (int i = 0; i < bodies.size(); i++) { +// MockRestClient +// .create(A.class) +// .header("Check", "Content-Type") +// .accept("application/json+simple") +// .build() +// .formPost("/checkHeader", body) +// .run() +// .assertBody().matchesSimple("['application/x-www-form-urlencoded*']"); +// +// MockRestClient +// .create(A.class) +// .build() +// .formPost("/bean", body) +// .accept("application/json+simple") +// .run() +// .assertBody().is("{f:1}"); +// } +// +// +// bodies = AList.of( +// bean, +// NameValuePairs.of("f","1"), +// new NameValuePair[]{BasicNameValuePair.of("f","1")}, +// new StringEntity("{f:1}", org.apache.http.entity.ContentType.APPLICATION_JSON), +// BasicNameValuePair.of("f","1") +// ); +// +// for (Object body : bodies) { +// MockRestClient +// .create(A.class) +// .build() +// .formPost("/echoBody", body) +// .run() +// .assertBody().is("{f:1}"); +// } +// } @Test public void a24_basicCalls_formPostPairs() throws Exception {