Search in sources :

Example 6 with GoPlugin

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

the class DefaultPluginManagerTest method shouldReturnTrueIfPluginIsOfGivenExtensionWhenReferenceFoundAndExtensionMatch.

@Test
public void shouldReturnTrueIfPluginIsOfGivenExtensionWhenReferenceFoundAndExtensionMatch() throws Exception {
    String pluginId = "plugin-id";
    GoPluginIdentifier pluginIdentifier = new GoPluginIdentifier("sample-extension", asList("1.0"));
    final GoPlugin goPlugin = mock(GoPlugin.class);
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    when(goPluginOSGiFramework.hasReferenceFor(GoPlugin.class, pluginId)).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(pluginId), any(ActionWithReturn.class));
    when(goPlugin.pluginIdentifier()).thenReturn(pluginIdentifier);
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, pluginWriter, pluginValidator, systemEnvironment, pluginsZipUpdater, pluginsListListener);
    assertTrue(pluginManager.isPluginOfType("sample-extension", pluginId));
}
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 7 with GoPlugin

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldLoadAValidGoPluginOSGiBundleAndShouldBeDiscoverableThroughSymbolicNameFilter.

@Test
public void shouldLoadAValidGoPluginOSGiBundleAndShouldBeDiscoverableThroughSymbolicNameFilter() throws Exception {
    Bundle bundle = pluginOSGiFramework.loadPlugin(new GoPluginDescriptor(null, null, null, null, descriptorBundleDir, true));
    assertThat(bundle.getState(), is(Bundle.ACTIVE));
    String filterBySymbolicName = String.format("(%s=%s)", Constants.BUNDLE_SYMBOLICNAME, "testplugin.descriptorValidator");
    BundleContext context = bundle.getBundleContext();
    ServiceReference<?>[] allServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), filterBySymbolicName);
    assertThat(allServiceReferences.length, is(1));
    try {
        GoPlugin service = (GoPlugin) context.getService(allServiceReferences[0]);
        service.pluginIdentifier();
    } catch (Exception e) {
        fail(String.format("pluginIdentifier should have been called. Exception: %s", e.getMessage()));
    }
}
Also used : GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with GoPlugin

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldSetCurrentThreadContextClassLoaderToBundleClassLoaderToAvoidDependenciesFromApplicationClassloaderMessingAroundWithThePluginBehavior.

@Test
void shouldSetCurrentThreadContextClassLoaderToBundleClassLoaderToAvoidDependenciesFromApplicationClassloaderMessingAroundWithThePluginBehavior() {
    systemEnvironment.setProperty("gocd.plugins.classloader.old", "false");
    final GoPluginDescriptor goPluginDescriptor = getPluginDescriptor("plugin.to.test.classloader", pluginToTestClassloadPluginBundleDir);
    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);
        assertThat(Thread.currentThread().getContextClassLoader().getClass().getCanonicalName()).isEqualTo(BundleClassLoader.class.getCanonicalName());
        plugin.pluginIdentifier();
        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)

Example 9 with GoPlugin

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldLoadAValidGoPluginOSGiBundleAndShouldBeDiscoverableThroughPluginIDFilter.

@Test
void shouldLoadAValidGoPluginOSGiBundleAndShouldBeDiscoverableThroughPluginIDFilter() 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);
    String filterByPluginID = String.format("(%s=%s)", "PLUGIN_ID", "testplugin.descriptorValidator");
    BundleContext context = bundle.getBundleContext();
    ServiceReference<?>[] allServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), filterByPluginID);
    assertThat(allServiceReferences.length).isEqualTo(1);
    try {
        GoPlugin service = (GoPlugin) context.getService(allServiceReferences[0]);
        service.pluginIdentifier();
    } 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 10 with GoPlugin

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldHandleErrorGeneratedByAValidGoPluginOSGiBundleAtUsageTime.

@Test
void shouldHandleErrorGeneratedByAValidGoPluginOSGiBundleAtUsageTime() {
    final GoPluginBundleDescriptor bundleDescriptor = new GoPluginBundleDescriptor(getPluginDescriptor(PLUGIN_ID, errorGeneratingDescriptorBundleDir));
    registry.loadPlugin(bundleDescriptor);
    Bundle bundle = pluginOSGiFramework.loadPlugin(bundleDescriptor);
    assertThat(bundle.getState()).isEqualTo(Bundle.ACTIVE);
    assertThat(bundleDescriptor.isInvalid()).isFalse();
    ActionWithReturn<GoPlugin, Object> action = (goPlugin, goPluginDescriptor) -> {
        goPlugin.initializeGoApplicationAccessor(null);
        return null;
    };
    try {
        pluginOSGiFramework.doOn(GoPlugin.class, PLUGIN_ID, "extension-1", action);
        fail("Should Throw An Exception");
    } catch (Exception ex) {
        ex.printStackTrace();
        assertThat(ex.getCause() instanceof AbstractMethodError).isTrue();
    }
}
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) IOException(java.io.IOException) 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