]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4600 Deactivate the "IssuePersister" in DryRun mode
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 29 Aug 2013 12:55:33 +0000 (14:55 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 29 Aug 2013 12:55:33 +0000 (14:55 +0200)
sonar-batch/src/main/java/org/sonar/batch/issue/IssuePersister.java
sonar-batch/src/test/java/org/sonar/batch/issue/IssuePersisterTest.java

index 917f65ad24c2dfe0af7eb54582a3ec17f23d9d9c..6668001f9b724ddc1d846558dade92b435f40b8e 100644 (file)
  */
 package org.sonar.batch.issue;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
 import org.sonar.api.issue.internal.DefaultIssue;
 import org.sonar.batch.index.ScanPersister;
 
@@ -27,16 +31,24 @@ import org.sonar.batch.index.ScanPersister;
  */
 public class IssuePersister implements ScanPersister {
 
+  private static final Logger LOG = LoggerFactory.getLogger(IssuePersister.class);
+
   private final IssueCache issueCache;
   private final ScanIssueStorage storage;
+  private Settings settings;
 
-  public IssuePersister(IssueCache issueCache, ScanIssueStorage storage) {
+  public IssuePersister(IssueCache issueCache, ScanIssueStorage storage, Settings settings) {
     this.issueCache = issueCache;
     this.storage = storage;
+    this.settings = settings;
   }
 
   @Override
   public void persist() {
+    if (settings.getBoolean(CoreProperties.DRY_RUN)) {
+      LOG.debug("IssuePersister skipped in dryRun");
+      return;
+    }
     Iterable<DefaultIssue> issues = issueCache.all();
     storage.save(issues);
   }
index 0f641d0079d1e86c81432d2d680d502c9c5b3786..0a30195aa1f5f16d49a4887826450486c6442377 100644 (file)
  */
 package org.sonar.batch.issue;
 
+import org.junit.Before;
 import org.junit.Test;
+import org.sonar.api.config.Settings;
 import org.sonar.api.issue.internal.DefaultIssue;
 import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.Arrays;
 import java.util.List;
 
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class IssuePersisterTest extends AbstractDaoTestCase {
 
   IssuePersister persister;
+  private ScanIssueStorage storage;
+  private List<DefaultIssue> issues;
+  private Settings settings;
 
-  @Test
-  public void should_persist_all_issues() throws Exception {
-    List<DefaultIssue> issues = Arrays.asList(new DefaultIssue());
+  @Before
+  public void prepare() {
+    issues = Arrays.asList(new DefaultIssue());
     IssueCache issueCache = mock(IssueCache.class);
     when(issueCache.all()).thenReturn(issues);
-    ScanIssueStorage storage = mock(ScanIssueStorage.class);
+    storage = mock(ScanIssueStorage.class);
+
+    settings = new Settings();
+    persister = new IssuePersister(issueCache, storage, settings);
+  }
 
-    persister = new IssuePersister(issueCache, storage);
+  @Test
+  public void should_persist_all_issues() throws Exception {
     persister.persist();
 
     verify(storage, times(1)).save(issues);
   }
+
+  @Test
+  public void should_not_persist_issues_in_dry_run() throws Exception {
+    settings.setProperty("sonar.dryRun", true);
+
+    persister.persist();
+
+    verify(storage, never()).save(issues);
+  }
 }