Skip to content

Commit

Permalink
Issue checkstyle#11446: update TreeWalkerTest to use verifyInlineConf…
Browse files Browse the repository at this point in the history
…igParser
  • Loading branch information
mahfouz72 authored and romani committed Feb 4, 2024
1 parent 9554a53 commit 680e6b8
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 63 deletions.
82 changes: 25 additions & 57 deletions src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck;
import com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck;
import com.puppycrawl.tools.checkstyle.filters.SuppressWithNearbyCommentFilter;
import com.puppycrawl.tools.checkstyle.filters.SuppressionCommentFilter;
import com.puppycrawl.tools.checkstyle.filters.SuppressionXpathFilter;
import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
Expand All @@ -93,18 +92,12 @@ protected String getPackageLocation() {

@Test
public void testProperFileExtension() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(ConstantNameCheck.class);
final File file = new File(temporaryFolder, "file.java");
try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
final String content = "public class Main { public static final int k = 5 + 4; }";
writer.write(content);
}
final String[] expected1 = {
"1:45: " + getCheckMessage(ConstantNameCheck.class,
MSG_INVALID_PATTERN, "k", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
final String path = getPath("InputTreeWalkerProperFileExtension.java");
final String[] expected = {
"10:27: " + getCheckMessage(ConstantNameCheck.class,
MSG_INVALID_PATTERN, "k", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
};
verify(checkConfig, file.getPath(), expected1);
verifyWithInlineConfigParser(path, expected);
}

/**
Expand All @@ -131,16 +124,15 @@ public void testProperFileExtension() throws Exception {
*/
@Test
public void testNoAuditEventsWithoutFilters() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OneTopLevelClassCheck.class);
final String[] expected = {
"5:1: " + getCheckMessage(OneTopLevelClassCheck.class,
"10:1: " + getCheckMessage(OneTopLevelClassCheck.class,
OneTopLevelClassCheck.MSG_KEY, "InputTreeWalkerInner"),
};
try (MockedConstruction<TreeWalkerAuditEvent> mocked =
Mockito.mockConstruction(TreeWalkerAuditEvent.class, (mock, context) -> {
throw new CheckstyleException("No audit events expected");
})) {
verify(checkConfig, getPath("InputTreeWalker.java"), expected);
verifyWithInlineConfigParser(getPath("InputTreeWalker.java"), expected);
}
}

Expand All @@ -150,9 +142,8 @@ public void testNoAuditEventsWithoutFilters() throws Exception {
*/
@Test
public void testConditionRequiredWithoutOrdinaryChecks() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(JavadocParagraphCheck.class);
final String[] expected = {
"3: " + getCheckMessage(JavadocParagraphCheck.class,
"7: " + getCheckMessage(JavadocParagraphCheck.class,
JavadocParagraphCheck.MSG_REDUNDANT_PARAGRAPH),
};
final String path = getPath("InputTreeWalkerJavadoc.java");
Expand All @@ -166,7 +157,7 @@ public void testConditionRequiredWithoutOrdinaryChecks() throws Exception {
// This will re-enable walk(..., AstState.WITH_COMMENTS)
parser.when(() -> JavaParser.appendHiddenCommentNodes(mockAst)).thenReturn(realAst);

verify(checkConfig, path, expected);
verifyWithInlineConfigParser(path, expected);
}
}

Expand All @@ -176,9 +167,8 @@ public void testConditionRequiredWithoutOrdinaryChecks() throws Exception {
*/
@Test
public void testConditionRequiredWithoutCommentChecks() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OneTopLevelClassCheck.class);
final String[] expected = {
"5:1: " + getCheckMessage(OneTopLevelClassCheck.class,
"10:1: " + getCheckMessage(OneTopLevelClassCheck.class,
OneTopLevelClassCheck.MSG_KEY, "InputTreeWalkerInner"),
};
try (MockedStatic<JavaParser> parser =
Expand All @@ -187,7 +177,7 @@ public void testConditionRequiredWithoutCommentChecks() throws Exception {
parser.when(() -> JavaParser.appendHiddenCommentNodes(any(DetailAST.class)))
.thenThrow(IllegalStateException.class);

verify(checkConfig, getPath("InputTreeWalker.java"), expected);
verifyWithInlineConfigParser(getPath("InputTreeWalker.java"), expected);
}
}

Expand Down Expand Up @@ -236,8 +226,8 @@ public void testOnEmptyFile() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(HiddenFieldCheck.class);
final String pathToEmptyFile =
File.createTempFile("file", ".java", temporaryFolder).getPath();
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, pathToEmptyFile, expected);
}

Expand Down Expand Up @@ -369,8 +359,8 @@ public void testWithCacheWithNoViolation() throws Exception {
new HashSet<>(), Thread.currentThread().getContextClassLoader());
checker.setModuleFactory(factory);
final File file = File.createTempFile("file", ".java", temporaryFolder);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checker, file.getPath(), expected);
}

