Search in sources :

Example 1 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class DefaultPluginManagerTest method shouldNotFindPluginIsOfGivenExtensionWhenReferenceNotFoundAndExtensionDoNotMatch.

@Test
public void shouldNotFindPluginIsOfGivenExtensionWhenReferenceNotFoundAndExtensionDoNotMatch() throws Exception {
    final String pluginThatDoesNotImplement = "plugin-that-does-not-implement";
    GoPluginIdentifier pluginIdentifier = new GoPluginIdentifier("another-extension-type", asList("1.0"));
    final GoPlugin goPlugin = mock(GoPlugin.class);
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    when(goPluginOSGiFramework.hasReferenceFor(GoPlugin.class, pluginThatDoesNotImplement)).thenReturn(true);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ActionWithReturn<GoPlugin, GoPluginApiResponse> action = (ActionWithReturn<GoPlugin, GoPluginApiResponse>) invocationOnMock.getArguments()[2];
            return action.execute(goPlugin, descriptor);
        }
    }).when(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginThatDoesNotImplement), any(ActionWithReturn.class));
    when(goPlugin.pluginIdentifier()).thenReturn(pluginIdentifier);
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, pluginWriter, pluginValidator, systemEnvironment, pluginsZipUpdater, pluginsListListener);
    assertFalse(pluginManager.isPluginOfType("extension-type", pluginThatDoesNotImplement));
    verify(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginThatDoesNotImplement), any(ActionWithReturn.class));
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.Test)

Example 2 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldPassInCorrectDescriptorToAction.

@Test
void shouldPassInCorrectDescriptorToAction() {
    final GoPluginDescriptor goPluginDescriptor = getPluginDescriptor(PLUGIN_ID, descriptorBundleDir);
    final GoPluginBundleDescriptor bundleDescriptor = new GoPluginBundleDescriptor(goPluginDescriptor);
    registry.loadPlugin(bundleDescriptor);
    Bundle bundle = pluginOSGiFramework.loadPlugin(bundleDescriptor);
    assertThat(bundle.getState()).isEqualTo(Bundle.ACTIVE);
    ActionWithReturn<GoPlugin, Object> action = (plugin, pluginDescriptor) -> {
        assertThat(pluginDescriptor).isEqualTo(goPluginDescriptor);
        plugin.pluginIdentifier();
        return null;
    };
    pluginOSGiFramework.doOn(GoPlugin.class, PLUGIN_ID, "notification", action);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ZipInputStream(java.util.zip.ZipInputStream) Constants(org.osgi.framework.Constants) ZipUtil(com.thoughtworks.go.util.ZipUtil) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) FileHelper(com.thoughtworks.go.plugin.FileHelper) FelixConstants(org.apache.felix.framework.util.FelixConstants) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) BundleClassLoader(org.apache.felix.framework.BundleWiringImpl.BundleClassLoader) ReflectionUtil(com.thoughtworks.go.util.ReflectionUtil) Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) File(java.io.File) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) DefaultPluginRegistry(com.thoughtworks.go.plugin.infra.plugininfo.DefaultPluginRegistry) Assertions.fail(org.assertj.core.api.Assertions.fail) TempDir(org.junit.jupiter.api.io.TempDir) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) Bundle(org.osgi.framework.Bundle) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Test(org.junit.jupiter.api.Test)

Example 3 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldLoadAValidGoPluginOSGiBundle.

@Test
void shouldLoadAValidGoPluginOSGiBundle() throws Exception {
    final GoPluginBundleDescriptor bundleDescriptor = new GoPluginBundleDescriptor(getPluginDescriptor(PLUGIN_ID, descriptorBundleDir));
    registry.loadPlugin(bundleDescriptor);
    Bundle bundle = pluginOSGiFramework.loadPlugin(bundleDescriptor);
    assertThat(bundle.getState()).isEqualTo(Bundle.ACTIVE);
    BundleContext context = bundle.getBundleContext();
    ServiceReference<?>[] allServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), null);
    assertThat(allServiceReferences.length).isEqualTo(1);
    try {
        GoPlugin service = (GoPlugin) context.getService(allServiceReferences[0]);
        service.pluginIdentifier();
        assertThat(getIntField(service, "loadCalled")).as("@Load should have been called").isEqualTo(1);
    } catch (Exception e) {
        fail(String.format("pluginIdentifier should have been called. Exception: %s", e.getMessage()));
    }
}
Also used : GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) Bundle(org.osgi.framework.Bundle) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.jupiter.api.Test)

