Skip to content

Commit

Permalink
(llorllale#119) Throws matcher now takes a message matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed May 30, 2019
1 parent 7fff8ac commit 4a4f7b8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
62 changes: 37 additions & 25 deletions src/main/java/org/llorllale/cactoos/matchers/Throws.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.Scalar;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.core.IsAnything;
import org.hamcrest.core.IsEqual;

/**
* Matcher to check that scalar throw the expected exception.
Expand All @@ -49,32 +50,57 @@
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class Throws<T> extends TypeSafeDiagnosingMatcher<Scalar<T>> {

/**
* The expected exception message.
*/
private final String msg;
private final Matcher<String> msg;

/**
* The expected exception type.
*/
private final Class<? extends Exception> type;

/**
* Ctor.
* @param type The expected exception type.
*/
public Throws(final Class<? extends Exception> type) {
this(new IsAnything<>(), type);
}

/**
* Ctor.
* @param msg The expected exception message.
* @param type The expected exception type.
*/
public Throws(final String msg, final Class<? extends Exception> type) {
this(new IsEqual<>(msg), type);
}

/**
* Ctor.
* @param msg The expected exception message.
* @param type The expected exception type.
*/
public Throws(
final Matcher<String> msg,
final Class<? extends Exception> type
) {
super();
this.msg = msg;
this.type = type;
}

@Override
public void describeTo(final Description dsc) {
describe(dsc, this.type, this.msg);
dsc
.appendText("Exception has type '")
.appendText(this.type.getName())
.appendText("' and message matches ")
.appendDescriptionOf(this.msg);
}

@Override
Expand All @@ -88,33 +114,19 @@ protected boolean matchesSafely(final Scalar<T> obj,
matches = false;
dsc.appendText("The exception wasn't thrown.");
} catch (final Exception cause) {
describe(dsc, cause.getClass(), cause.getMessage());
dsc
.appendText("Exception has type '")
.appendText(cause.getClass().getName())
.appendText("' and message '")
.appendText(cause.getMessage())
.appendText("'");
if (this.type.isAssignableFrom(cause.getClass())
&& this.msg.equals(cause.getMessage())) {
&& this.msg.matches(cause.getMessage())) {
matches = true;
} else {
matches = false;
}
}
return matches;
}

/**
* Add information about the exception to the hamcrest result message.
*
* @param dsc The description of the object.
* @param type The exception type.
* @param msg The exception message.
*/
private static void describe(final Description dsc, final Class<?> type,
final String msg) {
dsc.appendText(
new UncheckedText(
new FormattedText(
"Exception has type '%s' and message '%s'",
type.getName(), msg
)
).asString()
);
}
}
29 changes: 28 additions & 1 deletion src/test/java/org/llorllale/cactoos/matchers/ThrowsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,34 @@ public void describeExpectedValues() {
description.toString(),
new IsEqual<>(
"Exception has type 'java.lang.NullPointerException'"
+ " and message 'NPE'"
+ " and message matches \"NPE\""
)
).affirm();
}

@Test
public void noMessageMatchesException() {
new Assertion<>(
"must match if message is not present",
new Throws<>(IllegalArgumentException.class),
new Matches<>(
() -> {
throw new IllegalArgumentException("No object(s) found.");
}
)
).affirm();
}

@Test
public void describeExpectedValuesNoMessage() {
final Description description = new StringDescription();
new Throws<>(NullPointerException.class).describeTo(description);
new Assertion<>(
"describes the expected exception",
description.toString(),
new IsEqual<>(
"Exception has type 'java.lang.NullPointerException'"
+ " and message matches ANYTHING"
)
).affirm();
}
Expand Down

0 comments on commit 4a4f7b8

Please sign in to comment.