Search in sources :

Example 1 with PluginDescriptorAware

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldLoadAValidGoPluginOSGiBundle.

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

Example 2 with PluginDescriptorAware

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldUnloadALoadedPlugin.

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

Example 3 with PluginDescriptorAware

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldPassInCorrectDescriptorToAction.

@Test
public void shouldPassInCorrectDescriptorToAction() throws Exception {
    final GoPluginDescriptor descriptor = new GoPluginDescriptor("testplugin.descriptorValidator", null, null, null, descriptorBundleDir, true);
    Bundle bundle = pluginOSGiFramework.loadPlugin(descriptor);
    registry.loadPlugin(descriptor);
    assertThat(bundle.getState(), is(Bundle.ACTIVE));
    ActionWithReturn<PluginDescriptorAware, Object> action = new ActionWithReturn<PluginDescriptorAware, Object>() {

        @Override
        public Object execute(PluginDescriptorAware descriptorAware, GoPluginDescriptor pluginDescriptor) {
            assertThat(pluginDescriptor, is(descriptor));
            descriptorAware.setPluginDescriptor(pluginDescriptor);
            return null;
        }
    };
    pluginOSGiFramework.doOn(PluginDescriptorAware.class, "testplugin.descriptorValidator", action);
}
Also used : PluginDescriptorAware(com.thoughtworks.go.plugin.api.info.PluginDescriptorAware) Bundle(org.osgi.framework.Bundle) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Test(org.junit.Test)

Example 4 with PluginDescriptorAware

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

the class DefaultPluginJarChangeListenerTest method pluginDescriptorAwareCallbackErrorShouldNotBeFatal.

@Test
public void pluginDescriptorAwareCallbackErrorShouldNotBeFatal() throws Exception {
    String pluginJarFileName = "descriptor-aware-test-plugin.jar";
    File pluginJarFile = new File(PLUGIN_DIR, pluginJarFileName);
    copyPluginToTheDirectory(PLUGIN_DIR, pluginJarFileName);
    GoPluginDescriptor descriptor = new GoPluginDescriptor("some.old.id", "1.0", new GoPluginDescriptor.About(null, null, null, null, null, null), null, new File(pluginJarFileName), false);
    when(systemEnvironment.getOperatingSystemFamilyName()).thenReturn("Windows");
    when(goPluginDescriptorBuilder.build(pluginJarFile, true)).thenReturn(descriptor);
    when(osgiFramework.hasReferenceFor(PluginDescriptorAware.class, descriptor.id())).thenReturn(true);
    doThrow(new RuntimeException("Exception in plugin descriptor")).when(osgiFramework).doOnAllForPlugin(eq(PluginDescriptorAware.class), eq(descriptor.id()), Matchers.<Action<PluginDescriptorAware>>anyObject());
    listener = new DefaultPluginJarChangeListener(registry, osgiManifestGenerator, osgiFramework, goPluginDescriptorBuilder, systemEnvironment);
    listener.pluginJarAdded(new PluginFileDetails(pluginJarFile, true));
    verify(registry, times(1)).loadPlugin(descriptor);
    verify(osgiFramework, times(1)).loadPlugin(descriptor);
    verify(osgiFramework, times(1)).hasReferenceFor(PluginDescriptorAware.class, descriptor.id());
    verify(osgiFramework, times(1)).doOnAllWithExceptionHandlingForPlugin(eq(PluginDescriptorAware.class), eq(descriptor.id()), Matchers.<Action<PluginDescriptorAware>>anyObject(), Matchers.<ExceptionHandler<PluginDescriptorAware>>anyObject());
}
Also used : PluginDescriptorAware(com.thoughtworks.go.plugin.api.info.PluginDescriptorAware) PluginFileDetails(com.thoughtworks.go.plugin.infra.monitor.PluginFileDetails) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) File(java.io.File) Test(org.junit.Test)

Example 5 with PluginDescriptorAware

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

the class DefaultPluginJarChangeListenerTest method shouldNotThrowExceptionIfThePluginImplementsDescriptorAwareIsNotAvailable.

@Test
public void shouldNotThrowExceptionIfThePluginImplementsDescriptorAwareIsNotAvailable() throws Exception {
    String pluginJarFileName = "descriptor-aware-test-plugin.jar";
    File pluginJarFile = new File(PLUGIN_DIR, pluginJarFileName);
    copyPluginToTheDirectory(PLUGIN_DIR, pluginJarFileName);
    GoPluginDescriptor descriptor = new GoPluginDescriptor("some.old.id", "1.0", new GoPluginDescriptor.About(null, null, null, null, null, null), null, new File(pluginJarFileName), false);
    when(systemEnvironment.getOperatingSystemFamilyName()).thenReturn("Windows");
    when(goPluginDescriptorBuilder.build(pluginJarFile, true)).thenReturn(descriptor);
    when(osgiFramework.hasReferenceFor(PluginDescriptorAware.class, descriptor.id())).thenReturn(false);
    doThrow(new GoPluginFrameworkException("Failed to find service reference")).when(osgiFramework).doOnAllForPlugin(eq(PluginDescriptorAware.class), eq(descriptor.id()), Matchers.<Action<PluginDescriptorAware>>anyObject());
    listener = new DefaultPluginJarChangeListener(registry, osgiManifestGenerator, osgiFramework, goPluginDescriptorBuilder, systemEnvironment);
    listener.pluginJarAdded(new PluginFileDetails(pluginJarFile, true));
    verify(registry, times(1)).loadPlugin(descriptor);
    verify(osgiFramework, times(1)).loadPlugin(descriptor);
    verify(osgiFramework, times(1)).hasReferenceFor(PluginDescriptorAware.class, descriptor.id());
    verify(osgiFramework, never()).doOnAllForPlugin(eq(PluginDescriptorAware.class), eq(descriptor.id()), Matchers.<Action<PluginDescriptorAware>>anyObject());
}
Also used : PluginDescriptorAware(com.thoughtworks.go.plugin.api.info.PluginDescriptorAware) PluginFileDetails(com.thoughtworks.go.plugin.infra.monitor.PluginFileDetails) GoPluginFrameworkException(com.thoughtworks.go.plugin.infra.GoPluginFrameworkException) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) File(java.io.File) Test(org.junit.Test)

Aggregations

PluginDescriptorAware (com.thoughtworks.go.plugin.api.info.PluginDescriptorAware)9 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)9 Test (org.junit.Test)9 Bundle (org.osgi.framework.Bundle)5 PluginFileDetails (com.thoughtworks.go.plugin.infra.monitor.PluginFileDetails)4 File (java.io.File)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 BundleContext (org.osgi.framework.BundleContext)3 ServiceReference (org.osgi.framework.ServiceReference)3 GoPluginFrameworkException (com.thoughtworks.go.plugin.infra.GoPluginFrameworkException)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2