From c22e7f7e57a85b5e11dd071ab681eca650bbe477 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 17 May 2016 16:09:25 +0200 Subject: [PATCH] SONAR-7640 Add IT --- .../src/test/java/it/Category2Suite.java | 4 + .../issue/IssueFilterOnCommonRulesTest.java | 129 ++++++++++++++++++ .../test/java/it/issue/IssueFilterTest.java | 120 ++++++++++++++++ it/it-tests/src/test/java/util/ItUtils.java | 6 + .../xoo-common-rules-profile.xml | 55 ++++++++ .../issue/IssueFilterTest/with-many-rules.xml | 43 ++++++ 6 files changed, 357 insertions(+) create mode 100644 it/it-tests/src/test/java/it/issue/IssueFilterOnCommonRulesTest.java create mode 100644 it/it-tests/src/test/java/it/issue/IssueFilterTest.java create mode 100644 it/it-tests/src/test/resources/issue/IssueFilterOnCommonRulesTest/xoo-common-rules-profile.xml create mode 100644 it/it-tests/src/test/resources/issue/IssueFilterTest/with-many-rules.xml diff --git a/it/it-tests/src/test/java/it/Category2Suite.java b/it/it-tests/src/test/java/it/Category2Suite.java index 2af51cf7aa6..ad57842de3f 100644 --- a/it/it-tests/src/test/java/it/Category2Suite.java +++ b/it/it-tests/src/test/java/it/Category2Suite.java @@ -28,6 +28,8 @@ import it.issue.IssueBulkChangeTest; import it.issue.IssueChangelogTest; import it.issue.IssueCreationTest; import it.issue.IssueFilterExtensionTest; +import it.issue.IssueFilterOnCommonRulesTest; +import it.issue.IssueFilterTest; import it.issue.IssueMeasureTest; import it.issue.IssueNotificationsTest; import it.issue.IssuePurgeTest; @@ -73,6 +75,8 @@ import static util.ItUtils.xooPlugin; IssueBulkChangeTest.class, IssueChangelogTest.class, IssueCreationTest.class, + IssueFilterOnCommonRulesTest.class, + IssueFilterTest.class, IssueFilterExtensionTest.class, IssueMeasureTest.class, IssueNotificationsTest.class, diff --git a/it/it-tests/src/test/java/it/issue/IssueFilterOnCommonRulesTest.java b/it/it-tests/src/test/java/it/issue/IssueFilterOnCommonRulesTest.java new file mode 100644 index 00000000000..da74881e21e --- /dev/null +++ b/it/it-tests/src/test/java/it/issue/IssueFilterOnCommonRulesTest.java @@ -0,0 +1,129 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.issue; + +import com.sonar.orchestrator.locator.FileLocation; +import java.util.List; +import org.apache.commons.lang.ArrayUtils; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.sonarqube.ws.Issues; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.issue.SearchWsRequest; +import util.QaOnly; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.newAdminWsClient; +import static util.ItUtils.runProjectAnalysis; +import static util.ItUtils.setServerProperties; + +@Category(QaOnly.class) +public class IssueFilterOnCommonRulesTest extends AbstractIssueTest { + + private static WsClient adminWsClient; + + private static final String PROJECT_KEY = "common-rules-project"; + private static final String PROJECT_DIR = "issue/common-rules"; + + private static final String FILE_KEY = "common-rules-project:src/Sample.xoo"; + + @Before + public void resetData() { + ORCHESTRATOR.resetData(); + ORCHESTRATOR.getServer().restoreProfile(FileLocation.ofClasspath("/issue/IssueFilterOnCommonRulesTest/xoo-common-rules-profile.xml")); + ORCHESTRATOR.getServer().provisionProject(PROJECT_KEY, "Sample"); + ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "xoo-common-rules"); + + adminWsClient = newAdminWsClient(ORCHESTRATOR); + } + + @Test + public void ignore_all() { + executeAnalysis("sonar.issue.ignore.multicriteria", "1", + "sonar.issue.ignore.multicriteria.1.ruleKey", "*", + "sonar.issue.ignore.multicriteria.1.resourceKey", "**/*.xoo"); + + assertThat(findAllIssues()).hasSize(0); + } + + @Test + public void ignore_some_rule_and_file() { + executeAnalysis( + "sonar.issue.ignore.multicriteria", "1,2", + "sonar.issue.ignore.multicriteria.1.ruleKey", "common-xoo:DuplicatedBlocks", + "sonar.issue.ignore.multicriteria.1.resourceKey", "**/Sample.xoo", + "sonar.issue.ignore.multicriteria.2.ruleKey", "common-xoo:SkippedUnitTests", + "sonar.issue.ignore.multicriteria.2.resourceKey", "**/SampleTest.xoo"); + + assertThat(findAllIssues()).hasSize(4); + assertThat(findIssuesByRuleKey("common-xoo:DuplicatedBlocks")).isEmpty(); + assertThat(findIssuesByRuleKey("common-xoo:SkippedUnitTests")).isEmpty(); + } + + @Test + public void enforce_one_file() { + executeAnalysis( + "sonar.issue.enforce.multicriteria", "1", + "sonar.issue.enforce.multicriteria.1.ruleKey", "*", + // Only issues on this file will be accepted + "sonar.issue.enforce.multicriteria.1.resourceKey", "**/Sample.xoo"); + + assertThat(findAllIssues()).hasSize(4); + } + + @Test + public void enforce_on_rules() { + executeAnalysis( + "sonar.issue.enforce.multicriteria", "1,2", + "sonar.issue.enforce.multicriteria.1.ruleKey", "common-xoo:DuplicatedBlocks", + "sonar.issue.enforce.multicriteria.1.resourceKey", "**/Sample.xoo", + // This rule should only be applied on a file that do not exist => no issue for this rule + "sonar.issue.enforce.multicriteria.2.ruleKey", "common-xoo:InsufficientCommentDensity", + "sonar.issue.enforce.multicriteria.2.resourceKey", "**/OtherFile.xoo"); + + assertThat(findAllIssues()).hasSize(5); + assertThat(findIssuesByRuleKey("common-xoo:DuplicatedBlocks")).hasSize(1); + assertThat(findIssuesByRuleKey("common-xoo:InsufficientCommentDensity")).isEmpty(); + } + + private void executeAnalysis(String... serverProperties) { + String[] cpdProperties = new String[] { + "sonar.cpd.xoo.minimumTokens", "2", + "sonar.cpd.xoo.minimumLines", "2" + }; + setServerProperties(ORCHESTRATOR, PROJECT_KEY, (String[]) ArrayUtils.addAll(serverProperties, cpdProperties)); + runProjectAnalysis(ORCHESTRATOR, PROJECT_DIR); + } + + private List findIssuesByRuleKey(String ruleKey) { + return adminWsClient.issues().search( + new SearchWsRequest() + .setComponents(singletonList(FILE_KEY)) + .setRules(singletonList(ruleKey))) + .getIssuesList(); + } + + private List findAllIssues() { + return adminWsClient.issues().search(new SearchWsRequest()).getIssuesList(); + } + +} diff --git a/it/it-tests/src/test/java/it/issue/IssueFilterTest.java b/it/it-tests/src/test/java/it/issue/IssueFilterTest.java new file mode 100644 index 00000000000..61eea7bcd89 --- /dev/null +++ b/it/it-tests/src/test/java/it/issue/IssueFilterTest.java @@ -0,0 +1,120 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.issue; + +import com.sonar.orchestrator.locator.FileLocation; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.sonar.wsclient.services.Resource; +import org.sonar.wsclient.services.ResourceQuery; +import util.QaOnly; + +import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.runProjectAnalysis; +import static util.ItUtils.setServerProperties; + +@Category(QaOnly.class) +public class IssueFilterTest extends AbstractIssueTest { + + private static final String PROJECT_KEY = "com.sonarsource.it.samples:multi-modules-exclusions"; + private static final String PROJECT_DIR = "exclusions/xoo-multi-modules"; + + @Before + public void resetData() { + ORCHESTRATOR.resetData(); + + ORCHESTRATOR.getServer().restoreProfile(FileLocation.ofClasspath("/issue/IssueFilterTest/with-many-rules.xml")); + ORCHESTRATOR.getServer().provisionProject(PROJECT_KEY, "project"); + ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "with-many-rules"); + } + + @Test + public void ignore_all_files() { + executeAnalysis( + "sonar.issue.ignore.multicriteria", "1", + "sonar.issue.ignore.multicriteria.1.resourceKey", "**/*.xoo", + "sonar.issue.ignore.multicriteria.1.ruleKey", "*"); + + checkIssueCountBySeverity(4, 0, 0, 4); + } + + @Test + public void enforce_only_on_one_file() { + executeAnalysis( + "sonar.issue.enforce.multicriteria", "1", + "sonar.issue.enforce.multicriteria.1.resourceKey", "**/HelloA1.xoo", + "sonar.issue.enforce.multicriteria.1.ruleKey", "*"); + + checkIssueCountBySeverity( + 1 /* tag */ + 18 /* lines in HelloA1.xoo */ + 1 /* file */, + 0 + 1, + 0, + 0); + } + + @Test + public void enforce_on_two_files_with_same_rule() { + executeAnalysis( + "sonar.issue.enforce.multicriteria", "1,2", + "sonar.issue.enforce.multicriteria.1.resourceKey", "**/HelloA1.xoo", + "sonar.issue.enforce.multicriteria.1.ruleKey", "*", + "sonar.issue.enforce.multicriteria.2.resourceKey", "**/HelloA2.xoo", + "sonar.issue.enforce.multicriteria.2.ruleKey", "*"); + + checkIssueCountBySeverity( + 2 /* tags */ + 18 /* lines in HelloA1.xoo */ + 15 /* lines in HelloA2.xoo */ + 2 /* files */, + 0 + 2, + 0, + 0); + } + + @Test + public void enforce_on_two_files_with_different_rules() { + executeAnalysis( + "sonar.issue.enforce.multicriteria", "1,2", + "sonar.issue.enforce.multicriteria.1.resourceKey", "**/HelloA1.xoo", + "sonar.issue.enforce.multicriteria.1.ruleKey", "xoo:OneIssuePerLine", + "sonar.issue.enforce.multicriteria.2.resourceKey", "**/HelloA2.xoo", + "sonar.issue.enforce.multicriteria.2.ruleKey", "xoo:HasTag"); + + checkIssueCountBySeverity( + 1 /* tag in HelloA2 */ + 18 /* lines in HelloA1.xoo */ + 4 /* files */ + 4 /* modules */, + 4, + 0, + 4); + } + + private void executeAnalysis(String... serverProperties) { + setServerProperties(ORCHESTRATOR, PROJECT_KEY, serverProperties); + runProjectAnalysis(ORCHESTRATOR, PROJECT_DIR); + } + + private void checkIssueCountBySeverity(int total, int perFile, int perCommonRule, int perModule) { + Resource project = ORCHESTRATOR.getServer().getWsClient() + .find(ResourceQuery.createForMetrics(PROJECT_KEY, "violations", "major_violations", "blocker_violations", "critical_violations")); + assertThat(project.getMeasureIntValue("violations")).isEqualTo(total); + assertThat(project.getMeasureIntValue("major_violations")).isEqualTo(perFile); // One per file + assertThat(project.getMeasureIntValue("blocker_violations")).isEqualTo(perCommonRule); // On per common rule + // 'InsufficientCommentDensity' + assertThat(project.getMeasureIntValue("critical_violations")).isEqualTo(perModule); // One per module + } + +} diff --git a/it/it-tests/src/test/java/util/ItUtils.java b/it/it-tests/src/test/java/util/ItUtils.java index 21c8f9765ea..50a2fc1d414 100644 --- a/it/it-tests/src/test/java/util/ItUtils.java +++ b/it/it-tests/src/test/java/util/ItUtils.java @@ -214,6 +214,12 @@ public class ItUtils { } } + public static void setServerProperties(Orchestrator orchestrator, @Nullable String componentKey, String... properties) { + for (int i = 0; i < properties.length; i += 2) { + setServerProperty(orchestrator, componentKey, properties[i], properties[i + 1]); + } + } + public static void resetPeriods(Orchestrator orchestrator) { setServerProperty(orchestrator, "sonar.timemachine.period1", null); setServerProperty(orchestrator, "sonar.timemachine.period2", null); diff --git a/it/it-tests/src/test/resources/issue/IssueFilterOnCommonRulesTest/xoo-common-rules-profile.xml b/it/it-tests/src/test/resources/issue/IssueFilterOnCommonRulesTest/xoo-common-rules-profile.xml new file mode 100644 index 00000000000..9803527b94f --- /dev/null +++ b/it/it-tests/src/test/resources/issue/IssueFilterOnCommonRulesTest/xoo-common-rules-profile.xml @@ -0,0 +1,55 @@ + + + xoo-common-rules + xoo + + + common-xoo + DuplicatedBlocks + CRITICAL + + + common-xoo + InsufficientBranchCoverage + CRITICAL + + + minimumBranchCoverageRatio + 90 + + + + + common-xoo + InsufficientCommentDensity + CRITICAL + + + minimumCommentDensity + 90.0 + + + + + common-xoo + InsufficientLineCoverage + CRITICAL + + + minimumLineCoverageRatio + 90 + + + + + common-xoo + FailedUnitTests + CRITICAL + + + common-xoo + SkippedUnitTests + CRITICAL + + + diff --git a/it/it-tests/src/test/resources/issue/IssueFilterTest/with-many-rules.xml b/it/it-tests/src/test/resources/issue/IssueFilterTest/with-many-rules.xml new file mode 100644 index 00000000000..366a3ab7576 --- /dev/null +++ b/it/it-tests/src/test/resources/issue/IssueFilterTest/with-many-rules.xml @@ -0,0 +1,43 @@ + + with-many-rules + xoo + + + xoo + OneIssuePerLine + MINOR + + + xoo + OneIssuePerFile + MAJOR + + + xoo + OneIssuePerModule + CRITICAL + + + xoo + HasTag + INFO + + + tag + xoo + + + + + common-xoo + InsufficientLineCoverage + BLOCKER + + + minimumLineCoverageRatio + 90 + + + + + -- 2.39.5