import com.google.common.collect.Lists;
import org.apache.commons.lang.ClassUtils;
import org.sonar.api.batch.CheckProject;
-import org.sonar.api.batch.Sensor;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
import org.sonar.batch.scan.SensorWrapper;
*/
public class BatchExtensionDictionnary extends org.sonar.api.batch.BatchExtensionDictionnary {
- private AnalyzerContext context;
+ private SensorContext context;
private AnalyzerOptimizer analyzerOptimizer;
- public BatchExtensionDictionnary(ComponentContainer componentContainer, AnalyzerContext context, AnalyzerOptimizer analyzerOptimizer) {
+ public BatchExtensionDictionnary(ComponentContainer componentContainer, SensorContext context, AnalyzerOptimizer analyzerOptimizer) {
super(componentContainer);
this.context = context;
this.analyzerOptimizer = analyzerOptimizer;
private <T> List<T> getFilteredExtensions(Class<T> type, @Nullable Project project, @Nullable ExtensionMatcher matcher) {
List<T> result = Lists.newArrayList();
for (Object extension : getExtensions(type)) {
- if (Sensor.class.equals(type) && extension instanceof Analyzer) {
- extension = new SensorWrapper((Analyzer) extension, context, analyzerOptimizer);
+ if (org.sonar.api.batch.Sensor.class.equals(type) && extension instanceof Sensor) {
+ extension = new SensorWrapper((Sensor) extension, context, analyzerOptimizer);
}
if (shouldKeep(type, extension, project, matcher)) {
result.add((T) extension);
}
}
- if (Sensor.class.equals(type)) {
+ if (org.sonar.api.batch.Sensor.class.equals(type)) {
// Retrieve Analyzer and wrap then in SensorWrapper
- for (Object extension : getExtensions(Analyzer.class)) {
- extension = new SensorWrapper((Analyzer) extension, context, analyzerOptimizer);
+ for (Object extension : getExtensions(Sensor.class)) {
+ extension = new SensorWrapper((Sensor) extension, context, analyzerOptimizer);
if (shouldKeep(type, extension, project, matcher)) {
result.add((T) extension);
}
private boolean shouldKeep(Class type, Object extension, @Nullable Project project, @Nullable ExtensionMatcher matcher) {
boolean keep = (ClassUtils.isAssignable(extension.getClass(), type)
- || (Sensor.class.equals(type) && ClassUtils.isAssignable(extension.getClass(), Analyzer.class)))
+ || (Sensor.class.equals(type) && ClassUtils.isAssignable(extension.getClass(), Sensor.class)))
&& (matcher == null || matcher.accept(extension));
if (keep && project != null && ClassUtils.isAssignable(extension.getClass(), CheckProject.class)) {
keep = ((CheckProject) extension).shouldExecuteOnProject(project);
*/
package org.sonar.batch.mediumtest;
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.measure.Measure;
+
import org.apache.commons.io.IOUtils;
import org.sonar.api.SonarPlugin;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.batch.debt.internal.DefaultDebtModel;
import org.sonar.api.batch.fs.InputFile;
}
public static class TaskResult implements ScanTaskObserver {
- private List<AnalyzerIssue> issues = new ArrayList<AnalyzerIssue>();
- private List<AnalyzerMeasure> measures = new ArrayList<AnalyzerMeasure>();
+ private List<Issue> issues = new ArrayList<Issue>();
+ private List<Measure> measures = new ArrayList<Measure>();
private List<InputFile> inputFiles = new ArrayList<InputFile>();
@Override
public void scanTaskCompleted(ProjectScanContainer container) {
- for (AnalyzerIssue issue : container.getComponentByType(AnalyzerIssueCache.class).all()) {
+ for (Issue issue : container.getComponentByType(AnalyzerIssueCache.class).all()) {
issues.add(issue);
}
- for (AnalyzerMeasure<?> measure : container.getComponentByType(AnalyzerMeasureCache.class).all()) {
+ for (Measure<?> measure : container.getComponentByType(AnalyzerMeasureCache.class).all()) {
measures.add(measure);
}
}
}
- public List<AnalyzerIssue> issues() {
+ public List<Issue> issues() {
return issues;
}
- public List<AnalyzerMeasure> measures() {
+ public List<Measure> measures() {
return measures;
}
*/
package org.sonar.batch.phases;
-import org.sonar.api.batch.analyzer.Analyzer;
-
import org.apache.commons.lang.ClassUtils;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.InstantiationStrategy;
@Override
public final boolean accept(Object extension) {
return ClassUtils.isAssignable(extension.getClass(), Sensor.class)
- && acceptSensor((Sensor) extension) || ClassUtils.isAssignable(extension.getClass(), Analyzer.class);
+ && acceptSensor((Sensor) extension) || ClassUtils.isAssignable(extension.getClass(), Sensor.class);
}
public abstract boolean acceptSensor(Sensor sensor);
+++ /dev/null
-/*
- * 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.scan;
-
-import org.sonar.api.batch.SensorContext;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssueBuilder;
-import org.sonar.api.batch.analyzer.issue.internal.DefaultAnalyzerIssueBuilder;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
-import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
-import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.Metric;
-import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.component.ResourcePerspectives;
-import org.sonar.api.config.Settings;
-import org.sonar.api.issue.Issuable;
-import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.measures.Formula;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.api.measures.PersistenceMode;
-import org.sonar.api.measures.SumChildDistributionFormula;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Project;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.resources.Scopes;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.core.issue.DefaultIssueBuilder;
-
-import java.io.Serializable;
-
-/**
- * Implements {@link AnalyzerContext} but forward everything to {@link SensorContext} for backward compatibility.
- *
- */
-public class AnalyzerContextAdaptor implements AnalyzerContext {
-
- private SensorContext sensorContext;
- private MetricFinder metricFinder;
- private Project project;
- private ResourcePerspectives perspectives;
- private Settings settings;
- private FileSystem fs;
- private ActiveRules activeRules;
-
- public AnalyzerContextAdaptor(SensorContext sensorContext, MetricFinder metricFinder, Project project, ResourcePerspectives perspectives,
- Settings settings, FileSystem fs, ActiveRules activeRules) {
- this.sensorContext = sensorContext;
- this.metricFinder = metricFinder;
- this.project = project;
- this.perspectives = perspectives;
- this.settings = settings;
- this.fs = fs;
- this.activeRules = activeRules;
- }
-
- @Override
- public Settings settings() {
- return settings;
- }
-
- @Override
- public FileSystem fileSystem() {
- return fs;
- }
-
- @Override
- public ActiveRules activeRules() {
- return activeRules;
- }
-
- @Override
- public <G extends Serializable> AnalyzerMeasureBuilder<G> measureBuilder() {
- return new DefaultAnalyzerMeasureBuilder<G>();
- }
-
- @Override
- public AnalyzerMeasure getMeasure(String metricKey) {
- Metric<?> m = findMetricOrFail(metricKey);
- return getMeasure(m);
- }
-
- @Override
- public <G extends Serializable> AnalyzerMeasure<G> getMeasure(Metric<G> metric) {
- org.sonar.api.measures.Metric<G> m = (org.sonar.api.measures.Metric<G>) findMetricOrFail(metric.key());
- Measure<G> measure = sensorContext.getMeasure(m);
- if (measure == null) {
- return null;
- }
- return this.<G>measureBuilder()
- .onProject()
- .forMetric(metric)
- .withValue(measure.value())
- .build();
- }
-
- @Override
- public AnalyzerMeasure getMeasure(InputFile file, String metricKey) {
- Metric<?> m = findMetricOrFail(metricKey);
- return getMeasure(file, m);
- }
-
- private Metric<?> findMetricOrFail(String metricKey) {
- Metric<?> m = metricFinder.findByKey(metricKey);
- if (m == null) {
- throw new IllegalStateException("Unknow metric with key: " + metricKey);
- }
- return m;
- }
-
- @Override
- public <G extends Serializable> AnalyzerMeasure<G> getMeasure(InputFile file, Metric<G> metric) {
- File fileRes = File.create(file.relativePath());
- org.sonar.api.measures.Metric<G> m = (org.sonar.api.measures.Metric<G>) findMetricOrFail(metric.key());
- Measure<G> measure = sensorContext.getMeasure(fileRes, m);
- if (measure == null) {
- return null;
- }
- return this.<G>measureBuilder()
- .onFile(file)
- .forMetric(metric)
- .withValue(measure.value())
- .build();
- }
-
- @Override
- public void addMeasure(AnalyzerMeasure<?> measure) {
- org.sonar.api.measures.Metric<?> m = metricFinder.findByKey(measure.metric().key());
- if (m == null) {
- throw new IllegalStateException("Unknow metric with key: " + measure.metric().key());
- }
-
- Measure measureToSave = new Measure(m);
- setValueAccordingToMetricType(measure, m, measureToSave);
- if (measure.inputFile() != null) {
- Formula formula = measure.metric() instanceof org.sonar.api.measures.Metric ?
- ((org.sonar.api.measures.Metric) measure.metric()).getFormula() : null;
- if (formula instanceof SumChildDistributionFormula
- && !Scopes.isHigherThanOrEquals(Scopes.FILE, ((SumChildDistributionFormula) formula).getMinimumScopeToPersist())) {
- measureToSave.setPersistenceMode(PersistenceMode.MEMORY);
- }
- sensorContext.saveMeasure(measure.inputFile(), measureToSave);
- } else {
- sensorContext.saveMeasure(measureToSave);
- }
- }
-
- private void setValueAccordingToMetricType(AnalyzerMeasure<?> measure, org.sonar.api.measures.Metric<?> m, Measure measureToSave) {
- switch (m.getType()) {
- case BOOL:
- measureToSave.setValue(Boolean.TRUE.equals(measure.value()) ? 1.0 : 0.0);
- break;
- case INT:
- case MILLISEC:
- measureToSave.setValue(Double.valueOf((Integer) measure.value()));
- break;
- case FLOAT:
- case PERCENT:
- case RATING:
- measureToSave.setValue((Double) measure.value());
- break;
- case STRING:
- case LEVEL:
- case DATA:
- case DISTRIB:
- measureToSave.setData((String) measure.value());
- break;
- case WORK_DUR:
- measureToSave.setValue(Double.valueOf((Long) measure.value()));
- break;
- default:
- if (m.isNumericType()) {
- measureToSave.setValue((Double) measure.value());
- } else if (m.isDataType()) {
- measureToSave.setData((String) measure.value());
- } else {
- throw new UnsupportedOperationException("Unsupported type :" + m.getType());
- }
- }
- }
-
- @Override
- public AnalyzerIssueBuilder issueBuilder() {
- return new DefaultAnalyzerIssueBuilder();
- }
-
- @Override
- public boolean addIssue(AnalyzerIssue issue) {
- Resource r;
- InputFile inputFile = issue.inputFile();
- if (inputFile != null) {
- r = File.create(inputFile.relativePath());
- } else {
- r = project;
- }
- Issuable issuable = perspectives.as(Issuable.class, r);
- return issuable.addIssue(toDefaultIssue(project.getKey(), r.getKey(), issue));
- }
-
- public static DefaultIssue toDefaultIssue(String projectKey, String componentKey, AnalyzerIssue issue) {
- return new DefaultIssueBuilder()
- .componentKey(componentKey)
- .projectKey(projectKey)
- .ruleKey(RuleKey.of(issue.ruleKey().repository(), issue.ruleKey().rule()))
- .effortToFix(issue.effortToFix())
- .line(issue.line())
- .message(issue.message())
- .build();
- }
-
-}
TimeMachineConfiguration.class,
DefaultSensorContext.class,
- AnalyzerContextAdaptor.class,
+ SensorContextAdaptor.class,
BatchExtensionDictionnary.class,
DefaultTimeMachine.class,
ViolationFilters.class,
--- /dev/null
+/*
+ * 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.scan;
+
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.issue.IssueBuilder;
+import org.sonar.api.batch.sensor.issue.internal.DefaultIssueBuilder;
+import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.measure.MeasureBuilder;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder;
+import org.sonar.api.component.ResourcePerspectives;
+import org.sonar.api.config.Settings;
+import org.sonar.api.issue.Issuable;
+import org.sonar.api.issue.internal.DefaultIssue;
+import org.sonar.api.measures.Formula;
+import org.sonar.api.measures.MetricFinder;
+import org.sonar.api.measures.PersistenceMode;
+import org.sonar.api.measures.SumChildDistributionFormula;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.Scopes;
+import org.sonar.api.rule.RuleKey;
+
+import java.io.Serializable;
+
+/**
+ * Implements {@link SensorContext} but forward everything to {@link org.sonar.api.batch.SensorContext} for backward compatibility.
+ *
+ */
+public class SensorContextAdaptor implements SensorContext {
+
+ private org.sonar.api.batch.SensorContext sensorContext;
+ private MetricFinder metricFinder;
+ private Project project;
+ private ResourcePerspectives perspectives;
+ private Settings settings;
+ private FileSystem fs;
+ private ActiveRules activeRules;
+
+ public SensorContextAdaptor(org.sonar.api.batch.SensorContext sensorContext, MetricFinder metricFinder, Project project, ResourcePerspectives perspectives,
+ Settings settings, FileSystem fs, ActiveRules activeRules) {
+ this.sensorContext = sensorContext;
+ this.metricFinder = metricFinder;
+ this.project = project;
+ this.perspectives = perspectives;
+ this.settings = settings;
+ this.fs = fs;
+ this.activeRules = activeRules;
+ }
+
+ @Override
+ public Settings settings() {
+ return settings;
+ }
+
+ @Override
+ public FileSystem fileSystem() {
+ return fs;
+ }
+
+ @Override
+ public ActiveRules activeRules() {
+ return activeRules;
+ }
+
+ @Override
+ public <G extends Serializable> MeasureBuilder<G> measureBuilder() {
+ return new DefaultMeasureBuilder<G>();
+ }
+
+ @Override
+ public Measure getMeasure(String metricKey) {
+ Metric<?> m = findMetricOrFail(metricKey);
+ return getMeasure(m);
+ }
+
+ @Override
+ public <G extends Serializable> Measure<G> getMeasure(Metric<G> metric) {
+ org.sonar.api.measures.Metric<G> m = (org.sonar.api.measures.Metric<G>) findMetricOrFail(metric.key());
+ org.sonar.api.measures.Measure<G> measure = sensorContext.getMeasure(m);
+ if (measure == null) {
+ return null;
+ }
+ return this.<G>measureBuilder()
+ .onProject()
+ .forMetric(metric)
+ .withValue(measure.value())
+ .build();
+ }
+
+ @Override
+ public Measure getMeasure(InputFile file, String metricKey) {
+ Metric<?> m = findMetricOrFail(metricKey);
+ return getMeasure(file, m);
+ }
+
+ private Metric<?> findMetricOrFail(String metricKey) {
+ Metric<?> m = metricFinder.findByKey(metricKey);
+ if (m == null) {
+ throw new IllegalStateException("Unknow metric with key: " + metricKey);
+ }
+ return m;
+ }
+
+ @Override
+ public <G extends Serializable> Measure<G> getMeasure(InputFile file, Metric<G> metric) {
+ File fileRes = File.create(file.relativePath());
+ org.sonar.api.measures.Metric<G> m = (org.sonar.api.measures.Metric<G>) findMetricOrFail(metric.key());
+ org.sonar.api.measures.Measure<G> measure = sensorContext.getMeasure(fileRes, m);
+ if (measure == null) {
+ return null;
+ }
+ return this.<G>measureBuilder()
+ .onFile(file)
+ .forMetric(metric)
+ .withValue(measure.value())
+ .build();
+ }
+
+ @Override
+ public void addMeasure(Measure<?> measure) {
+ org.sonar.api.measures.Metric<?> m = metricFinder.findByKey(measure.metric().key());
+ if (m == null) {
+ throw new IllegalStateException("Unknow metric with key: " + measure.metric().key());
+ }
+
+ org.sonar.api.measures.Measure measureToSave = new org.sonar.api.measures.Measure(m);
+ setValueAccordingToMetricType(measure, m, measureToSave);
+ if (measure.inputFile() != null) {
+ Formula formula = measure.metric() instanceof org.sonar.api.measures.Metric ?
+ ((org.sonar.api.measures.Metric) measure.metric()).getFormula() : null;
+ if (formula instanceof SumChildDistributionFormula
+ && !Scopes.isHigherThanOrEquals(Scopes.FILE, ((SumChildDistributionFormula) formula).getMinimumScopeToPersist())) {
+ measureToSave.setPersistenceMode(PersistenceMode.MEMORY);
+ }
+ sensorContext.saveMeasure(measure.inputFile(), measureToSave);
+ } else {
+ sensorContext.saveMeasure(measureToSave);
+ }
+ }
+
+ private void setValueAccordingToMetricType(Measure<?> measure, org.sonar.api.measures.Metric<?> m, org.sonar.api.measures.Measure measureToSave) {
+ switch (m.getType()) {
+ case BOOL:
+ measureToSave.setValue(Boolean.TRUE.equals(measure.value()) ? 1.0 : 0.0);
+ break;
+ case INT:
+ case MILLISEC:
+ measureToSave.setValue(Double.valueOf((Integer) measure.value()));
+ break;
+ case FLOAT:
+ case PERCENT:
+ case RATING:
+ measureToSave.setValue((Double) measure.value());
+ break;
+ case STRING:
+ case LEVEL:
+ case DATA:
+ case DISTRIB:
+ measureToSave.setData((String) measure.value());
+ break;
+ case WORK_DUR:
+ measureToSave.setValue(Double.valueOf((Long) measure.value()));
+ break;
+ default:
+ if (m.isNumericType()) {
+ measureToSave.setValue((Double) measure.value());
+ } else if (m.isDataType()) {
+ measureToSave.setData((String) measure.value());
+ } else {
+ throw new UnsupportedOperationException("Unsupported type :" + m.getType());
+ }
+ }
+ }
+
+ @Override
+ public IssueBuilder issueBuilder() {
+ return new DefaultIssueBuilder();
+ }
+
+ @Override
+ public boolean addIssue(Issue issue) {
+ Resource r;
+ InputFile inputFile = issue.inputFile();
+ if (inputFile != null) {
+ r = File.create(inputFile.relativePath());
+ } else {
+ r = project;
+ }
+ Issuable issuable = perspectives.as(Issuable.class, r);
+ return issuable.addIssue(toDefaultIssue(project.getKey(), r.getKey(), issue));
+ }
+
+ public static DefaultIssue toDefaultIssue(String projectKey, String componentKey, Issue issue) {
+ return new org.sonar.core.issue.DefaultIssueBuilder()
+ .componentKey(componentKey)
+ .projectKey(projectKey)
+ .ruleKey(RuleKey.of(issue.ruleKey().repository(), issue.ruleKey().rule()))
+ .effortToFix(issue.effortToFix())
+ .line(issue.line())
+ .message(issue.message())
+ .build();
+ }
+
+}
import org.sonar.api.batch.DependedUpon;
import org.sonar.api.batch.DependsUpon;
-import org.sonar.api.batch.Sensor;
-import org.sonar.api.batch.SensorContext;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.api.batch.measure.Metric;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.resources.Project;
import org.sonar.batch.scan2.AnalyzerOptimizer;
import java.util.Arrays;
import java.util.List;
-public class SensorWrapper implements Sensor {
+public class SensorWrapper implements org.sonar.api.batch.Sensor {
- private Analyzer analyzer;
- private AnalyzerContext adaptor;
- private DefaultAnalyzerDescriptor descriptor;
+ private Sensor analyzer;
+ private SensorContext adaptor;
+ private DefaultSensorDescriptor descriptor;
private AnalyzerOptimizer optimizer;
- public SensorWrapper(Analyzer analyzer, AnalyzerContext adaptor, AnalyzerOptimizer optimizer) {
+ public SensorWrapper(Sensor analyzer, SensorContext adaptor, AnalyzerOptimizer optimizer) {
this.analyzer = analyzer;
this.optimizer = optimizer;
- descriptor = new DefaultAnalyzerDescriptor();
+ descriptor = new DefaultSensorDescriptor();
analyzer.describe(descriptor);
this.adaptor = adaptor;
}
}
@Override
- public void analyse(Project module, SensorContext context) {
+ public void analyse(Project module, org.sonar.api.batch.SensorContext context) {
analyzer.analyse(adaptor);
}
}
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.measure.Measure;
+
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
jsonWriter
.beginObject().name("issues")
.beginArray();
- for (AnalyzerIssue issue : issueCache.byModule(def.getKey())) {
+ for (Issue issue : issueCache.byModule(def.getKey())) {
jsonWriter.beginObject()
.prop("repository", issue.ruleKey().repository())
.prop("rule", issue.ruleKey().rule());
jsonWriter
.beginObject().name("measures")
.beginArray();
- for (AnalyzerMeasure<?> measure : measureCache.byModule(def.getKey())) {
+ for (Measure<?> measure : measureCache.byModule(def.getKey())) {
jsonWriter.beginObject()
.prop("metricKey", measure.metric().key());
if (measure.inputFile() != null) {
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
+
import org.sonar.api.BatchComponent;
-import org.sonar.api.batch.analyzer.issue.internal.DefaultAnalyzerIssue;
import org.sonar.batch.index.Cache;
import org.sonar.batch.index.Caches;
public class AnalyzerIssueCache implements BatchComponent {
// project key -> resource key -> issue key -> issue
- private final Cache<DefaultAnalyzerIssue> cache;
+ private final Cache<DefaultIssue> cache;
public AnalyzerIssueCache(Caches caches) {
cache = caches.createCache("issues");
}
- public Iterable<DefaultAnalyzerIssue> byComponent(String projectKey, String resourceKey) {
+ public Iterable<DefaultIssue> byComponent(String projectKey, String resourceKey) {
return cache.values(projectKey, resourceKey);
}
- public Iterable<DefaultAnalyzerIssue> all() {
+ public Iterable<DefaultIssue> all() {
return cache.values();
}
- public AnalyzerIssueCache put(String projectKey, String resourceKey, DefaultAnalyzerIssue issue) {
+ public AnalyzerIssueCache put(String projectKey, String resourceKey, DefaultIssue issue) {
cache.put(projectKey, resourceKey, issue.key(), issue);
return this;
}
- public Iterable<DefaultAnalyzerIssue> byModule(String projectKey) {
+ public Iterable<DefaultIssue> byModule(String projectKey) {
return cache.values(projectKey);
}
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
+
import com.google.common.base.Preconditions;
import org.sonar.api.BatchComponent;
-import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasure;
import org.sonar.batch.index.Cache;
import org.sonar.batch.index.Cache.Entry;
import org.sonar.batch.index.Caches;
public class AnalyzerMeasureCache implements BatchComponent {
// project key -> component key -> metric key -> measure
- private final Cache<DefaultAnalyzerMeasure> cache;
+ private final Cache<DefaultMeasure> cache;
public AnalyzerMeasureCache(Caches caches) {
cache = caches.createCache("measures");
}
- public Iterable<Entry<DefaultAnalyzerMeasure>> entries() {
+ public Iterable<Entry<DefaultMeasure>> entries() {
return cache.entries();
}
- public Iterable<DefaultAnalyzerMeasure> byModule(String projectKey) {
+ public Iterable<DefaultMeasure> byModule(String projectKey) {
return cache.values(projectKey);
}
- public DefaultAnalyzerMeasure<?> byMetric(String projectKey, String resourceKey, String metricKey) {
+ public DefaultMeasure<?> byMetric(String projectKey, String resourceKey, String metricKey) {
return cache.get(projectKey, resourceKey, metricKey);
}
- public AnalyzerMeasureCache put(String projectKey, String resourceKey, DefaultAnalyzerMeasure<?> measure) {
+ public AnalyzerMeasureCache put(String projectKey, String resourceKey, DefaultMeasure<?> measure) {
Preconditions.checkNotNull(projectKey);
Preconditions.checkNotNull(resourceKey);
Preconditions.checkNotNull(measure);
return this;
}
- public boolean contains(String projectKey, String resourceKey, DefaultAnalyzerMeasure<?> measure) {
+ public boolean contains(String projectKey, String resourceKey, DefaultMeasure<?> measure) {
Preconditions.checkNotNull(projectKey);
Preconditions.checkNotNull(resourceKey);
Preconditions.checkNotNull(measure);
return cache.containsKey(projectKey, resourceKey, measure.metric().key());
}
- public Iterable<DefaultAnalyzerMeasure> all() {
+ public Iterable<DefaultMeasure> all() {
return cache.values();
}
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+
import org.sonar.api.BatchComponent;
-import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
/**
* Decide if the given Analyzer should be executed.
*/
- public boolean shouldExecute(DefaultAnalyzerDescriptor descriptor) {
+ public boolean shouldExecute(DefaultSensorDescriptor descriptor) {
// FS Conditions
boolean fsCondition = fsCondition(descriptor);
boolean activeRulesCondition = activeRulesCondition(descriptor);
return fsCondition && activeRulesCondition;
}
- private boolean activeRulesCondition(DefaultAnalyzerDescriptor descriptor) {
+ private boolean activeRulesCondition(DefaultSensorDescriptor descriptor) {
if (!descriptor.ruleRepositories().isEmpty()) {
for (String repoKey : descriptor.ruleRepositories()) {
if (!activeRules.findByRepository(repoKey).isEmpty()) {
return true;
}
- private boolean fsCondition(DefaultAnalyzerDescriptor descriptor) {
+ private boolean fsCondition(DefaultSensorDescriptor descriptor) {
if (!descriptor.languages().isEmpty() || !descriptor.types().isEmpty()) {
FilePredicate langPredicate = descriptor.languages().isEmpty() ? fs.predicates().all() : fs.predicates().hasLanguages(descriptor.languages());
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.batch.bootstrap.BatchExtensionDictionnary;
import java.util.Collection;
this.optimizer = optimizer;
}
- public void execute(AnalyzerContext context) {
- Collection<Analyzer> analyzers = selector.select(Analyzer.class, null, true, null);
+ public void execute(SensorContext context) {
+ Collection<Sensor> analyzers = selector.select(Sensor.class, null, true, null);
- for (Analyzer analyzer : analyzers) {
+ for (Sensor analyzer : analyzers) {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
analyzer.describe(descriptor);
if (!optimizer.shouldExecute(descriptor)) {
}
- private void executeSensor(AnalyzerContext context, Analyzer analyzer) {
+ private void executeSensor(SensorContext context, Sensor analyzer) {
analyzer.analyse(context);
}
*/
package org.sonar.batch.scan2;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssueBuilder;
-import org.sonar.api.batch.analyzer.issue.internal.DefaultAnalyzerIssue;
-import org.sonar.api.batch.analyzer.issue.internal.DefaultAnalyzerIssueBuilder;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
-import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasure;
-import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.issue.IssueBuilder;
+import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
+import org.sonar.api.batch.sensor.issue.internal.DefaultIssueBuilder;
+import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.measure.MeasureBuilder;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder;
+
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.config.Settings;
import org.sonar.batch.issue.IssueFilters;
-import org.sonar.batch.scan.AnalyzerContextAdaptor;
+import org.sonar.batch.scan.SensorContextAdaptor;
import org.sonar.core.component.ComponentKeys;
import java.io.Serializable;
-public class DefaultAnalyzerContext implements AnalyzerContext {
+public class DefaultAnalyzerContext implements SensorContext {
private final AnalyzerMeasureCache measureCache;
private final AnalyzerIssueCache issueCache;
}
@Override
- public <G extends Serializable> AnalyzerMeasureBuilder<G> measureBuilder() {
- return new DefaultAnalyzerMeasureBuilder<G>();
+ public <G extends Serializable> MeasureBuilder<G> measureBuilder() {
+ return new DefaultMeasureBuilder<G>();
}
@Override
- public AnalyzerMeasure getMeasure(String metricKey) {
+ public Measure getMeasure(String metricKey) {
return measureCache.byMetric(def.getKey(), def.getKey(), metricKey);
}
@Override
- public <G extends Serializable> AnalyzerMeasure<G> getMeasure(Metric<G> metric) {
- return (AnalyzerMeasure<G>) measureCache.byMetric(def.getKey(), def.getKey(), metric.key());
+ public <G extends Serializable> Measure<G> getMeasure(Metric<G> metric) {
+ return (Measure<G>) measureCache.byMetric(def.getKey(), def.getKey(), metric.key());
}
@Override
- public AnalyzerMeasure getMeasure(InputFile file, String metricKey) {
+ public Measure getMeasure(InputFile file, String metricKey) {
return measureCache.byMetric(def.getKey(), ComponentKeys.createEffectiveKey(def.getKey(), file), metricKey);
}
@Override
- public <G extends Serializable> AnalyzerMeasure<G> getMeasure(InputFile file, Metric<G> metric) {
- return (AnalyzerMeasure<G>) measureCache.byMetric(def.getKey(), ComponentKeys.createEffectiveKey(def.getKey(), file), metric.key());
+ public <G extends Serializable> Measure<G> getMeasure(InputFile file, Metric<G> metric) {
+ return (Measure<G>) measureCache.byMetric(def.getKey(), ComponentKeys.createEffectiveKey(def.getKey(), file), metric.key());
}
@Override
- public void addMeasure(AnalyzerMeasure<?> measure) {
+ public void addMeasure(Measure<?> measure) {
if (measure.inputFile() != null) {
- measureCache.put(def.getKey(), ComponentKeys.createEffectiveKey(def.getKey(), measure.inputFile()), (DefaultAnalyzerMeasure) measure);
+ measureCache.put(def.getKey(), ComponentKeys.createEffectiveKey(def.getKey(), measure.inputFile()), (DefaultMeasure) measure);
} else {
- measureCache.put(def.getKey(), def.getKey(), (DefaultAnalyzerMeasure) measure);
+ measureCache.put(def.getKey(), def.getKey(), (DefaultMeasure) measure);
}
}
@Override
- public AnalyzerIssueBuilder issueBuilder() {
- return new DefaultAnalyzerIssueBuilder();
+ public IssueBuilder issueBuilder() {
+ return new DefaultIssueBuilder();
}
@Override
- public boolean addIssue(AnalyzerIssue issue) {
+ public boolean addIssue(Issue issue) {
String resourceKey;
if (issue.inputFile() != null) {
resourceKey = ComponentKeys.createEffectiveKey(def.getKey(), issue.inputFile());
resourceKey = def.getKey();
}
// TODO Lot of things to do. See ModuleIssues::initAndAddIssue
- if (issueFilters.accept(AnalyzerContextAdaptor.toDefaultIssue(def.getKey(), resourceKey, issue), null)) {
- issueCache.put(def.getKey(), resourceKey, (DefaultAnalyzerIssue) issue);
+ if (issueFilters.accept(SensorContextAdaptor.toDefaultIssue(def.getKey(), resourceKey, issue), null)) {
+ issueCache.put(def.getKey(), resourceKey, (DefaultIssue) issue);
return true;
}
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder;
+
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.measures.FileLinesContext;
Map<Integer, Object> lines = entry.getValue();
if (shouldSave(lines)) {
String data = KeyValueFormat.format(lines);
- measureCache.put(projectKey, ComponentKeys.createEffectiveKey(projectKey, inputFile), new DefaultAnalyzerMeasureBuilder<String>()
+ measureCache.put(projectKey, ComponentKeys.createEffectiveKey(projectKey, inputFile), new DefaultMeasureBuilder<String>()
.forMetric(metric)
.onFile(inputFile)
.withValue(data)
}
private Map loadData(String metricKey, Converter converter) {
- AnalyzerMeasure measure = measureCache.byMetric(projectKey, ComponentKeys.createEffectiveKey(projectKey, inputFile), metricKey);
+ Measure measure = measureCache.byMetric(projectKey, ComponentKeys.createEffectiveKey(projectKey, inputFile), metricKey);
if (measure == null) {
// no such measure
return ImmutableMap.of();
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.SensorContext;
+
import com.google.common.collect.Lists;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader;
import org.sonar.batch.phases.SensorsExecutor;
public final class ModuleScanExecutor {
private final AnalyzersExecutor analyzersExecutor;
- private final AnalyzerContext analyzerContext;
+ private final SensorContext analyzerContext;
private final FileSystemLogger fsLogger;
private final DefaultModuleFileSystem fs;
private final QProfileVerifier profileVerifier;
private AnalysisPublisher analyzisPublisher;
public ModuleScanExecutor(AnalyzersExecutor analyzersExecutor,
- AnalyzerContext analyzerContext,
+ SensorContext analyzerContext,
FileSystemLogger fsLogger, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier,
IssueExclusionsLoader issueExclusionsLoader, AnalysisPublisher analyzisPublisher) {
this.analyzersExecutor = analyzersExecutor;
import org.junit.Test;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.Sensor;
-import org.sonar.api.batch.SensorContext;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
+import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
import org.sonar.batch.scan2.AnalyzerOptimizer;
for (BatchExtension extension : extensions) {
iocContainer.addSingleton(extension);
}
- return new BatchExtensionDictionnary(iocContainer, mock(AnalyzerContext.class), mock(AnalyzerOptimizer.class));
+ return new BatchExtensionDictionnary(iocContainer, mock(SensorContext.class), mock(AnalyzerOptimizer.class));
}
@Test
class FakeSensor implements Sensor {
- public void analyse(Project project, SensorContext context) {
+ @Override
+ public void analyse(Project project, org.sonar.api.batch.SensorContext context) {
}
+ @Override
public boolean shouldExecuteOnProject(Project project) {
return true;
}
*/
package org.sonar.batch.mediumtest.issues;
+import org.sonar.api.batch.sensor.issue.Issue;
+
import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.batch.mediumtest.AnalyzerMediumTester;
import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult;
assertThat(result.issues()).hasSize(20);
boolean foundIssueAtLine1 = false;
- for (AnalyzerIssue issue : result.issues()) {
+ for (Issue issue : result.issues()) {
if (issue.line() == 1) {
foundIssueAtLine1 = true;
assertThat(issue.inputFile()).isEqualTo(new DefaultInputFile("src/sample.xoo"));
*/
package org.sonar.batch.mediumtest.measures;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder;
+
import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.batch.mediumtest.AnalyzerMediumTester;
assertThat(result.measures()).hasSize(1);
- assertThat(result.measures()).contains(new DefaultAnalyzerMeasureBuilder<Integer>()
+ assertThat(result.measures()).contains(new DefaultMeasureBuilder<Integer>()
.forMetric(CoreMetrics.LINES)
.onFile(new DefaultInputFile("src/sample.xoo"))
.withValue(20)
assertThat(result.measures()).hasSize(4);
- assertThat(result.measures()).contains(new DefaultAnalyzerMeasureBuilder<Integer>()
+ assertThat(result.measures()).contains(new DefaultMeasureBuilder<Integer>()
.forMetric(CoreMetrics.LINES)
.onFile(new DefaultInputFile("src/sample.xoo"))
.withValue(5)
.build());
- assertThat(result.measures()).contains(new DefaultAnalyzerMeasureBuilder<String>()
+ assertThat(result.measures()).contains(new DefaultMeasureBuilder<String>()
.forMetric(CoreMetrics.SCM_AUTHORS_BY_LINE)
.onFile(new DefaultInputFile("src/sample.xoo"))
.withValue("1=julien;2=julien;3=julien;4=julien;5=simon")
import org.sonar.api.SonarPlugin;
import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
-import org.sonar.batch.mediumtest.xoo.plugin.lang.MeasureAnalyzer;
-import org.sonar.batch.mediumtest.xoo.plugin.lang.ScmActivityAnalyzer;
+import org.sonar.batch.mediumtest.xoo.plugin.lang.MeasureSensor;
+import org.sonar.batch.mediumtest.xoo.plugin.lang.ScmActivitySensor;
import org.sonar.batch.mediumtest.xoo.plugin.rule.OneIssuePerLineAnalyzer;
import java.util.Arrays;
public List getExtensions() {
return Arrays.asList(
// language
- MeasureAnalyzer.class,
- ScmActivityAnalyzer.class,
+ MeasureSensor.class,
+ ScmActivitySensor.class,
Xoo.class,
// rules
+++ /dev/null
-/*
- * 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.mediumtest.xoo.plugin.lang;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.analyzer.AnalyzerDescriptor;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.MetricFinder;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
-import org.sonar.batch.mediumtest.xoo.plugin.base.XooConstants;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.List;
-
-/**
- * Parse files *.xoo.measures
- */
-public class MeasureAnalyzer implements Analyzer {
-
- private static final String MEASURES_EXTENSION = ".measures";
-
- private MetricFinder metricFinder;
-
- public MeasureAnalyzer(MetricFinder metricFinder) {
- this.metricFinder = metricFinder;
- }
-
- private void processFileMeasures(InputFile inputFile, AnalyzerContext context) {
- File ioFile = inputFile.file();
- File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION);
- if (measureFile.exists()) {
- XooConstants.LOG.debug("Processing " + measureFile.getAbsolutePath());
- try {
- List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name());
- int lineNumber = 0;
- for (String line : lines) {
- lineNumber++;
- if (StringUtils.isBlank(line)) {
- continue;
- }
- if (line.startsWith("#")) {
- continue;
- }
- try {
- String metricKey = StringUtils.substringBefore(line, ":");
- String value = line.substring(metricKey.length() + 1);
- context.addMeasure(createMeasure(context, inputFile, metricKey, value));
- } catch (Exception e) {
- throw new IllegalStateException("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
- }
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- private AnalyzerMeasure<?> createMeasure(AnalyzerContext context, InputFile xooFile, String metricKey, String value) {
- org.sonar.api.batch.measure.Metric<Serializable> metric = metricFinder.findByKey(metricKey);
- AnalyzerMeasureBuilder<Serializable> builder = context.measureBuilder()
- .forMetric(metric)
- .onFile(xooFile);
- if (Boolean.class.equals(metric.valueType())) {
- builder.withValue(Boolean.parseBoolean(value));
- } else if (Integer.class.equals(metric.valueType())) {
- builder.withValue(Integer.valueOf(value));
- } else if (Double.class.equals(metric.valueType())) {
- builder.withValue(Double.valueOf(value));
- } else if (String.class.equals(metric.valueType())) {
- builder.withValue(value);
- } else if (Long.class.equals(metric.valueType())) {
- builder.withValue(Long.valueOf(value));
- } else {
- throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
- }
- return builder.build();
- }
-
- @Override
- public void describe(AnalyzerDescriptor descriptor) {
- descriptor
- .name("Xoo Measure Analyzer")
- .provides(CoreMetrics.LINES)
- .workOnLanguages(Xoo.KEY)
- .workOnFileTypes(InputFile.Type.MAIN, InputFile.Type.TEST);
- }
-
- @Override
- public void analyse(AnalyzerContext context) {
- for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
- processFileMeasures(file, context);
- }
- }
-}
--- /dev/null
+/*
+ * 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.mediumtest.xoo.plugin.lang;
+
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorDescriptor;
+import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.measure.MeasureBuilder;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.MetricFinder;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
+import org.sonar.batch.mediumtest.xoo.plugin.base.XooConstants;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * Parse files *.xoo.measures
+ */
+public class MeasureSensor implements Sensor {
+
+ private static final String MEASURES_EXTENSION = ".measures";
+
+ private MetricFinder metricFinder;
+
+ public MeasureSensor(MetricFinder metricFinder) {
+ this.metricFinder = metricFinder;
+ }
+
+ private void processFileMeasures(InputFile inputFile, SensorContext context) {
+ File ioFile = inputFile.file();
+ File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION);
+ if (measureFile.exists()) {
+ XooConstants.LOG.debug("Processing " + measureFile.getAbsolutePath());
+ try {
+ List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name());
+ int lineNumber = 0;
+ for (String line : lines) {
+ lineNumber++;
+ if (StringUtils.isBlank(line)) {
+ continue;
+ }
+ if (line.startsWith("#")) {
+ continue;
+ }
+ try {
+ String metricKey = StringUtils.substringBefore(line, ":");
+ String value = line.substring(metricKey.length() + 1);
+ context.addMeasure(createMeasure(context, inputFile, metricKey, value));
+ } catch (Exception e) {
+ throw new IllegalStateException("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
+ }
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private Measure<?> createMeasure(SensorContext context, InputFile xooFile, String metricKey, String value) {
+ org.sonar.api.batch.measure.Metric<Serializable> metric = metricFinder.findByKey(metricKey);
+ MeasureBuilder<Serializable> builder = context.measureBuilder()
+ .forMetric(metric)
+ .onFile(xooFile);
+ if (Boolean.class.equals(metric.valueType())) {
+ builder.withValue(Boolean.parseBoolean(value));
+ } else if (Integer.class.equals(metric.valueType())) {
+ builder.withValue(Integer.valueOf(value));
+ } else if (Double.class.equals(metric.valueType())) {
+ builder.withValue(Double.valueOf(value));
+ } else if (String.class.equals(metric.valueType())) {
+ builder.withValue(value);
+ } else if (Long.class.equals(metric.valueType())) {
+ builder.withValue(Long.valueOf(value));
+ } else {
+ throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
+ }
+ return builder.build();
+ }
+
+ @Override
+ public void describe(SensorDescriptor descriptor) {
+ descriptor
+ .name("Xoo Measure Analyzer")
+ .provides(CoreMetrics.LINES)
+ .workOnLanguages(Xoo.KEY)
+ .workOnFileTypes(InputFile.Type.MAIN, InputFile.Type.TEST);
+ }
+
+ @Override
+ public void analyse(SensorContext context) {
+ for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
+ processFileMeasures(file, context);
+ }
+ }
+}
+++ /dev/null
-/*
- * 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.mediumtest.xoo.plugin.lang;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.analyzer.AnalyzerDescriptor;
-import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.FileLinesContext;
-import org.sonar.api.measures.FileLinesContextFactory;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Date;
-import java.util.List;
-
-public class ScmActivityAnalyzer implements Analyzer {
-
- private static final Logger LOG = LoggerFactory.getLogger(ScmActivityAnalyzer.class);
-
- private static final String SCM_EXTENSION = ".scm";
-
- private final FileSystem fs;
- private final FileLinesContextFactory fileLinesContextFactory;
-
- public ScmActivityAnalyzer(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem) {
- this.fs = fileSystem;
- this.fileLinesContextFactory = fileLinesContextFactory;
- }
-
- @Override
- public void describe(AnalyzerDescriptor descriptor) {
- descriptor
- .name(this.getClass().getSimpleName())
- .provides(CoreMetrics.SCM_AUTHORS_BY_LINE,
- CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE,
- CoreMetrics.SCM_REVISIONS_BY_LINE)
- .workOnLanguages(Xoo.KEY);
- }
-
- @Override
- public void analyse(AnalyzerContext context) {
- for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(Xoo.KEY))) {
- processFile(inputFile);
- }
-
- }
-
- @VisibleForTesting
- protected void processFile(InputFile inputFile) {
- File ioFile = inputFile.file();
- File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION);
- if (!scmDataFile.exists()) {
- LOG.debug("Skipping SCM data injection for " + inputFile.relativePath());
- return;
- }
-
- FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile);
- try {
- List<String> lines = FileUtils.readLines(scmDataFile, Charsets.UTF_8.name());
- int lineNumber = 0;
- for (String line : lines) {
- lineNumber++;
- if (StringUtils.isNotBlank(line)) {
- // revision,author,dateTime
- String[] fields = StringUtils.split(line, ',');
- if (fields.length < 3) {
- throw new IllegalStateException("Not enough fields on line " + lineNumber);
- }
- String revision = fields[0];
- String author = fields[1];
- // Will throw an exception, when date is not in format "yyyy-MM-dd"
- Date date = DateUtils.parseDate(fields[2]);
-
- fileLinesContext.setStringValue(CoreMetrics.SCM_REVISIONS_BY_LINE_KEY, lineNumber, revision);
- fileLinesContext.setStringValue(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY, lineNumber, author);
- fileLinesContext.setStringValue(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY, lineNumber, DateUtils.formatDateTime(date));
- }
- }
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- fileLinesContext.save();
- }
-}
--- /dev/null
+/*
+ * 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.mediumtest.xoo.plugin.lang;
+
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorDescriptor;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Charsets;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.FileLinesContext;
+import org.sonar.api.measures.FileLinesContextFactory;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
+import java.util.List;
+
+public class ScmActivitySensor implements Sensor {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ScmActivitySensor.class);
+
+ private static final String SCM_EXTENSION = ".scm";
+
+ private final FileSystem fs;
+ private final FileLinesContextFactory fileLinesContextFactory;
+
+ public ScmActivitySensor(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem) {
+ this.fs = fileSystem;
+ this.fileLinesContextFactory = fileLinesContextFactory;
+ }
+
+ @Override
+ public void describe(SensorDescriptor descriptor) {
+ descriptor
+ .name(this.getClass().getSimpleName())
+ .provides(CoreMetrics.SCM_AUTHORS_BY_LINE,
+ CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE,
+ CoreMetrics.SCM_REVISIONS_BY_LINE)
+ .workOnLanguages(Xoo.KEY);
+ }
+
+ @Override
+ public void analyse(SensorContext context) {
+ for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(Xoo.KEY))) {
+ processFile(inputFile);
+ }
+
+ }
+
+ @VisibleForTesting
+ protected void processFile(InputFile inputFile) {
+ File ioFile = inputFile.file();
+ File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION);
+ if (!scmDataFile.exists()) {
+ LOG.debug("Skipping SCM data injection for " + inputFile.relativePath());
+ return;
+ }
+
+ FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile);
+ try {
+ List<String> lines = FileUtils.readLines(scmDataFile, Charsets.UTF_8.name());
+ int lineNumber = 0;
+ for (String line : lines) {
+ lineNumber++;
+ if (StringUtils.isNotBlank(line)) {
+ // revision,author,dateTime
+ String[] fields = StringUtils.split(line, ',');
+ if (fields.length < 3) {
+ throw new IllegalStateException("Not enough fields on line " + lineNumber);
+ }
+ String revision = fields[0];
+ String author = fields[1];
+ // Will throw an exception, when date is not in format "yyyy-MM-dd"
+ Date date = DateUtils.parseDate(fields[2]);
+
+ fileLinesContext.setStringValue(CoreMetrics.SCM_REVISIONS_BY_LINE_KEY, lineNumber, revision);
+ fileLinesContext.setStringValue(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY, lineNumber, author);
+ fileLinesContext.setStringValue(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY, lineNumber, DateUtils.formatDateTime(date));
+ }
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ fileLinesContext.save();
+ }
+}
*/
package org.sonar.batch.mediumtest.xoo.plugin.rule;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorDescriptor;
+import org.sonar.api.batch.sensor.measure.Measure;
+
import org.slf4j.LoggerFactory;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.analyzer.AnalyzerDescriptor;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.rule.RuleKey;
import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
import org.sonar.batch.mediumtest.xoo.plugin.base.XooConstants;
-public class OneIssuePerLineAnalyzer implements Analyzer {
+public class OneIssuePerLineAnalyzer implements Sensor {
public static final String RULE_KEY = "OneIssuePerLine";
private static final String EFFORT_TO_FIX_PROPERTY = "sonar.oneIssuePerLine.effortToFix";
@Override
- public void describe(AnalyzerDescriptor descriptor) {
+ public void describe(SensorDescriptor descriptor) {
descriptor
.name("One Issue Per Line")
.dependsOn(CoreMetrics.LINES)
}
@Override
- public void analyse(AnalyzerContext context) {
+ public void analyse(SensorContext context) {
for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
createIssues(file, context);
}
}
- private void createIssues(InputFile file, AnalyzerContext context) {
+ private void createIssues(InputFile file, SensorContext context) {
RuleKey ruleKey = RuleKey.of(XooConstants.REPOSITORY_KEY, RULE_KEY);
- AnalyzerMeasure<Integer> linesMeasure = context.getMeasure(file, CoreMetrics.LINES);
+ Measure<Integer> linesMeasure = context.getMeasure(file, CoreMetrics.LINES);
if (linesMeasure == null) {
LoggerFactory.getLogger(getClass()).warn("Missing measure " + CoreMetrics.LINES_KEY + " on " + file);
} else {
+++ /dev/null
-/*
- * 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.scan;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.mockito.ArgumentCaptor;
-import org.sonar.api.batch.SensorContext;
-import org.sonar.api.batch.analyzer.issue.internal.DefaultAnalyzerIssueBuilder;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
-import org.sonar.api.component.ResourcePerspectives;
-import org.sonar.api.config.Settings;
-import org.sonar.api.issue.Issuable;
-import org.sonar.api.issue.Issue;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Project;
-import org.sonar.api.rule.RuleKey;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class AnalyzerContextAdapterTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- private ActiveRules activeRules;
- private DefaultFileSystem fs;
- private AnalyzerContextAdaptor adaptor;
- private SensorContext sensorContext;
- private Settings settings;
- private ResourcePerspectives resourcePerspectives;
-
- @Before
- public void prepare() {
- activeRules = new ActiveRulesBuilder().build();
- fs = new DefaultFileSystem();
- MetricFinder metricFinder = mock(MetricFinder.class);
- when(metricFinder.findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
- sensorContext = mock(SensorContext.class);
- settings = new Settings();
- resourcePerspectives = mock(ResourcePerspectives.class);
- adaptor = new AnalyzerContextAdaptor(sensorContext, metricFinder, new Project("myProject"),
- resourcePerspectives, settings, fs, activeRules);
- }
-
- @Test
- public void shouldProvideComponents() {
- assertThat(adaptor.activeRules()).isEqualTo(activeRules);
- assertThat(adaptor.fileSystem()).isEqualTo(fs);
- assertThat(adaptor.settings()).isEqualTo(settings);
-
- assertThat(adaptor.issueBuilder()).isNotNull();
- assertThat(adaptor.measureBuilder()).isNotNull();
- }
-
- @Test
- public void shouldRedirectProjectMeasuresToSensorContext() {
- AnalyzerMeasure<Integer> measure = adaptor.getMeasure(CoreMetrics.NCLOC_KEY);
- assertThat(measure).isNull();
-
- when(sensorContext.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure<Integer>(CoreMetrics.NCLOC, 10.0));
-
- measure = adaptor.getMeasure(CoreMetrics.NCLOC);
- assertThat(measure.metric()).isEqualTo(CoreMetrics.NCLOC);
- assertThat(measure.inputFile()).isNull();
- assertThat(measure.value()).isEqualTo(10);
-
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Unknow metric with key: lines");
- adaptor.getMeasure(CoreMetrics.LINES);
- }
-
- @Test
- public void shouldRedirectFileMeasuresToSensorContext() {
- InputFile file = new DefaultInputFile("src/Foo.php");
-
- AnalyzerMeasure<Integer> measure = adaptor.getMeasure(file, CoreMetrics.NCLOC_KEY);
- assertThat(measure).isNull();
-
- when(sensorContext.getMeasure(File.create("src/Foo.php"), CoreMetrics.NCLOC)).thenReturn(new Measure<Integer>(CoreMetrics.NCLOC, 10.0));
- measure = adaptor.getMeasure(file, CoreMetrics.NCLOC);
-
- assertThat(measure.metric()).isEqualTo(CoreMetrics.NCLOC);
- assertThat(measure.inputFile()).isEqualTo(file);
- assertThat(measure.value()).isEqualTo(10);
- }
-
- @Test
- public void shouldAddMeasureToSensorContext() {
- InputFile file = new DefaultInputFile("src/Foo.php");
-
- ArgumentCaptor<Measure> argumentCaptor = ArgumentCaptor.forClass(Measure.class);
- when(sensorContext.saveMeasure(eq(file), argumentCaptor.capture())).thenReturn(null);
-
- adaptor.addMeasure(new DefaultAnalyzerMeasureBuilder()
- .onFile(file)
- .forMetric(CoreMetrics.NCLOC)
- .withValue(10)
- .build());
-
- Measure m = argumentCaptor.getValue();
- assertThat(m.getValue()).isEqualTo(10.0);
- assertThat(m.getMetric()).isEqualTo(CoreMetrics.NCLOC);
- }
-
- @Test
- public void shouldAddIssue() {
- InputFile file = new DefaultInputFile("src/Foo.php");
-
- ArgumentCaptor<Issue> argumentCaptor = ArgumentCaptor.forClass(Issue.class);
-
- Issuable issuable = mock(Issuable.class);
- when(resourcePerspectives.as(Issuable.class, File.create("src/Foo.php"))).thenReturn(issuable);
-
- when(issuable.addIssue(argumentCaptor.capture())).thenReturn(true);
-
- adaptor.addIssue(new DefaultAnalyzerIssueBuilder()
- .onFile(file)
- .ruleKey(RuleKey.of("foo", "bar"))
- .message("Foo")
- .atLine(3)
- .effortToFix(10.0)
- .build());
-
- Issue issue = argumentCaptor.getValue();
- assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("foo", "bar"));
- assertThat(issue.message()).isEqualTo("Foo");
- assertThat(issue.line()).isEqualTo(3);
- assertThat(issue.effortToFix()).isEqualTo(10.0);
- }
-}
--- /dev/null
+/*
+ * 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.scan;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.ArgumentCaptor;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultFileSystem;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
+import org.sonar.api.batch.sensor.issue.internal.DefaultIssueBuilder;
+import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder;
+import org.sonar.api.component.ResourcePerspectives;
+import org.sonar.api.config.Settings;
+import org.sonar.api.issue.Issuable;
+import org.sonar.api.issue.Issue;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.MetricFinder;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.Project;
+import org.sonar.api.rule.RuleKey;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SensorContextAdapterTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private ActiveRules activeRules;
+ private DefaultFileSystem fs;
+ private SensorContextAdaptor adaptor;
+ private SensorContext sensorContext;
+ private Settings settings;
+ private ResourcePerspectives resourcePerspectives;
+
+ @Before
+ public void prepare() {
+ activeRules = new ActiveRulesBuilder().build();
+ fs = new DefaultFileSystem();
+ MetricFinder metricFinder = mock(MetricFinder.class);
+ when(metricFinder.findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
+ sensorContext = mock(SensorContext.class);
+ settings = new Settings();
+ resourcePerspectives = mock(ResourcePerspectives.class);
+ adaptor = new SensorContextAdaptor(sensorContext, metricFinder, new Project("myProject"),
+ resourcePerspectives, settings, fs, activeRules);
+ }
+
+ @Test
+ public void shouldProvideComponents() {
+ assertThat(adaptor.activeRules()).isEqualTo(activeRules);
+ assertThat(adaptor.fileSystem()).isEqualTo(fs);
+ assertThat(adaptor.settings()).isEqualTo(settings);
+
+ assertThat(adaptor.issueBuilder()).isNotNull();
+ assertThat(adaptor.measureBuilder()).isNotNull();
+ }
+
+ @Test
+ public void shouldRedirectProjectMeasuresToSensorContext() {
+ Measure<Integer> measure = adaptor.getMeasure(CoreMetrics.NCLOC_KEY);
+ assertThat(measure).isNull();
+
+ when(sensorContext.getMeasure(CoreMetrics.NCLOC)).thenReturn(new org.sonar.api.measures.Measure<Integer>(CoreMetrics.NCLOC, 10.0));
+
+ measure = adaptor.getMeasure(CoreMetrics.NCLOC);
+ assertThat(measure.metric()).isEqualTo(CoreMetrics.NCLOC);
+ assertThat(measure.inputFile()).isNull();
+ assertThat(measure.value()).isEqualTo(10);
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Unknow metric with key: lines");
+ adaptor.getMeasure(CoreMetrics.LINES);
+ }
+
+ @Test
+ public void shouldRedirectFileMeasuresToSensorContext() {
+ InputFile file = new DefaultInputFile("src/Foo.php");
+
+ Measure<Integer> measure = adaptor.getMeasure(file, CoreMetrics.NCLOC_KEY);
+ assertThat(measure).isNull();
+
+ when(sensorContext.getMeasure(File.create("src/Foo.php"), CoreMetrics.NCLOC)).thenReturn(new org.sonar.api.measures.Measure<Integer>(CoreMetrics.NCLOC, 10.0));
+ measure = adaptor.getMeasure(file, CoreMetrics.NCLOC);
+
+ assertThat(measure.metric()).isEqualTo(CoreMetrics.NCLOC);
+ assertThat(measure.inputFile()).isEqualTo(file);
+ assertThat(measure.value()).isEqualTo(10);
+ }
+
+ @Test
+ public void shouldAddMeasureToSensorContext() {
+ InputFile file = new DefaultInputFile("src/Foo.php");
+
+ ArgumentCaptor<org.sonar.api.measures.Measure> argumentCaptor = ArgumentCaptor.forClass(org.sonar.api.measures.Measure.class);
+ when(sensorContext.saveMeasure(eq(file), argumentCaptor.capture())).thenReturn(null);
+
+ adaptor.addMeasure(new DefaultMeasureBuilder()
+ .onFile(file)
+ .forMetric(CoreMetrics.NCLOC)
+ .withValue(10)
+ .build());
+
+ org.sonar.api.measures.Measure m = argumentCaptor.getValue();
+ assertThat(m.getValue()).isEqualTo(10.0);
+ assertThat(m.getMetric()).isEqualTo(CoreMetrics.NCLOC);
+ }
+
+ @Test
+ public void shouldAddIssue() {
+ InputFile file = new DefaultInputFile("src/Foo.php");
+
+ ArgumentCaptor<Issue> argumentCaptor = ArgumentCaptor.forClass(Issue.class);
+
+ Issuable issuable = mock(Issuable.class);
+ when(resourcePerspectives.as(Issuable.class, File.create("src/Foo.php"))).thenReturn(issuable);
+
+ when(issuable.addIssue(argumentCaptor.capture())).thenReturn(true);
+
+ adaptor.addIssue(new DefaultIssueBuilder()
+ .onFile(file)
+ .ruleKey(RuleKey.of("foo", "bar"))
+ .message("Foo")
+ .atLine(3)
+ .effortToFix(10.0)
+ .build());
+
+ Issue issue = argumentCaptor.getValue();
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("foo", "bar"));
+ assertThat(issue.message()).isEqualTo("Foo");
+ assertThat(issue.line()).isEqualTo(3);
+ assertThat(issue.effortToFix()).isEqualTo(10.0);
+ }
+}
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.rule.RuleKey;
-
import static org.fest.assertions.Assertions.assertThat;
public class AnalyzerOptimizerTest {
@Test
public void should_run_analyzer_with_no_metadata() throws Exception {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
assertThat(optimizer.shouldExecute(descriptor)).isTrue();
}
@Test
public void should_optimize_on_language() throws Exception {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor()
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor()
.workOnLanguages("java", "php");
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
@Test
public void should_optimize_on_type() throws Exception {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor()
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor()
.workOnFileTypes(InputFile.Type.MAIN);
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
@Test
public void should_optimize_on_both_type_and_language() throws Exception {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor()
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor()
.workOnLanguages("java", "php")
.workOnFileTypes(InputFile.Type.MAIN);
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
@Test
public void should_optimize_on_repository() throws Exception {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor()
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor()
.createIssuesForRuleRepositories("squid");
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
*/
package org.sonar.api.batch;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import org.apache.commons.lang.ClassUtils;
import org.sonar.api.BatchExtension;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.platform.ComponentContainer;
private <T> List<Object> getDependencies(T extension) {
List<Object> result = new ArrayList<Object>();
result.addAll(evaluateAnnotatedClasses(extension, DependsUpon.class));
- if (ClassUtils.isAssignable(extension.getClass(), Analyzer.class)) {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
- ((Analyzer) extension).describe(descriptor);
+ if (ClassUtils.isAssignable(extension.getClass(), Sensor.class)) {
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
+ ((Sensor) extension).describe(descriptor);
result.addAll(Arrays.asList(descriptor.dependsOn()));
}
return result;
public <T> List<Object> getDependents(T extension) {
List<Object> result = new ArrayList<Object>();
result.addAll(evaluateAnnotatedClasses(extension, DependedUpon.class));
- if (ClassUtils.isAssignable(extension.getClass(), Analyzer.class)) {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
- ((Analyzer) extension).describe(descriptor);
+ if (ClassUtils.isAssignable(extension.getClass(), Sensor.class)) {
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
+ ((Sensor) extension).describe(descriptor);
result.addAll(Arrays.asList(descriptor.provides()));
}
return result;
* </p>
*
* @since 1.10
+ * @deprecated since 4.5 use {@link org.sonar.api.batch.sensor.Sensor}
*/
+@Deprecated
public interface Sensor extends BatchExtension, CheckProject {
/**
/**
* @since 1.10
+ * @deprecated since 4.5
*/
public interface SensorContext {
+++ /dev/null
-/*
- * 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.batch.analyzer;
-
-import com.google.common.annotations.Beta;
-import org.sonar.api.BatchExtension;
-
-/**
- * <p>
- * An Analyzer is invoked once during the analysis of a project. The analyzer can parse a flat file, connect to a web server... Analyzers are
- * used to add measure and issues at file level.
- * </p>
- *
- * <p>
- * For example the Cobertura Analyzer parses Cobertura report and saves the first-level of measures on files.
- * </p>
- *
- * @since 4.4
- */
-@Beta
-public interface Analyzer extends BatchExtension {
-
- /**
- * Populate {@link AnalyzerDescriptor} of this analyzer.
- */
- void describe(AnalyzerDescriptor descriptor);
-
- /**
- * The actual analyzer code.
- */
- void analyse(AnalyzerContext context);
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer;
-
-import com.google.common.annotations.Beta;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssueBuilder;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
-import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.Metric;
-import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.config.Settings;
-
-import javax.annotation.CheckForNull;
-
-import java.io.Serializable;
-
-/**
- * @since 4.4
- */
-@Beta
-public interface AnalyzerContext {
-
- /**
- * Get settings of the current project.
- */
- Settings settings();
-
- /**
- * Get filesystem of the current project.
- */
- FileSystem fileSystem();
-
- /**
- * Get list of active rules.
- */
- ActiveRules activeRules();
-
- // ----------- MEASURES --------------
-
- /**
- * Builder to create a new {@link AnalyzerMeasure}.
- */
- <G extends Serializable> AnalyzerMeasureBuilder<G> measureBuilder();
-
- /**
- * Find a project measure.
- */
- @CheckForNull
- AnalyzerMeasure getMeasure(String metricKey);
-
- /**
- * Find a project measure.
- */
- @CheckForNull
- <G extends Serializable> AnalyzerMeasure<G> getMeasure(Metric<G> metric);
-
- /**
- * Find a file measure.
- */
- @CheckForNull
- AnalyzerMeasure getMeasure(InputFile file, String metricKey);
-
- /**
- * Find a file measure.
- */
- @CheckForNull
- <G extends Serializable> AnalyzerMeasure<G> getMeasure(InputFile file, Metric<G> metric);
-
- /**
- * Add a measure. Use {@link #measureBuilder()} to create the new measure.
- */
- void addMeasure(AnalyzerMeasure<?> measure);
-
- // ----------- ISSUES --------------
-
- /**
- * Builder to create a new {@link AnalyzerIssue}.
- */
- AnalyzerIssueBuilder issueBuilder();
-
- /**
- * Add an issue. Use {@link #issueBuilder()} to create the new issue.
- * @return true if the new issue is registered, false if the related rule does not exist or is disabled in the Quality profile.
- */
- boolean addIssue(AnalyzerIssue issue);
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer;
-
-import com.google.common.annotations.Beta;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.Metric;
-
-/**
- * Describe what an {@link Analyzer} is doing. Information may be used by the platform
- * to log interesting information or perform some optimization.
- * @since 4.4
- */
-@Beta
-public interface AnalyzerDescriptor {
-
- /**
- * Name of the {@link Analyzer}. Will be displayed in logs.
- */
- AnalyzerDescriptor name(String name);
-
- /**
- * List {@link Metric} this {@link Analyzer} depends on. Will be used to execute Analyzers in correct order.
- */
- AnalyzerDescriptor dependsOn(Metric<?>... metrics);
-
- /**
- * List {@link Metric} this {@link Analyzer} provides. Will be used to execute Analyzers in correct order.
- */
- AnalyzerDescriptor provides(Metric<?>... metrics);
-
- /**
- * List languages this {@link Analyzer} work on. May be used by the platform to skip execution of the {@link Analyzer} when
- * no file for given languages are present in the project.
- * If no language is provided then it will be executed for all languages.
- */
- AnalyzerDescriptor workOnLanguages(String... languageKeys);
-
- /**
- * List {@link InputFile.Type} this {@link Analyzer} work on. May be used by the platform to skip execution of the {@link Analyzer} when
- * no file for given type are present in the project.
- * If not type is provided then it will be executed for all types.
- */
- AnalyzerDescriptor workOnFileTypes(InputFile.Type... types);
-
- /**
- * List {@link InputFile.Type} this {@link Analyzer} work on. May be used by the platform to skip execution of the {@link Analyzer} when
- * no file for given type are present in the project.
- * If not type is provided then it will be executed for all types.
- */
- AnalyzerDescriptor createIssuesForRuleRepositories(String... repositoryKeys);
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.internal;
-
-import org.sonar.api.batch.analyzer.AnalyzerDescriptor;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.Metric;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-public class DefaultAnalyzerDescriptor implements AnalyzerDescriptor {
-
- private String name;
- private Metric<?>[] dependsOn = new Metric<?>[0];
- private Metric<?>[] provides = new Metric<?>[0];
- private String[] languages = new String[0];
- private InputFile.Type[] types = new InputFile.Type[0];
- private String[] ruleRepositories = new String[0];
-
- public String name() {
- return name;
- }
-
- public Metric[] dependsOn() {
- return dependsOn;
- }
-
- public Metric[] provides() {
- return provides;
- }
-
- public Collection<String> languages() {
- return Arrays.asList(languages);
- }
-
- public Collection<InputFile.Type> types() {
- return Arrays.asList(types);
- }
-
- public Collection<String> ruleRepositories() {
- return Arrays.asList(ruleRepositories);
- }
-
- @Override
- public DefaultAnalyzerDescriptor name(String name) {
- this.name = name;
- return this;
- }
-
- @Override
- public DefaultAnalyzerDescriptor dependsOn(Metric<?>... metrics) {
- this.dependsOn = metrics;
- return this;
- }
-
- @Override
- public DefaultAnalyzerDescriptor provides(Metric<?>... metrics) {
- this.provides = metrics;
- return this;
- }
-
- @Override
- public DefaultAnalyzerDescriptor workOnLanguages(String... languageKeys) {
- this.languages = languageKeys;
- return this;
- }
-
- @Override
- public DefaultAnalyzerDescriptor workOnFileTypes(InputFile.Type... types) {
- this.types = types;
- return this;
- }
-
- @Override
- public DefaultAnalyzerDescriptor createIssuesForRuleRepositories(String... repositoryKeys) {
- this.ruleRepositories = repositoryKeys;
- return this;
- }
-
-}
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.analyzer.internal;
+++ /dev/null
-/*
- * 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.batch.analyzer.issue;
-
-import com.google.common.annotations.Beta;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.Nullable;
-
-/**
- * Issue reported by an {@link Analyzer}
- *
- * @since 4.4
- */
-@Beta
-public interface AnalyzerIssue {
-
- /**
- * The {@link InputFile} this issue belongs to. Returns null if issue is global to the project.
- */
- @Nullable
- InputFile inputFile();
-
- /**
- * The {@link RuleKey} of this issue.
- */
- RuleKey ruleKey();
-
- /**
- * Message of the issue.
- */
- String message();
-
- /**
- * Line of the issue.
- */
- Integer line();
-
- /**
- * Effort to fix the issue. Used by technical debt model.
- */
- @Nullable
- Double effortToFix();
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.issue;
-
-import com.google.common.annotations.Beta;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.Nullable;
-
-/**
- * Builder for {@link AnalyzerIssue}.
- *
- * @since 4.4
- */
-@Beta
-public interface AnalyzerIssueBuilder {
-
- /**
- * The {@link RuleKey} of the issue.
- */
- AnalyzerIssueBuilder ruleKey(RuleKey ruleKey);
-
- /**
- * The {@link InputFile} the issue belongs to. For global issues call {@link #onProject()}.
- */
- AnalyzerIssueBuilder onFile(InputFile file);
-
- /**
- * Tell that the issue is global to the project.
- */
- AnalyzerIssueBuilder onProject();
-
- /**
- * Line of the issue. If no line is specified then issue is supposed to be global to the file.
- */
- AnalyzerIssueBuilder atLine(int line);
-
- /**
- * Effort to fix the issue.
- */
- AnalyzerIssueBuilder effortToFix(@Nullable Double effortToFix);
-
- /**
- * Message of the issue.
- */
- AnalyzerIssueBuilder message(String message);
-
- /**
- * Build the issue.
- */
- AnalyzerIssue build();
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.issue.internal;
-
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
-import org.apache.commons.lang.builder.ToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.Nullable;
-
-import java.io.Serializable;
-import java.util.UUID;
-
-public class DefaultAnalyzerIssue implements AnalyzerIssue, Serializable {
-
- private final String key;
- private final InputFile inputFile;
- private final RuleKey ruleKey;
- private final String message;
- private final Integer line;
- private final Double effortToFix;
-
- DefaultAnalyzerIssue(DefaultAnalyzerIssueBuilder builder) {
- Preconditions.checkNotNull(builder.ruleKey, "ruleKey is mandatory on issue");
- this.inputFile = builder.file;
- this.ruleKey = builder.ruleKey;
- this.message = builder.message;
- this.line = builder.line;
- this.effortToFix = builder.effortToFix;
- this.key = builder.key == null ? UUID.randomUUID().toString() : builder.key;
- Preconditions.checkState(!Strings.isNullOrEmpty(key), "Fail to generate issue key");
- }
-
- public String key() {
- return key;
- }
-
- @Override
- @Nullable
- public InputFile inputFile() {
- return inputFile;
- }
-
- @Override
- public RuleKey ruleKey() {
- return ruleKey;
- }
-
- @Override
- public String message() {
- return message;
- }
-
- @Override
- public Integer line() {
- return line;
- }
-
- @Override
- @Nullable
- public Double effortToFix() {
- return effortToFix;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- DefaultAnalyzerIssue that = (DefaultAnalyzerIssue) o;
- return !key.equals(that.key);
- }
-
- @Override
- public int hashCode() {
- return key.hashCode();
- }
-
- @Override
- public String toString() {
- return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
- }
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.issue.internal;
-
-import com.google.common.base.Preconditions;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssueBuilder;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.Nullable;
-
-public class DefaultAnalyzerIssueBuilder implements AnalyzerIssueBuilder {
-
- String key;
- Boolean onProject = null;
- InputFile file;
- RuleKey ruleKey;
- String message;
- Integer line;
- Double effortToFix;
-
- @Override
- public DefaultAnalyzerIssueBuilder ruleKey(RuleKey ruleKey) {
- this.ruleKey = ruleKey;
- return this;
- }
-
- @Override
- public DefaultAnalyzerIssueBuilder onFile(InputFile file) {
- onProject(false);
- Preconditions.checkNotNull(file, "InputFile should be non null");
- this.file = file;
- return this;
- }
-
- @Override
- public DefaultAnalyzerIssueBuilder onProject() {
- onProject(true);
- this.file = null;
- return this;
- }
-
- private void onProject(boolean isOnProject) {
- Preconditions.checkState(this.onProject == null, "onFile or onProject can be called only once");
- this.onProject = isOnProject;
- }
-
- @Override
- public DefaultAnalyzerIssueBuilder atLine(int line) {
- this.line = line;
- return this;
- }
-
- @Override
- public DefaultAnalyzerIssueBuilder effortToFix(@Nullable Double effortToFix) {
- this.effortToFix = effortToFix;
- return this;
- }
-
- @Override
- public DefaultAnalyzerIssueBuilder message(String message) {
- this.message = message;
- return this;
- }
-
- /**
- * For testing only.
- */
- public DefaultAnalyzerIssueBuilder withKey(String key) {
- this.key = key;
- return this;
- }
-
- @Override
- public AnalyzerIssue build() {
- return new DefaultAnalyzerIssue(this);
- }
-
-}
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.analyzer.issue.internal;
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.analyzer.issue;
+++ /dev/null
-/*
- * 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.batch.analyzer.measure;
-
-import com.google.common.annotations.Beta;
-import org.sonar.api.batch.analyzer.Analyzer;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.Metric;
-
-import javax.annotation.Nullable;
-
-import java.io.Serializable;
-
-/**
- * A measure computed by an {@link Analyzer}.
- * @since 4.4
- */
-@Beta
-public interface AnalyzerMeasure<G extends Serializable> {
-
- /**
- * The {@link InputFile} this measure belongs to. Returns null if measure is global to the project.
- */
- @Nullable
- InputFile inputFile();
-
- Metric<G> metric();
-
- G value();
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.measure;
-
-import org.sonar.api.batch.measure.Metric;
-
-import com.google.common.annotations.Beta;
-import org.sonar.api.batch.fs.InputFile;
-
-import java.io.Serializable;
-
-/**
- * Builder to create new {@link AnalyzerMeasure}
- * @since 4.4
- */
-@Beta
-public interface AnalyzerMeasureBuilder<G extends Serializable> {
-
- /**
- * The file the measure belongs to.
- */
- AnalyzerMeasureBuilder<G> onFile(InputFile file);
-
- /**
- * Tell that the measure is global to the project.
- */
- AnalyzerMeasureBuilder<G> onProject();
-
- /**
- * The metric this measure belong to.
- */
- AnalyzerMeasureBuilder<G> forMetric(Metric<G> metric);
-
- /**
- * Value of the measure.
- */
- AnalyzerMeasureBuilder<G> withValue(G value);
-
- /**
- * Build the measure.
- */
- AnalyzerMeasure<G> build();
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.measure.internal;
-
-import com.google.common.base.Preconditions;
-import org.apache.commons.lang.builder.EqualsBuilder;
-import org.apache.commons.lang.builder.HashCodeBuilder;
-import org.apache.commons.lang.builder.ToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.Metric;
-
-import javax.annotation.Nullable;
-
-import java.io.Serializable;
-
-public class DefaultAnalyzerMeasure<G extends Serializable> implements AnalyzerMeasure<G>, Serializable {
-
- private final InputFile inputFile;
- private final Metric<G> metric;
- private final G value;
-
- DefaultAnalyzerMeasure(DefaultAnalyzerMeasureBuilder<G> builder) {
- Preconditions.checkNotNull(builder.value, "Measure value can't be null");
- Preconditions.checkNotNull(builder.metric, "Measure metric can't be null");
- Preconditions.checkState(builder.metric.valueType().equals(builder.value.getClass()), "Measure value should be of type " + builder.metric.valueType());
- this.inputFile = builder.file;
- this.metric = builder.metric;
- this.value = builder.value;
- }
-
- @Nullable
- @Override
- public InputFile inputFile() {
- return inputFile;
- }
-
- @Override
- public Metric<G> metric() {
- return metric;
- }
-
- @Override
- public G value() {
- return value;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (obj.getClass() != getClass()) {
- return false;
- }
- DefaultAnalyzerMeasure rhs = (DefaultAnalyzerMeasure) obj;
- return new EqualsBuilder()
- .append(inputFile, rhs.inputFile)
- .append(metric, rhs.metric)
- .append(value, rhs.value)
- .isEquals();
- }
-
- @Override
- public int hashCode() {
- return new HashCodeBuilder(27, 45).
- append(inputFile).
- append(metric).
- append(value).
- toHashCode();
- }
-
- @Override
- public String toString() {
- return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
- }
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.measure.internal;
-
-import com.google.common.base.Preconditions;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.measure.Metric;
-
-import java.io.Serializable;
-
-public class DefaultAnalyzerMeasureBuilder<G extends Serializable> implements AnalyzerMeasureBuilder<G> {
-
- Boolean onProject = null;
- InputFile file;
- Metric<G> metric;
- G value;
-
- @Override
- public DefaultAnalyzerMeasureBuilder<G> onFile(InputFile inputFile) {
- onProject(false);
- Preconditions.checkNotNull(inputFile, "inputFile should be non null");
- this.file = inputFile;
- return this;
- }
-
- @Override
- public DefaultAnalyzerMeasureBuilder<G> onProject() {
- onProject(true);
- this.file = null;
- return this;
- }
-
- private void onProject(boolean isOnProject) {
- Preconditions.checkState(this.onProject == null, "onFile or onProject can be called only once");
- this.onProject = isOnProject;
- }
-
- @Override
- public DefaultAnalyzerMeasureBuilder<G> forMetric(Metric<G> metric) {
- Preconditions.checkState(metric != null, "Metric already defined");
- Preconditions.checkNotNull(metric, "metric should be non null");
- this.metric = metric;
- return this;
- }
-
- @Override
- public DefaultAnalyzerMeasureBuilder<G> withValue(G value) {
- Preconditions.checkState(this.value == null, "Measure value already defined");
- Preconditions.checkNotNull(value, "Measure value can't be null");
- this.value = value;
- return this;
- }
-
- @Override
- public DefaultAnalyzerMeasure<G> build() {
- return new DefaultAnalyzerMeasure<G>(this);
- }
-}
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.analyzer.measure.internal;
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.analyzer.measure;
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.analyzer;
--- /dev/null
+/*
+ * 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.batch.sensor;
+
+import com.google.common.annotations.Beta;
+import org.sonar.api.BatchExtension;
+
+/**
+ * <p>
+ * An Analyzer is invoked once during the analysis of a project. The analyzer can parse a flat file, connect to a web server... Analyzers are
+ * used to add measure and issues at file level.
+ * </p>
+ *
+ * <p>
+ * For example the Cobertura Analyzer parses Cobertura report and saves the first-level of measures on files.
+ * </p>
+ *
+ * @since 4.4
+ */
+@Beta
+public interface Sensor extends BatchExtension {
+
+ /**
+ * Populate {@link SensorDescriptor} of this analyzer.
+ */
+ void describe(SensorDescriptor descriptor);
+
+ /**
+ * The actual analyzer code.
+ */
+ void analyse(SensorContext context);
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor;
+
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.issue.IssueBuilder;
+import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.measure.MeasureBuilder;
+
+import com.google.common.annotations.Beta;
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.config.Settings;
+
+import javax.annotation.CheckForNull;
+
+import java.io.Serializable;
+
+/**
+ * @since 4.4
+ */
+@Beta
+public interface SensorContext {
+
+ /**
+ * Get settings of the current project.
+ */
+ Settings settings();
+
+ /**
+ * Get filesystem of the current project.
+ */
+ FileSystem fileSystem();
+
+ /**
+ * Get list of active rules.
+ */
+ ActiveRules activeRules();
+
+ // ----------- MEASURES --------------
+
+ /**
+ * Builder to create a new {@link Measure}.
+ */
+ <G extends Serializable> MeasureBuilder<G> measureBuilder();
+
+ /**
+ * Find a project measure.
+ */
+ @CheckForNull
+ Measure getMeasure(String metricKey);
+
+ /**
+ * Find a project measure.
+ */
+ @CheckForNull
+ <G extends Serializable> Measure<G> getMeasure(Metric<G> metric);
+
+ /**
+ * Find a file measure.
+ */
+ @CheckForNull
+ Measure getMeasure(InputFile file, String metricKey);
+
+ /**
+ * Find a file measure.
+ */
+ @CheckForNull
+ <G extends Serializable> Measure<G> getMeasure(InputFile file, Metric<G> metric);
+
+ /**
+ * Add a measure. Use {@link #measureBuilder()} to create the new measure.
+ */
+ void addMeasure(Measure<?> measure);
+
+ // ----------- ISSUES --------------
+
+ /**
+ * Builder to create a new {@link Issue}.
+ */
+ IssueBuilder issueBuilder();
+
+ /**
+ * Add an issue. Use {@link #issueBuilder()} to create the new issue.
+ * @return true if the new issue is registered, false if the related rule does not exist or is disabled in the Quality profile.
+ */
+ boolean addIssue(Issue issue);
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor;
+
+import com.google.common.annotations.Beta;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
+
+/**
+ * Describe what an {@link Sensor} is doing. Information may be used by the platform
+ * to log interesting information or perform some optimization.
+ * @since 4.4
+ */
+@Beta
+public interface SensorDescriptor {
+
+ /**
+ * Name of the {@link Sensor}. Will be displayed in logs.
+ */
+ SensorDescriptor name(String name);
+
+ /**
+ * List {@link Metric} this {@link Sensor} depends on. Will be used to execute Analyzers in correct order.
+ */
+ SensorDescriptor dependsOn(Metric<?>... metrics);
+
+ /**
+ * List {@link Metric} this {@link Sensor} provides. Will be used to execute Analyzers in correct order.
+ */
+ SensorDescriptor provides(Metric<?>... metrics);
+
+ /**
+ * List languages this {@link Sensor} work on. May be used by the platform to skip execution of the {@link Sensor} when
+ * no file for given languages are present in the project.
+ * If no language is provided then it will be executed for all languages.
+ */
+ SensorDescriptor workOnLanguages(String... languageKeys);
+
+ /**
+ * List {@link InputFile.Type} this {@link Sensor} work on. May be used by the platform to skip execution of the {@link Sensor} when
+ * no file for given type are present in the project.
+ * If not type is provided then it will be executed for all types.
+ */
+ SensorDescriptor workOnFileTypes(InputFile.Type... types);
+
+ /**
+ * List {@link InputFile.Type} this {@link Sensor} work on. May be used by the platform to skip execution of the {@link Sensor} when
+ * no file for given type are present in the project.
+ * If not type is provided then it will be executed for all types.
+ */
+ SensorDescriptor createIssuesForRuleRepositories(String... repositoryKeys);
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.internal;
+
+import org.sonar.api.batch.sensor.SensorDescriptor;
+
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+public class DefaultSensorDescriptor implements SensorDescriptor {
+
+ private String name;
+ private Metric<?>[] dependsOn = new Metric<?>[0];
+ private Metric<?>[] provides = new Metric<?>[0];
+ private String[] languages = new String[0];
+ private InputFile.Type[] types = new InputFile.Type[0];
+ private String[] ruleRepositories = new String[0];
+
+ public String name() {
+ return name;
+ }
+
+ public Metric[] dependsOn() {
+ return dependsOn;
+ }
+
+ public Metric[] provides() {
+ return provides;
+ }
+
+ public Collection<String> languages() {
+ return Arrays.asList(languages);
+ }
+
+ public Collection<InputFile.Type> types() {
+ return Arrays.asList(types);
+ }
+
+ public Collection<String> ruleRepositories() {
+ return Arrays.asList(ruleRepositories);
+ }
+
+ @Override
+ public DefaultSensorDescriptor name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ @Override
+ public DefaultSensorDescriptor dependsOn(Metric<?>... metrics) {
+ this.dependsOn = metrics;
+ return this;
+ }
+
+ @Override
+ public DefaultSensorDescriptor provides(Metric<?>... metrics) {
+ this.provides = metrics;
+ return this;
+ }
+
+ @Override
+ public DefaultSensorDescriptor workOnLanguages(String... languageKeys) {
+ this.languages = languageKeys;
+ return this;
+ }
+
+ @Override
+ public DefaultSensorDescriptor workOnFileTypes(InputFile.Type... types) {
+ this.types = types;
+ return this;
+ }
+
+ @Override
+ public DefaultSensorDescriptor createIssuesForRuleRepositories(String... repositoryKeys) {
+ this.ruleRepositories = repositoryKeys;
+ return this;
+ }
+
+}
--- /dev/null
+/*
+ * 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.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.sensor.internal;
--- /dev/null
+/*
+ * 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.batch.sensor.issue;
+
+import org.sonar.api.batch.sensor.Sensor;
+
+import com.google.common.annotations.Beta;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.Nullable;
+
+/**
+ * Issue reported by an {@link Sensor}
+ *
+ * @since 4.4
+ */
+@Beta
+public interface Issue {
+
+ /**
+ * The {@link InputFile} this issue belongs to. Returns null if issue is global to the project.
+ */
+ @Nullable
+ InputFile inputFile();
+
+ /**
+ * The {@link RuleKey} of this issue.
+ */
+ RuleKey ruleKey();
+
+ /**
+ * Message of the issue.
+ */
+ String message();
+
+ /**
+ * Line of the issue.
+ */
+ Integer line();
+
+ /**
+ * Effort to fix the issue. Used by technical debt model.
+ */
+ @Nullable
+ Double effortToFix();
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.issue;
+
+import com.google.common.annotations.Beta;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.Nullable;
+
+/**
+ * Builder for {@link Issue}.
+ *
+ * @since 4.4
+ */
+@Beta
+public interface IssueBuilder {
+
+ /**
+ * The {@link RuleKey} of the issue.
+ */
+ IssueBuilder ruleKey(RuleKey ruleKey);
+
+ /**
+ * The {@link InputFile} the issue belongs to. For global issues call {@link #onProject()}.
+ */
+ IssueBuilder onFile(InputFile file);
+
+ /**
+ * Tell that the issue is global to the project.
+ */
+ IssueBuilder onProject();
+
+ /**
+ * Line of the issue. If no line is specified then issue is supposed to be global to the file.
+ */
+ IssueBuilder atLine(int line);
+
+ /**
+ * Effort to fix the issue.
+ */
+ IssueBuilder effortToFix(@Nullable Double effortToFix);
+
+ /**
+ * Message of the issue.
+ */
+ IssueBuilder message(String message);
+
+ /**
+ * Build the issue.
+ */
+ Issue build();
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.issue.internal;
+
+import org.sonar.api.batch.sensor.issue.Issue;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.UUID;
+
+public class DefaultIssue implements Issue, Serializable {
+
+ private final String key;
+ private final InputFile inputFile;
+ private final RuleKey ruleKey;
+ private final String message;
+ private final Integer line;
+ private final Double effortToFix;
+
+ DefaultIssue(DefaultIssueBuilder builder) {
+ Preconditions.checkNotNull(builder.ruleKey, "ruleKey is mandatory on issue");
+ this.inputFile = builder.file;
+ this.ruleKey = builder.ruleKey;
+ this.message = builder.message;
+ this.line = builder.line;
+ this.effortToFix = builder.effortToFix;
+ this.key = builder.key == null ? UUID.randomUUID().toString() : builder.key;
+ Preconditions.checkState(!Strings.isNullOrEmpty(key), "Fail to generate issue key");
+ }
+
+ public String key() {
+ return key;
+ }
+
+ @Override
+ @Nullable
+ public InputFile inputFile() {
+ return inputFile;
+ }
+
+ @Override
+ public RuleKey ruleKey() {
+ return ruleKey;
+ }
+
+ @Override
+ public String message() {
+ return message;
+ }
+
+ @Override
+ public Integer line() {
+ return line;
+ }
+
+ @Override
+ @Nullable
+ public Double effortToFix() {
+ return effortToFix;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ DefaultIssue that = (DefaultIssue) o;
+ return !key.equals(that.key);
+ }
+
+ @Override
+ public int hashCode() {
+ return key.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+ }
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.issue.internal;
+
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.issue.IssueBuilder;
+
+import com.google.common.base.Preconditions;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.Nullable;
+
+public class DefaultIssueBuilder implements IssueBuilder {
+
+ String key;
+ Boolean onProject = null;
+ InputFile file;
+ RuleKey ruleKey;
+ String message;
+ Integer line;
+ Double effortToFix;
+
+ @Override
+ public DefaultIssueBuilder ruleKey(RuleKey ruleKey) {
+ this.ruleKey = ruleKey;
+ return this;
+ }
+
+ @Override
+ public DefaultIssueBuilder onFile(InputFile file) {
+ onProject(false);
+ Preconditions.checkNotNull(file, "InputFile should be non null");
+ this.file = file;
+ return this;
+ }
+
+ @Override
+ public DefaultIssueBuilder onProject() {
+ onProject(true);
+ this.file = null;
+ return this;
+ }
+
+ private void onProject(boolean isOnProject) {
+ Preconditions.checkState(this.onProject == null, "onFile or onProject can be called only once");
+ this.onProject = isOnProject;
+ }
+
+ @Override
+ public DefaultIssueBuilder atLine(int line) {
+ this.line = line;
+ return this;
+ }
+
+ @Override
+ public DefaultIssueBuilder effortToFix(@Nullable Double effortToFix) {
+ this.effortToFix = effortToFix;
+ return this;
+ }
+
+ @Override
+ public DefaultIssueBuilder message(String message) {
+ this.message = message;
+ return this;
+ }
+
+ /**
+ * For testing only.
+ */
+ public DefaultIssueBuilder withKey(String key) {
+ this.key = key;
+ return this;
+ }
+
+ @Override
+ public Issue build() {
+ return new DefaultIssue(this);
+ }
+
+}
--- /dev/null
+/*
+ * 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.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.sensor.issue.internal;
--- /dev/null
+/*
+ * 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.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.sensor.issue;
--- /dev/null
+/*
+ * 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.batch.sensor.measure;
+
+import org.sonar.api.batch.sensor.Sensor;
+
+import com.google.common.annotations.Beta;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+
+/**
+ * A measure computed by an {@link Sensor}.
+ * @since 4.4
+ */
+@Beta
+public interface Measure<G extends Serializable> {
+
+ /**
+ * The {@link InputFile} this measure belongs to. Returns null if measure is global to the project.
+ */
+ @Nullable
+ InputFile inputFile();
+
+ Metric<G> metric();
+
+ G value();
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.measure;
+
+import org.sonar.api.batch.measure.Metric;
+
+import com.google.common.annotations.Beta;
+import org.sonar.api.batch.fs.InputFile;
+
+import java.io.Serializable;
+
+/**
+ * Builder to create new {@link Measure}
+ * @since 4.4
+ */
+@Beta
+public interface MeasureBuilder<G extends Serializable> {
+
+ /**
+ * The file the measure belongs to.
+ */
+ MeasureBuilder<G> onFile(InputFile file);
+
+ /**
+ * Tell that the measure is global to the project.
+ */
+ MeasureBuilder<G> onProject();
+
+ /**
+ * The metric this measure belong to.
+ */
+ MeasureBuilder<G> forMetric(Metric<G> metric);
+
+ /**
+ * Value of the measure.
+ */
+ MeasureBuilder<G> withValue(G value);
+
+ /**
+ * Build the measure.
+ */
+ Measure<G> build();
+}
--- /dev/null
+/*
+ * 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.batch.sensor.measure.internal;
+
+import org.sonar.api.batch.sensor.measure.Measure;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+
+public class DefaultMeasure<G extends Serializable> implements Measure<G>, Serializable {
+
+ private final InputFile inputFile;
+ private final Metric<G> metric;
+ private final G value;
+
+ DefaultMeasure(DefaultMeasureBuilder<G> builder) {
+ Preconditions.checkNotNull(builder.value, "Measure value can't be null");
+ Preconditions.checkNotNull(builder.metric, "Measure metric can't be null");
+ Preconditions.checkState(builder.metric.valueType().equals(builder.value.getClass()), "Measure value should be of type " + builder.metric.valueType());
+ this.inputFile = builder.file;
+ this.metric = builder.metric;
+ this.value = builder.value;
+ }
+
+ @Nullable
+ @Override
+ public InputFile inputFile() {
+ return inputFile;
+ }
+
+ @Override
+ public Metric<G> metric() {
+ return metric;
+ }
+
+ @Override
+ public G value() {
+ return value;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (obj == this) {
+ return true;
+ }
+ if (obj.getClass() != getClass()) {
+ return false;
+ }
+ DefaultMeasure rhs = (DefaultMeasure) obj;
+ return new EqualsBuilder()
+ .append(inputFile, rhs.inputFile)
+ .append(metric, rhs.metric)
+ .append(value, rhs.value)
+ .isEquals();
+ }
+
+ @Override
+ public int hashCode() {
+ return new HashCodeBuilder(27, 45).
+ append(inputFile).
+ append(metric).
+ append(value).
+ toHashCode();
+ }
+
+ @Override
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+ }
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.measure.internal;
+
+import org.sonar.api.batch.sensor.measure.MeasureBuilder;
+
+import com.google.common.base.Preconditions;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
+
+import java.io.Serializable;
+
+public class DefaultMeasureBuilder<G extends Serializable> implements MeasureBuilder<G> {
+
+ Boolean onProject = null;
+ InputFile file;
+ Metric<G> metric;
+ G value;
+
+ @Override
+ public DefaultMeasureBuilder<G> onFile(InputFile inputFile) {
+ onProject(false);
+ Preconditions.checkNotNull(inputFile, "inputFile should be non null");
+ this.file = inputFile;
+ return this;
+ }
+
+ @Override
+ public DefaultMeasureBuilder<G> onProject() {
+ onProject(true);
+ this.file = null;
+ return this;
+ }
+
+ private void onProject(boolean isOnProject) {
+ Preconditions.checkState(this.onProject == null, "onFile or onProject can be called only once");
+ this.onProject = isOnProject;
+ }
+
+ @Override
+ public DefaultMeasureBuilder<G> forMetric(Metric<G> metric) {
+ Preconditions.checkState(metric != null, "Metric already defined");
+ Preconditions.checkNotNull(metric, "metric should be non null");
+ this.metric = metric;
+ return this;
+ }
+
+ @Override
+ public DefaultMeasureBuilder<G> withValue(G value) {
+ Preconditions.checkState(this.value == null, "Measure value already defined");
+ Preconditions.checkNotNull(value, "Measure value can't be null");
+ this.value = value;
+ return this;
+ }
+
+ @Override
+ public DefaultMeasure<G> build() {
+ return new DefaultMeasure<G>(this);
+ }
+}
--- /dev/null
+/*
+ * 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.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.sensor.measure.internal;
--- /dev/null
+/*
+ * 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.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.sensor.measure;
--- /dev/null
+/*
+ * 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.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.sensor;
+++ /dev/null
-/*
- * 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.batch.analyzer.internal;
-
-import org.junit.Test;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.measures.CoreMetrics;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultAnalyzerDescriptorTest {
-
- @Test
- public void describe() {
- DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
- descriptor
- .name("Foo")
- .dependsOn(CoreMetrics.NCLOC)
- .provides(CoreMetrics.BLOCKER_VIOLATIONS)
- .workOnLanguages("java", "php")
- .workOnFileTypes(InputFile.Type.MAIN);
-
- assertThat(descriptor.name()).isEqualTo("Foo");
- assertThat(descriptor.dependsOn()).containsOnly(CoreMetrics.NCLOC);
- assertThat(descriptor.provides()).containsOnly(CoreMetrics.BLOCKER_VIOLATIONS);
- assertThat(descriptor.languages()).containsOnly("java", "php");
- assertThat(descriptor.types()).containsOnly(InputFile.Type.MAIN);
- }
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.issue.internal;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.rule.RuleKey;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultAnalyzerIssueTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void build_file_issue() {
- AnalyzerIssue issue = new DefaultAnalyzerIssueBuilder()
- .onFile(new DefaultInputFile("src/Foo.php"))
- .ruleKey(RuleKey.of("repo", "rule"))
- .atLine(1)
- .effortToFix(10.0)
- .message("Wrong way!")
- .build();
-
- assertThat(issue.inputFile()).isEqualTo(new DefaultInputFile("src/Foo.php"));
- assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
- assertThat(issue.line()).isEqualTo(1);
- assertThat(issue.effortToFix()).isEqualTo(10.0);
- assertThat(issue.message()).isEqualTo("Wrong way!");
- }
-
- @Test
- public void build_project_issue() {
- AnalyzerIssue issue = new DefaultAnalyzerIssueBuilder()
- .onProject()
- .ruleKey(RuleKey.of("repo", "rule"))
- .atLine(1)
- .effortToFix(10.0)
- .message("Wrong way!")
- .build();
-
- assertThat(issue.inputFile()).isNull();
- assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
- assertThat(issue.line()).isEqualTo(1);
- assertThat(issue.effortToFix()).isEqualTo(10.0);
- assertThat(issue.message()).isEqualTo("Wrong way!");
- }
-
- @Test
- public void not_allowed_to_call_onFile_and_onProject() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("onFile or onProject can be called only once");
- new DefaultAnalyzerIssueBuilder()
- .onProject()
- .onFile(new DefaultInputFile("src/Foo.php"))
- .ruleKey(RuleKey.of("repo", "rule"))
- .atLine(1)
- .effortToFix(10.0)
- .message("Wrong way!")
- .build();
-
- }
-
-}
+++ /dev/null
-/*
- * 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.batch.analyzer.measure.internal;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.measures.CoreMetrics;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultAnalyzerMeasureTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void build_file_measure() {
- AnalyzerMeasure<Integer> issue = new DefaultAnalyzerMeasureBuilder<Integer>()
- .forMetric(CoreMetrics.LINES)
- .onFile(new DefaultInputFile("src/Foo.php"))
- .withValue(3)
- .build();
-
- assertThat(issue.inputFile()).isEqualTo(new DefaultInputFile("src/Foo.php"));
- assertThat(issue.metric()).isEqualTo(CoreMetrics.LINES);
- assertThat(issue.value()).isEqualTo(3);
- }
-
- @Test
- public void build_project_measure() {
- AnalyzerMeasure<Integer> issue = new DefaultAnalyzerMeasureBuilder<Integer>()
- .forMetric(CoreMetrics.LINES)
- .onProject()
- .withValue(3)
- .build();
-
- assertThat(issue.inputFile()).isNull();
- assertThat(issue.metric()).isEqualTo(CoreMetrics.LINES);
- assertThat(issue.value()).isEqualTo(3);
- }
-
- @Test
- public void not_allowed_to_call_onFile_and_onProject() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("onFile or onProject can be called only once");
- new DefaultAnalyzerMeasureBuilder<Integer>()
- .onProject()
- .onFile(new DefaultInputFile("src/Foo.php"))
- .withValue(3)
- .build();
- }
-
-}
--- /dev/null
+/*
+ * 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.batch.sensor.internal;
+
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+
+import org.junit.Test;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.measures.CoreMetrics;
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultSensorDescriptorTest {
+
+ @Test
+ public void describe() {
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
+ descriptor
+ .name("Foo")
+ .dependsOn(CoreMetrics.NCLOC)
+ .provides(CoreMetrics.BLOCKER_VIOLATIONS)
+ .workOnLanguages("java", "php")
+ .workOnFileTypes(InputFile.Type.MAIN);
+
+ assertThat(descriptor.name()).isEqualTo("Foo");
+ assertThat(descriptor.dependsOn()).containsOnly(CoreMetrics.NCLOC);
+ assertThat(descriptor.provides()).containsOnly(CoreMetrics.BLOCKER_VIOLATIONS);
+ assertThat(descriptor.languages()).containsOnly("java", "php");
+ assertThat(descriptor.types()).containsOnly(InputFile.Type.MAIN);
+ }
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.issue.internal;
+
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.issue.internal.DefaultIssueBuilder;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.rule.RuleKey;
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultIssueTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void build_file_issue() {
+ Issue issue = new DefaultIssueBuilder()
+ .onFile(new DefaultInputFile("src/Foo.php"))
+ .ruleKey(RuleKey.of("repo", "rule"))
+ .atLine(1)
+ .effortToFix(10.0)
+ .message("Wrong way!")
+ .build();
+
+ assertThat(issue.inputFile()).isEqualTo(new DefaultInputFile("src/Foo.php"));
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
+ assertThat(issue.line()).isEqualTo(1);
+ assertThat(issue.effortToFix()).isEqualTo(10.0);
+ assertThat(issue.message()).isEqualTo("Wrong way!");
+ }
+
+ @Test
+ public void build_project_issue() {
+ Issue issue = new DefaultIssueBuilder()
+ .onProject()
+ .ruleKey(RuleKey.of("repo", "rule"))
+ .atLine(1)
+ .effortToFix(10.0)
+ .message("Wrong way!")
+ .build();
+
+ assertThat(issue.inputFile()).isNull();
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
+ assertThat(issue.line()).isEqualTo(1);
+ assertThat(issue.effortToFix()).isEqualTo(10.0);
+ assertThat(issue.message()).isEqualTo("Wrong way!");
+ }
+
+ @Test
+ public void not_allowed_to_call_onFile_and_onProject() {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("onFile or onProject can be called only once");
+ new DefaultIssueBuilder()
+ .onProject()
+ .onFile(new DefaultInputFile("src/Foo.php"))
+ .ruleKey(RuleKey.of("repo", "rule"))
+ .atLine(1)
+ .effortToFix(10.0)
+ .message("Wrong way!")
+ .build();
+
+ }
+
+}
--- /dev/null
+/*
+ * 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.batch.sensor.measure.internal;
+
+import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.measures.CoreMetrics;
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultMeasureTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void build_file_measure() {
+ Measure<Integer> issue = new DefaultMeasureBuilder<Integer>()
+ .forMetric(CoreMetrics.LINES)
+ .onFile(new DefaultInputFile("src/Foo.php"))
+ .withValue(3)
+ .build();
+
+ assertThat(issue.inputFile()).isEqualTo(new DefaultInputFile("src/Foo.php"));
+ assertThat(issue.metric()).isEqualTo(CoreMetrics.LINES);
+ assertThat(issue.value()).isEqualTo(3);
+ }
+
+ @Test
+ public void build_project_measure() {
+ Measure<Integer> issue = new DefaultMeasureBuilder<Integer>()
+ .forMetric(CoreMetrics.LINES)
+ .onProject()
+ .withValue(3)
+ .build();
+
+ assertThat(issue.inputFile()).isNull();
+ assertThat(issue.metric()).isEqualTo(CoreMetrics.LINES);
+ assertThat(issue.value()).isEqualTo(3);
+ }
+
+ @Test
+ public void not_allowed_to_call_onFile_and_onProject() {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("onFile or onProject can be called only once");
+ new DefaultMeasureBuilder<Integer>()
+ .onProject()
+ .onFile(new DefaultInputFile("src/Foo.php"))
+ .withValue(3)
+ .build();
+ }
+
+}