Skip to content

Commit

Permalink
Issue checkstyle#10745: use forbidden api annotation for specific sup…
Browse files Browse the repository at this point in the history
…pression
  • Loading branch information
romani committed Aug 29, 2021
1 parent 1891eec commit 7822067
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
11 changes: 8 additions & 3 deletions pom.xml
Expand Up @@ -221,6 +221,7 @@
<pitest.junit5.plugin.version>0.14</pitest.junit5.plugin.version>
<sonar.test.exclusions>**/test/resources/**/*,**/it/resources/**/*</sonar.test.exclusions>
<junit.version>5.7.2</junit.version>
<forbiddenapis.version>3.1</forbiddenapis.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -358,6 +359,12 @@
<classifier>runtime</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
<version>${forbiddenapis.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
Expand Down Expand Up @@ -1473,7 +1480,7 @@
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
<version>3.1</version>
<version>${forbiddenapis.version}</version>
<configuration>
<targetVersion>${java.version}</targetVersion>
<failOnUnsupportedJava>false</failOnUnsupportedJava>
Expand Down Expand Up @@ -1582,8 +1589,6 @@

<exclude>**/SuppressWithNearbyCommentFilterTest.class</exclude>

<exclude>**/PackageDeclarationCheckTest.class</exclude>

<exclude>**/AbstractModuleTestSupport.class</exclude>

<exclude>**/CommentsIndentationCheckTest.class</exclude>
Expand Down
Expand Up @@ -28,6 +28,7 @@
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import de.thetaphi.forbiddenapis.SuppressForbidden;

public class PackageDeclarationCheckTest extends AbstractModuleTestSupport {

Expand Down Expand Up @@ -151,6 +152,7 @@ public void testNoPackage() throws Exception {
expected);
}

@SuppressForbidden
@Test
public void testEmptyFile() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
Expand Down
Expand Up @@ -200,13 +200,7 @@ private static void verifyHasProductionFile(Map<String, List<String>> allTests,
if (file.isFile()) {
final String fileName = file.getName().replace("Test.java", ".java");

if (!fileName.endsWith("TestSupport.java")
// tests external utility XPathEvaluator
&& !"XpathMapper.java".equals(fileName)
// JavadocMetadataScraper and related classes are temporarily hosted in test
&& !file.getPath().contains("meta")
// InlineConfigParser is hosted in test
&& !file.getPath().contains("bdd")) {
if (isTarget(file, fileName)) {
final String path;

try {
Expand All @@ -230,6 +224,18 @@ private static void verifyHasProductionFile(Map<String, List<String>> allTests,
}
}

private static boolean isTarget(File file, String fileName) {
return !fileName.endsWith("TestSupport.java")
// tests external utility XPathEvaluator
&& !"XpathMapper.java".equals(fileName)
// JavadocMetadataScraper and related classes are temporarily hosted in test
&& !file.getPath().contains("meta")
// InlineConfigParser is hosted in test
&& !file.getPath().contains("bdd")
// Annotation to suppress invocation of forbidden apis
&& !"SuppressForbiddenApi.java".equals(fileName);
}

private static boolean checkInputMatchCorrectFileStructure(List<String> classes,
String folderPath, boolean skipFileNaming, String fileName) {
boolean result = false;
Expand Down

0 comments on commit 7822067

Please sign in to comment.