summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Liebig2008-03-05 07:53:10 (EST)
committerStefan Liebig2008-03-05 07:53:10 (EST)
commit4b57b4e691dc2145e66302ca3d0a3fb1590668f3 (patch)
tree53c17b740aa41eb6fc19d8f475f254d2a9d161cc
parent2cc383108acb9202b3ec29dce223f935a362591b (diff)
downloadorg.eclipse.riena-4b57b4e691dc2145e66302ca3d0a3fb1590668f3.zip
org.eclipse.riena-4b57b4e691dc2145e66302ca3d0a3fb1590668f3.tar.gz
org.eclipse.riena-4b57b4e691dc2145e66302ca3d0a3fb1590668f3.tar.bz2
added more helper methods
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/tests/RienaTestCase.java208
1 files changed, 192 insertions, 16 deletions
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/tests/RienaTestCase.java b/org.eclipse.riena.tests/src/org/eclipse/riena/tests/RienaTestCase.java
index 8b332e2..24ca9ce 100644
--- a/org.eclipse.riena.tests/src/org/eclipse/riena/tests/RienaTestCase.java
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/tests/RienaTestCase.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007 compeople AG and others.
+ * Copyright (c) 2007, 2008 compeople AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -10,20 +10,35 @@
*******************************************************************************/
package org.eclipse.riena.tests;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
import java.util.regex.Pattern;
import junit.framework.TestCase;
+import org.eclipse.core.internal.registry.ExtensionRegistry;
+import org.eclipse.core.runtime.IExtension;
+import org.eclipse.core.runtime.IExtensionRegistry;
+import org.eclipse.core.runtime.RegistryFactory;
+import org.eclipse.core.runtime.spi.RegistryContributor;
import org.eclipse.riena.internal.tests.Activator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
/**
+ * Base class for test cases.<br>
+ * It extends the {@link junit.framework.TestCase} with a few helpers.
+ *
* @author campo
*
*/
-public class RienaTestCase extends TestCase {
+public abstract class RienaTestCase extends TestCase {
+
+ // Keep track of services and and corresponding service references.
+ private Map<Object, ServiceReference> services = new HashMap<Object, ServiceReference>();
/**
*
@@ -39,28 +54,189 @@ public class RienaTestCase extends TestCase {
super(name);
}
- public void startBundles(String truePattern, String falsePattern) throws BundleException {
- if (truePattern == null) {
- throw new UnsupportedOperationException("truePattern must be set");
- }
- if (falsePattern == null) {
- falsePattern = "";
- }
- Pattern truePat = Pattern.compile(truePattern);
- Pattern falsePat = Pattern.compile(falsePattern);
- BundleContext context = Activator.getContext();
+ /*
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
- Bundle[] bundles = context.getBundles();
- for (Bundle bundle : bundles) {
- if (truePat.matcher(bundle.getSymbolicName()).matches() && !(falsePat.matcher(bundle.getSymbolicName()).matches())) {
+ services.clear();
+ }
+
+ /*
+ * @see junit.framework.TestCase#tearDown()
+ */
+ @Override
+ protected void tearDown() throws Exception {
+ for (ServiceReference reference : services.values())
+ Activator.getContext().ungetService(reference);
+
+ services.clear();
+
+ super.tearDown();
+ }
+
+ /**
+ * Add an extension/extension point defined within the ´plugin.xml´ given
+ * with the <code>pluginResource</code> to the extension registry.
+ *
+ * @param forLoad
+ * @param pluginResource
+ */
+ protected void addPluginXml(Class<?> forLoad, String pluginResource) {
+ IExtensionRegistry registry = RegistryFactory.getRegistry();
+ InputStream inputStream = forLoad.getResourceAsStream(pluginResource);
+ RegistryContributor contributor = new RegistryContributor("4711", "org.eclipse.riena.tests", null, null);
+ assertTrue(registry.addContribution(inputStream, contributor, false, null, null, ((ExtensionRegistry) registry)
+ .getTemporaryUserToken()));
+ }
+
+ /**
+ * Remove the given extension from the extension registry.
+ *
+ * @param extensionId
+ */
+ protected void removeExtension(String extensionId) {
+ IExtensionRegistry registry = RegistryFactory.getRegistry();
+ IExtension extension = registry.getExtension(extensionId);
+ assertNotNull(extension);
+ assertTrue(registry.removeExtension(extension, ((ExtensionRegistry) registry).getTemporaryUserToken()));
+ }
+
+ /**
+ * Get the service for the specified <code>serviceClass</code>.
+ *
+ * @param <T>
+ * @param serviceClass
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ protected <T> T getService(Class<T> serviceClass) {
+ ServiceReference reference = Activator.getContext().getServiceReference(serviceClass.getName());
+ if (reference == null)
+ return null;
+ Object service = Activator.getContext().getService(reference);
+ if (service == null)
+ return null;
+ services.put(service, reference);
+ return (T) service;
+ }
+
+ /**
+ * Unget the specified <code>service</code>.
+ *
+ * @param service
+ */
+ protected void ungetService(Object service) {
+ ServiceReference reference = services.get(service);
+ if (reference == null)
+ return;
+ Activator.getContext().ungetService(reference);
+ }
+
+ /**
+ * Starts the bundle with the given <code>bundleName</code>.
+ *
+ * @param bundleName
+ * @throws BundleException
+ */
+ protected void startBundle(String bundleName) throws BundleException {
+ startBundles(bundleName.replaceAll("\\.", "\\\\."), null); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ /**
+ * Starts all bundles that match the <code>includePattern</code> but not
+ * the <code>excludePattern</code>. The <code>excludePattern</code> may
+ * be <code>null</code>.
+ *
+ * @param includePattern
+ * @param excludePattern
+ * @throws BundleException
+ */
+ protected void startBundles(String includePattern, String excludePattern) throws BundleException {
+ doWithBundles(includePattern, excludePattern, new IClosure() {
+
+ public void execute(Bundle bundle) throws BundleException {
if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING /* STARTING==LAZY */) {
bundle.start();
} else {
if (bundle.getState() == Bundle.INSTALLED) {
- throw new RuntimeException("can't start required bundle because it is not RESOLVED but only INSTALLED : " + bundle.getSymbolicName());
+ throw new RuntimeException(
+ "can't start required bundle because it is not RESOLVED but only INSTALLED : " //$NON-NLS-1$
+ + bundle.getSymbolicName());
+ }
+ }
+ }
+ });
+ }
+
+ /**
+ * Stops the bundle with the given <code>bundleName</code>.
+ *
+ * @param bundleName
+ * @throws BundleException
+ */
+ protected void stopBundle(String bundleName) throws BundleException {
+ stopBundles(bundleName.replaceAll("\\.", "\\\\."), null); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ /**
+ * Stops all bundles that match the <code>includePattern</code> but not
+ * the <code>excludePattern</code>. The <code>excludePattern</code> may
+ * be <code>null</code>.
+ *
+ * @param includePattern
+ * @param excludePattern
+ * @throws BundleException
+ */
+ protected void stopBundles(String includePattern, String excludePattern) throws BundleException {
+ doWithBundles(includePattern, excludePattern, new IClosure() {
+
+ public void execute(Bundle bundle) throws BundleException {
+ if (bundle.getState() == Bundle.ACTIVE) {
+ bundle.stop();
+ } else {
+ if (bundle.getState() != Bundle.UNINSTALLED) {
+ throw new RuntimeException(
+ "can't stop required bundle because it is not ACTIVE and not UNINSTALLED : " //$NON-NLS-1$
+ + bundle.getSymbolicName());
}
}
}
+ });
+ }
+
+ /**
+ * IClosure with all bundles that match the <code>includePattern</code>
+ * but not the <code>excludePattern</code> what is specified within the
+ * <code>closure</code>. The <code>excludePattern</code> may be
+ * <code>null</code>.
+ *
+ * @param includePattern
+ * @param excludePattern
+ * @param closure
+ * @throws BundleException
+ */
+ protected void doWithBundles(String includePattern, String excludePattern, IClosure closure) throws BundleException {
+ if (includePattern == null) {
+ throw new UnsupportedOperationException("truePattern must be set"); //$NON-NLS-1$
}
+ if (excludePattern == null) {
+ excludePattern = ""; //$NON-NLS-1$
+ }
+ Pattern inlcude = Pattern.compile(includePattern);
+ Pattern exclude = Pattern.compile(excludePattern);
+ BundleContext context = Activator.getContext();
+
+ Bundle[] bundles = context.getBundles();
+ for (Bundle bundle : bundles)
+ if (inlcude.matcher(bundle.getSymbolicName()).matches()
+ && !(exclude.matcher(bundle.getSymbolicName()).matches()))
+ closure.execute(bundle);
+ }
+
+ protected interface IClosure {
+ void execute(Bundle bundle) throws BundleException;
}
}