Allow to switch Note DB on or off for server tests
Introduce a new base class, GerritServerTests, that can cause derived
test classes to run with or without the note DB enabled, depending on
the value of the GERRIT_ENABLE_NOTEDB environment variable.
Tests can be run with note DB enabled by:
GERRIT_ENABLE_NOTEDB=Y buck test --no-results-cache
Setting the variable to any of the values 'Y', 'YES' or 'TRUE' (case
insensitive) will cause the note DB to be enabled for the tests. The
--no-results-cache option must be used when alternating between on and
off, so that buck correctly re-runs the tests.
Update existing tests that currently run with multiple configurations
(default, and note DB enabled) to use the new base class.
Remove the isNoteDbTestEnabled method from AbstractDaemonTest and
instead static import the one now defined in GerritServerTests.
Change-Id: Id30d6a622ec3d2e05ea0bec300c85a080ec8c053
diff --git a/gerrit-acceptance-framework/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java b/gerrit-acceptance-framework/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
index 7dbbfb5..86abd78 100644
--- a/gerrit-acceptance-framework/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
+++ b/gerrit-acceptance-framework/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
@@ -18,6 +18,7 @@
import static com.google.gerrit.acceptance.GitUtil.initSsh;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.gerrit.server.project.Util.block;
+import static com.google.gerrit.testutil.GerritServerTests.isNoteDbTestEnabled;
import com.google.common.base.Function;
import com.google.common.base.Optional;
@@ -233,13 +234,6 @@
return cfg.getBoolean("change", null, "submitWholeTopic", false);
}
- private static boolean isNoteDbTestEnabled() {
- final String[] RUN_FLAGS = {"yes", "y", "true"};
- String value = System.getenv("GERRIT_ENABLE_NOTEDB");
- return value != null &&
- Arrays.asList(RUN_FLAGS).contains(value.toLowerCase());
- }
-
protected void beforeTest(Description description) throws Exception {
GerritServer.Description classDesc =
GerritServer.Description.forTestClass(description, configName);
diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/change/CommentsTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/change/CommentsTest.java
index f5e6d74..5990fdc 100644
--- a/gerrit-server/src/test/java/com/google/gerrit/server/change/CommentsTest.java
+++ b/gerrit-server/src/test/java/com/google/gerrit/server/change/CommentsTest.java
@@ -67,8 +67,8 @@
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.ProjectCache;
-import com.google.gerrit.testutil.ConfigSuite;
import com.google.gerrit.testutil.FakeAccountCache;
+import com.google.gerrit.testutil.GerritServerTests;
import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.TestChanges;
import com.google.gwtorm.server.ListResultSet;
@@ -90,7 +90,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
import java.sql.Timestamp;
import java.util.Collections;
@@ -98,20 +97,10 @@
import java.util.Map;
import java.util.TimeZone;
-@RunWith(ConfigSuite.class)
-public class CommentsTest {
+public class CommentsTest extends GerritServerTests {
private static final TimeZone TZ =
TimeZone.getTimeZone("America/Los_Angeles");
- @ConfigSuite.Parameter
- public Config config;
-
- @ConfigSuite.Config
- @GerritServerConfig
- public static Config noteDbEnabled() {
- return NotesMigration.allEnabledConfig();
- }
-
@Rule
public ExpectedException exception = ExpectedException.none();
diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java
index feca80b..95b6c99 100644
--- a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java
+++ b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java
@@ -63,8 +63,8 @@
import com.google.gerrit.server.schema.SchemaCreator;
import com.google.gerrit.server.util.RequestContext;
import com.google.gerrit.server.util.ThreadLocalRequestContext;
-import com.google.gerrit.testutil.ConfigSuite;
import com.google.gerrit.testutil.DisabledReviewDb;
+import com.google.gerrit.testutil.GerritServerTests;
import com.google.gerrit.testutil.InMemoryDatabase;
import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo;
@@ -74,7 +74,6 @@
import com.google.inject.util.Providers;
import org.eclipse.jgit.junit.TestRepository;
-import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.revwalk.RevCommit;
import org.joda.time.DateTime;
@@ -86,7 +85,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Arrays;
@@ -94,27 +92,10 @@
import java.util.concurrent.atomic.AtomicLong;
@Ignore
-@RunWith(ConfigSuite.class)
-public abstract class AbstractQueryChangesTest {
- @ConfigSuite.Default
- public static Config defaultConfig() {
- return updateConfig(new Config());
- }
-
- @ConfigSuite.Config
- public static Config noteDbEnabled() {
- return updateConfig(NotesMigration.allEnabledConfig());
- }
-
+public abstract class AbstractQueryChangesTest extends GerritServerTests {
@Rule
public ExpectedException exception = ExpectedException.none();
- private static Config updateConfig(Config cfg) {
- cfg.setInt("index", null, "maxPages", 10);
- return cfg;
- }
-
- @ConfigSuite.Parameter public Config config;
@Inject protected AccountManager accountManager;
@Inject protected BatchUpdate.Factory updateFactory;
@Inject protected ChangeInserter.Factory changeFactory;
@@ -144,6 +125,11 @@
protected abstract Injector createInjector();
@Before
+ public void setUpConfig() throws Exception {
+ config.setInt("index", null, "maxPages", 10);
+ }
+
+ @Before
public void setUpInjector() throws Exception {
lifecycle = new LifecycleManager();
Injector injector = createInjector();
diff --git a/gerrit-server/src/test/java/com/google/gerrit/testutil/GerritServerTests.java b/gerrit-server/src/test/java/com/google/gerrit/testutil/GerritServerTests.java
new file mode 100644
index 0000000..fba4034
--- /dev/null
+++ b/gerrit-server/src/test/java/com/google/gerrit/testutil/GerritServerTests.java
@@ -0,0 +1,69 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.testutil;
+
+import com.google.gerrit.server.notedb.NotesMigration;
+
+import org.eclipse.jgit.lib.Config;
+import org.junit.Rule;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runner.RunWith;
+import org.junit.runners.model.Statement;
+
+import java.util.Arrays;
+
+@RunWith(ConfigSuite.class)
+public class GerritServerTests {
+ @ConfigSuite.Parameter
+ public Config config;
+
+ @ConfigSuite.Name
+ private String configName;
+
+ public static boolean isNoteDbTestEnabled() {
+ final String[] RUN_FLAGS = {"yes", "y", "true"};
+ String value = System.getenv("GERRIT_ENABLE_NOTEDB");
+ return value != null &&
+ Arrays.asList(RUN_FLAGS).contains(value.toLowerCase());
+ }
+
+ @Rule
+ public TestRule testRunner = new TestRule() {
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ beforeTest();
+ try {
+ base.evaluate();
+ } finally {
+ afterTest();
+ }
+ }
+ };
+ }
+ };
+
+ public void beforeTest() throws Exception {
+ if (isNoteDbTestEnabled()) {
+ NotesMigration.setAllEnabledConfig(config);
+ }
+ }
+
+ public void afterTest() {
+ }
+}