Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Watson2008-08-21 15:35:50 +0000
committerThomas Watson2008-08-21 15:35:50 +0000
commitc91c1e2b24e40f28ed163c1c298fbc2b6972a63a (patch)
treedf5d5a3427006190088da668e81256bf6a2a4817
parentd6ccb8d2af263c92604394537111b1bb77ad7374 (diff)
downloadrt.equinox.framework-c91c1e2b24e40f28ed163c1c298fbc2b6972a63a.tar.gz
rt.equinox.framework-c91c1e2b24e40f28ed163c1c298fbc2b6972a63a.tar.xz
rt.equinox.framework-c91c1e2b24e40f28ed163c1c298fbc2b6972a63a.zip
Bug 244629 implement the new ServiceException in the framework.
- Added testcases.
-rw-r--r--bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ServiceRegistryBundleTests.java225
1 files changed, 225 insertions, 0 deletions
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ServiceRegistryBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ServiceRegistryBundleTests.java
index e533b4dc4..db6eeb2bb 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ServiceRegistryBundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ServiceRegistryBundleTests.java
@@ -367,8 +367,233 @@ public class ServiceRegistryBundleTests extends AbstractBundleTests {
}
}
+ public void testServiceException01() {
+ // test a service factory which returns wrong object types
+ ServiceExceptionServiceFactory wrongObjectFactory = new ServiceExceptionServiceFactory("A String"); //$NON-NLS-1$
+ Hashtable props = new Hashtable();
+ props.put("name", "testServiceException01"); //$NON-NLS-1$ //$NON-NLS-2$
+ ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(Runnable.class.getName(), wrongObjectFactory, props);
+ ServiceExceptionFrameworkListener listener = new ServiceExceptionFrameworkListener(OSGiTestsActivator.getContext().getBundle(), null, ServiceException.FACTORY_ERROR);
+ OSGiTestsActivator.getContext().addFrameworkListener(listener);
+ try {
+ ServiceReference[] refs = null;
+ try {
+ refs = OSGiTestsActivator.getContext().getServiceReferences(Runnable.class.getName(), "(name=testServiceException01)"); //$NON-NLS-1$
+ } catch (InvalidSyntaxException e) {
+ fail("Unexpected syntax error", e); //$NON-NLS-1$
+ }
+ assertNotNull("service refs is null", refs); //$NON-NLS-1$
+ assertEquals("Wrong number of references", 1, refs.length); //$NON-NLS-1$
+ Runnable service = null;
+ try {
+ service = (Runnable) OSGiTestsActivator.getContext().getService(refs[0]);
+ } catch (ClassCastException e) {
+ fail("Unexpected cast exception", e); //$NON-NLS-1$
+ }
+ assertNull("service is not null", service); //$NON-NLS-1$
+ listener.waitForEvent("Failed to fire ServiceException"); //$NON-NLS-1$
+ OSGiTestsActivator.getContext().ungetService(refs[0]);
+ Error error = wrongObjectFactory.getUngetFailure();
+ if (error != null)
+ throw error;
+ } finally {
+ if (reg != null)
+ reg.unregister();
+ if (listener != null)
+ OSGiTestsActivator.getContext().removeFrameworkListener(listener);
+ }
+ }
+
+ public void testServiceException02() {
+ // test a service factory which returns null objects
+ ServiceExceptionServiceFactory nullObjectFactory = new ServiceExceptionServiceFactory(null);
+ Hashtable props = new Hashtable();
+ props.put("name", "testServiceException02"); //$NON-NLS-1$ //$NON-NLS-2$
+ ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(Runnable.class.getName(), nullObjectFactory, props);
+ ServiceExceptionFrameworkListener listener = new ServiceExceptionFrameworkListener(OSGiTestsActivator.getContext().getBundle(), null, ServiceException.FACTORY_ERROR);
+ OSGiTestsActivator.getContext().addFrameworkListener(listener);
+ try {
+ ServiceReference[] refs = null;
+ try {
+ refs = OSGiTestsActivator.getContext().getServiceReferences(Runnable.class.getName(), "(name=testServiceException02)"); //$NON-NLS-1$
+ } catch (InvalidSyntaxException e) {
+ fail("Unexpected syntax error", e); //$NON-NLS-1$
+ }
+ assertNotNull("service refs is null", refs); //$NON-NLS-1$
+ assertEquals("Wrong number of references", 1, refs.length); //$NON-NLS-1$
+ Runnable service = null;
+ try {
+ service = (Runnable) OSGiTestsActivator.getContext().getService(refs[0]);
+ } catch (ClassCastException e) {
+ fail("Unexpected cast exception", e); //$NON-NLS-1$
+ }
+ assertNull("service is not null", service); //$NON-NLS-1$
+ listener.waitForEvent("Failed to fire ServiceException"); //$NON-NLS-1$
+ OSGiTestsActivator.getContext().ungetService(refs[0]);
+ Error error = nullObjectFactory.getUngetFailure();
+ if (error != null)
+ throw error;
+ } finally {
+ if (reg != null)
+ reg.unregister();
+ if (listener != null)
+ OSGiTestsActivator.getContext().removeFrameworkListener(listener);
+ }
+ }
+
+ public void testServiceException03() {
+ // test a service factory which throws a RuntimeException
+ RuntimeException cause = new RuntimeException("testServiceException03"); //$NON-NLS-1$
+ ServiceExceptionServiceFactory runtimeExceptionFactory = new ServiceExceptionServiceFactory(cause);
+ Hashtable props = new Hashtable();
+ props.put("name", "testServiceException03"); //$NON-NLS-1$ //$NON-NLS-2$
+ ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(Runnable.class.getName(), runtimeExceptionFactory, props);
+ ServiceExceptionFrameworkListener listener = new ServiceExceptionFrameworkListener(OSGiTestsActivator.getContext().getBundle(), cause, ServiceException.FACTORY_EXCEPTION);
+ OSGiTestsActivator.getContext().addFrameworkListener(listener);
+ try {
+ ServiceReference[] refs = null;
+ try {
+ refs = OSGiTestsActivator.getContext().getServiceReferences(Runnable.class.getName(), "(name=testServiceException03)"); //$NON-NLS-1$
+ } catch (InvalidSyntaxException e) {
+ fail("Unexpected syntax error", e); //$NON-NLS-1$
+ }
+ assertNotNull("service refs is null", refs); //$NON-NLS-1$
+ assertEquals("Wrong number of references", 1, refs.length); //$NON-NLS-1$
+ Runnable service = null;
+ try {
+ service = (Runnable) OSGiTestsActivator.getContext().getService(refs[0]);
+ } catch (ClassCastException e) {
+ fail("Unexpected cast exception", e); //$NON-NLS-1$
+ }
+ assertNull("service is not null", service); //$NON-NLS-1$
+ listener.waitForEvent("Failed to fire ServiceException"); //$NON-NLS-1$
+ OSGiTestsActivator.getContext().ungetService(refs[0]);
+ Error error = runtimeExceptionFactory.getUngetFailure();
+ if (error != null)
+ throw error;
+ } finally {
+ if (reg != null)
+ reg.unregister();
+ if (listener != null)
+ OSGiTestsActivator.getContext().removeFrameworkListener(listener);
+ }
+ }
+
+ public void testServiceException04() {
+ // test a service factory which throws an Error
+ Error cause = new Error("testServiceException04"); //$NON-NLS-1$
+ ServiceExceptionServiceFactory errorFactory = new ServiceExceptionServiceFactory(cause);
+ Hashtable props = new Hashtable();
+ props.put("name", "testServiceException03"); //$NON-NLS-1$ //$NON-NLS-2$
+ ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(Runnable.class.getName(), errorFactory, props);
+ ServiceExceptionFrameworkListener listener = new ServiceExceptionFrameworkListener(OSGiTestsActivator.getContext().getBundle(), cause, ServiceException.FACTORY_EXCEPTION);
+ OSGiTestsActivator.getContext().addFrameworkListener(listener);
+ try {
+ ServiceReference[] refs = null;
+ try {
+ refs = OSGiTestsActivator.getContext().getServiceReferences(Runnable.class.getName(), "(name=testServiceException03)"); //$NON-NLS-1$
+ } catch (InvalidSyntaxException e) {
+ fail("Unexpected syntax error", e); //$NON-NLS-1$
+ }
+ assertNotNull("service refs is null", refs); //$NON-NLS-1$
+ assertEquals("Wrong number of references", 1, refs.length); //$NON-NLS-1$
+ Runnable service = null;
+ try {
+ service = (Runnable) OSGiTestsActivator.getContext().getService(refs[0]);
+ } catch (ClassCastException e) {
+ fail("Unexpected cast exception", e); //$NON-NLS-1$
+ }
+ assertNull("service is not null", service); //$NON-NLS-1$
+ listener.waitForEvent("Failed to fire ServiceException"); //$NON-NLS-1$
+ OSGiTestsActivator.getContext().ungetService(refs[0]);
+ Error error = errorFactory.getUngetFailure();
+ if (error != null)
+ throw error;
+ } finally {
+ if (reg != null)
+ reg.unregister();
+ if (listener != null)
+ OSGiTestsActivator.getContext().removeFrameworkListener(listener);
+ }
+ }
+
private void clearResults(boolean[] results) {
for (int i = 0; i < results.length; i++)
results[i] = false;
}
+
+ class ServiceExceptionServiceFactory implements ServiceFactory {
+ private final Object serviceOrThrowable;
+ private Error ungetFailure;
+
+ public ServiceExceptionServiceFactory(Object serviceOrThrowable) {
+ this.serviceOrThrowable = serviceOrThrowable;
+ }
+
+ public Object getService(Bundle bundle, ServiceRegistration registration) {
+ if (serviceOrThrowable instanceof RuntimeException)
+ throw (RuntimeException) serviceOrThrowable;
+ if (serviceOrThrowable instanceof Error)
+ throw (Error) serviceOrThrowable;
+ return serviceOrThrowable;
+ }
+
+ public synchronized void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
+ try {
+ if (serviceOrThrowable instanceof RuntimeException)
+ fail("Unexpected call to ungetService: " + serviceOrThrowable); //$NON-NLS-1$
+ if (serviceOrThrowable instanceof Error)
+ fail("Unexpected call to ungetService: " + serviceOrThrowable); //$NON-NLS-1$
+ } catch (Error error) {
+ ungetFailure = error;
+ }
+ }
+
+ public Error getUngetFailure() {
+ return ungetFailure;
+ }
+ }
+
+ class ServiceExceptionFrameworkListener implements FrameworkListener {
+ private final Bundle registrationBundle;
+ private final Throwable exception;
+ private final int exceptionType;
+ private boolean waitForEvent = true;
+
+ public ServiceExceptionFrameworkListener(Bundle registrationBundle, Throwable exception, int exceptionType) {
+ this.registrationBundle = registrationBundle;
+ this.exception = exception;
+ this.exceptionType = exceptionType;
+ }
+
+ public void frameworkEvent(FrameworkEvent event) {
+ if (event.getBundle() != registrationBundle)
+ return;
+ if (!(event.getThrowable() instanceof ServiceException))
+ return;
+ if (((ServiceException) event.getThrowable()).getCause() != exception)
+ return;
+ if (((ServiceException) event.getThrowable()).getType() != exceptionType)
+ return;
+ notifyWaiter();
+ }
+
+ private synchronized void notifyWaiter() {
+ waitForEvent = false;
+ notifyAll();
+ }
+
+ public synchronized void waitForEvent(String failMessage) {
+ if (waitForEvent) {
+ try {
+ wait(10000);
+ } catch (InterruptedException e) {
+ fail("unexpected interuption", e); //$NON-NLS-1$
+ }
+ // still waiting for event; we now fail
+ if (waitForEvent)
+ fail(failMessage);
+ }
+ }
+ }
}

Back to the top