diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-07-20 16:40:18 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-07-21 15:59:45 +0200 |
commit | cabe5c343615115d51fedc1c36e5f7a85fe9782a (patch) | |
tree | 403bb28fd268da75e27bc96d691e3bafb1b216a6 | |
parent | ba624912e734f614010752afefbce84ba0d73cff (diff) | |
download | sonarqube-cabe5c343615115d51fedc1c36e5f7a85fe9782a.tar.gz sonarqube-cabe5c343615115d51fedc1c36e5f7a85fe9782a.zip |
SONAR-6679 Remove computation of language distribution measures from batch
7 files changed, 2 insertions, 509 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java index 435c547020f..e372cabd17c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java @@ -27,7 +27,6 @@ import org.sonar.batch.compute.FilesDecorator; import org.sonar.batch.compute.UnitTestDecorator; import org.sonar.batch.cpd.CpdComponents; import org.sonar.batch.issue.tracking.IssueTracking; -import org.sonar.batch.language.LanguageDistributionDecorator; import org.sonar.batch.scan.report.ConsoleReport; import org.sonar.batch.scan.report.HtmlReport; import org.sonar.batch.scan.report.IssuesReportBuilder; @@ -67,14 +66,11 @@ public class BatchComponents { SourceProvider.class, RuleNameProvider.class, - // language - LanguageDistributionDecorator.class, - // to be moved to compute engine UnitTestDecorator.class, DirectoriesDecorator.class, FilesDecorator.class - ); + ); components.addAll(CorePropertyDefinitions.all()); // CPD components.addAll(CpdComponents.all()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/language/LanguageDistributionDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/language/LanguageDistributionDecorator.java deleted file mode 100644 index ab9fb78b8ff..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/language/LanguageDistributionDecorator.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.batch.language; - -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.CoreMetrics; -import org.sonar.api.measures.CountDistributionBuilder; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; - -public class LanguageDistributionDecorator implements Decorator { - - private static final String UNKNOWN_LANGUAGE_KEY = "<null>"; - - @Override - public boolean shouldExecuteOnProject(Project project) { - return true; - } - - @DependsUpon - public Metric dependsUponMetric() { - return CoreMetrics.LINES; - } - - @DependedUpon - public Metric generatesMetric() { - return CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION; - } - - @Override - public void decorate(Resource resource, DecoratorContext context) { - CountDistributionBuilder nclocDistribution = new CountDistributionBuilder(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION); - if (ResourceUtils.isFile(resource)) { - Language language = resource.getLanguage(); - Measure ncloc = context.getMeasure(CoreMetrics.NCLOC); - if (ncloc != null) { - nclocDistribution.add(language != null ? language.getKey() : UNKNOWN_LANGUAGE_KEY, ncloc.getIntValue()); - } - } else { - for (Measure measure : context.getChildrenMeasures(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION)) { - nclocDistribution.add(measure); - } - } - Measure measure = nclocDistribution.build(false); - if (measure != null) { - context.saveMeasure(measure); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/language/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/language/package-info.java deleted file mode 100644 index 78142a00deb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/language/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.language; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/test/java/org/sonar/batch/language/LanguageDistributionDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/language/LanguageDistributionDecoratorTest.java deleted file mode 100644 index aa351984b5a..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/language/LanguageDistributionDecoratorTest.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.batch.language; - -import com.google.common.collect.ImmutableMap; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.Scopes; -import org.sonar.api.utils.KeyValueFormat; - -import java.util.Collections; - -import static com.google.common.collect.Lists.newArrayList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class LanguageDistributionDecoratorTest { - - @Mock - DecoratorContext context; - - @Mock - Resource resource; - - @Captor - ArgumentCaptor<Measure> measureCaptor; - - LanguageDistributionDecorator decorator; - - @Before - public void setUp() { - decorator = new LanguageDistributionDecorator(); - } - - @Test - public void depended_upon_metric() { - assertThat(decorator.generatesMetric()).isEqualTo(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION); - } - - @Test - public void depens_upon_metric() { - assertThat(decorator.dependsUponMetric()).isEqualTo(CoreMetrics.LINES); - } - - @Test - public void save_ncloc_language_distribution_on_file() { - Language language = mock(Language.class); - when(language.getKey()).thenReturn("xoo"); - - when(resource.getScope()).thenReturn(Scopes.FILE); - when(resource.getLanguage()).thenReturn(language); - when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0)); - - decorator.decorate(resource, context); - - verify(context).saveMeasure(measureCaptor.capture()); - - Measure result = measureCaptor.getValue(); - assertThat(result.getMetric()).isEqualTo(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION); - assertThat(result.getData()).isEqualTo("xoo=200"); - } - - @Test - public void save_ncloc_language_distribution_on_file_without_language() { - - when(resource.getScope()).thenReturn(Scopes.FILE); - when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0)); - - decorator.decorate(resource, context); - - verify(context).saveMeasure(measureCaptor.capture()); - - Measure result = measureCaptor.getValue(); - assertThat(result.getMetric()).isEqualTo(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION); - assertThat(result.getData()).isEqualTo("<null>=200"); - } - - @Test - public void save_ncloc_language_distribution_on_project() { - when(resource.getScope()).thenReturn(Scopes.PROJECT); - when(context.getChildrenMeasures(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION)).thenReturn(newArrayList( - new Measure(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION, KeyValueFormat.format(ImmutableMap.of("java", 20))), - new Measure(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION, KeyValueFormat.format(ImmutableMap.of("xoo", 150))), - new Measure(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION, KeyValueFormat.format(ImmutableMap.of("xoo", 50))) - )); - - decorator.decorate(resource, context); - - verify(context).saveMeasure(measureCaptor.capture()); - - Measure result = measureCaptor.getValue(); - assertThat(result.getMetric()).isEqualTo(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION); - assertThat(result.getData()).isEqualTo("java=20;xoo=200"); - } - - @Test - public void not_save_language_distribution_on_file_if_no_measure() { - Language language = mock(Language.class); - when(language.getKey()).thenReturn("xoo"); - - when(resource.getScope()).thenReturn(Scopes.FILE); - when(resource.getLanguage()).thenReturn(language); - when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(null); - - decorator.decorate(resource, context); - - verify(context, never()).saveMeasure(measureCaptor.capture()); - } - - @Test - public void not_save_language_distribution_on_project_if_no_chidren_measures() { - when(resource.getScope()).thenReturn(Scopes.PROJECT); - when(context.getChildrenMeasures(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION)).thenReturn(Collections.<Measure>emptyList()); - - decorator.decorate(resource, context); - - verify(context, never()).saveMeasure(measureCaptor.capture()); - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java index 58cd7c18c3c..001673e63f8 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java @@ -67,7 +67,7 @@ public class MeasuresMediumTest { .newScanTask(new File(tmpDir, "sonar-project.properties")) .start(); - assertThat(result.allMeasures()).hasSize(43); + assertThat(result.allMeasures()).hasSize(37); } @Test diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java deleted file mode 100644 index 528d62312de..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.measures; - -import org.apache.commons.collections.SortedBag; -import org.apache.commons.collections.bag.TreeBag; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.math.NumberUtils; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.api.utils.SonarException; - -import java.util.Map; - -/** - * Utility to build a distribution based on discrete values - * - * <p>An example of usage : you wish to record the number of violations for each level of rules priority</p> - * - * @since 1.10 - */ -public class CountDistributionBuilder implements MeasureBuilder { - - private Metric metric; - // TODO to be replaced by com.google.common.collect.SortedMultiset while upgrading to Guava 11+ - private SortedBag countBag; - - /** - * Creates an empty CountDistributionBuilder for a specified metric - * - * @param metric the metric - */ - public CountDistributionBuilder(Metric metric) { - setMetric(metric); - this.countBag = new TreeBag(); - } - - /** - * Increments an entry - * - * @param value the value that should be incremented - * @param count the number by which to increment - * @return the current object - */ - public CountDistributionBuilder add(Object value, int count) { - if (count == 0) { - addZero(value); - - } else { - if (this.countBag.add(value, count)) { - //hack - this.countBag.add(value, 1); - } - } - return this; - } - - /** - * Increments an entry by one - * - * @param value the value that should be incremented - * @return the current object - */ - public CountDistributionBuilder add(Object value) { - return add(value, 1); - } - - /** - * Adds an entry without a zero count if it does not exist - * - * @param value the entry to be added - * @return the current object - */ - public CountDistributionBuilder addZero(Object value) { - if (!countBag.contains(value)) { - countBag.add(value, 1); - } - return this; - } - - /** - * Adds an existing Distribution to the current one. - * It will create the entries if they don't exist. - * Can be used to add the values of children resources for example - * - * @param measure the measure to add to the current one - * @return the current object - */ - public CountDistributionBuilder add(Measure measure) { - if (measure != null && measure.getData() != null) { - Map<String, String> map = KeyValueFormat.parse(measure.getData()); - for (Map.Entry<String, String> entry : map.entrySet()) { - String key = entry.getKey(); - int value = StringUtils.isBlank(entry.getValue()) ? 0 : Integer.parseInt(entry.getValue()); - if (NumberUtils.isNumber(key)) { - add(NumberUtils.toInt(key), value); - } else { - add(key, value); - } - } - } - return this; - } - - /** - * @return whether the current object is empty or not - */ - public boolean isEmpty() { - return countBag.isEmpty(); - } - - /** - * Resets all entries to zero - * - * @return the current object - */ - public CountDistributionBuilder clear() { - countBag.clear(); - return this; - } - - /** - * Shortcut for <code>build(true)</code> - * - * @return the built measure - */ - @Override - public Measure build() { - return build(true); - } - - /** - * Used to build a measure from the current object - * - * @param allowEmptyData should be built if current object is empty - * @return the built measure - */ - public Measure build(boolean allowEmptyData) { - if (!isEmpty() || allowEmptyData) { - //-1 is a hack to include zero values - return new Measure(metric, KeyValueFormat.format(countBag, -1)); - } - return null; - } - - private void setMetric(Metric metric) { - if (metric == null || !metric.isDataType()) { - throw new SonarException("Metric is null or has unvalid type"); - } - this.metric = metric; - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/CountDistributionBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/CountDistributionBuilderTest.java deleted file mode 100644 index 4e15d1ed2fe..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/CountDistributionBuilderTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.measures; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import org.junit.Test; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class CountDistributionBuilderTest { - @Test - public void buildDistribution() { - CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); - Measure measure = builder - .add("foo") - .add("bar") - .add("foo") - .add("hello") - .build(); - - assertThat(measure.getData(), is("bar=1;foo=2;hello=1")); - } - - @Test - public void addZeroValues() { - CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); - Measure measure = builder - .addZero("foo") - .add("bar") - .add("foo") - .addZero("hello") - .build(); - - assertThat(measure.getData(), is("bar=1;foo=1;hello=0")); - } - - @Test - public void addDistributionMeasureAsStrings() { - Measure measureToAdd = mock(Measure.class); - when(measureToAdd.getData()).thenReturn("foo=3;hello=5;none=0"); - - CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); - Measure measure = builder - .add("bar") - .add("foo") - .add(measureToAdd) - .build(); - - assertThat(measure.getData(), is("bar=1;foo=4;hello=5;none=0")); - } - - @Test - public void intervalsAreSorted() { - Measure measureToAdd = mock(Measure.class); - when(measureToAdd.getData()).thenReturn("10=5;3=2;1=3"); - - CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); - Measure measure = builder - .add(10) - .add(measureToAdd) - .add(1) - .build(); - - assertThat(measure.getData(), is("1=4;3=2;10=6")); - } - -} |