1  /* 
2   * JBoss, Home of Professional Open Source 
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated 
4   * by the @authors tag. See the copyright.txt in the distribution for a 
5   * full listing of individual contributors. 
6   * 
7   * This is free software; you can redistribute it and/or modify it 
8   * under the terms of the GNU Lesser General Public License as 
9   * published by the Free Software Foundation; either version 2.1 of 
10   * the License, or (at your option) any later version. 
11   * 
12   * This software is distributed in the hope that it will be useful, 
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of 
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
15   * Lesser General Public License for more details. 
16   * 
17   * You should have received a copy of the GNU Lesser General Public 
18   * License along with this software; if not, write to the Free 
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org
21   */ 
22  package org.jbpm.taskmgmt.exe; 
23  
24  import java.util.List; 
25  
26  import org.jbpm.db.AbstractDbTestCase; 
27  import org.jbpm.graph.def.ProcessDefinition; 
28  import org.jbpm.graph.exe.Comment; 
29  import org.jbpm.graph.exe.ProcessInstance;
30  import org.jbpm.graph.exe.Token; 
32  
33  public class TaskInstanceDbTest extends AbstractDbTestCase { 
34  
35    public void testTaskInstanceUnrelatedToAProcess() { 
36      TaskInstance taskInstance = new TaskInstance("do laundry", "someoneelse"); 
37      session.save(taskInstance); 
38      long id = taskInstance.getId(); 
39       
40      newTransaction(); 
41       
42      taskInstance = (TaskInstance) session.load(TaskInstance.class, new Long(id)); 
43      assertNotNull(taskInstance); 
44      assertEquals("do laundry", taskInstance.getName()); 
45      assertEquals("someoneelse", taskInstance.getActorId()); 
46    } 
47  
48    public void testTaskInstanceBasicLifeCycle() { 
49      ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 
50        "<process-definition>" + 
51        "  <start-state>" + 
52        "    <transition to='a' />" + 
53        "  </start-state>" + 
54        "  <task-node name='a'>" + 
55        "    <task name='clean ceiling' />" + 
56        "    <transition to='end' />" + 
57        "  </task-node>" + 
58        "  <end-state name='end' />" + 
59        "</process-definition>" 
60      ); 
61  
62      processDefinition = saveAndReload(processDefinition); 
63       
64      ProcessInstance processInstance = new ProcessInstance(processDefinition); 
65      processInstance.signal(); 
66       
67      processInstance = saveAndReload(processInstance); 
68  
69      long tokenId = processInstance.getRootToken().getId(); 
70      List taskInstances = taskMgmtSession.findTaskInstancesByToken(tokenId); 
71      assertEquals(1, taskInstances.size()); 
72      TaskInstance taskInstance = (TaskInstance) taskInstances.get(0); 
73      assertFalse(taskInstance.hasEnded()); 
74      assertEquals(tokenId, taskInstance.getToken().getId()); 
75      // do some updates 
76      taskInstance.end(); 
77       
78      processInstance = saveAndReload(processInstance); 
79       
80      taskInstance = taskMgmtSession.loadTaskInstance(taskInstance.getId()); 
81      assertTrue(taskInstance.hasEnded()); 
82    } 
83  
84    public void testTaskName() { 
85      ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 
86        "<process-definition>" + 
87        "  <start-state>" + 
88        "    <transition to='a' />" + 
89        "  </start-state>" + 
90        "  <task-node name='a'>" + 
91        "    <task name='clean ceiling' />" + 
92        "    <transition to='end' />" + 
93        "  </task-node>" + 
94        "  <end-state name='end' />" + 
95        "</process-definition>" 
96      ); 
97  
98      processDefinition = saveAndReload(processDefinition); 
99       
100      ProcessInstance processInstance = new ProcessInstance(processDefinition); 
101      processInstance.signal(); 
102       
103      processInstance = saveAndReload(processInstance); 
104  
105      long tokenId = processInstance.getRootToken().getId(); 
106      List taskInstances = taskMgmtSession.findTaskInstancesByToken(tokenId); 
107      assertEquals(1, taskInstances.size()); 
108      TaskInstance taskInstance = (TaskInstance) taskInstances.get(0); 
109      assertFalse(taskInstance.hasEnded()); 
110      assertEquals("clean ceiling", taskInstance.getName()); 
111      assertEquals("clean ceiling", taskInstance.getTask().getName()); 
112      // do some updates 
113      taskInstance.setName("clean ceiling thoroughly"); 
114       
115      processInstance = saveAndReload(processInstance); 
116      taskInstance = taskMgmtSession.loadTaskInstance(taskInstance.getId()); 
117       
118      assertEquals("clean ceiling thoroughly", taskInstance.getName()); 
119      assertEquals("clean ceiling", taskInstance.getTask().getName()); 
120    } 
121     
122    public void testTaskComments() { 
123      ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 
124        "<process-definition>" + 
125        "  <start-state>" + 
126        "    <transition to='a' />" + 
127        "  </start-state>" + 
128        "  <task-node name='a'>" + 
129        "    <task name='clean ceiling' />" + 
130        "    <transition to='end' />" + 
131        "  </task-node>" + 
132        "  <end-state name='end' />" + 
133        "</process-definition>" 
134      ); 
135  
136      processDefinition = saveAndReload(processDefinition); 
137       
138      ProcessInstance processInstance = new ProcessInstance(processDefinition); 
139      processInstance.signal(); 
140       
141      processInstance = saveAndReload(processInstance); 
142  
143      long tokenId = processInstance.getRootToken().getId(); 
144      List taskInstances = taskMgmtSession.findTaskInstancesByToken(tokenId); 
145      assertEquals(1, taskInstances.size()); 
146      TaskInstance taskInstance = (TaskInstance) taskInstances.get(0); 
147      taskInstance.addComment("please hurry!"); 
148       
149      processInstance = saveAndReload(processInstance); 
150      taskInstance = taskMgmtSession.loadTaskInstance(taskInstance.getId()); 
151       
152      List comments = taskInstance.getComments(); 
153      assertEquals(1, comments.size()); 
154       
155      Comment comment = (Comment) comments.get(0);  
156      assertEquals("please hurry!", comment.getMessage()); 
157      assertSame(taskInstance, comment.getTaskInstance()); 
158    }      
160    public void testBlockingTask() { 
161      ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 
162        "<process-definition>" + 
163        "  <start-state>" + 
164        "    <transition to='a' />" + 
165        "  </start-state>" + 
166        "  <task-node name='a'>" + 
167        "    <task name='laundry' blocking='true' />" + 
168        "    <transition to='b' />" + 
169        "  </task-node>" + 
170        "  <state name='b' />" + 
171        "</process-definition>" 
172      ); 
173      processDefinition = saveAndReload(processDefinition); 
174      ProcessInstance processInstance = new ProcessInstance(processDefinition); 
175      Token token = processInstance.getRootToken(); 
176      processInstance.signal(); 
177      assertEquals("a", token.getNode().getName()); 
178      processInstance = saveAndReload(processInstance); 
179      try { 
180        processInstance.signal(); 
181        fail("expected exception"); 
182      } catch (IllegalStateException e) { 
183        // OK 
184      } 
185    } 
186  
        } 
 
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jonim/archive/2006/10/10/1328483.aspx