1 package com.atlassian.plugin.osgi.bridge;
2
3 import com.atlassian.plugin.event.PluginEventManager;
4 import com.atlassian.plugin.osgi.event.PluginServiceDependencyWaitStartingEvent;
5 import org.eclipse.gemini.blueprint.context.ConfigurableOsgiBundleApplicationContext;
6 import org.eclipse.gemini.blueprint.extender.event.BootstrappingDependencyEvent;
7 import org.eclipse.gemini.blueprint.service.importer.OsgiServiceDependency;
8 import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyWaitStartingEvent;
9 import org.eclipse.gemini.blueprint.service.importer.support.AbstractOsgiServiceImportFactoryBean;
10 import org.hamcrest.BaseMatcher;
11 import org.hamcrest.Description;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.osgi.framework.Bundle;
15 import org.osgi.framework.BundleContext;
16 import org.springframework.context.ApplicationContext;
17
18 import java.util.Dictionary;
19 import java.util.Hashtable;
20
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 import static org.mockito.hamcrest.MockitoHamcrest.argThat;
25
26 public class TestSpringContextEventBridge {
27
28 private Bundle bundle;
29 private PluginEventManager eventManager;
30 private SpringContextEventBridge bridge;
31
32 @Before
33 public void setUp() {
34 eventManager = mock(PluginEventManager.class);
35
36 bridge = new SpringContextEventBridge(eventManager);
37
38 Dictionary<String, String> headers = new Hashtable<>();
39 headers.put("Atlassian-Plugin-Key", "foo");
40
41 bundle = mock(Bundle.class);
42 when(bundle.getHeaders()).thenReturn(headers);
43 }
44
45 @Test
46 public void testWaitingEventWithApplicationContext() {
47 ConfigurableOsgiBundleApplicationContext source = mock(ConfigurableOsgiBundleApplicationContext.class);
48 when(source.getBundle()).thenReturn(bundle);
49 OsgiServiceDependencyWaitStartingEvent startingEvent = new OsgiServiceDependencyWaitStartingEvent(source, mock(OsgiServiceDependency.class), 1000);
50 BootstrappingDependencyEvent bootstrapEvent = new BootstrappingDependencyEvent(mock(ApplicationContext.class), bundle, startingEvent);
51 bridge.onOsgiApplicationEvent(bootstrapEvent);
52
53 verify(eventManager).broadcast(isPluginKey("foo"));
54 }
55
56 @Test
57 public void testWaitingEventWithServiceFactoryBean() {
58 AbstractOsgiServiceImportFactoryBean source = mock(AbstractOsgiServiceImportFactoryBean.class);
59 when(source.getBeanName()).thenReturn("bar");
60 BundleContext ctx = mock(BundleContext.class);
61 when(ctx.getBundle()).thenReturn(bundle);
62 when(source.getBundleContext()).thenReturn(ctx);
63 OsgiServiceDependencyWaitStartingEvent startingEvent = new OsgiServiceDependencyWaitStartingEvent(source, mock(OsgiServiceDependency.class), 1000);
64 BootstrappingDependencyEvent bootstrapEvent = new BootstrappingDependencyEvent(mock(ApplicationContext.class), bundle, startingEvent);
65 bridge.onOsgiApplicationEvent(bootstrapEvent);
66
67 verify(eventManager).broadcast(isPluginKey("foo"));
68 }
69
70 private Object isPluginKey(String key) {
71 return argThat(new PluginKeyMatcher(key));
72 }
73
74 private static class PluginKeyMatcher extends BaseMatcher<PluginServiceDependencyWaitStartingEvent> {
75 private String key;
76
77 PluginKeyMatcher(String key) {
78 this.key = key;
79 }
80
81 @Override
82 public void describeTo(Description description) {
83 description.appendText("plugin with key ");
84 }
85
86 public boolean matches(Object o) {
87 return key.equals(((PluginServiceDependencyWaitStartingEvent) o).getPluginKey());
88 }
89 }
90 }