All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.karaf.testing.Helper Maven / Gradle / Ivy

There is a newer version: 2.2.11
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.karaf.testing;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import org.ops4j.pax.exam.CoreOptions;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.options.JUnitBundlesOption;
import org.ops4j.pax.exam.options.MavenArtifactProvisionOption;
import org.ops4j.pax.exam.container.def.options.FeaturesScannerProvisionOption;
import org.ops4j.pax.exam.options.SystemPropertyOption;

import static org.ops4j.pax.exam.CoreOptions.bootClasspathLibrary;
import static org.ops4j.pax.exam.CoreOptions.bootDelegationPackages;
import static org.ops4j.pax.exam.CoreOptions.frameworkStartLevel;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
import static org.ops4j.pax.exam.OptionUtils.combine;
import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.scanFeatures;
import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.vmOption;

/**
 * Helper class for setting up a pax-exam test environment for karaf.
 *
 * A simple configuration for pax-exam can be create using the following
 * code:
 * 
 *   @Configuration
 *   public static Option[] configuration() throws Exception{
 *       return combine(
 *           // Default karaf environment
 *           Helper.getDefaultOptions(),
 *           // Test on both equinox and felix
 *           equinox(), felix()
 *       );
 *   }
 * 
* */ public final class Helper { public static final String INCLUDES_PROPERTY = "${includes}"; private Helper() { } /** * Create an provisioning option for the specified maven artifact * (groupId and artifactId), using the version found in the list * of dependencies of this maven project. * * @param groupId the groupId of the maven bundle * @param artifactId the artifactId of the maven bundle * @return the provisioning option for the given bundle */ public static MavenArtifactProvisionOption mavenBundle(String groupId, String artifactId) { return mavenBundle(groupId, artifactId, null, null, null); } public static MavenArtifactProvisionOption mavenBundle(String groupId, String artifactId, String version, String classifier, String type) { MavenArtifactProvisionOption m = CoreOptions.mavenBundle().groupId(groupId).artifactId(artifactId); if (version != null) { m.version(version); } else { try { m.versionAsInProject(); } catch (RuntimeException t) { //in eclipse, the dependencies.properties may not be avail since it's not //generated into a source directory (directly into classes). //thus, try and load it manually try { File file = new File("META-INF/maven/dependencies.properties"); if (!file.exists()) { file = new File("target/classes/META-INF/maven/dependencies.properties"); } if (file.exists()) { Properties dependencies = new Properties(); InputStream is = new FileInputStream(file); try { dependencies.load(is); } finally { is.close(); } version = dependencies.getProperty( groupId + "/" + artifactId + "/version" ); m.version(version); } else { throw t; } } catch (Throwable t2) { throw t; } } } if (classifier != null) { m.classifier(classifier); } if (type != null) { m.type(type); } return m; } /** * Return a map of system properties for karaf. * The default karaf home directory is "target/karaf.home". * * @return a list of system properties for karaf */ public static Properties getDefaultSystemOptions() { return getDefaultSystemOptions("target/karaf.home"); } /** * Return a map of system properties for karaf, * using the specified folder for the karaf home directory. * * @param karafHome the karaf home directory * @return a list of system properties for karaf */ public static Properties getDefaultSystemOptions(String karafHome) { Properties sysProps = new Properties(); sysProps.setProperty("karaf.name", "root"); sysProps.setProperty("karaf.home", karafHome); sysProps.setProperty("karaf.base", karafHome); sysProps.setProperty("karaf.startLocalConsole", "false"); sysProps.setProperty("karaf.startRemoteShell", "false"); sysProps.setProperty("org.osgi.framework.startlevel.beginning", "100"); return sysProps; } /** * Return an array of pax-exam options to correctly configure the osgi * framework for karaf. * * @param sysOptions test-specific system property options * @return default pax-exam options for karaf osgi framework */ public static Option[] getDefaultConfigOptions(SystemPropertyOption... sysOptions) { return getDefaultConfigOptions(getDefaultSystemOptions(), getResource("/org/apache/karaf/testing/config.properties"), sysOptions); } /** * Return an array of pax-exam options to configure the osgi * framework for karaf, given the system properties and the * location of the osgi framework properties file. * * @param sysProps karaf system properties * @param configProperties the URL to load the osgi framework properties from * @param sysOptions test-specific system property options * @return pax-exam options for karaf osgi framework */ public static Option[] getDefaultConfigOptions(Properties sysProps, URL configProperties, SystemPropertyOption... sysOptions) { // Load props Properties configProps = loadProperties(configProperties); // Set system props for (Enumeration e = sysProps.propertyNames(); e.hasMoreElements();) { String key = (String) e.nextElement(); configProps.setProperty(key, sysProps.getProperty(key)); } // Perform variable substitution for system properties. for (Enumeration e = configProps.propertyNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); configProps.setProperty(name, substVars(configProps.getProperty(name), name, null, configProps)); } // Transform system properties to VM options List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy