]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7640 Add IT 988/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 17 May 2016 14:09:25 +0000 (16:09 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 May 2016 12:29:41 +0000 (14:29 +0200)
it/it-tests/src/test/java/it/Category2Suite.java
it/it-tests/src/test/java/it/issue/IssueFilterOnCommonRulesTest.java [new file with mode: 0644]
it/it-tests/src/test/java/it/issue/IssueFilterTest.java [new file with mode: 0644]
it/it-tests/src/test/java/util/ItUtils.java
it/it-tests/src/test/resources/issue/IssueFilterOnCommonRulesTest/xoo-common-rules-profile.xml [new file with mode: 0644]
it/it-tests/src/test/resources/issue/IssueFilterTest/with-many-rules.xml [new file with mode: 0644]

index 2af51cf7aa6b21652c1511ebce29823e90ada8b0..ad57842de3f5d48e5be8b9a3917a887fcefe0435 100644 (file)
@@ -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 (file)
index 0000000..da74881
--- /dev/null
@@ -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<Issues.Issue> findIssuesByRuleKey(String ruleKey) {
+    return adminWsClient.issues().search(
+      new SearchWsRequest()
+        .setComponents(singletonList(FILE_KEY))
+        .setRules(singletonList(ruleKey)))
+      .getIssuesList();
+  }
+
+  private List<Issues.Issue> 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 (file)
index 0000000..61eea7b
--- /dev/null
@@ -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
+  }
+
+}
index 21c8f9765ea1d8e7020962f7ff52f2140ebf6565..50a2fc1d4144d2af228396ed4939f6b3cefbba20 100644 (file)
@@ -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 (file)
index 0000000..9803527
--- /dev/null
@@ -0,0 +1,55 @@
+<?xml version="1.0"?><!-- Generated by Sonar -->
+<profile>
+  <name>xoo-common-rules</name>
+  <language>xoo</language>
+  <rules>
+    <rule>
+      <repositoryKey>common-xoo</repositoryKey>
+      <key>DuplicatedBlocks</key>
+      <priority>CRITICAL</priority>
+    </rule>
+    <rule>
+      <repositoryKey>common-xoo</repositoryKey>
+      <key>InsufficientBranchCoverage</key>
+      <priority>CRITICAL</priority>
+      <parameters>
+        <parameter>
+          <key>minimumBranchCoverageRatio</key>
+          <value>90</value>
+        </parameter>
+      </parameters>
+    </rule>
+    <rule>
+      <repositoryKey>common-xoo</repositoryKey>
+      <key>InsufficientCommentDensity</key>
+      <priority>CRITICAL</priority>
+      <parameters>
+        <parameter>
+          <key>minimumCommentDensity</key>
+          <value>90.0</value>
+        </parameter>
+      </parameters>
+    </rule>
+    <rule>
+      <repositoryKey>common-xoo</repositoryKey>
+      <key>InsufficientLineCoverage</key>
+      <priority>CRITICAL</priority>
+      <parameters>
+        <parameter>
+          <key>minimumLineCoverageRatio</key>
+          <value>90</value>
+        </parameter>
+      </parameters>
+    </rule>
+    <rule>
+      <repositoryKey>common-xoo</repositoryKey>
+      <key>FailedUnitTests</key>
+      <priority>CRITICAL</priority>
+    </rule>
+    <rule>
+      <repositoryKey>common-xoo</repositoryKey>
+      <key>SkippedUnitTests</key>
+      <priority>CRITICAL</priority>
+    </rule>
+  </rules>
+</profile>
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 (file)
index 0000000..366a3ab
--- /dev/null
@@ -0,0 +1,43 @@
+<profile>
+  <name>with-many-rules</name>
+  <language>xoo</language>
+  <rules>
+    <rule>
+      <repositoryKey>xoo</repositoryKey>
+      <key>OneIssuePerLine</key>
+      <priority>MINOR</priority>
+    </rule>
+    <rule>
+      <repositoryKey>xoo</repositoryKey>
+      <key>OneIssuePerFile</key>
+      <priority>MAJOR</priority>
+    </rule>
+    <rule>
+      <repositoryKey>xoo</repositoryKey>
+      <key>OneIssuePerModule</key>
+      <priority>CRITICAL</priority>
+    </rule>
+    <rule>
+      <repositoryKey>xoo</repositoryKey>
+      <key>HasTag</key>
+      <priority>INFO</priority>
+      <parameters>
+        <parameter>
+          <key>tag</key>
+          <value>xoo</value>
+        </parameter>
+      </parameters>
+    </rule>
+    <rule>
+      <repositoryKey>common-xoo</repositoryKey>
+      <key>InsufficientLineCoverage</key>
+      <priority>BLOCKER</priority>
+      <parameters>
+        <parameter>
+          <key>minimumLineCoverageRatio</key>
+          <value>90</value>
+        </parameter>
+      </parameters>
+    </rule>
+  </rules>
+</profile>