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.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;
28 import org.sonar.server.computation.queue.CeTaskResult;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.guava.api.Assertions.assertThat;
33 public class CeTaskProcessorRepositoryImplTest {
34 private static final String SOME_CE_TASK_TYPE = "some type";
35 private static final String SOME_COMPONENT_KEY = "key";
38 public ExpectedException expectedException = ExpectedException.none();
41 public void constructor_accepts_empty_array_argument() {
42 new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {});
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());
52 new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {
53 new SomeProcessor1(SOME_CE_TASK_TYPE),
54 new SomeProcessor2(SOME_CE_TASK_TYPE)
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());
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")
72 public void getForTask_returns_absent_if_repository_is_empty() {
73 CeTaskProcessorRepositoryImpl underTest = new CeTaskProcessorRepositoryImpl(new CeTaskProcessor[] {});
75 assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY))).isAbsent();
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"),
85 assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY))).isAbsent();
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});
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);
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});
102 assertThat(underTest.getForCeTask(createCeTask(SOME_CE_TASK_TYPE, SOME_COMPONENT_KEY)).get()).isSameAs(taskProcessor);
105 private CeTaskProcessor createCeTaskProcessor(final String... ceTaskTypes) {
106 return new HandleTypeOnlyTaskProcessor(ceTaskTypes);
109 private static CeTask createCeTask(String ceTaskType, String key) {
110 return new CeTask.Builder()
112 .setUuid("task_uuid_" + key)
113 .setComponentKey(key).setComponentUuid("uuid_" + key).setComponentName("name_" + key)
117 private static class HandleTypeOnlyTaskProcessor implements CeTaskProcessor {
118 private final String[] ceTaskTypes;
120 public HandleTypeOnlyTaskProcessor(String... ceTaskTypes) {
121 this.ceTaskTypes = ceTaskTypes;
125 public Set<String> getHandledCeTaskTypes() {
126 return ImmutableSet.copyOf(ceTaskTypes);
130 public CeTaskResult process(CeTask task) {
131 throw new UnsupportedOperationException("Process is not implemented");
135 private static class SomeProcessor1 extends HandleTypeOnlyTaskProcessor {
136 public SomeProcessor1(String... ceTaskTypes) {
141 private static class SomeProcessor2 extends HandleTypeOnlyTaskProcessor {
142 public SomeProcessor2(String... ceTaskTypes) {