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.component;
22 import com.google.common.base.Function;
23 import javax.annotation.Nullable;
24 import org.junit.rules.ExternalResource;
27 * Implementation of {@link DbIdsRepository} as a JUnit {@link org.junit.Rule} which supports both
28 * {@link ViewsComponent} and {@link ReportComponent}.
30 public class MutableDbIdsRepositoryRule extends ExternalResource implements MutableDbIdsRepository {
31 private final ComponentProvider componentProvider;
32 private MutableDbIdsRepository delegate = newDelegate();
34 private MutableDbIdsRepositoryRule(ComponentProvider componentProvider) {
35 this.componentProvider = componentProvider;
38 public static MutableDbIdsRepositoryRule standalone() {
39 return new MutableDbIdsRepositoryRule(NoComponentProvider.INSTANCE);
42 public static MutableDbIdsRepositoryRule create(TreeRootHolderRule treeRootHolder) {
43 return new MutableDbIdsRepositoryRule(new TreeRootHolderComponentProvider(treeRootHolder));
46 public static MutableDbIdsRepositoryRule create(Component root) {
47 return new MutableDbIdsRepositoryRule(new TreeComponentProvider(root));
50 private static MapBasedDbIdsRepository<String> newDelegate() {
51 return new MapBasedDbIdsRepository<>(new Function<Component, String>() {
54 public String apply(Component input) {
55 return input.getType().isReportType() ? String.valueOf(input.getReportAttributes().getRef()) : input.getDbKey();
61 protected void before() {
62 this.delegate = newDelegate();
65 public DbIdsRepository setComponentId(int componentRef, long componentId) {
66 this.componentProvider.ensureInitialized();
67 return delegate.setComponentId(componentProvider.getByRef(componentRef), componentId);
71 public DbIdsRepository setComponentId(Component component, long componentId) {
72 return delegate.setComponentId(component, componentId);
76 public DbIdsRepository setDeveloperId(Developer developer, long developerId) {
77 return delegate.setDeveloperId(developer, developerId);
81 public long getDeveloperId(Developer developer) {
82 return delegate.getDeveloperId(developer);
86 public long getComponentId(Component component) {
87 return delegate.getComponentId(component);