]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5094 Memoize loaded quality gate (do not reload on next provide call)
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 11 Mar 2014 11:11:03 +0000 (12:11 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 11 Mar 2014 11:11:10 +0000 (12:11 +0100)
sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java
sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java

index 3cfba31d384855c5b976a767b114ca3276991957..d628cd324b79bc14ee6ac0986426decfd317c208 100644 (file)
@@ -40,8 +40,13 @@ public class QualityGateProvider extends ProviderAdapter {
 
   private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
 
+  private QualityGate instance;
+
   public QualityGate provide(Settings settings, ServerClient client, MetricFinder metricFinder) {
-    return init(settings, client, metricFinder, LOG);
+    if (instance == null) {
+      instance = init(settings, client, metricFinder, LOG);
+    }
+    return instance;
   }
 
   @VisibleForTesting
@@ -52,8 +57,8 @@ public class QualityGateProvider extends ProviderAdapter {
       logger.info("No quality gate is configured.");
     } else {
       result = load(qualityGateSetting, client.wsClient().qualityGateClient(), metricFinder);
+      logger.info("Loaded quality gate '{}'", result.name());
     }
-    logger.info("Loaded quality gate '{}'", result.name());
     return result;
   }
 
index 467298667360a3a0f55309c73d0167e22d92fe81..739a380d4fa34e80f44315dae1a0f67a4846eadc 100644 (file)
@@ -40,9 +40,7 @@ import java.net.HttpURLConnection;
 import java.util.Collection;
 
 import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 @RunWith(MockitoJUnitRunner.class)
 public class QualityGateProviderTest {
@@ -71,9 +69,11 @@ public class QualityGateProviderTest {
 
   @Test
   public void should_load_empty_quality_gate_from_default_settings() {
-    assertThat(new QualityGateProvider().provide(settings, client, metricFinder).conditions()).isEmpty();
-    assertThat(new QualityGateProvider().init(settings, client, metricFinder, logger).isEnabled()).isFalse();
-    verify(logger).info("No quality gate is configured.");
+    QualityGateProvider provider = new QualityGateProvider();
+    assertThat(provider.provide(settings, client, metricFinder).conditions()).isEmpty();
+    assertThat(provider.init(settings, client, metricFinder, logger).isEnabled()).isFalse();
+    verify(logger, times(1)).info("No quality gate is configured.");
+    verify(settings, times(2)).getString("sonar.qualitygate");
   }
 
   @Test