From 9f8394b46a01f7ddac672afddd546ed6e82ccd7e Mon Sep 17 00:00:00 2001 From: Godin Date: Tue, 23 Nov 2010 18:52:32 +0000 Subject: SONAR-1845, SONAR-1931: Create a new Squid rules to check class and method complexity --- .../java/ast/check/ClassComplexityCheckTest.java | 64 -------------------- .../java/ast/check/MethodComplexityCheckTest.java | 35 ----------- .../sonar/java/bytecode/check/DITCheckTest.java | 44 -------------- .../java/squid/check/ClassComplexityCheckTest.java | 68 ++++++++++++++++++++++ .../org/sonar/java/squid/check/DITCheckTest.java | 46 +++++++++++++++ .../squid/check/MethodComplexityCheckTest.java | 39 +++++++++++++ 6 files changed, 153 insertions(+), 143 deletions(-) delete mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ClassComplexityCheckTest.java delete mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/MethodComplexityCheckTest.java delete mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java create mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java create mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java create mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java (limited to 'plugins/sonar-squid-java-plugin/src/test') diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ClassComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ClassComplexityCheckTest.java deleted file mode 100644 index 39692969332..00000000000 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ClassComplexityCheckTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * 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.ast.check; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.sonar.java.ast.SquidTestUtils.getFile; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.java.ast.JavaAstScanner; -import org.sonar.java.squid.JavaSquidConfiguration; -import org.sonar.squid.Squid; -import org.sonar.squid.api.CheckMessage; -import org.sonar.squid.api.SourceFile; - -public class ClassComplexityCheckTest { - - private Squid squid; - - @Before - public void setUp() { - squid = new Squid(new JavaSquidConfiguration()); - ClassComplexityCheck check = new ClassComplexityCheck(); - check.setThreshold(5); - squid.registerVisitor(check); - JavaAstScanner scanner = squid.register(JavaAstScanner.class); - scanner.scanFile(getFile("/metrics/branches/NoBranches.java")); - scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java")); - } - - @Test - public void testComplexityExceedsThreshold() { - SourceFile file = (SourceFile) squid.search("ComplexBranches.java"); - assertThat(file.getCheckMessages().size(), is(1)); - CheckMessage message = file.getCheckMessages().iterator().next(); - assertThat(message.getLine(), is(3)); - } - - @Test - public void testComplexityNotExceedsThreshold() { - SourceFile file = (SourceFile) squid.search("NoBranches.java"); - assertThat(file.getCheckMessages().size(), is(0)); - } - -} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/MethodComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/MethodComplexityCheckTest.java deleted file mode 100644 index 5f8a73396bd..00000000000 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/MethodComplexityCheckTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.sonar.java.ast.check; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.sonar.java.ast.SquidTestUtils.getFile; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.java.ast.JavaAstScanner; -import org.sonar.java.squid.JavaSquidConfiguration; -import org.sonar.squid.Squid; -import org.sonar.squid.api.CheckMessage; -import org.sonar.squid.api.SourceFile; - -public class MethodComplexityCheckTest { - private Squid squid; - - @Before - public void setUp() { - squid = new Squid(new JavaSquidConfiguration()); - MethodComplexityCheck check = new MethodComplexityCheck(); - check.setThreshold(5); - squid.registerVisitor(check); - JavaAstScanner scanner = squid.register(JavaAstScanner.class); - scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java")); - } - - @Test - public void testMethodComplexityExceedsThreshold() { - SourceFile file = (SourceFile) squid.search("ComplexBranches.java"); - assertThat(file.getCheckMessages().size(), is(1)); - CheckMessage message = file.getCheckMessages().iterator().next(); - assertThat(message.getLine(), is(10)); - } -} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java deleted file mode 100644 index bbe16d51117..00000000000 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.sonar.java.bytecode.check; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.sonar.java.ast.SquidTestUtils.getFile; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.sonar.java.ast.JavaAstScanner; -import org.sonar.java.bytecode.BytecodeScanner; -import org.sonar.java.squid.JavaSquidConfiguration; -import org.sonar.squid.Squid; -import org.sonar.squid.api.CheckMessage; -import org.sonar.squid.api.SourceFile; - -public class DITCheckTest { - - private static Squid squid; - - @BeforeClass - public static void setup() { - squid = new Squid(new JavaSquidConfiguration()); - squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/src")); - DITCheck check = new DITCheck(); - check.setMax(1); - squid.registerVisitor(check); - squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/bin")); - } - - @Test - public void testDepthOfInheritanceGreaterThanMaximum() { - SourceFile file = (SourceFile) squid.search("UnusedProtectedMethod.java"); - assertThat(file.getCheckMessages().size(), is(1)); - CheckMessage message = file.getCheckMessages().iterator().next(); - assertThat(message.getLine(), is(7)); - } - - @Test - public void testDepthOfInheritanceLowerThanMaximum() { - SourceFile file = (SourceFile) squid.search("Job.java"); - assertThat(file.getCheckMessages().size(), is(0)); - } - -} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java new file mode 100644 index 00000000000..24f7f02f261 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java @@ -0,0 +1,68 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.junit.Assert.assertThat; +import static org.sonar.java.ast.SquidTestUtils.getFile; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.java.ast.JavaAstScanner; +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 ClassComplexityCheckTest { + + private Squid squid; + + @Before + public void setUp() { + squid = new Squid(new JavaSquidConfiguration()); + ClassComplexityCheck check = new ClassComplexityCheck(); + check.setThreshold(5); + squid.registerVisitor(check); + JavaAstScanner scanner = squid.register(JavaAstScanner.class); + scanner.scanFile(getFile("/metrics/branches/NoBranches.java")); + scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java")); + squid.decorateSourceCodeTreeWith(Metric.values()); + squid.register(SquidScanner.class).scan(); + } + + @Test + public void testComplexityExceedsThreshold() { + SourceFile file = (SourceFile) squid.search("ComplexBranches.java"); + assertThat(file.getCheckMessages().size(), is(1)); + CheckMessage message = file.getCheckMessages().iterator().next(); + assertThat(message.getLine(), is(3)); + } + + @Test + public void testComplexityNotExceedsThreshold() { + SourceFile file = (SourceFile) squid.search("NoBranches.java"); + assertThat(file.getCheckMessages().size(), is(0)); + } + +} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java new file mode 100644 index 00000000000..51e9a215704 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java @@ -0,0 +1,46 @@ +package org.sonar.java.squid.check; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.sonar.java.ast.SquidTestUtils.getFile; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.sonar.java.ast.JavaAstScanner; +import org.sonar.java.bytecode.BytecodeScanner; +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; + +public class DITCheckTest { + + private static Squid squid; + + @BeforeClass + public static void setup() { + squid = new Squid(new JavaSquidConfiguration()); + DITCheck check = new DITCheck(); + check.setMax(1); + squid.registerVisitor(check); + squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/src")); + squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/bin")); + squid.register(SquidScanner.class).scan(); + } + + @Test + public void testDepthOfInheritanceGreaterThanMaximum() { + SourceFile file = (SourceFile) squid.search("UnusedProtectedMethod.java"); + assertThat(file.getCheckMessages().size(), is(1)); + CheckMessage message = file.getCheckMessages().iterator().next(); + assertThat(message.getLine(), is(7)); + } + + @Test + public void testDepthOfInheritanceLowerThanMaximum() { + SourceFile file = (SourceFile) squid.search("Job.java"); + assertThat(file.getCheckMessages().size(), is(0)); + } + +} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java new file mode 100644 index 00000000000..5533ca8e18d --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java @@ -0,0 +1,39 @@ +package org.sonar.java.squid.check; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.sonar.java.ast.SquidTestUtils.getFile; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.java.ast.JavaAstScanner; +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 MethodComplexityCheckTest { + private Squid squid; + + @Before + public void setUp() { + squid = new Squid(new JavaSquidConfiguration()); + MethodComplexityCheck check = new MethodComplexityCheck(); + check.setThreshold(5); + squid.registerVisitor(check); + JavaAstScanner scanner = squid.register(JavaAstScanner.class); + scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java")); + squid.decorateSourceCodeTreeWith(Metric.values()); + squid.register(SquidScanner.class).scan(); + } + + @Test + public void testMethodComplexityExceedsThreshold() { + SourceFile file = (SourceFile) squid.search("ComplexBranches.java"); + assertThat(file.getCheckMessages().size(), is(1)); + CheckMessage message = file.getCheckMessages().iterator().next(); + assertThat(message.getLine(), is(10)); + } +} -- cgit v1.2.3