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 0a653c520a..0ececed012 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 @@ -61,6 +61,27 @@ static final class AssertThatThrownByIllegalArgumentExceptionHasMessage { } } + static final class AssertThatThrownByIllegalArgumentExceptionRootCauseHasMessage { + @BeforeTemplate + @SuppressWarnings( + "AssertThatThrownByIllegalArgumentException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatIllegalArgumentException() + .isThrownBy(throwingCallable) + .havingRootCause() + .withMessage(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(IllegalArgumentException.class) + .rootCause() + .hasMessage(message); + } + } + static final class AssertThatThrownByIllegalArgumentExceptionHasMessageParameters { @BeforeTemplate @SuppressWarnings( @@ -171,6 +192,27 @@ static final class AssertThatThrownByIllegalStateExceptionHasMessage { } } + static final class AssertThatThrownByIllegalStateExceptionRootCauseHasMessage { + @BeforeTemplate + @SuppressWarnings( + "AssertThatThrownByIllegalStateException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatIllegalStateException() + .isThrownBy(throwingCallable) + .havingRootCause() + .withMessage(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(IllegalStateException.class) + .rootCause() + .hasMessage(message); + } + } + static final class AssertThatThrownByIllegalStateExceptionHasMessageParameters { @BeforeTemplate @SuppressWarnings( @@ -279,6 +321,27 @@ static final class AssertThatThrownByNullPointerExceptionHasMessage { } } + static final class AssertThatThrownByNullPointerExceptionRootCauseHasMessage { + @BeforeTemplate + @SuppressWarnings( + "AssertThatThrownByNullPointerException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatNullPointerException() + .isThrownBy(throwingCallable) + .havingRootCause() + .withMessage(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(NullPointerException.class) + .rootCause() + .hasMessage(message); + } + } + static final class AssertThatThrownByNullPointerExceptionHasMessageParameters { @BeforeTemplate @SuppressWarnings( @@ -386,6 +449,26 @@ static final class AssertThatThrownByIOExceptionHasMessage { } } + static final class AssertThatThrownByIOExceptionRootCauseHasMessage { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownByIOException" /* This is a more specific template. */) + AbstractObjectAssert before(ThrowingCallable throwingCallable, String message) { + return assertThatIOException() + .isThrownBy(throwingCallable) + .havingRootCause() + .withMessage(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after(ThrowingCallable throwingCallable, String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(IOException.class) + .rootCause() + .hasMessage(message); + } + } + static final class AssertThatThrownByIOExceptionHasMessageParameters { @BeforeTemplate @SuppressWarnings("AssertThatThrownByIOException" /* This is a more specific template. */) @@ -489,6 +572,32 @@ static final class AssertThatThrownByHasMessage { } } + static final class AssertThatThrownByRootCauseHasMessage { + @BeforeTemplate + @SuppressWarnings("AssertThatThrownBy" /* This is a more specific template. */) + AbstractObjectAssert before( + ThrowingCallable throwingCallable, + Class exceptionType, + String message) { + return assertThatExceptionOfType(exceptionType) + .isThrownBy(throwingCallable) + .havingRootCause() + .withMessage(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractObjectAssert after( + ThrowingCallable throwingCallable, + Class exceptionType, + String message) { + return assertThatThrownBy(throwingCallable) + .isInstanceOf(exceptionType) + .rootCause() + .hasMessage(message); + } + } + static final class AssertThatThrownByHasMessageParameters { @BeforeTemplate @SuppressWarnings("AssertThatThrownBy" /* This is a more specific template. */) 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 e34dc55611..317e5e1826 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 @@ -31,6 +31,13 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatIllegalArgumentException().isThrownBy(() -> {}).withMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByIllegalArgumentExceptionRootCauseHasMessage() { + return assertThatIllegalArgumentException() + .isThrownBy(() -> {}) + .havingRootCause() + .withMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByIllegalArgumentExceptionHasMessageParameters() { return assertThatIllegalArgumentException().isThrownBy(() -> {}).withMessage("foo %s", "bar"); } @@ -59,6 +66,13 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatIllegalStateException().isThrownBy(() -> {}).withMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByIllegalStateExceptionRootCauseHasMessage() { + return assertThatIllegalStateException() + .isThrownBy(() -> {}) + .havingRootCause() + .withMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByIllegalStateExceptionHasMessageParameters() { return assertThatIllegalStateException().isThrownBy(() -> {}).withMessage("foo %s", "bar"); } @@ -83,6 +97,13 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatNullPointerException().isThrownBy(() -> {}).withMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionRootCauseHasMessage() { + return assertThatNullPointerException() + .isThrownBy(() -> {}) + .havingRootCause() + .withMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionHasMessageParameters() { return assertThatNullPointerException().isThrownBy(() -> {}).withMessage("foo %s", "bar"); } @@ -107,6 +128,10 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatIOException().isThrownBy(() -> {}).withMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByIOExceptionRootCauseHasMessage() { + return assertThatIOException().isThrownBy(() -> {}).havingRootCause().withMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageParameters() { return assertThatIOException().isThrownBy(() -> {}).withMessage("foo %s", "bar"); } @@ -133,6 +158,13 @@ public ImmutableSet elidedTypesAndStaticImports() { .withMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByRootCauseHasMessage() { + return assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> {}) + .havingRootCause() + .withMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByHasMessageParameters() { return assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> {}) 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 bb2a99ca82..6a79a36cfd 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 @@ -34,6 +34,13 @@ public ImmutableSet elidedTypesAndStaticImports() { .hasMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByIllegalArgumentExceptionRootCauseHasMessage() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IllegalArgumentException.class) + .rootCause() + .hasMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByIllegalArgumentExceptionHasMessageParameters() { return assertThatThrownBy(() -> {}) .isInstanceOf(IllegalArgumentException.class) @@ -68,6 +75,13 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatThrownBy(() -> {}).isInstanceOf(IllegalStateException.class).hasMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByIllegalStateExceptionRootCauseHasMessage() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IllegalStateException.class) + .rootCause() + .hasMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByIllegalStateExceptionHasMessageParameters() { return assertThatThrownBy(() -> {}) .isInstanceOf(IllegalStateException.class) @@ -100,6 +114,13 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatThrownBy(() -> {}).isInstanceOf(NullPointerException.class).hasMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionRootCauseHasMessage() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(NullPointerException.class) + .rootCause() + .hasMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByNullPointerExceptionHasMessageParameters() { return assertThatThrownBy(() -> {}) .isInstanceOf(NullPointerException.class) @@ -132,6 +153,13 @@ public ImmutableSet elidedTypesAndStaticImports() { return assertThatThrownBy(() -> {}).isInstanceOf(IOException.class).hasMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByIOExceptionRootCauseHasMessage() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IOException.class) + .rootCause() + .hasMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByIOExceptionHasMessageParameters() { return assertThatThrownBy(() -> {}).isInstanceOf(IOException.class).hasMessage("foo %s", "bar"); } @@ -162,6 +190,13 @@ public ImmutableSet elidedTypesAndStaticImports() { .hasMessage("foo"); } + AbstractObjectAssert testAssertThatThrownByRootCauseHasMessage() { + return assertThatThrownBy(() -> {}) + .isInstanceOf(IllegalArgumentException.class) + .rootCause() + .hasMessage("foo"); + } + AbstractObjectAssert testAssertThatThrownByHasMessageParameters() { return assertThatThrownBy(() -> {}) .isInstanceOf(IllegalArgumentException.class)