XmlProcessReader xmlReader = new XmlProcessReader( modules, Thread.currentThread().getContextClassLoader() );
String xml = RuleFlowMigrator.convertReaderToString(isr);
Reader reader = new StringReader(xml);
List<Process> processes = xmlReader.read(reader);
if (processes != null && processes.size() == 1) {
final RuleFlowProcess process = (RuleFlowProcess) processes.get(0);
String packageName = process.getPackageName();
if (packageName == null || packageName.trim().length() == 0 || "org.drools.bpmn2".equals(packageName)) {
packageName = "org.jbpm";
}
final String pName = packageName;
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
public void execute(final IProgressMonitor monitor)
throws CoreException {
try {
IFolder folder = file.getProject().getFolder("src/main/java");
IPackageFragmentRoot packageFragmentRoot = javaProject
.getPackageFragmentRoot(folder);
IPackageFragment packageFragment = packageFragmentRoot
.createPackageFragment(pName, true, monitor);
String processName = process.getName();
processName = processName.replaceAll("\\s", "_");
if (processName == null || processName.trim().length() == 0) {
processName = "Process";
}
String fileName = processName + "JUnitTest";
String output =
"package " + pName + ";\n" +
"\n" +
"import java.util.ArrayList;\n" +
"import java.util.HashMap;\n" +
"import java.util.List;\n" +
"import java.util.Map;\n" +
"\n" +
"import org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler;\n" +
"import org.jbpm.test.JbpmJUnitTestCase;\n" +
"\n" +
"import org.junit.Test;\n" +
"\n" +
"import org.kie.api.runtime.KieSession;\n" +
"import org.kie.api.runtime.process.ProcessInstance;\n" +
"import org.kie.api.task.TaskService;\n" +
"import org.kie.api.task.model.TaskSummary;\n" +
"\n" +
"public class " + fileName + " extends JbpmJUnitTestCase {\n" +
"\n";
boolean containsHumanTasks = containsHumanTasks(process);
if (containsHumanTasks) {
output +=
" public " + fileName + "() {\n" +
" super(true);\n" +
" }\n" +
"\n";
}
Map<String, String> cases = new HashMap<String, String>();
Map<String, String> ongoingCases = new HashMap<String, String>();
boolean done = processNodes("", process.getStart(null), "", cases, ongoingCases);
if (!done) {
if (ongoingCases.size() == 1) {
cases.put("Implicit", ongoingCases.values().iterator().next());
} else {
throw new IllegalArgumentException("Could not create implicit case: " + ongoingCases.size());
}
}
for (Map.Entry<String, String> entry: cases.entrySet()) {
output +=
" @Test\n" +
" public void test" + entry.getKey() + "() {\n" +
" KieSession ksession = createKnowledgeSession(\"" + file.getName() + "\");\n";
Set<String> serviceTasks = new HashSet<String>();
containsServiceTasks(serviceTasks, process);
for (String service: serviceTasks) {
output += " ksession.getWorkItemManager().registerWorkItemHandler(\"" + service + "\", new SystemOutWorkItemHandler());\n";
}
if (containsHumanTasks) {
output += " TaskService taskService = getTaskService();\n";
}
List<Variable> variables = process.getVariableScope().getVariables();
if (variables != null && variables.size() > 0) {
output +=
" Map<String, Object> params = new HashMap<String, Object>();\n" +
" // initialize variables here if necessary\n";
for (Variable v: variables) {
output +=
" // params.put(\"" + v.getName() + "\", value); // type " + v.getType().getStringType() + "\n";
}
output +=
" ProcessInstance processInstance = ksession.startProcess(\"" + process.getId() + "\", params);\n";
} else {
output +=
" ProcessInstance processInstance = ksession.startProcess(\"" + process.getId() + "\");\n";
}
output +=
entry.getValue() +
" // do your checks here\n" +
" // for example, assertProcessInstanceCompleted(processInstance.getId(), ksession);\n" +