]> source.dussan.org Git - sonarqube.git/blob
1435f1ec54e2d0ed0004d06ae3918aa711e9e11e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.step;
21
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;
31
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;
40
41 public class ComputationStepExecutorTest {
42   @Rule
43   public LogTester logTester = new LogTester();
44   @Rule
45   public ExpectedException expectedException = ExpectedException.none();
46
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");
51
52   private CeLogging ceLogging = new CeLogging();
53
54   @Test
55   public void execute_call_execute_on_each_ComputationStep_in_order_returned_by_instances_method() {
56     new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2, computationStep3), ceLogging)
57       .execute();
58
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();
67   }
68
69   @Test
70   public void execute_let_exception_thrown_by_ComputationStep_go_up_as_is() {
71     String message = "Exception should go up";
72
73     ComputationStep computationStep = mockComputationStep("step1");
74     doThrow(new RuntimeException(message))
75       .when(computationStep)
76       .execute();
77
78     ComputationStepExecutor computationStepExecutor = new ComputationStepExecutor(mockComputationSteps(computationStep), ceLogging);
79
80     expectedException.expect(RuntimeException.class);
81     expectedException.expectMessage(message);
82
83     computationStepExecutor.execute();
84   }
85
86   @Test
87   public void execute_logs_end_timing_for_each_ComputationStep_called() {
88     new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2), ceLogging)
89       .execute();
90
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=");
95   }
96
97   @Test
98   public void execute_calls_listener_finished_method_with_all_step_runs() {
99     new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2), ceLogging, listener)
100       .execute();
101
102     verify(listener).finished(true);
103     verifyNoMoreInteractions(listener);
104   }
105
106   @Test
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");
109     doThrow(toBeThrown)
110       .when(computationStep1)
111       .execute();
112
113     try {
114       new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2), ceLogging, listener)
115         .execute();
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);
121     }
122
123   }
124
125   private static ComputationSteps mockComputationSteps(ComputationStep... computationSteps) {
126     ComputationSteps steps = mock(ComputationSteps.class);
127     when(steps.instances()).thenReturn(Arrays.asList(computationSteps));
128     return steps;
129   }
130
131   private static ComputationStep mockComputationStep(String desc) {
132     ComputationStep mock = mock(ComputationStep.class);
133     when(mock.getDescription()).thenReturn(desc);
134     return mock;
135   }
136 }