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.

ProjectAnalysisTaskContainerPopulatorTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.container;
  21. import com.google.common.base.Function;
  22. import com.google.common.collect.ImmutableList;
  23. import java.lang.reflect.Modifier;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import java.util.Objects;
  28. import java.util.Set;
  29. import java.util.stream.Collectors;
  30. import javax.annotation.Nonnull;
  31. import org.junit.Test;
  32. import org.picocontainer.DefaultPicoContainer;
  33. import org.picocontainer.PicoContainer;
  34. import org.reflections.Reflections;
  35. import org.sonar.ce.task.CeTask;
  36. import org.sonar.ce.task.container.TaskContainer;
  37. import org.sonar.ce.task.projectanalysis.step.PersistComponentsStep;
  38. import org.sonar.ce.task.step.ComputationStep;
  39. import org.sonar.core.platform.ComponentContainer;
  40. import org.sonar.core.util.stream.MoreCollectors;
  41. import static com.google.common.collect.Sets.difference;
  42. import static org.assertj.core.api.Assertions.assertThat;
  43. import static org.mockito.Mockito.mock;
  44. import static org.mockito.Mockito.when;
  45. public class ProjectAnalysisTaskContainerPopulatorTest {
  46. private static final String PROJECTANALYSIS_STEP_PACKAGE = "org.sonar.ce.task.projectanalysis.step";
  47. private CeTask task = mock(CeTask.class);
  48. private ProjectAnalysisTaskContainerPopulator underTest;
  49. @Test
  50. public void item_is_added_to_the_container() {
  51. underTest = new ProjectAnalysisTaskContainerPopulator(task, null);
  52. AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
  53. underTest.populateContainer(container);
  54. assertThat(container.added).contains(task);
  55. }
  56. @Test
  57. public void all_computation_steps_are_added_in_order_to_the_container() {
  58. underTest = new ProjectAnalysisTaskContainerPopulator(task, null);
  59. AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
  60. underTest.populateContainer(container);
  61. Set<String> computationStepClassNames = container.added.stream()
  62. .map(s -> {
  63. if (s instanceof Class) {
  64. return (Class<?>) s;
  65. }
  66. return null;
  67. })
  68. .filter(Objects::nonNull)
  69. .filter(ComputationStep.class::isAssignableFrom)
  70. .map(Class::getCanonicalName)
  71. .collect(MoreCollectors.toSet());
  72. assertThat(difference(retrieveStepPackageStepsCanonicalNames(PROJECTANALYSIS_STEP_PACKAGE), computationStepClassNames)).isEmpty();
  73. }
  74. /**
  75. * Compute set of canonical names of classes implementing ComputationStep in the specified package using reflection.
  76. */
  77. private static Set<Object> retrieveStepPackageStepsCanonicalNames(String packageName) {
  78. Reflections reflections = new Reflections(packageName);
  79. return reflections.getSubTypesOf(ComputationStep.class).stream()
  80. .filter(input -> !Modifier.isAbstract(input.getModifiers()))
  81. .map(Class::getCanonicalName)
  82. .filter(Objects::nonNull)
  83. .collect(Collectors.toSet());
  84. }
  85. @Test
  86. public void at_least_one_core_step_is_added_to_the_container() {
  87. underTest = new ProjectAnalysisTaskContainerPopulator(task, null);
  88. AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
  89. underTest.populateContainer(container);
  90. assertThat(container.added).contains(PersistComponentsStep.class);
  91. }
  92. @Test
  93. public void Components_of_ReportAnalysisComponentProvider_are_added_to_the_container() {
  94. Object object = new Object();
  95. Class<MyClass> clazz = MyClass.class;
  96. ReportAnalysisComponentProvider componentProvider = mock(ReportAnalysisComponentProvider.class);
  97. when(componentProvider.getComponents()).thenReturn(ImmutableList.of(object, clazz));
  98. underTest = new ProjectAnalysisTaskContainerPopulator(task, new ReportAnalysisComponentProvider[] {componentProvider});
  99. AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
  100. container.add(componentProvider);
  101. underTest.populateContainer(container);
  102. assertThat(container.added).contains(object, clazz);
  103. }
  104. private static final class MyClass {
  105. }
  106. private static class AddedObjectsRecorderTaskContainer implements TaskContainer {
  107. private static final DefaultPicoContainer SOME_EMPTY_PICO_CONTAINER = new DefaultPicoContainer();
  108. private List<Object> added = new ArrayList<>();
  109. @Override
  110. public void bootup() {
  111. // no effect
  112. }
  113. @Override
  114. public ComponentContainer getParent() {
  115. throw new UnsupportedOperationException("getParent is not implemented");
  116. }
  117. @Override
  118. public void close() {
  119. throw new UnsupportedOperationException("cleanup is not implemented");
  120. }
  121. @Override
  122. public PicoContainer getPicoContainer() {
  123. return SOME_EMPTY_PICO_CONTAINER;
  124. }
  125. @Override
  126. public ComponentContainer add(Object... objects) {
  127. added.addAll(Arrays.asList(objects));
  128. return null; // not used anyway
  129. }
  130. @Override
  131. public ComponentContainer addSingletons(Iterable<?> components) {
  132. for (Object component : components) {
  133. added.add(component);
  134. }
  135. return null; // not used anyway
  136. }
  137. @Override
  138. public <T> T getComponentByType(Class<T> type) {
  139. for (Object add : added) {
  140. if (add.getClass().getSimpleName().contains(type.getSimpleName())) {
  141. return (T) add;
  142. }
  143. }
  144. return null;
  145. }
  146. @Override
  147. public <T> List<T> getComponentsByType(final Class<T> type) {
  148. return added.stream()
  149. .filter(input -> input.getClass().getSimpleName().contains(type.getSimpleName()))
  150. .map(input -> (T) input)
  151. .collect(Collectors.toList());
  152. }
  153. }
  154. }