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.

InputComponentStoreTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.scan.filesystem;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.LinkedList;
  25. import java.util.List;
  26. import javax.annotation.Nullable;
  27. import org.junit.ClassRule;
  28. import org.junit.Test;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.sonar.api.SonarEdition;
  31. import org.sonar.api.SonarRuntime;
  32. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  33. import org.sonar.api.batch.fs.InputFile;
  34. import org.sonar.api.batch.fs.InputFile.Status;
  35. import org.sonar.api.batch.fs.InputFile.Type;
  36. import org.sonar.api.batch.fs.InputPath;
  37. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  38. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  39. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  40. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  41. import org.sonar.scanner.scan.branch.BranchConfiguration;
  42. import static java.util.Optional.ofNullable;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.assertj.core.api.Assertions.tuple;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.when;
  47. public class InputComponentStoreTest {
  48. private final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
  49. @ClassRule
  50. public static TemporaryFolder temp = new TemporaryFolder();
  51. @Test
  52. public void should_add_input_file() throws Exception {
  53. String rootModuleKey = "struts";
  54. String subModuleKey = "struts-core";
  55. File rootBaseDir = temp.newFolder();
  56. ProjectDefinition moduleDef = ProjectDefinition.create()
  57. .setKey(subModuleKey).setBaseDir(rootBaseDir).setWorkDir(temp.newFolder());
  58. ProjectDefinition rootDef = ProjectDefinition.create()
  59. .setKey(rootModuleKey).setBaseDir(rootBaseDir).setWorkDir(temp.newFolder()).addSubProject(moduleDef);
  60. DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(rootDef);
  61. DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(moduleDef);
  62. InputComponentStore store = new InputComponentStore(mock(BranchConfiguration.class), sonarRuntime);
  63. store.put(subModule);
  64. DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java")
  65. .setModuleBaseDir(rootBaseDir.toPath())
  66. .setPublish(true)
  67. .build();
  68. store.put(rootProject.key(), fooFile);
  69. store.put(subModuleKey, new TestInputFileBuilder(rootModuleKey, "src/main/java/Bar.java")
  70. .setLanguage("bla")
  71. .setPublish(false)
  72. .setType(Type.MAIN)
  73. .setStatus(Status.ADDED)
  74. .setLines(2)
  75. .setCharset(StandardCharsets.UTF_8)
  76. .setModuleBaseDir(temp.newFolder().toPath())
  77. .build());
  78. DefaultInputFile loadedFile = (DefaultInputFile) store.getFile(subModuleKey, "src/main/java/Bar.java");
  79. assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java");
  80. assertThat(loadedFile.charset()).isEqualTo(StandardCharsets.UTF_8);
  81. assertThat(store.filesByModule(rootModuleKey)).hasSize(1);
  82. assertThat(store.filesByModule(subModuleKey)).hasSize(1);
  83. assertThat(store.inputFiles()).hasSize(2);
  84. for (InputPath inputPath : store.inputFiles()) {
  85. assertThat(inputPath.relativePath()).startsWith("src/main/java/");
  86. }
  87. List<InputFile> toPublish = new LinkedList<>();
  88. store.allFilesToPublish().forEach(toPublish::add);
  89. assertThat(toPublish).containsExactly(fooFile);
  90. }
  91. static class InputComponentStoreTester extends InputComponentStore {
  92. InputComponentStoreTester(SonarRuntime sonarRuntime) {
  93. super(mock(BranchConfiguration.class), sonarRuntime);
  94. }
  95. InputFile addFile(String moduleKey, String relpath, @Nullable String language) {
  96. TestInputFileBuilder fileBuilder = new TestInputFileBuilder(moduleKey, relpath);
  97. ofNullable(language).ifPresent(fileBuilder::setLanguage);
  98. DefaultInputFile file = fileBuilder.build();
  99. put(moduleKey, file);
  100. return file;
  101. }
  102. InputFile addFile(String moduleKey, String relPath) {
  103. DefaultInputFile file = new TestInputFileBuilder(moduleKey, relPath)
  104. .build();
  105. put(moduleKey, file);
  106. return file;
  107. }
  108. }
  109. @Test
  110. public void should_add_languages_per_module_and_globally() {
  111. InputComponentStoreTester tester = new InputComponentStoreTester(sonarRuntime);
  112. String mod1Key = "mod1";
  113. tester.addFile(mod1Key, "src/main/java/Foo.java", "java");
  114. String mod2Key = "mod2";
  115. tester.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
  116. assertThat(tester.languages(mod1Key)).containsExactly("java");
  117. assertThat(tester.languages(mod2Key)).containsExactly("groovy");
  118. assertThat(tester.languages()).containsExactlyInAnyOrder("java", "groovy");
  119. }
  120. @Test
  121. public void should_find_files_per_module_and_globally() {
  122. InputComponentStoreTester tester = new InputComponentStoreTester(sonarRuntime);
  123. String mod1Key = "mod1";
  124. InputFile mod1File = tester.addFile(mod1Key, "src/main/java/Foo.java", "java");
  125. String mod2Key = "mod2";
  126. InputFile mod2File = tester.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
  127. assertThat(tester.filesByModule(mod1Key)).containsExactly(mod1File);
  128. assertThat(tester.filesByModule(mod2Key)).containsExactly(mod2File);
  129. assertThat(tester.inputFiles()).containsExactlyInAnyOrder(mod1File, mod2File);
  130. }
  131. @Test
  132. public void stores_not_analysed_c_file_count_in_sq_community_edition() {
  133. when(sonarRuntime.getEdition()).thenReturn(SonarEdition.COMMUNITY);
  134. InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime);
  135. String mod1Key = "mod1";
  136. underTest.addFile(mod1Key, "src/main/java/Foo.java", "java");
  137. underTest.addFile(mod1Key, "src/main/c/file1.c");
  138. underTest.addFile(mod1Key, "src/main/c/file2.c");
  139. String mod2Key = "mod2";
  140. underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
  141. underTest.addFile(mod2Key, "src/main/c/file3.c");
  142. assertThat(underTest.getNotAnalysedFilesByLanguage()).hasSize(1);
  143. assertThat(underTest.getNotAnalysedFilesByLanguage()).containsEntry("C", 3);
  144. }
  145. @Test
  146. public void stores_not_analysed_cpp_file_count_in_sq_community_edition() {
  147. when(sonarRuntime.getEdition()).thenReturn(SonarEdition.COMMUNITY);
  148. InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime);
  149. String mod1Key = "mod1";
  150. underTest.addFile(mod1Key, "src/main/java/Foo.java", "java");
  151. underTest.addFile(mod1Key, "src/main/c/file1.c");
  152. underTest.addFile(mod1Key, "src/main/c/file2.cpp");
  153. underTest.addFile(mod1Key, "src/main/c/file3.cxx");
  154. underTest.addFile(mod1Key, "src/main/c/file4.c++");
  155. underTest.addFile(mod1Key, "src/main/c/file5.cc");
  156. underTest.addFile(mod1Key, "src/main/c/file6.CPP");
  157. String mod2Key = "mod2";
  158. underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
  159. underTest.addFile(mod2Key, "src/main/c/file3.cpp");
  160. assertThat(underTest.getNotAnalysedFilesByLanguage()).hasSize(2);
  161. assertThat(underTest.getNotAnalysedFilesByLanguage()).containsEntry("C++", 6);
  162. }
  163. @Test
  164. public void does_not_store_not_analysed_file_counts_in_sq_non_community_editions() {
  165. when(sonarRuntime.getEdition()).thenReturn(SonarEdition.DEVELOPER);
  166. InputComponentStoreTester underTest = new InputComponentStoreTester(sonarRuntime);
  167. String mod1Key = "mod1";
  168. underTest.addFile(mod1Key, "src/main/java/Foo.java", "java");
  169. underTest.addFile(mod1Key, "src/main/java/file1.c");
  170. underTest.addFile(mod1Key, "src/main/java/file2.c");
  171. String mod2Key = "mod2";
  172. underTest.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
  173. underTest.addFile(mod2Key, "src/main/groovy/file4.c");
  174. assertThat(underTest.getNotAnalysedFilesByLanguage()).isEmpty();
  175. }
  176. }