2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.taskprocessor;
22 import com.google.common.collect.ImmutableSet;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.server.computation.queue.CeTask;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.guava.api.Assertions.assertThat;
32 public class CeTaskProcessorRepositoryImplTest {
33 private static final String SOME_CE_TASK_TYPE = "some type";
34 private static final String SOME_COMPONENT_KEY = "key";
37 public ExpectedException expectedException = ExpectedException.none();
40 public void constructor_accepts_empty_array_argument() {
41 new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {});
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());
51 new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {
52 new SomeProcessor1(SOME_CE_TASK_TYPE),
53 new SomeProcessor2(SOME_CE_TASK_TYPE)
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());
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")
71 public void getForTask_returns_absent_if_repository_is_empty() {
72 CeTaskProcessorRepositoryImpl underTest = new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {});
74 assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY))).isAbsent();
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"),
84 assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY))).isAbsent();
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});
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);
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});
101 assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY)).get()).isSameAs(taskProcessor);
104 private CeTaskProcessor createCeTaskProcessor(final String... ceTaskTypes) {
105 return new HandleTypeOnlyTaskProcessor(ceTaskTypes);
108 private static CeTask createCeTask(String ceTaskType, String key) {
109 return new CeTask.Builder()
111 .setUuid("task_uuid_" + key)
112 .setComponentKey(key).setComponentUuid("uuid_" + key).setComponentName("name_" + key)
116 private static class HandleTypeOnlyTaskProcessor implements CeTaskProcessor {
117 private final String[] ceTaskTypes;
119 public HandleTypeOnlyTaskProcessor(String... ceTaskTypes) {
120 this.ceTaskTypes = ceTaskTypes;
124 public Set<String> getHandledCeTaskTypes() {
125 return ImmutableSet.copyOf(ceTaskTypes);
129 public void process(CeTask task) {
130 throw new UnsupportedOperationException("Process is not implemented");
134 private static class SomeProcessor1 extends HandleTypeOnlyTaskProcessor {
135 public SomeProcessor1(String... ceTaskTypes) {
140 private static class SomeProcessor2 extends HandleTypeOnlyTaskProcessor {
141 public SomeProcessor2(String... ceTaskTypes) {