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.

FileSourceDataWarningsTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.source;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Arrays;
  25. import java.util.Random;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.IntStream;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.ce.task.log.CeTaskMessages;
  32. import org.sonar.ce.task.projectanalysis.component.Component;
  33. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  34. import org.sonar.ce.task.projectanalysis.source.linereader.LineReader;
  35. import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
  36. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.times;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.verifyNoInteractions;
  41. import static org.mockito.Mockito.when;
  42. import static org.sonar.ce.task.projectanalysis.source.linereader.LineReader.Data.HIGHLIGHTING;
  43. import static org.sonar.ce.task.projectanalysis.source.linereader.LineReader.Data.SYMBOLS;
  44. @RunWith(DataProviderRunner.class)
  45. public class FileSourceDataWarningsTest {
  46. private CeTaskMessages taskMessages = mock(CeTaskMessages.class);
  47. private System2 system2 = mock(System2.class);
  48. private Random random = new Random();
  49. private int line = 1 + new Random().nextInt(200);
  50. private long timeStamp = 9_887L + new Random().nextInt(300);
  51. private String path = randomAlphabetic(50);
  52. private FileSourceDataWarnings underTest = new FileSourceDataWarnings(taskMessages, system2);
  53. @Test
  54. public void addWarning_fails_with_NPE_if_file_is_null() {
  55. LineReader.ReadError readError = new LineReader.ReadError(HIGHLIGHTING, 2);
  56. assertThatThrownBy(() -> underTest.addWarning(null, readError))
  57. .isInstanceOf(NullPointerException.class)
  58. .hasMessage("file can't be null");
  59. }
  60. @Test
  61. public void addWarning_fails_with_NPE_if_readError_is_null() {
  62. Component component = mock(Component.class);
  63. assertThatThrownBy(() -> underTest.addWarning(component, null))
  64. .isInstanceOf(NullPointerException.class)
  65. .hasMessage("readError can't be null");
  66. }
  67. @Test
  68. public void addWarnings_fails_with_ISE_if_called_after_commitWarnings() {
  69. underTest.commitWarnings();
  70. assertThatThrownBy(() -> underTest.addWarning(null /*doesn't matter*/, null /*doesn't matter*/))
  71. .isInstanceOf(IllegalStateException.class)
  72. .hasMessage("warnings already commit");
  73. }
  74. @Test
  75. public void commitWarnings_fails_with_ISE_if_called_after_commitWarnings() {
  76. underTest.commitWarnings();
  77. assertThatThrownBy(() -> underTest.commitWarnings())
  78. .isInstanceOf(IllegalStateException.class)
  79. .hasMessage("warnings already commit");
  80. }
  81. @Test
  82. public void create_highlighting_warning_when_one_file_HIGHLIGHT_read_error() {
  83. ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1)
  84. .setUuid("uuid")
  85. .setName(path)
  86. .build();
  87. LineReader.ReadError readError = new LineReader.ReadError(HIGHLIGHTING, line);
  88. when(system2.now()).thenReturn(timeStamp);
  89. underTest.addWarning(file, readError);
  90. verifyNoInteractions(taskMessages);
  91. underTest.commitWarnings();
  92. verify(taskMessages, times(1))
  93. .add(new CeTaskMessages.Message("Inconsistent highlighting data detected on file '" + path + "'. " +
  94. "File source may have been modified while analysis was running.", timeStamp));
  95. }
  96. @Test
  97. public void create_highlighting_warning_when_any_number_of_read_error_for_one_file() {
  98. ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1)
  99. .setUuid("uuid")
  100. .setName(path)
  101. .build();
  102. LineReader.ReadError[] readErrors = IntStream.range(0, 1 + random.nextInt(10))
  103. .mapToObj(i -> new LineReader.ReadError(HIGHLIGHTING, line + i))
  104. .toArray(LineReader.ReadError[]::new);
  105. when(system2.now()).thenReturn(timeStamp);
  106. Arrays.stream(readErrors).forEach(readError -> underTest.addWarning(file, readError));
  107. verifyNoInteractions(taskMessages);
  108. underTest.commitWarnings();
  109. verify(taskMessages, times(1))
  110. .add(new CeTaskMessages.Message("Inconsistent highlighting data detected on file '" + path + "'. " +
  111. "File source may have been modified while analysis was running.", timeStamp));
  112. }
  113. @Test
  114. public void create_highlighting_warning_when_any_number_of_read_error_for_less_than_5_files() {
  115. int fileCount = 2 + random.nextInt(3);
  116. Component[] files = IntStream.range(0, fileCount)
  117. .mapToObj(i -> ReportComponent.builder(Component.Type.FILE, i)
  118. .setUuid("uuid_" + i)
  119. .setName(path + "_" + i)
  120. .build())
  121. .toArray(Component[]::new);
  122. when(system2.now()).thenReturn(timeStamp);
  123. Arrays.stream(files).forEach(file -> IntStream.range(0, 1 + random.nextInt(10))
  124. .forEach(i -> underTest.addWarning(file, new LineReader.ReadError(HIGHLIGHTING, line + i))));
  125. verifyNoInteractions(taskMessages);
  126. underTest.commitWarnings();
  127. String expectedMessage = "Inconsistent highlighting data detected on some files (" + fileCount + " in total). " +
  128. "File source may have been modified while analysis was running." +
  129. Arrays.stream(files).map(Component::getName).collect(Collectors.joining("\n ° ", "\n ° ", ""));
  130. verify(taskMessages, times(1))
  131. .add(new CeTaskMessages.Message(expectedMessage, timeStamp));
  132. }
  133. @Test
  134. public void create_highlighting_warning_when_any_number_of_read_error_for_more_than_5_files_only_the_5_first_by_ref() {
  135. int fileCount = 6 + random.nextInt(4);
  136. Component[] files = IntStream.range(0, fileCount)
  137. .mapToObj(i -> ReportComponent.builder(Component.Type.FILE, i)
  138. .setUuid("uuid_" + i)
  139. .setName(path + "_" + i)
  140. .build())
  141. .toArray(Component[]::new);
  142. when(system2.now()).thenReturn(timeStamp);
  143. Arrays.stream(files).forEach(file -> IntStream.range(0, 1 + random.nextInt(10))
  144. .forEach(i -> underTest.addWarning(file, new LineReader.ReadError(HIGHLIGHTING, line + i))));
  145. verifyNoInteractions(taskMessages);
  146. underTest.commitWarnings();
  147. String expectedMessage = "Inconsistent highlighting data detected on some files (" + fileCount + " in total). " +
  148. "File source may have been modified while analysis was running." +
  149. Arrays.stream(files).limit(5).map(Component::getName).collect(Collectors.joining("\n ° ", "\n ° ", ""));
  150. verify(taskMessages, times(1))
  151. .add(new CeTaskMessages.Message(expectedMessage, timeStamp));
  152. }
  153. @Test
  154. public void create_symbol_warning_when_one_file_HIGHLIGHT_read_error() {
  155. ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1)
  156. .setUuid("uuid")
  157. .setName(path)
  158. .build();
  159. LineReader.ReadError readError = new LineReader.ReadError(SYMBOLS, line);
  160. when(system2.now()).thenReturn(timeStamp);
  161. underTest.addWarning(file, readError);
  162. verifyNoInteractions(taskMessages);
  163. underTest.commitWarnings();
  164. verify(taskMessages, times(1))
  165. .add(new CeTaskMessages.Message("Inconsistent symbol data detected on file '" + path + "'. " +
  166. "File source may have been modified while analysis was running.", timeStamp));
  167. }
  168. @Test
  169. public void create_symbol_warning_when_any_number_of_read_error_for_one_file() {
  170. ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1)
  171. .setUuid("uuid")
  172. .setName(path)
  173. .build();
  174. LineReader.ReadError[] readErrors = IntStream.range(0, 1 + random.nextInt(10))
  175. .mapToObj(i -> new LineReader.ReadError(SYMBOLS, line + i))
  176. .toArray(LineReader.ReadError[]::new);
  177. when(system2.now()).thenReturn(timeStamp);
  178. Arrays.stream(readErrors).forEach(readError -> underTest.addWarning(file, readError));
  179. verifyNoInteractions(taskMessages);
  180. underTest.commitWarnings();
  181. verify(taskMessages, times(1))
  182. .add(new CeTaskMessages.Message("Inconsistent symbol data detected on file '" + path + "'. " +
  183. "File source may have been modified while analysis was running.", timeStamp));
  184. }
  185. @Test
  186. public void create_symbol_warning_when_any_number_of_read_error_for_less_than_5_files() {
  187. int fileCount = 2 + random.nextInt(3);
  188. Component[] files = IntStream.range(0, fileCount)
  189. .mapToObj(i -> ReportComponent.builder(Component.Type.FILE, i)
  190. .setUuid("uuid_" + i)
  191. .setName(path + "_" + i)
  192. .build())
  193. .toArray(Component[]::new);
  194. when(system2.now()).thenReturn(timeStamp);
  195. Arrays.stream(files).forEach(file -> IntStream.range(0, 1 + random.nextInt(10))
  196. .forEach(i -> underTest.addWarning(file, new LineReader.ReadError(SYMBOLS, line + i))));
  197. verifyNoInteractions(taskMessages);
  198. underTest.commitWarnings();
  199. String expectedMessage = "Inconsistent symbol data detected on some files (" + fileCount + " in total). " +
  200. "File source may have been modified while analysis was running." +
  201. Arrays.stream(files).map(Component::getName).collect(Collectors.joining("\n ° ", "\n ° ", ""));
  202. verify(taskMessages, times(1))
  203. .add(new CeTaskMessages.Message(expectedMessage, timeStamp));
  204. }
  205. @Test
  206. public void create_symbol_warning_when_any_number_of_read_error_for_more_than_5_files_only_the_5_first_by_ref() {
  207. int fileCount = 6 + random.nextInt(4);
  208. Component[] files = IntStream.range(0, fileCount)
  209. .mapToObj(i -> ReportComponent.builder(Component.Type.FILE, i)
  210. .setUuid("uuid_" + i)
  211. .setName(path + "_" + i)
  212. .build())
  213. .toArray(Component[]::new);
  214. when(system2.now()).thenReturn(timeStamp);
  215. Arrays.stream(files).forEach(file -> IntStream.range(0, 1 + random.nextInt(10))
  216. .forEach(i -> underTest.addWarning(file, new LineReader.ReadError(SYMBOLS, line + i))));
  217. verifyNoInteractions(taskMessages);
  218. underTest.commitWarnings();
  219. String expectedMessage = "Inconsistent symbol data detected on some files (" + fileCount + " in total). " +
  220. "File source may have been modified while analysis was running." +
  221. Arrays.stream(files).limit(5).map(Component::getName).collect(Collectors.joining("\n ° ", "\n ° ", ""));
  222. verify(taskMessages, times(1))
  223. .add(new CeTaskMessages.Message(expectedMessage, timeStamp));
  224. }
  225. @Test
  226. @UseDataProvider("anyDataButHighlightAndSymbols")
  227. public void creates_no_warning_when_read_error_for_anything_but_highlighting_and_symbols(LineReader.Data data) {
  228. ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1)
  229. .setUuid("uuid")
  230. .setName(path)
  231. .build();
  232. LineReader.ReadError readError = new LineReader.ReadError(data, line);
  233. when(system2.now()).thenReturn(timeStamp);
  234. underTest.addWarning(file, readError);
  235. verifyNoInteractions(taskMessages);
  236. underTest.commitWarnings();
  237. verifyNoInteractions(taskMessages);
  238. }
  239. @DataProvider
  240. public static Object[][] anyDataButHighlightAndSymbols() {
  241. return Arrays.stream(LineReader.Data.values())
  242. .filter(t -> t != HIGHLIGHTING && t != SYMBOLS)
  243. .map(t -> new Object[] {t})
  244. .toArray(Object[][]::new);
  245. }
  246. }