import org.sonar.api.ce.posttask.QualityGate;
import org.sonar.api.ce.posttask.ScannerContext;
import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition;
public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor.Listener {
private static final PostProjectAnalysisTask[] NO_POST_PROJECT_ANALYSIS_TASKS = new PostProjectAnalysisTask[0];
+ private static final Logger LOG = Loggers.get(PostProjectAnalysisTasksExecutor.class);
+
private final org.sonar.ce.queue.CeTask ceTask;
private final AnalysisMetadataHolder analysisMetadataHolder;
private final QualityGateHolder qualityGateHolder;
ProjectAnalysis projectAnalysis = createProjectAnalysis(allStepsExecuted ? SUCCESS : FAILED);
for (PostProjectAnalysisTask postProjectAnalysisTask : postProjectAnalysisTasks) {
+ executeTask(projectAnalysis, postProjectAnalysisTask);
+ }
+ }
+
+ private static void executeTask(ProjectAnalysis projectAnalysis, PostProjectAnalysisTask postProjectAnalysisTask) {
+ try {
postProjectAnalysisTask.finished(projectAnalysis);
+ } catch (Exception e) {
+ LOG.error("Execution of task " + postProjectAnalysisTask.getClass() + " failed", e);
}
}
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
assertThat(scannerContext.getProperties()).containsExactly(entry("foo", "bar"));
}
+ @Test
+ @UseDataProvider("booleanValues")
+ public void finished_does_not_fail_if_listener_throws_exception_and_execute_subsequent_listeners(boolean allStepsExecuted) {
+ PostProjectAnalysisTask postProjectAnalysisTask1 = mock(PostProjectAnalysisTask.class);
+ PostProjectAnalysisTask postProjectAnalysisTask2 = mock(PostProjectAnalysisTask.class);
+ PostProjectAnalysisTask postProjectAnalysisTask3 = mock(PostProjectAnalysisTask.class);
+ InOrder inOrder = inOrder(postProjectAnalysisTask1, postProjectAnalysisTask2, postProjectAnalysisTask3);
+
+ doThrow(new RuntimeException("Faking a listener throws an exception"))
+ .when(postProjectAnalysisTask2)
+ .finished(any(PostProjectAnalysisTask.ProjectAnalysis.class));
+
+ new PostProjectAnalysisTasksExecutor(
+ ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader,
+ system2, new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2, postProjectAnalysisTask3})
+ .finished(allStepsExecuted);
+
+ inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture());
+ inOrder.verify(postProjectAnalysisTask2).finished(projectAnalysisArgumentCaptor.capture());
+ inOrder.verify(postProjectAnalysisTask3).finished(projectAnalysisArgumentCaptor.capture());
+ inOrder.verifyNoMoreInteractions();
+ }
+
@DataProvider
public static Object[][] booleanValues() {
return new Object[][] {