From c256c10eea2e4f39c3206ab1f2a6bb31c1941f19 Mon Sep 17 00:00:00 2001 From: akanksha Date: Mon, 28 Oct 2024 01:05:56 +0530 Subject: [PATCH] Issue #6207: Added XPath regression test for ClassTypeParameterName --- ...hRegressionClassTypeParameterNameTest.java | 128 ++++++++++++++++++ ...InputXpathClassTypeParameterNameClass.java | 7 + ...tXpathClassTypeParameterNameInterface.java | 16 +++ ...nputXpathClassTypeParameterNameMethod.java | 22 +++ .../internal/XpathRegressionTest.java | 1 - 5 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionClassTypeParameterNameTest.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameClass.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameInterface.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameMethod.java diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionClassTypeParameterNameTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionClassTypeParameterNameTest.java new file mode 100644 index 00000000000..cb0c8ecfbf1 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionClassTypeParameterNameTest.java @@ -0,0 +1,128 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// 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.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.AbstractClassNameCheck; +import com.puppycrawl.tools.checkstyle.checks.naming.ClassTypeParameterNameCheck; + +public class XpathRegressionClassTypeParameterNameTest extends AbstractXpathTestSupport { + + private final String checkName = ClassTypeParameterNameCheck.class.getSimpleName(); + + @Override + protected String getCheckName() { + return checkName; + } + + @Test + public void testClass() throws Exception { + final File fileToProcess = + new File(getPath("InputXpathClassTypeParameterNameClass.java")); + final String pattern = "^[A-Z]$"; + final DefaultConfiguration moduleConfig = + createModuleConfig(ClassTypeParameterNameCheck.class); + + final String[] expectedViolation = { + "5:20: " + getCheckMessage(ClassTypeParameterNameCheck.class, + AbstractClassNameCheck.MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "abc", pattern), + }; + + final List expectedXpathQueries = Arrays.asList( + "/COMPILATION_UNIT/CLASS_DEF" + + "[./IDENT[@text='InputXpathClassTypeParameterNameClass']]" + + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='MyClass2']]" + + "/TYPE_PARAMETERS/TYPE_PARAMETER[./IDENT[@text='abc']]", + "/COMPILATION_UNIT/CLASS_DEF" + + "[./IDENT[@text='InputXpathClassTypeParameterNameClass']]" + + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='MyClass2']]/TYPE_PARAMETERS" + + "/TYPE_PARAMETER/IDENT[@text='abc']" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testInterface() throws Exception { + final String pattern = "^[A-Z]{2,}$"; + final File fileToProcess = + new File(getPath("InputXpathClassTypeParameterNameInterface.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(ClassTypeParameterNameCheck.class); + moduleConfig.addProperty("format", pattern); + + final String[] expectedViolation = { + "9:20: " + getCheckMessage(ClassTypeParameterNameCheck.class, + AbstractClassNameCheck.MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "aBc", pattern), + }; + + final List expectedXpathQueries = Arrays.asList( + "/COMPILATION_UNIT/INTERFACE_DEF" + + "[./IDENT[@text='InputXpathClassTypeParameterNameInterface']]" + + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='MyClass2']]" + + "/TYPE_PARAMETERS/TYPE_PARAMETER[./IDENT[@text='aBc']]", + "/COMPILATION_UNIT/INTERFACE_DEF" + + "[./IDENT[@text='InputXpathClassTypeParameterNameInterface']]" + + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='MyClass2']]" + + "/TYPE_PARAMETERS/TYPE_PARAMETER/IDENT[@text='aBc']" + ); + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testMethod() throws Exception { + final String pattern = "(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"; + final File fileToProcess = + new File(getPath("InputXpathClassTypeParameterNameMethod.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(ClassTypeParameterNameCheck.class); + moduleConfig.addProperty("format", pattern); + + final String[] expectedViolation = { + "12:24: " + getCheckMessage(ClassTypeParameterNameCheck.class, + AbstractClassNameCheck.MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "ABC", pattern), + }; + + final List expectedXpathQueries = Arrays.asList( + "/COMPILATION_UNIT/CLASS_DEF" + + "[./IDENT[@text='InputXpathClassTypeParameterNameMethod']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest2']]/SLIST/CLASS_DEF" + + "[./IDENT[@text='MyClass2']]/TYPE_PARAMETERS" + + "/TYPE_PARAMETER[./IDENT[@text='ABC']]", + "/COMPILATION_UNIT/CLASS_DEF" + + "[./IDENT[@text='InputXpathClassTypeParameterNameMethod']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest2']]/SLIST/CLASS_DEF" + + "[./IDENT[@text='MyClass2']]/TYPE_PARAMETERS" + + "/TYPE_PARAMETER/IDENT[@text='ABC']" + ); + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameClass.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameClass.java new file mode 100644 index 00000000000..fa6523dda14 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameClass.java @@ -0,0 +1,7 @@ +package org.checkstyle.suppressionxpathfilter.classtypeparametername; + +public class InputXpathClassTypeParameterNameClass { + class MyClass1 {} + class MyClass2 {} // warn + class MyClass3 {} +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameInterface.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameInterface.java new file mode 100644 index 00000000000..13ca95d797a --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameInterface.java @@ -0,0 +1,16 @@ +package org.checkstyle.suppressionxpathfilter.classtypeparametername; + +interface InputXpathClassTypeParameterNameInterface { + + class MyClass1 { + public void myTest1() {} + } + + class MyClass2 { // warn + public void myTest2() {} + } + + class MyClass3 { + public void myTest3() {} + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameMethod.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameMethod.java new file mode 100644 index 00000000000..253c05f643d --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/classtypeparametername/InputXpathClassTypeParameterNameMethod.java @@ -0,0 +1,22 @@ +package org.checkstyle.suppressionxpathfilter.classtypeparametername; + +public class InputXpathClassTypeParameterNameMethod { + + public void myTest1() { + class MyClass1 { + public void test() {} + } + } + + public void myTest2() { + class MyClass2 { // warn + public void test() {} + } + } + + public void myTest3() { + class MyClass3 { + public void test() {} + } +} +} 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 c8b156b38ec..75e1f6aba5d 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -92,7 +92,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { // till https://github.com/checkstyle/checkstyle/issues/6207 private static final Set MISSING_CHECK_NAMES = Set.of( "ClassFanOutComplexity", - "ClassTypeParameterName", "DescendantToken", "DesignForExtension", "HideUtilityClassConstructor",