You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DuplicationRepositoryRule.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.duplication;
  21. import com.google.common.collect.ArrayListMultimap;
  22. import com.google.common.collect.Multimap;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.stream.Collectors;
  26. import javax.annotation.CheckForNull;
  27. import org.junit.rules.ExternalResource;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.ComponentProvider;
  30. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  31. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderComponentProvider;
  32. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  33. import static com.google.common.base.Preconditions.checkArgument;
  34. import static java.util.Objects.requireNonNull;
  35. public class DuplicationRepositoryRule extends ExternalResource implements DuplicationRepository {
  36. @CheckForNull
  37. private final ComponentProvider componentProvider;
  38. private DuplicationRepositoryImpl delegate;
  39. private final Multimap<Component, TextBlock> componentRefsWithInnerDuplications = ArrayListMultimap.create();
  40. private final Multimap<Component, TextBlock> componentRefsWithInProjectDuplications = ArrayListMultimap.create();
  41. private final Multimap<Component, TextBlock> componentRefsWithCrossProjectDuplications = ArrayListMultimap.create();
  42. private DuplicationRepositoryRule(TreeRootHolder treeRootHolder) {
  43. this.componentProvider = new TreeRootHolderComponentProvider(treeRootHolder);
  44. }
  45. public DuplicationRepositoryRule() {
  46. this.componentProvider = null;
  47. }
  48. public static DuplicationRepositoryRule create(TreeRootHolderRule treeRootHolder) {
  49. return new DuplicationRepositoryRule(treeRootHolder);
  50. }
  51. public static DuplicationRepositoryRule create() {
  52. return new DuplicationRepositoryRule();
  53. }
  54. @Override
  55. protected void before() {
  56. this.delegate = new DuplicationRepositoryImpl();
  57. }
  58. @Override
  59. protected void after() {
  60. if (this.componentProvider != null) {
  61. this.componentProvider.reset();
  62. }
  63. this.componentRefsWithInnerDuplications.clear();
  64. this.componentRefsWithInProjectDuplications.clear();
  65. this.componentRefsWithCrossProjectDuplications.clear();
  66. this.delegate = null;
  67. }
  68. public Iterable<Duplication> getDuplications(int fileRef) {
  69. ensureComponentProviderInitialized();
  70. return delegate.getDuplications(componentProvider.getByRef(fileRef));
  71. }
  72. public void add(int fileRef, Duplication duplication) {
  73. ensureComponentProviderInitialized();
  74. delegate.add(componentProvider.getByRef(fileRef), duplication);
  75. }
  76. public DuplicationRepositoryRule addDuplication(int fileRef, TextBlock original, TextBlock... duplicates) {
  77. ensureComponentProviderInitialized();
  78. Component component = componentProvider.getByRef(fileRef);
  79. checkArgument(!componentRefsWithInnerDuplications.containsEntry(component, original), "Inner duplications for file %s and original %s already set", fileRef, original);
  80. checkArgument(!componentRefsWithInProjectDuplications.containsEntry(component, original),
  81. "InProject duplications for file %s and original %s already set. Use add(int, Duplication) instead", fileRef, original);
  82. componentRefsWithInnerDuplications.put(component, original);
  83. delegate.add(
  84. component,
  85. new Duplication(
  86. original,
  87. Arrays.stream(duplicates).map(InnerDuplicate::new).collect(Collectors.toList())));
  88. return this;
  89. }
  90. public DuplicationRepositoryRule addDuplication(int fileRef, TextBlock original, int otherFileRef, TextBlock duplicate) {
  91. ensureComponentProviderInitialized();
  92. Component component = componentProvider.getByRef(fileRef);
  93. checkArgument(!componentRefsWithInProjectDuplications.containsEntry(component, original), "InProject duplications for file %s and original %s already set", fileRef, original);
  94. checkArgument(!componentRefsWithInnerDuplications.containsEntry(component, original),
  95. "Inner duplications for file %s and original %s already set. Use add(int, Duplication) instead", fileRef, original);
  96. componentRefsWithInProjectDuplications.put(component, original);
  97. delegate.add(component,
  98. new Duplication(
  99. original,
  100. Collections.singletonList(new InProjectDuplicate(componentProvider.getByRef(otherFileRef), duplicate))));
  101. return this;
  102. }
  103. public DuplicationRepositoryRule addExtendedProjectDuplication(int fileRef, TextBlock original, int otherFileRef, TextBlock duplicate) {
  104. ensureComponentProviderInitialized();
  105. Component component = componentProvider.getByRef(fileRef);
  106. checkArgument(!componentRefsWithCrossProjectDuplications.containsEntry(component, original), "CrossProject duplications for file %s and original %s already set", fileRef);
  107. componentRefsWithCrossProjectDuplications.put(component, original);
  108. delegate.add(componentProvider.getByRef(fileRef),
  109. new Duplication(
  110. original,
  111. Collections.singletonList(new InExtendedProjectDuplicate(componentProvider.getByRef(otherFileRef), duplicate))));
  112. return this;
  113. }
  114. public DuplicationRepositoryRule addCrossProjectDuplication(int fileRef, TextBlock original, String otherFileKey, TextBlock duplicate) {
  115. ensureComponentProviderInitialized();
  116. Component component = componentProvider.getByRef(fileRef);
  117. checkArgument(!componentRefsWithCrossProjectDuplications.containsEntry(component, original), "CrossProject duplications for file %s and original %s already set", fileRef);
  118. componentRefsWithCrossProjectDuplications.put(component, original);
  119. delegate.add(componentProvider.getByRef(fileRef),
  120. new Duplication(
  121. original,
  122. Collections.singletonList(new CrossProjectDuplicate(otherFileKey, duplicate))));
  123. return this;
  124. }
  125. @Override
  126. public Iterable<Duplication> getDuplications(Component file) {
  127. return delegate.getDuplications(file);
  128. }
  129. @Override
  130. public void add(Component file, Duplication duplication) {
  131. TextBlock original = duplication.getOriginal();
  132. checkArgument(!componentRefsWithInnerDuplications.containsEntry(file, original), "Inner duplications for file %s and original %s already set", file, original);
  133. checkArgument(!componentRefsWithInProjectDuplications.containsEntry(file, original), "InProject duplications for file %s and original %s already set", file, original);
  134. checkArgument(!componentRefsWithCrossProjectDuplications.containsEntry(file, original), "CrossProject duplications for file %s and original %s already set", file, original);
  135. delegate.add(file, duplication);
  136. }
  137. private void ensureComponentProviderInitialized() {
  138. requireNonNull(this.componentProvider, "Methods with component reference can not be used unless a TreeRootHolder has been provided when instantiating the rule");
  139. this.componentProvider.ensureInitialized();
  140. }
  141. }