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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.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.Collection;
  29. import org.apache.commons.io.FileUtils;
  30. import org.apache.commons.io.filefilter.AbstractFileFilter;
  31. import org.junit.Before;
  32. import org.junit.BeforeClass;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import org.sonar.api.config.Configuration;
  36. import org.sonar.api.config.internal.MapSettings;
  37. import org.sonar.ce.task.CeTask;
  38. import org.sonar.ce.task.projectanalysis.filemove.ScoreMatrix.ScoreFile;
  39. import static org.apache.commons.lang.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 ScoreMatrixDumper underTest = new ScoreMatrixDumperImpl(configuration, ceTask);
  54. private static Path tempDir;
  55. @BeforeClass
  56. public static void lookupTempDir() throws IOException {
  57. Path tempFile = Files.createTempFile("a", "b");
  58. Files.delete(tempFile);
  59. tempDir = tempFile.getParent();
  60. }
  61. @Before
  62. public void setUp() {
  63. FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() {
  64. @Override
  65. public boolean accept(File file) {
  66. if (file.getName().contains("score-matrix-")) {
  67. file.delete();
  68. }
  69. return false;
  70. }
  71. }, null);
  72. }
  73. @Test
  74. public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException {
  75. String taskUuid = "acme";
  76. when(ceTask.getUuid()).thenReturn(taskUuid);
  77. settings.setProperty("sonar.filemove.dumpCsv", "true");
  78. underTest.dumpAsCsv(A_SCORE_MATRIX);
  79. Collection<File> files = listDumpFilesForTaskUuid(taskUuid);
  80. assertThat(files).hasSize(1);
  81. assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';'));
  82. }
  83. @Test
  84. public void dumpAsCsv_has_no_effect_if_configuration_is_empty() throws IOException {
  85. String taskUuid = randomAlphabetic(6);
  86. when(ceTask.getUuid()).thenReturn(taskUuid);
  87. underTest.dumpAsCsv(A_SCORE_MATRIX);
  88. assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
  89. }
  90. @Test
  91. @UseDataProvider("notTruePropertyValues")
  92. public void dumpAsCsv_has_no_effect_if_property_is_not_true(String value) throws IOException {
  93. String taskUuid = randomAlphabetic(6);
  94. when(ceTask.getUuid()).thenReturn(taskUuid);
  95. settings.setProperty("sonar.filemove.dumpCsv", value);
  96. underTest.dumpAsCsv(A_SCORE_MATRIX);
  97. assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
  98. }
  99. @DataProvider
  100. public static Object[][] notTruePropertyValues() {
  101. return new Object[][] {
  102. {randomAlphabetic(6)},
  103. {"false"},
  104. };
  105. }
  106. private static Collection<File> listDumpFilesForTaskUuid(String taskUuid) {
  107. return FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() {
  108. @Override
  109. public boolean accept(File file) {
  110. String name = file.getName();
  111. return name.startsWith("score-matrix-" + taskUuid) && name.endsWith(".csv");
  112. }
  113. }, null);
  114. }
  115. }