From 1b769c6cb0acc05dd700df5356cad622cd26e207 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 29 Aug 2013 14:55:33 +0200 Subject: [PATCH] SONAR-4600 Deactivate the "IssuePersister" in DryRun mode --- .../org/sonar/batch/issue/IssuePersister.java | 14 +++++++- .../sonar/batch/issue/IssuePersisterTest.java | 35 +++++++++++++++---- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuePersister.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssuePersister.java index 917f65ad24c..6668001f9b7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/IssuePersister.java @@ -19,6 +19,10 @@ */ 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 issues = issueCache.all(); storage.save(issues); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/IssuePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/IssuePersisterTest.java index 0f641d0079d..0a30195aa1f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/IssuePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/IssuePersisterTest.java @@ -19,29 +19,52 @@ */ 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 issues; + private Settings settings; - @Test - public void should_persist_all_issues() throws Exception { - List 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); + } } -- 2.39.5