diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRules.java index a1c571ee21b..74bd22df350 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRules.java @@ -319,6 +319,44 @@ static final class AssertThatThrownByNullPointerExceptionHasMessageStartingWith } } + static final class AssertThatThrownByNullPointerExceptionHasMessageContaining { + @BeforeTemplate + @SuppressWarnings( + "AssertThatThrownByNullPointerException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatNullPointerException() + .isThrownBy(throwingCallable) + .withMessageContaining(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(NullPointerException.class) + .hasMessageContaining(message); + } + } + + static final class AssertThatThrownByNullPointerExceptionHasMessageNotContaining { + @BeforeTemplate + @SuppressWarnings( + "AssertThatThrownByNullPointerException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatNullPointerException() + .isThrownBy(throwingCallable) + .withMessageNotContaining(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(NullPointerException.class) + .hasMessageNotContaining(message); + } + } + static final class AssertThatThrownByIOException { @BeforeTemplate AbstractObjectAssert before(ThrowingCallable throwingCallable) { @@ -366,6 +404,54 @@ static final class AssertThatThrownByIOExceptionHasMessageParameters { } } + static final class AssertThatThrownByIOExceptionHasMessageStartingWith { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownByIOException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatIOException().isThrownBy(throwingCallable).withMessageStartingWith(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(IOException.class) + .hasMessageStartingWith(message); + } + } + + static final class AssertThatThrownByIOExceptionHasMessageContaining { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownByIOException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatIOException().isThrownBy(throwingCallable).withMessageContaining(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(IOException.class) + .hasMessageContaining(message); + } + } + + static final class AssertThatThrownByIOExceptionHasMessageNotContaining { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownByIOException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatIOException().isThrownBy(throwingCallable).withMessageNotContaining(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(IOException.class) + .hasMessageNotContaining(message); + } + } + static final class AssertThatThrownBy { @BeforeTemplate AbstractObjectAssert before( @@ -429,6 +515,78 @@ static final class AssertThatThrownByHasMessageParameters { } } + static final class AssertThatThrownByHasMessageStartingWith { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownBy" /* This is a more specific template. */) + AbstractObjectAssert before( + Class exceptionType, + ThrowingCallable throwingCallable, + String message) { + return assertThatExceptionOfType(exceptionType) + .isThrownBy(throwingCallable) + .withMessageStartingWith(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after( + Class exceptionType, + ThrowingCallable throwingCallable, + String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(exceptionType) + .hasMessageStartingWith(message); + } + } + + static final class AssertThatThrownByHasMessageContaining { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownBy" /* This is a more specific template. */) + AbstractObjectAssert before( + Class exceptionType, + ThrowingCallable throwingCallable, + String message) { + return assertThatExceptionOfType(exceptionType) + .isThrownBy(throwingCallable) + .withMessageContaining(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after( + Class exceptionType, + ThrowingCallable throwingCallable, + String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(exceptionType) + .hasMessageContaining(message); + } + } + + static final class AssertThatThrownByHasMessageNotContaining { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownBy" /* This is a more specific template. */) + AbstractObjectAssert before( + Class exceptionType, + ThrowingCallable throwingCallable, + String message) { + return assertThatExceptionOfType(exceptionType) + .isThrownBy(throwingCallable) + .withMessageNotContaining(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after( + Class exceptionType, + ThrowingCallable throwingCallable, + String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(exceptionType) + .hasMessageNotContaining(message); + } + } + // XXX: Drop this rule in favour of a generic Error Prone check that flags `String.format(...)` // arguments to a wide range of format methods. static final class AbstractThrowableAssertHasMessage { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestInput.java index a6fb1b2bec0..e34dc55611b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestInput.java @@ -91,6 +91,14 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatNullPointerException().isThrownBy(() -> {}).withMessageStartingWith("foo"); } + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionHasMessageContaining() { + return assertThatNullPointerException().isThrownBy(() -> {}).withMessageContaining("foo"); + } + + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionHasMessageNotContaining() { + return assertThatNullPointerException().isThrownBy(() -> {}).withMessageNotContaining("foo"); + } + AbstractObjectAssert testAssertThatThrownByIOException() { return assertThatIOException().isThrownBy(() -> {}); } @@ -103,6 +111,18 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatIOException().isThrownBy(() -> {}).withMessage("foo %s", "bar"); } + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageStartingWith() { + return assertThatIOException().isThrownBy(() -> {}).withMessageStartingWith("foo"); + } + + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageContaining() { + return assertThatIOException().isThrownBy(() -> {}).withMessageContaining("foo"); + } + + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageNotContaining() { + return assertThatIOException().isThrownBy(() -> {}).withMessageNotContaining("foo"); + } + AbstractObjectAssert testAssertThatThrownBy() { return assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {}); } @@ -119,6 +139,24 @@ public ImmutableSet elidedTypesAndStaticImports() { .withMessage("foo %s", "bar"); } + AbstractObjectAssert testAssertThatThrownByHasMessageStartingWith() { + return assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> {}) + .withMessageStartingWith("foo"); + } + + AbstractObjectAssert testAssertThatThrownByHasMessageContaining() { + return assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> {}) + .withMessageContaining("foo"); + } + + AbstractObjectAssert testAssertThatThrownByHasMessageNotContaining() { + return assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> {}) + .withMessageNotContaining("foo"); + } + ImmutableSet> testAbstractThrowableAssertHasMessage() { return ImmutableSet.of( diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestOutput.java index e4c70ca0611..bb2a99ca826 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRulesTestOutput.java @@ -112,6 +112,18 @@ public ImmutableSet elidedTypesAndStaticImports() { .hasMessageStartingWith("foo"); } + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionHasMessageContaining() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(NullPointerException.class) + .hasMessageContaining("foo"); + } + + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionHasMessageNotContaining() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(NullPointerException.class) + .hasMessageNotContaining("foo"); + } + AbstractObjectAssert testAssertThatThrownByIOException() { return assertThatThrownBy(() -> {}).isInstanceOf(IOException.class); } @@ -124,6 +136,22 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatThrownBy(() -> {}).isInstanceOf(IOException.class).hasMessage("foo %s", "bar"); } + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageStartingWith() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IOException.class) + .hasMessageStartingWith("foo"); + } + + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageContaining() { + return assertThatThrownBy(() -> {}).isInstanceOf(IOException.class).hasMessageContaining("foo"); + } + + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageNotContaining() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IOException.class) + .hasMessageNotContaining("foo"); + } + AbstractObjectAssert testAssertThatThrownBy() { return assertThatThrownBy(() -> {}).isInstanceOf(IllegalArgumentException.class); } @@ -140,6 +168,24 @@ public ImmutableSet elidedTypesAndStaticImports() { .hasMessage("foo %s", "bar"); } + AbstractObjectAssert testAssertThatThrownByHasMessageStartingWith() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageStartingWith("foo"); + } + + AbstractObjectAssert testAssertThatThrownByHasMessageContaining() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("foo"); + } + + AbstractObjectAssert testAssertThatThrownByHasMessageNotContaining() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageNotContaining("foo"); + } + ImmutableSet> testAbstractThrowableAssertHasMessage() { return ImmutableSet.of(