Skip to content

Commit

Permalink
Issue checkstyle#7755: Update AbstractChecks to log DetailAST - Outer…
Browse files Browse the repository at this point in the history
…TypeFilename
  • Loading branch information
HuGanghui authored and ImmortalRabbit committed Apr 9, 2020
1 parent aa2fb33 commit 92dad8e
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config/checkstyle_resources_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@
files="outertypefilename[\\/]InputOuterTypeFilenameNoPublic\.java"/>
<suppress checks="OuterTypeFilename"
files="[\\/]package-info\.java"/>
<suppress checks="OuterTypeFilename"
files="src[\\/]it[\\/]resources[\\/].*[\\/]SuppressionXpathRegressionOuterTypeFilename.*\.java"/>

</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testOuterTypeFilename2() throws Exception {
@Test
public void testOuterTypeFilename3() throws Exception {
final String[] expected = {
"3: " + getCheckMessage(OuterTypeFilenameCheck.class, MSG_KEY),
"3:1: " + getCheckMessage(OuterTypeFilenameCheck.class, MSG_KEY),
};

final Configuration checkConfig = getModuleConfig("OuterTypeFilename");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 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.OuterTypeFilenameCheck;

public class XpathRegressionOuterTypeFilenameTest extends AbstractXpathTestSupport {

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(OuterTypeFilenameCheck.class);

final String[] expectedViolation = {
"3:1: " + getCheckMessage(OuterTypeFilenameCheck.class,
OuterTypeFilenameCheck.MSG_KEY),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/CLASS_DEF[./IDENT[@text='Class1']]",
"/CLASS_DEF[./IDENT[@text='Class1']]/MODIFIERS",
"/CLASS_DEF[./IDENT[@text='Class1']]/LITERAL_CLASS"
);

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(OuterTypeFilenameCheck.class);

final String[] expectedViolation = {
"3:1: " + getCheckMessage(OuterTypeFilenameCheck.class,
OuterTypeFilenameCheck.MSG_KEY),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/CLASS_DEF[./IDENT[@text='Test']]",
"/CLASS_DEF[./IDENT[@text='Test']]/MODIFIERS",
"/CLASS_DEF[./IDENT[@text='Test']]/LITERAL_CLASS"
);

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

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

class Class1 { // warn

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

class Test { // warn
public interface NestedInterface {}
public enum NestedEnum {}
class NestedClass {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void visitToken(DetailAST ast) {
@Override
public void finishTree(DetailAST rootAST) {
if (!hasPublic && wrongType != null) {
log(wrongType.getLineNo(), MSG_KEY);
log(wrongType, MSG_KEY);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@
* NoLineWrap
* </li>
* <li>
* OuterTypeFilename
* </li>
* <li>
* OverloadMethodsDeclarationOrder
* </li>
* <li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testNestedClass() throws Exception {
public void testNestedClass2() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
final String[] expected = {
"3: " + getCheckMessage(MSG_KEY),
"3:1: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputOuterTypeFilename1a.java"), expected);
}
Expand All @@ -112,7 +112,7 @@ public void testPublicClassIsNotFirst() throws Exception {
public void testNoPublicClasses() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
final String[] expected = {
"3: " + getCheckMessage(MSG_KEY),
"3:1: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputOuterTypeFilenameNoPublic.java"), expected);
}
Expand All @@ -128,7 +128,7 @@ public void testFineDefault() throws Exception {
public void testWrongDefault() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
final String[] expected = {
"4: " + getCheckMessage(MSG_KEY),
"4:2: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputOuterTypeFilename5.java"), expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"MissingSwitchDefault",
"NeedBraces",
"NoLineWrap",
"OuterTypeFilename",
"OverloadMethodsDeclarationOrder",
"PackageAnnotation",
"PackageDeclaration",
Expand Down
1 change: 0 additions & 1 deletion src/xdocs/config_filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,6 @@ public class UserService {
<li>MissingSwitchDefault</li>
<li>NeedBraces</li>
<li>NoLineWrap</li>
<li>OuterTypeFilename</li>
<li>OverloadMethodsDeclarationOrder</li>
<li>PackageAnnotation</li>
<li>PackageDeclaration</li>
Expand Down

0 comments on commit 92dad8e

Please sign in to comment.