*
* @throws Exception
*/
public void testProxyDestruction() throws Exception {
MyService reference = ServiceReferer.serviceReference;
assertNotNull("reference not initialized", reference);
assertNotNull("no value specified in the reference", reference.stringValue());
Bundle simpleServiceBundle = OsgiBundleUtils.findBundleBySymbolicName(bundleContext,
"org.springframework.osgi.iandt.simpleservice");
assertNotNull("Cannot find the simple service bundle", simpleServiceBundle);
System.out.println("stopping service bundle");
simpleServiceBundle.stop();
while (simpleServiceBundle.getState() == Bundle.STOPPING) {
System.out.println("waiting for service bundle to stop...");
Thread.sleep(500);
}
System.out.println("service bundle stopped");
final Bundle proxyDestructionBundle = OsgiBundleUtils.findBundleBySymbolicName(bundleContext,
"org.springframework.osgi.iandt.proxy.destruction");
Thread t = new Thread() {
public void run() {
try {
// wait a bit so the proxy invocation executes
Thread.sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
try {
System.out.println("Stopping referring bundle...");
proxyDestructionBundle.stop();
}
catch (BundleException e) {
e.printStackTrace();
}
}
};
t.start();
// Service should be unavailable
try {
System.out.println("Invoking method on OSGi service proxy...");
reference.stringValue();
fail("ServiceProxyDestroyedException should have been thrown!");
}
catch (ServiceProxyDestroyedException e) {
// Expected
}