diff --git a/activemq-karaf-itest/src/test/java/org/apache/activemq/karaf/itest/AbstractFeatureTest.java b/activemq-karaf-itest/src/test/java/org/apache/activemq/karaf/itest/AbstractFeatureTest.java index aad2825564..95b3348da4 100644 --- a/activemq-karaf-itest/src/test/java/org/apache/activemq/karaf/itest/AbstractFeatureTest.java +++ b/activemq-karaf-itest/src/test/java/org/apache/activemq/karaf/itest/AbstractFeatureTest.java @@ -16,6 +16,27 @@ */ package org.apache.activemq.karaf.itest; +import static org.junit.Assert.assertTrue; +import static org.ops4j.pax.exam.CoreOptions.composite; +import static org.ops4j.pax.exam.CoreOptions.maven; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.replaceConfigurationFile; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import java.security.PrivilegedAction; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; + +import javax.inject.Inject; import javax.security.auth.Subject; import org.apache.karaf.features.FeaturesService; @@ -25,11 +46,12 @@ import org.apache.karaf.shell.api.console.Session; import org.apache.karaf.shell.api.console.SessionFactory; import org.junit.After; import org.junit.Before; +import org.ops4j.pax.exam.MavenUtils; import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.TestProbeBuilder; import org.ops4j.pax.exam.ProbeBuilder; -import org.ops4j.pax.exam.karaf.options.KarafDistributionOption; +import org.ops4j.pax.exam.TestProbeBuilder; import org.ops4j.pax.exam.karaf.options.LogLevelOption; +import org.ops4j.pax.exam.options.MavenUrlReference; import org.ops4j.pax.exam.options.UrlReference; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -37,49 +59,21 @@ import org.osgi.framework.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.inject.Inject; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.security.PrivilegedAction; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.FutureTask; -import java.util.concurrent.TimeUnit; - -import static org.ops4j.pax.exam.CoreOptions.*; -import static org.junit.Assert.assertTrue; -import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*; - public abstract class AbstractFeatureTest { + private static final String KARAF_MAJOR_VERSION = "4.0.0"; public static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureTest.class); public static final long ASSERTION_TIMEOUT = 30000L; public static final long COMMAND_TIMEOUT = 30000L; public static final String USER = "karaf"; public static final String PASSWORD = "karaf"; - - static String basedir; - static { - try { - File location = new File(AbstractFeatureTest.class.getProtectionDomain().getCodeSource().getLocation().getFile()); - basedir = new File(location, "../..").getCanonicalPath(); - System.err.println("basedir=" + basedir); - } catch (IOException e) { - throw new RuntimeException(e); - } - } + public static final String RESOURCE_BASE = "src/test/resources/org/apache/activemq/karaf/itest/"; @Inject BundleContext bundleContext; - @Inject - FeaturesService featuresService; + @Inject + FeaturesService featuresService; @Before public void setUp() throws Exception { @@ -101,7 +95,7 @@ public abstract class AbstractFeatureTest { ExecutorService executor = Executors.newCachedThreadPool(); - protected String executeCommand(final String command, final Long timeout, final Boolean silent) { + private String executeCommand(final String command, final Long timeout, final Boolean silent) { String response; final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final PrintStream printStream = new PrintStream(byteArrayOutputStream); @@ -151,19 +145,17 @@ public abstract class AbstractFeatureTest { return executeCommand(command, COMMAND_TIMEOUT, false); } - /** - * Installs a feature and asserts that feature is properly installed. - * @param feature - * @throws Exception - */ - public void installAndAssertFeature(final String feature) throws Throwable { - executeCommand("feature:list -i"); - executeCommand("feature:install " + feature); - assertFeatureInstalled(feature); - } + /** + * Installs a feature and asserts that feature is properly installed. + * + * @param feature + * @throws Exception + */ + public void installAndAssertFeature(final String feature) throws Throwable { + featuresService.installFeature(feature); + } public void assertFeatureInstalled(final String feature) throws Throwable { - executeCommand("feature:list -i"); withinReason(new Callable() { @Override public Boolean call() throws Exception { @@ -172,95 +164,49 @@ public abstract class AbstractFeatureTest { } }); } - - public boolean verifyBundleInstalled(final String bundleName) throws Exception { - boolean found = false; + + public Bundle getBundle(String symName) { for (Bundle bundle: bundleContext.getBundles()) { - LOG.debug("Checking: " + bundle.getSymbolicName()); - if (bundle.getSymbolicName().contains(bundleName)) { - found = true; - break; + if (bundle.getSymbolicName().contains(symName)) { + return bundle; } } - return found; + throw new RuntimeException("Bundle " + symName + " not found"); } - public static String karafVersion() { - return System.getProperty("karafVersion", "unknown-need-env-var"); + public static Option configureBrokerStart(String xmlConfig) { + return composite( + replaceConfigurationFile("etc/activemq.xml", new File(RESOURCE_BASE + xmlConfig + ".xml")), + replaceConfigurationFile("etc/org.apache.activemq.server-default.cfg", + new File(RESOURCE_BASE + "org.apache.activemq.server-default.cfg")) + ); } - public static UrlReference getActiveMQKarafFeatureUrl() { - String type = "xml/features"; - UrlReference urlReference = mavenBundle().groupId("org.apache.activemq"). - artifactId("activemq-karaf").versionAsInProject().type(type); - System.err.println("FeatureURL: " + urlReference.getURL()); - return urlReference; + public static Option configureBrokerStart() { + return configureBrokerStart("activemq"); } - // for use from a probe - public String getCamelFeatureUrl() { - return getCamelFeatureUrl(System.getProperty("camel.version", "unknown")); + public static Option configure(String... features) { + MavenUrlReference karafUrl = maven().groupId("org.apache.karaf").artifactId("apache-karaf") + .type("tar.gz").versionAsInProject(); + UrlReference camelUrl = maven().groupId("org.apache.camel.karaf") + .artifactId("apache-camel").type("xml").classifier("features").versionAsInProject(); + UrlReference activeMQUrl = maven().groupId("org.apache.activemq"). + artifactId("activemq-karaf").versionAsInProject().type("xml").classifier("features").versionAsInProject(); + return composite( + karafDistributionConfiguration().frameworkUrl(karafUrl).karafVersion(KARAF_MAJOR_VERSION) + .name("Apache Karaf").unpackDirectory(new File("target/paxexam/unpack/")), + keepRuntimeFolder(), // + logLevel(LogLevelOption.LogLevel.WARN), // + editConfigurationFilePut("etc/config.properties", "karaf.startlevel.bundle", "50"), + // debugConfiguration("5005", true), + features(activeMQUrl, features), // + features(camelUrl) + ); } - public static String getCamelFeatureUrl(String ver) { - return "mvn:org.apache.camel.karaf/apache-camel/" - + ver - + "/xml/features"; - } - - public static UrlReference getKarafFeatureUrl() { - LOG.info("*** The karaf version is " + karafVersion() + " ***"); - - String type = "xml/features"; - return mavenBundle().groupId("org.apache.karaf.assemblies.features"). - artifactId("standard").version(karafVersion()).type(type); - } - - public static Option[] configureBrokerStart(Option[] existingOptions, String xmlConfig) { - existingOptions = append( - replaceConfigurationFile("etc/activemq.xml", new File(basedir + "/src/test/resources/org/apache/activemq/karaf/itest/" + xmlConfig + ".xml")), - existingOptions); - return append( - replaceConfigurationFile("etc/org.apache.activemq.server-default.cfg", new File(basedir + "/src/test/resources/org/apache/activemq/karaf/itest/org.apache.activemq.server-default.cfg")), - existingOptions); - } - - public static Option[] configureBrokerStart(Option[] existingOptions) { - final String xmlConfig = "activemq"; - return configureBrokerStart(existingOptions, xmlConfig); - } - - public static Option[] append(Option toAdd, Option[] existingOptions) { - ArrayList