]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4382 Add a success log at the end of dryRun analysis
authorSimon Brandhof <simon.brandhof@gmail.com>
Sun, 9 Jun 2013 20:54:25 +0000 (22:54 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Sun, 9 Jun 2013 20:54:25 +0000 (22:54 +0200)
sonar-batch/src/main/java/org/sonar/batch/phases/UpdateStatusJob.java
sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java

index 4ee9697b5ff3a6bf27a49f8efee25b6a02877269..0dd90eca8dff1611ab828d83965a768b574ba9a3 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.batch.phases;
 
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.CoreProperties;
@@ -31,7 +33,6 @@ import org.sonar.batch.bootstrap.ServerClient;
 import org.sonar.batch.index.ResourcePersister;
 
 import javax.persistence.Query;
-
 import java.util.List;
 
 public class UpdateStatusJob implements BatchComponent {
@@ -75,7 +76,15 @@ public class UpdateStatusJob implements BatchComponent {
     Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, false);
     boolean isLast = (previousLastSnapshot == null || previousLastSnapshot.getCreatedAt().before(snapshot.getCreatedAt()));
     setFlags(snapshot, isLast, Snapshot.STATUS_PROCESSED);
-    if (!settings.getBoolean(CoreProperties.DRY_RUN)) {
+    logSuccess(LoggerFactory.getLogger(getClass()));
+  }
+
+  @VisibleForTesting
+  void logSuccess(Logger logger) {
+    if (settings.getBoolean(CoreProperties.DRY_RUN)) {
+      logger.info("ANALYSIS SUCCESSFUL");
+
+    } else {
       String baseUrl = settings.getString(CoreProperties.SERVER_BASE_URL);
       if (baseUrl.equals(settings.getDefaultValue(CoreProperties.SERVER_BASE_URL))) {
         // If server base URL was not configured in Sonar server then is is better to take URL configured on batch side
@@ -85,7 +94,7 @@ public class UpdateStatusJob implements BatchComponent {
         baseUrl += "/";
       }
       String url = baseUrl + "dashboard/index/" + project.getKey();
-      LoggerFactory.getLogger(getClass()).info("ANALYSIS SUCCESSFUL, you can browse {}", url);
+      logger.info("ANALYSIS SUCCESSFUL, you can browse {}", url);
     }
   }
 
index 3102e3cc36d96bd23cef9981ef74971ac2966ef5..b38e777583eb97a9edaaa5d22fd0dce095c7b097 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.batch.phases;
 
 import org.junit.Test;
+import org.slf4j.Logger;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.database.DatabaseSession;
@@ -29,12 +30,14 @@ import org.sonar.api.security.ResourcePermissions;
 import org.sonar.batch.bootstrap.ServerClient;
 import org.sonar.batch.index.DefaultResourcePersister;
 import org.sonar.batch.index.ResourceCache;
+import org.sonar.batch.index.ResourcePersister;
 import org.sonar.batch.index.SnapshotCache;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
 import javax.persistence.Query;
 
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
 public class UpdateStatusJobTest extends AbstractDbUnitTestCase {
 
@@ -57,10 +60,10 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase {
     setupData("sharedFixture", fixture);
 
     DatabaseSession session = getSession();
-    UpdateStatusJob sensor = new UpdateStatusJob(new Settings().appendProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"), mock(ServerClient.class), session,
-        new DefaultResourcePersister(session, mock(ResourcePermissions.class), mock(SnapshotCache.class), mock(ResourceCache.class)),
-        mock(Project.class), loadSnapshot(snapshotId));
-    sensor.execute();
+    UpdateStatusJob job = new UpdateStatusJob(new Settings().appendProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"), mock(ServerClient.class), session,
+      new DefaultResourcePersister(session, mock(ResourcePermissions.class), mock(SnapshotCache.class), mock(ResourceCache.class)),
+      mock(Project.class), loadSnapshot(snapshotId));
+    job.execute();
 
     checkTables(fixture, "snapshots");
   }
@@ -70,4 +73,32 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase {
     query.setParameter("id", id);
     return (Snapshot) query.getSingleResult();
   }
+
+  @Test
+  public void should_log_successful_analysis() throws Exception {
+    Settings settings = new Settings();
+    settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/");
+    Project project = new Project("struts");
+    UpdateStatusJob job = new UpdateStatusJob(settings, mock(ServerClient.class), mock(DatabaseSession.class),
+      mock(ResourcePersister.class), project, mock(Snapshot.class));
+
+    Logger logger = mock(Logger.class);
+    job.logSuccess(logger);
+
+    verify(logger).info("ANALYSIS SUCCESSFUL, you can browse {}", "http://myserver/dashboard/index/struts");
+  }
+
+  @Test
+  public void should_log_successful_dry_run_analysis() throws Exception {
+    Settings settings = new Settings();
+    settings.setProperty("sonar.dryRun", true);
+    Project project = new Project("struts");
+    UpdateStatusJob job = new UpdateStatusJob(settings, mock(ServerClient.class), mock(DatabaseSession.class),
+      mock(ResourcePersister.class), project, mock(Snapshot.class));
+
+    Logger logger = mock(Logger.class);
+    job.logSuccess(logger);
+
+    verify(logger).info("ANALYSIS SUCCESSFUL");
+  }
 }