001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.test.blueprint;
018
019 import org.apache.camel.CamelContext;
020 import org.apache.camel.test.junit4.CamelTestSupport;
021 import org.junit.After;
022 import org.junit.Before;
023 import org.osgi.framework.BundleContext;
024 import org.osgi.service.blueprint.container.BlueprintContainer;
025
026 /**
027 * Base class for OSGi Blueprint unit tests with Camel.
028 */
029 public abstract class CamelBlueprintTestSupport extends CamelTestSupport {
030
031 private BundleContext bundleContext;
032
033 @Before
034 @Override
035 public void setUp() throws Exception {
036 String symbolicName = getClass().getSimpleName();
037 this.bundleContext = CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(),
038 true, getBundleFilter(), getBundleVersion());
039
040 super.setUp();
041
042 // must wait for blueprint container to be published then the namespace parser is complete and we are ready for testing
043 log.debug("Waiting for BlueprintContainer to be published with symbolicName: {}", symbolicName);
044 getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=" + symbolicName + ")");
045 }
046
047 @After
048 @Override
049 public void tearDown() throws Exception {
050 super.tearDown();
051 CamelBlueprintHelper.disposeBundleContext(bundleContext);
052 }
053
054 /**
055 * Return the system bundle context
056 */
057 protected BundleContext getBundleContext() {
058 return bundleContext;
059 }
060
061 /**
062 * Gets the bundle descriptor from the classpath.
063 * <p/>
064 * Return the location(s) of the bundle descriptors from the classpath.
065 * Separate multiple locations by comma, or return a single location.
066 * <p/>
067 * For example override this method and return <tt>OSGI-INF/blueprint/camel-context.xml</tt>
068 *
069 * @return the location of the bundle descriptor file.
070 */
071 protected String getBlueprintDescriptor() {
072 return null;
073 }
074
075 /**
076 * Gets filter expression of bundle descriptors. Modify this method if you wish to change
077 * default behavior.
078 *
079 * @return filter expression for OSGi bundles.
080 */
081 protected String getBundleFilter() {
082 return CamelBlueprintHelper.BUNDLE_FILTER;
083 }
084
085 /**
086 * Gets test bundle version.
087 * Modify this method if you wish to change default behavior.
088 *
089 * @return test bundle version
090 */
091 protected String getBundleVersion() {
092 return CamelBlueprintHelper.BUNDLE_VERSION;
093 }
094
095 @Override
096 protected CamelContext createCamelContext() throws Exception {
097 return CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class);
098 }
099
100 protected <T> T getOsgiService(Class<T> type) {
101 return CamelBlueprintHelper.getOsgiService(bundleContext, type);
102 }
103
104 protected <T> T getOsgiService(Class<T> type, long timeout) {
105 return CamelBlueprintHelper.getOsgiService(bundleContext, type, timeout);
106 }
107
108 protected <T> T getOsgiService(Class<T> type, String filter) {
109 return CamelBlueprintHelper.getOsgiService(bundleContext, type, filter);
110 }
111
112 protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
113 return CamelBlueprintHelper.getOsgiService(bundleContext, type, filter, timeout);
114 }
115
116 }
117
118