diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-03-20 10:40:13 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-03-20 13:51:34 +0400 |
commit | 380241e75968446018fba19e65df8ffe549bf1da (patch) | |
tree | 6d573b44e9c86353f09b1d29cfae3e0e8e4a6e27 | |
parent | a9f1cd33f9c640f78ea7aa464a7e3b7cab30dbfd (diff) | |
download | sonarqube-380241e75968446018fba19e65df8ffe549bf1da.tar.gz sonarqube-380241e75968446018fba19e65df8ffe549bf1da.zip |
SONAR-3289 On java projects compute complexity distribution by file
And do not compute complexity distribution by class.
-rw-r--r-- | plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java | 13 | ||||
-rw-r--r-- | plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/FileComplexityDistributionDecorator.java (renamed from plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java) | 38 | ||||
-rw-r--r-- | plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java | 4 | ||||
-rw-r--r-- | plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java | 103 | ||||
-rw-r--r-- | plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/FileComplexityDistributionDecoratorTest.java | 87 |
5 files changed, 115 insertions, 130 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 7932d233545..789c0c0d1cf 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 @@ -20,7 +20,6 @@ package org.sonar.plugins.squid; import org.sonar.api.*; -import org.sonar.api.PropertyType; import org.sonar.plugins.squid.decorators.*; import java.util.Arrays; @@ -58,9 +57,15 @@ import java.util.List; public final class SquidPlugin extends SonarPlugin { public List getExtensions() { - return Arrays.asList(SquidSensor.class, SquidRuleRepository.class, JavaSourceImporter.class, - ClassComplexityDistributionBuilder.class, FunctionComplexityDistributionBuilder.class, ClassesDecorator.class, - ChidamberKemererDistributionBuilder.class, FunctionsDecorator.class); + return Arrays.asList( + SquidSensor.class, + SquidRuleRepository.class, + JavaSourceImporter.class, + FileComplexityDistributionDecorator.class, + FunctionComplexityDistributionBuilder.class, + ClassesDecorator.class, + ChidamberKemererDistributionBuilder.class, + FunctionsDecorator.class); } } 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/FileComplexityDistributionDecorator.java index efc4049fbf2..2ac235dcd69 100644 --- 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/FileComplexityDistributionDecorator.java @@ -30,11 +30,11 @@ import org.sonar.api.resources.Resource; import org.sonar.api.resources.Scopes; /** - * @since 2.6 + * @since 2.15 */ -public final class ClassComplexityDistributionBuilder implements Decorator { +public class FileComplexityDistributionDecorator implements Decorator { - private static final Number[] LIMITS = { 0, 5, 10, 20, 30, 60, 90 }; + private static final Number[] LIMITS = {0, 5, 10, 20, 30, 60, 90}; @DependsUpon public Metric dependOnComplexity() { @@ -42,32 +42,28 @@ public final class ClassComplexityDistributionBuilder implements Decorator { } @DependedUpon - public Metric generatesFunctionComplexityDistribution() { - return CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION; + public Metric generatesComplexityDistribution() { + return CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION; } + public boolean shouldExecuteOnProject(Project project) { + return Java.KEY.equals(project.getLanguageKey()); + } + + private static boolean shouldExecuteOn(Resource resource, DecoratorContext context) { + return Scopes.isFile(resource) && context.getMeasure(CoreMetrics.COMPLEXITY) != null; + } + + @Override 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.isProgramUnit(childContext.getResource())) { - Measure complexity = childContext.getMeasure(CoreMetrics.COMPLEXITY); - if (complexity != null) { - builder.add(complexity.getValue()); - } - } - } + RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, LIMITS); + Measure complexity = context.getMeasure(CoreMetrics.COMPLEXITY); + 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/test/java/org/sonar/plugins/squid/SquidPluginTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java index 42d793b2f87..c42ac5ecb76 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 @@ -19,11 +19,11 @@ */ package org.sonar.plugins.squid; +import org.junit.Test; + import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -import org.junit.Test; - public class SquidPluginTest { @Test 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 deleted file mode 100644 index 6af44b077f0..00000000000 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 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.plugins.squid.decorators; - -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -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 java.util.Arrays; -import java.util.List; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -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)); - } - - @Test - public void shouldCalculateDistributionOnFile() throws Exception { - - List<DecoratorContext> children = Arrays.asList( - // first range - newClassChild("One", 2.0), newClassChild("Two", 1.0), newClassChild("Zero complexity", 0.0), - - // second range - newClassChild("Three", 8.0), - - // out of range - newClassChild("No complexity", null)); - - DecoratorContext context = mock(DecoratorContext.class); - when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 20.0)); - when(context.getChildren()).thenReturn(children); - - ClassComplexityDistributionBuilder builder = new ClassComplexityDistributionBuilder(); - builder.decorate(JavaFile.fromRelativePath("org/foo/MyFile.java", false), context); - - verify(context).saveMeasure(argThat(new BaseMatcher<Measure>() { - public boolean matches(Object o) { - Measure measure = (Measure) o; - return measure.getMetric().equals(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION) && - measure.getData().equals("0=3;5=1;10=0;20=0;30=0;60=0;90=0"); - } - - public void describeTo(Description description) { - - } - })); - } - - private DecoratorContext newClassChild(String classname, Double complexity) { - DecoratorContext context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(JavaClass.create(classname)); - when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, complexity)); - return context; - } -} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/FileComplexityDistributionDecoratorTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/FileComplexityDistributionDecoratorTest.java new file mode 100644 index 00000000000..f0adaa85f9c --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/FileComplexityDistributionDecoratorTest.java @@ -0,0 +1,87 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 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.plugins.squid.decorators; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.measures.PersistenceMode; +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 static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class FileComplexityDistributionDecoratorTest { + + private FileComplexityDistributionDecorator decorator; + + @Before + public void setUp() { + decorator = new FileComplexityDistributionDecorator(); + } + + @Test + public void shouldExecuteOnJavaProjectsOnly() throws Exception { + assertThat(decorator.shouldExecuteOnProject(new Project("java").setLanguageKey(Java.KEY)), is(true)); + assertThat(decorator.shouldExecuteOnProject(new Project("php").setLanguageKey("php")), is(false)); + } + + @Test + public void shouldExecuteOnFilesOnly() throws Exception { + DecoratorContext context = mock(DecoratorContext.class); + when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 20.0)); + + decorator.decorate(new JavaPackage("org.foo"), context); + verify(context, never()).saveMeasure(any(Measure.class)); + } + + @Test + public void shouldDependOnComplexity() { + DecoratorContext context = mock(DecoratorContext.class); + assertThat(decorator.dependOnComplexity(), is(CoreMetrics.COMPLEXITY)); + assertThat(decorator.generatesComplexityDistribution(), is(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION)); + + decorator.decorate(new JavaFile("org.foo.Bar"), context); + verify(context, never()).saveMeasure(any(Measure.class)); + } + + @Test + public void shouldCalculateDistributionOnFile() throws Exception { + DecoratorContext context = mock(DecoratorContext.class); + when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 20.0)); + + decorator.decorate(JavaFile.fromRelativePath("org/foo/MyFile.java", false), context); + ArgumentCaptor<Measure> measureCaptor = ArgumentCaptor.forClass(Measure.class); + verify(context).saveMeasure(measureCaptor.capture()); + Measure measure = measureCaptor.getValue(); + assertThat(measure.getMetric(), is(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION)); + assertThat(measure.getData(), is("0=0;5=0;10=0;20=1;30=0;60=0;90=0")); + assertThat(measure.getPersistenceMode(), is(PersistenceMode.MEMORY)); + } + +} |