import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
-import java.util.Collection;
import java.util.Dictionary;
-import java.util.Map.Entry;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
private BundleContext context;
@Mock
private ServiceRegistration<?> reg;
- @Mock
- private BlankTransactionServiceTracker blankTxTracker;
@Before
public void setUp() throws Exception {
}
}).when(bundle).loadClass(anyString());
doReturn("mockBundle").when(bundle).toString();
- doReturn("mockBundleContext").when(context).toString();
doReturn(context).when(bundle).getBundleContext();
- doReturn(100L).when(bundle).getBundleId();
doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
}
@Test
public void testRegisterFactory() throws Exception {
- Entry<ModuleFactory, Bundle> entry = ModuleFactoryBundleTracker.registerFactory(
- TestingFactory.class.getName(), bundle);
- assertEquals(TestingFactory.currentInstance, entry.getKey());
- assertEquals(bundle, entry.getValue());
+ ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
+ verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
}
@Test
fail("Cannot register without extend");
}
+ @Mock
+ private BlankTransactionServiceTracker blankTxTracker;
+
@Test
- public void testBundleAddAndRemove() throws Exception {
+ public void testAddingBundle() throws Exception {
final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
- tracker.addingBundle(bundle, null);
-
- Collection<Entry<ModuleFactory, BundleContext>> entries = tracker.getModuleFactoryEntries();
- assertNotNull(entries);
- assertEquals(1, entries.size());
- Entry<ModuleFactory, BundleContext> entry = entries.iterator().next();
- assertEquals(TestingFactory.currentInstance, entry.getKey());
- assertEquals(context, entry.getValue());
-
- doNothing().when(blankTxTracker).blankTransaction();;
-
- BundleEvent mockEvent = mock(BundleEvent.class);
- doReturn(BundleEvent.STOPPING).when(mockEvent).getType();
-
- tracker.removedBundle(bundle, mockEvent, bundle);
-
- entries = tracker.getModuleFactoryEntries();
- assertNotNull(entries);
- assertEquals(0, entries.size());
-
- verify(blankTxTracker).blankTransaction();
-
- reset(mockEvent);
- doReturn(BundleEvent.STOPPED).when(mockEvent).getType();
-
- tracker.addingBundle(bundle, mockEvent);
-
- assertEquals(0, tracker.getModuleFactoryEntries().size());
+ tracker.addingBundle(bundle, mock(BundleEvent.class));
+ verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
}
@Test