Skip to content

Commit

Permalink
Issue #11343: Wrap assertThrows with getExpectedThrowable method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin222004 authored and nrmancuso committed Mar 26, 2024
1 parent 9846c80 commit bfb3e5f
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 81 deletions.
6 changes: 5 additions & 1 deletion config/import-control-test.xml
Expand Up @@ -20,13 +20,16 @@
<!-- Disallow @BeforeAll, see https://github.com/checkstyle/checkstyle/issues/12100 -->
<disallow class="org.junit.jupiter.api.BeforeAll"/>
<!-- Disallow Junit assert in favor of Truth asserts in few exceptions -->
<allow class="org.junit.jupiter.api.Assertions.assertThrows"/>
<allow class="org.junit.jupiter.api.Assertions.assertDoesNotThrow"/>
<disallow pkg="org.junit.jupiter.api.Assertions"/>

<!-- Reflection shouldn't be used in tests. -->
<disallow pkg="java\.lang\.reflect\.*" regex="true" />

<!-- see reason at https://github.com/checkstyle/checkstyle/issues/11343 -->
<disallow pkg="org.junit.jupiter.api.Assertions.assertThrows"/>
<disallow pkg="org.junit.Assert.assertThrows"/>

<allow pkg=".*" regex="true" />

<subpackage name="api">
Expand All @@ -40,6 +43,7 @@
<file name="TestUtil">
<!-- All reflection usage should be in this class. -->
<allow pkg="java.lang.reflect" />
<allow class="org.junit.jupiter.api.Assertions.assertThrows" />
</file>
</subpackage>
</subpackage>
Expand Down
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -234,9 +234,12 @@ public void testUnmodifiableCollection() throws Exception {
final Set<String> actualPackageNames = PackageNamesLoader
.getPackageNames(new TestUrlsClassLoader(Collections.emptyEnumeration()));

assertThrows(UnsupportedOperationException.class,
final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
() -> actualPackageNames.add("com.puppycrawl.tools.checkstyle.checks.modifier"));

assertWithMessage("Exception class is not expected")
.that(ex.getClass())
.isEqualTo(UnsupportedOperationException.class);
}

@Test
Expand Down
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;

