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
37
38 abstract class AbstractOsgiITest {
39 private static final String EXPECTED_BUNDLE_NAME = "org.apache.commons.commons-compress";
40
41 @Inject
42 private BundleContext ctx;
43
44
45
46
47
48 public abstract Option[] config();
49
50 private Bundle loadBundle() {
51 for (final Bundle b : ctx.getBundles()) {
52 if (EXPECTED_BUNDLE_NAME.equals(b.getSymbolicName())) {
53 return b;
54 }
55 }
56 return null;
57 }
58
59 @Test
60 public void testCanLoadBundle() {
61 assertNotNull("Expected to find bundle " + EXPECTED_BUNDLE_NAME, loadBundle());
62 }
63
64 @Test
65 public void testProperlyDetectsRunningInsideOsgiEnv() throws Exception {
66 final Class<?> osgiUtils = loadBundle().loadClass("org.apache.commons.compress.utils.OsgiUtils");
67 assertNotNull("Can load OsgiUtils via bundle", osgiUtils);
68
69 final Method method = osgiUtils.getMethod("isRunningInOsgiEnvironment");
70 assertNotNull("Can access isRunningInOsgiEnvironment method", method);
71
72 assertTrue("Compress detects OSGi environment", (Boolean) method.invoke(null));
73 }
74 }