aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application/BundleInstallerIntegrationTest.java
blob: 3c030fcc258470e2a44a80bb9e53cf3d90237654 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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.BundleException;

import java.util.LinkedList;
import java.util.List;

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

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

    @Test
    public void requireThatInstallWorks() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        List<Bundle> prev = new LinkedList<>(driver.osgiFramework().bundles());

        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        Bundle bundle = installer.installAndStart("cert-a.jar").get(0);
        assertNotNull(bundle);
        assertEquals("com.yahoo.vespa.jdisc_core.cert-a", bundle.getSymbolicName());

        boolean found = false;
        for (Bundle entry : driver.osgiFramework().bundles()) {
            assertNotNull(entry);
            if (prev.remove(entry)) {
                continue;
            }
            assertFalse(found);
            assertSame(bundle, entry);
            found = true;
        }
        assertTrue(prev.isEmpty());
        assertTrue(found);
        driver.close();
    }

    @Test
    public void requireThatInstallAllWorks() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        List<Bundle> bundles = installer.installAndStart(
                "cert-a.jar",
                "cert-b.jar");
        assertNotNull(bundles);
        assertEquals(2, bundles.size());
        driver.close();
    }

    @Test
    public void requireThatUninstallWorks() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        List<Bundle> prev = new LinkedList<>(driver.osgiFramework().bundles());

        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        Bundle bundle = installer.installAndStart("cert-a.jar").get(0);
        assertNotNull(bundle);
        installer.stopAndUninstall(bundle);

        for (Bundle entry : driver.osgiFramework().bundles()) {
            assertTrue(prev.remove(entry));
        }
        assertTrue(prev.isEmpty());
        driver.close();
    }

    @Test
    public void requireThatUninstallAllWorks() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        List<Bundle> prev = new LinkedList<>(driver.osgiFramework().bundles());

        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        List<Bundle> bundles = installer.installAndStart(
                "cert-a.jar",
                "cert-b.jar");
        assertNotNull(bundles);
        installer.stopAndUninstall(bundles);

        List<Bundle> next = new LinkedList<>(driver.osgiFramework().bundles());
        next.removeAll(prev);
        assertTrue(next.isEmpty());
        driver.close();
    }

    @Test
    public void requireThatUninstallUninstalledThrowsException() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        Bundle bundle = installer.installAndStart("cert-a.jar").get(0);
        assertNotNull(bundle);
        installer.stopAndUninstall(bundle);
        try {
            installer.stopAndUninstall(bundle);
            fail();
        } catch (BundleException e) {
            assertEquals("OSGi bundle com.yahoo.vespa.jdisc_core.cert-a not started.",
                         e.getMessage());
        }
        driver.close();
    }

    @Test
    public void requireThatInstallInstalledThrowsException() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        Bundle bundle = installer.installAndStart("cert-a.jar").get(0);
        assertNotNull(bundle);
        try {
            installer.installAndStart("cert-a.jar");
            fail();
        } catch (BundleException e) {
            assertEquals("OSGi bundle com.yahoo.vespa.jdisc_core.cert-a already started.",
                         e.getMessage());
        }
        driver.close();
    }

    @Test
    public void requireThatApplicationInstructionThrowsException() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        try {
            installer.installAndStart("app-a.jar");
            fail();
        } catch (BundleException e) {
            assertEquals("OSGi header 'X-JDisc-Application' not allowed for non-application bundle " +
                         "com.yahoo.vespa.jdisc_core.app-a.", e.getMessage());
        }
        driver.close();
    }

    @Test
    public void requireThatPrivilegedActivatorInstructionButNoRootPrivilegesKindOfWorksOnABestEffortBasis() throws Exception {
        TestDriver driver = TestDriver.newSimpleApplicationInstance();
        BundleInstaller installer = new BundleInstaller(driver.osgiFramework());
        installer.installAndStart("cert-j-priv.jar");
        driver.close();
    }

    @Test
    public void requireThatInstallExceptionContainsInstalledBundles() throws BundleException {
        OsgiFramework osgi = TestDriver.newOsgiFramework();
        osgi.start();
        BundleInstaller installer = new BundleInstaller(osgi);
        assertEquals(1, osgi.bundles().size());
        try {
            installer.installAndStart("cert-a.jar", "cert-tp.jar");
            fail();
        } catch (BundleException e) {
            assertTrue(e instanceof BundleInstallationException);
            List<Bundle> bundles = ((BundleInstallationException)e).installedBundles();
            assertEquals(2, bundles.size());
            assertEquals("com.yahoo.vespa.jdisc_core.cert-a", bundles.get(0).getSymbolicName());
            assertEquals("com.yahoo.vespa.jdisc_core.cert-tp", bundles.get(1).getSymbolicName());
        }
        assertEquals(3, osgi.bundles().size()); // did not clean up the installed bundles inside the catch-block
        osgi.stop();
    }

    @Test
    public void requireThatStartExceptionContainsInstalledBundles() throws BundleException {
        OsgiFramework osgi = TestDriver.newOsgiFramework();
        osgi.start();
        osgi.bundleContext().registerService(RuntimeException.class, new RuntimeException(), null);
        BundleInstaller installer = new BundleInstaller(osgi);
        assertEquals(1, osgi.bundles().size());
        try {
            installer.installAndStart("cert-a.jar", "cert-us.jar");
            fail();
        } catch (BundleException e) {
            assertTrue(e instanceof BundleInstallationException);
            List<Bundle> bundles = ((BundleInstallationException)e).installedBundles();
            assertEquals(3, bundles.size());
            assertEquals("com.yahoo.vespa.jdisc_core.cert-a", bundles.get(0).getSymbolicName());
            assertEquals("com.yahoo.vespa.jdisc_core.cert-us", bundles.get(1).getSymbolicName());
            assertEquals("com.yahoo.vespa.jdisc_core.cert-s-act", bundles.get(2).getSymbolicName());
        }
        assertEquals(4, osgi.bundles().size()); // did not clean up the installed bundles inside the catch-block
        osgi.stop();
    }
}