Coverage has been increased to 100% in UncommentedMainCheck. Issue #1290

This commit is contained in:
Ilja Dubinin 2015-07-30 00:46:28 +01:00 committed by Roman Ivanov
parent e3366844c4
commit dcd7085ce2
6 changed files with 125 additions and 9 deletions

View File

@ -1095,7 +1095,6 @@
<regex><pattern>.*.checks.SuppressWarningsHolder</pattern><branchRate>75</branchRate><lineRate>93</lineRate></regex>
<regex><pattern>.*.checks.TrailingCommentCheck</pattern><branchRate>90</branchRate><lineRate>93</lineRate></regex>
<regex><pattern>.*.checks.TranslationCheck</pattern><branchRate>81</branchRate><lineRate>83</lineRate></regex>
<regex><pattern>.*.checks.UncommentedMainCheck</pattern><branchRate>83</branchRate><lineRate>88</lineRate></regex>
<regex><pattern>.*.checks.UniquePropertiesCheck\$.*</pattern><branchRate>75</branchRate><lineRate>90</lineRate></regex>
<regex><pattern>.*.checks.coding.AbstractSuperCheck</pattern><branchRate>78</branchRate><lineRate>89</lineRate></regex>

View File

@ -115,6 +115,7 @@ public class UncommentedMainCheck
@Override
public void visitToken(DetailAST ast) {
switch (ast.getType()) {
case TokenTypes.PACKAGE_DEF:
visitPackageDef(ast);
@ -235,13 +236,8 @@ public class UncommentedMainCheck
final DetailAST arrayType = arrayDecl.getFirstChild();
if (arrayType.getType() == TokenTypes.IDENT
|| arrayType.getType() == TokenTypes.DOT) {
final FullIdent type = FullIdent.createFullIdent(arrayType);
return "String".equals(type.getText())
|| "java.lang.String".equals(type.getText());
}
return false;
final FullIdent type = FullIdent.createFullIdent(arrayType);
return "String".equals(type.getText())
|| "java.lang.String".equals(type.getText());
}
}

View File

@ -21,10 +21,15 @@ package com.puppycrawl.tools.checkstyle.checks;
import static com.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck.MSG_KEY;
import org.junit.Assert;
import org.junit.Test;
import antlr.CommonHiddenStreamToken;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public class UncommentedMainCheckTest
extends BaseCheckTestSupport {
@ -53,4 +58,52 @@ public class UncommentedMainCheckTest
};
verify(checkConfig, getPath("InputUncommentedMain.java"), expected);
}
@Test
public void testTokens() throws Exception {
UncommentedMainCheck check = new UncommentedMainCheck();
Assert.assertNotNull(check.getRequiredTokens());
Assert.assertNotNull(check.getAcceptableTokens());
Assert.assertArrayEquals(check.getDefaultTokens(), check.getAcceptableTokens());
Assert.assertArrayEquals(check.getDefaultTokens(), check.getRequiredTokens());
}
@Test
public void testDeepDepth() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(UncommentedMainCheck.class);
final String[] expected = {
};
verify(checkConfig, getPath("InputUncommentedMain2.java"), expected);
}
@Test
public void testWrongName() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(UncommentedMainCheck.class);
final String[] expected = {
};
verify(checkConfig, getPath("InputUncommentedMain3.java"), expected);
}
@Test
public void testWrongArrayType() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(UncommentedMainCheck.class);
final String[] expected = {
};
verify(checkConfig, getPath("InputUncommentedMain4.java"), expected);
}
@Test
public void testIllegalStateException() throws Exception {
UncommentedMainCheck check = new UncommentedMainCheck();
DetailAST ast = new DetailAST();
ast.initialize(new CommonHiddenStreamToken(TokenTypes.CTOR_DEF, "ctor"));
try {
check.visitToken(ast);
Assert.fail();
}
catch (IllegalStateException ex) {
ast.toString().equals(ex.getMessage());
}
}
}

View File

@ -0,0 +1,41 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
// Created: 2003
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
public class InputUncommentedMain2
{
private class PC {
// uncommented main with depth 2
public void main(String[] args)
{
System.out.println("InputUncommentedMain.main()");
}
//lets go deeper
private class PC2 {
// uncommented main with depth 3
public void main(String[] args)
{
System.out.println("InputUncommentedMain.main()");
}
}
}
public static void main(String[] args)
{
System.out.println("InputUncommentedMain.main()");
}
}
interface IF {
void main(String[] args);
}

View File

@ -0,0 +1,13 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
// Created: 2003
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
class oneMoreClass {
public static void anyWrongMethodName(String[] args)
{
System.out.println("InputUncommentedMain.main()");
}
}

View File

@ -0,0 +1,14 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
// Created: 2003
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
class InputUncommentedMainTest4
{
// one more uncommented main
public static void main(int[] args)
{
System.out.println("test1.main()");
}
}