diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java index 9d10231fb2a..f28b2d479df 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java @@ -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; @@ -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); } /** @@ -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 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); } } @@ -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"); @@ -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); } } @@ -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 parser = @@ -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); } } @@ -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); } @@ -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); } @@ -502,38 +492,28 @@ 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); } @@ -541,26 +521,14 @@ public void testMultiCheckOrder() throws Exception { @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); } @@ -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); @@ -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); } @@ -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); } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalker.java b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalker.java index 1309fde90cb..07cf0b64c2a 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalker.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalker.java @@ -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 { } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerJavadoc.java b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerJavadoc.java index b04ad091866..e1251082ca8 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerJavadoc.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerJavadoc.java @@ -1,5 +1,9 @@ +/* +com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck + +*/ package com.puppycrawl.tools.checkstyle.treewalker; -/**

Javadoc comment. */ // violation +/**

Javadoc comment. */ // violation, 'Redundant

tag' public class InputTreeWalkerJavadoc { } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder.java b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder.java index 1f8ab2bc504..0960c13d4ff 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder.java @@ -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 + } } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder2.java index 68b8ff04b88..84649964994 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder2.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerMultiCheckOrder2.java @@ -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" } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerProperFileExtension.java b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerProperFileExtension.java new file mode 100644 index 00000000000..851e1fda448 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerProperFileExtension.java @@ -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" +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerSuppressionCommentFilter.java b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerSuppressionCommentFilter.java index 69dd15ac809..8b75cf8a403 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerSuppressionCommentFilter.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/treewalker/InputTreeWalkerSuppressionCommentFilter.java @@ -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 }