123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /*
- * SonarQube
- * Copyright (C) 2009-2017 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 it.analysis;
-
- import com.sonar.orchestrator.Orchestrator;
- import com.sonar.orchestrator.build.BuildResult;
- import com.sonar.orchestrator.build.SonarScanner;
- import com.sonar.orchestrator.locator.FileLocation;
- import com.sonar.orchestrator.locator.ResourceLocation;
- import it.Category3Suite;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Pattern;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.junit.Before;
- import org.junit.ClassRule;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.TemporaryFolder;
- import org.skyscreamer.jsonassert.JSONAssert;
- import util.ItUtils;
-
- import static org.assertj.core.api.Assertions.assertThat;
-
- public class IssueJsonReportTest {
-
- private static final String SONAR_VERSION_PLACEHOLDER = "<SONAR_VERSION>";
-
- @ClassRule
- public static Orchestrator orchestrator = Category3Suite.ORCHESTRATOR;
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Before
- public void resetData() {
- orchestrator.resetData();
- }
-
- @Test
- public void issue_line() throws IOException {
- orchestrator.getServer().restoreProfile(getResource("one-issue-per-line.xml"));
- orchestrator.getServer().provisionProject("sample", "xoo-sample");
- orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
-
- File projectDir = ItUtils.projectDir("shared/xoo-sample");
- SonarScanner runner = SonarScanner.create(projectDir,
- "sonar.analysis.mode", "issues",
- "sonar.verbose", "true",
- "sonar.report.export.path", "sonar-report.json");
- BuildResult result = orchestrator.executeBuild(runner);
- assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(17);
-
- JSONObject obj = ItUtils.getJSONReport(result);
- JSONArray issues = (JSONArray) obj.get("issues");
- for (Object issue : issues) {
- JSONObject jsonIssue = (JSONObject) issue;
- assertThat(jsonIssue.get("startLine")).isNotNull();
- assertThat(jsonIssue.get("line")).isEqualTo(jsonIssue.get("startLine"));
- assertThat(jsonIssue.get("endLine")).isEqualTo(jsonIssue.get("startLine"));
-
- assertThat(jsonIssue.get("endOffset")).isNotNull();
- assertThat(jsonIssue.get("startOffset")).isNotNull();
- }
-
- List<Long> lineNumbers = new ArrayList<>(16);
- for (long i = 1L; i < 18; i++) {
- lineNumbers.add(i);
- }
- assertThat(issues).extracting("startLine").containsAll(lineNumbers);
- assertThat(issues).extracting("endLine").containsAll(lineNumbers);
- }
-
- @Test
- public void precise_issue_location() throws IOException {
- orchestrator.getServer().restoreProfile(getResource("multiline.xml"));
- orchestrator.getServer().provisionProject("sample-multiline", "xoo-sample");
- orchestrator.getServer().associateProjectToQualityProfile("sample-multiline", "xoo", "multiline");
-
- File projectDir = ItUtils.projectDir("shared/xoo-precise-issues");
- SonarScanner runner = SonarScanner.create(projectDir,
- "sonar.analysis.mode", "issues",
- "sonar.verbose", "true",
- "sonar.report.export.path", "sonar-report.json");
- BuildResult result = orchestrator.executeBuild(runner);
- assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(2);
-
- JSONObject obj = ItUtils.getJSONReport(result);
- JSONArray issues = (JSONArray) obj.get("issues");
-
- JSONObject issue1 = (JSONObject) issues.get(0);
- JSONObject issue2 = (JSONObject) issues.get(1);
-
- assertThat(issue1.get("startLine")).isIn(6L);
- assertThat(issue1.get("line")).isIn(6L);
- assertThat(issue1.get("endLine")).isIn(6L);
- assertThat(issue1.get("startOffset")).isIn(27L);
- assertThat(issue1.get("endOffset")).isIn(32L);
-
- assertThat(issue2.get("startLine")).isIn(9L);
- assertThat(issue2.get("line")).isIn(9L);
- assertThat(issue2.get("endLine")).isIn(15L);
- assertThat(issue2.get("startOffset")).isIn(20L);
- assertThat(issue2.get("endOffset")).isIn(2L);
-
- }
-
- @Test
- public void test_json_report_no_server_analysis() throws Exception {
- orchestrator.getServer().restoreProfile(getResource("one-issue-per-line.xml"));
- orchestrator.getServer().provisionProject("sample", "tracking");
- orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
-
- File projectDir = ItUtils.projectDir("analysis/tracking/v1");
- SonarScanner issuesModeScan = SonarScanner.create(projectDir)
- .setProperty("sonar.analysis.mode", "issues")
- .setProperty("sonar.userHome", temp.newFolder().getAbsolutePath())
- .setProperty("sonar.report.export.path", "sonar-report.json")
- .setProperty("sonar.projectDate", "2013-05-02");
- orchestrator.executeBuild(issuesModeScan);
-
- File report = new File(projectDir, ".sonar/sonar-report.json");
- assertThat(report).isFile().exists();
-
- String json = sanitize(FileUtils.readFileToString(report));
- String expectedJson = expected("no-server-analysis.json");
- JSONAssert.assertEquals(expectedJson, json, false);
- }
-
- @Test
- public void test_json_report() throws Exception {
- orchestrator.getServer().restoreProfile(getResource("one-issue-per-line.xml"));
- orchestrator.getServer().provisionProject("sample", "tracking");
- orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
-
- SonarScanner scan = SonarScanner.create(ItUtils.projectDir("analysis/tracking/v1"))
- .setProperty("sonar.projectDate", "2013-05-01");
- orchestrator.executeBuild(scan);
-
- // Issues mode scan -> 2 new issues and 13 existing issues
- File projectDir = ItUtils.projectDir("analysis/tracking/v2");
- SonarScanner issuesModeScan = SonarScanner.create(projectDir)
- .setProperty("sonar.analysis.mode", "issues")
- .setProperty("sonar.userHome", temp.newFolder().getAbsolutePath())
- .setProperty("sonar.report.export.path", "sonar-report.json")
- .setProperty("sonar.projectDate", "2013-05-02");
- orchestrator.executeBuild(issuesModeScan);
-
- File report = new File(projectDir, ".sonar/sonar-report.json");
- assertThat(report).isFile().exists();
-
- String json = sanitize(FileUtils.readFileToString(report));
- String expectedJson = expected("report-on-single-module.json");
- JSONAssert.assertEquals(expectedJson, json, false);
- }
-
- @Test
- public void test_json_report_on_branch() throws Exception {
- orchestrator.getServer().restoreProfile(getResource("one-issue-per-line.xml"));
- orchestrator.getServer().provisionProject("sample:mybranch", "Sample");
- orchestrator.getServer().associateProjectToQualityProfile("sample:mybranch", "xoo", "one-issue-per-line");
-
- SonarScanner scan = SonarScanner.create(ItUtils.projectDir("analysis/tracking/v1"))
- .setProperty("sonar.projectDate", "2013-05-01")
- .setProperty("sonar.branch", "mybranch");
- orchestrator.executeBuild(scan);
-
- // issues mode scan -> 2 new issues and 13 existing issues
- File projectDir = ItUtils.projectDir("analysis/tracking/v2");
- SonarScanner issuesModeScan = SonarScanner.create(projectDir)
- .setProperty("sonar.analysis.mode", "issues")
- .setProperty("sonar.userHome", temp.newFolder().getAbsolutePath())
- .setProperty("sonar.report.export.path", "sonar-report.json")
- .setProperty("sonar.issuesReport.console.enable", "true")
- .setProperty("sonar.projectDate", "2013-05-02")
- .setProperty("sonar.verbose", "true")
- .setProperty("sonar.branch", "mybranch");
- orchestrator.executeBuild(issuesModeScan);
-
- File report = new File(projectDir, ".sonar/sonar-report.json");
- assertThat(report).isFile().exists();
-
- String json = sanitize(FileUtils.readFileToString(report));
- String expectedJson = expected("report-on-single-module-branch.json");
- JSONAssert.assertEquals(expectedJson, json, false);
- }
-
- /**
- * Multi-modules project but Eclipse scans only a single module
- */
- @Test
- public void test_json_report_on_sub_module() throws Exception {
- orchestrator.getServer().restoreProfile(getResource("one-issue-per-line.xml"));
- orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "Multi-module sample");
- orchestrator.getServer().associateProjectToQualityProfile("com.sonarsource.it.samples:multi-modules-sample", "xoo", "one-issue-per-line");
-
- File rootDir = ItUtils.projectDir("shared/xoo-multi-modules-sample");
- SonarScanner scan = SonarScanner.create(rootDir)
- .setProperty("sonar.projectDate", "2013-05-01");
- orchestrator.executeBuild(scan);
-
- // Issues mode scan on a module -> no new issues
- File moduleDir = ItUtils.projectDir("shared/xoo-multi-modules-sample/module_a/module_a1");
- SonarScanner issuesModeScan = SonarScanner.create(moduleDir)
- .setProperty("sonar.projectKey", "com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1")
- .setProperty("sonar.projectVersion", "1.0-SNAPSHOT")
- .setProperty("sonar.projectName", "ModuleA1")
- .setProperty("sonar.sources", "src/main/xoo")
- .setProperty("sonar.language", "xoo")
- .setProperty("sonar.analysis.mode", "issues")
- .setProperty("sonar.userHome", temp.newFolder().getAbsolutePath())
- .setProperty("sonar.report.export.path", "sonar-report.json")
- .setProperty("sonar.projectDate", "2013-05-02");
- orchestrator.executeBuild(issuesModeScan);
-
- File report = new File(moduleDir, ".sonar/sonar-report.json");
- assertThat(report).isFile().exists();
-
- String json = sanitize(FileUtils.readFileToString(report));
- // SONAR-5218 All issues are updated as their root project id has changed (it's now the sub module)
- String expectedJson = expected("report-on-sub-module.json");
- JSONAssert.assertEquals(expectedJson, json, false);
- }
-
- /**
- * Multi-modules project
- */
- @Test
- public void test_json_report_on_root_module() throws Exception {
- orchestrator.getServer().restoreProfile(getResource("/one-issue-per-line.xml"));
- orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "Sonar :: Integration Tests :: Multi-modules Sample");
- orchestrator.getServer().associateProjectToQualityProfile("com.sonarsource.it.samples:multi-modules-sample", "xoo", "one-issue-per-line");
-
- File rootDir = ItUtils.projectDir("shared/xoo-multi-modules-sample");
- SonarScanner scan = SonarScanner.create(rootDir)
- .setProperty("sonar.projectDate", "2013-05-01");
- orchestrator.executeBuild(scan);
-
- // issues mode scan -> no new issues
- SonarScanner issuesModeScan = SonarScanner.create(rootDir)
- .setProperty("sonar.analysis.mode", "issues")
- .setProperty("sonar.userHome", temp.newFolder().getAbsolutePath())
- .setProperty("sonar.report.export.path", "sonar-report.json")
- .setProperty("sonar.projectDate", "2013-05-02");
- orchestrator.executeBuild(issuesModeScan);
-
- File report = new File(rootDir, ".sonar/sonar-report.json");
- assertThat(report).isFile().exists();
-
- String json = sanitize(FileUtils.readFileToString(report));
- String expectedJson = expected("report-on-root-module.json");
- JSONAssert.assertEquals(expectedJson, json, false);
- }
-
- private String expected(String path) throws IOException, FileNotFoundException {
- return sanitize(IOUtils.toString(getResourceInputStream(path)))
- .replaceAll(Pattern.quote(SONAR_VERSION_PLACEHOLDER), orchestrator.getServer().version().toString());
- }
-
- @Test
- public void issueSanityCheck() {
- assertThat(sanitize("s\"0150F1EBDB8E000003\"f")).isEqualTo("s<ISSUE_KEY>f");
- }
-
- private static String sanitize(String s) {
- // sanitize issue uuid keys
- s = s.replaceAll("\"[a-zA-Z_0-9\\-]{15,20}\"", "<ISSUE_KEY>");
-
- return ItUtils.sanitizeTimezones(s);
- }
-
- private InputStream getResourceInputStream(String resource) throws FileNotFoundException {
- ResourceLocation res = getResource(resource);
- return getClass().getResourceAsStream(res.getPath());
- }
-
- private ResourceLocation getResource(String resource) {
- return FileLocation.ofClasspath("/analysis/IssueJsonReportTest/" + resource);
- }
-
- }
|