package org.kie.spring.jbpm;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.List;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import org.jbpm.process.audit.AuditLogService;
import org.jbpm.process.audit.ProcessInstanceLog;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.runtime.process.ProcessInstance;
import org.kie.api.task.TaskService;
import org.kie.api.task.model.TaskSummary;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JTAUserManagedSharedEntityManagerFactorySpringTest extends AbstractJbpmSpringTest {
/* The purpose of the shared entity manager is to allow the application domain
* and JBPM domain to be persisted by a single entity manager and transaction.
*/
@Test
public void testSpringWithJTAAndSharedEMFAndUserManagedTx() throws Exception {
context = new ClassPathXmlApplicationContext("jbpm/shared-taskservice/jta-emf-spring.xml");
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
ut.begin();
RuntimeManager manager = (RuntimeManager) context.getBean("runtimeManager");
RuntimeEngine engine = manager.getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
TaskService taskService = (TaskService) context.getBean("taskService");
ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello");
System.out.println("Process started");
AuditLogService logService = (AuditLogService) context.getBean("logService");
ProcessInstanceLog log = logService.findProcessInstance(processInstance.getId());
assertNotNull(log);
List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
System.out.println("Found " + tasks.size() + " task(s) for user 'john'");
assertEquals(1, tasks.size());
long taskId = tasks.get(0).getId();
taskService.start(taskId, "john");
taskService.complete(taskId, "john", null);
tasks = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
System.out.println("Found " + tasks.size() + " task(s) for user 'mary'");
assertEquals(1, tasks.size());
taskId = tasks.get(0).getId();
taskService.start(taskId, "mary");
taskService.complete(taskId, "mary", null);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNull(processInstance);
System.out.println("Process instance completed");
ut.commit();
}
}