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.

ComponentsPublisherTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.scanner.report;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.file.Path;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.TemporaryFolder;
  28. import org.sonar.api.CoreProperties;
  29. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  30. import org.sonar.api.batch.fs.InputFile;
  31. import org.sonar.api.batch.fs.InputFile.Type;
  32. import org.sonar.api.utils.DateUtils;
  33. import org.sonar.scanner.ProjectInfo;
  34. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  35. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  36. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  37. import org.sonar.scanner.protocol.output.FileStructure;
  38. import org.sonar.scanner.protocol.output.ScannerReport.Component;
  39. import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus;
  40. import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType;
  41. import org.sonar.scanner.protocol.output.ScannerReportReader;
  42. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  43. import org.sonar.scanner.scan.branch.BranchConfiguration;
  44. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  45. import static org.assertj.core.api.Assertions.assertThat;
  46. import static org.mockito.Mockito.mock;
  47. import static org.mockito.Mockito.when;
  48. public class ComponentsPublisherTest {
  49. @Rule
  50. public TemporaryFolder temp = new TemporaryFolder();
  51. private File outputDir;
  52. private ScannerReportWriter writer;
  53. private BranchConfiguration branchConfiguration;
  54. @Before
  55. public void setUp() throws IOException {
  56. branchConfiguration = mock(BranchConfiguration.class);
  57. outputDir = temp.newFolder();
  58. writer = new ScannerReportWriter(outputDir);
  59. }
  60. @Test
  61. public void add_components_to_report() throws Exception {
  62. ProjectInfo projectInfo = mock(ProjectInfo.class);
  63. when(projectInfo.getAnalysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
  64. ProjectDefinition rootDef = ProjectDefinition.create()
  65. .setKey("foo")
  66. .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0")
  67. .setName("Root project")
  68. .setDescription("Root description")
  69. .setBaseDir(temp.newFolder())
  70. .setWorkDir(temp.newFolder());
  71. DefaultInputProject project = new DefaultInputProject(rootDef, 1);
  72. InputComponentStore store = new InputComponentStore(branchConfiguration);
  73. Path moduleBaseDir = temp.newFolder().toPath();
  74. ProjectDefinition module1Def = ProjectDefinition.create()
  75. .setKey("module1")
  76. .setName("Module1")
  77. .setDescription("Module description")
  78. .setBaseDir(moduleBaseDir.toFile())
  79. .setWorkDir(temp.newFolder());
  80. rootDef.addSubProject(module1Def);
  81. DefaultInputFile file = new TestInputFileBuilder("foo", "module1/src/Foo.java", 4).setLines(2).setStatus(InputFile.Status.SAME).build();
  82. store.put("module1", file);
  83. DefaultInputFile file18 = new TestInputFileBuilder("foo", "module1/src2/Foo.java", 18).setLines(2).setStatus(InputFile.Status.SAME).build();
  84. store.put("module1", file18);
  85. DefaultInputFile file2 = new TestInputFileBuilder("foo", "module1/src/Foo2.java", 5).setPublish(false).setLines(2).build();
  86. store.put("module1", file2);
  87. DefaultInputFile fileWithoutLang = new TestInputFileBuilder("foo", "module1/src/make", 6).setLines(10).setStatus(InputFile.Status.CHANGED).build();
  88. store.put("module1", fileWithoutLang);
  89. DefaultInputFile testFile = new TestInputFileBuilder("foo", "module1/test/FooTest.java", 7).setType(Type.TEST).setStatus(InputFile.Status.ADDED).setLines(4).build();
  90. store.put("module1", testFile);
  91. ComponentsPublisher publisher = new ComponentsPublisher(project, store);
  92. publisher.publish(writer);
  93. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
  94. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue();
  95. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue();
  96. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isTrue();
  97. // not marked for publishing
  98. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isFalse();
  99. // no such reference
  100. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 8)).isFalse();
  101. ScannerReportReader reader = new ScannerReportReader(outputDir);
  102. Component rootProtobuf = reader.readComponent(1);
  103. assertThat(rootProtobuf.getKey()).isEqualTo("foo");
  104. assertThat(rootProtobuf.getDescription()).isEqualTo("Root description");
  105. assertThat(rootProtobuf.getLinkCount()).isEqualTo(0);
  106. assertThat(reader.readComponent(4).getStatus()).isEqualTo(FileStatus.SAME);
  107. assertThat(reader.readComponent(6).getStatus()).isEqualTo(FileStatus.CHANGED);
  108. assertThat(reader.readComponent(7).getStatus()).isEqualTo(FileStatus.ADDED);
  109. }
  110. @Test
  111. public void publish_unchanged_components_even_in_short_branches() throws IOException {
  112. when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
  113. ProjectInfo projectInfo = mock(ProjectInfo.class);
  114. when(projectInfo.getAnalysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
  115. Path baseDir = temp.newFolder().toPath();
  116. ProjectDefinition rootDef = ProjectDefinition.create()
  117. .setKey("foo")
  118. .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0")
  119. .setName("Root project")
  120. .setDescription("Root description")
  121. .setBaseDir(baseDir.toFile())
  122. .setWorkDir(temp.newFolder());
  123. DefaultInputProject project = new DefaultInputProject(rootDef, 1);
  124. InputComponentStore store = new InputComponentStore(branchConfiguration);
  125. DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.java", 5)
  126. .setLines(2)
  127. .setPublish(true)
  128. .setStatus(InputFile.Status.ADDED)
  129. .build();
  130. store.put("foo", file);
  131. DefaultInputFile file2 = new TestInputFileBuilder("foo", "src2/Foo2.java", 6)
  132. .setPublish(true)
  133. .setStatus(InputFile.Status.SAME)
  134. .setLines(2)
  135. .build();
  136. store.put("foo", file2);
  137. ComponentsPublisher publisher = new ComponentsPublisher(project, store);
  138. publisher.publish(writer);
  139. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue();
  140. // do not skip, needed for computing overall coverage
  141. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue();
  142. }
  143. @Test
  144. public void publish_project_without_version_and_name() throws IOException {
  145. ProjectInfo projectInfo = mock(ProjectInfo.class);
  146. when(projectInfo.getAnalysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
  147. ProjectDefinition rootDef = ProjectDefinition.create()
  148. .setKey("foo")
  149. .setDescription("Root description")
  150. .setBaseDir(temp.newFolder())
  151. .setWorkDir(temp.newFolder());
  152. DefaultInputProject project = new DefaultInputProject(rootDef, 1);
  153. InputComponentStore store = new InputComponentStore(branchConfiguration);
  154. ComponentsPublisher publisher = new ComponentsPublisher(project, store);
  155. publisher.publish(writer);
  156. assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
  157. ScannerReportReader reader = new ScannerReportReader(outputDir);
  158. Component rootProtobuf = reader.readComponent(1);
  159. assertThat(rootProtobuf.getKey()).isEqualTo("foo");
  160. assertThat(rootProtobuf.getName()).isEqualTo("");
  161. assertThat(rootProtobuf.getDescription()).isEqualTo("Root description");
  162. assertThat(rootProtobuf.getLinkCount()).isEqualTo(0);
  163. }
  164. @Test
  165. public void publish_project_with_links() throws Exception {
  166. ProjectInfo projectInfo = mock(ProjectInfo.class);
  167. when(projectInfo.getAnalysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
  168. ProjectDefinition rootDef = ProjectDefinition.create()
  169. .setKey("foo")
  170. .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0")
  171. .setName("Root project")
  172. .setProperty(CoreProperties.LINKS_HOME_PAGE, "http://home")
  173. .setProperty(CoreProperties.LINKS_CI, "http://ci")
  174. .setDescription("Root description")
  175. .setBaseDir(temp.newFolder())
  176. .setWorkDir(temp.newFolder());
  177. DefaultInputProject project = new DefaultInputProject(rootDef, 1);
  178. InputComponentStore store = new InputComponentStore(branchConfiguration);
  179. ComponentsPublisher publisher = new ComponentsPublisher(project, store);
  180. publisher.publish(writer);
  181. ScannerReportReader reader = new ScannerReportReader(outputDir);
  182. Component rootProtobuf = reader.readComponent(1);
  183. assertThat(rootProtobuf.getLinkCount()).isEqualTo(2);
  184. assertThat(rootProtobuf.getLink(0).getType()).isEqualTo(ComponentLinkType.HOME);
  185. assertThat(rootProtobuf.getLink(0).getHref()).isEqualTo("http://home");
  186. assertThat(rootProtobuf.getLink(1).getType()).isEqualTo(ComponentLinkType.CI);
  187. assertThat(rootProtobuf.getLink(1).getHref()).isEqualTo("http://ci");
  188. }
  189. }