public void testLazyBeanServiceWithServiceFactoryBean() throws Exception {
final ServiceRegistration reg = new MockServiceRegistration();
final ServiceFactory[] factory = new ServiceFactory[1];
final Object actualService = new Object();
Object service = new ServiceFactory() {
public Object getService(Bundle arg0, ServiceRegistration arg1) {
return actualService;
}
public void ungetService(Bundle arg0, ServiceRegistration arg1, Object arg2) {
}
};
BundleContext ctx = new MockBundleContext() {
public ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties) {
assertTrue(service instanceof ServiceFactory);
factory[0] = (ServiceFactory) service;
return reg;
}
};
String beanName = "fooBar";
beanFactoryControl.expectAndReturn(beanFactory.isSingleton(beanName), true);
beanFactoryControl.expectAndReturn(beanFactory.containsBean(beanName), true);
beanFactoryControl.expectAndReturn(beanFactory.getBean(beanName), service);
beanFactoryControl.replay();
exporter.setBundleContext(ctx);
exporter.setBeanFactory(beanFactory);
exporter.setTargetBeanName(beanName);
exporter.setInterfaces(new Class[] { service.getClass() });
exporter.afterPropertiesSet();
exporter.registerService(new Class[] { actualService.getClass() }, new Properties());
assertSame(actualService, factory[0].getService(null, null));
beanFactoryControl.verify();