Skip to content

Commit

Permalink
Issue #6207: Add Xpath regression test for LocalFinalVariableName
Browse files Browse the repository at this point in the history
  • Loading branch information
Lmh-java authored and romani committed Mar 15, 2024
1 parent 1bb3d56 commit bbb28a7
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
@@ -0,0 +1,115 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// 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.naming.LocalFinalVariableNameCheck.MSG_INVALID_PATTERN;

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.naming.LocalFinalVariableNameCheck;

public class XpathRegressionLocalFinalVariableNameTest extends AbstractXpathTestSupport {

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(LocalFinalVariableNameCheck.class);
moduleConfig.addProperty("format", "^[A-Z][A-Z0-9]*$");
moduleConfig.addProperty("tokens", "PARAMETER_DEF,RESOURCE");

final String[] expectedViolation = {
"7:21: " + getCheckMessage(LocalFinalVariableNameCheck.class,
MSG_INVALID_PATTERN, "scanner", "^[A-Z][A-Z0-9]*$"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameResource']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/LITERAL_TRY"
+ "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='scanner']"
);

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(LocalFinalVariableNameCheck.class);
moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");

final String[] expectedViolation = {
"5:19: " + getCheckMessage(LocalFinalVariableNameCheck.class,
MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameVar']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
+ "/IDENT[@text='VAR1']"
);

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(LocalFinalVariableNameCheck.class);
moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");

final String[] expectedViolation = {
"8:23: " + getCheckMessage(LocalFinalVariableNameCheck.class,
MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT["
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameInner']]"
+ "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK"
+ "/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
+ "/IDENT[@text='VAR1']"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
}
}
@@ -0,0 +1,11 @@
package org.checkstyle.suppressionxpathfilter.localfinalvariablename;

import java.util.Scanner;

public class SuppressionXpathRegressionLocalFinalVariableNameInner {
class InnerClass {
void MyMethod() {
final int VAR1 = 10; // warn
}
}
}
@@ -0,0 +1,12 @@
package org.checkstyle.suppressionxpathfilter.localfinalvariablename;

import java.util.Scanner;

public class SuppressionXpathRegressionLocalFinalVariableNameResource {
void MyMethod() {
try(Scanner scanner = new Scanner("ABC")) { // warn
final int VAR1 = 5;
final int var1 = 10;
}
}
}
@@ -0,0 +1,7 @@
package org.checkstyle.suppressionxpathfilter.localfinalvariablename;

public class SuppressionXpathRegressionLocalFinalVariableNameVar {
void MyMethod() {
final int VAR1 = 5; // warn
}
}
Expand Up @@ -97,7 +97,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"DesignForExtension",
"HideUtilityClassConstructor",
"InterfaceTypeParameterName",
"LocalFinalVariableName",
"LocalVariableName",
"MethodTypeParameterName",
"ModifiedControlVariable",
Expand Down

0 comments on commit bbb28a7

Please sign in to comment.