diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/server/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 7a85eb6731b4..766a5d1ba386 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -83,6 +83,10 @@ public void init(Properties properties, ServletContext servletContext) { // Platform is injected in Pico, so do not rename this method "start" public void doStart() { + doStart(Startup.ALL); + } + + protected void doStart(Startup startup) { if (started && !isInSafeMode()) { return; } @@ -94,7 +98,7 @@ public void doStart() { started = true; } else { startLevel34Containers(); - executeStartupTasks(); + executeStartupTasks(startup); // switch current container last to avoid giving access to a partially initialized container currentContainer = level4Container; started = true; @@ -105,6 +109,10 @@ public void doStart() { } public void restart() { + restart(Startup.ALL); + } + + protected void restart(Startup startup) { // switch currentContainer on level1 now to avoid exposing a container in the process of stopping currentContainer = level1Container; @@ -115,7 +123,7 @@ public void restart() { // no need to initialize database connection, so level 1 is skipped startLevel2Container(); startLevel34Containers(); - executeStartupTasks(); + executeStartupTasks(startup); currentContainer = level4Container; } @@ -175,7 +183,13 @@ private void startLevel34Containers() { } public void executeStartupTasks() { - serverComponents.executeStartupTasks(level4Container); + executeStartupTasks(Startup.ALL); + } + + private void executeStartupTasks(Startup startup) { + if (startup.ordinal() >= Startup.ALL.ordinal()) { + serverComponents.executeStartupTasks(level4Container); + } } private void startSafeModeContainer() { @@ -256,4 +270,8 @@ public Object getComponent(Object key) { public enum Status { BOOTING, SAFEMODE, UP; } + + public enum Startup { + NO_STARTUP_TASKS, ALL + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java index b38d7754079f..25a4e53be94d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java @@ -35,7 +35,7 @@ public class ActivitiesWsMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryActionMediumTest.java index 21bd55209481..9c146fbba9a1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryActionMediumTest.java @@ -41,7 +41,7 @@ public class HistoryActionMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java index bf6b9e8a2e44..6e01a4b3c9eb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java @@ -62,7 +62,7 @@ public class IssueBulkChangeServiceMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java index f3978d66bf0e..7942227af2d1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java @@ -58,7 +58,7 @@ public class IssueCommentServiceMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java index 474954387061..24ae581f6fb9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java @@ -79,7 +79,7 @@ public class IssueServiceMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsMediumTest.java index c18d372aa25e..18ac9cd2a4a1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsMediumTest.java @@ -59,7 +59,7 @@ public class SearchActionComponentsMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java index 714cc6b1b126..7c429c8c139d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java @@ -63,7 +63,7 @@ public class SearchActionMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceMediumTest.java index 6023df7133eb..6c369d534a99 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceMediumTest.java @@ -52,7 +52,7 @@ public class InternalPermissionServiceMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ServerTesterPlatform.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerTesterPlatform.java new file mode 100644 index 000000000000..786f15000789 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerTesterPlatform.java @@ -0,0 +1,38 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.platform; + +public class ServerTesterPlatform extends Platform { + /** + * Override to make public + */ + @Override + public void doStart(Startup startup) { + super.doStart(startup); + } + + /** + * Override to make public + */ + @Override + public void restart(Startup startup) { + super.restart(startup); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java index 25ebecadd78e..b7f6ebcff4b1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java @@ -54,7 +54,7 @@ public class QProfileExportersTest { @ClassRule - public static ServerTester tester = new ServerTester().addXoo().addComponents( + public static ServerTester tester = new ServerTester().withStartupTasks().addXoo().addComponents( XooRulesDefinition.class, XooProfileDefinition.class, XooExporter.class, StandardExporter.class, XooProfileImporter.class, XooProfileImporterWithMessages.class, XooProfileImporterWithError.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java index e635ee2d7bae..98e82dafb36f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java @@ -64,7 +64,7 @@ public class QProfileServiceMediumTest { @ClassRule - public static ServerTester tester = new ServerTester().addComponents(XooProfileImporter.class, XooExporter.class); + public static ServerTester tester = new ServerTester().withStartupTasks().addComponents(XooProfileImporter.class, XooExporter.class); @org.junit.Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java index 72f032203208..f8891e1eb022 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java @@ -63,7 +63,7 @@ public void tearDown() { @Test public void register_existing_profile_definitions() { - tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class); + tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class); tester.start(); dbSession = dbClient().openSession(false); @@ -106,7 +106,7 @@ public void register_existing_profile_definitions() { @Test public void register_profile_definitions() { - tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class); + tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class); tester.start(); dbSession = dbClient().openSession(false); @@ -164,7 +164,7 @@ public void fail_if_two_definitions_are_marked_as_default_on_the_same_language() @Test public void mark_profile_as_default() { - tester = new ServerTester().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true)); + tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true)); tester.start(); verifyDefaultProfile("xoo", "two"); @@ -172,7 +172,7 @@ public void mark_profile_as_default() { @Test public void use_sonar_way_as_default_profile_if_none_are_marked_as_default() { - tester = new ServerTester().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false)); + tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false)); tester.start(); verifyDefaultProfile("xoo", "Sonar way"); @@ -180,7 +180,7 @@ public void use_sonar_way_as_default_profile_if_none_are_marked_as_default() { @Test public void do_not_reset_default_profile_if_still_valid() { - tester = new ServerTester().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false)); + tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false)); tester.start(); QualityProfileDao profileDao = dbClient().qualityProfileDao(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CompareActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CompareActionMediumTest.java index d60550fb732f..639548566c4c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CompareActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CompareActionMediumTest.java @@ -46,7 +46,7 @@ public class CompareActionMediumTest { @ClassRule - public static ServerTester tester = new ServerTester().addXoo() + public static ServerTester tester = new ServerTester().withStartupTasks().addXoo() .addComponents(new RulesDefinition() { @Override public void define(Context context) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CreateActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CreateActionMediumTest.java index 994cee4a07df..5a137338b5ba 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CreateActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CreateActionMediumTest.java @@ -45,7 +45,7 @@ public class CreateActionMediumTest { // TODO Replace with simpler test with DbTester / EsTester after removal of DaoV2 @ClassRule - public static ServerTester tester = new ServerTester().addXoo().addComponents( + public static ServerTester tester = new ServerTester().withStartupTasks().addXoo().addComponents( XooRulesDefinition.class, XooProfileDefinition.class, XooExporter.class, StandardExporter.class, XooProfileImporter.class, XooProfileImporterWithMessages.class, XooProfileImporterWithError.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java b/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java index 7aa97b0a5354..6c5506ca867a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java +++ b/server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java @@ -19,6 +19,9 @@ */ package org.sonar.server.tester; +import com.google.common.base.Preconditions; +import com.google.common.base.Throwables; +import com.google.common.collect.Lists; import java.io.File; import java.io.IOException; import java.net.URL; @@ -26,10 +29,8 @@ import java.util.List; import java.util.Map; import java.util.Properties; - import javax.annotation.Nullable; import javax.servlet.ServletContext; - import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.junit.rules.ExternalResource; @@ -40,14 +41,13 @@ import org.sonar.process.ProcessProperties; import org.sonar.server.es.EsServerHolder; import org.sonar.server.platform.BackendCleanup; -import org.sonar.server.platform.Platform; +import org.sonar.server.platform.ServerTesterPlatform; import org.sonar.server.plugins.UpdateCenterClient; import org.sonar.server.ws.WsTester; import org.sonar.test.TestUtils; -import com.google.common.base.Preconditions; -import com.google.common.base.Throwables; -import com.google.common.collect.Lists; +import static org.sonar.server.platform.Platform.Startup.ALL; +import static org.sonar.server.platform.Platform.Startup.NO_STARTUP_TASKS; /** * Entry point to implement medium tests of server components. @@ -62,13 +62,19 @@ public class ServerTester extends ExternalResource { private static final Logger LOG = Loggers.get(ServerTester.class); private static final String PROP_PREFIX = "mediumTests."; - private Platform platform; + private ServerTesterPlatform platform; private EsServerHolder esServerHolder; private final File homeDir = TestUtils.newTempDir("tmp-sq-"); private final List components = Lists.newArrayList(WsTester.class); private final Properties initialProps = new Properties(); private final ServletContext servletContext = new AttributeHolderServletContext(); private URL updateCenterUrl; + private boolean startupTasks = false; + + public ServerTester withStartupTasks() { + this.startupTasks = true; + return this; + } /** * Called only when JUnit @Rule or @ClassRule is used. @@ -105,10 +111,10 @@ public void start() { properties.put(StringUtils.substringAfter(key, PROP_PREFIX), entry.getValue()); } } - platform = new Platform(); + platform = new ServerTesterPlatform(); platform.init(properties, servletContext); platform.addComponents(components); - platform.doStart(); + platform.doStart(startupTasks ? ALL : NO_STARTUP_TASKS); } catch (Exception e) { stop(); Throwables.propagate(e); diff --git a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerMediumTest.java index 6f9ae0d9a0ff..54b377a9f8b5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerMediumTest.java @@ -62,7 +62,7 @@ public class ViewIndexerMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withStartupTasks(); @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);