3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.filemove;
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import org.junit.After;
32 import org.junit.Before;
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 org.sonar.server.platform.ServerFileSystem;
41 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
46 @RunWith(DataProviderRunner.class)
47 public class ScoreMatrixDumperImplTest {
48 private static final ScoreMatrix A_SCORE_MATRIX = new ScoreMatrix(
49 new ScoreFile[] {new ScoreFile("A", 12), new ScoreFile("B", 8)},
50 new ScoreFile[] {new ScoreFile("1", 7)},
51 new int[][] {{10}, {2}},
53 private MapSettings settings = new MapSettings();
54 private Configuration configuration = settings.asConfig();
55 private CeTask ceTask = mock(CeTask.class);
57 private ServerFileSystem serverFileSystem = mock(ServerFileSystem.class);
58 private ScoreMatrixDumper underTest = new ScoreMatrixDumperImpl(configuration, ceTask, serverFileSystem);
62 public void before() throws IOException {
63 Path tempFile = Files.createTempFile("a", "b");
64 Files.delete(tempFile);
65 tempDir = tempFile.getParent();
66 when(serverFileSystem.getTempDir()).thenReturn(tempDir.toFile());
70 public void cleanUp() {
72 Files.list(tempDir.toAbsolutePath()).filter(p -> p.getFileName().toString().contains("score-matrix-")).forEach((p) -> {
74 Files.deleteIfExists(p);
75 } catch (Exception e) {
76 System.out.println("Could not delete file. Details: " + e.getMessage());
79 } catch (Exception e) {
80 System.out.println("Cleaning up temp directory failed. Details: " + e.getMessage());
85 public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException {
86 String taskUuid = "acme";
87 when(ceTask.getUuid()).thenReturn(taskUuid);
88 settings.setProperty("sonar.filemove.dumpCsv", "true");
90 underTest.dumpAsCsv(A_SCORE_MATRIX);
92 Collection<File> files = listDumpFilesForTaskUuid(taskUuid);
93 assertThat(files).hasSize(1);
94 assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';'));
98 public void dumpAsCsv_has_no_effect_if_configuration_is_empty() throws IOException {
99 String taskUuid = randomAlphabetic(6);
100 when(ceTask.getUuid()).thenReturn(taskUuid);
102 underTest.dumpAsCsv(A_SCORE_MATRIX);
104 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
108 @UseDataProvider("notTruePropertyValues")
109 public void dumpAsCsv_has_no_effect_if_property_is_not_true(String value) throws IOException {
110 String taskUuid = randomAlphabetic(6);
111 when(ceTask.getUuid()).thenReturn(taskUuid);
112 settings.setProperty("sonar.filemove.dumpCsv", value);
114 underTest.dumpAsCsv(A_SCORE_MATRIX);
116 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
120 public static Object[][] notTruePropertyValues() {
121 return new Object[][] {
122 {randomAlphabetic(6)},
127 private Collection<File> listDumpFilesForTaskUuid(String taskUuid) {
128 Collection<File> dumpFiles = new ArrayList<>();
129 File dir = tempDir.toFile();
130 File[] files = dir.listFiles();
131 if (!dir.exists() || files == null) {
132 throw new IllegalStateException("Temp directory does not exist");
134 for (File file : files) {
136 String name = file.getName();
137 if (name.startsWith("score-matrix-" + taskUuid) && name.endsWith(".csv")) {