Expand Down Expand Up @@ -346,7 +346,7 @@ public void testSymbolicLinkToNonDirectory() throws IOException {
final String cacheFilePath = symbolicLink.resolve("cache.temp").toString();
final PropertyCacheFile cache = new PropertyCacheFile(config, cacheFilePath);

final IOException thrown = assertThrows(IOException.class, cache::persist);
final IOException thrown = getExpectedThrowable(IOException.class, cache::persist);

final String expectedMessage = "Resolved symbolic link " + symbolicLink
+ " is not a directory.";
Expand Down Expand Up @@ -476,7 +476,7 @@ public void testExceptionNoSuchAlgorithmException() {
.thenThrow(NoSuchAlgorithmException.class);

final ReflectiveOperationException ex =
assertThrows(ReflectiveOperationException.class, () -> {
getExpectedThrowable(ReflectiveOperationException.class, () -> {
TestUtil.invokeStaticMethod(PropertyCacheFile.class,
"getHashCodeBasedOnObjectContent", config);
});
Expand Down
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle.ant;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -245,7 +245,7 @@ public final void testNoConfigFile() throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setProject(new Project());
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand All @@ -259,7 +259,7 @@ public final void testNonExistentConfig() throws IOException {
antTask.setConfig(getPath(NOT_EXISTING_FILE));
antTask.setProject(new Project());
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand All @@ -273,7 +273,7 @@ public final void testEmptyConfigFile() throws IOException {
antTask.setConfig(getPath("InputCheckstyleAntTaskEmptyConfig.xml"));
antTask.setProject(new Project());
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand All @@ -284,7 +284,7 @@ public final void testEmptyConfigFile() throws IOException {
@Test
public final void testNoFile() throws IOException {
final CheckstyleAntTask antTask = getCheckstyleAntTask();
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand All @@ -297,7 +297,7 @@ public final void testMaxWarningExceeded() throws IOException {
final CheckstyleAntTask antTask = getCheckstyleAntTask();
antTask.setFile(new File(getPath(WARNING_INPUT)));
antTask.setMaxWarnings(0);
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand Down Expand Up @@ -331,7 +331,7 @@ public final void testFailureProperty() throws IOException {

antTask.setProject(project);
antTask.setFailureProperty(failurePropertyName);
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand Down Expand Up @@ -463,7 +463,7 @@ public final void testSimultaneousConfiguration() throws IOException {

final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setConfig(url.toString());
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
() -> antTask.setConfig("Any string value"),
"BuildException is expected");
final String expected = "Attribute 'config' has already been set";
Expand Down Expand Up @@ -492,7 +492,7 @@ public final void testSetPropertiesNonExistentFile() throws IOException {
final CheckstyleAntTask antTask = getCheckstyleAntTask();
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
antTask.setProperties(new File(getPath(NOT_EXISTING_FILE)));
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand Down Expand Up @@ -572,7 +572,7 @@ public final void testCreateListenerException() throws IOException {
final File outputFile = new File("target/");
formatter.setTofile(outputFile);
antTask.addFormatter(formatter);
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand All @@ -591,7 +591,7 @@ public final void testCreateListenerExceptionWithXmlLogger() throws IOException
formatterType.setValue("xml");
formatter.setType(formatterType);
antTask.addFormatter(formatter);
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand All @@ -610,7 +610,7 @@ public final void testCreateListenerExceptionWithSarifLogger() throws IOExceptio
formatterType.setValue("sarif");
formatter.setType(formatterType);
antTask.addFormatter(formatter);
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand All @@ -621,7 +621,7 @@ public final void testCreateListenerExceptionWithSarifLogger() throws IOExceptio
@Test
public void testSetInvalidType() {
final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
() -> formatterType.setValue("foo"),
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand Down Expand Up @@ -832,7 +832,7 @@ public void testCheckerException() throws IOException {
antTask.setConfig(getPath(CONFIG_FILE));
antTask.setProject(new Project());
antTask.setFile(new File(""));
final BuildException ex = assertThrows(BuildException.class,
final BuildException ex = getExpectedThrowable(BuildException.class,
antTask::execute,
"BuildException is expected");
assertWithMessage("Error message is unexpected")
Expand Down
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle.api;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.io.File;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -397,7 +397,11 @@ public void testCheck() throws Exception {
public void testTokensAreUnmodifiable() {
final DummyAbstractCheck check = new DummyAbstractCheck();
final Set<String> tokenNameSet = check.getTokenNames();
assertThrows(UnsupportedOperationException.class, () -> tokenNameSet.add(""));
final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
() -> tokenNameSet.add(""));
assertWithMessage("Exception class is not expected")
.that(ex.getClass())
.isEqualTo(UnsupportedOperationException.class);
}

public static final class DummyAbstractCheck extends AbstractCheck {
Expand Down
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle.api;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import org.junit.jupiter.api.Test;

Expand All @@ -46,7 +46,7 @@ public void test() {

@Test
public void testNoSource() {
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
final IllegalArgumentException ex = getExpectedThrowable(IllegalArgumentException.class,
() -> new AuditEvent(null),
"IllegalArgumentException expected");
assertWithMessage("Invalid exception message")
Expand Down
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle.api;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -114,8 +114,11 @@ public void testUnmodifiableSet() {
filterSet.addBeforeExecutionFileFilter(filter);
final Set<BeforeExecutionFileFilter> excFilterSet =
filterSet.getBeforeExecutionFileFilters();
assertThrows(UnsupportedOperationException.class,
final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
() -> excFilterSet.add(filter));
assertWithMessage("Exception message is not expected")
.that(ex.getClass())
.isEqualTo(UnsupportedOperationException.class);
}

/*
Expand Down
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle.api;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.io.File;
import java.util.Arrays;
Expand Down Expand Up @@ -339,7 +339,11 @@ public void testUnmodifiableGetSingleLineComment() {
Arrays.asList("// comment ", " A + B ", " ")));
cppComments.reportSingleLineComment(1, 0);
final Map<Integer, TextBlock> comments = cppComments.getSingleLineComments();
assertThrows(UnsupportedOperationException.class, () -> comments.remove(0));
final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
() -> comments.remove(0));
assertWithMessage("Exception message not expected")
.that(ex.getClass())
.isEqualTo(UnsupportedOperationException.class);
}

@Test
Expand All @@ -348,6 +352,10 @@ public void testUnmodifiableGetBlockComments() {
Arrays.asList("/* comment ", " ", " comment */")));
clangComments.reportBlockComment(1, 0, 3, 9);
final Map<Integer, List<TextBlock>> comments = clangComments.getBlockComments();
assertThrows(UnsupportedOperationException.class, () -> comments.remove(0));
final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
() -> comments.remove(0));
assertWithMessage("Exception message not expected")
.that(ex.getClass())
.isEqualTo(UnsupportedOperationException.class);
}
}
Expand Up @@ -20,7 +20,7 @@
package com.puppycrawl.tools.checkstyle.api;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -115,8 +115,11 @@ public void testUnmodifiableSet() {
final Filter filter = new FilterSet();
filterSet.addFilter(filter);
final Set<Filter> subFilterSet = filterSet.getFilters();
assertThrows(UnsupportedOperationException.class,
final Exception ex = getExpectedThrowable(UnsupportedOperationException.class,
() -> subFilterSet.add(filter));
assertWithMessage("Exception message not expected")
.that(ex.getClass())
.isEqualTo(UnsupportedOperationException.class);
}

/*
Expand Down
Expand Up @@ -21,7 +21,7 @@

import static com.google.common.truth.Truth.assertWithMessage;
import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.MSG_KEY;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.io.File;

Expand Down Expand Up @@ -439,7 +439,7 @@ public void testImproperLeaveToken() {
final IllegalTypeCheck check = new IllegalTypeCheck();
final DetailAstImpl enumAst = new DetailAstImpl();
enumAst.setType(TokenTypes.ENUM_DEF);
final IllegalStateException exception = assertThrows(IllegalStateException.class,
final IllegalStateException exception = getExpectedThrowable(IllegalStateException.class,
() -> check.visitToken(enumAst), "IllegalStateException was expected");

assertWithMessage("Message doesn't contain ast")
Expand Down
Expand Up @@ -21,7 +21,7 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -236,7 +236,7 @@ public void testGetRequiredTokens() {

@Test
public void testMatchXpathWithFailedEvaluation() {
final CheckstyleException ex = assertThrows(CheckstyleException.class,
final CheckstyleException ex = getExpectedThrowable(CheckstyleException.class,
() -> verifyWithInlineConfigParser(getPath("InputMatchXpath5.java")));
assertThat(ex.getCause().getMessage())
.isEqualTo("Evaluation of Xpath query failed: count(*) div 0");
Expand Down
Expand Up @@ -22,7 +22,7 @@
import static com.google.common.truth.Truth.assertWithMessage;
import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY;
import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY_VOID;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;

import java.io.File;
import java.util.Collection;
Expand Down Expand Up @@ -186,7 +186,7 @@ public void testImproperVisitToken() {
final ReturnCountCheck check = new ReturnCountCheck();
final DetailAstImpl classDefAst = new DetailAstImpl();
classDefAst.setType(TokenTypes.CLASS_DEF);
final IllegalStateException exception = assertThrows(IllegalStateException.class,
final IllegalStateException exception = getExpectedThrowable(IllegalStateException.class,
() -> check.visitToken(classDefAst), "IllegalStateException was expected");

assertWithMessage("Message doesn't contain ast")
Expand All @@ -205,7 +205,7 @@ public void testImproperLeaveToken() {
final ReturnCountCheck check = new ReturnCountCheck();
final DetailAstImpl classDefAst = new DetailAstImpl();
classDefAst.setType(TokenTypes.CLASS_DEF);
final IllegalStateException exception = assertThrows(IllegalStateException.class,
final IllegalStateException exception = getExpectedThrowable(IllegalStateException.class,
() -> check.leaveToken(classDefAst), "IllegalStateException was expected");

assertWithMessage("Message doesn't contain ast")
Expand Down

0 comments on commit bfb3e5f

Please sign in to comment.