aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application/BundleActivatorIntegrationTest.java
blob: beec3fe663e86df2875c3362fec46433910273a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import com.yahoo.jdisc.test.TestDriver;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;


/**
 * @author Simon Thoresen Hult
 */
public class BundleActivatorIntegrationTest {

    @Test
    public void requireThatBundleActivatorHasAccessToCurrentContainer() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        OsgiFramework osgi = driver.osgiFramework();
        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        Bundle bundle = installer.installAndStart("my-bundle-activator.jar").get(0);
        Class<?> serviceClass = bundle.loadClass("com.yahoo.jdisc.bundle.my_act.MyService");
        assertNotNull(serviceClass);
        BundleContext ctx = osgi.bundleContext();

        ServiceReference<?>[] serviceRefs = bundle.getRegisteredServices();
        assertEquals(1, serviceRefs.length);
        ServiceReference<?> serviceRef = serviceRefs[0];
        assertNotNull(serviceRef);

        Object service = ctx.getService(serviceRef);
        assertNotNull(service);
        assertTrue(serviceClass.isInstance(service));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatApplicationBundleActivatorHasAccessToCurrentContainer() throws Exception {
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-g-act.jar", false);
        OsgiFramework osgi = driver.osgiFramework();
        Bundle bundle = osgi.bundles().get(1);
        Class<?> serviceClass = bundle.loadClass("com.yahoo.jdisc.bundle.g_act.MyService");
        assertNotNull(serviceClass);
        BundleContext ctx = osgi.bundleContext();

        ServiceReference<?>[] serviceRefs = bundle.getRegisteredServices();
        assertEquals(1, serviceRefs.length);
        ServiceReference<?> serviceRef = serviceRefs[0];
        assertNotNull(serviceRef);

        Object service = ctx.getService(serviceRef);
        assertNotNull(service);
        assertTrue(serviceClass.isInstance(service));
        assertTrue(driver.close());
    }
}