Skip to content

Commit

Permalink
Issue checkstyle#6207: Add Xpath Regression Test for FinalParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Zopsss authored and romani committed Jan 6, 2024
1 parent e449968 commit 720ad55
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// 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 java.io.File;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck;

public class XpathRegressionFinalParametersTest extends AbstractXpathTestSupport {

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

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

@Test
public void testOne() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionFinalParameters1.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(FinalParametersCheck.class);

final String[] expectedViolation = {
"5:24: " + getCheckMessage(FinalParametersCheck.class,
FinalParametersCheck.MSG_KEY, "argOne"),
};

final List<String> expectedXpathQueries = List.of(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters1']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
+ "/PARAMETERS",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters1']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters1']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters1']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters1']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/LITERAL_INT"
);

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

@Test
public void testTwo() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionFinalParameters2.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(FinalParametersCheck.class);

moduleConfig.addProperty("tokens", "CTOR_DEF");

final String[] expectedViolation = {
"5:55: " + getCheckMessage(FinalParametersCheck.class,
FinalParametersCheck.MSG_KEY, "argOne"),
};

final List<String> expectedXpathQueries = List.of(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/OBJBLOCK/CTOR_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/PARAMETERS",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/OBJBLOCK/CTOR_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/OBJBLOCK/CTOR_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/OBJBLOCK/CTOR_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/OBJBLOCK/CTOR_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionFinalParameters2']]"
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/LITERAL_INT"
);

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

@Test
public void testThree() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionFinalParameters3.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(FinalParametersCheck.class);

moduleConfig.addProperty("ignorePrimitiveTypes", "true");

final String[] expectedViolation = {
"11:32: " + getCheckMessage(FinalParametersCheck.class,
FinalParametersCheck.MSG_KEY, "argOne"),
};

final List<String> expectedXpathQueries = List.of(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters3']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters3']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters3']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters3']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE[./IDENT[@text='String']]",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='SuppressionXpathRegressionFinalParameters3']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/IDENT[@text='String']"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.checkstyle.suppressionxpathfilter.finalparameters;

public class SuppressionXpathRegressionFinalParameters1 {

public void method(int argOne, final int argTwo) { // warn
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.checkstyle.suppressionxpathfilter.finalparameters;

public class SuppressionXpathRegressionFinalParameters2 {

public SuppressionXpathRegressionFinalParameters2(int argOne, final int argTwo) { // warn
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.checkstyle.suppressionxpathfilter.finalparameters;

public class SuppressionXpathRegressionFinalParameters3 {

class AnonymousClass {
public void method(int argOne, int argTwo) {} // ok, int is primitive
}

public void createClass() {
AnonymousClass obj = new AnonymousClass() {
public void method(String[] argOne, final String[] argTwo) {} // warn
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"DesignForExtension",
"EqualsHashCode",
"ExecutableStatementCount",
"FinalParameters",
"HideUtilityClassConstructor",
"IllegalInstantiation",
"IllegalTokenText",
Expand Down

0 comments on commit 720ad55

Please sign in to comment.