3 * Copyright (C) 2009-2019 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.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.sonar.api.config.Configuration;
37 import org.sonar.api.impl.config.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);
57 private static Path tempDir;
60 public static void lookupTempDir() throws IOException {
61 Path tempFile = Files.createTempFile("a", "b");
62 Files.delete(tempFile);
63 tempDir = tempFile.getParent();
67 public void setUp() throws Exception {
68 FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() {
70 public boolean accept(File file) {
71 if (file.getName().contains("score-matrix-")) {
80 public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException {
81 String taskUuid = "acme";
82 when(ceTask.getUuid()).thenReturn(taskUuid);
83 settings.setProperty("sonar.filemove.dumpCsv", "true");
85 underTest.dumpAsCsv(A_SCORE_MATRIX);
87 Collection<File> files = listDumpFilesForTaskUuid(taskUuid);
88 assertThat(files).hasSize(1);
89 assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';'));
93 public void dumpAsCsv_has_no_effect_if_configuration_is_empty() throws IOException {
94 String taskUuid = randomAlphabetic(6);
95 when(ceTask.getUuid()).thenReturn(taskUuid);
97 underTest.dumpAsCsv(A_SCORE_MATRIX);
99 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
103 @UseDataProvider("notTruePropertyValues")
104 public void dumpAsCsv_has_no_effect_if_property_is_not_true(String value) throws IOException {
105 String taskUuid = randomAlphabetic(6);
106 when(ceTask.getUuid()).thenReturn(taskUuid);
107 settings.setProperty("sonar.filemove.dumpCsv", value);
109 underTest.dumpAsCsv(A_SCORE_MATRIX);
111 assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty();
115 public static Object[][] notTruePropertyValues() {
116 return new Object[][] {
117 {randomAlphabetic(6)},
122 private static Collection<File> listDumpFilesForTaskUuid(String taskUuid) throws IOException {
123 return FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() {
125 public boolean accept(File file) {
126 String name = file.getName();
127 return name.startsWith("score-matrix-" + taskUuid) && name.endsWith(".csv");