]> source.dussan.org Git - sonarqube.git/blob
d3412b0882b3ee06acdac33048ee1328c7f33a5a
[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.taskprocessor;
21
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Set;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.server.computation.queue.CeTask;
28 import org.sonar.server.computation.queue.CeTaskResult;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.guava.api.Assertions.assertThat;
32
33 public class CeTaskProcessorRepositoryImplTest {
34   private static final String SOME_CE_TASK_TYPE = "some type";
35   private static final String SOME_COMPONENT_KEY = "key";
36
37   @Rule
38   public ExpectedException expectedException = ExpectedException.none();
39
40   @Test
41   public void constructor_accepts_empty_array_argument() {
42     new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {});
43   }
44
45   @Test
46   public void constructor_throws_IAE_if_two_TaskProcessor_handle_the_same_CeTask_type() {
47     expectedException.expect(IllegalArgumentException.class);
48     expectedException.expectMessage(
49       "There can be only one CeTaskProcessor instance registered as the processor for CeTask type " + SOME_CE_TASK_TYPE + ". " +
50         "More than one found. Please fix your configuration: " + SomeProcessor1.class.getName() + ", " + SomeProcessor2.class.getName());
51
52     new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {
53       new SomeProcessor1(SOME_CE_TASK_TYPE),
54       new SomeProcessor2(SOME_CE_TASK_TYPE)
55     });
56   }
57
58   @Test
59   public void constructor_throws_IAE_if_multiple_TaskProcessor_overlap_their_supported_CeTask_type() {
60     expectedException.expect(IllegalArgumentException.class);
61     expectedException.expectMessage(
62       "There can be only one CeTaskProcessor instance registered as the processor for CeTask type " + SOME_CE_TASK_TYPE + ". " +
63         "More than one found. Please fix your configuration: " + SomeProcessor1.class.getName() + ", " + SomeProcessor2.class.getName());
64
65     new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {
66       new SomeProcessor2(SOME_CE_TASK_TYPE + "_2", SOME_CE_TASK_TYPE),
67       new SomeProcessor1(SOME_CE_TASK_TYPE, SOME_CE_TASK_TYPE + "_3")
68     });
69   }
70
71   @Test
72   public void getForTask_returns_absent_if_repository_is_empty() {
73     CeTaskProcessorRepositoryImpl underTest = new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {});
74
75     assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY))).isAbsent();
76   }
77
78   @Test
79   public void getForTask_returns_absent_if_repository_does_not_contain_matching_TaskProcessor() {
80     CeTaskProcessorRepositoryImpl underTest = new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {
81       createCeTaskProcessor(SOME_CE_TASK_TYPE + "_1"),
82       createCeTaskProcessor(SOME_CE_TASK_TYPE + "_2", SOME_CE_TASK_TYPE + "_3"),
83     });
84
85     assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY))).isAbsent();
86   }
87
88   @Test
89   public void getForTask_returns_TaskProcessor_based_on_CeTask_type_only() {
90     CeTaskProcessor taskProcessor = createCeTaskProcessor(SOME_CE_TASK_TYPE);
91     CeTaskProcessorRepositoryImpl underTest = new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {taskProcessor});
92
93     assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY)).get()).isSameAs(taskProcessor);
94     assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY + "2")).get()).isSameAs(taskProcessor);
95   }
96
97   @Test
98   public void getForTask_returns_TaskProcessor_even_if_it_is_not_specific() {
99     CeTaskProcessor taskProcessor = createCeTaskProcessor(SOME_CE_TASK_TYPE + "_1", SOME_CE_TASK_TYPE, SOME_CE_TASK_TYPE + "_3");
100     CeTaskProcessorRepositoryImpl underTest = new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {taskProcessor});
101
102     assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY)).get()).isSameAs(taskProcessor);
103   }
104
105   private CeTaskProcessor createCeTaskProcessor(final String... ceTaskTypes) {
106     return new HandleTypeOnlyTaskProcessor(ceTaskTypes);
107   }
108
109   private static CeTask createCeTask(String ceTaskType, String key) {
110     return new CeTask.Builder()
111       .setType(ceTaskType)
112       .setUuid("task_uuid_" + key)
113       .setComponentKey(key).setComponentUuid("uuid_" + key).setComponentName("name_" + key)
114       .build();
115   }
116
117   private static class HandleTypeOnlyTaskProcessor implements CeTaskProcessor {
118     private final String[] ceTaskTypes;
119
120     public HandleTypeOnlyTaskProcessor(String... ceTaskTypes) {
121       this.ceTaskTypes = ceTaskTypes;
122     }
123
124     @Override
125     public Set<String> getHandledCeTaskTypes() {
126       return ImmutableSet.copyOf(ceTaskTypes);
127     }
128
129     @Override
130     public CeTaskResult process(CeTask task) {
131       throw new UnsupportedOperationException("Process is not implemented");
132     }
133   }
134
135   private static class SomeProcessor1 extends HandleTypeOnlyTaskProcessor {
136     public SomeProcessor1(String... ceTaskTypes) {
137       super(ceTaskTypes);
138     }
139   }
140
141   private static class SomeProcessor2 extends HandleTypeOnlyTaskProcessor {
142     public SomeProcessor2(String... ceTaskTypes) {
143       super(ceTaskTypes);
144     }
145   }
146 }