Manually bind plugin guice modules in gerrit Guice universe
Short circuit the plugin loading phase from the unit tests by passing the module names from the manifest file. This allows us to avoid building and deploying the plugin JAR from within build tool chain. To use the simplified plugin bootstrap tests, plugin test class must be inherited from the LightweightPluginDaemonTest class and must be annotated with @TestPlugin annotation: @TestPlugin( name = "cookbook", sysModule = "com.googlesource.gerrit.plugins.cookbook.Module", httpModule = "com.googlesource.gerrit.plugins.cookbook.HttpModule", sshModule = "com.googlesource.gerrit.plugins.cookbook.SshModule" ) public class CookbookIT extends LightweightPluginDaemonTest { @Test public void revisionTest() throws Exception { createChange(); RestResponse response = adminRestSession.post("/changes/1/revisions/1/cookbook~hello-revision"); assertThat(response.getEntityContent()) .contains("Hello admin from change 1, patch set 1!"); } } Inspired-By: Dave Borowitz <dborowitz@google.com> Change-Id: I689bb71413ecfbbf99f72730b0d2617bf526d9dd
This commit is contained in:
parent
9c6306532b
commit
8df8af2572
@ -0,0 +1,64 @@
|
|||||||
|
// Copyright (C) 2016 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.acceptance;
|
||||||
|
|
||||||
|
import com.google.gerrit.server.PluginUser;
|
||||||
|
import com.google.gerrit.server.plugins.PluginGuiceEnvironment;
|
||||||
|
import com.google.gerrit.server.plugins.TestServerPlugin;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
|
||||||
|
public class LightweightPluginDaemonTest extends AbstractDaemonTest {
|
||||||
|
@Inject
|
||||||
|
private PluginGuiceEnvironment env;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private PluginUser.Factory pluginUserFactory;
|
||||||
|
|
||||||
|
private TestServerPlugin plugin;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
TestPlugin testPlugin = getTestPlugin(getClass());
|
||||||
|
String name = testPlugin.name();
|
||||||
|
plugin = new TestServerPlugin(name,
|
||||||
|
canonicalWebUrl.get() + "plugins/" + name,
|
||||||
|
pluginUserFactory.create(name),
|
||||||
|
getClass().getClassLoader(),
|
||||||
|
testPlugin.sysModule(),
|
||||||
|
testPlugin.httpModule(),
|
||||||
|
testPlugin.sshModule());
|
||||||
|
|
||||||
|
plugin.start(env);
|
||||||
|
env.onStartPlugin(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
plugin.stop(env);
|
||||||
|
env.onStopPlugin(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TestPlugin getTestPlugin(Class<?> clazz) {
|
||||||
|
for (; clazz != null; clazz = clazz.getSuperclass()) {
|
||||||
|
if (clazz.getAnnotation(TestPlugin.class) != null) {
|
||||||
|
return clazz.getAnnotation(TestPlugin.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("TestPlugin annotation missing");
|
||||||
|
}
|
||||||
|
}
|
@ -104,12 +104,11 @@ public abstract class PluginDaemonTest extends AbstractDaemonTest {
|
|||||||
if (subPath.endsWith("plugins")) {
|
if (subPath.endsWith("plugins")) {
|
||||||
pluginsIdx = idx;
|
pluginsIdx = idx;
|
||||||
}
|
}
|
||||||
if (subPath.endsWith(BAZELOUT)) {
|
if (subPath.endsWith(BAZELOUT) || subPath.endsWith(ECLIPSE)) {
|
||||||
bazel = true;
|
bazel = true;
|
||||||
buckOutIdx = idx;
|
buckOutIdx = idx;
|
||||||
}
|
}
|
||||||
// TODO(davido): Fix Bazel plugin test from Eclipse
|
if (subPath.endsWith(BUCKOUT)) {
|
||||||
if (subPath.endsWith(BUCKOUT) || subPath.endsWith(ECLIPSE)) {
|
|
||||||
buckOutIdx = idx;
|
buckOutIdx = idx;
|
||||||
}
|
}
|
||||||
idx++;
|
idx++;
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
// Copyright (C) 2016 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.acceptance;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target({TYPE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface TestPlugin {
|
||||||
|
String name();
|
||||||
|
|
||||||
|
String sysModule() default "";
|
||||||
|
String httpModule() default "";
|
||||||
|
String sshModule() default "";
|
||||||
|
}
|
@ -90,7 +90,8 @@ public abstract class Plugin {
|
|||||||
this.snapshot = snapshot;
|
this.snapshot = snapshot;
|
||||||
this.pluginUser = pluginUser;
|
this.pluginUser = pluginUser;
|
||||||
this.cacheKey = new Plugin.CacheKey(name);
|
this.cacheKey = new Plugin.CacheKey(name);
|
||||||
this.disabled = srcPath.getFileName().toString().endsWith(".disabled");
|
this.disabled = srcPath != null
|
||||||
|
&& srcPath.getFileName().toString().endsWith(".disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
public CleanupHandle getCleanupHandle() {
|
public CleanupHandle getCleanupHandle() {
|
||||||
|
@ -265,7 +265,7 @@ public class PluginGuiceEnvironment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onStopPlugin(Plugin plugin) {
|
public void onStopPlugin(Plugin plugin) {
|
||||||
for (StopPluginListener l : onStop) {
|
for (StopPluginListener l : onStop) {
|
||||||
l.onStopPlugin(plugin);
|
l.onStopPlugin(plugin);
|
||||||
}
|
}
|
||||||
|
@ -42,9 +42,9 @@ public class ServerPlugin extends Plugin {
|
|||||||
private final Path dataDir;
|
private final Path dataDir;
|
||||||
private final String pluginCanonicalWebUrl;
|
private final String pluginCanonicalWebUrl;
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
private Class<? extends Module> sysModule;
|
protected Class<? extends Module> sysModule;
|
||||||
private Class<? extends Module> sshModule;
|
protected Class<? extends Module> sshModule;
|
||||||
private Class<? extends Module> httpModule;
|
protected Class<? extends Module> httpModule;
|
||||||
|
|
||||||
private Injector sysInjector;
|
private Injector sysInjector;
|
||||||
private Injector sshInjector;
|
private Injector sshInjector;
|
||||||
@ -61,14 +61,18 @@ public class ServerPlugin extends Plugin {
|
|||||||
Path dataDir,
|
Path dataDir,
|
||||||
ClassLoader classLoader) throws InvalidPluginException {
|
ClassLoader classLoader) throws InvalidPluginException {
|
||||||
super(name, srcJar, pluginUser, snapshot,
|
super(name, srcJar, pluginUser, snapshot,
|
||||||
Plugin.getApiType(getPluginManifest(scanner)));
|
scanner == null
|
||||||
|
? ApiType.PLUGIN
|
||||||
|
: Plugin.getApiType(getPluginManifest(scanner)));
|
||||||
this.pluginCanonicalWebUrl = pluginCanonicalWebUrl;
|
this.pluginCanonicalWebUrl = pluginCanonicalWebUrl;
|
||||||
this.scanner = scanner;
|
this.scanner = scanner;
|
||||||
this.dataDir = dataDir;
|
this.dataDir = dataDir;
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
this.manifest = getPluginManifest(scanner);
|
this.manifest = scanner == null ? null : getPluginManifest(scanner);
|
||||||
|
if (manifest != null) {
|
||||||
loadGuiceModules(manifest, classLoader);
|
loadGuiceModules(manifest, classLoader);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void loadGuiceModules(Manifest manifest, ClassLoader classLoader) throws InvalidPluginException {
|
private void loadGuiceModules(Manifest manifest, ClassLoader classLoader) throws InvalidPluginException {
|
||||||
Attributes main = manifest.getMainAttributes();
|
Attributes main = manifest.getMainAttributes();
|
||||||
@ -92,7 +96,7 @@ public class ServerPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private static Class<? extends Module> load(String name, ClassLoader pluginLoader)
|
protected static Class<? extends Module> load(String name, ClassLoader pluginLoader)
|
||||||
throws ClassNotFoundException {
|
throws ClassNotFoundException {
|
||||||
if (Strings.isNullOrEmpty(name)) {
|
if (Strings.isNullOrEmpty(name)) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
// Copyright (C) 2016 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.plugins;
|
||||||
|
|
||||||
|
import com.google.gerrit.server.PluginUser;
|
||||||
|
|
||||||
|
public class TestServerPlugin extends ServerPlugin {
|
||||||
|
private final ClassLoader classLoader;
|
||||||
|
private String sysName;
|
||||||
|
private String httpName;
|
||||||
|
private String sshName;
|
||||||
|
|
||||||
|
public TestServerPlugin(String name, String pluginCanonicalWebUrl,
|
||||||
|
PluginUser user, ClassLoader classloader, String sysName,
|
||||||
|
String httpName, String sshName)
|
||||||
|
throws InvalidPluginException {
|
||||||
|
super(name, pluginCanonicalWebUrl, user, null, null, null, null, classloader);
|
||||||
|
this.classLoader = classloader;
|
||||||
|
this.sysName = sysName;
|
||||||
|
this.httpName = httpName;
|
||||||
|
this.sshName = sshName;
|
||||||
|
loadGuiceModules();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadGuiceModules() throws InvalidPluginException {
|
||||||
|
try {
|
||||||
|
this.sysModule = load(sysName, classLoader);
|
||||||
|
this.httpModule = load(httpName, classLoader);
|
||||||
|
this.sshModule = load(sshName, classLoader);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new InvalidPluginException("Unable to load plugin Guice Modules", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getVersion() {
|
||||||
|
return "1.0";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canReload() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
// Widen access modifier in derived class
|
||||||
|
public void start(PluginGuiceEnvironment env) throws Exception {
|
||||||
|
super.start(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
// Widen access modifier in derived class
|
||||||
|
public void stop(PluginGuiceEnvironment env) {
|
||||||
|
super.stop(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PluginContentScanner getContentScanner() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
Subproject commit aa4601c16e42c8ea0de251299b2a493a4068add0
|
Subproject commit d3c74f3692b0b10926cab300baaef905fcdf4136
|
Loading…
Reference in New Issue
Block a user