KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > log > ActionLogTest


1 package org.jbpm.graph.log;
2
3 import java.util.List JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.jbpm.graph.def.ActionHandler;
8 import org.jbpm.graph.def.ProcessDefinition;
9 import org.jbpm.graph.exe.ExecutionContext;
10 import org.jbpm.graph.exe.ProcessInstance;
11 import org.jbpm.graph.exe.Token;
12 import org.jbpm.logging.exe.LoggingInstance;
13
14 public class ActionLogTest extends TestCase {
15   
16   public static class LoggedAction implements ActionHandler {
17     private static final long serialVersionUID = 1L;
18     public void execute(ExecutionContext executionContext) throws Exception JavaDoc {
19       // just for testing the logs, so we don't have to do anything here
20
}
21   }
22   
23   public void testSimpleActionLog() {
24     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
25       "<process-definition>" +
26       " <start-state>" +
27       " <transition to='state'>" +
28       " <action class='org.jbpm.graph.log.ActionLogTest$LoggedAction' />" +
29       " </transition>" +
30       " </start-state>" +
31       " <state name='state' />" +
32       "</process-definition>"
33     );
34     
35     // start a process instance
36
ProcessInstance processInstance = new ProcessInstance(processDefinition);
37     Token token = processInstance.getRootToken();
38     processInstance.signal();
39     
40     // check the transition log (from the start state to the state)
41
LoggingInstance loggingInstance = processInstance.getLoggingInstance();
42     List JavaDoc actionLogs = loggingInstance.getLogs(ActionLog.class);
43     
44     assertEquals(1, actionLogs.size());
45
46     ActionLog actionLog = (ActionLog) actionLogs.get(0);
47     assertSame(token, actionLog.getToken());
48     assertSame(LoggedAction.class, actionLog.getAction().getActionDelegation().getInstance().getClass());
49     assertNull(actionLog.getException());
50     assertNotNull(actionLog.getDate());
51   }
52
53   public static class LoggedExceptionAction implements ActionHandler {
54     private static final long serialVersionUID = 1L;
55     public void execute(ExecutionContext executionContext) throws Exception JavaDoc {
56       throw new RuntimeException JavaDoc("please, log me");
57     }
58   }
59   
60   public void testActionExceptionLog() {
61     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
62       "<process-definition>" +
63       " <start-state>" +
64       " <transition to='state'>" +
65       " <action class='org.jbpm.graph.log.ActionLogTest$LoggedExceptionAction' />" +
66       " </transition>" +
67       " </start-state>" +
68       " <state name='state' />" +
69       "</process-definition>"
70     );
71     
72     // start a process instance
73
ProcessInstance processInstance = new ProcessInstance(processDefinition);
74     Token token = processInstance.getRootToken();
75     try {
76       processInstance.signal();
77       fail("expected exception");
78     } catch (RuntimeException JavaDoc e) {
79
80       // check the transition log (from the start state to the state)
81
LoggingInstance loggingInstance = processInstance.getLoggingInstance();
82       List JavaDoc actionLogs = loggingInstance.getLogs(ActionLog.class);
83       
84       assertEquals(1, actionLogs.size());
85
86       ActionLog actionLog = (ActionLog) actionLogs.get(0);
87       assertSame(token, actionLog.getToken());
88       assertSame(LoggedExceptionAction.class, actionLog.getAction().getActionDelegation().getInstance().getClass());
89       assertNotNull(actionLog.getException());
90       assertNotNull(actionLog.getDate());
91     }
92   }
93 }
94
Popular Tags