View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Tests if the library can be loaded with an OSGi environment provided by {@link #config()}.
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       * @return the OSGi configuration to use for the test
46       * @implNote Concrete implementation needs the @Configuration annotation
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  }