Search in sources :

Example 36 with BundleEvent

use of org.osgi.framework.BundleEvent in project bnd by bndtools.

the class Activator method automatic.

void automatic(File reportDir) throws IOException {
    final List<Bundle> queue = new Vector<Bundle>();
    if (!reportDir.exists() && !reportDir.mkdirs()) {
        throw new IOException("Could not create directory " + reportDir);
    }
    trace("using %s", reportDir);
    trace("adding Bundle Listener for getting test bundle events");
    context.addBundleListener(new SynchronousBundleListener() {

        public void bundleChanged(BundleEvent event) {
            if (event.getType() == BundleEvent.STARTED) {
                checkBundle(queue, event.getBundle());
            }
        }
    });
    for (Bundle b : context.getBundles()) {
        checkBundle(queue, b);
    }
    trace("starting queue");
    int result = 0;
    outer: while (active) {
        Bundle bundle;
        synchronized (queue) {
            while (queue.isEmpty() && active) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    trace("tests bundle queue interrupted");
                    thread.interrupt();
                    break outer;
                }
            }
        }
        try {
            bundle = queue.remove(0);
            trace("received bundle to test: %s", bundle.getLocation());
            try (Writer report = getReportWriter(reportDir, bundle)) {
                trace("test will run");
                result += test(bundle, (String) bundle.getHeaders().get(aQute.bnd.osgi.Constants.TESTCASES), report);
                trace("test ran");
                if (queue.isEmpty() && !continuous) {
                    trace("queue " + queue);
                    System.exit(result);
                }
            }
        } catch (Exception e) {
            error("Not sure what happened anymore %s", e);
            System.exit(-2);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) IOException(java.io.IOException) Vector(java.util.Vector) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) IOException(java.io.IOException) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 37 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class ConfigurationMetadataUtil method registerAnnotationClasses.

/**
     * Search classpath for given class names to scan for and register all classes with @Configuration annotation.
     * @param bundleContext Bundle context
     * @param classNames Java class names
     */
public static void registerAnnotationClasses(BundleContext bundleContext, String... classNames) {
    Bundle bundle = new RegisterConfigurationMetadataBundle(bundleContext, Bundle.ACTIVE, classNames);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 38 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class BundleEventUtil method startDummyBundle.

/**
     * Simulate a bundle STARTED event with a given set of classes simulated to be found in the bundle's classpath. 
     */
public static Bundle startDummyBundle(BundleContext bundleContext, Class... classes) {
    DummyBundle bundle = new DummyBundle(bundleContext, classes);
    bundle.setState(Bundle.ACTIVE);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
    return bundle;
}
Also used : BundleEvent(org.osgi.framework.BundleEvent)

Example 39 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class MockBundleContextTest method testBundleListener.

@Test
public void testBundleListener() throws Exception {
    BundleListener bundleListener = mock(BundleListener.class);
    BundleEvent bundleEvent = mock(BundleEvent.class);
    bundleContext.addBundleListener(bundleListener);
    MockOsgi.sendBundleEvent(bundleContext, bundleEvent);
    verify(bundleListener).bundleChanged(bundleEvent);
    bundleContext.removeBundleListener(bundleListener);
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) Test(org.junit.Test)

Example 40 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class ModelAdapterFactoryUtil method addModelsForPackages.

/**
     * Search classpath for given java package names (and sub packages) to scan for and
     * register all classes with @Model annotation.
     * @param bundleContext Bundle context
     * @param packageNames Java package names
     */
public static void addModelsForPackages(BundleContext bundleContext, String... packageNames) {
    Bundle bundle = new RegisterModelsBundle(bundleContext, Bundle.ACTIVE, packageNames, null);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Aggregations

BundleEvent (org.osgi.framework.BundleEvent)53 Bundle (org.osgi.framework.Bundle)41 Test (org.junit.Test)12 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)12 BundleListener (org.osgi.framework.BundleListener)9 FrameworkEvent (org.osgi.framework.FrameworkEvent)7 StartLevel (org.osgi.service.startlevel.StartLevel)7 BundleException (org.osgi.framework.BundleException)6 File (java.io.File)5 BundleContext (org.osgi.framework.BundleContext)5 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Endpoint (javax.xml.ws.Endpoint)3 BundleTracker (org.osgi.util.tracker.BundleTracker)3 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2