1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.osgi;
20
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.lang.reflect.Method;
25
26 import javax.inject.Inject;
27
28 import org.junit.Test;
29 import org.ops4j.pax.exam.Option;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleContext;
32
33
34
35
36 abstract class AbstractOsgiITest {
37
38 private static final String EXPECTED_BUNDLE_NAME = "org.apache.commons.commons-compress";
39
40 @Inject
41 private BundleContext ctx;
42
43
44
45
46
47
48
49 public abstract Option[] config();
50
51 private Bundle loadBundle() {
52 for (final Bundle b : ctx.getBundles()) {
53 if (EXPECTED_BUNDLE_NAME.equals(b.getSymbolicName())) {
54 return b;
55 }
56 }
57 return null;
58 }
59
60 @Test
61 void testCanLoadBundle() {
62 assertNotNull("Expected to find bundle " + EXPECTED_BUNDLE_NAME, loadBundle());
63 }
64
65 @Test
66 void testProperlyDetectsRunningInsideOsgiEnv() throws Exception {
67 final String className = "org.apache.commons.compress.utils.OsgiUtils";
68 final Class<?> osgiUtils = loadBundle().loadClass(className);
69 assertNotNull("Can't load " + className + " via bundle", osgiUtils);
70 final Method method = osgiUtils.getMethod("isRunningInOsgiEnvironment");
71 assertNotNull("Can't access isRunningInOsgiEnvironment method", method);
72 assertTrue("Compress can't detect OSGi environment", (Boolean) method.invoke(null));
73 }
74 }