aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisWarningsPublisherTest.java
blob: 0704f79709348053424f36e6203b27a23659ce61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.report;

import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.utils.System2;
import org.sonar.scanner.notifications.DefaultAnalysisWarnings;
import org.sonar.scanner.protocol.output.FileStructure;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReportReader;
import org.sonar.scanner.protocol.output.ScannerReportWriter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class AnalysisWarningsPublisherTest {

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  private final DefaultAnalysisWarnings analysisWarnings = new DefaultAnalysisWarnings(mock(System2.class));
  private final AnalysisWarningsPublisher underTest = new AnalysisWarningsPublisher(analysisWarnings);
  private FileStructure fileStructure;

  @Before
  public void setUp() throws IOException {
    fileStructure = new FileStructure(temp.newFolder());
  }

  @Test
  public void publish_warnings() throws IOException {
    ScannerReportWriter writer = new ScannerReportWriter(fileStructure);

    String warning1 = "warning 1";
    String warning2 = "warning 2";
    analysisWarnings.addUnique(warning1);
    analysisWarnings.addUnique(warning1);
    analysisWarnings.addUnique(warning2);

    underTest.publish(writer);

    ScannerReportReader reader = new ScannerReportReader(fileStructure);
    List<ScannerReport.AnalysisWarning> warnings = Lists.newArrayList(reader.readAnalysisWarnings());

    assertThat(warnings)
      .extracting(ScannerReport.AnalysisWarning::getText)
      .containsExactly(warning1, warning2);
  }

  @Test
  public void do_not_write_warnings_report_when_empty() throws IOException {
    File outputDir = temp.newFolder();
    ScannerReportWriter writer = new ScannerReportWriter(fileStructure);

    underTest.publish(writer);

    assertThat(writer.getFileStructure().analysisWarnings()).doesNotExist();

    ScannerReportReader reader = new ScannerReportReader(fileStructure);
    List<ScannerReport.AnalysisWarning> warnings = Lists.newArrayList(reader.readAnalysisWarnings());

    assertThat(warnings).isEmpty();
  }
}