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.
21 package org.sonar.server.computation.issue;
23 import java.util.List;
24 import javax.annotation.CheckForNull;
25 import org.junit.rules.ExternalResource;
26 import org.sonar.core.issue.DefaultIssue;
27 import org.sonar.server.computation.component.Component;
28 import org.sonar.server.computation.component.ReportTreeRootHolder;
30 import static com.google.common.base.Preconditions.checkArgument;
31 import static com.google.common.base.Preconditions.checkNotNull;
32 import static com.google.common.base.Preconditions.checkState;
33 import static java.util.Objects.requireNonNull;
35 public class ComponentIssuesRepositoryRule extends ExternalResource implements MutableComponentIssuesRepository, ComponentIssuesRepository {
37 private final ReportTreeRootHolder reportTreeRootHolder;
40 private List<DefaultIssue> issues;
43 private Component component;
45 public ComponentIssuesRepositoryRule(ReportTreeRootHolder reportTreeRootHolder) {
46 this.reportTreeRootHolder = reportTreeRootHolder;
50 public void setIssues(Component component, List<DefaultIssue> issues) {
51 checkNotNull(component, "component cannot be null");
52 setIssues(component.getReportAttributes().getRef(), issues);
55 public void setIssues(int componentRef, List<DefaultIssue> issues) {
56 this.issues = requireNonNull(issues, "issues cannot be null");
57 Component component = reportTreeRootHolder.getComponentByRef(componentRef);
58 checkArgument(component != null, String.format("Component '%s' does not exists in the report ", componentRef));
59 this.component = component;
63 public List<DefaultIssue> getIssues(Component component) {
64 checkNotNull(component, "component cannot be null");
65 return getIssues(component.getReportAttributes().getRef());
68 public List<DefaultIssue> getIssues(int componentRef) {
69 checkState(this.component != null && this.issues != null, "Issues have not been initialized");
70 Component component = reportTreeRootHolder.getComponentByRef(componentRef);
71 checkArgument(component != null, String.format("Component '%s' does not exists in the report ", componentRef));
72 checkArgument(component == this.component,
73 String.format("Only issues from component '%s' are available, but wanted component is '%s'.",
74 this.component.getReportAttributes().getRef(), component.getReportAttributes().getRef()));