Expand Down Expand Up @@ -502,65 +492,43 @@ public void testSetupChild() throws Exception {

@Test
public void testBehaviourWithChecksAndFilters() throws Exception {
final DefaultConfiguration filterConfig =
createModuleConfig(SuppressionCommentFilter.class);
filterConfig.addProperty("checkCPP", "false");

final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
treeWalkerConfig.addChild(createModuleConfig(MemberNameCheck.class));
treeWalkerConfig.addChild(filterConfig);

final String[] expected = {
"9:17: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern", "P",
"17:17: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern", "P",
"^[a-z][a-zA-Z0-9]*$"),
"4:17: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern", "I",
"12:17: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern", "I",
"^[a-z][a-zA-Z0-9]*$"),
};

verify(treeWalkerConfig,
verifyWithInlineConfigParser(
getPath("InputTreeWalkerSuppressionCommentFilter.java"),
expected);
}

@Test
public void testMultiCheckOrder() throws Exception {
final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
treeWalkerConfig.addChild(createModuleConfig(WhitespaceAroundCheck.class));
treeWalkerConfig.addChild(createModuleConfig(WhitespaceAfterCheck.class));

final String[] expected = {
"6:9: " + getCheckMessage(WhitespaceAfterCheck.class, "ws.notFollowed", "if"),
"6:9: " + getCheckMessage(WhitespaceAroundCheck.class, "ws.notFollowed", "if"),
"13:9: " + getCheckMessage(WhitespaceAfterCheck.class, "ws.notFollowed", "if"),
"13:9: " + getCheckMessage(WhitespaceAroundCheck.class, "ws.notFollowed", "if"),
};

verify(treeWalkerConfig,
verifyWithInlineConfigParser(
getPath("InputTreeWalkerMultiCheckOrder.java"),
expected);
}

@Test
public void testMultiCheckOfSameTypeNoIdResultsInOrderingByHash() throws Exception {

final DefaultConfiguration configuration1 = createModuleConfig(ParameterNameCheck.class);
configuration1.addProperty("format", "^[a-z]([a-z0-9][a-zA-Z0-9]*)?$");
configuration1.addProperty("accessModifiers", "protected, package, private");

final DefaultConfiguration configuration2 = createModuleConfig(ParameterNameCheck.class);
configuration2.addProperty("format", "^[a-z][a-z0-9][a-zA-Z0-9]*$");
configuration2.addProperty("accessModifiers", "PUBLIC");

final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
treeWalkerConfig.addChild(configuration1);
treeWalkerConfig.addChild(configuration2);

final String[] expected = {
"5:28: " + getCheckMessage(ParameterNameCheck.class,
"15:28: " + getCheckMessage(ParameterNameCheck.class,
"name.invalidPattern", "V2", "^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"),
"7:25: " + getCheckMessage(ParameterNameCheck.class,
"17:25: " + getCheckMessage(ParameterNameCheck.class,
"name.invalidPattern", "b", "^[a-z][a-z0-9][a-zA-Z0-9]*$"),
};

verify(treeWalkerConfig,
verifyWithInlineConfigParser(
getPath("InputTreeWalkerMultiCheckOrder2.java"),
expected);
}
Expand Down Expand Up @@ -631,8 +599,8 @@ public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Excep
checkerConfig.addProperty("cacheFile", cacheFile.getPath());

final String filePath = File.createTempFile("file", ".java", temporaryFolder).getPath();
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkerConfig, filePath, expected);
// One more time to use cache.
verify(checkerConfig, filePath, expected);
Expand All @@ -656,8 +624,8 @@ public void testTreeWalkerFilterAbsolutePath() throws Exception {
// test is only valid when relative paths are given
final String filePath = "src/test/resources/" + getPackageLocation()
+ "/InputTreeWalkerSuppressionXpathFilterAbsolute.java";
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(treeWalkerConfig, filePath, expected);
}

Expand All @@ -675,8 +643,8 @@ public void testExternalResourceFiltersWithNoExternalResource() throws Exception
checkerConfig.addProperty("cacheFile", cacheFile.getPath());

final String filePath = File.createTempFile("file", ".java", temporaryFolder).getPath();
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkerConfig, filePath, expected);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/*
com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck
*/
package com.puppycrawl.tools.checkstyle.treewalker;
/*comment*/
public class InputTreeWalker {
}
//violation below,'Top-level class InputTreeWalkerInner has to reside in its own source file'
class InputTreeWalkerInner {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/*
com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck
*/
package com.puppycrawl.tools.checkstyle.treewalker;

/** <p>Javadoc comment. */ // violation
/** <p>Javadoc comment. */ // violation, 'Redundant <p> tag'
public class InputTreeWalkerJavadoc {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/*
com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck
com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck
*/
package com.puppycrawl.tools.checkstyle.treewalker;

public class InputTreeWalkerMultiCheckOrder {
public void method() {
boolean test = true;
if(test) {
if(test) { // 2 violations

}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
/*
com.puppycrawl.tools.checkstyle.checks.naming.ParameterNameCheck
format = ^[a-z]([a-z0-9][a-zA-Z0-9]*)?$
accessModifiers = protected, package, private
com.puppycrawl.tools.checkstyle.checks.naming.ParameterNameCheck
format = ^[a-z][a-z0-9][a-zA-Z0-9]*$
accessModifiers = public
*/
package com.puppycrawl.tools.checkstyle.treewalker;

public class InputTreeWalkerMultiCheckOrder2 {
void fn1(int v1) {}
protected void fn2(int V2) {} // violation "Parameter name 'V2' must match pattern"
protected void fn2(int V2) {} // violation "Name 'V2' must match pattern"
private void fn3(int a) {}
public void fn4(int b) {} // violation "Parameter name 'b' must match pattern"
public void fn4(int b) {} // violation "Name 'b' must match pattern"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck
*/

package com.puppycrawl.tools.checkstyle.treewalker;

public class InputTreeWalkerProperFileExtension {

public static final int k = 5 + 4;
// violation above, "Name 'k' must match pattern"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/*
com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck
com.puppycrawl.tools.checkstyle.filters.SuppressionCommentFilter
checkCPP = false
*/
package com.puppycrawl.tools.checkstyle.treewalker;

public class InputTreeWalkerSuppressionCommentFilter {
private int I;
private int I; //violation, "Name 'I' must match pattern"
/* CHECKSTYLE:OFF */
private int J;
/* CHECKSTYLE:ON */
//CHECKSTYLE:OFF
private int P;
private int P; //violation, "Name 'P' must match pattern"
//CHECKSTYLE:ON
}

0 comments on commit 680e6b8

Please sign in to comment.