diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-19 18:08:26 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-22 10:49:34 +0200 |
commit | 14f36bfb794a37ac67b643924e8608421a8f549c (patch) | |
tree | b8cf4ee1232bcd5376fb5801a0901a340d48f626 /sonar-plugin-api | |
parent | 78a1280ead8a756ca52dc13ce31b17e08ab2f703 (diff) | |
download | sonarqube-14f36bfb794a37ac67b643924e8608421a8f549c.tar.gz sonarqube-14f36bfb794a37ac67b643924e8608421a8f549c.zip |
SONAR-5389 Refactor new issue API
Diffstat (limited to 'sonar-plugin-api')
12 files changed, 224 insertions, 326 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java index f01ab0fa900..a0be6e447ab 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java @@ -19,7 +19,6 @@ */ package org.sonar.api.batch.sensor; -import com.google.common.annotations.Beta; import org.sonar.api.BatchExtension; /** @@ -32,9 +31,8 @@ import org.sonar.api.BatchExtension; * For example the Cobertura Sensor parses Cobertura report and saves the first-level of measures on files. * </p> * - * @since 4.4 + * @since 5.0 */ -@Beta public interface Sensor extends BatchExtension { /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index 016bda15f94..b79471f7975 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -19,7 +19,6 @@ */ package org.sonar.api.batch.sensor; -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.rule.ActiveRules; @@ -28,7 +27,6 @@ import org.sonar.api.batch.sensor.duplication.DuplicationGroup; import org.sonar.api.batch.sensor.duplication.DuplicationTokenBuilder; import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; 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.symbol.SymbolTableBuilder; import org.sonar.api.batch.sensor.test.TestCase; @@ -41,9 +39,9 @@ import java.io.Serializable; import java.util.List; /** - * @since 4.4 + * See {@link Sensor#execute(SensorContext)} + * @since 5.0 */ -@Beta public interface SensorContext { /** @@ -64,26 +62,16 @@ public interface SensorContext { // ----------- MEASURES -------------- /** - * Builder to create a new {@link Measure}. + * Fluent builder to create a new {@link Measure}. Don't forget to call {@link Measure#save()} once all parameters are provided. */ <G extends Serializable> Measure<G> newMeasure(); // ----------- ISSUES -------------- /** - * Builder to create a new {@link Issue}. + * Fluent builder to create a new {@link Issue}. Don't forget to call {@link Issue#save()} once all parameters are provided. */ - IssueBuilder issueBuilder(); - - /** - * Add an issue. Use {@link #issueBuilder()} to create the new issue. - * @return <code>true</code> if the new issue is registered, <code>false</code> if: - * <ul> - * <li>the rule does not exist</li> - * <li>the rule is disabled in the Quality profile</li> - * </ul> - */ - boolean addIssue(Issue issue); + Issue newIssue(); // ------------ HIGHLIGHTING ------------ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java index 9fcd030e553..a2187c13c49 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java @@ -19,16 +19,15 @@ */ 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 + * See {@link Sensor#describe(SensorDescriptor)} + * @since 5.0 */ -@Beta public interface SensorDescriptor { /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java index c48035c8101..ce2bbc1dbd3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java @@ -19,11 +19,17 @@ */ package org.sonar.api.batch.sensor; +import org.sonar.api.batch.sensor.issue.Issue; +import org.sonar.api.batch.sensor.measure.Measure; + /** * Interface for storing data computed by sensors. + * @since 5.0 */ -public interface SensorStorage<G> { +public interface SensorStorage { + + void store(Measure measure); - void store(G data); + void store(Issue issue); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java index a8d2512542a..e126ddef95b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java @@ -20,25 +20,35 @@ package org.sonar.api.batch.sensor.issue; import com.google.common.annotations.Beta; +import org.sonar.api.batch.fs.InputDir; +import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.sensor.Sensor; import org.sonar.api.rule.RuleKey; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; /** - * Issue reported by an {@link Sensor} + * Represents an issue detected by a {@link Sensor}. * - * @since 4.4 + * @since 5.0 */ @Beta public interface Issue { + public enum Severity { + INFO, + MINOR, + MAJOR, + CRITICAL, + BLOCKER; + } + /** - * The {@link InputPath} this issue belongs to. Returns null if issue is global to the project. + * The {@link RuleKey} of the issue. */ - @CheckForNull - InputPath inputPath(); + Issue ruleKey(RuleKey ruleKey); /** * The {@link RuleKey} of this issue. @@ -46,10 +56,31 @@ public interface Issue { RuleKey ruleKey(); /** - * Message of the issue. + * The {@link InputFile} the issue belongs to. For global issues call {@link #onProject()}. + */ + Issue onFile(InputFile file); + + /** + * The {@link InputDir} the issue belongs to. For global issues call {@link #onProject()}. + */ + Issue onDir(InputDir inputDir); + + /** + * Tell that the issue is global to the project. + */ + Issue onProject(); + + /** + * The {@link InputPath} this issue belongs to. Returns null if issue is global to the project. */ @CheckForNull - String message(); + InputPath inputPath(); + + /** + * Line of the issue. Only available for {@link #onFile(InputFile)} issues. + * If no line is specified it means that issue is global to the file. + */ + Issue atLine(int line); /** * Line of the issue. Null for global issues and issues on directories. Can also be null @@ -59,16 +90,43 @@ public interface Issue { Integer line(); /** + * Effort to fix the issue. + */ + Issue effortToFix(@Nullable Double effortToFix); + + /** * Effort to fix the issue. Used by technical debt model. */ @CheckForNull Double effortToFix(); /** - * See constants in {@link org.sonar.api.rule.Severity}. - * Can be null before issue is saved. Means to use severity configured in quality profile. + * Message of the issue. + */ + Issue message(String message); + + /** + * Message of the issue. */ @CheckForNull - String severity(); + String message(); + + /** + * Override severity of the issue. + * Setting a null value or not calling this method means to use severity configured in quality profile. + */ + Issue overrideSeverity(@Nullable Severity severity); + + /** + * Overriden severity. + */ + @CheckForNull + Severity overridenSeverity(); + + /** + * Save the issue. If rule key is unknow or rule not enabled in the current quality profile then a warning is logged but no exception + * is thrown. + */ + void save(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/IssueBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/IssueBuilder.java deleted file mode 100644 index f6b4fa85aaa..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/IssueBuilder.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.batch.sensor.issue; - -import com.google.common.annotations.Beta; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rule.Severity; - -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); - - /** - * The {@link InputDir} the issue belongs to. For global issues call {@link #onProject()}. - */ - IssueBuilder onDir(InputDir inputDir); - - /** - * Tell that the issue is global to the project. - */ - IssueBuilder onProject(); - - /** - * Line of the issue. Only available for {@link #onFile(InputFile)} issues. - * If no line is specified it means that issue is 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); - - /** - * Severity of the issue. See {@link Severity}. - * Setting a null value means to use severity configured in quality profile. - */ - IssueBuilder severity(@Nullable String severity); - - /** - * Build the issue. After call of this method the builder is cleaned and can be used to build another issue. - */ - Issue build(); - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java index f3b56f73b6c..23148378f35 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java @@ -23,82 +23,152 @@ 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.InputDir; +import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; +import org.sonar.api.batch.sensor.SensorStorage; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.rule.RuleKey; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.io.Serializable; import java.util.UUID; -public class DefaultIssue implements Issue, Serializable { +public class DefaultIssue implements Issue { - private final String key; - private final InputPath inputPath; - private final RuleKey ruleKey; + private static final String INPUT_DIR_SHOULD_BE_NON_NULL = "InputDir should be non null"; + private static final String INPUT_FILE_SHOULD_BE_NON_NULL = "InputFile should be non null"; + private static final String ON_FILE_OR_ON_DIR_ALREADY_CALLED = "onFile or onDir already called"; + private static final String ON_PROJECT_ALREADY_CALLED = "onProject already called"; + private String key; + private boolean onProject = false; + private InputPath path; + private RuleKey ruleKey; private String message; - private final Integer line; - private final Double effortToFix; - private String severity; - - DefaultIssue(DefaultIssueBuilder builder) { - Preconditions.checkNotNull(builder.ruleKey, "ruleKey is mandatory on issue"); - this.inputPath = builder.path; - this.ruleKey = builder.ruleKey; - this.message = builder.message; - this.line = builder.line; - this.effortToFix = builder.effortToFix; - this.severity = builder.severity; - this.key = builder.key == null ? UUID.randomUUID().toString() : builder.key; - Preconditions.checkState(!Strings.isNullOrEmpty(key), "Fail to generate issue key"); + private Integer line; + private Double effortToFix; + private Severity overridenSeverity; + private final SensorStorage storage; + + public DefaultIssue() { + this.storage = null; } - public String key() { - return key; + public DefaultIssue(SensorStorage storage) { + this.storage = storage; } @Override - @Nullable - public InputPath inputPath() { - return inputPath; + public DefaultIssue ruleKey(RuleKey ruleKey) { + this.ruleKey = ruleKey; + return this; } @Override - public RuleKey ruleKey() { - return ruleKey; + public DefaultIssue onFile(InputFile file) { + Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED); + Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED); + Preconditions.checkNotNull(file, INPUT_FILE_SHOULD_BE_NON_NULL); + this.path = file; + return this; } - @CheckForNull @Override - public String message() { - return message; + public DefaultIssue onDir(InputDir dir) { + Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED); + Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED); + Preconditions.checkNotNull(dir, INPUT_DIR_SHOULD_BE_NON_NULL); + this.path = dir; + return this; + } + + @Override + public DefaultIssue onProject() { + Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED); + Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED); + this.onProject = true; + return this; + } + + @Override + public DefaultIssue atLine(int line) { + Preconditions.checkState(this.path != null && this.path instanceof InputFile, "atLine should be called after onFile"); + this.line = line; + return this; + } + + @Override + public DefaultIssue effortToFix(@Nullable Double effortToFix) { + this.effortToFix = effortToFix; + return this; } - public void setMessage(String message) { + @Override + public DefaultIssue message(String message) { this.message = message; + return this; + } + + @Override + public Issue overrideSeverity(@Nullable Severity severity) { + this.overridenSeverity = severity; + return this; + } + + @Override + public RuleKey ruleKey() { + return this.ruleKey; + } + + @CheckForNull + @Override + public InputPath inputPath() { + return this.path; } @Override public Integer line() { - return line; + return this.line; + } + + @Override + public String message() { + return this.message; + } + + @Override + public Severity overridenSeverity() { + return this.overridenSeverity; } @Override - @Nullable public Double effortToFix() { - return effortToFix; + return this.effortToFix; + } + + public String key() { + return this.key; } @Override - @CheckForNull - public String severity() { - return severity; + public void save() { + Preconditions.checkNotNull(this.storage, "No persister on this object"); + Preconditions.checkNotNull(this.ruleKey, "ruleKey is mandatory on issue"); + if (this.key == null) { + this.key = UUID.randomUUID().toString(); + } + Preconditions.checkState(!Strings.isNullOrEmpty(key), "Fail to generate issue key"); + + storage.store(this); } - public void setSeverity(String severity) { - this.severity = severity; + /** + * For testing only. + */ + public DefaultIssue withKey(String key) { + this.key = key; + return this; } @Override @@ -122,5 +192,4 @@ public class DefaultIssue implements Issue, Serializable { public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } - } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueBuilder.java deleted file mode 100644 index 10c0c71f5be..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueBuilder.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.batch.sensor.issue.internal; - -import com.google.common.base.Preconditions; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputPath; -import org.sonar.api.batch.sensor.issue.IssueBuilder; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rule.Severity; - -import javax.annotation.Nullable; - -public class DefaultIssueBuilder implements IssueBuilder { - - private static final String INPUT_DIR_SHOULD_BE_NON_NULL = "InputDir should be non null"; - private static final String INPUT_FILE_SHOULD_BE_NON_NULL = "InputFile should be non null"; - private static final String ON_FILE_OR_ON_DIR_ALREADY_CALLED = "onFile or onDir already called"; - private static final String ON_PROJECT_ALREADY_CALLED = "onProject already called"; - String key; - boolean onProject = false; - InputPath path; - RuleKey ruleKey; - String message; - Integer line; - Double effortToFix; - String severity; - - @Override - public DefaultIssueBuilder ruleKey(RuleKey ruleKey) { - this.ruleKey = ruleKey; - return this; - } - - @Override - public DefaultIssueBuilder onFile(InputFile file) { - Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED); - Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED); - Preconditions.checkNotNull(file, INPUT_FILE_SHOULD_BE_NON_NULL); - this.path = file; - return this; - } - - @Override - public DefaultIssueBuilder onDir(InputDir dir) { - Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED); - Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED); - Preconditions.checkNotNull(dir, INPUT_DIR_SHOULD_BE_NON_NULL); - this.path = dir; - return this; - } - - @Override - public DefaultIssueBuilder onProject() { - Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED); - Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED); - this.onProject = true; - return this; - } - - @Override - public DefaultIssueBuilder atLine(int line) { - Preconditions.checkState(this.path != null && this.path instanceof InputFile, "atLine should be called after onFile"); - 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; - } - - @Override - public IssueBuilder severity(@Nullable String severity) { - Preconditions.checkState(severity == null || Severity.ALL.contains(severity), "Invalid severity: " + severity); - this.severity = severity; - return this; - } - - /** - * For testing only. - */ - public DefaultIssueBuilder withKey(String key) { - this.key = key; - return this; - } - - @Override - public DefaultIssue build() { - DefaultIssue result = new DefaultIssue(this); - reset(); - return result; - } - - private void reset() { - key = null; - onProject = false; - path = null; - ruleKey = null; - message = null; - line = null; - effortToFix = null; - severity = null; - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java index 2ce2cf98361..86a0abdffad 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java @@ -19,7 +19,6 @@ */ package org.sonar.api.batch.sensor.measure; -import com.google.common.annotations.Beta; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.measure.Metric; @@ -29,9 +28,8 @@ import java.io.Serializable; /** * Builder to create new Measure. - * @since 4.4 + * @since 5.0 */ -@Beta public interface Measure<G extends Serializable> { /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java index f6b191355c7..fee7172f9ea 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java @@ -36,7 +36,7 @@ import java.io.Serializable; public class DefaultMeasure<G extends Serializable> implements Measure<G> { - private final SensorStorage<DefaultMeasure<G>> measureStorage; + private final SensorStorage storage; private boolean onProject = false; private InputFile file; private Metric<G> metric; @@ -44,11 +44,11 @@ public class DefaultMeasure<G extends Serializable> implements Measure<G> { private boolean saved = false; public DefaultMeasure() { - this.measureStorage = null; + this.storage = null; } - public DefaultMeasure(@Nullable SensorStorage<DefaultMeasure<G>> measureStorage) { - this.measureStorage = measureStorage; + public DefaultMeasure(@Nullable SensorStorage storage) { + this.storage = storage; } @Override @@ -86,14 +86,12 @@ public class DefaultMeasure<G extends Serializable> implements Measure<G> { @Override public void save() { - Preconditions.checkNotNull(this.measureStorage, "No persister on this object"); + Preconditions.checkNotNull(this.storage, "No persister on this object"); Preconditions.checkState(!saved, "This measure was already saved"); Preconditions.checkNotNull(this.value, "Measure value can't be null"); Preconditions.checkNotNull(this.metric, "Measure metric can't be null"); Preconditions.checkState(this.metric.valueType().equals(this.value.getClass()), "Measure value should be of type " + this.metric.valueType()); - if (measureStorage != null) { - measureStorage.store(this); - } + storage.store((Measure<Serializable>) this); this.saved = true; } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java index 6c0d12da82c..722f0948120 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java @@ -23,10 +23,12 @@ 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.batch.sensor.issue.Issue; +import org.sonar.api.batch.sensor.SensorStorage; import org.sonar.api.rule.RuleKey; import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class DefaultIssueTest { @@ -35,58 +37,56 @@ public class DefaultIssueTest { @Test public void build_file_issue() { - Issue issue = new DefaultIssueBuilder() + SensorStorage storage = mock(SensorStorage.class); + DefaultIssue issue = new DefaultIssue(storage) .onFile(new DefaultInputFile("foo", "src/Foo.php")) .ruleKey(RuleKey.of("repo", "rule")) .atLine(1) .effortToFix(10.0) - .message("Wrong way!") - .build(); + .message("Wrong way!"); assertThat(issue.inputPath()).isEqualTo(new DefaultInputFile("foo", "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!"); + + issue.save(); + + verify(storage).store(issue); } @Test public void build_project_issue() { - Issue issue = new DefaultIssueBuilder() + SensorStorage storage = mock(SensorStorage.class); + DefaultIssue issue = new DefaultIssue(storage) .onProject() .ruleKey(RuleKey.of("repo", "rule")) .effortToFix(10.0) - .message("Wrong way!") - .build(); + .message("Wrong way!"); assertThat(issue.inputPath()).isNull(); assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule")); assertThat(issue.line()).isNull(); assertThat(issue.effortToFix()).isEqualTo(10.0); assertThat(issue.message()).isEqualTo("Wrong way!"); + + issue.save(); + + verify(storage).store(issue); } @Test public void not_allowed_to_call_onFile_and_onProject() { thrown.expect(IllegalStateException.class); thrown.expectMessage("onProject already called"); - new DefaultIssueBuilder() + new DefaultIssue() .onProject() .onFile(new DefaultInputFile("foo", "src/Foo.php")) .ruleKey(RuleKey.of("repo", "rule")) .atLine(1) .effortToFix(10.0) - .message("Wrong way!") - .build(); - } - - @Test - public void validate_severity() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Invalid severity: FOO"); - new DefaultIssueBuilder() - .severity("FOO") - .build(); + .message("Wrong way!"); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java index 7085ca90169..7fa5fbfc593 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java @@ -37,8 +37,8 @@ public class DefaultMeasureTest { @Test public void build_file_measure() { - SensorStorage<DefaultMeasure<Integer>> persister = mock(SensorStorage.class); - DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(persister) + SensorStorage storage = mock(SensorStorage.class); + DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(storage) .forMetric(CoreMetrics.LINES) .onFile(new DefaultInputFile("foo", "src/Foo.php")) .withValue(3); @@ -49,13 +49,13 @@ public class DefaultMeasureTest { newMeasure.save(); - verify(persister).store(newMeasure); + verify(storage).store(newMeasure); } @Test public void build_project_measure() { - SensorStorage<DefaultMeasure<Integer>> persister = mock(SensorStorage.class); - DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(persister) + SensorStorage storage = mock(SensorStorage.class); + DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(storage) .forMetric(CoreMetrics.LINES) .onProject() .withValue(3); @@ -66,7 +66,7 @@ public class DefaultMeasureTest { newMeasure.save(); - verify(persister).store(newMeasure); + verify(storage).store(newMeasure); } @Test |