import java.util.Properties;
import junit.framework.Assert;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SimpleTest {
static Log logger = LogFactory.getLog(SimpleTest.class);
static String PROCESS_XML = "" +
"" +
"" +
" start of the process" +
" " +
" " +
" " +
" " +
"" +
"" +
" " +
" " +
"" +
"" +
" " +
" " +
"" +
"" +
" " +
" " +
"" +
"" +
" " +
" " +
"" +
"" +
" " +
"" +
"" +
" process finished normally" +
" " +
" " +
" " +
"" +
"";
static JbpmConfiguration config;
@BeforeClass
public static void beforeClass()
{
config = JbpmConfiguration.getInstance("jbpm-test.cfg.xml");
config.createSchema();
config.startJobExecutor();
}
@AfterClass
public static void afterClass()
{
config.close();
}
@Test
public void testSimple() throws Exception
{
//Assert.assertTrue(config.getJobExecutor().getNbrOfThreads()>1);
logger.info("### TEST: deploy + start processes ###");
// create test properties
final Properties testProperties =new Properties();
testProperties.put("test", "true");
execute(new JbpmContextCallback(){
public Void doWithinJpbmContext(JbpmContext context) throws Exception {
logger.info(context);
context.deployProcessDefinition(ProcessDefinition
.parseXmlString(PROCESS_XML));
return null;
}
});
for (int i = 0; i < 10; i++)
{
logger.info("### TEST: starting process " + i + " ###");
final ProcessInstance pi = execute(new JbpmContextCallback(){
public ProcessInstance doWithinJpbmContext(JbpmContext context) throws Exception {
ProcessInstance pi = context.newProcessInstance("simpletest");
pi.getContextInstance().addVariables(testProperties);
context.save(pi);
pi.signal();
return pi;
}
});
logger.info("### TEST: wait for process completion ###");
waitFor( pi.getId() );
execute(new JbpmContextCallback(){
public Void doWithinJpbmContext(JbpmContext context) throws Exception {
ProcessInstance pi2 = context.getProcessInstance(pi.getId());
Assert.assertEquals("end-state-success", pi2.getRootToken().getNode().getName());
return null;
}
});
logger.info("### TEST: finished ###");
}
}
protected T execute(JbpmContextCallback callback) throws Exception
{
JbpmContext context = config.createJbpmContext();
try
{
return callback.doWithinJpbmContext(context);
}
finally
{
context.close();
}
}
protected void waitFor(final long piId) throws Exception
{
long startTime = System.currentTimeMillis();
int MIN = 2;
while (true)
{
if (System.currentTimeMillis() - startTime > MIN * 60 * 1000)
{
Assert.fail("Aborting after "+MIN+" min.");
return;
}
logger.info("waiting for workflow completion...."); // + pi.getRootToken().getNode().getName() );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.error("", e);
Assert.fail();
}
if (execute(new JbpmContextCallback(){
public Boolean doWithinJpbmContext(JbpmContext context) throws Exception {
return context.getProcessInstance(piId).hasEnded();
}})) break;
}
}
interface JbpmContextCallback
{
public T doWithinJpbmContext(JbpmContext context) throws Exception;
}
}