diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-07-14 18:19:37 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-07-14 18:19:52 +0200 |
commit | 8d542803129dc572828a4a12174c5c4d2c1c81c8 (patch) | |
tree | f539237e35b66ce4978e07a130078076c0678c3a /plugins | |
parent | 75eeadcdaa600a5d2e2729125f7258ebddeb8101 (diff) | |
download | sonarqube-8d542803129dc572828a4a12174c5c4d2c1c81c8.tar.gz sonarqube-8d542803129dc572828a4a12174c5c4d2c1c81c8.zip |
SONAR-2610 web service for manual measures + copy manual measures during analysis
Diffstat (limited to 'plugins')
4 files changed, 133 insertions, 0 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index 5b4358eb5cb..807e8834852 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -214,6 +214,7 @@ public class CorePlugin extends SonarPlugin { extensions.add(FilesDecorator.class); extensions.add(CloseReviewsDecorator.class); extensions.add(ReferenceAnalysis.class); + extensions.add(ManualMeasureDecorator.class); // time machine extensions.add(TendencyDecorator.class); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ManualMeasureDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ManualMeasureDecorator.java new file mode 100644 index 00000000000..5d26067deee --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ManualMeasureDecorator.java @@ -0,0 +1,69 @@ +/* + * 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.plugins.core.sensors; + +import org.sonar.api.batch.Decorator; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.batch.Phase; +import org.sonar.api.database.DatabaseSession; +import org.sonar.api.measures.Measure; +import org.sonar.api.measures.MetricFinder; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.jpa.entity.ManualMeasure; + +import java.util.List; + +@Phase(name = Phase.Name.PRE) +public class ManualMeasureDecorator implements Decorator { + + private DatabaseSession session; + private MetricFinder metricFinder; + + public ManualMeasureDecorator(DatabaseSession session, MetricFinder metricFinder) { + this.session = session; + this.metricFinder = metricFinder; + } + + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + public void decorate(Resource resource, DecoratorContext context) { + if (resource.getId() != null) { + List<ManualMeasure> manualMeasures = session.getResults(ManualMeasure.class, "resourceId", resource.getId()); + for (ManualMeasure manualMeasure : manualMeasures) { + context.saveMeasure(copy(manualMeasure)); + } + } + } + + private Measure copy(ManualMeasure manualMeasure) { + Measure measure = new Measure(metricFinder.findById(manualMeasure.getMetricId())); + measure.setValue(manualMeasure.getValue(), 5); + measure.setData(manualMeasure.getTextValue()); + return measure; + } + + @Override + public String toString() { + return getClass().getSimpleName(); + } +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ManualMeasureDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ManualMeasureDecoratorTest.java new file mode 100644 index 00000000000..4ca1719de50 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ManualMeasureDecoratorTest.java @@ -0,0 +1,52 @@ +/* + * 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.plugins.core.sensors; + +import org.junit.Test; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.measures.Metric; +import org.sonar.api.resources.JavaFile; +import org.sonar.api.test.IsMeasure; +import org.sonar.core.components.DefaultMetricFinder; +import org.sonar.jpa.test.AbstractDbUnitTestCase; + +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class ManualMeasureDecoratorTest extends AbstractDbUnitTestCase { + + private Metric reviewNote = new Metric.Builder("review_note", "Note", Metric.ValueType.FLOAT).create().setId(2); + + @Test + public void testCopyManualMeasures() throws Exception { + setupData("testCopyManualMeasures"); + + JavaFile javaFile = new JavaFile("Foo.java"); + javaFile.setId(40); + + ManualMeasureDecorator decorator = new ManualMeasureDecorator(getSession(), new DefaultMetricFinder(getSessionFactory())); + DecoratorContext context = mock(DecoratorContext.class); + decorator.decorate(javaFile, context); + + verify(context).saveMeasure(argThat(new IsMeasure(reviewNote, 6.0, "six"))); + } + +} diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ManualMeasureDecoratorTest/testCopyManualMeasures.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ManualMeasureDecoratorTest/testCopyManualMeasures.xml new file mode 100644 index 00000000000..9c772983186 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/sensors/ManualMeasureDecoratorTest/testCopyManualMeasures.xml @@ -0,0 +1,11 @@ +<dataset> + <metrics id="1" NAME="ncloc" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name="" + enabled="true" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="0" hidden="false"/> + <metrics id="2" NAME="review_note" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name="" + enabled="true" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="0" hidden="false"/> + + + <manual_measures id="1" metric_id="2" resource_id="30" value="3.14" text_value="pi" created_at="[null]" updated_at="[null]"/> + <manual_measures id="2" metric_id="2" resource_id="40" value="6" text_value="six" created_at="[null]" updated_at="[null]"/> + +</dataset>
\ No newline at end of file |