From 276376b288f031cd90ac1fdfa45aa0e3ed30a806 Mon Sep 17 00:00:00 2001 From: Tanay Date: Fri, 12 Jan 2024 22:28:13 +0530 Subject: [PATCH] Issue #6207: Add Xpath Regression Test for InnerTypeLastTest --- .../XpathRegressionInnerTypeLastTest.java | 141 ++++++++++++++++++ ...essionXpathRegressionInnerTypeLastOne.java | 11 ++ ...sionXpathRegressionInnerTypeLastThree.java | 13 ++ ...essionXpathRegressionInnerTypeLastTwo.java | 15 ++ .../internal/XpathRegressionTest.java | 1 - 5 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInnerTypeLastTest.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastOne.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastThree.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastTwo.java diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInnerTypeLastTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInnerTypeLastTest.java new file mode 100644 index 00000000000..dfe9a51e0c3 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInnerTypeLastTest.java @@ -0,0 +1,141 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// 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.design.InnerTypeLastCheck.MSG_KEY; + +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.design.InnerTypeLastCheck; + +public class XpathRegressionInnerTypeLastTest extends AbstractXpathTestSupport { + + private final String checkName = InnerTypeLastCheck.class.getSimpleName(); + + @Override + protected String getCheckName() { + return checkName; + } + + @Test + public void testOne() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInnerTypeLastOne.java")); + + final DefaultConfiguration moduleConfig = createModuleConfig(InnerTypeLastCheck.class); + + final String[] expectedViolations = { + "8:5: " + getCheckMessage(InnerTypeLastCheck.class, MSG_KEY), + }; + + final List expectedXpathQueries = Arrays.asList( + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastOne']]" + + "/OBJBLOCK/CTOR_DEF" + + "[./IDENT[@text='SuppressionXpathRegressionInnerTypeLastOne']]", + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastOne']]" + + "/OBJBLOCK/CTOR_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastOne']]" + + "/MODIFIERS", + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastOne']]" + + "/OBJBLOCK/CTOR_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastOne']]" + + "/MODIFIERS/LITERAL_PUBLIC" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolations, + expectedXpathQueries); + } + + @Test + public void testTwo() throws Exception { + + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInnerTypeLastTwo.java")); + + final DefaultConfiguration moduleConfig = createModuleConfig(InnerTypeLastCheck.class); + + final String[] expectedViolations = { + "11:9: " + getCheckMessage(InnerTypeLastCheck.class, MSG_KEY), + }; + + final List expectedXpathQueries = Arrays.asList( + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastTwo']]" + + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='innerMethod']]", + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastTwo']]" + + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT" + + "[@text='innerMethod']]/MODIFIERS", + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastTwo']]" + + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT" + + "[@text='innerMethod']]/MODIFIERS/LITERAL_PUBLIC" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolations, + expectedXpathQueries); + + } + + @Test + public void testThree() throws Exception { + + final File fileToProcess = new File( + getPath("SuppressionXpathRegressionInnerTypeLastThree.java")); + + final DefaultConfiguration moduleConfig = createModuleConfig(InnerTypeLastCheck.class); + + final String[] expectedViolations = { + "10:5: " + getCheckMessage(InnerTypeLastCheck.class, MSG_KEY), + }; + + final List expectedXpathQueries = Arrays.asList( + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastThree']]" + + "/OBJBLOCK/CTOR_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastThree']]", + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastThree']]" + + "/OBJBLOCK/CTOR_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastThree']]" + + "/MODIFIERS", + "/COMPILATION_UNIT/CLASS_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastThree']]" + + "/OBJBLOCK/CTOR_DEF[./IDENT" + + "[@text='SuppressionXpathRegressionInnerTypeLastThree']]" + + "/MODIFIERS/LITERAL_PUBLIC" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolations, + expectedXpathQueries); + + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastOne.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastOne.java new file mode 100644 index 00000000000..11675f1d750 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastOne.java @@ -0,0 +1,11 @@ +package org.checkstyle.suppressionxpathfilter.innertypelast; + +public class SuppressionXpathRegressionInnerTypeLastOne { + + public class Inner { + } + + public SuppressionXpathRegressionInnerTypeLastOne() { // warn + } + +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastThree.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastThree.java new file mode 100644 index 00000000000..9a60650d682 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastThree.java @@ -0,0 +1,13 @@ +package org.checkstyle.suppressionxpathfilter.innertypelast; + +public class SuppressionXpathRegressionInnerTypeLastThree { + + static {} // OK + + interface Inner { + } + + public SuppressionXpathRegressionInnerTypeLastThree() { // warn + } + +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastTwo.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastTwo.java new file mode 100644 index 00000000000..018f51d9c01 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/innertypelast/SuppressionXpathRegressionInnerTypeLastTwo.java @@ -0,0 +1,15 @@ +package org.checkstyle.suppressionxpathfilter.innertypelast; + +public class SuppressionXpathRegressionInnerTypeLastTwo { + + static {}; // OK + + public void innerMethod() {} // OK + + class Inner { + class InnerInInner {} + public void innerMethod() {} // warn + + }; + +} 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 8757e1baea4..25f97769a23 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -101,7 +101,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { "HideUtilityClassConstructor", "IllegalInstantiation", "IllegalTokenText", - "InnerTypeLast", "InterfaceTypeParameterName", "LocalFinalVariableName", "LocalVariableName",