- @Test(expected = NullPointerException.class)
- public void testNullBundleName() throws Exception {
- doReturn(null).when(s1).getBundle();
- resolver.getAllFactories();
+ @Test
+ public void testBundleContextInitiallyNull() throws Exception {
+ final AtomicReference<BundleContext> bundleContext = new AtomicReference<>();
+ Answer<BundleContext> answer = new Answer<BundleContext>() {
+ @Override
+ public BundleContext answer(InvocationOnMock invocation) throws Throwable {
+ return bundleContext.get();
+ }
+ };
+
+ doAnswer(answer).when(mockBundle1).getBundleContext();
+ doReturn(Arrays.asList(new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext1))).
+ when(mockBundleTracker).getModuleFactoryEntries();
+
+ final AtomicReference<Map<String, Map.Entry<ModuleFactory, BundleContext>>> allFactories = new AtomicReference<>();
+ final AtomicReference<Exception> caughtEx = new AtomicReference<>();
+ final CountDownLatch doneLatch = new CountDownLatch(1);
+ new Thread() {
+ @Override
+ public void run() {
+ try {
+ allFactories.set(resolver.getAllFactories());
+ } catch (Exception e) {
+ caughtEx.set(e);
+ } finally {
+ doneLatch.countDown();
+ }
+ }
+ }.start();
+
+ Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
+ bundleContext.set(mockBundleContext1);
+
+ assertEquals(true, doneLatch.await(5, TimeUnit.SECONDS));
+ if(caughtEx.get() != null) {
+ throw caughtEx.get();
+ }
+
+ assertEquals(1, allFactories.get().size());
+ assertTrue(allFactories.get().containsKey(f1.getImplementationName()));