]> source.dussan.org Git - sonarqube.git/commitdiff
fix issues callback with multiple analysis
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 31 Jul 2015 15:31:27 +0000 (17:31 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 31 Jul 2015 15:37:28 +0000 (17:37 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java
sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesPreviewMediumTest.java [new file with mode: 0644]

index 94065d8a2d375636a2435a2a85f4ed56d2f56faa..d28ff6cf2ac12ba22cc8e93a70ce31b5604ff4a3 100644 (file)
@@ -110,8 +110,7 @@ public final class Batch {
    */
   public Batch executeTask(Map<String, String> analysisProperties, IssueListener issueListener) {
     checkStarted();
-    components.add(issueListener);
-    bootstrapContainer.executeAnalysis(analysisProperties, components);
+    bootstrapContainer.executeAnalysis(analysisProperties, components, issueListener);
     return this;
   }
 
index 3f078b2b08f7884353e851cf5de42efac966a603..88690a3bfcba7cca837bea635a1d69c525004571 100644 (file)
@@ -181,6 +181,11 @@ public class BatchMediumTester {
       projectRefProvider.addFileData(moduleKey, path, fileData);
       return this;
     }
+    
+    public BatchMediumTesterBuilder setLastBuildDate(Date d) {
+      projectRefProvider.setLastAnalysisDate(d);
+      return this;
+    }
 
     public BatchMediumTesterBuilder mockServerIssue(ServerIssue issue) {
       serverIssues.getServerIssues().add(issue);
@@ -346,6 +351,11 @@ public class BatchMediumTester {
       ref.addFileData(moduleKey, path, fileData);
       return this;
     }
+    
+    public FakeProjectRepositoriesLoader setLastAnalysisDate(Date d) {
+      ref.setLastAnalysisDate(d);
+      return this;
+    }
 
   }
 
diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesPreviewMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesPreviewMediumTest.java
new file mode 100644 (file)
index 0000000..6177d15
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.mediumtest.issues;
+
+import org.junit.rules.TemporaryFolder;
+import org.sonar.batch.bootstrapper.IssueListener;
+import org.junit.After;
+import org.junit.Before;
+import com.google.common.collect.ImmutableMap;
+import org.sonar.api.CoreProperties;
+import org.sonar.batch.mediumtest.BatchMediumTester;
+import org.sonar.batch.protocol.input.ActiveRule;
+import org.sonar.xoo.XooPlugin;
+import org.sonar.xoo.rule.XooRulesDefinition;
+import org.apache.commons.io.FileUtils;
+import org.junit.Test;
+import org.sonar.batch.mediumtest.TaskResult;
+
+import java.io.File;
+import java.util.Date;
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class IssuesPreviewMediumTest {
+  @org.junit.Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  public BatchMediumTester testerPreview = BatchMediumTester.builder()
+    .registerPlugin("xoo", new XooPlugin())
+    .addDefaultQProfile("xoo", "Sonar Way")
+    .addRules(new XooRulesDefinition())
+    .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"))
+    .setLastBuildDate(new Date())
+    .build();
+
+  @Before
+  public void prepare() {
+    testerPreview.start();
+  }
+
+  @After
+  public void stop() {
+    testerPreview.stop();
+  }
+
+  @Test
+  public void testIssueCallback() throws Exception {
+    File projectDir = new File(IssuesMediumTest.class.getResource("/mediumtest/xoo/sample").toURI());
+    File tmpDir = temp.newFolder();
+    FileUtils.copyDirectory(projectDir, tmpDir);
+    IssueRecorder issueListener = new IssueRecorder();
+
+    TaskResult result1 = testerPreview
+      .newScanTask(new File(tmpDir, "sonar-project.properties"))
+      .setIssueListener(issueListener)
+      .property(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW)
+      .start();
+    
+    assertThat(result1.trackedIssues()).hasSize(14);
+    assertThat(issueListener.issueList).hasSize(14);
+    issueListener = new IssueRecorder();
+    
+    TaskResult result2 = testerPreview
+      .newScanTask(new File(tmpDir, "sonar-project.properties"))
+      .setIssueListener(issueListener)
+      .property(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW)
+      .start();
+    
+    
+    assertThat(result2.trackedIssues()).hasSize(28);
+    assertThat(issueListener.issueList).hasSize(28);
+  }
+
+  private class IssueRecorder implements IssueListener {
+    List<org.sonar.api.issue.Issue> issueList = new LinkedList<>();
+
+    @Override
+    public void handle(org.sonar.api.issue.Issue issue) {
+      issueList.add(issue);
+    }
+  }
+}