Skip to content

Commit

Permalink
Issue checkstyle#7758: Update AbstractChecks to log DetailAST - Packa…
Browse files Browse the repository at this point in the history
…geDeclaration
  • Loading branch information
DXTkastb authored and romani committed Apr 19, 2020
1 parent cae000f commit 621975e
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package org.checkstyle.suppressionxpathfilter;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCheck;

public class XpathRegressionPackageDeclarationTest extends AbstractXpathTestSupport {

private final String checkName = PackageDeclarationCheck.class.getSimpleName();

@Override
protected String getCheckName() {
return checkName;
}

@Test
public void test1() throws Exception {
final File fileToProcess =
new File(getNonCompilablePath("SuppressionXpathRegression1.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(PackageDeclarationCheck.class);

final String[] expectedViolation = {
"2:1: " + getCheckMessage(PackageDeclarationCheck.class,
PackageDeclarationCheck.MSG_KEY_MISMATCH),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/PACKAGE_DEF"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void test2() throws Exception {
final File fileToProcess =
new File(getNonCompilablePath("SuppressionXpathRegression2.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(PackageDeclarationCheck.class);

final String[] expectedViolation = {
"3:1: " + getCheckMessage(PackageDeclarationCheck.class,
PackageDeclarationCheck.MSG_KEY_MISSING),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegression2']]",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegression2']]/MODIFIERS",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegression2']]/MODIFIERS/LITERAL_PUBLIC"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// non-compiled with javac: wrong package. Used for Testing purpose.
package my.packagename.mismatch.with.folder; // warn

public class SuppressionXpathRegression1 {
// code
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// non-compiled with javac: missing package. Used for Testing purpose.
// package is missing.
public class SuppressionXpathRegression2 { // warn
// code
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
* These directories are added to the classpath so that your classes
* are visible to JVM when it runs the code.
* </p>
* <p>
* <b>Note:</b>
* This Check is partially supported by Xpath Suppression Filters. In case of testing empty
* java files, Xpath Suppression Filters are not supported for this Check.
* (until
* <a href="https://github.com/checkstyle/checkstyle/pull/8110">#8110</a>)
* </p>
* <ul>
* <li>
* Property {@code matchDirectoryStructure} - Control whether to check for
Expand Down Expand Up @@ -141,11 +148,12 @@ public void beginTree(DetailAST ast) {
@Override
public void finishTree(DetailAST ast) {
if (!defined) {
int lineNumber = DEFAULT_LINE_NUMBER;
if (ast != null) {
lineNumber = ast.getLineNo();
if (ast == null) {
log(DEFAULT_LINE_NUMBER, MSG_KEY_MISSING);
}
else {
log(ast, MSG_KEY_MISSING);
}
log(lineNumber, MSG_KEY_MISSING);
}
}

Expand All @@ -161,7 +169,7 @@ public void visitToken(DetailAST ast) {
final String directoryName = getDirectoryName();

if (!directoryName.endsWith(packageName)) {
log(fullIdent.getLineNo(), MSG_KEY_MISMATCH, packageName);
log(ast, MSG_KEY_MISMATCH, packageName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@
* MissingJavadocType
* </li>
* <li>
* PackageDeclaration
* </li>
* <li>
* Regexp
* </li>
* <li>
Expand All @@ -90,6 +87,15 @@
* </li>
* </ul>
* <p>
* Certain Checks are partially supported by the filter:
* </p>
* <ul>
* <li>
* PackageDeclaration (until
* <a href="https://github.com/checkstyle/checkstyle/pull/8110">#8110</a>)
* </li>
* </ul>
* <p>
* Also, the filter does not support suppressions inside javadoc reported by Javadoc checks:
* </p>
* <ul id="SuppressionXpathFilter_JavadocChecks">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testDefaultNoPackage() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);

final String[] expected = {
"1: " + getCheckMessage(MSG_KEY_MISSING),
"1:1: " + getCheckMessage(MSG_KEY_MISSING),
};

verify(checkConfig, getPath("InputPackageDeclarationNoPackage.java"), expected);
Expand Down Expand Up @@ -72,7 +72,7 @@ public void testFileForDiffDirectoryMismatch() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);

final String[] expected = {
"1: " + getCheckMessage(MSG_KEY_MISMATCH),
"1:1: " + getCheckMessage(MSG_KEY_MISMATCH),
};

verify(checkConfig,
Expand All @@ -84,7 +84,7 @@ public void testFileForDirectoryMismatchAtParent() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);

final String[] expected = {
"1: " + getCheckMessage(MSG_KEY_MISMATCH),
"1:1: " + getCheckMessage(MSG_KEY_MISMATCH),
};

verify(checkConfig,
Expand All @@ -97,7 +97,7 @@ public void testFileForDirectoryMismatchAtSubpackage() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);

final String[] expected = {
"1: " + getCheckMessage(MSG_KEY_MISMATCH),
"1:1: " + getCheckMessage(MSG_KEY_MISMATCH),
};

verify(checkConfig,
Expand Down Expand Up @@ -141,7 +141,7 @@ public void testFileIgnoreDirectoryMismatchAtSubpackage() throws Exception {
public void testNoPackage() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
final String[] expected = {
"2: " + getCheckMessage(MSG_KEY_MISSING),
"2:1: " + getCheckMessage(MSG_KEY_MISSING),
};

verify(checkConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"InterfaceMemberImpliedModifier",
"JavadocMethod",
"MissingJavadocType",
"PackageDeclaration",
"Regexp",
"RegexpSinglelineJava",
"TodoComment",
Expand Down
7 changes: 7 additions & 0 deletions src/xdocs/config_coding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4738,6 +4738,13 @@ public void foo(String s, int i) {}
These directories are added to the classpath so that your classes
are visible to JVM when it runs the code.
</p>
<p>
<b>Note:</b>
This Check is partially supported by Xpath Suppression Filters. In case of testing empty
java files, Xpath Suppression Filters are not supported for this Check. (until
<a href="https://github.com/checkstyle/checkstyle/pull/8110">#8110
</a>)
</p>
</subsection>

<subsection name="Properties" id="PackageDeclaration_Properties">
Expand Down
11 changes: 10 additions & 1 deletion src/xdocs/config_filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -927,14 +927,23 @@ public class UserService {
<li>InterfaceMemberImpliedModifier</li>
<li>JavadocMethod</li>
<li>MissingJavadocType</li>
<li>PackageDeclaration</li>
<li>Regexp</li>
<li>RegexpSinglelineJava</li>
<li>TodoComment</li>
<li>TrailingComment</li>
<li>UnnecessaryParentheses</li>
<li>VariableDeclarationUsageDistance</li>
</ul>
<p>
Certain Checks are partially supported by the filter:
</p>
<ul>
<li>
PackageDeclaration
(until
<a href="https://github.com/checkstyle/checkstyle/pull/8110">#8110</a>)
</li>
</ul>
<p>
Also, the filter does not support suppressions inside javadoc reported by Javadoc checks:
</p>
Expand Down

0 comments on commit 621975e

Please sign in to comment.