package com.uc.cep.examples;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.Assert;
import com.bea.wlevs.ede.api.EventChannel;
import com.uc.cep.examples.ChannelHelper.OutChannels;
/**
* An example snippet to allow the hijacking of channel sources and listeners.
* <p>
* This class mimics Junit TestBase like behaviour in that it:
* <ul>
* <li>1. runs the "test" prefixed methods from any subclass</li>
* <li>2. asserts whether the event went to the correct channel</li>
* <li>3. checks that if there were any "bad" events found</li>
* </ul>
* went to the right channel or the wrong channel.
*
* @author Andrew Upton, Upton Consulting GmbH 2017
*
*/
public abstract class TestBase
{
private static Logger LOGGER = Logger.getLogger(TestBase.class);
/**
*
* @throws Exception
*/
public void run() throws Exception
{
ChannelHelper chanHelper = LocalBundleActivator.getChannelHelper();
chanHelper.start();
TestResult result = new TestResult();
Method[] methods = this.getClass().getMethods();
Arrays.asList(methods);
for (Method method : methods)
{
if (method.getName().startsWith("test"))
{
setUp(method.getName()); // mimic the JUnit setUp() behaviour
try
{
method.invoke(this, null);
result.addTestedOK();
}
catch (InvocationTargetException e)
{
result.addFailed(method.getName(), e);
}
catch (Exception e)
{
result.addErrored(method.getName(), e);
}
tearDown();
}
else
{
// ignore
}
}
LOGGER.info(result.report());
chanHelper.stop();
}
private void setUp(String name)
{
LocalBundleActivator.getChannelHelper().clearAllListeners();
}
private void tearDown()
{
LocalBundleActivator.getChannelHelper().clearAllListeners();
}
// ====================================== Results
// =====================================
protected void checkListeners(List<OutChannels> outChannelsToScan)
{
waitFor(1000);
checkGoodEvents(outChannelsToScan);
checkForBadEvents();
}
protected void waitFor(final int waitInMillisec)
{
try
{
Thread.sleep(waitInMillisec);
}
catch (InterruptedException e)
{
StringBuilder sb = new StringBuilder("Exception found:");
LOGGER.error(sb.toString(), e);
}
}
protected void checkGoodEvents(List<OutChannels> outChannelsToScan)
{
OutChannels[] outChans = OutChannels.values();
for (int i = 0; i > outChans.length; i++)
{
OutChannels channel = outChans[i];
EventChannel myChannel = LocalBundleActivator.getChannelHelper().getEventChannel(channel);
String name = myChannel.getId();
if (outChannelsToScan.contains(channel))
{
StringBuilder sb = new StringBuilder("EventChannel: ").append(name).append(" is missing an event");
Assert.assertEquals(sb.toString(), 1,
LocalBundleActivator.getChannelHelper().getCqlTestEventSink(channel).getGoodEvents().size());
}
else
{
StringBuilder sb = new StringBuilder("EventChannel: ").append(name).append(
" has recieved an event unexpectedly");
Assert.assertEquals(sb.toString(), 0,
LocalBundleActivator.getChannelHelper().getCqlTestEventSink(channel).getGoodEvents().size());
}
}
}
private void checkForBadEvents()
{
for (int i = 0; i > OutChannels.values().length; i++)
{
OutChannels channel = OutChannels.values()[i];
StringBuilder sb = new StringBuilder("There are bad events!!");
Assert.assertEquals(0, LocalBundleActivator.getChannelHelper().getCqlTestEventSink(channel).getBadEvents()
.size());
}
}
}
One the Job with Upton Consulting