aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/ApplicationLoaderIntegrationTest.java
blob: ea147f2bbc6a37ff8e25074ac34ea29747e70518 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import com.yahoo.jdisc.test.TestDriver;
import org.apache.felix.framework.util.FelixConstants;
import org.junit.Test;
import org.osgi.framework.Bundle;

import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

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


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

    @Test
    public void requireThatLifecycleWorks() throws Exception {
        MyModule module = new MyModule();
        ApplicationLoader loader = new ApplicationLoader(TestDriver.newOsgiFramework(),
                                                         Arrays.asList(module));
        loader.init("app-a.jar", false);

        assertFalse(module.init.await(100, TimeUnit.MILLISECONDS));
        assertFalse(module.start.await(100, TimeUnit.MILLISECONDS));
        loader.start();
        assertTrue(module.init.await(60, TimeUnit.SECONDS));
        assertTrue(module.start.await(60, TimeUnit.SECONDS));

        Iterator<Bundle> it = loader.osgiFramework().bundles().iterator();
        assertTrue(it.hasNext());
        Bundle bundle = it.next();
        assertNotNull(bundle);
        assertEquals(FelixConstants.SYSTEM_BUNDLE_SYMBOLICNAME,
                     bundle.getSymbolicName());
        assertTrue(it.hasNext());
        assertNotNull(bundle = it.next());
        assertEquals("com.yahoo.vespa.jdisc_core.app-a",
                     bundle.getSymbolicName());
        assertFalse(it.hasNext());

        assertFalse(module.stop.await(100, TimeUnit.MILLISECONDS));
        assertFalse(module.destroy.await(100, TimeUnit.MILLISECONDS));
        loader.stop();
        assertTrue(module.stop.await(60, TimeUnit.SECONDS));
        assertTrue(module.destroy.await(60, TimeUnit.SECONDS));

        loader.destroy();
    }

    @Test
    public void requireThatNoApplicationInstructionThrowsException() throws Exception {
        try {
            TestDriver.newApplicationBundleInstance("cert-a.jar", false);
            fail();
        } catch (IllegalArgumentException e) {

        }
    }

    @Test
    public void requireThatMultipleApplicationInstructionsThrowException() throws Exception {
        try {
            TestDriver.newApplicationBundleInstance("app-f-more.jar", false);
            fail();
        } catch (IllegalArgumentException e) {

        }
    }

    @Test
    public void requireThatUnprivilegedBundleCanBeLoadedUnprivileged() throws Exception {
        MyModule module = new MyModule();
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-a.jar", false, module);
        assertTrue(module.init.await(60, TimeUnit.SECONDS));
        assertEquals("com.yahoo.jdisc.bundle.ApplicationA", driver.application().getClass().getName());
        driver.close();
    }

    @Test
    public void requireThatUnprivilegedBundleCanBeLoadedPrivileged() throws Exception {
        MyModule module = new MyModule();
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-a.jar", true, module);
        assertTrue(module.init.await(60, TimeUnit.SECONDS));
        assertEquals("com.yahoo.jdisc.bundle.ApplicationA", driver.application().getClass().getName());
        driver.close();
    }

    @Test
    public void requireThatPrivilegedBundleCanBeLoadedUnprivilegedOnABestEffortBasis() throws Exception {
        MyModule module = new MyModule();
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-b-priv.jar",
                                                                    false, module);
        assertTrue(module.init.await(60, TimeUnit.SECONDS));
        assertEquals("com.yahoo.jdisc.bundle.ApplicationB", driver.application().getClass().getName());
        driver.close();
    }

    @Test
    public void requireThatPrivilegedBundleCanBeLoadedPrivileged() throws Exception {
        MyModule module = new MyModule();
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-b-priv.jar",
                                                                    true, module);
        assertTrue(module.init.await(60, TimeUnit.SECONDS));
        assertEquals("com.yahoo.jdisc.bundle.ApplicationB", driver.application().getClass().getName());
        driver.close();
    }

    @Test
    public void requireThatInstallBundleInstructionWorks() throws Exception {
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-ca.jar", true);
        assertEquals("com.yahoo.jdisc.bundle.ApplicationC", driver.application().getClass().getName());
        driver.close();
    }

    @Test
    public void requireThatInstallBundleInstructionDoesNotIgnorePrivilegedActivatorOfDependencies() throws Exception {
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-dj.jar", true);
        driver.close();
    }

    @Test
    public void requireThatInstallBundleInstructionPropagatesPrivileges() throws Exception {
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-ej-priv.jar", true);
        assertEquals("com.yahoo.jdisc.bundle.ApplicationE", driver.application().getClass().getName());
        driver.close();
    }

    private static class MyModule extends AbstractModule {

        final CountDownLatch init = new CountDownLatch(1);
        final CountDownLatch start = new CountDownLatch(1);
        final CountDownLatch stop = new CountDownLatch(1);
        final CountDownLatch destroy = new CountDownLatch(1);

        @Override
        protected void configure() {
            bind(CountDownLatch.class).annotatedWith(Names.named("Init")).toInstance(init);
            bind(CountDownLatch.class).annotatedWith(Names.named("Start")).toInstance(start);
            bind(CountDownLatch.class).annotatedWith(Names.named("Stop")).toInstance(stop);
            bind(CountDownLatch.class).annotatedWith(Names.named("Destroy")).toInstance(destroy);
        }
    }
}