Search in sources :

Example 6 with PluginDescriptorAware

use of com.thoughtworks.go.plugin.api.info.PluginDescriptorAware 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(PluginDescriptorAware.class.getCanonicalName(), filterBySymbolicName);
    assertThat(allServiceReferences.length, is(1));
    try {
        PluginDescriptorAware service = (PluginDescriptorAware) context.getService(allServiceReferences[0]);
        service.setPluginDescriptor(getDescriptor());
    } 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 7 with PluginDescriptorAware

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

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldHandleErrorGeneratedByAValidGoPluginOSGiBundleAtUsageTime.

@Test
public void shouldHandleErrorGeneratedByAValidGoPluginOSGiBundleAtUsageTime() throws Exception {
    Bundle bundle = pluginOSGiFramework.loadPlugin(new GoPluginDescriptor(null, null, null, null, errorGeneratingDescriptorBundleDir, true));
    assertThat(bundle.getState(), is(Bundle.ACTIVE));
    ActionWithReturn<PluginDescriptorAware, Object> action = new ActionWithReturn<PluginDescriptorAware, Object>() {

        @Override
        public Object execute(PluginDescriptorAware descriptorAware, GoPluginDescriptor goPluginDescriptor) {
            descriptorAware.setPluginDescriptor(null);
            return null;
        }
    };
    try {
        pluginOSGiFramework.doOn(PluginDescriptorAware.class, "testplugin.descriptorValidator", action);
        fail("Should Throw An Exception");
    } catch (Exception ex) {
        assertThat(ex.getCause() instanceof AbstractMethodError, is(true));
    }
}
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) Test(org.junit.Test)

Example 8 with PluginDescriptorAware

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

the class DefaultPluginJarChangeListenerTest method shouldNotProvidePluginDescriptorIfThePluginIsInvalidatedDuringLoad.

@Test
public void shouldNotProvidePluginDescriptorIfThePluginIsInvalidatedDuringLoad() throws Exception {
    String pluginJarFileName = "descriptor-aware-test-plugin.jar";
    File pluginJarFile = new File(PLUGIN_DIR, pluginJarFileName);
    copyPluginToTheDirectory(PLUGIN_DIR, pluginJarFileName);
    final 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);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            descriptor.markAsInvalid(Arrays.asList("Marking invalid for test"), new Exception("dummy test exception"));
            return null;
        }
    }).when(osgiFramework).loadPlugin(descriptor);
    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, never()).hasReferenceFor(PluginDescriptorAware.class, descriptor.id());
    verify(osgiFramework, never()).doOnAllForPlugin(eq(PluginDescriptorAware.class), eq(descriptor.id()), Matchers.<Action<PluginDescriptorAware>>anyObject());
}
Also used : URISyntaxException(java.net.URISyntaxException) GoPluginFrameworkException(com.thoughtworks.go.plugin.infra.GoPluginFrameworkException) IOException(java.io.IOException) PluginDescriptorAware(com.thoughtworks.go.plugin.api.info.PluginDescriptorAware) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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 9 with PluginDescriptorAware

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

the class DefaultPluginJarChangeListenerTest method shouldLoadAPluginAndProvidePluginDescriptorIfThePluginImplementsDescriptorAware.

@Test
public void shouldLoadAPluginAndProvidePluginDescriptorIfThePluginImplementsDescriptorAware() 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);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            return null;
        }
    }).when(osgiFramework).doOnAllWithExceptionHandlingForPlugin(eq(PluginDescriptorAware.class), eq(descriptor.id()), Matchers.<Action<PluginDescriptorAware>>anyObject(), Matchers.<ExceptionHandler<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) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PluginFileDetails(com.thoughtworks.go.plugin.infra.monitor.PluginFileDetails) 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