aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorEric Hartmann <hartmann.eric@gmail.com>2017-09-27 17:32:21 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-10-17 15:13:58 +0200
commit80645fca839ea97ee472f01ebb81f40af0f179f5 (patch)
tree358bef7e60d1248875557c9a8d24f63fc48cdf5f /sonar-plugin-api
parent332cb13bb6a20434d358db772386a4e87200a9a4 (diff)
downloadsonarqube-80645fca839ea97ee472f01ebb81f40af0f179f5.tar.gz
sonarqube-80645fca839ea97ee472f01ebb81f40af0f179f5.zip
SONAR-9891 Add Analysis to PostProjectAnalysisTask.ProjectAnalysis
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/Analysis.java35
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java14
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java217
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java5
4 files changed, 221 insertions, 50 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/Analysis.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/Analysis.java
new file mode 100644
index 00000000000..b8f10fd6607
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/Analysis.java
@@ -0,0 +1,35 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.ce.posttask;
+
+import java.util.Date;
+
+public interface Analysis {
+ /**
+ * UUID of the analysis
+ */
+ String getAnalysisUuid();
+
+ /**
+ * Date of the analysis.
+ */
+ Date getDate();
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java
index c63043a85e2..d8c2a07640b 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java
@@ -80,7 +80,7 @@ public interface PostProjectAnalysisTask {
* <p>
* This date is the same as the date of the project analysis report and the snapshot.
*
- * @deprecated use {@link #getAnalysisDate()} instead. When {@link #getAnalysisDate()} returns
+ * @deprecated use {@link #getAnalysis().getDate()} instead. When {@link #getAnalysis()} returns
* {@link Optional#empty() empty}, the current date will be returned.
*/
@Deprecated
@@ -92,10 +92,22 @@ public interface PostProjectAnalysisTask {
* This date is the same as the date of the project analysis report and therefore as the analysis in DB. It can be
* missing when the status of the task is {@link org.sonar.api.ce.posttask.CeTask.Status#FAILED FAILED}.
* </p>
+ * @deprecated use {@link #getAnalysis().getDate()} instead
*/
+ @Deprecated
Optional<Date> getAnalysisDate();
/**
+ * Analysis containing the UUID of the analysis and the date
+ *
+ * <p>
+ * This Analysis can be missing when the status of the task is
+ * {@link org.sonar.api.ce.posttask.CeTask.Status#FAILED FAILED}.
+ * </p>
+ */
+ Optional<Analysis> getAnalysis();
+
+ /**
* Context as defined by scanner through {@link org.sonar.api.batch.sensor.SensorContext#addContextProperty(String, String)}.
* It does not contain the settings used by scanner.
*
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java
index 2ab390b0cff..34f861791b7 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java
@@ -70,6 +70,7 @@ import static java.util.Objects.requireNonNull;
* .setName("name")
* .build())
* .at(new Date())
+ * .withAnalysisUuid("uuid")
* .withQualityGate(
* newQualityGateBuilder()
* .setId("id")
@@ -111,6 +112,7 @@ public class PostProjectAnalysisTaskTester {
@CheckForNull
private Branch branch;
private ScannerContext scannerContext;
+ private String analysisUuid;
private PostProjectAnalysisTaskTester(PostProjectAnalysisTask underTest) {
this.underTest = requireNonNull(underTest, "PostProjectAnalysisTask instance cannot be null");
@@ -177,59 +179,39 @@ public class PostProjectAnalysisTaskTester {
return this;
}
+ /**
+ * @since 6.6
+ */
+ public PostProjectAnalysisTaskTester withAnalysisUuid(@Nullable String analysisUuid) {
+ this.analysisUuid = analysisUuid;
+ return this;
+ }
+
public PostProjectAnalysisTask.ProjectAnalysis execute() {
requireNonNull(ceTask, CE_TASK_CAN_NOT_BE_NULL);
requireNonNull(project, PROJECT_CAN_NOT_BE_NULL);
requireNonNull(date, DATE_CAN_NOT_BE_NULL);
- PostProjectAnalysisTask.ProjectAnalysis projectAnalysis = new PostProjectAnalysisTask.ProjectAnalysis() {
- @Override
- public ScannerContext getScannerContext() {
- return scannerContext;
- }
-
- @Override
- public CeTask getCeTask() {
- return ceTask;
- }
-
- @Override
- public Project getProject() {
- return project;
- }
-
- @Override
- public Optional<Branch> getBranch() {
- return Optional.ofNullable(branch);
- }
-
- @Override
- public QualityGate getQualityGate() {
- return qualityGate;
- }
-
- @Override
- public Date getDate() {
- return date;
- }
-
- @Override
- public Optional<Date> getAnalysisDate() {
- return Optional.of(date);
- }
-
- @Override
- public String toString() {
- return "ProjectAnalysis{" +
- "ceTask=" + ceTask +
- ", project=" + project +
- ", date=" + date.getTime() +
- ", analysisDate=" + date.getTime() +
- ", qualityGate=" + qualityGate +
- '}';
- }
- };
- this.underTest.finished(projectAnalysis);
+ Analysis analysis = null;
+ if (date != null && analysisUuid != null) {
+ analysis = new AnalysisBuilder()
+ .setDate(date)
+ .setAnalysisUuid(analysisUuid)
+ .build();
+ }
+
+ PostProjectAnalysisTask.ProjectAnalysis projectAnalysis = new ProjectAnalysisBuilder()
+ .setCeTask(ceTask)
+ .setProject(project)
+ .setBranch(branch)
+ .setQualityGate(qualityGate)
+ .setAnalysis(analysis)
+ .setScannerContext(scannerContext)
+ .setDate(date)
+ .build();
+
+ this.underTest.
+ finished(projectAnalysis);
return projectAnalysis;
}
@@ -634,4 +616,143 @@ public class PostProjectAnalysisTaskTester {
return () -> properties;
}
}
+
+ public static final class ProjectAnalysisBuilder {
+ private CeTask ceTask;
+ private Project project;
+ private Branch branch;
+ private QualityGate qualityGate;
+ private Analysis analysis;
+ private ScannerContext scannerContext;
+ private Date date;
+
+ private ProjectAnalysisBuilder() {
+ // prevents instantiation outside PostProjectAnalysisTaskTester
+ }
+
+ public ProjectAnalysisBuilder setCeTask(CeTask ceTask) {
+ this.ceTask = ceTask;
+ return this;
+ }
+
+ public ProjectAnalysisBuilder setProject(Project project) {
+ this.project = project;
+ return this;
+ }
+
+ public ProjectAnalysisBuilder setBranch(@Nullable Branch branch) {
+ this.branch = branch;
+ return this;
+ }
+
+ public ProjectAnalysisBuilder setQualityGate(QualityGate qualityGate) {
+ this.qualityGate = qualityGate;
+ return this;
+ }
+
+ public ProjectAnalysisBuilder setAnalysis(@Nullable Analysis analysis) {
+ this.analysis = analysis;
+ return this;
+ }
+
+ public ProjectAnalysisBuilder setScannerContext(ScannerContext scannerContext) {
+ this.scannerContext = scannerContext;
+ return this;
+ }
+
+ public ProjectAnalysisBuilder setDate(Date date) {
+ this.date = date;
+ return this;
+ }
+
+ public PostProjectAnalysisTask.ProjectAnalysis build() {
+ return new PostProjectAnalysisTask.ProjectAnalysis() {
+ @Override
+ public CeTask getCeTask() {
+ return ceTask;
+ }
+
+ @Override
+ public Project getProject() {
+ return project;
+ }
+
+ @Override
+ public Optional<Branch> getBranch() {
+ return Optional.ofNullable(branch);
+ }
+
+ @CheckForNull
+ @Override
+ public QualityGate getQualityGate() {
+ return qualityGate;
+ }
+
+ @Override
+ public Date getDate() {
+ return date;
+ }
+
+ @Override
+ public Optional<Date> getAnalysisDate() {
+ return getAnalysis().map(Analysis::getDate);
+ }
+
+ @Override
+ public Optional<Analysis> getAnalysis() {
+ return Optional.ofNullable(analysis);
+ }
+
+ @Override
+ public ScannerContext getScannerContext() {
+ return scannerContext;
+ }
+
+ @Override
+ public String toString() {
+ return "ProjectAnalysis{" +
+ "ceTask=" + ceTask +
+ ", project=" + project +
+ ", date=" + date.getTime() +
+ ", analysisDate=" + date.getTime() +
+ ", qualityGate=" + qualityGate +
+ '}';
+ }
+ };
+ }
+ }
+
+ public static final class AnalysisBuilder {
+ private String analysisUuid;
+ private Date date;
+
+ private AnalysisBuilder() {
+ // prevents instantiation outside PostProjectAnalysisTaskTester
+ }
+
+ public AnalysisBuilder setAnalysisUuid(String analysisUuid) {
+ this.analysisUuid = analysisUuid;
+ return this;
+ }
+
+ public AnalysisBuilder setDate(Date date) {
+ this.date = date;
+ return this;
+ }
+
+ public Analysis build() {
+ return new Analysis() {
+
+ @Override
+ public String getAnalysisUuid() {
+ return analysisUuid;
+ }
+
+ @Override
+ public Date getDate() {
+ return date;
+ }
+ };
+ }
+ }
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java
index 7696b02c7ad..baec1f163e7 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java
@@ -20,6 +20,7 @@
package org.sonar.api.ce.posttask;
import java.util.Date;
+import org.apache.commons.lang.RandomStringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -36,6 +37,7 @@ public class PostProjectAnalysisTaskTesterTest {
private Project project = mock(Project.class);
private long someDateAsLong = 846351351684351L;
private Date someDate = new Date(someDateAsLong);
+ private String analysisUuid = RandomStringUtils.randomAlphanumeric(40);
private QualityGate qualityGate = mock(QualityGate.class);
private CaptorPostProjectAnalysisTask captorPostProjectAnalysisTask = new CaptorPostProjectAnalysisTask();
private PostProjectAnalysisTaskTester underTest = PostProjectAnalysisTaskTester.of(captorPostProjectAnalysisTask);
@@ -99,7 +101,7 @@ public class PostProjectAnalysisTaskTesterTest {
@Test
public void verify_getters_of_ProjectAnalysis_object_passed_to_PostProjectAnalysisTask() {
- underTest.withCeTask(ceTask).withProject(project).withQualityGate(qualityGate).at(someDate);
+ underTest.withCeTask(ceTask).withProject(project).withQualityGate(qualityGate).withAnalysisUuid(analysisUuid).at(someDate);
underTest.execute();
@@ -109,6 +111,7 @@ public class PostProjectAnalysisTaskTesterTest {
assertThat(projectAnalysis.getProject()).isSameAs(project);
assertThat(projectAnalysis.getDate()).isSameAs(someDate);
assertThat(projectAnalysis.getQualityGate()).isSameAs(qualityGate);
+ assertThat(projectAnalysis.getAnalysis().get().getAnalysisUuid()).isSameAs(analysisUuid);
}
@Test