3 * Copyright (C) 2009-2022 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.Collection;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.io.filefilter.AbstractFileFilter;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.sonar.api.config.Configuration;
37 import org.sonar.api.config.internal.MapSettings;
38 import org.sonar.ce.task.CeTask;
39 import org.sonar.ce.task.projectanalysis.filemove.ScoreMatrix.ScoreFile;
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);
56 private ScoreMatrixDumper underTest = new ScoreMatrixDumperImpl(configuration, ceTask);
60 public void before() throws IOException {
61 Path tempFile = Files.createTempFile("a", "b");
62 Files.delete(tempFile);
63 tempDir = tempFile.getParent();
67 public void cleanUp() {
69 Files.list(tempDir.toAbsolutePath()).filter(p -> p.getFileName().toString().contains("score-matrix-")).forEach((p) -> {
71 Files.deleteIfExists(p);
72 } catch (Exception e) {
73 System.out.println("Could not delete file. Details: " + e.getMessage());
76 } catch (Exception e) {
77 System.out.println("Cleaning up temp directory failed. Details: " + e.getMessage());
82 public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException {
83 String taskUuid = "acme";
84 when(ceTask.getUuid()).thenReturn(taskUuid);
85 settings.setProperty("sonar.filemove.dumpCsv", "true");
87 underTest.dumpAsCsv(A_SCORE_MATRIX);
89 Collection<File> files = listDumpFilesForTaskUuid(taskUuid);
90 assertThat(files).hasSize(1);
91 assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';'));
95 public void dumpAsCsv_has_no_effect_if_configuration_is_empty() throws IOException {
96 String taskUuid = randomAlphabetic(6);
97 when(ceTask.getUuid()).thenReturn(taskUuid);
99 underTest.dumpAsCsv(A_SCORE_MATRIX);
101 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
105 @UseDataProvider("notTruePropertyValues")
106 public void dumpAsCsv_has_no_effect_if_property_is_not_true(String value) throws IOException {
107 String taskUuid = randomAlphabetic(6);
108 when(ceTask.getUuid()).thenReturn(taskUuid);
109 settings.setProperty("sonar.filemove.dumpCsv", value);
111 underTest.dumpAsCsv(A_SCORE_MATRIX);
113 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
117 public static Object[][] notTruePropertyValues() {
118 return new Object[][] {
119 {randomAlphabetic(6)},
124 private Collection<File> listDumpFilesForTaskUuid(String taskUuid) {
125 return FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() {
127 public boolean accept(File file) {
128 String name = file.getName();
129 return name.startsWith("score-matrix-" + taskUuid) && name.endsWith(".csv");