Skip to content

Commit

Permalink
Issue checkstyle#6207: Add Xpath Regression Test for InnerTypeLastTest
Browse files Browse the repository at this point in the history
  • Loading branch information
TanayMorakhia authored and romani committed Jan 14, 2024
1 parent 0c7e5f9 commit 276376b
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.checkstyle.suppressionxpathfilter.innertypelast;

public class SuppressionXpathRegressionInnerTypeLastOne {

public class Inner {
}

public SuppressionXpathRegressionInnerTypeLastOne() { // warn
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.checkstyle.suppressionxpathfilter.innertypelast;

public class SuppressionXpathRegressionInnerTypeLastThree {

static {} // OK

interface Inner {
}

public SuppressionXpathRegressionInnerTypeLastThree() { // warn
}

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

};

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"HideUtilityClassConstructor",
"IllegalInstantiation",
"IllegalTokenText",
"InnerTypeLast",
"InterfaceTypeParameterName",
"LocalFinalVariableName",
"LocalVariableName",
Expand Down

0 comments on commit 276376b

Please sign in to comment.