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