add some commamd assertions to broker feature test

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1441209 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2013-01-31 21:44:45 +00:00
parent 7f022c9379
commit 75d4b7643f
2 changed files with 106 additions and 2 deletions

View File

@ -16,20 +16,33 @@
*/
package org.apache.activemq.karaf.itest;
import org.apache.felix.service.command.CommandProcessor;
import org.apache.felix.service.command.CommandSession;
import org.junit.After;
import org.junit.Before;
import org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.TestProbeBuilder;
import org.ops4j.pax.exam.junit.ProbeBuilder;
import org.ops4j.pax.exam.options.UrlReference;
import org.osgi.framework.BundleContext;
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.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.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.replaceConfigurationFile;
@ -38,6 +51,9 @@ import static org.ops4j.pax.exam.CoreOptions.*;
public abstract class AbstractFeatureTest {
private static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureTest.class);
private static final long ASSERTION_TIMEOUT = 20000L;
private static final long COMMAND_TIMEOUT = 10000L;
static String basedir;
static {
try {
@ -59,6 +75,57 @@ public abstract class AbstractFeatureTest {
public void tearDown() throws Exception {
}
@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE, "*,org.ops4j.pax.exam.options.*,org.apache.felix.service.*;status=provisional");
return probe;
}
@Inject
CommandProcessor commandProcessor;
ExecutorService executor = Executors.newCachedThreadPool();
protected String executeCommand(final String command, final Long timeout, final Boolean silent) {
String response;
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream);
final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, printStream);
commandSession.put("APPLICATION", System.getProperty("karaf.name", "root"));
commandSession.put("USER", "karaf");
FutureTask<String> commandFuture = new FutureTask<String>(
new Callable<String>() {
public String call() {
try {
if (!silent) {
System.out.println(command);
System.out.flush();
}
commandSession.execute(command);
} catch (Exception e) {
e.printStackTrace(System.err);
}
printStream.flush();
return byteArrayOutputStream.toString();
}
});
try {
executor.submit(commandFuture);
response = commandFuture.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace(System.err);
response = "SHELL COMMAND TIMED OUT: ";
}
return response;
}
protected String executeCommand(final String command) {
return executeCommand(command, COMMAND_TIMEOUT, false);
}
// protected void testComponent(String component) throws Exception {
// long max = System.currentTimeMillis() + 10000;
// while (true) {
@ -172,9 +239,25 @@ public abstract class AbstractFeatureTest {
replaceConfigurationFile("etc/custom.properties", new File(basedir+"/src/test/resources/org/apache/activemq/karaf/itest/custom.properties")),
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")),
replaceConfigurationFile("etc/activemq.xml", new File(basedir+"/src/test/resources/org/apache/activemq/karaf/itest/activemq.xml")),
replaceConfigurationFile("etc/org.ops4j.pax.logging.cfg", new File(basedir+"/src/test/resources/org/apache/activemq/karaf/itest/org.ops4j.pax.logging.cfg")),
scanFeatures(getActiveMQKarafFeatureUrl(), f.toArray(new String[f.size()]))};
return options;
}
protected boolean withinReason(Callable<Boolean> callable) throws Throwable {
long max = System.currentTimeMillis() + ASSERTION_TIMEOUT;
while (true) {
try {
return callable.call();
} catch (Throwable t) {
if (System.currentTimeMillis() < max) {
TimeUnit.SECONDS.sleep(1);
continue;
} else {
throw t;
}
}
}
}
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.activemq.karaf.itest;
import java.util.concurrent.Callable;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -23,6 +24,9 @@ import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(JUnit4TestRunner.class)
public class ActiveMQBrokerFeatureTest extends AbstractFeatureTest {
@ -32,9 +36,26 @@ public class ActiveMQBrokerFeatureTest extends AbstractFeatureTest {
}
@Test
public void test() throws Exception {
public void test() throws Throwable {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.getBrokerURL();
}
withinReason(new Callable<Boolean>(){
@Override
public Boolean call() throws Exception {
assertEquals("brokerName = amq-broker", executeCommand("activemq:list").trim());
return true;
}
});
withinReason(new Callable<Boolean>(){
@Override
public Boolean call() throws Exception {
assertTrue(executeCommand("activemq:bstat").trim().contains("BrokerName = amq-broker"));
return true;
}
});
}
}