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