@Test
public void testBundleChanged() throws Exception {

    //Set up mocks
    Set<Bundle> resultBundles;
    MockBundleContext mockBundleContext = new MockBundleContext();
    MockBundle startedBundle = new MockBundle();
    startedBundle.setState(Bundle.ACTIVE);
    startedBundle.setBundleId(1);
    startedBundle.setBundleContext(mockBundleContext);
    MockBundle stoppedBundle = new MockBundle();
    stoppedBundle.setState(Bundle.INSTALLED);
    stoppedBundle.setBundleId(2);
    stoppedBundle.setBundleContext(mockBundleContext);
    MockBundle flipBundle = new MockBundle();
    flipBundle.setState(Bundle.INSTALLED);
    flipBundle.setBundleId(3);
    flipBundle.setBundleContext(mockBundleContext);
    mockBundleContext.installBundle(startedBundle);
    mockBundleContext.installBundle(stoppedBundle);
    mockBundleContext.installBundle(flipBundle);
    //Default case
    mockBundleContext.setServiceCount(0);
    OSGIServiceLoader instance = new OSGIServiceLoader(mockBundleContext);
    resultBundles = instance.getResourceBundles();
    assertThat(resultBundles).hasSize(1);
    assertThat(mockBundleContext.getServiceCount()).isEqualTo(2);
    //After start
    mockBundleContext.setServiceCount(0);
    BundleEvent startedEvent = new BundleEvent(BundleEvent.STARTED, flipBundle);
    instance.bundleChanged(startedEvent);
    resultBundles = instance.getResourceBundles();
    assertThat(resultBundles).hasSize(2);
    assertThat(mockBundleContext.getServiceCount()).isEqualTo(2);
    //After stop
    mockBundleContext.setServiceCount(0);
    BundleEvent stoppedEvent = new BundleEvent(BundleEvent.STOPPED, flipBundle);
    instance.bundleChanged(stoppedEvent);
    resultBundles = instance.getResourceBundles();
    assertThat(resultBundles).hasSize(1);
    assertThat(mockBundleContext.getServiceCount()).isEqualTo(0);
}