BundleContext bundleContext = createMock(BundleContext.class);
Bundle sysBundle = createMock(Bundle.class);
BundleContext sysBundleContext = createMock(BundleContext.class);
Bundle bundle = createMock(Bundle.class);
Bundle bundle2 = createMock(Bundle.class);
FrameworkWiring wiring = createMock(FrameworkWiring.class);
//
// Create a new service, download a patch
//
expect(bundleContext.getBundle(0)).andReturn(sysBundle);
expect(sysBundle.getBundleContext()).andReturn(sysBundleContext);
expect(sysBundleContext.getProperty(Service.PATCH_LOCATION))
.andReturn(storage.toString()).anyTimes();
replay(sysBundleContext, sysBundle, bundleContext, bundle);
ServiceImpl service = new ServiceImpl(bundleContext);
try {
service.download(new URL("file:" + storage + "/temp/f00.zip"));
fail("Should have thrown exception on non existent patch file.");
} catch (Exception e) {
}
Iterable<Patch> patches = service.download(patch132.toURI().toURL());
assertNotNull(patches);
Iterator<Patch> it = patches.iterator();
assertTrue( it.hasNext() );
Patch patch = it.next();
assertNotNull( patch );
assertEquals("patch-1.3.2", patch.getId());
assertNotNull(patch.getBundles());
assertEquals(1, patch.getBundles().size());
Iterator<String> itb = patch.getBundles().iterator();
assertEquals("mvn:foo/my-bsn/1.3.2", itb.next());
assertNull(patch.getResult());
verify(sysBundleContext, sysBundle, bundleContext, bundle);
//
// Simulate the patch
//
reset(sysBundleContext, sysBundle, bundleContext, bundle);
expect(sysBundleContext.getBundles()).andReturn(new Bundle[] { bundle });
expect(bundle.getSymbolicName()).andReturn("my-bsn").anyTimes();
expect(bundle.getVersion()).andReturn(new Version("1.3.1")).anyTimes();
expect(bundle.getLocation()).andReturn("location");
expect(bundle.getBundleId()).andReturn(123L);
replay(sysBundleContext, sysBundle, bundleContext, bundle);
Result result = patch.simulate();
assertNotNull( result );
assertNull( patch.getResult() );
assertTrue(result.isSimulation());
verify(sysBundleContext, sysBundle, bundleContext, bundle);
//
// Recreate a new service and verify the downloaded patch is still available
//
reset(sysBundleContext, sysBundle, bundleContext, bundle);
expect(bundleContext.getBundle(0)).andReturn(sysBundle);
expect(sysBundle.getBundleContext()).andReturn(sysBundleContext);
expect(sysBundleContext.getProperty(Service.PATCH_LOCATION))
.andReturn(storage.toString()).anyTimes();
replay(sysBundleContext, sysBundle, bundleContext, bundle);
service = new ServiceImpl(bundleContext);
patches = service.getPatches();
assertNotNull(patches);
it = patches.iterator();
assertTrue( it.hasNext() );
patch = it.next();
assertNotNull( patch );
assertEquals("patch-1.3.2", patch.getId());
assertNotNull(patch.getBundles());
assertEquals(1, patch.getBundles().size());
itb = patch.getBundles().iterator();
assertEquals("mvn:foo/my-bsn/1.3.2", itb.next());
assertNull(patch.getResult());
verify(sysBundleContext, sysBundle, bundleContext, bundle);
//
// Install the patch
//
reset(sysBundleContext, sysBundle, bundleContext, bundle);
expect(sysBundleContext.getBundles()).andReturn(new Bundle[] { bundle });
expect(bundle.getSymbolicName()).andReturn("my-bsn").anyTimes();
expect(bundle.getVersion()).andReturn(new Version("1.3.1")).anyTimes();
expect(bundle.getLocation()).andReturn("location");
expect(bundle.getHeaders()).andReturn(new Hashtable()).anyTimes();
expect(bundle.getBundleId()).andReturn(123L);
bundle.update(EasyMock.<InputStream>anyObject());
expect(sysBundleContext.getBundles()).andReturn(new Bundle[] { bundle });
expect(bundle.getState()).andReturn(Bundle.INSTALLED).anyTimes();
expect(bundle.getRegisteredServices()).andReturn(null);
expect(sysBundleContext.getBundle(0)).andReturn(sysBundle);
expect(sysBundle.adapt(FrameworkWiring.class)).andReturn(wiring);
bundle.start();
wiring.refreshBundles(eq(asSet(bundle)), anyObject(FrameworkListener[].class));
expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
for (FrameworkListener l : (FrameworkListener[]) (EasyMock.getCurrentArguments()[1])) {
l.frameworkEvent(null);