]> source.dussan.org Git - sonarqube.git/blob
96bf0996b82b9bfe28227b03c1e1e61e844f42f7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.scanner.mediumtest.issues;
21
22 import org.junit.rules.TemporaryFolder;
23 import org.junit.After;
24 import org.junit.Before;
25 import com.google.common.collect.ImmutableMap;
26 import org.sonar.api.CoreProperties;
27 import org.sonar.batch.bootstrapper.IssueListener;
28 import org.sonar.scanner.mediumtest.ScannerMediumTester;
29 import org.sonar.scanner.mediumtest.TaskResult;
30 import org.sonar.xoo.XooPlugin;
31 import org.sonar.xoo.rule.XooRulesDefinition;
32 import org.apache.commons.io.FileUtils;
33 import org.junit.Test;
34 import java.io.File;
35 import java.util.Date;
36 import java.util.LinkedList;
37 import java.util.List;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40
41 public class IssuesIssuesModeMediumTest {
42   @org.junit.Rule
43   public TemporaryFolder temp = new TemporaryFolder();
44
45   public ScannerMediumTester testerPreview = ScannerMediumTester.builder()
46     .registerPlugin("xoo", new XooPlugin())
47     .addDefaultQProfile("xoo", "Sonar Way")
48     .addRules(new XooRulesDefinition())
49     .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES))
50     .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo")
51     .setLastBuildDate(new Date())
52     .build();
53
54   @Before
55   public void prepare() {
56     testerPreview.start();
57   }
58
59   @After
60   public void stop() {
61     testerPreview.stop();
62   }
63
64   @Test
65   public void testIssueCallback() throws Exception {
66     File projectDir = new File(IssuesMediumTest.class.getResource("/mediumtest/xoo/sample").toURI());
67     File tmpDir = temp.getRoot();
68     FileUtils.copyDirectory(projectDir, tmpDir);
69     IssueRecorder issueListener = new IssueRecorder();
70
71     TaskResult result1 = testerPreview
72       .newScanTask(new File(tmpDir, "sonar-project.properties"))
73       .setIssueListener(issueListener)
74       .property(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES)
75       .start();
76
77     assertThat(result1.trackedIssues()).hasSize(14);
78     assertThat(issueListener.issueList).hasSize(14);
79     issueListener = new IssueRecorder();
80
81     TaskResult result2 = testerPreview
82       .newScanTask(new File(tmpDir, "sonar-project.properties"))
83       .setIssueListener(issueListener)
84       .property(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES)
85       .start();
86
87     assertThat(result2.trackedIssues()).hasSize(14);
88     assertThat(issueListener.issueList).hasSize(14);
89   }
90
91   private class IssueRecorder implements IssueListener {
92     List<Issue> issueList = new LinkedList<>();
93
94     @Override
95     public void handle(Issue issue) {
96       issueList.add(issue);
97     }
98   }
99 }