Introduce test to prove PersistentCacheFactory can be overridden
Gerrit persistent cache defaults to an H2 implementation, which is
bound at start up via the sys injector.
Change-Id I7562b210fa introduced the ability to override this binding by
the usage of the CacheImpl annotation, however no tests were provided to
prove this behavior.
Introduce an integration test to ensure a different
PersistenCacheFactory implementation can indeed be bound.
Bug: Issue 10276
Change-Id: Ib4133eb899a04680099ca50e359f48653a7d23eb
diff --git a/javatests/com/google/gerrit/server/cache/BUILD b/javatests/com/google/gerrit/server/cache/BUILD
index c255e61..b3b2f5a 100644
--- a/javatests/com/google/gerrit/server/cache/BUILD
+++ b/javatests/com/google/gerrit/server/cache/BUILD
@@ -1,8 +1,9 @@
load("//tools/bzl:junit.bzl", "junit_tests")
+load("//javatests/com/google/gerrit/acceptance:tests.bzl", "acceptance_tests")
junit_tests(
name = "tests",
- srcs = glob(["*.java"]),
+ srcs = glob(["*Test.java"]),
deps = [
"//java/com/google/gerrit/server",
"//java/com/google/gerrit/testing:gerrit-test-util",
@@ -10,3 +11,9 @@
"//lib/truth",
],
)
+
+acceptance_tests(
+ srcs = glob(["*IT.java"]),
+ group = "server_cache",
+ labels = ["server"],
+)
diff --git a/javatests/com/google/gerrit/server/cache/PersistentCacheFactoryIT.java b/javatests/com/google/gerrit/server/cache/PersistentCacheFactoryIT.java
new file mode 100644
index 0000000..d8c6fe2
--- /dev/null
+++ b/javatests/com/google/gerrit/server/cache/PersistentCacheFactoryIT.java
@@ -0,0 +1,74 @@
+// Copyright (C) 2020 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.server.cache;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.server.ModuleImpl;
+import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
+import org.junit.Test;
+
+public class PersistentCacheFactoryIT extends AbstractDaemonTest {
+
+ @Inject PersistentCacheFactory persistentCacheFactory;
+
+ @ModuleImpl(name = CacheModule.PERSISTENT_MODULE)
+ public static class Module extends AbstractModule {
+
+ @Override
+ protected void configure() {
+ bind(PersistentCacheFactory.class).to(TestCacheFactory.class);
+ }
+ }
+
+ @Override
+ public com.google.inject.Module createModule() {
+ return new Module();
+ }
+
+ @Test
+ public void shouldH2PersistentCacheBeReplaceableByADifferentCacheImplementation() {
+ assertThat(persistentCacheFactory).isInstanceOf(TestCacheFactory.class);
+ }
+
+ public static class TestCacheFactory implements PersistentCacheFactory {
+
+ private final MemoryCacheFactory memoryCacheFactory;
+
+ @Inject
+ TestCacheFactory(MemoryCacheFactory memoryCacheFactory) {
+ this.memoryCacheFactory = memoryCacheFactory;
+ }
+
+ @Override
+ public <K, V> com.google.common.cache.Cache<K, V> build(
+ PersistentCacheDef<K, V> def, CacheBackend backend) {
+ return memoryCacheFactory.build(def, backend);
+ }
+
+ @Override
+ public <K, V> LoadingCache<K, V> build(
+ PersistentCacheDef<K, V> def, CacheLoader<K, V> loader, CacheBackend backend) {
+ return memoryCacheFactory.build(def, loader, backend);
+ }
+
+ @Override
+ public void onStop(String plugin) {}
+ }
+}