]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8888 load DefinedQProfiles even when no startup leader
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 16 Mar 2017 09:55:37 +0000 (10:55 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:38:34 +0000 (17:38 +0100)
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileLoader.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileLoaderTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesTest.java

index 4b65e4d69e55028fb3f372c69233fcb154deb901..ae4211f07331214dc2c919f6329d3601f1d0ae62 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.server.platform.web.RegisterServletFilters;
 import org.sonar.server.qualitygate.RegisterQualityGates;
 import org.sonar.server.qualityprofile.CachingRuleActivator;
 import org.sonar.server.qualityprofile.CachingRuleActivatorContextFactory;
+import org.sonar.server.qualityprofile.DefinedQProfileLoader;
 import org.sonar.server.qualityprofile.RegisterQualityProfiles;
 import org.sonar.server.rule.RegisterRules;
 import org.sonar.server.startup.ClearRulesOverloadedDebt;
@@ -54,7 +55,9 @@ public class PlatformLevelStartup extends PlatformLevel {
       IndexerStartupTask.class,
       RegisterMetrics.class,
       RegisterQualityGates.class,
-      RegisterRules.class,
+      RegisterRules.class);
+    add(DefinedQProfileLoader.class);
+    addIfStartupLeader(
       CachingRuleActivatorContextFactory.class,
       CachingRuleActivator.class,
       RegisterQualityProfiles.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileLoader.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileLoader.java
new file mode 100644 (file)
index 0000000..1709b65
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.server.qualityprofile;
+
+import org.picocontainer.Startable;
+
+/**
+ * Startable added to {@link org.sonar.server.platform.platformlevel.PlatformLevelStartup} responsible for initializing
+ * {@link DefinedQProfileRepository}.
+ */
+public class DefinedQProfileLoader implements Startable {
+  private final DefinedQProfileRepository definedQProfileRepository;
+
+  public DefinedQProfileLoader(DefinedQProfileRepository definedQProfileRepository) {
+    this.definedQProfileRepository = definedQProfileRepository;
+  }
+
+  @Override
+  public void start() {
+    definedQProfileRepository.initialize();
+  }
+
+  @Override
+  public void stop() {
+    // nothing to do
+  }
+}
index 23ef3093c429a77fbbc6cf739ee40e09dd46f520..6be9d79fb91e5d98fe0ba1824dbdcaeb260b965a 100644 (file)
@@ -65,7 +65,6 @@ public class RegisterQualityProfiles {
   public void start() {
     Profiler profiler = Profiler.create(Loggers.get(getClass())).startInfo("Register quality profiles");
 
-    definedQProfileRepository.initialize();
     try (DbSession session = dbClient.openSession(false)) {
       List<ActiveRuleChange> changes = new ArrayList<>();
       definedQProfileRepository.getQProfilesByLanguage().entrySet()
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileLoaderTest.java
new file mode 100644 (file)
index 0000000..8828ff2
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.server.qualityprofile;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DefinedQProfileLoaderTest {
+  @Rule
+  public DefinedQProfileRepositoryRule definedQProfileRepositoryRule = new DefinedQProfileRepositoryRule();
+
+  private DefinedQProfileLoader underTest = new DefinedQProfileLoader(definedQProfileRepositoryRule);
+
+  @Test
+  public void start_initializes_DefinedQProfileRepository() {
+    underTest.start();
+
+    assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
+  }
+}
index d25d5f8f88c249147ddf70b14defe6cde1ecc845..007a1cd715ccb296b53c25041d85912197ddcacc 100644 (file)
@@ -71,9 +71,18 @@ public class RegisterQualityProfilesTest {
     new CachingRuleActivator(mockedSystem2, dbClient, null, new CachingRuleActivatorContextFactory(dbClient), null, null, userSessionRule),
     mockedActiveRuleIndexer);
 
+  @Test
+  public void start_fails_if_DefinedQProfileRepository_has_not_been_initialized() {
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("initialize must be called first");
+    
+    underTest.start();
+  }
+
   @Test
   public void no_action_in_DB_nothing_to_index_when_there_is_no_DefinedQProfile() {
     RegisterQualityProfiles underTest = new RegisterQualityProfiles(definedQProfileRepositoryRule, mockedDbClient, null, null, mockedActiveRuleIndexer);
+    definedQProfileRepositoryRule.initialize();
 
     underTest.start();
 
@@ -91,6 +100,7 @@ public class RegisterQualityProfilesTest {
     String[] formattedDates = {formatDateTime(new Date(dates[0])), formatDateTime(new Date(dates[1]))};
     DefinedQProfile definedQProfile = definedQProfileRepositoryRule.add(FOO_LANGUAGE, "foo1");
     mockForQPInserts(uuids, dates);
+    definedQProfileRepositoryRule.initialize();
 
     underTest.start();
 
@@ -123,10 +133,10 @@ public class RegisterQualityProfilesTest {
     definedQProfileRepositoryRule.add(BAR_LANGUAGE, "bar1", true);
     definedQProfileRepositoryRule.add(BAR_LANGUAGE, "bar2", false);
     mockForQPInserts(new String[]{"uuid1", "uuid2", "uuid3", "uuid4"}, new Long[]{ 1L, 2L, 3L, 4L});
+    definedQProfileRepositoryRule.initialize();
 
     underTest.start();
 
-    assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
     assertThat(getPersistedQP(dbTester.getDefaultOrganization(), FOO_LANGUAGE, "foo1").isDefault()).isFalse();
     assertThat(getPersistedQP(dbTester.getDefaultOrganization(), FOO_LANGUAGE, "foo2").isDefault()).isTrue();
     assertThat(getPersistedQP(dbTester.getDefaultOrganization(), BAR_LANGUAGE, "bar1").isDefault()).isTrue();
@@ -142,10 +152,10 @@ public class RegisterQualityProfilesTest {
     dbClient.loadedTemplateDao().insert(new LoadedTemplateDto(org1.getUuid(), definedQProfile.getLoadedTemplateType()), dbTester.getSession());
     dbTester.commit();
     mockForSingleQPInsert();
+    definedQProfileRepositoryRule.initialize();
 
     underTest.start();
 
-    assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
     assertThat(dbClient.loadedTemplateDao().countByTypeAndKey(definedQProfile.getLoadedTemplateType(), org2.getUuid(), dbTester.getSession()))
       .isEqualTo(1);
     assertThat(dbTester.countRowsOfTable(dbTester.getSession(), TABLE_LOADED_TEMPLATES)).isEqualTo(3);
@@ -163,10 +173,10 @@ public class RegisterQualityProfilesTest {
     DefinedQProfile definedQProfile1 = definedQProfileRepositoryRule.add(FOO_LANGUAGE, name, true);
     DefinedQProfile definedQProfile2 = definedQProfileRepositoryRule.add(BAR_LANGUAGE, name, true);
     mockForQPInserts(new String[]{uuid1, uuid2}, new Long[]{date1, date2});
+    definedQProfileRepositoryRule.initialize();
 
     underTest.start();
 
-    assertThat(definedQProfileRepositoryRule.isInitialized()).isTrue();
     OrganizationDto organization = dbTester.getDefaultOrganization();
     QualityProfileDto dto = getPersistedQP(organization, FOO_LANGUAGE, name);
     String uuidQP1 = dto.getKee();