Example 4 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldUnloadALoadedPlugin.

@Test
void shouldUnloadALoadedPlugin() throws Exception {
    GoPluginBundleDescriptor bundleDescriptor = new GoPluginBundleDescriptor(getPluginDescriptor(PLUGIN_ID, descriptorBundleDir));
    registry.loadPlugin(bundleDescriptor);
    Bundle bundle = pluginOSGiFramework.loadPlugin(bundleDescriptor);
    BundleContext context = bundle.getBundleContext();
    ServiceReference<?>[] allServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), null);
    assertThat(allServiceReferences.length).isEqualTo(1);
    GoPlugin service = (GoPlugin) context.getService(allServiceReferences[0]);
    assertThat(getIntField(service, "loadCalled")).as("@Load should have been called").isEqualTo(1);
    bundleDescriptor.setBundle(bundle);
    pluginOSGiFramework.unloadPlugin(bundleDescriptor);
    assertThat(bundle.getState()).isEqualTo(Bundle.UNINSTALLED);
    assertThat(getIntField(service, "unloadCalled")).as("@UnLoad should have been called").isEqualTo(1);
}
Also used : GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) Bundle(org.osgi.framework.Bundle) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.jupiter.api.Test)

Example 5 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldUseOldClassLoaderBehaviourWhenSystemPropertyIsSet.

@Test
void shouldUseOldClassLoaderBehaviourWhenSystemPropertyIsSet() {
    systemEnvironment.setProperty("gocd.plugins.classloader.old", "true");
    final GoPluginDescriptor goPluginDescriptor = getPluginDescriptor("plugin.to.test.classloader", pluginToTestClassloadPluginBundleDir);
    final GoPluginBundleDescriptor descriptor = new GoPluginBundleDescriptor(goPluginDescriptor);
    registry.loadPlugin(descriptor);
    Bundle bundle = pluginOSGiFramework.loadPlugin(descriptor);
    assertThat(bundle.getState()).isEqualTo(Bundle.ACTIVE);
    ActionWithReturn<GoPlugin, Object> action = (plugin, pluginDescriptor) -> {
        assertThat(pluginDescriptor).isEqualTo(goPluginDescriptor);
        assertThat(Thread.currentThread().getContextClassLoader().getClass().getCanonicalName()).isNotEqualTo(BundleClassLoader.class.getCanonicalName());
        return null;
    };
    pluginOSGiFramework.doOn(GoPlugin.class, "plugin.to.test.classloader", "notification", action);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ZipInputStream(java.util.zip.ZipInputStream) Constants(org.osgi.framework.Constants) ZipUtil(com.thoughtworks.go.util.ZipUtil) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) FileHelper(com.thoughtworks.go.plugin.FileHelper) FelixConstants(org.apache.felix.framework.util.FelixConstants) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) BundleClassLoader(org.apache.felix.framework.BundleWiringImpl.BundleClassLoader) ReflectionUtil(com.thoughtworks.go.util.ReflectionUtil) Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) File(java.io.File) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) DefaultPluginRegistry(com.thoughtworks.go.plugin.infra.plugininfo.DefaultPluginRegistry) Assertions.fail(org.assertj.core.api.Assertions.fail) TempDir(org.junit.jupiter.api.io.TempDir) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) Bundle(org.osgi.framework.Bundle) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

GoPlugin (com.thoughtworks.go.plugin.api.GoPlugin)15 Test (org.junit.jupiter.api.Test)12 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)9 GoPluginBundleDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor)8 IOException (java.io.IOException)8 Bundle (org.osgi.framework.Bundle)8 BundleContext (org.osgi.framework.BundleContext)8 ServiceReference (org.osgi.framework.ServiceReference)8 GoPluginIdentifier (com.thoughtworks.go.plugin.api.GoPluginIdentifier)5 FileHelper (com.thoughtworks.go.plugin.FileHelper)4 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)4 DefaultPluginRegistry (com.thoughtworks.go.plugin.infra.plugininfo.DefaultPluginRegistry)4 ReflectionUtil (com.thoughtworks.go.util.ReflectionUtil)4 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)4 ZipUtil (com.thoughtworks.go.util.ZipUtil)4 File (java.io.File)4 HashMap (java.util.HashMap)4 ZipInputStream (java.util.zip.ZipInputStream)4 FileUtils (org.apache.commons.io.FileUtils)4 BundleClassLoader (org.apache.felix.framework.BundleWiringImpl.BundleClassLoader)4