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.base.Optional;
23 import java.util.HashMap;
26 import org.junit.rules.ExternalResource;
27 import org.sonar.server.computation.queue.CeTask;
28 import org.sonar.server.computation.queue.CeTaskResult;
30 import static com.google.common.base.Preconditions.checkState;
31 import static java.util.Objects.requireNonNull;
34 * A {@link org.junit.Rule} that implements the {@link CeTaskProcessorRepository} interface and
35 * requires consumer to explicitly define if a specific Task type has an associated {@link CeTaskProcessor} or not.
37 public class CeTaskProcessorRepositoryRule extends ExternalResource implements CeTaskProcessorRepository {
39 private final Map<String, CeTaskProcessor> index = new HashMap<>();
42 protected void after() {
46 public CeTaskProcessorRepositoryRule setNoProcessorForTask(String taskType) {
47 index.put(requireNonNull(taskType), NoCeTaskProcessor.INSTANCE);
51 public CeTaskProcessorRepositoryRule setProcessorForTask(String taskType, CeTaskProcessor taskProcessor) {
52 index.put(requireNonNull(taskType), requireNonNull(taskProcessor));
57 public Optional<CeTaskProcessor> getForCeTask(CeTask ceTask) {
58 CeTaskProcessor taskProcessor = index.get(ceTask.getType());
59 checkState(taskProcessor != null, "CeTaskProcessor was not set in rule for task %s", ceTask);
60 return taskProcessor instanceof NoCeTaskProcessor ? Optional.<CeTaskProcessor>absent() : Optional.of(taskProcessor);
63 private enum NoCeTaskProcessor implements CeTaskProcessor {
66 private static final String UOE_MESSAGE = "NoCeTaskProcessor does not implement any method since it not supposed to be ever used";
69 public Set<String> getHandledCeTaskTypes() {
70 throw new UnsupportedOperationException(UOE_MESSAGE);
74 public CeTaskResult process(CeTask task) {
75 throw new UnsupportedOperationException(UOE_MESSAGE);