Skip to content

Commit

Permalink
ServerTester don't do startup tasks by default anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed May 20, 2015
1 parent 1abbd25 commit 695f310
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 30 deletions.
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -256,4 +270,8 @@ public Object getComponent(Object key) {
public enum Status {
BOOTING, SAFEMODE, UP;
}

public enum Startup {
NO_STARTUP_TASKS, ALL
}
}
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down
@@ -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);
}
}
Expand Up @@ -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);
Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -164,23 +164,23 @@ 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");
}

@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");
}

@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();
Expand Down
Expand Up @@ -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) {
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -19,17 +19,18 @@
*/
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;
import java.util.Arrays;
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;
Expand All @@ -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.
Expand All @@ -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<Object> components = Lists.<Object>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.
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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);

Expand Down

0 comments on commit 695f310

Please sign in to comment.