--- /dev/null
+/*
+ * 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);
+ }
+ }
+
+}
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;
// 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);
}
}
--- /dev/null
+/*
+ * 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));
+ }
+}