diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionParameterNumberTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionParameterNumberTest.java new file mode 100644 index 00000000000..365be83b633 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionParameterNumberTest.java @@ -0,0 +1,106 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code and other text files for adherence to a set of rules. +// Copyright (C) 2001-2024 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 static com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck.MSG_KEY; + +import java.io.File; +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.sizes.ParameterNumberCheck; + +public class XpathRegressionParameterNumberTest extends AbstractXpathTestSupport { + + @Override + protected String getCheckName() { + return ParameterNumberCheck.class.getSimpleName(); + } + + @Test + public void testDefault() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionParameterNumberDefault.java")); + + final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class); + + final String[] expectedViolations = { + "5:10: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 7, 11), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/COMPILATION_UNIT/CLASS_DEF" + + "[./IDENT[@text='SuppressionXpathRegressionParameterNumberDefault']]" + + "/OBJBLOCK/METHOD_DEF/IDENT[@text='myMethod']" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries); + + } + + @Test + public void testMethods() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionParameterNumberMethods.java")); + + final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class); + moduleConfig.addProperty("max", "10"); + moduleConfig.addProperty("tokens", "METHOD_DEF"); + + final String[] expectedViolations = { + "7:10: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 10, 11), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/COMPILATION_UNIT/CLASS_DEF" + + "[./IDENT[@text='SuppressionXpathRegressionParameterNumberMethods']]" + + "/OBJBLOCK/METHOD_DEF/IDENT[@text='myMethod']" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries); + } + + @Test + public void testIgnoreOverriddenMethods() throws Exception { + final String filePath = + getPath("SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods.java"); + final File fileToProcess = new File(filePath); + + final DefaultConfiguration moduleConfig = createModuleConfig(ParameterNumberCheck.class); + moduleConfig.addProperty("ignoreOverriddenMethods", "true"); + + final String[] expectedViolations = { + "6:13: " + getCheckMessage(ParameterNumberCheck.class, MSG_KEY, 7, 8), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods']]" + + "/OBJBLOCK/CTOR_DEF/IDENT" + + "[@text='SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods']" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries); + } + +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberDefault.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberDefault.java new file mode 100644 index 00000000000..f291b2f368c --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberDefault.java @@ -0,0 +1,15 @@ +package org.checkstyle.suppressionxpathfilter.parameternumber; + +public class SuppressionXpathRegressionParameterNumberDefault { + + void myMethod(int a, int b, int c, int d, int e, int f, int g, int h, // warn + int i, int j, int k) { + } + + public SuppressionXpathRegressionParameterNumberDefault() { // ok + } + + void myMethod2(int a, int b, int c, int d) { // ok + } + +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods.java new file mode 100644 index 00000000000..52a9ee1fcc5 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods.java @@ -0,0 +1,14 @@ +package org.checkstyle.suppressionxpathfilter.parameternumber; + +public class SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods + extends SuppressionXpathRegressionParameterNumberDefault { + + public SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods(int a, // warn + int b, int c, int d, int e, int f, int g, int h) + { + } + @Override + void myMethod(int a, int b, int c, int d, int e, int f, int g, int h, // ok + int k, int l, int m) { + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberMethods.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberMethods.java new file mode 100644 index 00000000000..a973d791723 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/parameternumber/SuppressionXpathRegressionParameterNumberMethods.java @@ -0,0 +1,16 @@ +package org.checkstyle.suppressionxpathfilter.parameternumber; + +public class SuppressionXpathRegressionParameterNumberMethods + extends SuppressionXpathRegressionParameterNumberDefault { + + @Override + void myMethod(int a, int b, int c, int d, int e, int f, int g, int h, // warn + int k, int l, int m) { + } + public SuppressionXpathRegressionParameterNumberMethods(int a, int b, int c, // ok + int d, int e, int f, int g, int h, int k, int l, int m) + { + } + void myMethod3(int a, int b, int c, int d, int e, int f, int g, int h) { // ok + } +} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java index 850eaba3b9e..36c74770a50 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -106,7 +106,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { "ModifiedControlVariable", "MutableException", "ParameterAssignment", - "ParameterNumber", "RedundantModifier", "SeparatorWrap", "SimplifyBooleanExpression",