diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-28 13:17:57 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-28 13:18:46 +0100 |
commit | b8eecc041ad4aa23a3bf5cf7c8847573b125d187 (patch) | |
tree | d0b719f881f8f324d574fa699bd6befb7608c058 /plugins | |
parent | 220dd330306d76b7ff13baa07abf461688c92fc1 (diff) | |
download | sonarqube-b8eecc041ad4aa23a3bf5cf7c8847573b125d187.tar.gz sonarqube-b8eecc041ad4aa23a3bf5cf7c8847573b125d187.zip |
SONAR-2149 Resource filters are ignored in complexity distributions of Java projects
This issue implies SONAR-2153 : API: A decorator should override formulas
Diffstat (limited to 'plugins')
10 files changed, 271 insertions, 89 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java index 856c3438fea..ea18236faef 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java @@ -23,6 +23,8 @@ import org.sonar.api.CoreProperties; import org.sonar.api.Plugin; import org.sonar.api.Properties; import org.sonar.api.Property; +import org.sonar.plugins.squid.decorators.ClassComplexityDistributionBuilder; +import org.sonar.plugins.squid.decorators.FunctionComplexityDistributionBuilder; import java.util.Arrays; import java.util.List; @@ -58,7 +60,8 @@ public class SquidPlugin implements Plugin { } public List getExtensions() { - return Arrays.asList(SquidSearchProxy.class, SquidSensor.class, SquidRuleRepository.class, JavaSourceImporter.class); + return Arrays.asList(SquidSearchProxy.class, SquidSensor.class, SquidRuleRepository.class, JavaSourceImporter.class, + ClassComplexityDistributionBuilder.class, FunctionComplexityDistributionBuilder.class); } @Override diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java index 05b8726e1b1..b621e87b230 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java @@ -36,17 +36,17 @@ public final class BridgeFactory { private static List<Bridge> create(NoSonarFilter noSonarFilter) { return Arrays.asList(new CopyBasicMeasuresBridge(), new PackagesBridge(), new PublicUndocumentedApiBridge(), - new ClassComplexityDistributionBridge(), new FunctionComplexityDistributionBridge(), new NoSonarFilterLoader(noSonarFilter), + new NoSonarFilterLoader(noSonarFilter), new ChidamberKemererBridge(), new RobertCMartinBridge(), new ChidamberKemererDistributionBridge(), new DesignBridge(), new Lcom4BlocksBridge(), new ChecksBridge()); } public static List<Bridge> create(boolean bytecodeScanned, SensorContext context, CheckFactory checkFactory, - ResourceIndex resourceIndex, Squid squid, NoSonarFilter noSonarFilter) { + ResourceIndex resourceIndex, Squid squid, NoSonarFilter noSonarFilter) { List<Bridge> result = new ArrayList<Bridge>(); for (Bridge bridge : create(noSonarFilter)) { bridge.setCheckFactory(checkFactory); - if ( !bridge.needsBytecode() || bytecodeScanned) { + if (!bridge.needsBytecode() || bytecodeScanned) { bridge.setContext(context); bridge.setSquid(squid); bridge.setResourceIndex(resourceIndex); diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java index f0720cdb7f8..105a33e6b2d 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java @@ -31,6 +31,7 @@ import org.sonar.java.api.JavaMethod; import org.sonar.squid.Squid; import org.sonar.squid.api.*; import org.sonar.squid.indexer.QueryByType; +import org.sonar.squid.measures.Metric; import java.util.Collection; import java.util.HashMap; @@ -93,6 +94,7 @@ public final class ResourceIndex extends HashMap<SourceCode, Resource> { .setSignature(squidMethod.getName()) .setFromLine(squidMethod.getStartAtLine()) .setToLine(squidMethod.getEndAtLine()) + .setAccessor(squidMethod.getInt(Metric.ACCESSORS)>0) .create(); context.index(sonarMethod, sonarClass); diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java new file mode 100644 index 00000000000..f0a0636f19c --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java @@ -0,0 +1,73 @@ +/* + * 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.plugins.squid.decorators; + +import org.sonar.api.batch.Decorator; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.batch.DependedUpon; +import org.sonar.api.batch.DependsUpon; +import org.sonar.api.measures.*; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.api.resources.Scopes; + +/** + * @since 2.6 + */ +public final class ClassComplexityDistributionBuilder implements Decorator { + + public static final Number[] LIMITS = {0, 5, 10, 20, 30, 60, 90}; + + @DependsUpon + public Metric dependOnComplexity() { + return CoreMetrics.COMPLEXITY; + } + + @DependedUpon + public Metric generatesFunctionComplexityDistribution() { + return CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION; + } + + public void decorate(Resource resource, DecoratorContext context) { + if (shouldExecuteOn(resource, context)) { + RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, LIMITS); + for (DecoratorContext childContext : context.getChildren()) { + if (Scopes.isType(childContext.getResource())) { + Measure complexity = childContext.getMeasure(CoreMetrics.COMPLEXITY); + if (complexity != null) { + builder.add(complexity.getValue()); + } + } + } + Measure measure = builder.build(true); + measure.setPersistenceMode(PersistenceMode.MEMORY); + context.saveMeasure(measure); + } + } + + boolean shouldExecuteOn(Resource resource, DecoratorContext context) { + return Scopes.isFile(resource) && context.getMeasure(CoreMetrics.COMPLEXITY) != null; + } + + public boolean shouldExecuteOnProject(Project project) { + return Java.KEY.equals(project.getLanguageKey()); + } +} diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/FunctionComplexityDistributionBuilder.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/FunctionComplexityDistributionBuilder.java new file mode 100644 index 00000000000..75c5c28fd8e --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/FunctionComplexityDistributionBuilder.java @@ -0,0 +1,76 @@ +/* + * 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.plugins.squid.decorators; + +import org.sonar.api.batch.Decorator; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.batch.DependedUpon; +import org.sonar.api.batch.DependsUpon; +import org.sonar.api.measures.*; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.api.resources.Scopes; +import org.sonar.java.api.JavaMethod; + +/** + * @since 2.6 + */ +public final class FunctionComplexityDistributionBuilder implements Decorator { + + public static final Number[] LIMITS = {1, 2, 4, 6, 8, 10, 12}; + + @DependsUpon + public Metric dependOnComplexity() { + return CoreMetrics.COMPLEXITY; + } + + @DependedUpon + public Metric generatesFunctionComplexityDistribution() { + return CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION; + } + + public void decorate(Resource resource, DecoratorContext context) { + if (shouldExecuteOn(resource, context)) { + RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, LIMITS); + for (DecoratorContext childContext : context.getChildren()) { + if (childContext.getResource() instanceof JavaMethod) { + JavaMethod javaMethod = (JavaMethod)childContext.getResource(); + Measure complexity = childContext.getMeasure(CoreMetrics.COMPLEXITY); + if (!javaMethod.isAccessor() && complexity != null) { + builder.add(complexity.getValue()); + } + } + } + Measure measure = builder.build(true); + measure.setPersistenceMode(PersistenceMode.MEMORY); + context.saveMeasure(measure); + } + } + + boolean shouldExecuteOn(Resource resource, DecoratorContext context) { + return Scopes.isType(resource) && context.getMeasure(CoreMetrics.COMPLEXITY) != null; + } + + public boolean shouldExecuteOnProject(Project project) { + return Java.KEY.equals(project.getLanguageKey()); + } +} + diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java index 12a6cbed182..0387b501095 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java @@ -31,6 +31,6 @@ public class SquidPluginTest { @Test public void coverageForFun() { assertThat(new SquidPlugin().getKey(), not(nullValue())); - assertThat(new SquidPlugin().getExtensions().size(), is(4)); + assertThat(new SquidPlugin().getExtensions().size(), is(6)); } } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridgeTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridgeTest.java deleted file mode 100644 index 9e132df3471..00000000000 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridgeTest.java +++ /dev/null @@ -1,41 +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.plugins.squid.bridges;
-
-import org.junit.Test;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.never;
-import static org.mockito.Matchers.*;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.test.IsMeasure;
-
-public class ClassComplexityDistributionBridgeTest extends BridgeTestCase {
-
- @Test
- public void classComplexityDistribution() {
- verify(context).saveMeasure(eq(new JavaPackage("org.apache.struts.config")), argThat(new IsMeasure(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, "0=10;5=3;10=2;20=1;30=4;60=4;90=1")));
- verify(context, never()).saveMeasure(eq(new JavaFile("org.apache.struts.config.ConfigRuleSet")), eq(new Measure(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, "equals() on measure only uses the metric")));
- verify(context, never()).saveMeasure(eq(project), eq(new Measure(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, "equals() on measure only uses the metric")));
- }
-}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridgeTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridgeTest.java deleted file mode 100644 index d5203779f6a..00000000000 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridgeTest.java +++ /dev/null @@ -1,43 +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.plugins.squid.bridges;
-
-import org.junit.Test;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
-import org.sonar.api.test.IsMeasure;
-
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
-
-public class FunctionComplexityDistributionBridgeTest extends BridgeTestCase {
-
- @Test
- public void functionComplexityDistribution() {
- verify(context).saveMeasure(eq(new JavaPackage("org.apache.struts.config")), argThat(new IsMeasure(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, "1=186;2=88;4=11;6=12;8=7;10=2;12=8")));
- verify(context, never()).saveMeasure(eq(new JavaFile("org.apache.struts.config.ConfigRuleSet")), eq(new Measure(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, "equals() on measure only uses the metric")));
- verify(context, never()).saveMeasure(eq(project), eq(new Measure(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, "equals() on measure only uses the metric")));
- }
-}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java new file mode 100644 index 00000000000..477643076d1 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java @@ -0,0 +1,57 @@ +/* + * 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.plugins.squid.decorators; + +import org.junit.Test; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.JavaFile; +import org.sonar.api.resources.JavaPackage; +import org.sonar.api.resources.Project; +import org.sonar.java.api.JavaClass; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ClassComplexityDistributionBuilderTest { + + @Test + public void shouldExecuteOnJavaProjectsOnly() throws Exception { + ClassComplexityDistributionBuilder builder = new ClassComplexityDistributionBuilder(); + assertThat(builder.shouldExecuteOnProject(new Project("java").setLanguageKey(Java.KEY)), is(true)); + assertThat(builder.shouldExecuteOnProject(new Project("php").setLanguageKey("php")), is(false)); + } + + @Test + public void shouldExecuteOnFilesOnly() throws Exception { + ClassComplexityDistributionBuilder builder = new ClassComplexityDistributionBuilder(); + DecoratorContext context = mock(DecoratorContext.class); + when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 20.0)); + + assertThat(builder.shouldExecuteOn(new JavaPackage("org.foo"), context), is(false)); + assertThat(builder.shouldExecuteOn(new JavaFile("org.foo.Bar"), context), is(true)); + assertThat(builder.shouldExecuteOn(JavaClass.create("org.foo.Bar"), context), is(false)); + } + +} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/FunctionComplexityDistributionBuilderTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/FunctionComplexityDistributionBuilderTest.java new file mode 100644 index 00000000000..aa0efe31cac --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/FunctionComplexityDistributionBuilderTest.java @@ -0,0 +1,55 @@ +/* + * 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.plugins.squid.decorators; + +import org.junit.Test; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.JavaFile; +import org.sonar.api.resources.JavaPackage; +import org.sonar.api.resources.Project; +import org.sonar.java.api.JavaClass; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FunctionComplexityDistributionBuilderTest { + @Test + public void shouldExecuteOnJavaProjectsOnly() throws Exception { + FunctionComplexityDistributionBuilder builder = new FunctionComplexityDistributionBuilder(); + assertThat(builder.shouldExecuteOnProject(new Project("java").setLanguageKey(Java.KEY)), is(true)); + assertThat(builder.shouldExecuteOnProject(new Project("php").setLanguageKey("php")), is(false)); + } + + @Test + public void shouldExecuteOnClassesOnly() throws Exception { + FunctionComplexityDistributionBuilder builder = new FunctionComplexityDistributionBuilder(); + DecoratorContext context = mock(DecoratorContext.class); + when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 20.0)); + + assertThat(builder.shouldExecuteOn(new JavaPackage("org.foo"), context), is(false)); + assertThat(builder.shouldExecuteOn(new JavaFile("org.foo.Bar"), context), is(false)); + assertThat(builder.shouldExecuteOn(JavaClass.create("org.foo.Bar"), context), is(true)); + } +} |