3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.step;
22 import java.util.Arrays;
23 import java.util.List;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.mockito.InOrder;
28 import org.sonar.api.utils.log.LogTester;
29 import org.sonar.api.utils.log.LoggerLevel;
30 import org.sonar.ce.log.CeLogging;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.fail;
34 import static org.mockito.Mockito.doThrow;
35 import static org.mockito.Mockito.inOrder;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.verifyNoMoreInteractions;
39 import static org.mockito.Mockito.when;
41 public class ComputationStepExecutorTest {
43 public LogTester logTester = new LogTester();
45 public ExpectedException expectedException = ExpectedException.none();
47 private final ComputationStepExecutor.Listener listener = mock(ComputationStepExecutor.Listener.class);
48 private final ComputationStep computationStep1 = mockComputationStep("step1");
49 private final ComputationStep computationStep2 = mockComputationStep("step2");
50 private final ComputationStep computationStep3 = mockComputationStep("step3");
52 private CeLogging ceLogging = new CeLogging();
55 public void execute_call_execute_on_each_ComputationStep_in_order_returned_by_instances_method() {
56 new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2, computationStep3), ceLogging)
59 InOrder inOrder = inOrder(computationStep1, computationStep2, computationStep3);
60 inOrder.verify(computationStep1).execute();
61 inOrder.verify(computationStep1).getDescription();
62 inOrder.verify(computationStep2).execute();
63 inOrder.verify(computationStep2).getDescription();
64 inOrder.verify(computationStep3).execute();
65 inOrder.verify(computationStep3).getDescription();
66 inOrder.verifyNoMoreInteractions();
70 public void execute_let_exception_thrown_by_ComputationStep_go_up_as_is() {
71 String message = "Exception should go up";
73 ComputationStep computationStep = mockComputationStep("step1");
74 doThrow(new RuntimeException(message))
75 .when(computationStep)
78 ComputationStepExecutor computationStepExecutor = new ComputationStepExecutor(mockComputationSteps(computationStep), ceLogging);
80 expectedException.expect(RuntimeException.class);
81 expectedException.expectMessage(message);
83 computationStepExecutor.execute();
87 public void execute_logs_end_timing_for_each_ComputationStep_called() {
88 new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2), ceLogging)
91 List<String> infoLogs = logTester.logs(LoggerLevel.INFO);
92 assertThat(infoLogs).hasSize(2);
93 assertThat(infoLogs.get(0)).contains("step1 | time=");
94 assertThat(infoLogs.get(1)).contains("step2 | time=");
98 public void execute_calls_listener_finished_method_with_all_step_runs() {
99 new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2), ceLogging, listener)
102 verify(listener).finished(true);
103 verifyNoMoreInteractions(listener);
107 public void execute_calls_listener_finished_method_even_if_a_step_throws_an_exception() {
108 RuntimeException toBeThrown = new RuntimeException("simulating failing execute Step method");
110 .when(computationStep1)
114 new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2), ceLogging, listener)
116 fail("exception toBeThrown should have been raised");
117 } catch (RuntimeException e) {
118 assertThat(e).isSameAs(toBeThrown);
119 verify(listener).finished(false);
120 verifyNoMoreInteractions(listener);
125 private static ComputationSteps mockComputationSteps(ComputationStep... computationSteps) {
126 ComputationSteps steps = mock(ComputationSteps.class);
127 when(steps.instances()).thenReturn(Arrays.asList(computationSteps));
131 private static ComputationStep mockComputationStep(String desc) {
132 ComputationStep mock = mock(ComputationStep.class);
133 when(mock.getDescription()).thenReturn(desc);