3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.issue;
22 import com.google.common.collect.ArrayListMultimap;
23 import com.google.common.collect.ListMultimap;
24 import org.junit.rules.TestRule;
25 import org.junit.runner.Description;
26 import org.junit.runners.model.Statement;
27 import org.sonar.ce.task.projectanalysis.component.Component;
28 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
29 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
30 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
31 import org.sonar.core.issue.DefaultIssue;
33 import static com.google.common.base.Preconditions.checkArgument;
34 import static com.google.common.base.Preconditions.checkNotNull;
35 import static java.util.Arrays.asList;
36 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
39 * This rule can be used when testing a visitor that depends on {@link ComponentIssuesRepository}.
41 public class FillComponentIssuesVisitorRule extends TypeAwareVisitorAdapter implements TestRule {
43 private MutableComponentIssuesRepository issuesRepository = new ComponentIssuesRepositoryImpl();
44 private final TreeRootHolder treeRootHolder;
46 private ListMultimap<Component, DefaultIssue> issues = ArrayListMultimap.create();
48 public FillComponentIssuesVisitorRule(MutableComponentIssuesRepository issuesRepository, TreeRootHolder treeRootHolder) {
49 super(CrawlerDepthLimit.FILE, POST_ORDER);
50 this.issuesRepository = issuesRepository;
51 this.treeRootHolder = treeRootHolder;
55 public Statement apply(final Statement statement, Description description) {
56 return new Statement() {
58 public void evaluate() throws Throwable {
62 issues = ArrayListMultimap.create();
68 public void setIssues(Component component, DefaultIssue... issues) {
69 checkNotNull(component, "component cannot be null");
70 setIssues(component.getReportAttributes().getRef(), issues);
73 public void setIssues(int componentRef, DefaultIssue... issues) {
74 Component component = treeRootHolder.getComponentByRef(componentRef);
75 checkArgument(component != null, String.format("Component '%s' does not exists in the report ", componentRef));
76 this.issues.get(component).clear();
77 this.issues.putAll(component, asList(issues));
81 public void visitAny(Component component) {
82 issuesRepository.setIssues(component, issues.get(component));