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.

ModuleInputComponentStoreTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.scan.filesystem;
  21. import java.io.IOException;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.TemporaryFolder;
  26. import org.sonar.api.batch.fs.InputFile;
  27. import org.sonar.api.batch.fs.InputModule;
  28. import org.sonar.api.batch.fs.internal.SensorStrategy;
  29. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  30. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  31. import org.sonar.scanner.scan.branch.BranchConfiguration;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.mockito.ArgumentMatchers.any;
  34. import static org.mockito.ArgumentMatchers.eq;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.verify;
  37. import static org.mockito.Mockito.when;
  38. public class ModuleInputComponentStoreTest {
  39. @Rule
  40. public TemporaryFolder temp = new TemporaryFolder();
  41. private InputComponentStore componentStore;
  42. private final String projectKey = "dummy key";
  43. @Before
  44. public void setUp() throws IOException {
  45. DefaultInputProject root = TestInputFileBuilder.newDefaultInputProject(projectKey, temp.newFolder());
  46. componentStore = new InputComponentStore(mock(BranchConfiguration.class));
  47. }
  48. @Test
  49. public void should_cache_files_by_filename() throws IOException {
  50. ModuleInputComponentStore store = newModuleInputComponentStore();
  51. String filename = "some name";
  52. InputFile inputFile1 = new TestInputFileBuilder(projectKey, "some/path/" + filename).build();
  53. store.doAdd(inputFile1);
  54. InputFile inputFile2 = new TestInputFileBuilder(projectKey, "other/path/" + filename).build();
  55. store.doAdd(inputFile2);
  56. InputFile dummyInputFile = new TestInputFileBuilder(projectKey, "some/path/Dummy.java").build();
  57. store.doAdd(dummyInputFile);
  58. assertThat(store.getFilesByName(filename)).containsExactlyInAnyOrder(inputFile1, inputFile2);
  59. }
  60. @Test
  61. public void should_cache_files_by_extension() throws IOException {
  62. ModuleInputComponentStore store = newModuleInputComponentStore();
  63. InputFile inputFile1 = new TestInputFileBuilder(projectKey, "some/path/Program.java").build();
  64. store.doAdd(inputFile1);
  65. InputFile inputFile2 = new TestInputFileBuilder(projectKey, "other/path/Utils.java").build();
  66. store.doAdd(inputFile2);
  67. InputFile dummyInputFile = new TestInputFileBuilder(projectKey, "some/path/NotJava.cpp").build();
  68. store.doAdd(dummyInputFile);
  69. assertThat(store.getFilesByExtension("java")).containsExactlyInAnyOrder(inputFile1, inputFile2);
  70. }
  71. @Test
  72. public void should_not_cache_duplicates() throws IOException {
  73. ModuleInputComponentStore store = newModuleInputComponentStore();
  74. String ext = "java";
  75. String filename = "Program." + ext;
  76. InputFile inputFile = new TestInputFileBuilder(projectKey, "some/path/" + filename).build();
  77. store.doAdd(inputFile);
  78. store.doAdd(inputFile);
  79. store.doAdd(inputFile);
  80. assertThat(store.getFilesByName(filename)).containsExactly(inputFile);
  81. assertThat(store.getFilesByExtension(ext)).containsExactly(inputFile);
  82. }
  83. @Test
  84. public void should_get_empty_iterable_on_cache_miss() {
  85. ModuleInputComponentStore store = newModuleInputComponentStore();
  86. String ext = "java";
  87. String filename = "Program." + ext;
  88. InputFile inputFile = new TestInputFileBuilder(projectKey, "some/path/" + filename).build();
  89. store.doAdd(inputFile);
  90. assertThat(store.getFilesByName("nonexistent")).isEmpty();
  91. assertThat(store.getFilesByExtension("nonexistent")).isEmpty();
  92. }
  93. private ModuleInputComponentStore newModuleInputComponentStore() {
  94. InputModule module = mock(InputModule.class);
  95. when(module.key()).thenReturn("moduleKey");
  96. return new ModuleInputComponentStore(module, componentStore, mock(SensorStrategy.class));
  97. }
  98. @Test
  99. public void should_find_module_components_with_non_global_strategy() {
  100. InputComponentStore inputComponentStore = mock(InputComponentStore.class);
  101. SensorStrategy strategy = new SensorStrategy();
  102. InputModule module = mock(InputModule.class);
  103. when(module.key()).thenReturn("foo");
  104. ModuleInputComponentStore store = new ModuleInputComponentStore(module, inputComponentStore, strategy);
  105. strategy.setGlobal(false);
  106. store.inputFiles();
  107. verify(inputComponentStore).filesByModule("foo");
  108. String relativePath = "somepath";
  109. store.inputFile(relativePath);
  110. verify(inputComponentStore).getFile(any(String.class), eq(relativePath));
  111. store.languages();
  112. verify(inputComponentStore).languages(any(String.class));
  113. }
  114. @Test
  115. public void should_find_all_components_with_global_strategy() {
  116. InputComponentStore inputComponentStore = mock(InputComponentStore.class);
  117. SensorStrategy strategy = new SensorStrategy();
  118. ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), inputComponentStore, strategy);
  119. strategy.setGlobal(true);
  120. store.inputFiles();
  121. verify(inputComponentStore).inputFiles();
  122. String relativePath = "somepath";
  123. store.inputFile(relativePath);
  124. verify(inputComponentStore).inputFile(relativePath);
  125. store.languages();
  126. verify(inputComponentStore).languages();
  127. }
  128. }