Skip to content

Commit

Permalink
Issue checkstyle#6207: Add XPath IT Regression test for ParameterNumb…
Browse files Browse the repository at this point in the history
…erCheck
  • Loading branch information
mahfouz72 authored and romani committed Feb 11, 2024
1 parent b05d8f6 commit 28e85b7
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> expectedXpathQueries = Collections.singletonList(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods']]"
+ "/OBJBLOCK/CTOR_DEF/IDENT"
+ "[@text='SuppressionXpathRegressionParameterNumberIgnoreOverriddenMethods']"
);

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

}
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -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) {
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"ModifiedControlVariable",
"MutableException",
"ParameterAssignment",
"ParameterNumber",
"RedundantModifier",
"SeparatorWrap",
"SimplifyBooleanExpression",
Expand Down

0 comments on commit 28e85b7

Please sign in to comment.