From e3a78a308c1075af82603492dcc26dd7245328c9 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 6 Oct 2024 22:26:02 +0200 Subject: [PATCH 01/16] Change: create base matcher for Runnables throwing exceptions The matcher matches specific exceptions for now (matching the extact class and message) --- .../hamcrest/exception/ThrowsException.java | 53 +++++++++++++++++++ .../exception/ThrowsExceptionTest.java | 40 ++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java create mode 100644 hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java new file mode 100644 index 00000000..c730592b --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -0,0 +1,53 @@ +package org.hamcrest.exception; + +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; + +/** + * A matcher that checks if a Runnable throws an expected exception when run. + * + * @param the type of the expected exception. + */ +public class ThrowsException extends TypeSafeMatcher { + private final T expectedException; + private Throwable actualThrowable; + + /** + * Constructor + * + * @param expectedException the expected exception. + */ + ThrowsException(T expectedException) { + super(Runnable.class); + this.expectedException = expectedException; + } + + public static ThrowsException throwsException(U expectedException) { + return new ThrowsException<>(expectedException); + } + + @Override + protected boolean matchesSafely(Runnable item) { + try { + item.run(); + return false; + } catch (Throwable t) { + this.actualThrowable = t; + return expectedException.getClass().isInstance(t) && t.getMessage().equals(expectedException.getMessage()); + } + } + + @Override + protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { + if (expectedException.getClass().isInstance(actualThrowable)) { + mismatchDescription.appendText("threw ").appendValue(actualThrowable.getClass().getName()).appendText(" with message ").appendValue(actualThrowable.getMessage()).appendText(" instead of ").appendValue(expectedException.getMessage()); + return; + } + mismatchDescription.appendText("threw a ").appendValue(actualThrowable.getClass().getName()).appendText(" instead of a ").appendValue(expectedException.getClass().getName()).appendText(" exception"); + } + + @Override + public void describeTo(Description description) { + description.appendText("throws ").appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage()); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java new file mode 100644 index 00000000..d70cdcce --- /dev/null +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java @@ -0,0 +1,40 @@ +package org.hamcrest.exception; + +import org.hamcrest.AbstractMatcherTest; +import org.hamcrest.Matcher; + +import static org.hamcrest.exception.ThrowsException.throwsException; + +public class ThrowsExceptionTest extends AbstractMatcherTest { + + private static Runnable runnableThrowing(String message) { + return () -> { + throw new IllegalArgumentException(message); + }; + } + + private static Runnable runnableThrowing(Class exceptionClass, String message) { + return () -> { + try { + throw exceptionClass.getDeclaredConstructor().newInstance(message); + } catch (Throwable e) { + throw new RuntimeException(e); + } + }; + } + + @Override + protected Matcher createMatcher() { + return throwsException(new IllegalArgumentException("Boom!")); + } + + public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { + Matcher matcher = throwsException(new IllegalArgumentException("Boom!")); + + assertMatches(matcher, runnableThrowing("Boom!")); + assertDoesNotMatch(matcher, runnableThrowing("Bang!")); + assertMismatchDescription("threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"", matcher, runnableThrowing("Bang!")); + assertDoesNotMatch(matcher, runnableThrowing(NullPointerException.class, "Bang!")); + assertMismatchDescription("threw a \"java.lang.RuntimeException\" instead of a \"java.lang.IllegalArgumentException\" exception", matcher, runnableThrowing(NullPointerException.class, "Bang!")); + } +} \ No newline at end of file From d22e5224b888a7004dcac82bab22b2014f923437 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 6 Oct 2024 11:16:33 +0200 Subject: [PATCH 02/16] Change: match exception subtypes We can't assume the specific exception class will be accessible by test writers. They might need to use an exception superclass, which means the matcher must consider any exception subtype. --- .../hamcrest/exception/ThrowsException.java | 6 +-- .../exception/ThrowsExceptionTest.java | 50 +++++++++++++------ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index c730592b..a8c18ef7 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -8,7 +8,7 @@ * * @param the type of the expected exception. */ -public class ThrowsException extends TypeSafeMatcher { +public class ThrowsException extends TypeSafeMatcher { private final T expectedException; private Throwable actualThrowable; @@ -22,7 +22,7 @@ public class ThrowsException extends TypeSafeMatcher ThrowsException throwsException(U expectedException) { + public static ThrowsException throwsException(U expectedException) { return new ThrowsException<>(expectedException); } @@ -33,7 +33,7 @@ protected boolean matchesSafely(Runnable item) { return false; } catch (Throwable t) { this.actualThrowable = t; - return expectedException.getClass().isInstance(t) && t.getMessage().equals(expectedException.getMessage()); + return expectedException.getClass().isAssignableFrom(t.getClass()) && t.getMessage().equals(expectedException.getMessage()); } } diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java index d70cdcce..3706bf9b 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java @@ -7,20 +7,12 @@ public class ThrowsExceptionTest extends AbstractMatcherTest { - private static Runnable runnableThrowing(String message) { - return () -> { - throw new IllegalArgumentException(message); - }; + private Runnable runnableThrowing(String message) { + return new ThrowingRunnable(new IllegalArgumentException(message)); } - private static Runnable runnableThrowing(Class exceptionClass, String message) { - return () -> { - try { - throw exceptionClass.getDeclaredConstructor().newInstance(message); - } catch (Throwable e) { - throw new RuntimeException(e); - } - }; + private Runnable runnableThrowing(Throwable exception) { + return new ThrowingRunnable(exception); } @Override @@ -34,7 +26,37 @@ public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMess assertMatches(matcher, runnableThrowing("Boom!")); assertDoesNotMatch(matcher, runnableThrowing("Bang!")); assertMismatchDescription("threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"", matcher, runnableThrowing("Bang!")); - assertDoesNotMatch(matcher, runnableThrowing(NullPointerException.class, "Bang!")); - assertMismatchDescription("threw a \"java.lang.RuntimeException\" instead of a \"java.lang.IllegalArgumentException\" exception", matcher, runnableThrowing(NullPointerException.class, "Bang!")); + assertDoesNotMatch(matcher, runnableThrowing(new NullPointerException("Boom!"))); + assertMismatchDescription("threw a \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception", matcher, runnableThrowing(new NullPointerException("Boom!"))); + } + + public void testEvaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() { + Matcher matcher = throwsException(new IllegalArgumentException("Boom!")); + + assertMatches(matcher, runnableThrowing(new TestException("Boom!"))); + } + + public static class TestException extends IllegalArgumentException { + public TestException(String message) { + super(message); + } + } + + static class ThrowingRunnable implements Runnable { + private final Throwable throwable; + + ThrowingRunnable(Throwable throwable) { + this.throwable = throwable; + } + + @Override + public void run() { + sneakyThrow(throwable); + } + + @SuppressWarnings("unchecked") + private void sneakyThrow(Throwable throwable) throws T { + throw (T) throwable; + } } } \ No newline at end of file From 39dc0139a6691966efdb79518d33faede1c0c160 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 6 Oct 2024 11:31:42 +0200 Subject: [PATCH 03/16] Change: make the matcher composable --- .../hamcrest/exception/ThrowsException.java | 50 ++++++++--------- .../exception/ThrowsExceptionEqual.java | 53 +++++++++++++++++++ ...est.java => ThrowsExceptionEqualTest.java} | 2 +- 3 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java rename hamcrest/src/test/java/org/hamcrest/exception/{ThrowsExceptionTest.java => ThrowsExceptionEqualTest.java} (97%) diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index a8c18ef7..9e53e2f3 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -1,53 +1,47 @@ package org.hamcrest.exception; import org.hamcrest.Description; +import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; +import static org.hamcrest.exception.ThrowsExceptionEqual.throwsExceptionEqual; + /** - * A matcher that checks if a Runnable throws an expected exception when run. - * - * @param the type of the expected exception. + * Tests if a Runnable throws a matching exception. */ -public class ThrowsException extends TypeSafeMatcher { - private final T expectedException; - private Throwable actualThrowable; +public class ThrowsException extends TypeSafeMatcher { + + private final Matcher elementMatcher; /** - * Constructor - * - * @param expectedException the expected exception. + * Constructor, best called from one of the static factory methods. + * @param elementMatcher matches the expected element */ - ThrowsException(T expectedException) { + ThrowsException(Matcher elementMatcher) { super(Runnable.class); - this.expectedException = expectedException; + this.elementMatcher = elementMatcher; + } + + public static ThrowsException throwsException(U item) { + return new ThrowsException(throwsExceptionEqual(item)); } - public static ThrowsException throwsException(U expectedException) { - return new ThrowsException<>(expectedException); + public static ThrowsException throwsException(Matcher itemMatcher) { + return new ThrowsException(itemMatcher); } @Override protected boolean matchesSafely(Runnable item) { - try { - item.run(); - return false; - } catch (Throwable t) { - this.actualThrowable = t; - return expectedException.getClass().isAssignableFrom(t.getClass()) && t.getMessage().equals(expectedException.getMessage()); - } + return elementMatcher.matches(item); } @Override - protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { - if (expectedException.getClass().isInstance(actualThrowable)) { - mismatchDescription.appendText("threw ").appendValue(actualThrowable.getClass().getName()).appendText(" with message ").appendValue(actualThrowable.getMessage()).appendText(" instead of ").appendValue(expectedException.getMessage()); - return; - } - mismatchDescription.appendText("threw a ").appendValue(actualThrowable.getClass().getName()).appendText(" instead of a ").appendValue(expectedException.getClass().getName()).appendText(" exception"); + public void describeTo(Description description) { + description.appendText("a runnable throwing ").appendDescriptionOf(elementMatcher); } @Override - public void describeTo(Description description) { - description.appendText("throws ").appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage()); + protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { + elementMatcher.describeMismatch(item, mismatchDescription); } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java new file mode 100644 index 00000000..24117e51 --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java @@ -0,0 +1,53 @@ +package org.hamcrest.exception; + +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; + +/** + * A matcher that checks if a Runnable throws an expected exception when run. + * + * @param the type of the expected exception. + */ +public class ThrowsExceptionEqual extends TypeSafeMatcher { + private final T item; + private Throwable actualItem; + + /** + * Constructor + * + * @param item the expected exception. + */ + ThrowsExceptionEqual(T item) { + super(Runnable.class); + this.item = item; + } + + public static ThrowsExceptionEqual throwsExceptionEqual(U item) { + return new ThrowsExceptionEqual<>(item); + } + + @Override + protected boolean matchesSafely(Runnable item) { + try { + item.run(); + return false; + } catch (Throwable t) { + this.actualItem = t; + return this.item.getClass().isAssignableFrom(t.getClass()) && t.getMessage().equals(this.item.getMessage()); + } + } + + @Override + protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { + if (this.item.getClass().isInstance(actualItem)) { + mismatchDescription.appendText("threw ").appendValue(actualItem.getClass().getName()).appendText(" with message ").appendValue(actualItem.getMessage()).appendText(" instead of ").appendValue(this.item.getMessage()); + return; + } + mismatchDescription.appendText("threw a ").appendValue(actualItem.getClass().getName()).appendText(" instead of a ").appendValue(this.item.getClass().getName()).appendText(" exception"); + } + + @Override + public void describeTo(Description description) { + description.appendText("throws ").appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java similarity index 97% rename from hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java rename to hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index 3706bf9b..c30e061d 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -5,7 +5,7 @@ import static org.hamcrest.exception.ThrowsException.throwsException; -public class ThrowsExceptionTest extends AbstractMatcherTest { +public class ThrowsExceptionEqualTest extends AbstractMatcherTest { private Runnable runnableThrowing(String message) { return new ThrowingRunnable(new IllegalArgumentException(message)); From b6d7137b2d0bcbd484e5355eadc5b9f92a5d3936 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 6 Oct 2024 11:40:57 +0200 Subject: [PATCH 04/16] Change: add composable exception message matcher This matcher doesn't care about the exception's class --- .../exception/ThrowsExceptionWithMessage.java | 56 +++++++++++++++++++ .../exception/ThrowsExceptionEqualTest.java | 14 +++++ 2 files changed, 70 insertions(+) create mode 100644 hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java new file mode 100644 index 00000000..97e5f914 --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java @@ -0,0 +1,56 @@ +package org.hamcrest.exception; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +import static org.hamcrest.core.IsEqual.equalTo; + +/** + * A matcher that checks if a Runnable throws an expected exception when run. + * + * @param the type of the expected exception. + */ +public class ThrowsExceptionWithMessage extends TypeSafeMatcher { + private final Matcher messageMatcher; + private String actualMessage; + + /** + * Constructor, best called from one of the static factory methods. + * + * @param messageMatcher matches the exception message + */ + ThrowsExceptionWithMessage(Matcher messageMatcher) { + super(Runnable.class); + this.messageMatcher = messageMatcher; + } + + public static ThrowsExceptionWithMessage withMessage(U message) { + return new ThrowsExceptionWithMessage<>(equalTo(message)); + } + + public static ThrowsExceptionWithMessage withMessage(Matcher messageMatcher) { + return new ThrowsExceptionWithMessage<>(messageMatcher); + } + + @Override + protected boolean matchesSafely(Runnable item) { + try { + item.run(); + return false; + } catch (Throwable t) { + actualMessage = t.getMessage(); + return this.messageMatcher.matches(t.getMessage()); + } + } + + @Override + public void describeTo(Description description) { + description.appendText("a runnable throwing an exception with message ").appendDescriptionOf(messageMatcher); + } + + @Override + protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { + mismatchDescription.appendText("exception message was ").appendValue(actualMessage).appendText(" instead of ").appendDescriptionOf(messageMatcher); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index c30e061d..3e9c24fb 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -3,7 +3,9 @@ import org.hamcrest.AbstractMatcherTest; import org.hamcrest.Matcher; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.exception.ThrowsException.throwsException; +import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage; public class ThrowsExceptionEqualTest extends AbstractMatcherTest { @@ -36,6 +38,18 @@ public void testEvaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExce assertMatches(matcher, runnableThrowing(new TestException("Boom!"))); } + public void testEvaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() { + Matcher matcher = throwsException(withMessage("Boom!")); + + assertMatches(matcher, runnableThrowing(new TestException("Boom!"))); + } + + public void testEvaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() { + Matcher matcher = throwsException(withMessage(containsString("bar"))); + + assertMatches(matcher, runnableThrowing(new TestException("Foo bar baz"))); + } + public static class TestException extends IllegalArgumentException { public TestException(String message) { super(message); From 384ed573520981af9f6ef47e699d24eff006355e Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 6 Oct 2024 22:22:37 +0200 Subject: [PATCH 05/16] Change: fix composition chain. `ThrowsException` is now responsible for safely executing the runnable, extracting the exception (if any), and delegating the actual test to the composed matcher. --- .../hamcrest/exception/ThrowsException.java | 44 ++++++--- .../exception/ThrowsExceptionEqual.java | 53 ---------- .../exception/ThrowsExceptionEqualTo.java | 45 +++++++++ .../exception/ThrowsExceptionWithMessage.java | 28 ++---- .../exception/ThrowsExceptionEqualTest.java | 99 +++++++++++++++---- 5 files changed, 164 insertions(+), 105 deletions(-) delete mode 100644 hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java create mode 100644 hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index 9e53e2f3..ecdadeb1 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -4,44 +4,58 @@ import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; -import static org.hamcrest.exception.ThrowsExceptionEqual.throwsExceptionEqual; +import static org.hamcrest.exception.ThrowsExceptionEqualTo.exceptionEqualTo; /** * Tests if a Runnable throws a matching exception. + * + * @param the type of the matched Runnable */ -public class ThrowsException extends TypeSafeMatcher { +public class ThrowsException extends TypeSafeMatcher { - private final Matcher elementMatcher; + private final Matcher exceptionMatcher; + private Throwable actualException; /** * Constructor, best called from one of the static factory methods. + * * @param elementMatcher matches the expected element */ - ThrowsException(Matcher elementMatcher) { - super(Runnable.class); - this.elementMatcher = elementMatcher; + ThrowsException(Matcher elementMatcher) { + this.exceptionMatcher = elementMatcher; } - public static ThrowsException throwsException(U item) { - return new ThrowsException(throwsExceptionEqual(item)); + public static ThrowsException throwsException(T item) { + return new ThrowsException<>(exceptionEqualTo(item)); } - public static ThrowsException throwsException(Matcher itemMatcher) { - return new ThrowsException(itemMatcher); + public static ThrowsException throwsException(Matcher exceptionMatcher) { + return new ThrowsException<>(exceptionMatcher); } @Override - protected boolean matchesSafely(Runnable item) { - return elementMatcher.matches(item); + public boolean matchesSafely(T runnable) { + try { + runnable.run(); + return false; + } catch (Throwable t) { + actualException = t; + return exceptionMatcher.matches(t); + } } @Override public void describeTo(Description description) { - description.appendText("a runnable throwing ").appendDescriptionOf(elementMatcher); + description.appendText("a runnable throwing ").appendDescriptionOf(exceptionMatcher); } @Override - protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { - elementMatcher.describeMismatch(item, mismatchDescription); + public void describeMismatchSafely(T runnable, Description mismatchDescription) { + if (actualException == null) { + mismatchDescription.appendText("the runnable didn't throw"); + return; + } + mismatchDescription.appendText("the runnable threw "); + exceptionMatcher.describeMismatch(actualException, mismatchDescription); } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java deleted file mode 100644 index 24117e51..00000000 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqual.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.hamcrest.exception; - -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; - -/** - * A matcher that checks if a Runnable throws an expected exception when run. - * - * @param the type of the expected exception. - */ -public class ThrowsExceptionEqual extends TypeSafeMatcher { - private final T item; - private Throwable actualItem; - - /** - * Constructor - * - * @param item the expected exception. - */ - ThrowsExceptionEqual(T item) { - super(Runnable.class); - this.item = item; - } - - public static ThrowsExceptionEqual throwsExceptionEqual(U item) { - return new ThrowsExceptionEqual<>(item); - } - - @Override - protected boolean matchesSafely(Runnable item) { - try { - item.run(); - return false; - } catch (Throwable t) { - this.actualItem = t; - return this.item.getClass().isAssignableFrom(t.getClass()) && t.getMessage().equals(this.item.getMessage()); - } - } - - @Override - protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { - if (this.item.getClass().isInstance(actualItem)) { - mismatchDescription.appendText("threw ").appendValue(actualItem.getClass().getName()).appendText(" with message ").appendValue(actualItem.getMessage()).appendText(" instead of ").appendValue(this.item.getMessage()); - return; - } - mismatchDescription.appendText("threw a ").appendValue(actualItem.getClass().getName()).appendText(" instead of a ").appendValue(this.item.getClass().getName()).appendText(" exception"); - } - - @Override - public void describeTo(Description description) { - description.appendText("throws ").appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()); - } -} diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java new file mode 100644 index 00000000..f097f6df --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java @@ -0,0 +1,45 @@ +package org.hamcrest.exception; + +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; + +/** + * A matcher that checks if a Runnable throws an expected exception when run. + * + * @param the type of the expected exception. + */ +public class ThrowsExceptionEqualTo extends TypeSafeMatcher { + private final T expectedException; + + /** + * Constructor, best called from one of the static factory methods. + * + * @param expectedException the expected exception. + */ + ThrowsExceptionEqualTo(T expectedException) { + this.expectedException = expectedException; + } + + public static ThrowsExceptionEqualTo exceptionEqualTo(U expectedException) { + return new ThrowsExceptionEqualTo<>(expectedException); + } + + @Override + public boolean matchesSafely(T item) { + return this.expectedException.getClass().isAssignableFrom(item.getClass()) && item.getMessage().equals(this.expectedException.getMessage()); + } + + @Override + public void describeTo(Description description) { + description.appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage()); + } + + @Override + protected void describeMismatchSafely(T item, Description mismatchDescription) { + if (this.expectedException.getClass().isAssignableFrom(item.getClass())) { + mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage()); + return; + } + mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception"); + } +} diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java index 97e5f914..b717c038 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java @@ -11,46 +11,38 @@ * * @param the type of the expected exception. */ -public class ThrowsExceptionWithMessage extends TypeSafeMatcher { - private final Matcher messageMatcher; - private String actualMessage; +public class ThrowsExceptionWithMessage extends TypeSafeMatcher { + private final Matcher messageMatcher; /** * Constructor, best called from one of the static factory methods. * * @param messageMatcher matches the exception message */ - ThrowsExceptionWithMessage(Matcher messageMatcher) { - super(Runnable.class); + ThrowsExceptionWithMessage(Matcher messageMatcher) { this.messageMatcher = messageMatcher; } - public static ThrowsExceptionWithMessage withMessage(U message) { + public static ThrowsExceptionWithMessage withMessage(V message) { return new ThrowsExceptionWithMessage<>(equalTo(message)); } - public static ThrowsExceptionWithMessage withMessage(Matcher messageMatcher) { + public static ThrowsExceptionWithMessage withMessage(Matcher messageMatcher) { return new ThrowsExceptionWithMessage<>(messageMatcher); } @Override - protected boolean matchesSafely(Runnable item) { - try { - item.run(); - return false; - } catch (Throwable t) { - actualMessage = t.getMessage(); - return this.messageMatcher.matches(t.getMessage()); - } + protected boolean matchesSafely(T item) { + return this.messageMatcher.matches(item.getMessage()); } @Override public void describeTo(Description description) { - description.appendText("a runnable throwing an exception with message ").appendDescriptionOf(messageMatcher); + description.appendText("an exception with message ").appendDescriptionOf(messageMatcher); } @Override - protected void describeMismatchSafely(Runnable item, Description mismatchDescription) { - mismatchDescription.appendText("exception message was ").appendValue(actualMessage).appendText(" instead of ").appendDescriptionOf(messageMatcher); + protected void describeMismatchSafely(T item, Description mismatchDescription) { + mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher); } } diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index 3e9c24fb..87c3acb9 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -4,15 +4,12 @@ import org.hamcrest.Matcher; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.exception.ThrowsException.throwsException; import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage; public class ThrowsExceptionEqualTest extends AbstractMatcherTest { - private Runnable runnableThrowing(String message) { - return new ThrowingRunnable(new IllegalArgumentException(message)); - } - private Runnable runnableThrowing(Throwable exception) { return new ThrowingRunnable(exception); } @@ -23,31 +20,95 @@ protected Matcher createMatcher() { } public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { - Matcher matcher = throwsException(new IllegalArgumentException("Boom!")); - - assertMatches(matcher, runnableThrowing("Boom!")); - assertDoesNotMatch(matcher, runnableThrowing("Bang!")); - assertMismatchDescription("threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"", matcher, runnableThrowing("Bang!")); - assertDoesNotMatch(matcher, runnableThrowing(new NullPointerException("Boom!"))); - assertMismatchDescription("threw a \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception", matcher, runnableThrowing(new NullPointerException("Boom!"))); + assertMatches( + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new IllegalArgumentException("Boom!")) + ); + + assertDescription( + "a runnable throwing \"java.lang.IllegalArgumentException\" with message \"Boom!\"", + throwsException(new IllegalArgumentException("Boom!")) + ); + + assertMismatchDescription( + "the runnable threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"", + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); + + assertMismatchDescription( + "the runnable threw \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception", + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new NullPointerException("Boom!")) + ); + + assertMismatchDescription( + "the runnable didn't throw", + throwsException(new IllegalArgumentException("Boom!")), + (Runnable) () -> {} + ); } public void testEvaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() { - Matcher matcher = throwsException(new IllegalArgumentException("Boom!")); - - assertMatches(matcher, runnableThrowing(new TestException("Boom!"))); + assertMatches( + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new TestException("Boom!")) + ); } public void testEvaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() { - Matcher matcher = throwsException(withMessage("Boom!")); - - assertMatches(matcher, runnableThrowing(new TestException("Boom!"))); + assertMatches( + throwsException(withMessage("Boom!")), + runnableThrowing(new TestException("Boom!")) + ); + + assertDescription( + "a runnable throwing an exception with message \"Boom!\"", + throwsException(withMessage("Boom!")) + ); + + assertMismatchDescription( + "the runnable threw an exception with message \"Bang!\" instead of \"Boom!\"", + throwsException(withMessage("Boom!")), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); } public void testEvaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() { - Matcher matcher = throwsException(withMessage(containsString("bar"))); - assertMatches(matcher, runnableThrowing(new TestException("Foo bar baz"))); + assertMatches( + throwsException(withMessage(containsString("bar"))), + runnableThrowing(new TestException("Foo bar baz")) + ); + + assertDescription( + "a runnable throwing an exception with message a string containing \"bar\"", + throwsException(withMessage(containsString("bar"))) + ); + + assertMismatchDescription( + "the runnable threw an exception with message \"Bang!\" instead of a string containing \"bar\"", + throwsException(withMessage(containsString("bar"))), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); + } + + public void testEvaluatesToTrueIfRunnableThrowsMatchingException() { + assertMatches( + throwsException(instanceOf(TestException.class)), + runnableThrowing(new TestException("Boom!")) + ); + + assertDescription( + "a runnable throwing an instance of java.lang.NullPointerException", + throwsException(instanceOf(NullPointerException.class)) + ); + + assertMismatchDescription( + "the runnable threw is a java.lang.IllegalArgumentException", + throwsException(instanceOf(NullPointerException.class)), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); } public static class TestException extends IllegalArgumentException { From d1da83d5923432af0bda49dc7b220eeddcd6aee5 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 6 Oct 2024 22:54:14 +0200 Subject: [PATCH 06/16] Change: Port Throwable matchers to `TypeSafeDiagnosingMatcher` --- .../exception/ThrowsExceptionEqualTo.java | 26 +++++++++---------- .../exception/ThrowsExceptionWithMessage.java | 18 ++++++------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java index f097f6df..81973b79 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java @@ -1,14 +1,14 @@ package org.hamcrest.exception; import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; /** * A matcher that checks if a Runnable throws an expected exception when run. * * @param the type of the expected exception. */ -public class ThrowsExceptionEqualTo extends TypeSafeMatcher { +public class ThrowsExceptionEqualTo extends TypeSafeDiagnosingMatcher { private final T expectedException; /** @@ -25,21 +25,21 @@ public static ThrowsExceptionEqualTo exceptionEqualTo(U } @Override - public boolean matchesSafely(T item) { - return this.expectedException.getClass().isAssignableFrom(item.getClass()) && item.getMessage().equals(this.expectedException.getMessage()); + public boolean matchesSafely(T item, Description mismatchDescription) { + if (!this.expectedException.getClass().isAssignableFrom(item.getClass())) { + mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception"); + return false; + } + if (!item.getMessage().equals(this.expectedException.getMessage())) { + mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage()); + return false; + } + + return true; } @Override public void describeTo(Description description) { description.appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage()); } - - @Override - protected void describeMismatchSafely(T item, Description mismatchDescription) { - if (this.expectedException.getClass().isAssignableFrom(item.getClass())) { - mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage()); - return; - } - mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception"); - } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java index b717c038..2e47d57c 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java @@ -2,7 +2,7 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeMatcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; import static org.hamcrest.core.IsEqual.equalTo; @@ -11,7 +11,7 @@ * * @param the type of the expected exception. */ -public class ThrowsExceptionWithMessage extends TypeSafeMatcher { +public class ThrowsExceptionWithMessage extends TypeSafeDiagnosingMatcher { private final Matcher messageMatcher; /** @@ -32,17 +32,17 @@ public static ThrowsExceptionWithMessage withMessage(Ma } @Override - protected boolean matchesSafely(T item) { - return this.messageMatcher.matches(item.getMessage()); + protected boolean matchesSafely(T item, Description mismatchDescription) { + if (!this.messageMatcher.matches(item.getMessage())) { + mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher); + return false; + } + + return true; } @Override public void describeTo(Description description) { description.appendText("an exception with message ").appendDescriptionOf(messageMatcher); } - - @Override - protected void describeMismatchSafely(T item, Description mismatchDescription) { - mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher); - } } From e0221d740a055807a8747a7528ce8d480fcd410f Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 07:05:45 +0100 Subject: [PATCH 07/16] Change: support junit style, matching against type matching of exceptions. At heart, the matcher keeps checking instances, but some QoL alias support matching against types. --- .../hamcrest/exception/ThrowsException.java | 19 ++++++++++++++++ .../exception/ThrowsExceptionEqualTest.java | 22 +++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index ecdadeb1..f97bb489 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -4,7 +4,10 @@ import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.exception.ThrowsExceptionEqualTo.exceptionEqualTo; +import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage; /** * Tests if a Runnable throws a matching exception. @@ -25,6 +28,10 @@ public class ThrowsException extends TypeSafeMatcher { this.exceptionMatcher = elementMatcher; } + public static ThrowsException throwsException() { + return new ThrowsException<>(instanceOf(Throwable.class)); + } + public static ThrowsException throwsException(T item) { return new ThrowsException<>(exceptionEqualTo(item)); } @@ -33,6 +40,18 @@ public static ThrowsException throwsException(Matcher(exceptionMatcher); } + public static ThrowsException throwsException(Class item) { + return new ThrowsException<>(instanceOf(item)); + } + + public static ThrowsException throwsException(Class item , String message) { + return new ThrowsException<>(allOf(instanceOf(item), withMessage(message))); + } + + public static ThrowsException throwsException(Class item , Matcher exceptionMatcher) { + return new ThrowsException<>(allOf(instanceOf(item), exceptionMatcher)); + } + @Override public boolean matchesSafely(T runnable) { try { diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index 87c3acb9..83f1a301 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -3,6 +3,7 @@ import org.hamcrest.AbstractMatcherTest; import org.hamcrest.Matcher; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.exception.ThrowsException.throwsException; @@ -16,7 +17,23 @@ private Runnable runnableThrowing(Throwable exception) { @Override protected Matcher createMatcher() { - return throwsException(new IllegalArgumentException("Boom!")); + return throwsException(IllegalArgumentException.class); + } + + public void testExamples() { + RuntimeException boom = new RuntimeException("boom"); + Runnable codeThatThrows = () -> { + throw boom; + }; + + assertThat(codeThatThrows, throwsException()); + assertThat(codeThatThrows, throwsException(boom)); + assertThat(codeThatThrows, throwsException(RuntimeException.class)); + assertThat(codeThatThrows, throwsException(withMessage("boom"))); + assertThat(codeThatThrows, throwsException(withMessage(containsString("oom")))); + assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom")); + assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage("boom"))); + assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage(containsString("boom")))); } public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { @@ -45,7 +62,8 @@ public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMess assertMismatchDescription( "the runnable didn't throw", throwsException(new IllegalArgumentException("Boom!")), - (Runnable) () -> {} + (Runnable) () -> { + } ); } From 4cd878aafcfb6400328861f12b4e2e523d21d07b Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 07:33:42 +0100 Subject: [PATCH 08/16] Change: add alias to new matchers in org.hamcrest.Matchers --- .../src/main/java/org/hamcrest/Matchers.java | 85 +++++++++++++++++++ .../hamcrest/exception/ThrowsException.java | 2 +- .../exception/ThrowsExceptionEqualTo.java | 2 +- .../exception/ThrowsExceptionWithMessage.java | 2 +- 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/Matchers.java b/hamcrest/src/main/java/org/hamcrest/Matchers.java index d5260fb1..4d563f6c 100644 --- a/hamcrest/src/main/java/org/hamcrest/Matchers.java +++ b/hamcrest/src/main/java/org/hamcrest/Matchers.java @@ -3,6 +3,9 @@ import org.hamcrest.collection.ArrayMatching; import org.hamcrest.core.IsIterableContaining; import org.hamcrest.core.StringRegularExpression; +import org.hamcrest.exception.ThrowsException; +import org.hamcrest.exception.ThrowsExceptionEqualTo; +import org.hamcrest.exception.ThrowsExceptionWithMessage; import org.hamcrest.optional.OptionalEmpty; import org.hamcrest.optional.OptionalWithValue; import org.hamcrest.text.IsEqualCompressingWhiteSpace; @@ -2228,4 +2231,86 @@ public static Matcher> optionalWithValue(T value) { public static Matcher> optionalWithValue(Matcher matcher) { return OptionalWithValue.optionalWithValue(matcher); } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception + * + * @param type of the Runnable + * @return The matcher. + */ + public static ThrowsException throwsException() { + return ThrowsException.throwsException(); + } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided throwable + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwable the Throwable instance against which examined exceptions are compared + * @return The matcher. + */ + public static ThrowsException throwsException(U throwable) { + return ThrowsException.throwsException(throwable); + } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @return The matcher. + */ + public static ThrowsException throwsException(Class throwableClass) { + return ThrowsException.throwsException(throwableClass); + } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided message class + * + * @param type of the Runnable + * @param message the String against which examined exception messages are compared + * @return The matcher. + */ + public static ThrowsException throwsException(String message) { + return ThrowsException.throwsException(message); + } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception matching provided matcher + * + * @param type of the Runnable + * @param matcher matcher to validate the exception + * @return The matcher. + */ + public static ThrowsException throwsException(Matcher matcher) { + return ThrowsException.throwsException(matcher); + } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and has a message equal to the provided message + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param message the String against which examined exception messages are compared + * @return The matcher. + */ + public static ThrowsException throwsException(Class throwableClass, String message) { + return ThrowsException.throwsException(throwableClass, message); + } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and matches the provided matcher + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param matcher matcher to validate the exception + * @return The matcher. + */ + public static ThrowsException throwsException(Class throwableClass, Matcher matcher) { + return ThrowsException.throwsException(throwableClass, matcher); + } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index f97bb489..7fefec6d 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -24,7 +24,7 @@ public class ThrowsException extends TypeSafeMatcher { * * @param elementMatcher matches the expected element */ - ThrowsException(Matcher elementMatcher) { + public ThrowsException(Matcher elementMatcher) { this.exceptionMatcher = elementMatcher; } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java index 81973b79..7640d165 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java @@ -16,7 +16,7 @@ public class ThrowsExceptionEqualTo extends TypeSafeDiagnos * * @param expectedException the expected exception. */ - ThrowsExceptionEqualTo(T expectedException) { + public ThrowsExceptionEqualTo(T expectedException) { this.expectedException = expectedException; } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java index 2e47d57c..96e895f3 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java @@ -19,7 +19,7 @@ public class ThrowsExceptionWithMessage extends TypeSafeDia * * @param messageMatcher matches the exception message */ - ThrowsExceptionWithMessage(Matcher messageMatcher) { + public ThrowsExceptionWithMessage(Matcher messageMatcher) { this.messageMatcher = messageMatcher; } From bdcc5b5b8c32cd4a1324b741a471c7994c281f20 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 07:34:34 +0100 Subject: [PATCH 09/16] Change: fix factory type names --- .../main/java/org/hamcrest/exception/ThrowsException.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index 7fefec6d..6090922f 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -32,7 +32,7 @@ public static ThrowsException throwsException() { return new ThrowsException<>(instanceOf(Throwable.class)); } - public static ThrowsException throwsException(T item) { + public static ThrowsException throwsException(V item) { return new ThrowsException<>(exceptionEqualTo(item)); } @@ -40,15 +40,15 @@ public static ThrowsException throwsException(Matcher(exceptionMatcher); } - public static ThrowsException throwsException(Class item) { + public static ThrowsException throwsException(Class item) { return new ThrowsException<>(instanceOf(item)); } - public static ThrowsException throwsException(Class item , String message) { + public static ThrowsException throwsException(Class item , String message) { return new ThrowsException<>(allOf(instanceOf(item), withMessage(message))); } - public static ThrowsException throwsException(Class item , Matcher exceptionMatcher) { + public static ThrowsException throwsException(Class item , Matcher exceptionMatcher) { return new ThrowsException<>(allOf(instanceOf(item), exceptionMatcher)); } From d928bdfca7161e2f0c25bfe7e73f9a703d931dff Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 07:44:37 +0100 Subject: [PATCH 10/16] Change: migrate tests to junit5 --- .../exception/ThrowsExceptionEqualTest.java | 277 +++++++++--------- 1 file changed, 139 insertions(+), 138 deletions(-) diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index 83f1a301..d54cf772 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -1,155 +1,156 @@ package org.hamcrest.exception; -import org.hamcrest.AbstractMatcherTest; -import org.hamcrest.Matcher; +import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.exception.ThrowsException.throwsException; import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage; - -public class ThrowsExceptionEqualTest extends AbstractMatcherTest { - - private Runnable runnableThrowing(Throwable exception) { - return new ThrowingRunnable(exception); - } - - @Override - protected Matcher createMatcher() { - return throwsException(IllegalArgumentException.class); - } - - public void testExamples() { - RuntimeException boom = new RuntimeException("boom"); - Runnable codeThatThrows = () -> { - throw boom; - }; - - assertThat(codeThatThrows, throwsException()); - assertThat(codeThatThrows, throwsException(boom)); - assertThat(codeThatThrows, throwsException(RuntimeException.class)); - assertThat(codeThatThrows, throwsException(withMessage("boom"))); - assertThat(codeThatThrows, throwsException(withMessage(containsString("oom")))); - assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom")); - assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage("boom"))); - assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage(containsString("boom")))); - } - - public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { - assertMatches( - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new IllegalArgumentException("Boom!")) - ); - - assertDescription( - "a runnable throwing \"java.lang.IllegalArgumentException\" with message \"Boom!\"", - throwsException(new IllegalArgumentException("Boom!")) - ); - - assertMismatchDescription( - "the runnable threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"", - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); - - assertMismatchDescription( - "the runnable threw \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception", - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new NullPointerException("Boom!")) - ); - - assertMismatchDescription( - "the runnable didn't throw", - throwsException(new IllegalArgumentException("Boom!")), - (Runnable) () -> { - } - ); - } - - public void testEvaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() { - assertMatches( - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new TestException("Boom!")) - ); - } - - public void testEvaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() { - assertMatches( - throwsException(withMessage("Boom!")), - runnableThrowing(new TestException("Boom!")) - ); - - assertDescription( - "a runnable throwing an exception with message \"Boom!\"", - throwsException(withMessage("Boom!")) - ); - - assertMismatchDescription( - "the runnable threw an exception with message \"Bang!\" instead of \"Boom!\"", - throwsException(withMessage("Boom!")), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); +import static org.hamcrest.test.MatcherAssertions.*; + +public final class ThrowsExceptionEqualTest { + + private Runnable runnableThrowing(Throwable exception) { + return new ThrowingRunnable(exception); + } + + @Test + public void examples() { + RuntimeException boom = new RuntimeException("boom"); + Runnable codeThatThrows = () -> { + throw boom; + }; + + assertThat(codeThatThrows, throwsException()); + assertThat(codeThatThrows, throwsException(boom)); + assertThat(codeThatThrows, throwsException(RuntimeException.class)); + assertThat(codeThatThrows, throwsException(withMessage("boom"))); + assertThat(codeThatThrows, throwsException(withMessage(containsString("oom")))); + assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom")); + assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage("boom"))); + assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage(containsString("boom")))); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { + assertMatches( + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new IllegalArgumentException("Boom!")) + ); + + assertDescription( + "a runnable throwing \"java.lang.IllegalArgumentException\" with message \"Boom!\"", + throwsException(new IllegalArgumentException("Boom!")) + ); + + assertMismatchDescription( + "the runnable threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"", + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); + + assertMismatchDescription( + "the runnable threw \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception", + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new NullPointerException("Boom!")) + ); + + assertMismatchDescription( + "the runnable didn't throw", + throwsException(new IllegalArgumentException("Boom!")), + (Runnable) () -> { + } + ); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() { + assertMatches( + throwsException(new IllegalArgumentException("Boom!")), + runnableThrowing(new TestException("Boom!")) + ); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() { + assertMatches( + throwsException(withMessage("Boom!")), + runnableThrowing(new TestException("Boom!")) + ); + + assertDescription( + "a runnable throwing an exception with message \"Boom!\"", + throwsException(withMessage("Boom!")) + ); + + assertMismatchDescription( + "the runnable threw an exception with message \"Bang!\" instead of \"Boom!\"", + throwsException(withMessage("Boom!")), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() { + + assertMatches( + throwsException(withMessage(containsString("bar"))), + runnableThrowing(new TestException("Foo bar baz")) + ); + + assertDescription( + "a runnable throwing an exception with message a string containing \"bar\"", + throwsException(withMessage(containsString("bar"))) + ); + + assertMismatchDescription( + "the runnable threw an exception with message \"Bang!\" instead of a string containing \"bar\"", + throwsException(withMessage(containsString("bar"))), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsMatchingException() { + assertMatches( + throwsException(instanceOf(TestException.class)), + runnableThrowing(new TestException("Boom!")) + ); + + assertDescription( + "a runnable throwing an instance of java.lang.NullPointerException", + throwsException(instanceOf(NullPointerException.class)) + ); + + assertMismatchDescription( + "the runnable threw is a java.lang.IllegalArgumentException", + throwsException(instanceOf(NullPointerException.class)), + runnableThrowing(new IllegalArgumentException("Bang!")) + ); + } + + public static class TestException extends IllegalArgumentException { + public TestException(String message) { + super(message); } + } - public void testEvaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() { - - assertMatches( - throwsException(withMessage(containsString("bar"))), - runnableThrowing(new TestException("Foo bar baz")) - ); + static class ThrowingRunnable implements Runnable { + private final Throwable throwable; - assertDescription( - "a runnable throwing an exception with message a string containing \"bar\"", - throwsException(withMessage(containsString("bar"))) - ); - - assertMismatchDescription( - "the runnable threw an exception with message \"Bang!\" instead of a string containing \"bar\"", - throwsException(withMessage(containsString("bar"))), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); + ThrowingRunnable(Throwable throwable) { + this.throwable = throwable; } - public void testEvaluatesToTrueIfRunnableThrowsMatchingException() { - assertMatches( - throwsException(instanceOf(TestException.class)), - runnableThrowing(new TestException("Boom!")) - ); - - assertDescription( - "a runnable throwing an instance of java.lang.NullPointerException", - throwsException(instanceOf(NullPointerException.class)) - ); - - assertMismatchDescription( - "the runnable threw is a java.lang.IllegalArgumentException", - throwsException(instanceOf(NullPointerException.class)), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); - } - - public static class TestException extends IllegalArgumentException { - public TestException(String message) { - super(message); - } + @Override + public void run() { + sneakyThrow(throwable); } - static class ThrowingRunnable implements Runnable { - private final Throwable throwable; - - ThrowingRunnable(Throwable throwable) { - this.throwable = throwable; - } - - @Override - public void run() { - sneakyThrow(throwable); - } - - @SuppressWarnings("unchecked") - private void sneakyThrow(Throwable throwable) throws T { - throw (T) throwable; - } + @SuppressWarnings("unchecked") + private void sneakyThrow(Throwable throwable) throws T { + throw (T) throwable; } + } } \ No newline at end of file From 7cd5c4e1c0e5524f7e709b294d5e1b6c18cbf4e0 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 07:46:35 +0100 Subject: [PATCH 11/16] Cleanup: reformat to 2 spaces --- .../hamcrest/exception/ThrowsException.java | 98 +++++++++---------- .../exception/ThrowsExceptionEqualTo.java | 56 +++++------ .../exception/ThrowsExceptionWithMessage.java | 60 ++++++------ 3 files changed, 107 insertions(+), 107 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index 6090922f..44dbc8fe 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -16,65 +16,65 @@ */ public class ThrowsException extends TypeSafeMatcher { - private final Matcher exceptionMatcher; - private Throwable actualException; + private final Matcher exceptionMatcher; + private Throwable actualException; - /** - * Constructor, best called from one of the static factory methods. - * - * @param elementMatcher matches the expected element - */ - public ThrowsException(Matcher elementMatcher) { - this.exceptionMatcher = elementMatcher; - } + /** + * Constructor, best called from one of the static factory methods. + * + * @param elementMatcher matches the expected element + */ + public ThrowsException(Matcher elementMatcher) { + this.exceptionMatcher = elementMatcher; + } - public static ThrowsException throwsException() { - return new ThrowsException<>(instanceOf(Throwable.class)); - } + public static ThrowsException throwsException() { + return new ThrowsException<>(instanceOf(Throwable.class)); + } - public static ThrowsException throwsException(V item) { - return new ThrowsException<>(exceptionEqualTo(item)); - } + public static ThrowsException throwsException(V item) { + return new ThrowsException<>(exceptionEqualTo(item)); + } - public static ThrowsException throwsException(Matcher exceptionMatcher) { - return new ThrowsException<>(exceptionMatcher); - } + public static ThrowsException throwsException(Matcher exceptionMatcher) { + return new ThrowsException<>(exceptionMatcher); + } - public static ThrowsException throwsException(Class item) { - return new ThrowsException<>(instanceOf(item)); - } + public static ThrowsException throwsException(Class item) { + return new ThrowsException<>(instanceOf(item)); + } - public static ThrowsException throwsException(Class item , String message) { - return new ThrowsException<>(allOf(instanceOf(item), withMessage(message))); - } + public static ThrowsException throwsException(Class item, String message) { + return new ThrowsException<>(allOf(instanceOf(item), withMessage(message))); + } - public static ThrowsException throwsException(Class item , Matcher exceptionMatcher) { - return new ThrowsException<>(allOf(instanceOf(item), exceptionMatcher)); - } + public static ThrowsException throwsException(Class item, Matcher exceptionMatcher) { + return new ThrowsException<>(allOf(instanceOf(item), exceptionMatcher)); + } - @Override - public boolean matchesSafely(T runnable) { - try { - runnable.run(); - return false; - } catch (Throwable t) { - actualException = t; - return exceptionMatcher.matches(t); - } + @Override + public boolean matchesSafely(T runnable) { + try { + runnable.run(); + return false; + } catch (Throwable t) { + actualException = t; + return exceptionMatcher.matches(t); } + } - @Override - public void describeTo(Description description) { - description.appendText("a runnable throwing ").appendDescriptionOf(exceptionMatcher); - } + @Override + public void describeTo(Description description) { + description.appendText("a runnable throwing ").appendDescriptionOf(exceptionMatcher); + } - @Override - public void describeMismatchSafely(T runnable, Description mismatchDescription) { - if (actualException == null) { - mismatchDescription.appendText("the runnable didn't throw"); - return; - } - mismatchDescription.appendText("the runnable threw "); - exceptionMatcher.describeMismatch(actualException, mismatchDescription); + @Override + public void describeMismatchSafely(T runnable, Description mismatchDescription) { + if (actualException == null) { + mismatchDescription.appendText("the runnable didn't throw"); + return; } + mismatchDescription.appendText("the runnable threw "); + exceptionMatcher.describeMismatch(actualException, mismatchDescription); + } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java index 7640d165..6c4afe07 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java @@ -9,37 +9,37 @@ * @param the type of the expected exception. */ public class ThrowsExceptionEqualTo extends TypeSafeDiagnosingMatcher { - private final T expectedException; + private final T expectedException; - /** - * Constructor, best called from one of the static factory methods. - * - * @param expectedException the expected exception. - */ - public ThrowsExceptionEqualTo(T expectedException) { - this.expectedException = expectedException; - } - - public static ThrowsExceptionEqualTo exceptionEqualTo(U expectedException) { - return new ThrowsExceptionEqualTo<>(expectedException); - } + /** + * Constructor, best called from one of the static factory methods. + * + * @param expectedException the expected exception. + */ + public ThrowsExceptionEqualTo(T expectedException) { + this.expectedException = expectedException; + } - @Override - public boolean matchesSafely(T item, Description mismatchDescription) { - if (!this.expectedException.getClass().isAssignableFrom(item.getClass())) { - mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception"); - return false; - } - if (!item.getMessage().equals(this.expectedException.getMessage())) { - mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage()); - return false; - } + public static ThrowsExceptionEqualTo exceptionEqualTo(U expectedException) { + return new ThrowsExceptionEqualTo<>(expectedException); + } - return true; + @Override + public boolean matchesSafely(T item, Description mismatchDescription) { + if (!this.expectedException.getClass().isAssignableFrom(item.getClass())) { + mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception"); + return false; } - - @Override - public void describeTo(Description description) { - description.appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage()); + if (!item.getMessage().equals(this.expectedException.getMessage())) { + mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage()); + return false; } + + return true; + } + + @Override + public void describeTo(Description description) { + description.appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage()); + } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java index 96e895f3..8ee3b320 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java @@ -12,37 +12,37 @@ * @param the type of the expected exception. */ public class ThrowsExceptionWithMessage extends TypeSafeDiagnosingMatcher { - private final Matcher messageMatcher; - - /** - * Constructor, best called from one of the static factory methods. - * - * @param messageMatcher matches the exception message - */ - public ThrowsExceptionWithMessage(Matcher messageMatcher) { - this.messageMatcher = messageMatcher; + private final Matcher messageMatcher; + + /** + * Constructor, best called from one of the static factory methods. + * + * @param messageMatcher matches the exception message + */ + public ThrowsExceptionWithMessage(Matcher messageMatcher) { + this.messageMatcher = messageMatcher; + } + + public static ThrowsExceptionWithMessage withMessage(V message) { + return new ThrowsExceptionWithMessage<>(equalTo(message)); + } + + public static ThrowsExceptionWithMessage withMessage(Matcher messageMatcher) { + return new ThrowsExceptionWithMessage<>(messageMatcher); + } + + @Override + protected boolean matchesSafely(T item, Description mismatchDescription) { + if (!this.messageMatcher.matches(item.getMessage())) { + mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher); + return false; } - public static ThrowsExceptionWithMessage withMessage(V message) { - return new ThrowsExceptionWithMessage<>(equalTo(message)); - } - - public static ThrowsExceptionWithMessage withMessage(Matcher messageMatcher) { - return new ThrowsExceptionWithMessage<>(messageMatcher); - } - - @Override - protected boolean matchesSafely(T item, Description mismatchDescription) { - if (!this.messageMatcher.matches(item.getMessage())) { - mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher); - return false; - } + return true; + } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("an exception with message ").appendDescriptionOf(messageMatcher); - } + @Override + public void describeTo(Description description) { + description.appendText("an exception with message ").appendDescriptionOf(messageMatcher); + } } From a2d32696219cb868c1bc67cc86818b1098d18a44 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 08:08:31 +0100 Subject: [PATCH 12/16] Change: fix matcher factories and expand examples test --- .../hamcrest/exception/ThrowsException.java | 80 ++++++++++++++++--- .../exception/ThrowsExceptionEqualTest.java | 1 + 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index 44dbc8fe..da47cd3a 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -28,28 +28,86 @@ public ThrowsException(Matcher elementMatcher) { this.exceptionMatcher = elementMatcher; } - public static ThrowsException throwsException() { + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception + * + * @param type of the Runnable + * @return The matcher. + */ + public static ThrowsException throwsException() { return new ThrowsException<>(instanceOf(Throwable.class)); } - public static ThrowsException throwsException(V item) { - return new ThrowsException<>(exceptionEqualTo(item)); + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided throwable + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwable the Throwable instance against which examined exceptions are compared + * @return The matcher. + */ + public static ThrowsException throwsException(U throwable) { + return new ThrowsException<>(exceptionEqualTo(throwable)); } - public static ThrowsException throwsException(Matcher exceptionMatcher) { - return new ThrowsException<>(exceptionMatcher); + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @return The matcher. + */ + public static ThrowsException throwsException(Class throwableClass) { + return new ThrowsException<>(instanceOf(throwableClass)); } - public static ThrowsException throwsException(Class item) { - return new ThrowsException<>(instanceOf(item)); + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided message class + * + * @param type of the Runnable + * @param message the String against which examined exception messages are compared + * @return The matcher. + */ + public static ThrowsException throwsException(String message) { + return new ThrowsException<>(withMessage(message)); } - public static ThrowsException throwsException(Class item, String message) { - return new ThrowsException<>(allOf(instanceOf(item), withMessage(message))); + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception matching provided matcher + * + * @param type of the Runnable + * @param matcher matcher to validate the exception + * @return The matcher. + */ + public static ThrowsException throwsException(Matcher matcher) { + return new ThrowsException<>(matcher); } - public static ThrowsException throwsException(Class item, Matcher exceptionMatcher) { - return new ThrowsException<>(allOf(instanceOf(item), exceptionMatcher)); + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and has a message equal to the provided message + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param message the String against which examined exception messages are compared + * @return The matcher. + */ + public static ThrowsException throwsException(Class throwableClass, String message) { + return new ThrowsException<>(allOf(instanceOf(throwableClass), withMessage(message))); + } + + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and matches the provided matcher + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param matcher matcher to validate the exception + * @return The matcher. + */ + public static ThrowsException throwsException(Class throwableClass, Matcher matcher) { + return new ThrowsException<>(allOf(instanceOf(throwableClass), matcher)); } @Override diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index d54cf772..55253432 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -25,6 +25,7 @@ public void examples() { assertThat(codeThatThrows, throwsException()); assertThat(codeThatThrows, throwsException(boom)); assertThat(codeThatThrows, throwsException(RuntimeException.class)); + assertThat(codeThatThrows, throwsException("boom")); assertThat(codeThatThrows, throwsException(withMessage("boom"))); assertThat(codeThatThrows, throwsException(withMessage(containsString("oom")))); assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom")); From 5161c76c916d0cb666b9d5ca14ff71ac0c7e1d56 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 08:15:30 +0100 Subject: [PATCH 13/16] Cleanup: add empty line at the end of the file --- .../java/org/hamcrest/exception/ThrowsExceptionEqualTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index 55253432..971d4f25 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -154,4 +154,4 @@ private void sneakyThrow(Throwable throwable) throws T { throw (T) throwable; } } -} \ No newline at end of file +} From 00fb99e304275d79d9636d1f1c2fad59a9ad73d2 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sat, 2 Nov 2024 08:08:04 +0100 Subject: [PATCH 14/16] Change: applied suggestions from https://github.com/tumbarumba/JavaHamcrest/blob/throws-exception/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java --- .../src/main/java/org/hamcrest/Matchers.java | 867 +++++++----------- .../hamcrest/exception/ThrowsException.java | 138 +-- .../exception/ThrowsExceptionEqualTest.java | 157 ---- .../exception/ThrowsExceptionTest.java | 86 ++ 4 files changed, 432 insertions(+), 816 deletions(-) delete mode 100644 hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java create mode 100644 hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java diff --git a/hamcrest/src/main/java/org/hamcrest/Matchers.java b/hamcrest/src/main/java/org/hamcrest/Matchers.java index 4d563f6c..37a9e164 100644 --- a/hamcrest/src/main/java/org/hamcrest/Matchers.java +++ b/hamcrest/src/main/java/org/hamcrest/Matchers.java @@ -4,8 +4,6 @@ import org.hamcrest.core.IsIterableContaining; import org.hamcrest.core.StringRegularExpression; import org.hamcrest.exception.ThrowsException; -import org.hamcrest.exception.ThrowsExceptionEqualTo; -import org.hamcrest.exception.ThrowsExceptionWithMessage; import org.hamcrest.optional.OptionalEmpty; import org.hamcrest.optional.OptionalWithValue; import org.hamcrest.text.IsEqualCompressingWhiteSpace; @@ -32,10 +30,8 @@ private Matchers() { * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param - * the matcher type. - * @param matchers - * all the matchers must pass. + * @param the matcher type. + * @param matchers all the matchers must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(java.lang.Iterable> matchers) { @@ -47,10 +43,8 @@ public static org.hamcrest.Matcher allOf(java.lang.IterableassertThat("myValue", allOf(startsWith("my"), containsString("Val"))) * - * @param - * the matcher type. - * @param matchers - * all the matchers must pass. + * @param the matcher type. + * @param matchers all the matchers must pass. * @return The matcher. */ @SafeVarargs @@ -63,12 +57,9 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher. * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param - * the matcher type. - * @param first - * first matcher that must pass. - * @param second - * second matcher that must pass. + * @param the matcher type. + * @param first first matcher that must pass. + * @param second second matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second) { @@ -80,14 +71,10 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param - * the matcher type. - * @param first - * first matcher that must pass. - * @param second - * second matcher that must pass. - * @param third - * third matcher that must pass. + * @param the matcher type. + * @param first first matcher that must pass. + * @param second second matcher that must pass. + * @param third third matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third) { @@ -99,16 +86,11 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param - * the matcher type. - * @param first - * first matcher that must pass. - * @param second - * second matcher that must pass. - * @param third - * third matcher that must pass. - * @param fourth - * fourth matcher that must pass. + * @param the matcher type. + * @param first first matcher that must pass. + * @param second second matcher that must pass. + * @param third third matcher that must pass. + * @param fourth fourth matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth) { @@ -120,18 +102,12 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param - * the matcher type. - * @param first - * first matcher that must pass. - * @param second - * second matcher that must pass. - * @param third - * third matcher that must pass. - * @param fourth - * fourth matcher that must pass. - * @param fifth - * fifth matcher that must pass. + * @param the matcher type. + * @param first first matcher that must pass. + * @param second second matcher that must pass. + * @param third third matcher that must pass. + * @param fourth fourth matcher that must pass. + * @param fifth fifth matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth) { @@ -143,20 +119,13 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param - * the matcher type. - * @param first - * first matcher that must pass. - * @param second - * second matcher that must pass. - * @param third - * third matcher that must pass. - * @param fourth - * fourth matcher that must pass. - * @param fifth - * fifth matcher that must pass. - * @param sixth - * sixth matcher that must pass. + * @param the matcher type. + * @param first first matcher that must pass. + * @param second second matcher that must pass. + * @param third third matcher that must pass. + * @param fourth fourth matcher that must pass. + * @param fifth fifth matcher that must pass. + * @param sixth sixth matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth, org.hamcrest.Matcher sixth) { @@ -168,10 +137,8 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
* - * @param - * the matcher type. - * @param matchers - * any the matchers must pass. + * @param the matcher type. + * @param matchers any the matchers must pass. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(java.lang.Iterable> matchers) { @@ -183,10 +150,8 @@ public static org.hamcrest.core.AnyOf anyOf(java.lang.IterableassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param - * the matcher type. - * @param matchers - * any the matchers must pass. + * @param the matcher type. + * @param matchers any the matchers must pass. * @return The matcher. */ @SafeVarargs @@ -199,12 +164,9 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param - * the matcher type. - * @param first - * first matcher to check. - * @param second - * second matcher to check. + * @param the matcher type. + * @param first first matcher to check. + * @param second second matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second) { @@ -216,14 +178,10 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param - * the matcher type. - * @param first - * first matcher to check. - * @param second - * second matcher to check. - * @param third - * third matcher to check. + * @param the matcher type. + * @param first first matcher to check. + * @param second second matcher to check. + * @param third third matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third) { @@ -235,16 +193,11 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param - * the matcher type. - * @param first - * first matcher to check. - * @param second - * second matcher to check. - * @param third - * third matcher to check. - * @param fourth - * fourth matcher to check. + * @param the matcher type. + * @param first first matcher to check. + * @param second second matcher to check. + * @param third third matcher to check. + * @param fourth fourth matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth) { @@ -256,18 +209,12 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param - * the matcher type. - * @param first - * first matcher to check. - * @param second - * second matcher to check. - * @param third - * third matcher to check. - * @param fourth - * fourth matcher to check. - * @param fifth - * fifth matcher to check. + * @param the matcher type. + * @param first first matcher to check. + * @param second second matcher to check. + * @param third third matcher to check. + * @param fourth fourth matcher to check. + * @param fifth fifth matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth) { @@ -279,20 +226,13 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param - * the matcher type. - * @param first - * first matcher to check. - * @param second - * second matcher to check. - * @param third - * third matcher to check. - * @param fourth - * fourth matcher to check. - * @param fifth - * fifth matcher to check. - * @param sixth - * sixth matcher to check. + * @param the matcher type. + * @param first first matcher to check. + * @param second second matcher to check. + * @param third third matcher to check. + * @param fourth fourth matcher to check. + * @param fifth fifth matcher to check. + * @param sixth sixth matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth, org.hamcrest.Matcher sixth) { @@ -304,10 +244,8 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("fab", both(containsString("a")).and(containsString("b"))) * - * @param - * the matcher type. - * @param matcher - * the matcher to combine, and both must pass. + * @param the matcher type. + * @param matcher the matcher to combine, and both must pass. * @return The matcher. */ public static org.hamcrest.core.CombinableMatcher.CombinableBothMatcher both(org.hamcrest.Matcher matcher) { @@ -319,10 +257,8 @@ public static org.hamcrest.core.CombinableMatcher.CombinableBothMatcherassertThat("fan", either(containsString("a")).or(containsString("b"))) * - * @param - * the matcher type. - * @param matcher - * the matcher to combine, and either must pass. + * @param the matcher type. + * @param matcher the matcher to combine, and either must pass. * @return The matcher. */ public static org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher either(org.hamcrest.Matcher matcher) { @@ -335,14 +271,10 @@ public static org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher< * For example: *
describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())
* - * @param - * the matcher type. - * @param description - * the new description for the wrapped matcher - * @param matcher - * the matcher to wrap - * @param values - * optional values to insert into the tokenized description + * @param the matcher type. + * @param description the new description for the wrapped matcher + * @param matcher the matcher to wrap + * @param values optional values to insert into the tokenized description * @return The matcher. */ public static org.hamcrest.Matcher describedAs(java.lang.String description, org.hamcrest.Matcher matcher, java.lang.Object... values) { @@ -356,10 +288,8 @@ public static org.hamcrest.Matcher describedAs(java.lang.String descripti * For example: *
assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))
* - * @param - * the matcher type. - * @param itemMatcher - * the matcher to apply to every item provided by the examined {@link Iterable} + * @param the matcher type. + * @param itemMatcher the matcher to apply to every item provided by the examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> everyItem(org.hamcrest.Matcher itemMatcher) { @@ -374,10 +304,8 @@ public static org.hamcrest.Matcher> everyIte * instead of: *
assertThat(cheese, equalTo(smelly))
* - * @param - * the matcher type. - * @param matcher - * the matcher to wrap. + * @param the matcher type. + * @param matcher the matcher to wrap. * @return The matcher. */ public static org.hamcrest.Matcher is(org.hamcrest.Matcher matcher) { @@ -391,10 +319,8 @@ public static org.hamcrest.Matcher is(org.hamcrest.Matcher matcher) { * instead of: *
assertThat(cheese, is(equalTo(smelly)))
* - * @param - * the matcher type. - * @param value - * the value to check. + * @param the matcher type. + * @param value the value to check. * @return The matcher. */ public static org.hamcrest.Matcher is(T value) { @@ -408,10 +334,8 @@ public static org.hamcrest.Matcher is(T value) { * instead of: *
assertThat(cheese, is(instanceOf(Cheddar.class)))
* - * @param - * the matcher type. - * @param type - * the type to check. + * @param the matcher type. + * @param type the type to check. * @return The matcher. */ public static org.hamcrest.Matcher isA(java.lang.Class type) { @@ -420,6 +344,7 @@ public static org.hamcrest.Matcher isA(java.lang.Class type) { /** * Creates a matcher that always matches, regardless of the examined object. + * * @return The matcher. */ public static org.hamcrest.Matcher anything() { @@ -430,8 +355,7 @@ public static org.hamcrest.Matcher anything() { * Creates a matcher that always matches, regardless of the examined object, but describes * itself with the specified {@link String}. * - * @param description - * a meaningful {@link String} used when describing itself + * @param description a meaningful {@link String} used when describing itself * @return The matcher. */ public static org.hamcrest.Matcher anything(java.lang.String description) { @@ -446,10 +370,8 @@ public static org.hamcrest.Matcher anything(java.lang.String d * For example: *
assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))
* - * @param - * the matcher type. - * @param itemMatcher - * the matcher to apply to items provided by the examined {@link Iterable} + * @param the matcher type. + * @param itemMatcher the matcher to apply to items provided by the examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> hasItem(org.hamcrest.Matcher itemMatcher) { @@ -464,10 +386,8 @@ public static org.hamcrest.Matcher> hasItem( * For example: *
assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))
* - * @param - * the matcher type. - * @param item - * the item to compare against the items provided by the examined {@link Iterable} + * @param the matcher type. + * @param item the item to compare against the items provided by the examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> hasItem(T item) { @@ -482,10 +402,8 @@ public static org.hamcrest.Matcher> hasItem( * For example: *
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))
* - * @param - * the matcher type. - * @param itemMatchers - * the matchers to apply to items provided by the examined {@link Iterable} + * @param the matcher type. + * @param itemMatchers the matchers to apply to items provided by the examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -501,10 +419,8 @@ public static org.hamcrest.Matcher> hasItems * For example: *
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))
* - * @param - * the matcher type. - * @param items - * the items to compare against the items provided by the examined {@link Iterable} + * @param the matcher type. + * @param items the items to compare against the items provided by the examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -533,10 +449,8 @@ public static org.hamcrest.Matcher> hasItems * assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"})); * * - * @param - * the matcher type. - * @param operand - * the value to check. + * @param the matcher type. + * @param operand the value to check. * @return The matcher. */ public static org.hamcrest.Matcher equalTo(T operand) { @@ -547,8 +461,7 @@ public static org.hamcrest.Matcher equalTo(T operand) { * Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being * compared to be of the same static type. * - * @param operand - * the value to check. + * @param operand the value to check. * @return The matcher. */ public static org.hamcrest.Matcher equalToObject(java.lang.Object operand) { @@ -566,10 +479,8 @@ public static org.hamcrest.Matcher equalToObject(java.lang.Obj * For example: *
assertThat(new Canoe(), instanceOf(Canoe.class));
* - * @param - * the matcher type. - * @param type - * the type to check. + * @param the matcher type. + * @param type the type to check. * @return The matcher. */ public static org.hamcrest.Matcher any(java.lang.Class type) { @@ -585,10 +496,8 @@ public static org.hamcrest.Matcher any(java.lang.Class type) { * For example: *
assertThat(new Canoe(), instanceOf(Paddlable.class));
* - * @param - * the matcher type. - * @param type - * the type to check. + * @param the matcher type. + * @param type the type to check. * @return The matcher. */ public static org.hamcrest.Matcher instanceOf(java.lang.Class type) { @@ -601,10 +510,8 @@ public static org.hamcrest.Matcher instanceOf(java.lang.Class type) { * For example: *
assertThat(cheese, is(not(equalTo(smelly))))
* - * @param - * the matcher type. - * @param matcher - * the matcher whose sense should be inverted + * @param the matcher type. + * @param matcher the matcher whose sense should be inverted * @return The matcher. */ public static org.hamcrest.Matcher not(org.hamcrest.Matcher matcher) { @@ -618,10 +525,8 @@ public static org.hamcrest.Matcher not(org.hamcrest.Matcher matcher) { * instead of: *
assertThat(cheese, is(not(equalTo(smelly))))
* - * @param - * the matcher type. - * @param value - * the value that any examined object should not equal + * @param the matcher type. + * @param value the value that any examined object should not equal * @return The matcher. */ public static org.hamcrest.Matcher not(T value) { @@ -649,10 +554,8 @@ public static org.hamcrest.Matcher notNullValue() { * instead of: *
assertThat(cheese, is(not(nullValue(X.class))))
* - * @param - * the matcher type. - * @param type - * dummy parameter used to infer the generic type of the returned matcher + * @param the matcher type. + * @param type dummy parameter used to infer the generic type of the returned matcher * @return The matcher. */ public static org.hamcrest.Matcher notNullValue(java.lang.Class type) { @@ -676,10 +579,8 @@ public static org.hamcrest.Matcher nullValue() { * For example: *
assertThat(cheese, is(nullValue(Cheese.class))
* - * @param - * the matcher type. - * @param type - * dummy parameter used to infer the generic type of the returned matcher + * @param the matcher type. + * @param type dummy parameter used to infer the generic type of the returned matcher * @return The matcher. */ public static org.hamcrest.Matcher nullValue(java.lang.Class type) { @@ -690,10 +591,8 @@ public static org.hamcrest.Matcher nullValue(java.lang.Class type) { * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * - * @param - * the matcher type. - * @param target - * the target instance against which others should be assessed + * @param the matcher type. + * @param target the target instance against which others should be assessed * @return The matcher. */ public static org.hamcrest.Matcher sameInstance(T target) { @@ -704,10 +603,8 @@ public static org.hamcrest.Matcher sameInstance(T target) { * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * - * @param - * the matcher type. - * @param target - * the target instance against which others should be assessed + * @param the matcher type. + * @param target the target instance against which others should be assessed * @return The matcher. */ public static org.hamcrest.Matcher theInstance(T target) { @@ -720,8 +617,7 @@ public static org.hamcrest.Matcher theInstance(T target) { * For example: *
assertThat("myStringOfNote", containsString("ring"))
* - * @param substring - * the substring that the returned matcher will expect to find within any examined string + * @param substring the substring that the returned matcher will expect to find within any examined string * @return The matcher. */ public static Matcher containsString(java.lang.String substring) { @@ -734,8 +630,7 @@ public static Matcher containsString(java.lang.String substrin * For example: *
assertThat("myStringOfNote", containsStringIgnoringCase("Ring"))
* - * @param substring - * the substring that the returned matcher will expect to find within any examined string + * @param substring the substring that the returned matcher will expect to find within any examined string * @return The matcher. */ public static Matcher containsStringIgnoringCase(java.lang.String substring) { @@ -750,8 +645,7 @@ public static Matcher containsStringIgnoringCase(java.lang.Str * For example: *
assertThat("myStringOfNote", startsWith("my"))
* - * @param prefix - * the substring that the returned matcher will expect at the start of any examined string + * @param prefix the substring that the returned matcher will expect at the start of any examined string * @return The matcher. */ public static Matcher startsWith(java.lang.String prefix) { @@ -766,8 +660,7 @@ public static Matcher startsWith(java.lang.String prefix) { * For example: *
assertThat("myStringOfNote", startsWithIgnoringCase("My"))
* - * @param prefix - * the substring that the returned matcher will expect at the start of any examined string + * @param prefix the substring that the returned matcher will expect at the start of any examined string * @return The matcher. */ public static Matcher startsWithIgnoringCase(java.lang.String prefix) { @@ -780,8 +673,7 @@ public static Matcher startsWithIgnoringCase(java.lang.String * For example: *
assertThat("myStringOfNote", endsWith("Note"))
* - * @param suffix - * the substring that the returned matcher will expect at the end of any examined string + * @param suffix the substring that the returned matcher will expect at the end of any examined string * @return The matcher. */ public static Matcher endsWith(java.lang.String suffix) { @@ -794,8 +686,7 @@ public static Matcher endsWith(java.lang.String suffix) { * For example: *
assertThat("myStringOfNote", endsWithIgnoringCase("note"))
* - * @param suffix - * the substring that the returned matcher will expect at the end of any examined string + * @param suffix the substring that the returned matcher will expect at the end of any examined string * @return The matcher. */ public static Matcher endsWithIgnoringCase(java.lang.String suffix) { @@ -809,8 +700,7 @@ public static Matcher endsWithIgnoringCase(java.lang.String su * assertThat("abc", matchesRegex(Pattern.compile("ˆ[a-z]$")); * * - * @param pattern - * the pattern to be used. + * @param pattern the pattern to be used. * @return The matcher. */ public static Matcher matchesRegex(Pattern pattern) { @@ -824,8 +714,7 @@ public static Matcher matchesRegex(Pattern pattern) { * assertThat("abc", matchesRegex("ˆ[a-z]+$")); * * - * @param regex - * The regex to be used for the validation. + * @param regex The regex to be used for the validation. * @return The matcher. */ public static Matcher matchesRegex(String regex) { @@ -839,10 +728,8 @@ public static Matcher matchesRegex(String regex) { * For example: *
assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))
* - * @param - * the matcher type. - * @param elementMatchers - * the matchers that the elements of examined arrays should satisfy + * @param the matcher type. + * @param elementMatchers the matchers that the elements of examined arrays should satisfy * @return The matcher. */ @SafeVarargs @@ -857,10 +744,8 @@ public static org.hamcrest.collection.IsArray array(org.hamcrest.Matcher< * For example: *
assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))
* - * @param - * the matcher type. - * @param elementMatcher - * the matcher to apply to elements in examined arrays + * @param the matcher type. + * @param elementMatcher the matcher to apply to elements in examined arrays * @return The matcher. */ public static org.hamcrest.Matcher hasItemInArray(org.hamcrest.Matcher elementMatcher) { @@ -874,10 +759,8 @@ public static org.hamcrest.Matcher hasItemInArray(org.hamcrest.Matcher< * instead of: *
assertThat(hasItemInArray(equalTo(x)))
* - * @param - * the matcher type. - * @param element - * the element that should be present in examined arrays + * @param the matcher type. + * @param element the element that should be present in examined arrays * @return The matcher. */ public static org.hamcrest.Matcher hasItemInArray(T element) { @@ -891,10 +774,8 @@ public static org.hamcrest.Matcher hasItemInArray(T element) { * For example: *
assertThat(new String[]{"foo", "bar"}, arrayContaining("foo", "bar"))
* - * @param - * the matcher type. - * @param items - * the items that must equal the items within an examined array + * @param the matcher type. + * @param items the items that must equal the items within an examined array * @return The matcher. */ @SafeVarargs @@ -909,10 +790,8 @@ public static org.hamcrest.Matcher arrayContaining(E... items) { * For example: *
assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")))
* - * @param - * the matcher type. - * @param itemMatchers - * the matchers that must be satisfied by the items in the examined array + * @param the matcher type. + * @param itemMatchers the matchers that must be satisfied by the items in the examined array * @return The matcher. */ @SafeVarargs @@ -927,10 +806,8 @@ public static org.hamcrest.Matcher arrayContaining(org.hamcrest.Matcher * For example: *
assertThat(new String[]{"foo", "bar"}, arrayContaining(Arrays.asList(equalTo("foo"), equalTo("bar"))))
* - * @param - * the matcher type. - * @param itemMatchers - * a list of matchers, each of which must be satisfied by the corresponding item in an examined array + * @param the matcher type. + * @param itemMatchers a list of matchers, each of which must be satisfied by the corresponding item in an examined array * @return The matcher. */ public static org.hamcrest.Matcher arrayContaining(java.util.List> itemMatchers) { @@ -954,10 +831,8 @@ public static org.hamcrest.Matcher arrayContaining(java.util.List *
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))
* - * @param - * the matcher type. - * @param itemMatchers - * a list of matchers, each of which must be satisfied by an entry in an examined array + * @param the matcher type. + * @param itemMatchers a list of matchers, each of which must be satisfied by an entry in an examined array * @return The matcher. */ @SafeVarargs @@ -982,10 +857,8 @@ public static org.hamcrest.Matcher arrayContainingInAnyOrder(org.hamcre *

*
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))
* - * @param - * the matcher type. - * @param itemMatchers - * a list of matchers, each of which must be satisfied by an item provided by an examined array + * @param the matcher type. + * @param itemMatchers a list of matchers, each of which must be satisfied by an item provided by an examined array * @return The matcher. */ public static org.hamcrest.Matcher arrayContainingInAnyOrder(java.util.Collection> itemMatchers) { @@ -1007,10 +880,8 @@ public static org.hamcrest.Matcher arrayContainingInAnyOrder(java.util. *

*
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder("bar", "foo"))
* - * @param - * the matcher type. - * @param items - * the items that must equal the entries of an examined array, in any order + * @param the matcher type. + * @param items the items that must equal the entries of an examined array, in any order * @return The matcher. */ @SafeVarargs @@ -1024,10 +895,8 @@ public static org.hamcrest.Matcher arrayContainingInAnyOrder(E... items * For example: *
assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))
* - * @param - * the matcher type. - * @param sizeMatcher - * a matcher for the length of an examined array + * @param the matcher type. + * @param sizeMatcher a matcher for the length of an examined array * @return The matcher. */ public static org.hamcrest.Matcher arrayWithSize(org.hamcrest.Matcher sizeMatcher) { @@ -1040,10 +909,8 @@ public static org.hamcrest.Matcher arrayWithSize(org.hamcrest.MatcherassertThat(new String[]{"foo", "bar"}, arrayWithSize(2)) * - * @param - * the matcher type. - * @param size - * the length that an examined array must have for a positive match + * @param the matcher type. + * @param size the length that an examined array must have for a positive match * @return The matcher. */ public static org.hamcrest.Matcher arrayWithSize(int size) { @@ -1056,8 +923,7 @@ public static org.hamcrest.Matcher arrayWithSize(int size) { * For example: *
assertThat(new String[0], emptyArray())
* - * @param - * the matcher type. + * @param the matcher type. * @return The matcher. */ public static org.hamcrest.Matcher emptyArray() { @@ -1070,15 +936,12 @@ public static org.hamcrest.Matcher emptyArray() { * For example: *
assertThat(myMap, is(aMapWithSize(equalTo(2))))
* - * @param - * the map key type. - * @param - * the map value type. - * @param sizeMatcher - * a matcher for the size of an examined {@link java.util.Map} + * @param the map key type. + * @param the map value type. + * @param sizeMatcher a matcher for the size of an examined {@link java.util.Map} * @return The matcher. */ - public static org.hamcrest.Matcher> aMapWithSize(org.hamcrest.Matcher sizeMatcher) { + public static org.hamcrest.Matcher> aMapWithSize(org.hamcrest.Matcher sizeMatcher) { return org.hamcrest.collection.IsMapWithSize.aMapWithSize(sizeMatcher); } @@ -1088,15 +951,12 @@ public static org.hamcrest.Matcher * For example: *
assertThat(myMap, is(aMapWithSize(2)))
* - * @param - * the map key type. - * @param - * the map value type. - * @param size - * the expected size of an examined {@link java.util.Map} + * @param the map key type. + * @param the map value type. + * @param size the expected size of an examined {@link java.util.Map} * @return The matcher. */ - public static org.hamcrest.Matcher> aMapWithSize(int size) { + public static org.hamcrest.Matcher> aMapWithSize(int size) { return org.hamcrest.collection.IsMapWithSize.aMapWithSize(size); } @@ -1106,13 +966,11 @@ public static org.hamcrest.Matcher * For example: *
assertThat(myMap, is(anEmptyMap()))
* - * @param - * the map key type. - * @param - * the map value type. + * @param the map key type. + * @param the map value type. * @return The matcher. */ - public static org.hamcrest.Matcher> anEmptyMap() { + public static org.hamcrest.Matcher> anEmptyMap() { return org.hamcrest.collection.IsMapWithSize.anEmptyMap(); } @@ -1122,10 +980,8 @@ public static org.hamcrest.Matcher * For example: *
assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))
* - * @param - * the matcher type. - * @param sizeMatcher - * a matcher for the size of an examined {@link java.util.Collection} + * @param the matcher type. + * @param sizeMatcher a matcher for the size of an examined {@link java.util.Collection} * @return The matcher. */ public static org.hamcrest.Matcher> hasSize(org.hamcrest.Matcher sizeMatcher) { @@ -1138,10 +994,8 @@ public static org.hamcrest.Matcher> hasSiz * For example: *
assertThat(Arrays.asList("foo", "bar"), hasSize(2))
* - * @param - * the matcher type. - * @param size - * the expected size of an examined {@link java.util.Collection} + * @param the matcher type. + * @param size the expected size of an examined {@link java.util.Collection} * @return The matcher. */ public static org.hamcrest.Matcher> hasSize(int size) { @@ -1154,8 +1008,7 @@ public static org.hamcrest.Matcher> hasSiz * For example: *
assertThat(new ArrayList<String>(), is(empty()))
* - * @param - * the matcher type. + * @param the matcher type. * @return The matcher. */ public static org.hamcrest.Matcher> empty() { @@ -1168,10 +1021,8 @@ public static org.hamcrest.Matcher> empty( * For example: *
assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)))
* - * @param - * the matcher type. - * @param unusedToForceReturnType - * the type of the collection's content + * @param the matcher type. + * @param unusedToForceReturnType the type of the collection's content * @return The matcher. */ public static org.hamcrest.Matcher> emptyCollectionOf(java.lang.Class unusedToForceReturnType) { @@ -1183,8 +1034,7 @@ public static org.hamcrest.Matcher> emptyCollectionO * For example: *
assertThat(new ArrayList<String>(), is(emptyIterable()))
* - * @param - * the matcher type. + * @param the matcher type. * @return The matcher. */ public static org.hamcrest.Matcher> emptyIterable() { @@ -1196,10 +1046,8 @@ public static org.hamcrest.Matcher> emptyIte * For example: *
assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)))
* - * @param - * the matcher type. - * @param unusedToForceReturnType - * the type of the iterable's content + * @param the matcher type. + * @param unusedToForceReturnType the type of the iterable's content * @return The matcher. */ public static org.hamcrest.Matcher> emptyIterableOf(java.lang.Class unusedToForceReturnType) { @@ -1214,10 +1062,8 @@ public static org.hamcrest.Matcher> emptyIterableOf(ja * For example: *
assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))
* - * @param - * the matcher type. - * @param items - * the items that must equal the items provided by an examined {@link Iterable} + * @param the matcher type. + * @param items the items that must equal the items provided by an examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -1232,11 +1078,9 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo"), contains(equalTo("foo")))
* - * @param - * the matcher type. - * @param itemMatcher - * the matcher that must be satisfied by the single item provided by an - * examined {@link Iterable} + * @param the matcher type. + * @param itemMatcher the matcher that must be satisfied by the single item provided by an + * examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> contains(org.hamcrest.Matcher itemMatcher) { @@ -1251,10 +1095,8 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))
* - * @param - * the matcher type. - * @param itemMatchers - * the matchers that must be satisfied by the items provided by an examined {@link Iterable} + * @param the matcher type. + * @param itemMatchers the matchers that must be satisfied by the items provided by an examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -1270,11 +1112,9 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))
* - * @param - * the matcher type. - * @param itemMatchers - * a list of matchers, each of which must be satisfied by the corresponding item provided by - * an examined {@link Iterable} + * @param the matcher type. + * @param itemMatchers a list of matchers, each of which must be satisfied by the corresponding item provided by + * an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> contains(java.util.List> itemMatchers) { @@ -1298,10 +1138,8 @@ public static org.hamcrest.Matcher> contains *

*
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(equalTo("bar"), equalTo("foo")))
* - * @param - * the matcher type. - * @param itemMatchers - * a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} + * @param the matcher type. + * @param itemMatchers a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -1326,10 +1164,8 @@ public static org.hamcrest.Matcher> contains *

*
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"))
* - * @param - * the matcher type. - * @param items - * the items that must equal the items provided by an examined {@link Iterable} in any order + * @param the matcher type. + * @param items the items that must equal the items provided by an examined {@link Iterable} in any order * @return The matcher. */ @SafeVarargs @@ -1352,10 +1188,8 @@ public static org.hamcrest.Matcher> contains *

For example:

*
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))
* - * @param - * the matcher type. - * @param itemMatchers - * a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} + * @param the matcher type. + * @param itemMatchers a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> containsInAnyOrder(java.util.Collection> itemMatchers) { @@ -1369,10 +1203,8 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder("b", "d"))
* - * @param - * the matcher type. - * @param items - * the items that must be contained within items provided by an examined {@link Iterable} in the same relative order + * @param the matcher type. + * @param items the items that must be contained within items provided by an examined {@link Iterable} in the same relative order * @return The matcher. */ @SafeVarargs @@ -1387,10 +1219,8 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder(equalTo("b"), equalTo("d")))
* - * @param - * the matcher type. - * @param itemMatchers - * the matchers that must be satisfied by the items provided by an examined {@link Iterable} in the same relative order + * @param the matcher type. + * @param itemMatchers the matchers that must be satisfied by the items provided by an examined {@link Iterable} in the same relative order * @return The matcher. */ @SafeVarargs @@ -1405,11 +1235,9 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("a", "b", "c", "d", "e"), contains(Arrays.asList(equalTo("b"), equalTo("d"))))
* - * @param - * the matcher type. - * @param itemMatchers - * a list of matchers, each of which must be satisfied by the items provided by - * an examined {@link Iterable} in the same relative order + * @param the matcher type. + * @param itemMatchers a list of matchers, each of which must be satisfied by the items provided by + * an examined {@link Iterable} in the same relative order * @return The matcher. */ public static org.hamcrest.Matcher> containsInRelativeOrder(java.util.List> itemMatchers) { @@ -1423,10 +1251,8 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)))
* - * @param - * the matcher type. - * @param sizeMatcher - * a matcher for the number of items that should be yielded by an examined {@link Iterable} + * @param the matcher type. + * @param sizeMatcher a matcher for the number of items that should be yielded by an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> iterableWithSize(org.hamcrest.Matcher sizeMatcher) { @@ -1440,10 +1266,8 @@ public static org.hamcrest.Matcher> iterableWithSize(o * For example: *
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2))
* - * @param - * the matcher type. - * @param size - * the number of items that should be yielded by an examined {@link Iterable} + * @param the matcher type. + * @param size the number of items that should be yielded by an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> iterableWithSize(int size) { @@ -1457,17 +1281,13 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))
* - * @param - * the map key type. - * @param - * the map value type. - * @param keyMatcher - * the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry - * @param valueMatcher - * the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry + * @param the map key type. + * @param the map value type. + * @param keyMatcher the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry + * @param valueMatcher the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry * @return The matcher. */ - public static org.hamcrest.Matcher> hasEntry(org.hamcrest.Matcher keyMatcher, org.hamcrest.Matcher valueMatcher) { + public static org.hamcrest.Matcher> hasEntry(org.hamcrest.Matcher keyMatcher, org.hamcrest.Matcher valueMatcher) { return org.hamcrest.collection.IsMapContaining.hasEntry(keyMatcher, valueMatcher); } @@ -1478,17 +1298,13 @@ public static org.hamcrest.Matcher * For example: *
assertThat(myMap, hasEntry("bar", "foo"))
* - * @param - * the map key type. - * @param - * the map value type. - * @param key - * the key that, in combination with the value, must be describe at least one entry - * @param value - * the value that, in combination with the key, must be describe at least one entry + * @param the map key type. + * @param the map value type. + * @param key the key that, in combination with the value, must be describe at least one entry + * @param value the value that, in combination with the key, must be describe at least one entry * @return The matcher. */ - public static org.hamcrest.Matcher> hasEntry(K key, V value) { + public static org.hamcrest.Matcher> hasEntry(K key, V value) { return org.hamcrest.collection.IsMapContaining.hasEntry(key, value); } @@ -1498,13 +1314,11 @@ public static org.hamcrest.Matcher * For example: *
assertThat(myMap, hasKey(equalTo("bar")))
* - * @param - * the map key type. - * @param keyMatcher - * the matcher that must be satisfied by at least one key + * @param the map key type. + * @param keyMatcher the matcher that must be satisfied by at least one key * @return The matcher. */ - public static org.hamcrest.Matcher> hasKey(org.hamcrest.Matcher keyMatcher) { + public static org.hamcrest.Matcher> hasKey(org.hamcrest.Matcher keyMatcher) { return org.hamcrest.collection.IsMapContaining.hasKey(keyMatcher); } @@ -1514,13 +1328,11 @@ public static org.hamcrest.Matcher> hasKey(org. * For example: *
assertThat(myMap, hasKey("bar"))
* - * @param - * the map key type. - * @param key - * the key that satisfying maps must contain + * @param the map key type. + * @param key the key that satisfying maps must contain * @return The matcher. */ - public static org.hamcrest.Matcher> hasKey(K key) { + public static org.hamcrest.Matcher> hasKey(K key) { return org.hamcrest.collection.IsMapContaining.hasKey(key); } @@ -1530,13 +1342,11 @@ public static org.hamcrest.Matcher> hasKey(K ke * For example: *
assertThat(myMap, hasValue(equalTo("foo")))
* - * @param - * the value type. - * @param valueMatcher - * the matcher that must be satisfied by at least one value + * @param the value type. + * @param valueMatcher the matcher that must be satisfied by at least one value * @return The matcher. */ - public static org.hamcrest.Matcher> hasValue(org.hamcrest.Matcher valueMatcher) { + public static org.hamcrest.Matcher> hasValue(org.hamcrest.Matcher valueMatcher) { return org.hamcrest.collection.IsMapContaining.hasValue(valueMatcher); } @@ -1546,13 +1356,11 @@ public static org.hamcrest.Matcher> hasValue(or * For example: *
assertThat(myMap, hasValue("foo"))
* - * @param - * the value type. - * @param value - * the value that satisfying maps must contain + * @param the value type. + * @param value the value that satisfying maps must contain * @return The matcher. */ - public static org.hamcrest.Matcher> hasValue(V value) { + public static org.hamcrest.Matcher> hasValue(V value) { return org.hamcrest.collection.IsMapContaining.hasValue(value); } @@ -1562,10 +1370,8 @@ public static org.hamcrest.Matcher> hasValue(V * For example: *
assertThat("foo", is(in(Arrays.asList("bar", "foo"))))
* - * @param - * the matcher type. - * @param collection - * the collection in which matching items must be found + * @param the matcher type. + * @param collection the collection in which matching items must be found * @return The matcher. */ public static org.hamcrest.Matcher in(java.util.Collection collection) { @@ -1578,10 +1384,8 @@ public static org.hamcrest.Matcher in(java.util.Collection collection) * For example: *
assertThat("foo", is(in(new String[]{"bar", "foo"})))
* - * @param - * the matcher type. - * @param elements - * the array in which matching items must be found + * @param the matcher type. + * @param elements the array in which matching items must be found * @return The matcher. */ public static org.hamcrest.Matcher in(T[] elements) { @@ -1594,12 +1398,10 @@ public static org.hamcrest.Matcher in(T[] elements) { * For example: *
assertThat("foo", isIn(Arrays.asList("bar", "foo")))
* - * @param - * the matcher type. - * @deprecated use is(in(...)) instead - * @param collection - * the collection in which matching items must be found + * @param the matcher type. + * @param collection the collection in which matching items must be found * @return The matcher. + * @deprecated use is(in(...)) instead */ @SuppressWarnings("deprecation") public static org.hamcrest.Matcher isIn(java.util.Collection collection) { @@ -1612,12 +1414,10 @@ public static org.hamcrest.Matcher isIn(java.util.Collection collectio * For example: *
assertThat("foo", isIn(new String[]{"bar", "foo"}))
* - * @deprecated use is(in(...)) instead - * @param - * the matcher type. - * @param elements - * the array in which matching items must be found + * @param the matcher type. + * @param elements the array in which matching items must be found * @return The matcher. + * @deprecated use is(in(...)) instead */ @SuppressWarnings("deprecation") public static org.hamcrest.Matcher isIn(T[] elements) { @@ -1630,12 +1430,10 @@ public static org.hamcrest.Matcher isIn(T[] elements) { * For example: *
assertThat("foo", isOneOf("bar", "foo"))
* - * @deprecated use is(oneOf(...)) instead - * @param - * the matcher type. - * @param elements - * the elements amongst which matching items will be found + * @param the matcher type. + * @param elements the elements amongst which matching items will be found * @return The matcher. + * @deprecated use is(oneOf(...)) instead */ @SuppressWarnings("deprecation") @SafeVarargs @@ -1649,10 +1447,8 @@ public static org.hamcrest.Matcher isOneOf(T... elements) { * For example: *
assertThat("foo", is(oneOf("bar", "foo")))
* - * @param - * the matcher type. - * @param elements - * the elements amongst which matching items will be found + * @param the matcher type. + * @param elements the elements amongst which matching items will be found * @return The matcher. */ @SafeVarargs @@ -1666,10 +1462,8 @@ public static org.hamcrest.Matcher oneOf(T... elements) { * For example: *
assertThat(1.03, is(closeTo(1.0, 0.03)))
* - * @param operand - * the expected value of matching doubles - * @param error - * the delta (+/-) within which matches will be allowed + * @param operand the expected value of matching doubles + * @param error the delta (+/-) within which matches will be allowed * @return The matcher. */ public static org.hamcrest.Matcher closeTo(double operand, double error) { @@ -1694,10 +1488,8 @@ public static org.hamcrest.Matcher notANumber() { * For example: *
assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))
* - * @param operand - * the expected value of matching BigDecimals - * @param error - * the delta (+/-) within which matches will be allowed + * @param operand the expected value of matching BigDecimals + * @param error the delta (+/-) within which matches will be allowed * @return The matcher. */ public static org.hamcrest.Matcher closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) { @@ -1711,8 +1503,7 @@ public static org.hamcrest.Matcher closeTo(java.math.BigDe * For example: *
assertThat(1, comparesEqualTo(1))
* - * @param - * the matcher type. + * @param the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return zero * @return The matcher. */ @@ -1727,8 +1518,7 @@ public static > org.hamcrest.Matcher compar * For example: *
assertThat(2, greaterThan(1))
* - * @param - * the matcher type. + * @param the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return greater * than zero * @return The matcher. @@ -1744,8 +1534,7 @@ public static > org.hamcrest.Matcher greate * For example: *
assertThat(1, greaterThanOrEqualTo(1))
* - * @param - * the matcher type. + * @param the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return greater * than or equal to zero * @return The matcher. @@ -1761,8 +1550,7 @@ public static > org.hamcrest.Matcher greate * For example: *
assertThat(1, lessThan(2))
* - * @param - * the matcher type. + * @param the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return less * than zero * @return The matcher. @@ -1778,8 +1566,7 @@ public static > org.hamcrest.Matcher lessTh * For example: *
assertThat(1, lessThanOrEqualTo(1))
* - * @param - * the matcher type. + * @param the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return less * than or equal to zero * @return The matcher. @@ -1794,8 +1581,7 @@ public static > org.hamcrest.Matcher lessTh * For example: *
assertThat("Foo", equalToIgnoringCase("FOO"))
* - * @param expectedString - * the expected value of matched strings + * @param expectedString the expected value of matched strings * @return The matcher. */ public static Matcher equalToIgnoringCase(java.lang.String expectedString) { @@ -1803,10 +1589,9 @@ public static Matcher equalToIgnoringCase(java.lang.String exp } /** - * @deprecated {@link #equalToCompressingWhiteSpace(String)} - * @param expectedString - * the expected value of matched strings + * @param expectedString the expected value of matched strings * @return The matcher. + * @deprecated {@link #equalToCompressingWhiteSpace(String)} */ public static Matcher equalToIgnoringWhiteSpace(java.lang.String expectedString) { return equalToCompressingWhiteSpace(expectedString); @@ -1823,8 +1608,7 @@ public static Matcher equalToIgnoringWhiteSpace(java.lang.Stri * For example: *
assertThat("   my\tfoo  bar ", equalToIgnoringWhiteSpace(" my  foo bar"))
* - * @param expectedString - * the expected value of matched strings + * @param expectedString the expected value of matched strings * @return The matcher. */ public static Matcher equalToCompressingWhiteSpace(java.lang.String expectedString) { @@ -1860,8 +1644,8 @@ public static Matcher emptyString() { * For example: *
assertThat(((String)null), isEmptyOrNullString())
* - * @deprecated use is(emptyOrNullString()) instead * @return The matcher. + * @deprecated use is(emptyOrNullString()) instead */ @SuppressWarnings("deprecation") public static Matcher isEmptyOrNullString() { @@ -1873,8 +1657,8 @@ public static Matcher isEmptyOrNullString() { * For example: *
assertThat("", isEmptyString())
* - * @deprecated use is(emptyString()) instead * @return The matcher. + * @deprecated use is(emptyString()) instead */ @SuppressWarnings("deprecation") public static Matcher isEmptyString() { @@ -1909,8 +1693,7 @@ public static Matcher blankString() { * Creates a matcher of {@link java.lang.String} that matches when the examined string * exactly matches the given {@link java.util.regex.Pattern}. * - * @param pattern - * the text pattern to match. + * @param pattern the text pattern to match. * @return The matcher. */ public static Matcher matchesPattern(java.util.regex.Pattern pattern) { @@ -1921,8 +1704,7 @@ public static Matcher matchesPattern(java.util.regex.Pattern p * Creates a matcher of {@link java.lang.String} that matches when the examined string * exactly matches the given regular expression, treated as a {@link java.util.regex.Pattern}. * - * @param regex - * the regex to match. + * @param regex the regex to match. * @return The matcher. */ public static Matcher matchesPattern(java.lang.String regex) { @@ -1936,8 +1718,7 @@ public static Matcher matchesPattern(java.lang.String regex) { *
assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")))
* fails as "foo" occurs before "bar" in the string "myfoobarbaz" * - * @param substrings - * the substrings that must be contained within matching strings + * @param substrings the substrings that must be contained within matching strings * @return The matcher. */ public static Matcher stringContainsInOrder(java.lang.Iterable substrings) { @@ -1951,8 +1732,7 @@ public static Matcher stringContainsInOrder(java.lang.Iterable *
assertThat("myfoobarbaz", stringContainsInOrder("bar", "foo"))
* fails as "foo" occurs before "bar" in the string "myfoobarbaz" * - * @param substrings - * the substrings that must be contained within matching strings + * @param substrings the substrings that must be contained within matching strings * @return The matcher. */ public static Matcher stringContainsInOrder(java.lang.String... substrings) { @@ -1975,21 +1755,21 @@ public static Matcher hasLength(org.hamcrest.Matcherargument. - * For example: - * - *
-     * assertThat("text", length(4))
-     * 
- * - * @param length the expected length of the string - * @return The matcher. - */ - public static Matcher hasLength(int length) { - return org.hamcrest.text.CharSequenceLength.hasLength(length); - } + /** + * Creates a matcher of {@link CharSequence} that matches when a char sequence has the length + * of the specified argument. + * For example: + * + *
+   * assertThat("text", length(4))
+   * 
+ * + * @param length the expected length of the string + * @return The matcher. + */ + public static Matcher hasLength(int length) { + return org.hamcrest.text.CharSequenceLength.hasLength(length); + } /** * Creates a matcher that matches any examined object whose toString method @@ -1997,10 +1777,8 @@ public static Matcher hasLength(int length) { * For example: *
assertThat(true, hasToString(equalTo("TRUE")))
* - * @param - * the matcher type. - * @param toStringMatcher - * the matcher used to verify the toString result + * @param the matcher type. + * @param toStringMatcher the matcher used to verify the toString result * @return The matcher. */ public static org.hamcrest.Matcher hasToString(org.hamcrest.Matcher toStringMatcher) { @@ -2013,10 +1791,8 @@ public static org.hamcrest.Matcher hasToString(org.hamcrest.MatcherassertThat(true, hasToString("TRUE")) * - * @param - * the matcher type. - * @param expectedToString - * the expected toString result + * @param the matcher type. + * @param expectedToString the expected toString result * @return The matcher. */ public static org.hamcrest.Matcher hasToString(java.lang.String expectedToString) { @@ -2029,10 +1805,8 @@ public static org.hamcrest.Matcher hasToString(java.lang.String expectedT * For example: *
assertThat(Integer.class, typeCompatibleWith(Number.class))
* - * @param - * the matcher type. - * @param baseType - * the base class to examine classes against + * @param the matcher type. + * @param baseType the base class to examine classes against * @return The matcher. */ public static org.hamcrest.Matcher> typeCompatibleWith(java.lang.Class baseType) { @@ -2045,10 +1819,8 @@ public static org.hamcrest.Matcher> typeCompatibleWith(ja * For example: *
assertThat(myEvent, is(eventFrom(PropertyChangeEvent.class, myBean)))
* - * @param eventClass - * the class of the event to match on - * @param source - * the source of the event + * @param eventClass the class of the event to match on + * @param source the source of the event * @return The matcher. */ public static org.hamcrest.Matcher eventFrom(java.lang.Class eventClass, java.lang.Object source) { @@ -2061,8 +1833,7 @@ public static org.hamcrest.Matcher eventFrom(java.lang.Cl * For example: *
assertThat(myEvent, is(eventFrom(myBean)))
* - * @param source - * the source of the event + * @param source the source of the event * @return The matcher. */ public static org.hamcrest.Matcher eventFrom(java.lang.Object source) { @@ -2075,10 +1846,8 @@ public static org.hamcrest.Matcher eventFrom(java.lang.Ob * For example: *
assertThat(myBean, hasProperty("foo"))
* - * @param - * the matcher type. - * @param propertyName - * the name of the JavaBean property that examined beans should possess + * @param the matcher type. + * @param propertyName the name of the JavaBean property that examined beans should possess * @return The matcher. */ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyName) { @@ -2091,12 +1860,9 @@ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyN * For example: *
assertThat(myBean, hasProperty("foo", equalTo("bar"))
* - * @param - * the matcher type. - * @param propertyName - * the name of the JavaBean property that examined beans should possess - * @param valueMatcher - * a matcher for the value of the specified property of the examined bean + * @param the matcher type. + * @param propertyName the name of the JavaBean property that examined beans should possess + * @param valueMatcher a matcher for the value of the specified property of the examined bean * @return The matcher. */ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyName, org.hamcrest.Matcher valueMatcher) { @@ -2113,12 +1879,9 @@ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyN *
assertThat(myBean, samePropertyValuesAs(myExpectedBean))
*
assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")
* - * @param - * the matcher type. - * @param expectedBean - * the bean against which examined beans are compared - * @param ignoredProperties - * do not check any of these named properties. + * @param the matcher type. + * @param expectedBean the bean against which examined beans are compared + * @param ignoredProperties do not check any of these named properties. * @return The matcher. */ public static Matcher samePropertyValuesAs(B expectedBean, String... ignoredProperties) { @@ -2131,10 +1894,8 @@ public static Matcher samePropertyValuesAs(B expectedBean, String... igno * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))
* - * @param xPath - * the target xpath - * @param valueMatcher - * matcher for the value at the specified xpath + * @param xPath the target xpath + * @param valueMatcher matcher for the value at the specified xpath * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, Matcher valueMatcher) { @@ -2148,12 +1909,9 @@ public static org.hamcrest.Matcher hasXPath(java.lang.String x * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))
* - * @param xPath - * the target xpath - * @param namespaceContext - * the namespace for matching nodes - * @param valueMatcher - * matcher for the value at the specified xpath + * @param xPath the target xpath + * @param namespaceContext the namespace for matching nodes + * @param valueMatcher matcher for the value at the specified xpath * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher valueMatcher) { @@ -2166,8 +1924,7 @@ public static org.hamcrest.Matcher hasXPath(java.lang.String x * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese"))
* - * @param xPath - * the target xpath + * @param xPath the target xpath * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath) { @@ -2180,10 +1937,8 @@ public static org.hamcrest.Matcher hasXPath(java.lang.String x * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))
* - * @param xPath - * the target xpath - * @param namespaceContext - * the namespace for matching nodes + * @param xPath the target xpath + * @param namespaceContext the namespace for matching nodes * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext) { @@ -2213,7 +1968,7 @@ public static Matcher> optionalWithValue() { /** * Matcher for {@link Optional} that expects that value is present and is equal to value * - * @param type of optional value + * @param type of optional value * @param value to validate present optional value * @return The matcher. */ @@ -2224,7 +1979,7 @@ public static Matcher> optionalWithValue(T value) { /** * Matcher for {@link Optional} that expects that value is present and matches matcher * - * @param type of optional value + * @param type of optional value * @param matcher matcher to validate present optional value * @return The matcher. */ @@ -2242,18 +1997,6 @@ public static ThrowsException throwsException() { return ThrowsException.throwsException(); } - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided throwable - * - * @param type of the Runnable - * @param type of the Throwable - * @param throwable the Throwable instance against which examined exceptions are compared - * @return The matcher. - */ - public static ThrowsException throwsException(U throwable) { - return ThrowsException.throwsException(throwable); - } - /** * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class * @@ -2267,50 +2010,52 @@ public static ThrowsException throw } /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided message class + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and has a message equal to the provided message * - * @param type of the Runnable - * @param message the String against which examined exception messages are compared + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param message the String against which examined exception messages are compared * @return The matcher. */ - public static ThrowsException throwsException(String message) { - return ThrowsException.throwsException(message); + public static ThrowsException throwsException(Class throwableClass, String message) { + return ThrowsException.throwsException(throwableClass, message); } /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception matching provided matcher + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and has a message matching the provided messageMatcher * - * @param type of the Runnable - * @param matcher matcher to validate the exception + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param messageMatcher matcher to validate exception's message * @return The matcher. */ - public static ThrowsException throwsException(Matcher matcher) { - return ThrowsException.throwsException(matcher); + public static ThrowsException throwsException(Class throwableClass, Matcher messageMatcher) { + return ThrowsException.throwsException(throwableClass, messageMatcher); } /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and has a message equal to the provided message + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided message * - * @param type of the Runnable - * @param type of the Throwable - * @param throwableClass the Throwable class against which examined exceptions are compared - * @param message the String against which examined exception messages are compared + * @param type of the Runnable + * @param type of the Throwable + * @param message the String against which examined exception messages are compared * @return The matcher. */ - public static ThrowsException throwsException(Class throwableClass, String message) { - return ThrowsException.throwsException(throwableClass, message); + public static ThrowsException throwsExceptionWithMessage(String message) { + return ThrowsException.throwsExceptionWithMessage(message); } /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and matches the provided matcher + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message matching the provided messageMatcher * - * @param type of the Runnable - * @param type of the Throwable - * @param throwableClass the Throwable class against which examined exceptions are compared - * @param matcher matcher to validate the exception + * @param type of the Runnable + * @param type of the Throwable + * @param messageMatcher matcher to validate exception's message * @return The matcher. */ - public static ThrowsException throwsException(Class throwableClass, Matcher matcher) { - return ThrowsException.throwsException(throwableClass, matcher); + public static ThrowsException throwsExceptionWithMessage(Matcher messageMatcher) { + return ThrowsException.throwsExceptionWithMessage(messageMatcher); } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index da47cd3a..19db5f90 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -2,137 +2,79 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeMatcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.hamcrest.core.IsInstanceOf; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.exception.ThrowsExceptionEqualTo.exceptionEqualTo; -import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage; +import static org.hamcrest.core.IsAnything.anything; +import static org.hamcrest.core.IsEqual.equalTo; /** * Tests if a Runnable throws a matching exception. * * @param the type of the matched Runnable */ -public class ThrowsException extends TypeSafeMatcher { +public class ThrowsException extends TypeSafeDiagnosingMatcher { + private final IsInstanceOf classMatcher; + private final Matcher messageMatcher; - private final Matcher exceptionMatcher; - private Throwable actualException; - - /** - * Constructor, best called from one of the static factory methods. - * - * @param elementMatcher matches the expected element - */ - public ThrowsException(Matcher elementMatcher) { - this.exceptionMatcher = elementMatcher; + public ThrowsException(IsInstanceOf classMatcher, Matcher messageMatcher) { + this.classMatcher = classMatcher; + this.messageMatcher = messageMatcher; } - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception - * - * @param type of the Runnable - * @return The matcher. - */ public static ThrowsException throwsException() { - return new ThrowsException<>(instanceOf(Throwable.class)); - } - - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided throwable - * - * @param type of the Runnable - * @param type of the Throwable - * @param throwable the Throwable instance against which examined exceptions are compared - * @return The matcher. - */ - public static ThrowsException throwsException(U throwable) { - return new ThrowsException<>(exceptionEqualTo(throwable)); + return throwsException(Throwable.class); } - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class - * - * @param type of the Runnable - * @param type of the Throwable - * @param throwableClass the Throwable class against which examined exceptions are compared - * @return The matcher. - */ public static ThrowsException throwsException(Class throwableClass) { - return new ThrowsException<>(instanceOf(throwableClass)); + return new ThrowsException<>(new IsInstanceOf(throwableClass), anything("")); } - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided message class - * - * @param type of the Runnable - * @param message the String against which examined exception messages are compared - * @return The matcher. - */ - public static ThrowsException throwsException(String message) { - return new ThrowsException<>(withMessage(message)); + public static ThrowsException throwsException(Class throwableClass, String exactMessage) { + return throwsException(throwableClass, equalTo(exactMessage)); } - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception matching provided matcher - * - * @param type of the Runnable - * @param matcher matcher to validate the exception - * @return The matcher. - */ - public static ThrowsException throwsException(Matcher matcher) { - return new ThrowsException<>(matcher); + public static ThrowsException throwsException(Class throwableClass, Matcher messageMatcher) { + return new ThrowsException<>(new IsInstanceOf(throwableClass), messageMatcher); } - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and has a message equal to the provided message - * - * @param type of the Runnable - * @param type of the Throwable - * @param throwableClass the Throwable class against which examined exceptions are compared - * @param message the String against which examined exception messages are compared - * @return The matcher. - */ - public static ThrowsException throwsException(Class throwableClass, String message) { - return new ThrowsException<>(allOf(instanceOf(throwableClass), withMessage(message))); + public static ThrowsException throwsExceptionWithMessage(String exactMessage) { + return throwsException(Throwable.class, equalTo(exactMessage)); } - /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided throwableClass class and matches the provided matcher - * - * @param type of the Runnable - * @param type of the Throwable - * @param throwableClass the Throwable class against which examined exceptions are compared - * @param matcher matcher to validate the exception - * @return The matcher. - */ - public static ThrowsException throwsException(Class throwableClass, Matcher matcher) { - return new ThrowsException<>(allOf(instanceOf(throwableClass), matcher)); + public static ThrowsException throwsExceptionWithMessage(Matcher messageMatcher) { + return throwsException(Throwable.class, messageMatcher); } @Override - public boolean matchesSafely(T runnable) { + protected boolean matchesSafely(T runnable, Description mismatchDescription) { try { runnable.run(); + mismatchDescription.appendText("the runnable didn't throw"); return false; } catch (Throwable t) { - actualException = t; - return exceptionMatcher.matches(t); + boolean classMatches = classMatcher.matches(t); + if (!classMatches) { + mismatchDescription.appendText("thrown exception class was ").appendText(t.getClass().getName()); + } + + boolean messageMatches = messageMatcher.matches(t.getMessage()); + if (!messageMatches) { + if (!classMatches) { + mismatchDescription.appendText(" and the "); + } + mismatchDescription.appendText("thrown exception message "); + messageMatcher.describeMismatch(t.getMessage(), mismatchDescription); + } + + return classMatches && messageMatches; } } @Override public void describeTo(Description description) { - description.appendText("a runnable throwing ").appendDescriptionOf(exceptionMatcher); - } - - @Override - public void describeMismatchSafely(T runnable, Description mismatchDescription) { - if (actualException == null) { - mismatchDescription.appendText("the runnable didn't throw"); - return; - } - mismatchDescription.appendText("the runnable threw "); - exceptionMatcher.describeMismatch(actualException, mismatchDescription); + description + .appendText("a runnable throwing ").appendDescriptionOf(classMatcher) + .appendText(" with message ").appendDescriptionOf(messageMatcher); } } diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java deleted file mode 100644 index 971d4f25..00000000 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ /dev/null @@ -1,157 +0,0 @@ -package org.hamcrest.exception; - -import org.junit.jupiter.api.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.exception.ThrowsException.throwsException; -import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage; -import static org.hamcrest.test.MatcherAssertions.*; - -public final class ThrowsExceptionEqualTest { - - private Runnable runnableThrowing(Throwable exception) { - return new ThrowingRunnable(exception); - } - - @Test - public void examples() { - RuntimeException boom = new RuntimeException("boom"); - Runnable codeThatThrows = () -> { - throw boom; - }; - - assertThat(codeThatThrows, throwsException()); - assertThat(codeThatThrows, throwsException(boom)); - assertThat(codeThatThrows, throwsException(RuntimeException.class)); - assertThat(codeThatThrows, throwsException("boom")); - assertThat(codeThatThrows, throwsException(withMessage("boom"))); - assertThat(codeThatThrows, throwsException(withMessage(containsString("oom")))); - assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom")); - assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage("boom"))); - assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage(containsString("boom")))); - } - - @Test - public void evaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { - assertMatches( - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new IllegalArgumentException("Boom!")) - ); - - assertDescription( - "a runnable throwing \"java.lang.IllegalArgumentException\" with message \"Boom!\"", - throwsException(new IllegalArgumentException("Boom!")) - ); - - assertMismatchDescription( - "the runnable threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"", - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); - - assertMismatchDescription( - "the runnable threw \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception", - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new NullPointerException("Boom!")) - ); - - assertMismatchDescription( - "the runnable didn't throw", - throwsException(new IllegalArgumentException("Boom!")), - (Runnable) () -> { - } - ); - } - - @Test - public void evaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() { - assertMatches( - throwsException(new IllegalArgumentException("Boom!")), - runnableThrowing(new TestException("Boom!")) - ); - } - - @Test - public void evaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() { - assertMatches( - throwsException(withMessage("Boom!")), - runnableThrowing(new TestException("Boom!")) - ); - - assertDescription( - "a runnable throwing an exception with message \"Boom!\"", - throwsException(withMessage("Boom!")) - ); - - assertMismatchDescription( - "the runnable threw an exception with message \"Bang!\" instead of \"Boom!\"", - throwsException(withMessage("Boom!")), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); - } - - @Test - public void evaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() { - - assertMatches( - throwsException(withMessage(containsString("bar"))), - runnableThrowing(new TestException("Foo bar baz")) - ); - - assertDescription( - "a runnable throwing an exception with message a string containing \"bar\"", - throwsException(withMessage(containsString("bar"))) - ); - - assertMismatchDescription( - "the runnable threw an exception with message \"Bang!\" instead of a string containing \"bar\"", - throwsException(withMessage(containsString("bar"))), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); - } - - @Test - public void evaluatesToTrueIfRunnableThrowsMatchingException() { - assertMatches( - throwsException(instanceOf(TestException.class)), - runnableThrowing(new TestException("Boom!")) - ); - - assertDescription( - "a runnable throwing an instance of java.lang.NullPointerException", - throwsException(instanceOf(NullPointerException.class)) - ); - - assertMismatchDescription( - "the runnable threw is a java.lang.IllegalArgumentException", - throwsException(instanceOf(NullPointerException.class)), - runnableThrowing(new IllegalArgumentException("Bang!")) - ); - } - - public static class TestException extends IllegalArgumentException { - public TestException(String message) { - super(message); - } - } - - static class ThrowingRunnable implements Runnable { - private final Throwable throwable; - - ThrowingRunnable(Throwable throwable) { - this.throwable = throwable; - } - - @Override - public void run() { - sneakyThrow(throwable); - } - - @SuppressWarnings("unchecked") - private void sneakyThrow(Throwable throwable) throws T { - throw (T) throwable; - } - } -} diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java new file mode 100644 index 00000000..37445065 --- /dev/null +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java @@ -0,0 +1,86 @@ +package org.hamcrest.exception; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.exception.ThrowsException.throwsException; +import static org.hamcrest.test.MatcherAssertions.*; + +public final class ThrowsExceptionTest { + + public static void throwIllegalArgumentException() { + throw new IllegalArgumentException("Boom!"); + } + + public static void throwNullPointerException() { + throw new NullPointerException("Boom!"); + } + + @Test + public void examples() { + assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException()); + assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class)); + assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, "Boom!")); + assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, containsString("Boo"))); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { + assertMatches( + throwsException(IllegalArgumentException.class, "Boom!"), + ThrowsExceptionTest::throwIllegalArgumentException + ); + + assertDescription( + "a runnable throwing an instance of java.lang.IllegalArgumentException with message \"Boom!\"", + throwsException(IllegalArgumentException.class, "Boom!") + ); + + assertMismatchDescription( + "thrown exception message was \"Boom!\"", + throwsException(IllegalArgumentException.class, "Bang!"), + (Runnable) ThrowsExceptionTest::throwIllegalArgumentException + ); + + assertMismatchDescription( + "thrown exception class was java.lang.NullPointerException", + throwsException(IllegalArgumentException.class, "Boom!"), + (Runnable) ThrowsExceptionTest::throwNullPointerException + ); + + assertMismatchDescription( + "the runnable didn't throw", + throwsException(IllegalArgumentException.class, "Boom!"), + (Runnable) () -> { + } + ); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() { + assertMatches( + throwsException(IllegalArgumentException.class, "Boom!"), + ThrowsExceptionTest::throwIllegalArgumentException + ); + } + + @Test + public void evaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() { + assertMatches( + throwsException(IllegalArgumentException.class, containsString("Boo")), + ThrowsExceptionTest::throwIllegalArgumentException + ); + + assertDescription( + "a runnable throwing an instance of java.lang.IllegalArgumentException with message a string containing \"Boo\"", + throwsException(IllegalArgumentException.class, containsString("Boo")) + ); + + assertMismatchDescription( + "thrown exception class was java.lang.NullPointerException", + throwsException(IllegalArgumentException.class, containsString("Boo")), + (Runnable) ThrowsExceptionTest::throwNullPointerException + ); + } +} From d5cfa9c07fde26b6f0adf84984726b3895279de2 Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sat, 2 Nov 2024 08:08:25 +0100 Subject: [PATCH 15/16] Cleanup: remove unused classes --- .../exception/ThrowsExceptionEqualTo.java | 45 ----------------- .../exception/ThrowsExceptionWithMessage.java | 48 ------------------- 2 files changed, 93 deletions(-) delete mode 100644 hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java delete mode 100644 hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java deleted file mode 100644 index 6c4afe07..00000000 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.hamcrest.exception; - -import org.hamcrest.Description; -import org.hamcrest.TypeSafeDiagnosingMatcher; - -/** - * A matcher that checks if a Runnable throws an expected exception when run. - * - * @param the type of the expected exception. - */ -public class ThrowsExceptionEqualTo extends TypeSafeDiagnosingMatcher { - private final T expectedException; - - /** - * Constructor, best called from one of the static factory methods. - * - * @param expectedException the expected exception. - */ - public ThrowsExceptionEqualTo(T expectedException) { - this.expectedException = expectedException; - } - - public static ThrowsExceptionEqualTo exceptionEqualTo(U expectedException) { - return new ThrowsExceptionEqualTo<>(expectedException); - } - - @Override - public boolean matchesSafely(T item, Description mismatchDescription) { - if (!this.expectedException.getClass().isAssignableFrom(item.getClass())) { - mismatchDescription.appendValue(item.getClass().getName()).appendText(" instead of a ").appendValue(this.expectedException.getClass().getName()).appendText(" exception"); - return false; - } - if (!item.getMessage().equals(this.expectedException.getMessage())) { - mismatchDescription.appendValue(item.getClass().getName()).appendText(" with message ").appendValue(item.getMessage()).appendText(" instead of ").appendValue(this.expectedException.getMessage()); - return false; - } - - return true; - } - - @Override - public void describeTo(Description description) { - description.appendValue(expectedException.getClass().getName()).appendText(" with message ").appendValue(expectedException.getMessage()); - } -} diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java deleted file mode 100644 index 8ee3b320..00000000 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.hamcrest.exception; - -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeDiagnosingMatcher; - -import static org.hamcrest.core.IsEqual.equalTo; - -/** - * A matcher that checks if a Runnable throws an expected exception when run. - * - * @param the type of the expected exception. - */ -public class ThrowsExceptionWithMessage extends TypeSafeDiagnosingMatcher { - private final Matcher messageMatcher; - - /** - * Constructor, best called from one of the static factory methods. - * - * @param messageMatcher matches the exception message - */ - public ThrowsExceptionWithMessage(Matcher messageMatcher) { - this.messageMatcher = messageMatcher; - } - - public static ThrowsExceptionWithMessage withMessage(V message) { - return new ThrowsExceptionWithMessage<>(equalTo(message)); - } - - public static ThrowsExceptionWithMessage withMessage(Matcher messageMatcher) { - return new ThrowsExceptionWithMessage<>(messageMatcher); - } - - @Override - protected boolean matchesSafely(T item, Description mismatchDescription) { - if (!this.messageMatcher.matches(item.getMessage())) { - mismatchDescription.appendText("an exception with message ").appendValue(item.getMessage()).appendText(" instead of ").appendDescriptionOf(messageMatcher); - return false; - } - - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("an exception with message ").appendDescriptionOf(messageMatcher); - } -} From ed8fab09147e71baaa3b3e0773ce481325a4521d Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 3 Nov 2024 22:30:50 +0100 Subject: [PATCH 16/16] Cleanup: revert unwanted reformatting --- .../src/main/java/org/hamcrest/Matchers.java | 773 ++++++++++++------ 1 file changed, 508 insertions(+), 265 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/Matchers.java b/hamcrest/src/main/java/org/hamcrest/Matchers.java index 37a9e164..300560ae 100644 --- a/hamcrest/src/main/java/org/hamcrest/Matchers.java +++ b/hamcrest/src/main/java/org/hamcrest/Matchers.java @@ -30,8 +30,10 @@ private Matchers() { * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param the matcher type. - * @param matchers all the matchers must pass. + * @param + * the matcher type. + * @param matchers + * all the matchers must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(java.lang.Iterable> matchers) { @@ -43,8 +45,10 @@ public static org.hamcrest.Matcher allOf(java.lang.IterableassertThat("myValue", allOf(startsWith("my"), containsString("Val"))) * - * @param the matcher type. - * @param matchers all the matchers must pass. + * @param + * the matcher type. + * @param matchers + * all the matchers must pass. * @return The matcher. */ @SafeVarargs @@ -57,9 +61,12 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher. * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param the matcher type. - * @param first first matcher that must pass. - * @param second second matcher that must pass. + * @param + * the matcher type. + * @param first + * first matcher that must pass. + * @param second + * second matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second) { @@ -71,10 +78,14 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param the matcher type. - * @param first first matcher that must pass. - * @param second second matcher that must pass. - * @param third third matcher that must pass. + * @param + * the matcher type. + * @param first + * first matcher that must pass. + * @param second + * second matcher that must pass. + * @param third + * third matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third) { @@ -86,11 +97,16 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param the matcher type. - * @param first first matcher that must pass. - * @param second second matcher that must pass. - * @param third third matcher that must pass. - * @param fourth fourth matcher that must pass. + * @param + * the matcher type. + * @param first + * first matcher that must pass. + * @param second + * second matcher that must pass. + * @param third + * third matcher that must pass. + * @param fourth + * fourth matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth) { @@ -102,12 +118,18 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param the matcher type. - * @param first first matcher that must pass. - * @param second second matcher that must pass. - * @param third third matcher that must pass. - * @param fourth fourth matcher that must pass. - * @param fifth fifth matcher that must pass. + * @param + * the matcher type. + * @param first + * first matcher that must pass. + * @param second + * second matcher that must pass. + * @param third + * third matcher that must pass. + * @param fourth + * fourth matcher that must pass. + * @param fifth + * fifth matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth) { @@ -119,13 +141,20 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
* - * @param the matcher type. - * @param first first matcher that must pass. - * @param second second matcher that must pass. - * @param third third matcher that must pass. - * @param fourth fourth matcher that must pass. - * @param fifth fifth matcher that must pass. - * @param sixth sixth matcher that must pass. + * @param + * the matcher type. + * @param first + * first matcher that must pass. + * @param second + * second matcher that must pass. + * @param third + * third matcher that must pass. + * @param fourth + * fourth matcher that must pass. + * @param fifth + * fifth matcher that must pass. + * @param sixth + * sixth matcher that must pass. * @return The matcher. */ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth, org.hamcrest.Matcher sixth) { @@ -137,8 +166,10 @@ public static org.hamcrest.Matcher allOf(org.hamcrest.Matcher * For example: *
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
* - * @param the matcher type. - * @param matchers any the matchers must pass. + * @param + * the matcher type. + * @param matchers + * any the matchers must pass. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(java.lang.Iterable> matchers) { @@ -150,8 +181,10 @@ public static org.hamcrest.core.AnyOf anyOf(java.lang.IterableassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param the matcher type. - * @param matchers any the matchers must pass. + * @param + * the matcher type. + * @param matchers + * any the matchers must pass. * @return The matcher. */ @SafeVarargs @@ -164,9 +197,12 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param the matcher type. - * @param first first matcher to check. - * @param second second matcher to check. + * @param + * the matcher type. + * @param first + * first matcher to check. + * @param second + * second matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second) { @@ -178,10 +214,14 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param the matcher type. - * @param first first matcher to check. - * @param second second matcher to check. - * @param third third matcher to check. + * @param + * the matcher type. + * @param first + * first matcher to check. + * @param second + * second matcher to check. + * @param third + * third matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third) { @@ -193,11 +233,16 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param the matcher type. - * @param first first matcher to check. - * @param second second matcher to check. - * @param third third matcher to check. - * @param fourth fourth matcher to check. + * @param + * the matcher type. + * @param first + * first matcher to check. + * @param second + * second matcher to check. + * @param third + * third matcher to check. + * @param fourth + * fourth matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth) { @@ -209,12 +254,18 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param the matcher type. - * @param first first matcher to check. - * @param second second matcher to check. - * @param third third matcher to check. - * @param fourth fourth matcher to check. - * @param fifth fifth matcher to check. + * @param + * the matcher type. + * @param first + * first matcher to check. + * @param second + * second matcher to check. + * @param third + * third matcher to check. + * @param fourth + * fourth matcher to check. + * @param fifth + * fifth matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth) { @@ -226,13 +277,20 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("myValue", anyOf(startsWith("foo"), containsString("Val"))) * - * @param the matcher type. - * @param first first matcher to check. - * @param second second matcher to check. - * @param third third matcher to check. - * @param fourth fourth matcher to check. - * @param fifth fifth matcher to check. - * @param sixth sixth matcher to check. + * @param + * the matcher type. + * @param first + * first matcher to check. + * @param second + * second matcher to check. + * @param third + * third matcher to check. + * @param fourth + * fourth matcher to check. + * @param fifth + * fifth matcher to check. + * @param sixth + * sixth matcher to check. * @return The matcher. */ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.Matcher first, org.hamcrest.Matcher second, org.hamcrest.Matcher third, org.hamcrest.Matcher fourth, org.hamcrest.Matcher fifth, org.hamcrest.Matcher sixth) { @@ -244,8 +302,10 @@ public static org.hamcrest.core.AnyOf anyOf(org.hamcrest.MatcherassertThat("fab", both(containsString("a")).and(containsString("b"))) * - * @param the matcher type. - * @param matcher the matcher to combine, and both must pass. + * @param + * the matcher type. + * @param matcher + * the matcher to combine, and both must pass. * @return The matcher. */ public static org.hamcrest.core.CombinableMatcher.CombinableBothMatcher both(org.hamcrest.Matcher matcher) { @@ -257,8 +317,10 @@ public static org.hamcrest.core.CombinableMatcher.CombinableBothMatcherassertThat("fan", either(containsString("a")).or(containsString("b"))) * - * @param the matcher type. - * @param matcher the matcher to combine, and either must pass. + * @param + * the matcher type. + * @param matcher + * the matcher to combine, and either must pass. * @return The matcher. */ public static org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher either(org.hamcrest.Matcher matcher) { @@ -271,10 +333,14 @@ public static org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher< * For example: *
describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())
* - * @param the matcher type. - * @param description the new description for the wrapped matcher - * @param matcher the matcher to wrap - * @param values optional values to insert into the tokenized description + * @param + * the matcher type. + * @param description + * the new description for the wrapped matcher + * @param matcher + * the matcher to wrap + * @param values + * optional values to insert into the tokenized description * @return The matcher. */ public static org.hamcrest.Matcher describedAs(java.lang.String description, org.hamcrest.Matcher matcher, java.lang.Object... values) { @@ -288,8 +354,10 @@ public static org.hamcrest.Matcher describedAs(java.lang.String descripti * For example: *
assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))
* - * @param the matcher type. - * @param itemMatcher the matcher to apply to every item provided by the examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatcher + * the matcher to apply to every item provided by the examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> everyItem(org.hamcrest.Matcher itemMatcher) { @@ -304,8 +372,10 @@ public static org.hamcrest.Matcher> everyIte * instead of: *
assertThat(cheese, equalTo(smelly))
* - * @param the matcher type. - * @param matcher the matcher to wrap. + * @param + * the matcher type. + * @param matcher + * the matcher to wrap. * @return The matcher. */ public static org.hamcrest.Matcher is(org.hamcrest.Matcher matcher) { @@ -319,8 +389,10 @@ public static org.hamcrest.Matcher is(org.hamcrest.Matcher matcher) { * instead of: *
assertThat(cheese, is(equalTo(smelly)))
* - * @param the matcher type. - * @param value the value to check. + * @param + * the matcher type. + * @param value + * the value to check. * @return The matcher. */ public static org.hamcrest.Matcher is(T value) { @@ -334,8 +406,10 @@ public static org.hamcrest.Matcher is(T value) { * instead of: *
assertThat(cheese, is(instanceOf(Cheddar.class)))
* - * @param the matcher type. - * @param type the type to check. + * @param + * the matcher type. + * @param type + * the type to check. * @return The matcher. */ public static org.hamcrest.Matcher isA(java.lang.Class type) { @@ -344,7 +418,6 @@ public static org.hamcrest.Matcher isA(java.lang.Class type) { /** * Creates a matcher that always matches, regardless of the examined object. - * * @return The matcher. */ public static org.hamcrest.Matcher anything() { @@ -355,7 +428,8 @@ public static org.hamcrest.Matcher anything() { * Creates a matcher that always matches, regardless of the examined object, but describes * itself with the specified {@link String}. * - * @param description a meaningful {@link String} used when describing itself + * @param description + * a meaningful {@link String} used when describing itself * @return The matcher. */ public static org.hamcrest.Matcher anything(java.lang.String description) { @@ -370,8 +444,10 @@ public static org.hamcrest.Matcher anything(java.lang.String d * For example: *
assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))
* - * @param the matcher type. - * @param itemMatcher the matcher to apply to items provided by the examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatcher + * the matcher to apply to items provided by the examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> hasItem(org.hamcrest.Matcher itemMatcher) { @@ -386,8 +462,10 @@ public static org.hamcrest.Matcher> hasItem( * For example: *
assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))
* - * @param the matcher type. - * @param item the item to compare against the items provided by the examined {@link Iterable} + * @param + * the matcher type. + * @param item + * the item to compare against the items provided by the examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> hasItem(T item) { @@ -402,8 +480,10 @@ public static org.hamcrest.Matcher> hasItem( * For example: *
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))
* - * @param the matcher type. - * @param itemMatchers the matchers to apply to items provided by the examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatchers + * the matchers to apply to items provided by the examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -419,8 +499,10 @@ public static org.hamcrest.Matcher> hasItems * For example: *
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))
* - * @param the matcher type. - * @param items the items to compare against the items provided by the examined {@link Iterable} + * @param + * the matcher type. + * @param items + * the items to compare against the items provided by the examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -449,8 +531,10 @@ public static org.hamcrest.Matcher> hasItems * assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"})); * * - * @param the matcher type. - * @param operand the value to check. + * @param + * the matcher type. + * @param operand + * the value to check. * @return The matcher. */ public static org.hamcrest.Matcher equalTo(T operand) { @@ -461,7 +545,8 @@ public static org.hamcrest.Matcher equalTo(T operand) { * Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being * compared to be of the same static type. * - * @param operand the value to check. + * @param operand + * the value to check. * @return The matcher. */ public static org.hamcrest.Matcher equalToObject(java.lang.Object operand) { @@ -479,8 +564,10 @@ public static org.hamcrest.Matcher equalToObject(java.lang.Obj * For example: *
assertThat(new Canoe(), instanceOf(Canoe.class));
* - * @param the matcher type. - * @param type the type to check. + * @param + * the matcher type. + * @param type + * the type to check. * @return The matcher. */ public static org.hamcrest.Matcher any(java.lang.Class type) { @@ -496,8 +583,10 @@ public static org.hamcrest.Matcher any(java.lang.Class type) { * For example: *
assertThat(new Canoe(), instanceOf(Paddlable.class));
* - * @param the matcher type. - * @param type the type to check. + * @param + * the matcher type. + * @param type + * the type to check. * @return The matcher. */ public static org.hamcrest.Matcher instanceOf(java.lang.Class type) { @@ -510,8 +599,10 @@ public static org.hamcrest.Matcher instanceOf(java.lang.Class type) { * For example: *
assertThat(cheese, is(not(equalTo(smelly))))
* - * @param the matcher type. - * @param matcher the matcher whose sense should be inverted + * @param + * the matcher type. + * @param matcher + * the matcher whose sense should be inverted * @return The matcher. */ public static org.hamcrest.Matcher not(org.hamcrest.Matcher matcher) { @@ -525,8 +616,10 @@ public static org.hamcrest.Matcher not(org.hamcrest.Matcher matcher) { * instead of: *
assertThat(cheese, is(not(equalTo(smelly))))
* - * @param the matcher type. - * @param value the value that any examined object should not equal + * @param + * the matcher type. + * @param value + * the value that any examined object should not equal * @return The matcher. */ public static org.hamcrest.Matcher not(T value) { @@ -554,8 +647,10 @@ public static org.hamcrest.Matcher notNullValue() { * instead of: *
assertThat(cheese, is(not(nullValue(X.class))))
* - * @param the matcher type. - * @param type dummy parameter used to infer the generic type of the returned matcher + * @param + * the matcher type. + * @param type + * dummy parameter used to infer the generic type of the returned matcher * @return The matcher. */ public static org.hamcrest.Matcher notNullValue(java.lang.Class type) { @@ -579,8 +674,10 @@ public static org.hamcrest.Matcher nullValue() { * For example: *
assertThat(cheese, is(nullValue(Cheese.class))
* - * @param the matcher type. - * @param type dummy parameter used to infer the generic type of the returned matcher + * @param + * the matcher type. + * @param type + * dummy parameter used to infer the generic type of the returned matcher * @return The matcher. */ public static org.hamcrest.Matcher nullValue(java.lang.Class type) { @@ -591,8 +688,10 @@ public static org.hamcrest.Matcher nullValue(java.lang.Class type) { * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * - * @param the matcher type. - * @param target the target instance against which others should be assessed + * @param + * the matcher type. + * @param target + * the target instance against which others should be assessed * @return The matcher. */ public static org.hamcrest.Matcher sameInstance(T target) { @@ -603,8 +702,10 @@ public static org.hamcrest.Matcher sameInstance(T target) { * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * - * @param the matcher type. - * @param target the target instance against which others should be assessed + * @param + * the matcher type. + * @param target + * the target instance against which others should be assessed * @return The matcher. */ public static org.hamcrest.Matcher theInstance(T target) { @@ -617,7 +718,8 @@ public static org.hamcrest.Matcher theInstance(T target) { * For example: *
assertThat("myStringOfNote", containsString("ring"))
* - * @param substring the substring that the returned matcher will expect to find within any examined string + * @param substring + * the substring that the returned matcher will expect to find within any examined string * @return The matcher. */ public static Matcher containsString(java.lang.String substring) { @@ -630,7 +732,8 @@ public static Matcher containsString(java.lang.String substrin * For example: *
assertThat("myStringOfNote", containsStringIgnoringCase("Ring"))
* - * @param substring the substring that the returned matcher will expect to find within any examined string + * @param substring + * the substring that the returned matcher will expect to find within any examined string * @return The matcher. */ public static Matcher containsStringIgnoringCase(java.lang.String substring) { @@ -645,7 +748,8 @@ public static Matcher containsStringIgnoringCase(java.lang.Str * For example: *
assertThat("myStringOfNote", startsWith("my"))
* - * @param prefix the substring that the returned matcher will expect at the start of any examined string + * @param prefix + * the substring that the returned matcher will expect at the start of any examined string * @return The matcher. */ public static Matcher startsWith(java.lang.String prefix) { @@ -660,7 +764,8 @@ public static Matcher startsWith(java.lang.String prefix) { * For example: *
assertThat("myStringOfNote", startsWithIgnoringCase("My"))
* - * @param prefix the substring that the returned matcher will expect at the start of any examined string + * @param prefix + * the substring that the returned matcher will expect at the start of any examined string * @return The matcher. */ public static Matcher startsWithIgnoringCase(java.lang.String prefix) { @@ -673,7 +778,8 @@ public static Matcher startsWithIgnoringCase(java.lang.String * For example: *
assertThat("myStringOfNote", endsWith("Note"))
* - * @param suffix the substring that the returned matcher will expect at the end of any examined string + * @param suffix + * the substring that the returned matcher will expect at the end of any examined string * @return The matcher. */ public static Matcher endsWith(java.lang.String suffix) { @@ -686,7 +792,8 @@ public static Matcher endsWith(java.lang.String suffix) { * For example: *
assertThat("myStringOfNote", endsWithIgnoringCase("note"))
* - * @param suffix the substring that the returned matcher will expect at the end of any examined string + * @param suffix + * the substring that the returned matcher will expect at the end of any examined string * @return The matcher. */ public static Matcher endsWithIgnoringCase(java.lang.String suffix) { @@ -700,7 +807,8 @@ public static Matcher endsWithIgnoringCase(java.lang.String su * assertThat("abc", matchesRegex(Pattern.compile("ˆ[a-z]$")); * * - * @param pattern the pattern to be used. + * @param pattern + * the pattern to be used. * @return The matcher. */ public static Matcher matchesRegex(Pattern pattern) { @@ -714,7 +822,8 @@ public static Matcher matchesRegex(Pattern pattern) { * assertThat("abc", matchesRegex("ˆ[a-z]+$")); * * - * @param regex The regex to be used for the validation. + * @param regex + * The regex to be used for the validation. * @return The matcher. */ public static Matcher matchesRegex(String regex) { @@ -728,8 +837,10 @@ public static Matcher matchesRegex(String regex) { * For example: *
assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))
* - * @param the matcher type. - * @param elementMatchers the matchers that the elements of examined arrays should satisfy + * @param + * the matcher type. + * @param elementMatchers + * the matchers that the elements of examined arrays should satisfy * @return The matcher. */ @SafeVarargs @@ -744,8 +855,10 @@ public static org.hamcrest.collection.IsArray array(org.hamcrest.Matcher< * For example: *
assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))
* - * @param the matcher type. - * @param elementMatcher the matcher to apply to elements in examined arrays + * @param + * the matcher type. + * @param elementMatcher + * the matcher to apply to elements in examined arrays * @return The matcher. */ public static org.hamcrest.Matcher hasItemInArray(org.hamcrest.Matcher elementMatcher) { @@ -759,8 +872,10 @@ public static org.hamcrest.Matcher hasItemInArray(org.hamcrest.Matcher< * instead of: *
assertThat(hasItemInArray(equalTo(x)))
* - * @param the matcher type. - * @param element the element that should be present in examined arrays + * @param + * the matcher type. + * @param element + * the element that should be present in examined arrays * @return The matcher. */ public static org.hamcrest.Matcher hasItemInArray(T element) { @@ -774,8 +889,10 @@ public static org.hamcrest.Matcher hasItemInArray(T element) { * For example: *
assertThat(new String[]{"foo", "bar"}, arrayContaining("foo", "bar"))
* - * @param the matcher type. - * @param items the items that must equal the items within an examined array + * @param + * the matcher type. + * @param items + * the items that must equal the items within an examined array * @return The matcher. */ @SafeVarargs @@ -790,8 +907,10 @@ public static org.hamcrest.Matcher arrayContaining(E... items) { * For example: *
assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")))
* - * @param the matcher type. - * @param itemMatchers the matchers that must be satisfied by the items in the examined array + * @param + * the matcher type. + * @param itemMatchers + * the matchers that must be satisfied by the items in the examined array * @return The matcher. */ @SafeVarargs @@ -806,8 +925,10 @@ public static org.hamcrest.Matcher arrayContaining(org.hamcrest.Matcher * For example: *
assertThat(new String[]{"foo", "bar"}, arrayContaining(Arrays.asList(equalTo("foo"), equalTo("bar"))))
* - * @param the matcher type. - * @param itemMatchers a list of matchers, each of which must be satisfied by the corresponding item in an examined array + * @param + * the matcher type. + * @param itemMatchers + * a list of matchers, each of which must be satisfied by the corresponding item in an examined array * @return The matcher. */ public static org.hamcrest.Matcher arrayContaining(java.util.List> itemMatchers) { @@ -831,8 +952,10 @@ public static org.hamcrest.Matcher arrayContaining(java.util.List *
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))
* - * @param the matcher type. - * @param itemMatchers a list of matchers, each of which must be satisfied by an entry in an examined array + * @param + * the matcher type. + * @param itemMatchers + * a list of matchers, each of which must be satisfied by an entry in an examined array * @return The matcher. */ @SafeVarargs @@ -857,8 +980,10 @@ public static org.hamcrest.Matcher arrayContainingInAnyOrder(org.hamcre *

*
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))
* - * @param the matcher type. - * @param itemMatchers a list of matchers, each of which must be satisfied by an item provided by an examined array + * @param + * the matcher type. + * @param itemMatchers + * a list of matchers, each of which must be satisfied by an item provided by an examined array * @return The matcher. */ public static org.hamcrest.Matcher arrayContainingInAnyOrder(java.util.Collection> itemMatchers) { @@ -880,8 +1005,10 @@ public static org.hamcrest.Matcher arrayContainingInAnyOrder(java.util. *

*
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder("bar", "foo"))
* - * @param the matcher type. - * @param items the items that must equal the entries of an examined array, in any order + * @param + * the matcher type. + * @param items + * the items that must equal the entries of an examined array, in any order * @return The matcher. */ @SafeVarargs @@ -895,8 +1022,10 @@ public static org.hamcrest.Matcher arrayContainingInAnyOrder(E... items * For example: *
assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))
* - * @param the matcher type. - * @param sizeMatcher a matcher for the length of an examined array + * @param + * the matcher type. + * @param sizeMatcher + * a matcher for the length of an examined array * @return The matcher. */ public static org.hamcrest.Matcher arrayWithSize(org.hamcrest.Matcher sizeMatcher) { @@ -909,8 +1038,10 @@ public static org.hamcrest.Matcher arrayWithSize(org.hamcrest.MatcherassertThat(new String[]{"foo", "bar"}, arrayWithSize(2)) * - * @param the matcher type. - * @param size the length that an examined array must have for a positive match + * @param + * the matcher type. + * @param size + * the length that an examined array must have for a positive match * @return The matcher. */ public static org.hamcrest.Matcher arrayWithSize(int size) { @@ -923,7 +1054,8 @@ public static org.hamcrest.Matcher arrayWithSize(int size) { * For example: *
assertThat(new String[0], emptyArray())
* - * @param the matcher type. + * @param + * the matcher type. * @return The matcher. */ public static org.hamcrest.Matcher emptyArray() { @@ -936,12 +1068,15 @@ public static org.hamcrest.Matcher emptyArray() { * For example: *
assertThat(myMap, is(aMapWithSize(equalTo(2))))
* - * @param the map key type. - * @param the map value type. - * @param sizeMatcher a matcher for the size of an examined {@link java.util.Map} + * @param + * the map key type. + * @param + * the map value type. + * @param sizeMatcher + * a matcher for the size of an examined {@link java.util.Map} * @return The matcher. */ - public static org.hamcrest.Matcher> aMapWithSize(org.hamcrest.Matcher sizeMatcher) { + public static org.hamcrest.Matcher> aMapWithSize(org.hamcrest.Matcher sizeMatcher) { return org.hamcrest.collection.IsMapWithSize.aMapWithSize(sizeMatcher); } @@ -951,12 +1086,15 @@ public static org.hamcrest.Matcher emptyArray() { * For example: *
assertThat(myMap, is(aMapWithSize(2)))
* - * @param the map key type. - * @param the map value type. - * @param size the expected size of an examined {@link java.util.Map} + * @param + * the map key type. + * @param + * the map value type. + * @param size + * the expected size of an examined {@link java.util.Map} * @return The matcher. */ - public static org.hamcrest.Matcher> aMapWithSize(int size) { + public static org.hamcrest.Matcher> aMapWithSize(int size) { return org.hamcrest.collection.IsMapWithSize.aMapWithSize(size); } @@ -966,11 +1104,13 @@ public static org.hamcrest.Matcher emptyArray() { * For example: *
assertThat(myMap, is(anEmptyMap()))
* - * @param the map key type. - * @param the map value type. + * @param + * the map key type. + * @param + * the map value type. * @return The matcher. */ - public static org.hamcrest.Matcher> anEmptyMap() { + public static org.hamcrest.Matcher> anEmptyMap() { return org.hamcrest.collection.IsMapWithSize.anEmptyMap(); } @@ -980,8 +1120,10 @@ public static org.hamcrest.Matcher emptyArray() { * For example: *
assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))
* - * @param the matcher type. - * @param sizeMatcher a matcher for the size of an examined {@link java.util.Collection} + * @param + * the matcher type. + * @param sizeMatcher + * a matcher for the size of an examined {@link java.util.Collection} * @return The matcher. */ public static org.hamcrest.Matcher> hasSize(org.hamcrest.Matcher sizeMatcher) { @@ -994,8 +1136,10 @@ public static org.hamcrest.Matcher> hasSiz * For example: *
assertThat(Arrays.asList("foo", "bar"), hasSize(2))
* - * @param the matcher type. - * @param size the expected size of an examined {@link java.util.Collection} + * @param + * the matcher type. + * @param size + * the expected size of an examined {@link java.util.Collection} * @return The matcher. */ public static org.hamcrest.Matcher> hasSize(int size) { @@ -1008,7 +1152,8 @@ public static org.hamcrest.Matcher> hasSiz * For example: *
assertThat(new ArrayList<String>(), is(empty()))
* - * @param the matcher type. + * @param + * the matcher type. * @return The matcher. */ public static org.hamcrest.Matcher> empty() { @@ -1021,8 +1166,10 @@ public static org.hamcrest.Matcher> empty( * For example: *
assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)))
* - * @param the matcher type. - * @param unusedToForceReturnType the type of the collection's content + * @param + * the matcher type. + * @param unusedToForceReturnType + * the type of the collection's content * @return The matcher. */ public static org.hamcrest.Matcher> emptyCollectionOf(java.lang.Class unusedToForceReturnType) { @@ -1034,7 +1181,8 @@ public static org.hamcrest.Matcher> emptyCollectionO * For example: *
assertThat(new ArrayList<String>(), is(emptyIterable()))
* - * @param the matcher type. + * @param + * the matcher type. * @return The matcher. */ public static org.hamcrest.Matcher> emptyIterable() { @@ -1046,8 +1194,10 @@ public static org.hamcrest.Matcher> emptyIte * For example: *
assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)))
* - * @param the matcher type. - * @param unusedToForceReturnType the type of the iterable's content + * @param + * the matcher type. + * @param unusedToForceReturnType + * the type of the iterable's content * @return The matcher. */ public static org.hamcrest.Matcher> emptyIterableOf(java.lang.Class unusedToForceReturnType) { @@ -1062,8 +1212,10 @@ public static org.hamcrest.Matcher> emptyIterableOf(ja * For example: *
assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))
* - * @param the matcher type. - * @param items the items that must equal the items provided by an examined {@link Iterable} + * @param + * the matcher type. + * @param items + * the items that must equal the items provided by an examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -1078,9 +1230,11 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo"), contains(equalTo("foo")))
* - * @param the matcher type. - * @param itemMatcher the matcher that must be satisfied by the single item provided by an - * examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatcher + * the matcher that must be satisfied by the single item provided by an + * examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> contains(org.hamcrest.Matcher itemMatcher) { @@ -1095,8 +1249,10 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))
* - * @param the matcher type. - * @param itemMatchers the matchers that must be satisfied by the items provided by an examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatchers + * the matchers that must be satisfied by the items provided by an examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -1112,9 +1268,11 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))
* - * @param the matcher type. - * @param itemMatchers a list of matchers, each of which must be satisfied by the corresponding item provided by - * an examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatchers + * a list of matchers, each of which must be satisfied by the corresponding item provided by + * an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> contains(java.util.List> itemMatchers) { @@ -1138,8 +1296,10 @@ public static org.hamcrest.Matcher> contains *

*
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(equalTo("bar"), equalTo("foo")))
* - * @param the matcher type. - * @param itemMatchers a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatchers + * a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} * @return The matcher. */ @SafeVarargs @@ -1164,8 +1324,10 @@ public static org.hamcrest.Matcher> contains *

*
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"))
* - * @param the matcher type. - * @param items the items that must equal the items provided by an examined {@link Iterable} in any order + * @param + * the matcher type. + * @param items + * the items that must equal the items provided by an examined {@link Iterable} in any order * @return The matcher. */ @SafeVarargs @@ -1188,8 +1350,10 @@ public static org.hamcrest.Matcher> contains *

For example:

*
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))
* - * @param the matcher type. - * @param itemMatchers a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} + * @param + * the matcher type. + * @param itemMatchers + * a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> containsInAnyOrder(java.util.Collection> itemMatchers) { @@ -1203,8 +1367,10 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder("b", "d"))
* - * @param the matcher type. - * @param items the items that must be contained within items provided by an examined {@link Iterable} in the same relative order + * @param + * the matcher type. + * @param items + * the items that must be contained within items provided by an examined {@link Iterable} in the same relative order * @return The matcher. */ @SafeVarargs @@ -1219,8 +1385,10 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder(equalTo("b"), equalTo("d")))
* - * @param the matcher type. - * @param itemMatchers the matchers that must be satisfied by the items provided by an examined {@link Iterable} in the same relative order + * @param + * the matcher type. + * @param itemMatchers + * the matchers that must be satisfied by the items provided by an examined {@link Iterable} in the same relative order * @return The matcher. */ @SafeVarargs @@ -1235,9 +1403,11 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("a", "b", "c", "d", "e"), contains(Arrays.asList(equalTo("b"), equalTo("d"))))
* - * @param the matcher type. - * @param itemMatchers a list of matchers, each of which must be satisfied by the items provided by - * an examined {@link Iterable} in the same relative order + * @param + * the matcher type. + * @param itemMatchers + * a list of matchers, each of which must be satisfied by the items provided by + * an examined {@link Iterable} in the same relative order * @return The matcher. */ public static org.hamcrest.Matcher> containsInRelativeOrder(java.util.List> itemMatchers) { @@ -1251,8 +1421,10 @@ public static org.hamcrest.Matcher> contains * For example: *
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)))
* - * @param the matcher type. - * @param sizeMatcher a matcher for the number of items that should be yielded by an examined {@link Iterable} + * @param + * the matcher type. + * @param sizeMatcher + * a matcher for the number of items that should be yielded by an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> iterableWithSize(org.hamcrest.Matcher sizeMatcher) { @@ -1266,8 +1438,10 @@ public static org.hamcrest.Matcher> iterableWithSize(o * For example: *
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2))
* - * @param the matcher type. - * @param size the number of items that should be yielded by an examined {@link Iterable} + * @param + * the matcher type. + * @param size + * the number of items that should be yielded by an examined {@link Iterable} * @return The matcher. */ public static org.hamcrest.Matcher> iterableWithSize(int size) { @@ -1281,13 +1455,17 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))
* - * @param the map key type. - * @param the map value type. - * @param keyMatcher the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry - * @param valueMatcher the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry + * @param + * the map key type. + * @param + * the map value type. + * @param keyMatcher + * the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry + * @param valueMatcher + * the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry * @return The matcher. */ - public static org.hamcrest.Matcher> hasEntry(org.hamcrest.Matcher keyMatcher, org.hamcrest.Matcher valueMatcher) { + public static org.hamcrest.Matcher> hasEntry(org.hamcrest.Matcher keyMatcher, org.hamcrest.Matcher valueMatcher) { return org.hamcrest.collection.IsMapContaining.hasEntry(keyMatcher, valueMatcher); } @@ -1298,13 +1476,17 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat(myMap, hasEntry("bar", "foo"))
* - * @param the map key type. - * @param the map value type. - * @param key the key that, in combination with the value, must be describe at least one entry - * @param value the value that, in combination with the key, must be describe at least one entry + * @param + * the map key type. + * @param + * the map value type. + * @param key + * the key that, in combination with the value, must be describe at least one entry + * @param value + * the value that, in combination with the key, must be describe at least one entry * @return The matcher. */ - public static org.hamcrest.Matcher> hasEntry(K key, V value) { + public static org.hamcrest.Matcher> hasEntry(K key, V value) { return org.hamcrest.collection.IsMapContaining.hasEntry(key, value); } @@ -1314,11 +1496,13 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat(myMap, hasKey(equalTo("bar")))
* - * @param the map key type. - * @param keyMatcher the matcher that must be satisfied by at least one key + * @param + * the map key type. + * @param keyMatcher + * the matcher that must be satisfied by at least one key * @return The matcher. */ - public static org.hamcrest.Matcher> hasKey(org.hamcrest.Matcher keyMatcher) { + public static org.hamcrest.Matcher> hasKey(org.hamcrest.Matcher keyMatcher) { return org.hamcrest.collection.IsMapContaining.hasKey(keyMatcher); } @@ -1328,11 +1512,13 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat(myMap, hasKey("bar"))
* - * @param the map key type. - * @param key the key that satisfying maps must contain + * @param + * the map key type. + * @param key + * the key that satisfying maps must contain * @return The matcher. */ - public static org.hamcrest.Matcher> hasKey(K key) { + public static org.hamcrest.Matcher> hasKey(K key) { return org.hamcrest.collection.IsMapContaining.hasKey(key); } @@ -1342,11 +1528,13 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat(myMap, hasValue(equalTo("foo")))
* - * @param the value type. - * @param valueMatcher the matcher that must be satisfied by at least one value + * @param + * the value type. + * @param valueMatcher + * the matcher that must be satisfied by at least one value * @return The matcher. */ - public static org.hamcrest.Matcher> hasValue(org.hamcrest.Matcher valueMatcher) { + public static org.hamcrest.Matcher> hasValue(org.hamcrest.Matcher valueMatcher) { return org.hamcrest.collection.IsMapContaining.hasValue(valueMatcher); } @@ -1356,11 +1544,13 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat(myMap, hasValue("foo"))
* - * @param the value type. - * @param value the value that satisfying maps must contain + * @param + * the value type. + * @param value + * the value that satisfying maps must contain * @return The matcher. */ - public static org.hamcrest.Matcher> hasValue(V value) { + public static org.hamcrest.Matcher> hasValue(V value) { return org.hamcrest.collection.IsMapContaining.hasValue(value); } @@ -1370,8 +1560,10 @@ public static org.hamcrest.Matcher> iterableWithSize(i * For example: *
assertThat("foo", is(in(Arrays.asList("bar", "foo"))))
* - * @param the matcher type. - * @param collection the collection in which matching items must be found + * @param + * the matcher type. + * @param collection + * the collection in which matching items must be found * @return The matcher. */ public static org.hamcrest.Matcher in(java.util.Collection collection) { @@ -1384,8 +1576,10 @@ public static org.hamcrest.Matcher in(java.util.Collection collection) * For example: *
assertThat("foo", is(in(new String[]{"bar", "foo"})))
* - * @param the matcher type. - * @param elements the array in which matching items must be found + * @param + * the matcher type. + * @param elements + * the array in which matching items must be found * @return The matcher. */ public static org.hamcrest.Matcher in(T[] elements) { @@ -1398,10 +1592,12 @@ public static org.hamcrest.Matcher in(T[] elements) { * For example: *
assertThat("foo", isIn(Arrays.asList("bar", "foo")))
* - * @param the matcher type. - * @param collection the collection in which matching items must be found - * @return The matcher. + * @param + * the matcher type. * @deprecated use is(in(...)) instead + * @param collection + * the collection in which matching items must be found + * @return The matcher. */ @SuppressWarnings("deprecation") public static org.hamcrest.Matcher isIn(java.util.Collection collection) { @@ -1414,10 +1610,12 @@ public static org.hamcrest.Matcher isIn(java.util.Collection collectio * For example: *
assertThat("foo", isIn(new String[]{"bar", "foo"}))
* - * @param the matcher type. - * @param elements the array in which matching items must be found - * @return The matcher. * @deprecated use is(in(...)) instead + * @param + * the matcher type. + * @param elements + * the array in which matching items must be found + * @return The matcher. */ @SuppressWarnings("deprecation") public static org.hamcrest.Matcher isIn(T[] elements) { @@ -1430,10 +1628,12 @@ public static org.hamcrest.Matcher isIn(T[] elements) { * For example: *
assertThat("foo", isOneOf("bar", "foo"))
* - * @param the matcher type. - * @param elements the elements amongst which matching items will be found - * @return The matcher. * @deprecated use is(oneOf(...)) instead + * @param + * the matcher type. + * @param elements + * the elements amongst which matching items will be found + * @return The matcher. */ @SuppressWarnings("deprecation") @SafeVarargs @@ -1447,8 +1647,10 @@ public static org.hamcrest.Matcher isOneOf(T... elements) { * For example: *
assertThat("foo", is(oneOf("bar", "foo")))
* - * @param the matcher type. - * @param elements the elements amongst which matching items will be found + * @param + * the matcher type. + * @param elements + * the elements amongst which matching items will be found * @return The matcher. */ @SafeVarargs @@ -1462,8 +1664,10 @@ public static org.hamcrest.Matcher oneOf(T... elements) { * For example: *
assertThat(1.03, is(closeTo(1.0, 0.03)))
* - * @param operand the expected value of matching doubles - * @param error the delta (+/-) within which matches will be allowed + * @param operand + * the expected value of matching doubles + * @param error + * the delta (+/-) within which matches will be allowed * @return The matcher. */ public static org.hamcrest.Matcher closeTo(double operand, double error) { @@ -1488,8 +1692,10 @@ public static org.hamcrest.Matcher notANumber() { * For example: *
assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))
* - * @param operand the expected value of matching BigDecimals - * @param error the delta (+/-) within which matches will be allowed + * @param operand + * the expected value of matching BigDecimals + * @param error + * the delta (+/-) within which matches will be allowed * @return The matcher. */ public static org.hamcrest.Matcher closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) { @@ -1503,7 +1709,8 @@ public static org.hamcrest.Matcher closeTo(java.math.BigDe * For example: *
assertThat(1, comparesEqualTo(1))
* - * @param the matcher type. + * @param + * the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return zero * @return The matcher. */ @@ -1518,7 +1725,8 @@ public static > org.hamcrest.Matcher compar * For example: *
assertThat(2, greaterThan(1))
* - * @param the matcher type. + * @param + * the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return greater * than zero * @return The matcher. @@ -1534,7 +1742,8 @@ public static > org.hamcrest.Matcher greate * For example: *
assertThat(1, greaterThanOrEqualTo(1))
* - * @param the matcher type. + * @param + * the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return greater * than or equal to zero * @return The matcher. @@ -1550,7 +1759,8 @@ public static > org.hamcrest.Matcher greate * For example: *
assertThat(1, lessThan(2))
* - * @param the matcher type. + * @param + * the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return less * than zero * @return The matcher. @@ -1566,7 +1776,8 @@ public static > org.hamcrest.Matcher lessTh * For example: *
assertThat(1, lessThanOrEqualTo(1))
* - * @param the matcher type. + * @param + * the matcher type. * @param value the value which, when passed to the compareTo method of the examined object, should return less * than or equal to zero * @return The matcher. @@ -1581,7 +1792,8 @@ public static > org.hamcrest.Matcher lessTh * For example: *
assertThat("Foo", equalToIgnoringCase("FOO"))
* - * @param expectedString the expected value of matched strings + * @param expectedString + * the expected value of matched strings * @return The matcher. */ public static Matcher equalToIgnoringCase(java.lang.String expectedString) { @@ -1589,9 +1801,10 @@ public static Matcher equalToIgnoringCase(java.lang.String exp } /** - * @param expectedString the expected value of matched strings - * @return The matcher. * @deprecated {@link #equalToCompressingWhiteSpace(String)} + * @param expectedString + * the expected value of matched strings + * @return The matcher. */ public static Matcher equalToIgnoringWhiteSpace(java.lang.String expectedString) { return equalToCompressingWhiteSpace(expectedString); @@ -1608,7 +1821,8 @@ public static Matcher equalToIgnoringWhiteSpace(java.lang.Stri * For example: *
assertThat("   my\tfoo  bar ", equalToIgnoringWhiteSpace(" my  foo bar"))
* - * @param expectedString the expected value of matched strings + * @param expectedString + * the expected value of matched strings * @return The matcher. */ public static Matcher equalToCompressingWhiteSpace(java.lang.String expectedString) { @@ -1644,8 +1858,8 @@ public static Matcher emptyString() { * For example: *
assertThat(((String)null), isEmptyOrNullString())
* - * @return The matcher. * @deprecated use is(emptyOrNullString()) instead + * @return The matcher. */ @SuppressWarnings("deprecation") public static Matcher isEmptyOrNullString() { @@ -1657,8 +1871,8 @@ public static Matcher isEmptyOrNullString() { * For example: *
assertThat("", isEmptyString())
* - * @return The matcher. * @deprecated use is(emptyString()) instead + * @return The matcher. */ @SuppressWarnings("deprecation") public static Matcher isEmptyString() { @@ -1693,7 +1907,8 @@ public static Matcher blankString() { * Creates a matcher of {@link java.lang.String} that matches when the examined string * exactly matches the given {@link java.util.regex.Pattern}. * - * @param pattern the text pattern to match. + * @param pattern + * the text pattern to match. * @return The matcher. */ public static Matcher matchesPattern(java.util.regex.Pattern pattern) { @@ -1704,7 +1919,8 @@ public static Matcher matchesPattern(java.util.regex.Pattern p * Creates a matcher of {@link java.lang.String} that matches when the examined string * exactly matches the given regular expression, treated as a {@link java.util.regex.Pattern}. * - * @param regex the regex to match. + * @param regex + * the regex to match. * @return The matcher. */ public static Matcher matchesPattern(java.lang.String regex) { @@ -1718,7 +1934,8 @@ public static Matcher matchesPattern(java.lang.String regex) { *
assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")))
* fails as "foo" occurs before "bar" in the string "myfoobarbaz" * - * @param substrings the substrings that must be contained within matching strings + * @param substrings + * the substrings that must be contained within matching strings * @return The matcher. */ public static Matcher stringContainsInOrder(java.lang.Iterable substrings) { @@ -1732,7 +1949,8 @@ public static Matcher stringContainsInOrder(java.lang.Iterable *
assertThat("myfoobarbaz", stringContainsInOrder("bar", "foo"))
* fails as "foo" occurs before "bar" in the string "myfoobarbaz" * - * @param substrings the substrings that must be contained within matching strings + * @param substrings + * the substrings that must be contained within matching strings * @return The matcher. */ public static Matcher stringContainsInOrder(java.lang.String... substrings) { @@ -1777,8 +1995,10 @@ public static Matcher hasLength(int length) { * For example: *
assertThat(true, hasToString(equalTo("TRUE")))
* - * @param the matcher type. - * @param toStringMatcher the matcher used to verify the toString result + * @param + * the matcher type. + * @param toStringMatcher + * the matcher used to verify the toString result * @return The matcher. */ public static org.hamcrest.Matcher hasToString(org.hamcrest.Matcher toStringMatcher) { @@ -1791,8 +2011,10 @@ public static org.hamcrest.Matcher hasToString(org.hamcrest.MatcherassertThat(true, hasToString("TRUE")) * - * @param the matcher type. - * @param expectedToString the expected toString result + * @param + * the matcher type. + * @param expectedToString + * the expected toString result * @return The matcher. */ public static org.hamcrest.Matcher hasToString(java.lang.String expectedToString) { @@ -1805,8 +2027,10 @@ public static org.hamcrest.Matcher hasToString(java.lang.String expectedT * For example: *
assertThat(Integer.class, typeCompatibleWith(Number.class))
* - * @param the matcher type. - * @param baseType the base class to examine classes against + * @param + * the matcher type. + * @param baseType + * the base class to examine classes against * @return The matcher. */ public static org.hamcrest.Matcher> typeCompatibleWith(java.lang.Class baseType) { @@ -1819,8 +2043,10 @@ public static org.hamcrest.Matcher> typeCompatibleWith(ja * For example: *
assertThat(myEvent, is(eventFrom(PropertyChangeEvent.class, myBean)))
* - * @param eventClass the class of the event to match on - * @param source the source of the event + * @param eventClass + * the class of the event to match on + * @param source + * the source of the event * @return The matcher. */ public static org.hamcrest.Matcher eventFrom(java.lang.Class eventClass, java.lang.Object source) { @@ -1833,7 +2059,8 @@ public static org.hamcrest.Matcher eventFrom(java.lang.Cl * For example: *
assertThat(myEvent, is(eventFrom(myBean)))
* - * @param source the source of the event + * @param source + * the source of the event * @return The matcher. */ public static org.hamcrest.Matcher eventFrom(java.lang.Object source) { @@ -1846,8 +2073,10 @@ public static org.hamcrest.Matcher eventFrom(java.lang.Ob * For example: *
assertThat(myBean, hasProperty("foo"))
* - * @param the matcher type. - * @param propertyName the name of the JavaBean property that examined beans should possess + * @param + * the matcher type. + * @param propertyName + * the name of the JavaBean property that examined beans should possess * @return The matcher. */ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyName) { @@ -1860,9 +2089,12 @@ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyN * For example: *
assertThat(myBean, hasProperty("foo", equalTo("bar"))
* - * @param the matcher type. - * @param propertyName the name of the JavaBean property that examined beans should possess - * @param valueMatcher a matcher for the value of the specified property of the examined bean + * @param + * the matcher type. + * @param propertyName + * the name of the JavaBean property that examined beans should possess + * @param valueMatcher + * a matcher for the value of the specified property of the examined bean * @return The matcher. */ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyName, org.hamcrest.Matcher valueMatcher) { @@ -1879,9 +2111,12 @@ public static org.hamcrest.Matcher hasProperty(java.lang.String propertyN *
assertThat(myBean, samePropertyValuesAs(myExpectedBean))
*
assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")
* - * @param the matcher type. - * @param expectedBean the bean against which examined beans are compared - * @param ignoredProperties do not check any of these named properties. + * @param + * the matcher type. + * @param expectedBean + * the bean against which examined beans are compared + * @param ignoredProperties + * do not check any of these named properties. * @return The matcher. */ public static Matcher samePropertyValuesAs(B expectedBean, String... ignoredProperties) { @@ -1894,8 +2129,10 @@ public static Matcher samePropertyValuesAs(B expectedBean, String... igno * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))
* - * @param xPath the target xpath - * @param valueMatcher matcher for the value at the specified xpath + * @param xPath + * the target xpath + * @param valueMatcher + * matcher for the value at the specified xpath * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, Matcher valueMatcher) { @@ -1909,9 +2146,12 @@ public static org.hamcrest.Matcher hasXPath(java.lang.String x * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))
* - * @param xPath the target xpath - * @param namespaceContext the namespace for matching nodes - * @param valueMatcher matcher for the value at the specified xpath + * @param xPath + * the target xpath + * @param namespaceContext + * the namespace for matching nodes + * @param valueMatcher + * matcher for the value at the specified xpath * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher valueMatcher) { @@ -1924,7 +2164,8 @@ public static org.hamcrest.Matcher hasXPath(java.lang.String x * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese"))
* - * @param xPath the target xpath + * @param xPath + * the target xpath * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath) { @@ -1937,8 +2178,10 @@ public static org.hamcrest.Matcher hasXPath(java.lang.String x * For example: *
assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))
* - * @param xPath the target xpath - * @param namespaceContext the namespace for matching nodes + * @param xPath + * the target xpath + * @param namespaceContext + * the namespace for matching nodes * @return The matcher. */ public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext) { @@ -1968,7 +2211,7 @@ public static Matcher> optionalWithValue() { /** * Matcher for {@link Optional} that expects that value is present and is equal to value * - * @param type of optional value + * @param type of optional value * @param value to validate present optional value * @return The matcher. */ @@ -1979,7 +2222,7 @@ public static Matcher> optionalWithValue(T value) { /** * Matcher for {@link Optional} that expects that value is present and matches matcher * - * @param type of optional value + * @param type of optional value * @param matcher matcher to validate present optional value * @return The matcher. */