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