Source code: ras/Basic.java
1 /*
2 * Danet GmbH
3 * Beratung und Software-Entwicklung
4 * Geschäftstelle AN
5 *
6 * $Id: Basic.java,v 1.33 2003/05/02 14:55:58 lipp Exp $
7 *
8 * $Log: Basic.java,v $
9 * Revision 1.33 2003/05/02 14:55:58 lipp
10 * Resolved some more package dependencies.
11 *
12 * Revision 1.32 2003/04/26 16:46:55 lipp
13 * Made unittests and systemtests coexist in eclipse.
14 *
15 * Revision 1.31 2003/04/16 19:25:04 lipp
16 * Adapted to jdk 1.4
17 *
18 * Revision 1.30 2003/02/25 17:08:27 lipp
19 * Reorganized requester implementation.
20 *
21 * Revision 1.29 2003/02/05 15:57:06 lipp
22 * Replaced DummyRequester with DefaultRequester.
23 *
24 * Revision 1.28 2003/01/13 20:29:42 lipp
25 * Classpath fixes.
26 *
27 * Revision 1.27 2002/12/19 21:37:42 lipp
28 * Reorganized interfaces.
29 *
30 * Revision 1.26 2002/12/05 10:20:39 lipp
31 * Adapted unittests to assignment changes.
32 *
33 * Revision 1.25 2002/12/04 16:05:08 lipp
34 * Reorganized assignment handling.
35 *
36 * Revision 1.24 2002/10/23 11:36:47 lipp
37 * Got them running again.
38 *
39 * Revision 1.23 2002/09/18 21:26:51 lipp
40 * Removed SAXFacade (integrated with WorkflowEngine).
41 *
42 * Revision 1.22 2002/09/18 20:48:21 lipp
43 * Cleanly separated workflow engine and service.
44 *
45 * Revision 1.21 2002/09/18 13:00:26 lipp
46 * Renamed WorkflowEngine to WorkflowService and introduced
47 * WorkflowServiceFactory.
48 *
49 * Revision 1.20 2002/09/08 18:49:18 lipp
50 * Proper use of packageId and processId.
51 *
52 * Revision 1.19 2002/09/04 20:50:44 lipp
53 * Adapted to new process manager name schema.
54 *
55 * Revision 1.18 2002/08/30 21:32:07 lipp
56 * Finished transition to WorkflowEngine.
57 *
58 * Revision 1.17 2002/08/30 13:37:05 lipp
59 * Using Workflow engine facade now.
60 *
61 * Revision 1.16 2002/08/26 20:23:14 lipp
62 * Lots of method renames.
63 *
64 * Revision 1.15 2002/08/21 22:06:48 lipp
65 * Finished transition to ProcessMgrStub.
66 *
67 * Revision 1.14 2002/06/27 10:48:36 lipp
68 * Adapted to change in RAS/RMS interface.
69 *
70 * Revision 1.13 2002/02/04 15:18:55 lipp
71 * Made ActivityFinderAndKey visible in EJB Remote interface.
72 *
73 * Revision 1.12 2002/02/03 21:41:42 lipp
74 * Cleaned up unittests.
75 *
76 * Revision 1.11 2002/01/23 14:09:57 huaiyang
77 * Adapted to API changed and new test case.
78 *
79 * Revision 1.10 2002/01/09 14:00:01 lipp
80 * Cleaned up relation between wfcore, resource assignment and resource
81 * management service.
82 *
83 * Revision 1.9 2001/12/19 09:06:15 lipp
84 * Added some comments.
85 *
86 * Revision 1.8 2001/12/18 22:16:53 lipp
87 * Restructured DOM generation, implemented "assignments" method from ras.
88 *
89 * Revision 1.7 2001/12/18 15:35:11 lipp
90 * Implemented workItems and isMemberOfWorkItem.
91 *
92 * Revision 1.6 2001/12/17 15:52:12 lipp
93 * New version.
94 *
95 * Revision 1.5 2001/12/17 12:14:04 lipp
96 * Adapted to configurable ResourceManagement/AssignmentServices.
97 *
98 * Revision 1.4 2001/12/16 21:48:57 lipp
99 * addAssignment implemented.
100 *
101 * Revision 1.3 2001/12/16 10:37:35 lipp
102 * Assignment service implemented.
103 *
104 * Revision 1.2 2001/12/15 18:04:28 lipp
105 * Fixed client class packing.
106 *
107 * Revision 1.1 2001/12/15 15:52:01 lipp
108 * Implementation of ActivityFinder completed.
109 *
110 */
111 package ras;
112
113 import java.io.BufferedReader;
114 import java.io.InputStream;
115 import java.io.InputStreamReader;
116
117 import java.util.Collection;
118 import java.util.Iterator;
119
120 import de.danet.an.util.EJBUtil;
121
122 import de.danet.an.workflow.omgcore.WfActivity;
123 import de.danet.an.workflow.omgcore.WfAssignment;
124 import de.danet.an.workflow.omgcore.WfProcess;
125 import de.danet.an.workflow.omgcore.WfProcessMgr;
126 import de.danet.an.workflow.omgcore.WfResource;
127
128 import de.danet.an.workflow.api.Activity;
129 import de.danet.an.workflow.api.DefaultRequester;
130 import de.danet.an.workflow.api.FactoryConfigurationError;
131 import de.danet.an.workflow.api.ProcessDefinitionDirectory;
132 import de.danet.an.workflow.api.WorkflowService;
133 import de.danet.an.workflow.api.WorkflowServiceFactory;
134
135 import de.danet.an.workflow.apix.ExtActivity;
136 import de.danet.an.workflow.ejbs.admin.ProcessDefinitionDirectoryHome;
137 import de.danet.an.workflow.ejbs.util.WorkflowEngine;
138 import de.danet.an.workflow.ejbs.util.WorkflowEngineHome;
139 import de.danet.an.workflow.spis.ras.ActivityFinder;
140 import de.danet.an.workflow.spis.ras.ResourceAssignmentService;
141 import de.danet.an.workflow.spis.ras.ResourceAssignmentServiceFactory;
142 import de.danet.an.workflow.spis.rms.ResourceManagementService;
143 import de.danet.an.workflow.spis.rms.ResourceManagementServiceFactory;
144
145 import common.UTLoginContext;
146 import junit.framework.Test;
147 import junit.framework.TestCase;
148 import junit.framework.TestSuite;
149
150 /**
151 * Zusammenstellung aller RasObjectTests.
152 */
153 public class Basic extends TestCase {
154
155 /**
156 * Konstruktor zum Erzeugen eines TestCase
157 */
158 public Basic(String name) {
159 super (name);
160 }
161
162 /**
163 * Stellt diese TestSuite zusammen.
164 */
165 public static Test suite() {
166 TestSuite suite = new TestSuite();
167 suite.addTest(new Basic("getAFinder"));
168 suite.addTest(new Basic("getRAS"));
169 suite.addTest(new Basic("getServerRAS"));
170 suite.addTest(new Basic("addAssignment"));
171 return suite;
172 }
173
174 private static UTLoginContext plc = null;
175
176 /**
177 * Initialisierung.
178 */
179 protected void setUp() throws Exception {
180 if (plc == null) {
181 plc = new UTLoginContext();
182 plc.login();
183 }
184 }
185
186 private static WorkflowService wfsCache = null;
187
188 private WorkflowService workflowService() {
189 if (wfsCache == null) {
190 try {
191 WorkflowServiceFactory wfsf
192 = WorkflowServiceFactory.newInstance ();
193 wfsCache = wfsf.newWorkflowService();
194 } catch (FactoryConfigurationError e) {
195 throw new IllegalStateException (e.getMessage());
196 }
197 }
198 return wfsCache;
199 }
200
201 private static WfProcess testProc = null;
202
203 /**
204 * "Simply" try to get a finder.
205 */
206 public void getAFinder () throws Exception {
207 importProcessDefinition ();
208 // To get a finder, we need a process.
209 testProc = createProcess ("ut-ras", "ras_test");
210 assertTrue (testProc != null);
211 Collection acts = testProc.steps();
212 assertTrue (acts.size() > 0);
213 Iterator i = acts.iterator();
214 ExtActivity act = (ExtActivity)i.next();
215 assertTrue (act != null);
216 ActivityFinder af = act.activityFinder();
217 Activity restored = (Activity)af.find(act.key());
218 assertTrue (act.key().equals(restored.key()));
219 }
220
221 private static ResourceAssignmentService ras = null;
222
223 /**
224 * Just try to get the RAS.
225 */
226 public void getRAS () throws Exception {
227 ResourceAssignmentServiceFactory rasf
228 = ResourceAssignmentServiceFactory.newInstance();
229 ras = rasf.newResourceAssignmentService();
230 // ResourceManagementServiceFactory rmsf
231 // = ResourceManagementServiceFactory.newInstance();
232 // rmsf.setResourceAssignmentService (ras);
233 }
234
235 /**
236 * Get RAS from server.
237 */
238 public void getServerRAS () throws Exception {
239 WorkflowEngineHome home = (WorkflowEngineHome)EJBUtil.lookupEJBHome
240 (WorkflowEngineHome.class, "de.danet.an.workflow.Engine");
241 WorkflowEngine engine = home.create();
242 ras = engine.resourceAssignmentService();
243 }
244
245 private ResourceManagementService getRms() throws Exception {
246 ResourceAssignmentServiceFactory rasf
247 = ResourceAssignmentServiceFactory.newInstance();
248 ResourceManagementServiceFactory rmsf
249 = ResourceManagementServiceFactory.newInstance();
250 rmsf.setResourceAssignmentService
251 (rasf.newResourceAssignmentService());
252 return rmsf.newResourceManagementService ();
253 }
254
255 /**
256 * Add an assignment
257 */
258 public void addAssignment () throws Exception {
259 ResourceManagementService rms = getRms();
260 // find predefined resource "Test, JUnit"
261 WfResource res = null;
262 Collection resList = rms.listResources();
263 for (Iterator i = resList.iterator(); i.hasNext();) {
264 WfResource resource = (WfResource)i.next();
265 if (resource.resourceName().startsWith ("Test, JUnit")) {
266 res = resource;
267 break;
268 }
269 }
270 assertTrue (res != null);
271 // get current number of work items
272 int prevCount = res.workItems().size();
273 Collection acts = testProc.steps();
274 Iterator i = acts.iterator();
275 ExtActivity act = (ExtActivity)i.next();
276 ActivityFinder af = act.activityFinder ();
277 // assign
278 ras.addAssignment (af, act.key(), act, res);
279 // read back work items
280 Collection assmnts = res.workItems ();
281 // must be one more
282 assertTrue (assmnts.size() == prevCount + 1);
283 // find this assignment/activity
284 WfAssignment assmt = null;
285 boolean found = false;
286 for (Iterator a = assmnts.iterator(); a.hasNext();) {
287 assmt = (WfAssignment)a.next();
288 WfActivity act2 = assmt.activity();
289 String a2key = act2.key();
290 if (act.key().equals(a2key)
291 && act.container().key().equals(act2.container().key())) {
292 found = true;
293 break;
294 }
295 }
296 assertTrue (found);
297 assertTrue (res.isMemberOfWorkItems (assmt));
298 if (1 == 1) {
299 return;
300 }
301 // get assignments (other way round) and find this
302 assmnts = act.assignments();
303 assertTrue (assmnts != null);
304 assertTrue (assmnts.size() == 1);
305 found = false;
306 for (Iterator a2 = assmnts.iterator(); a2.hasNext();) {
307 assmt = (WfAssignment)a2.next();
308 WfResource res2 = assmt.assignee();
309 if (res.resourceKey().equals(res2.resourceKey())) {
310 found = true;
311 break;
312 }
313 }
314 assertTrue (found);
315 }
316
317 /**
318 * Uploading the XPDL file and saving the process definitions in the
319 * database.
320 */
321 private void importProcessDefinition() throws Exception {
322 // now read input
323 InputStream is = getClass()
324 .getResourceAsStream("/ras/rasTestXPDL.xml");
325 if (is == null) {
326 return;
327 }
328 BufferedReader br = null;
329 br = new BufferedReader (new InputStreamReader(is, "ISO-8859-1"));
330
331 StringBuffer toParse = new StringBuffer();
332 String st;
333 while ((st = br.readLine()) != null)
334 toParse.append(st);
335
336 ProcessDefinitionDirectoryHome pddh
337 = (ProcessDefinitionDirectoryHome)EJBUtil.lookupEJBHome
338 (ProcessDefinitionDirectoryHome.class,
339 "ProcessDefinitionDirectory");
340 ProcessDefinitionDirectory pdd =
341 (ProcessDefinitionDirectory)pddh.create();
342 if (!pdd.processDefinitionExists("ut-ras", "ras_test")) {
343 pdd.importProcessDefinitions(toParse.toString());
344 }
345 }
346
347 /**
348 * Try to create the process identified by the id
349 */
350 public WfProcess createProcess (String pkgId, String prcId)
351 throws Exception {
352 ProcessDefinitionDirectory pdd = null;
353 try {
354 pdd = workflowService().processDefinitionDirectory();
355 WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
356 return pmgr.createProcess (new DefaultRequester(workflowService()));
357 } finally {
358 workflowService().release (pdd);
359 }
360 }
361
362 }