1 package com.atlassian.plugin.osgi.bridge.external;
2
3 import junit.framework.TestCase;
4 import org.mockito.ArgumentCaptor;
5 import static org.mockito.Matchers.anyObject;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.when;
9 import org.osgi.framework.BundleContext;
10 import org.osgi.framework.ServiceListener;
11 import org.osgi.framework.ServiceReference;
12 import org.osgi.framework.ServiceEvent;
13
14 import java.util.concurrent.Callable;
15
16
17 public class TestHostComponentFactoryBean extends TestCase
18 {
19 public void testGetService() throws Exception
20 {
21 BundleContext ctx = mock(BundleContext.class);
22 ServiceReference ref = mock(ServiceReference.class);
23 when(ctx.getServiceReferences(null, "(foo=bar)")).thenReturn(new ServiceReference[] {ref});
24 when(ctx.getService(ref)).thenReturn(new Callable()
25 {
26 public Object call() throws Exception
27 {
28 return "foo";
29 }
30 });
31
32 ArgumentCaptor<ServiceListener> serviceListener = new ArgumentCaptor<ServiceListener>();
33
34
35 HostComponentFactoryBean bean = new HostComponentFactoryBean();
36 bean.setBundleContext(ctx);
37 bean.setFilter("(foo=bar)");
38 bean.setInterfaces(new Class[] {Callable.class});
39 bean.afterPropertiesSet();
40
41 assertEquals("foo", ((Callable)bean.getObject()).call());
42
43 verify(ctx).addServiceListener(serviceListener.capture(), (String)anyObject());
44 ServiceReference updatedRef = mock(ServiceReference.class);
45 when(ctx.getService(updatedRef)).thenReturn(new Callable()
46 {
47 public Object call() throws Exception
48 {
49 return "boo";
50 }
51 });
52 serviceListener.getValue().serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, updatedRef));
53
54 assertEquals("boo", ((Callable)bean.getObject()).call());
55 }
56
57 }