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.

ScoreMatrixDumperImplTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.filemove;
  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.io.File;
  25. import java.io.IOException;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import org.junit.After;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.sonar.api.config.Configuration;
  35. import org.sonar.api.config.internal.MapSettings;
  36. import org.sonar.ce.task.CeTask;
  37. import org.sonar.ce.task.projectanalysis.filemove.ScoreMatrix.ScoreFile;
  38. import org.sonar.server.platform.ServerFileSystem;
  39. import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.when;
  43. @RunWith(DataProviderRunner.class)
  44. public class ScoreMatrixDumperImplTest {
  45. private static final ScoreMatrix A_SCORE_MATRIX = new ScoreMatrix(
  46. new ScoreFile[] {new ScoreFile("A", 12), new ScoreFile("B", 8)},
  47. new ScoreFile[] {new ScoreFile("1", 7)},
  48. new int[][] {{10}, {2}},
  49. 10);
  50. private MapSettings settings = new MapSettings();
  51. private Configuration configuration = settings.asConfig();
  52. private CeTask ceTask = mock(CeTask.class);
  53. private ServerFileSystem serverFileSystem = mock(ServerFileSystem.class);
  54. private ScoreMatrixDumper underTest = new ScoreMatrixDumperImpl(configuration, ceTask, serverFileSystem);
  55. private Path tempDir;
  56. @Before
  57. public void before() throws IOException {
  58. Path tempFile = Files.createTempFile("a", "b");
  59. Files.delete(tempFile);
  60. tempDir = tempFile.getParent();
  61. when(serverFileSystem.getTempDir()).thenReturn(tempDir.toFile());
  62. }
  63. @After
  64. public void cleanUp() {
  65. try {
  66. Files.list(tempDir.toAbsolutePath()).filter(p -> p.getFileName().toString().contains("score-matrix-")).forEach((p) -> {
  67. try {
  68. Files.deleteIfExists(p);
  69. } catch (Exception e) {
  70. System.out.println("Could not delete file. Details: " + e.getMessage());
  71. }
  72. });
  73. } catch (Exception e) {
  74. System.out.println("Cleaning up temp directory failed. Details: " + e.getMessage());
  75. }
  76. }
  77. @Test
  78. public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException {
  79. String taskUuid = "acme";
  80. when(ceTask.getUuid()).thenReturn(taskUuid);
  81. settings.setProperty("sonar.filemove.dumpCsv", "true");
  82. underTest.dumpAsCsv(A_SCORE_MATRIX);
  83. Collection<File> files = listDumpFilesForTaskUuid(taskUuid);
  84. assertThat(files).hasSize(1);
  85. assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';'));
  86. }
  87. @Test
  88. public void dumpAsCsv_has_no_effect_if_configuration_is_empty() throws IOException {
  89. String taskUuid = randomAlphabetic(6);
  90. when(ceTask.getUuid()).thenReturn(taskUuid);
  91. underTest.dumpAsCsv(A_SCORE_MATRIX);
  92. assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
  93. }
  94. @Test
  95. @UseDataProvider("notTruePropertyValues")
  96. public void dumpAsCsv_has_no_effect_if_property_is_not_true(String value) throws IOException {
  97. String taskUuid = randomAlphabetic(6);
  98. when(ceTask.getUuid()).thenReturn(taskUuid);
  99. settings.setProperty("sonar.filemove.dumpCsv", value);
  100. underTest.dumpAsCsv(A_SCORE_MATRIX);
  101. assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
  102. }
  103. @DataProvider
  104. public static Object[][] notTruePropertyValues() {
  105. return new Object[][] {
  106. {randomAlphabetic(6)},
  107. {"false"},
  108. };
  109. }
  110. private Collection<File> listDumpFilesForTaskUuid(String taskUuid) {
  111. Collection<File> dumpFiles = new ArrayList<>();
  112. File dir = tempDir.toFile();
  113. File[] files = dir.listFiles();
  114. if (!dir.exists() || files == null) {
  115. throw new IllegalStateException("Temp directory does not exist");
  116. }
  117. for (File file : files) {
  118. if (file.exists()) {
  119. String name = file.getName();
  120. if (name.startsWith("score-matrix-" + taskUuid) && name.endsWith(".csv")) {
  121. dumpFiles.add(file);
  122. }
  123. }
  124. }
  125. return dumpFiles;
  126. }
  127. }