diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-04-12 09:50:48 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-04-12 10:04:58 +0200 |
commit | 3f782b64d76ebb1de03e8eb6d1f902a6bbba63de (patch) | |
tree | d510784206f9237e0e6a8ada06537f6d86395334 /plugins | |
parent | d6693ffe167bb5d92aaddbd28ff3780a7494f54d (diff) | |
download | sonarqube-3f782b64d76ebb1de03e8eb6d1f902a6bbba63de.tar.gz sonarqube-3f782b64d76ebb1de03e8eb6d1f902a6bbba63de.zip |
SONAR-2349 New rule to detect empty Java files
Diffstat (limited to 'plugins')
3 files changed, 112 insertions, 5 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/EmptyFileCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/EmptyFileCheck.java new file mode 100644 index 00000000000..40e276d4d13 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/EmptyFileCheck.java @@ -0,0 +1,44 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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 3 of the License, or (at your option) any later version. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +package org.sonar.java.squid.check; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.check.RuleProperty; +import org.sonar.squid.api.CheckMessage; +import org.sonar.squid.api.SourceClass; +import org.sonar.squid.api.SourceFile; +import org.sonar.squid.measures.Metric; + +@Rule(key = "EmptyFile", name = "Empty file", priority = Priority.MAJOR, + description = "Detect empty files, which do not have any lines of code. Example: <pre>\n//package org.foo;\n//\n//public class Bar {}\n</pre>") +public final class EmptyFileCheck extends SquidCheck { + + @Override + public void visitFile(SourceFile file) { + int loc = file.getInt(Metric.LINES_OF_CODE); + if (loc==0) { + CheckMessage message = new CheckMessage(this, "This Java file is empty"); + file.log(message); + } + } + +} diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java index 7558aa95b6e..fe31dba88ab 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java @@ -34,10 +34,7 @@ import org.sonar.java.bytecode.check.ArchitectureCheck; import org.sonar.java.bytecode.check.CallToDeprecatedMethodCheck; import org.sonar.java.bytecode.check.UnusedPrivateMethodCheck; import org.sonar.java.bytecode.check.UnusedProtectedMethodCheck; -import org.sonar.java.squid.check.ClassComplexityCheck; -import org.sonar.java.squid.check.DITCheck; -import org.sonar.java.squid.check.MethodComplexityCheck; -import org.sonar.java.squid.check.NoSonarCheck; +import org.sonar.java.squid.check.*; public final class SquidRuleRepository extends RuleRepository { private AnnotationRuleParser ruleParser; @@ -61,6 +58,6 @@ public final class SquidRuleRepository extends RuleRepository { // AST checks UndocumentedApiCheck.class, ContinueCheck.class, BreakCheck.class, // Squid checks - DITCheck.class, ClassComplexityCheck.class, MethodComplexityCheck.class, NoSonarCheck.class); + DITCheck.class, ClassComplexityCheck.class, MethodComplexityCheck.class, NoSonarCheck.class, EmptyFileCheck.class); } } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java new file mode 100644 index 00000000000..358dc657b29 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java @@ -0,0 +1,66 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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 3 of the License, or (at your option) any later version. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +package org.sonar.java.squid.check; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.java.ast.JavaAstScanner; +import org.sonar.java.ast.SquidTestUtils; +import org.sonar.java.squid.JavaSquidConfiguration; +import org.sonar.java.squid.SquidScanner; +import org.sonar.squid.Squid; +import org.sonar.squid.api.CheckMessage; +import org.sonar.squid.api.SourceFile; +import org.sonar.squid.measures.Metric; + +public class EmptyFileCheckTest { + private Squid squid; + + @Before + public void setUp() { + squid = new Squid(new JavaSquidConfiguration()); + EmptyFileCheck check = new EmptyFileCheck(); + squid.registerVisitor(check); + JavaAstScanner scanner = squid.register(JavaAstScanner.class); + scanner.scanDirectory(SquidTestUtils.getFile("/metrics/commentedCode")); + squid.decorateSourceCodeTreeWith(Metric.values()); + squid.register(SquidScanner.class).scan(); + } + + @Test + public void shouldDetectEmptyFiles() { + SourceFile file = (SourceFile) squid.search("org/foo/CommentedOutFile.java"); + assertThat(file.getCheckMessages().size(), is(1)); + CheckMessage message = file.getCheckMessages().iterator().next(); + assertThat(message.getLine(), nullValue()); + assertThat(message.getDefaultMessage(), is("This Java file is empty")); + } + + @Test + public void shouldNotLogOnCorrectFiles() { + SourceFile file = (SourceFile) squid.search("CommentedCode.java"); + assertThat(file.getCheckMessages().size(), is(0)); + } +} |