Skip to content

Commit

Permalink
Issue checkstyle#7764: Update AbstractChecks to log DetailAST - Uncom…
Browse files Browse the repository at this point in the history
…mentedMain
  • Loading branch information
AmrDeveloper authored and ImmortalRabbit committed Apr 9, 2020
1 parent ed4869b commit 2c5ea15
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 24 deletions.
@@ -0,0 +1,95 @@
////////////////////////////////////////////////////////////////////////////////
// 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.UncommentedMainCheck;

public class XpathRegressionUncommentedMainTest extends AbstractXpathTestSupport {

private final Class<UncommentedMainCheck> clazz =
UncommentedMainCheck.class;

@Override
protected String getCheckName() {
return clazz.getSimpleName();
}

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

final DefaultConfiguration moduleConfig =
createModuleConfig(UncommentedMainCheck.class);

final String[] expectedViolation = {
"4:5: " + getCheckMessage(UncommentedMainCheck.class,
UncommentedMainCheck.MSG_KEY),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionUncommentedMain']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionUncommentedMain']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionUncommentedMain']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS/LITERAL_PUBLIC"
);

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(UncommentedMainCheck.class);

final String[] expectedViolation = {
"5:9: " + getCheckMessage(UncommentedMainCheck.class,
UncommentedMainCheck.MSG_KEY),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionUncommentedMainTwo']]"
+ "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Launcher']"
+ "]/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionUncommentedMainTwo']]"
+ "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Launcher']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionUncommentedMainTwo']]"
+ "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Launcher']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS/LITERAL_PUBLIC"
);

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

public class SuppressionXpathRegressionUncommentedMain {
public static void main(String... args) {} // warn
}
@@ -0,0 +1,7 @@
package org.checkstyle.suppressionxpathfilter.uncommentedmain;

public class SuppressionXpathRegressionUncommentedMainTwo {
public static class Launcher {
public static void main(String[] args) {} // warn
}
}
Expand Up @@ -217,7 +217,7 @@ && checkName(method)
&& checkModifiers(method)
&& checkType(method)
&& checkParams(method)) {
log(method.getLineNo(), MSG_KEY);
log(method, MSG_KEY);
}
}

Expand Down
Expand Up @@ -122,9 +122,6 @@
* TrailingComment
* </li>
* <li>
* UncommentedMain
* </li>
* <li>
* UnnecessaryParentheses
* </li>
* <li>
Expand Down
Expand Up @@ -48,10 +48,10 @@ public void testDefaults()
final DefaultConfiguration checkConfig =
createModuleConfig(UncommentedMainCheck.class);
final String[] expected = {
"14: " + getCheckMessage(MSG_KEY),
"23: " + getCheckMessage(MSG_KEY),
"32: " + getCheckMessage(MSG_KEY),
"96: " + getCheckMessage(MSG_KEY),
"14:5: " + getCheckMessage(MSG_KEY),
"23:5: " + getCheckMessage(MSG_KEY),
"32:5: " + getCheckMessage(MSG_KEY),
"96:5: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputUncommentedMain.java"), expected);
}
Expand All @@ -63,9 +63,9 @@ public void testExcludedClasses()
createModuleConfig(UncommentedMainCheck.class);
checkConfig.addAttribute("excludedClasses", "\\.Main.*$");
final String[] expected = {
"14: " + getCheckMessage(MSG_KEY),
"32: " + getCheckMessage(MSG_KEY),
"96: " + getCheckMessage(MSG_KEY),
"14:5: " + getCheckMessage(MSG_KEY),
"32:5: " + getCheckMessage(MSG_KEY),
"96:5: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputUncommentedMain.java"), expected);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public void testVisitPackage() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
checkConfig.addAttribute("excludedClasses", "uncommentedmain\\.InputUncommentedMain5");
final String[] expected = {
"14: " + getCheckMessage(MSG_KEY),
"14:5: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputUncommentedMain5.java"), expected);
}
Expand Down
Expand Up @@ -77,13 +77,13 @@ public class SuppressWarningsFilterTest
"56:9: "
+ getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
"61: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
"71: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"71:5: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"76: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
"77: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"77:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"83: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
"84: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"84:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"90: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
"91: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"91:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"97: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

Expand Down Expand Up @@ -117,10 +117,10 @@ public void testDefault() throws Exception {
+ getCheckMessage(ParameterNumberCheck.class, ParameterNumberCheck.MSG_KEY, 7, 8),
"56:9: "
+ getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY, "Exception"),
"71: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"77: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"84: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"91: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"71:5: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"77:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"84:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
"91:9: " + getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
};
verifySuppressed(filterConfig, suppressed);
}
Expand Down Expand Up @@ -191,15 +191,15 @@ public void testSuppressById() throws Exception {
"6:17: "
+ getCheckMessage(AbstractNameCheck.class,
MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
"8: "
"8:5: "
+ getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
};
final String[] expectedViolationMessages = {
"3: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
"6:17: "
+ getCheckMessage(AbstractNameCheck.class,
MSG_INVALID_PATTERN, "A1", "^[a-z][a-zA-Z0-9]*$"),
"8: "
"8:5: "
+ getCheckMessage(UncommentedMainCheck.class, UncommentedMainCheck.MSG_KEY),
};

Expand Down
Expand Up @@ -79,7 +79,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"RegexpSinglelineJava",
"TodoComment",
"TrailingComment",
"UncommentedMain",
"UnnecessaryParentheses",
"VariableDeclarationUsageDistance",
"WriteTag"
Expand Down
1 change: 0 additions & 1 deletion src/xdocs/config_filters.xml
Expand Up @@ -928,7 +928,6 @@ public class UserService {
<li>RegexpSinglelineJava</li>
<li>TodoComment</li>
<li>TrailingComment</li>
<li>UncommentedMain</li>
<li>UnnecessaryParentheses</li>
<li>VariableDeclarationUsageDistance</li>
<li>WriteTag</li>
Expand Down

0 comments on commit 2c5ea15

Please sign in to comment.