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 FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() {
71 public boolean accept(File file) {
72 if (file.getName().contains("score-matrix-")) {
78 } catch (Exception e) {
79 System.out.println("Cleaning up temp directory failed. Details: " + e.getMessage());
84 public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException {
85 String taskUuid = "acme";
86 when(ceTask.getUuid()).thenReturn(taskUuid);
87 settings.setProperty("sonar.filemove.dumpCsv", "true");
89 underTest.dumpAsCsv(A_SCORE_MATRIX);
91 Collection<File> files = listDumpFilesForTaskUuid(taskUuid);
92 assertThat(files).hasSize(1);
93 assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';'));
97 public void dumpAsCsv_has_no_effect_if_configuration_is_empty() throws IOException {
98 String taskUuid = randomAlphabetic(6);
99 when(ceTask.getUuid()).thenReturn(taskUuid);
101 underTest.dumpAsCsv(A_SCORE_MATRIX);
103 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
107 @UseDataProvider("notTruePropertyValues")
108 public void dumpAsCsv_has_no_effect_if_property_is_not_true(String value) throws IOException {
109 String taskUuid = randomAlphabetic(6);
110 when(ceTask.getUuid()).thenReturn(taskUuid);
111 settings.setProperty("sonar.filemove.dumpCsv", value);
113 underTest.dumpAsCsv(A_SCORE_MATRIX);
115 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
119 public static Object[][] notTruePropertyValues() {
120 return new Object[][] {
121 {randomAlphabetic(6)},
126 private Collection<File> listDumpFilesForTaskUuid(String taskUuid) {
127 return FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() {
129 public boolean accept(File file) {
130 String name = file.getName();
131 return name.startsWith("score-matrix-" + taskUuid) && name.endsWith(".csv");