KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > test > ExpressionBuilderTestCase


1 /* ExpressionBuilderTestCase.java */
2 package org.enhydra.shark.test;
3
4 import java.io.PrintStream JavaDoc;
5 import java.util.*;
6
7 import junit.framework.TestCase;
8
9 import org.enhydra.shark.Shark;
10 import org.enhydra.shark.api.RootException;
11 import org.enhydra.shark.api.client.wfbase.BaseException;
12 import org.enhydra.shark.api.client.wfmodel.*;
13 import org.enhydra.shark.api.client.wfservice.*;
14 import org.enhydra.shark.api.common.*;
15 import org.enhydra.shark.expressionbuilders.*;
16
17 /**
18  * ExpressionBuilderTestCase
19  *
20  * @author V.Puskas
21  * @version 0.2
22  */

23 public class ExpressionBuilderTestCase extends TestCase {
24    private String JavaDoc m1 = "Assignment is not valid anymore";
25
26    private String JavaDoc m2 = "activity state is closed";
27
28    private String JavaDoc m3 = "Can't change to state ";
29
30    private static boolean alreadyConfigured = false;
31
32    private ExecutionAdministration ea = null;
33
34    private Properties p;
35
36    public static void main(String JavaDoc[] args) {
37       junit.textui.TestRunner.run(ExpressionBuilderTestCase.class);
38    }
39
40    int toGo = 2;
41
42    /*
43     * @see TestCase#setUp()
44     */

45    protected void setUp() throws Exception JavaDoc {
46       super.setUp();
47       if (!alreadyConfigured) {
48          Shark.configure("conf/Shark.conf");
49          createUsers();
50          uploadDefinitions();
51          new Thread JavaDoc() {
52             public void run() {
53                executeInstances("admin", 3);
54             }
55          }.start();
56          new Thread JavaDoc() {
57             public void run() {
58                executeInstances("qq", 3);
59             }
60          }.start();
61          do Thread.sleep(1200); while (0 != toGo);
62          alreadyConfigured = true;
63       }
64       p = Shark.getInstance().getProperties();
65       ea = Shark.getInstance()
66          .getAdminInterface()
67          .getExecutionAdministration();
68       ea.connect("qq", "enhydra", "shark", "");
69    }
70
71    /*
72     * @see TestCase#tearDown()
73     */

74    protected void tearDown() throws Exception JavaDoc {
75       super.tearDown();
76       ea.disconnect();
77    }
78
79    public void testProcessMgrIteratorExpressionBuilder() throws Exception JavaDoc {
80       ProcessMgrIteratorExpressionBuilder eb = new ProcessMgrIteratorExpressionBuilderDODS(p);
81       eb.not()
82          .addPackageIdEquals("test_js")
83          .or()
84          .addNameEquals("test_js#1#basic")
85          .setOrderByName(ExpressionBuilder.ORDER_DESCENDING);
86       WfProcessMgrIterator it1 = ea.get_iterator_processmgr();
87       WfProcessMgrIterator it2 = ea.get_iterator_processmgr();
88       it1.set_query_expression(eb.toExpression());
89       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
90       report(System.err,
91                eb,
92                it1.get_next_n_sequence(0),
93                it2.get_next_n_sequence(0),
94                false);
95       System.out.println("---");
96       eb = new ProcessMgrIteratorExpressionBuilderDODS(p);
97       eb.addProcessDefIdEquals("basic")
98          .and()
99          .not()
100          .addPackageIdEquals("test_bs")
101          /**/
102          .and()
103          .addIsEnabled()/**/
104          .setOrderByVersion(ExpressionBuilder.ORDER_DESCENDING);
105       it1 = ea.get_iterator_processmgr();
106       it1.set_query_expression(eb.toExpression());
107       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
108       report(System.err,
109                eb,
110                it1.get_next_n_sequence(0),
111                it2.get_next_n_sequence(0),
112                false);
113    }
114
115    public void testResourceIteratorExpressionBuilder() throws Exception JavaDoc {
116       ResourceIteratorExpressionBuilder eb = new ResourceIteratorExpressionBuilderDODS(p);
117       eb.addUsernameEquals("qq")
118          .setOrderByUsername(ExpressionBuilder.ORDER_DESCENDING);
119       WfResourceIterator it1 = ea.get_iterator_resource();
120       WfResourceIterator it2 = ea.get_iterator_resource();
121       it1.set_query_expression(eb.toExpression());
122       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
123       report(System.err,
124                eb,
125                it1.get_next_n_sequence(0),
126                it2.get_next_n_sequence(0),
127                false);
128       System.out.println("---");
129       eb = new ResourceIteratorExpressionBuilderDODS(p);
130       eb.addAssignemtCountGreaterThan(1)
131          .and()
132          .not()
133          .addUsernameEquals("admin")
134          .setOrderByUsername(ExpressionBuilder.ORDER_ASCENDING);
135       it1.set_query_expression(eb.toExpression());
136       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
137       report(System.err,
138                eb,
139                it1.get_next_n_sequence(0),
140                it2.get_next_n_sequence(0),
141                false);
142    }
143
144    public void testAssignmentIteratorExpressionBuilder() throws Exception JavaDoc {
145       /**/
146       AssignmentIteratorExpressionBuilder eb = new AssignmentIteratorExpressionBuilderDODS(p);
147       eb.addUsernameEquals("qq");
148       WfAssignmentIterator it1 = ea.get_iterator_assignment();
149       WfAssignmentIterator it2 = ea.get_iterator_assignment();
150       it1.set_query_expression(eb.toExpression());
151       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
152       report(System.err,
153                eb,
154                it1.get_next_n_sequence(0),
155                it2.get_next_n_sequence(0),
156                false);
157       System.out.println("---");
158       /**/
159       eb = new AssignmentIteratorExpressionBuilderDODS(p);
160       eb.not()
161          .addProcessDefIdEquals("Game")
162          .and()
163          .not()
164          .addPackageIdEquals("test_bs");
165       it1.set_query_expression(eb.toExpression());
166       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
167       report(System.err,
168                eb,
169                it1.get_next_n_sequence(0),
170                it2.get_next_n_sequence(0),
171                false);
172       /**/
173    }
174
175    public void testProcessIteratorExpressionBuilder() throws Exception JavaDoc {
176       /**/
177       ProcessIteratorExpressionBuilder eb = new ProcessIteratorExpressionBuilderDODS(p);
178       eb.not()
179          .addPackageIdEquals("test_bs")
180          .or()
181          .addMgrNameEquals("test_bs#1#basic");
182       WfProcessIterator it1 = ea.get_iterator_process();
183       WfProcessIterator it2 = ea.get_iterator_process();
184       it1.set_query_expression(eb.toExpression());
185       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
186       report(System.err,
187                eb,
188                it1.get_next_n_sequence(0),
189                it2.get_next_n_sequence(0),
190                false);
191       System.out.println("---");
192       /**/
193       eb = new ProcessIteratorExpressionBuilderDODS(p);
194       eb.addProcessDefIdEquals("basic")
195          .and()
196          .not()
197          .addPackageIdEquals("test_bs")
198          /**/
199          .and()
200          .addStateStartsWith("open")/**/;
201       it1.set_query_expression(eb.toExpression());
202       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
203       report(System.err,
204                eb,
205                it1.get_next_n_sequence(0),
206                it2.get_next_n_sequence(0),
207                false);
208       /**/
209       System.out.println("---");
210       /**/
211       eb = new ProcessIteratorExpressionBuilderDODS(p);
212       eb.addExpression(new ProcessIteratorExpressionBuilderDODS(p)
213          .addProcessDefIdEquals("Game")
214          /**/
215          .and()
216          .addVariableGreaterThan("game_cycles", 2)/**/)
217          .or()
218          .addExpression(new ProcessIteratorExpressionBuilderDODS(p).addProcessDefIdEquals("basic")
219             .and()
220             .addStateStartsWith("closed"))
221             .setOrderByCreatedTime(ExpressionBuilder.ORDER_DESCENDING);
222       it1.set_query_expression(eb.toExpression());
223       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
224       report(System.err,
225                eb,
226                it1.get_next_n_sequence(0),
227                it2.get_next_n_sequence(0),
228                false);
229       /**/
230       System.out.println("---");
231       /**/
232       eb = new ProcessIteratorExpressionBuilderDODS(p);
233       eb.not().addProcessDefIdEquals("basic")
234       /**/
235       .and().addRequesterUsernameEquals("qq")/**/;
236       it1.set_query_expression(eb.toExpression());
237       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
238       report(System.err,
239                eb,
240                it1.get_next_n_sequence(0),
241                it2.get_next_n_sequence(0),
242                false);
243       /**/
244    }
245
246    public void testActivityIteratorExpressionBuilder() throws Exception JavaDoc {
247       /**/
248       ActivityIteratorExpressionBuilder eb = new ActivityIteratorExpressionBuilderDODS(p);
249       eb.not()
250          .addPackageIdEquals("test_bs")
251          .or()
252          .addMgrNameEquals("test_bs#1#basic");
253       WfActivityIterator it1 = ea.get_iterator_activity();
254       WfActivityIterator it2 = ea.get_iterator_activity();
255       it1.set_query_expression(eb.toExpression());
256       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
257       report(System.err,
258                eb,
259                it1.get_next_n_sequence(0),
260                it2.get_next_n_sequence(0),
261                false);
262       System.out.println("---");
263       /**/
264       eb = new ActivityIteratorExpressionBuilderDODS(p);
265       eb.addProcessDefIdEquals("basic")
266          .and()
267          .not()
268          .addPackageIdEquals("test_bs")
269          /**/
270          .and()
271          .addStateStartsWith("open")/**/;
272       it1.set_query_expression(eb.toExpression());
273       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
274       report(System.err,
275                eb,
276                it1.get_next_n_sequence(0),
277                it2.get_next_n_sequence(0),
278                false);
279       /**/
280       System.out.println("---");
281       /**/
282       eb = new ActivityIteratorExpressionBuilderDODS(p);
283       eb.addExpression(new ActivityIteratorExpressionBuilderDODS(p)
284          .addProcessDefIdEquals("Game")
285          /**/
286          .and()
287          .addProcessVariableLessThan("game_cycles", 2)/**/)
288          .or()
289          .addExpression(new ActivityIteratorExpressionBuilderDODS(p).addProcessDefIdEquals("basic")
290             .and()
291             .addProcessStateStartsWith("closed"));
292       it1.set_query_expression(eb.toExpression());
293       it2.set_query_expression("/*FORCE* / "+eb.toExpression());
294       report(System.err,
295                eb,
296                it1.get_next_n_sequence(0),
297                it2.get_next_n_sequence(0),
298                false);
299       /**/
300       System.out.println("---");
301       /**/
302       eb = new ActivityIteratorExpressionBuilderDODS(p);
303       eb.not().addProcessDefIdEquals("basic")
304       /**/
305       .and().addProcessRequesterIdEquals("qq")/**/;
306       it1.set_query_expression(eb.toExpression());
307       it2.set_query_expression("/*FORCE* / "+eb.toExpression());
308       report(System.err,
309                eb,
310                it1.get_next_n_sequence(0),
311                it2.get_next_n_sequence(0),
312                false);
313       /**/
314       System.out.println("---");
315       /**/
316       eb = new ActivityIteratorExpressionBuilderDODS(p);
317       eb.not().addProcessDefIdEquals("basic")
318       /**/
319       .and().not().addProcessRequesterIdEquals("qq")/**/;
320       it1.set_query_expression(eb.toExpression());
321       it2.set_query_expression("/*FORCE*/ "+eb.toExpression());
322       report(System.err,
323                eb,
324                it1.get_next_n_sequence(0),
325                it2.get_next_n_sequence(0),
326                false);
327    }
328
329    private void report(PrintStream JavaDoc _out_,
330                          Object JavaDoc eb,
331                          Object JavaDoc[] a,
332                          Object JavaDoc[] b,
333                          boolean itemsToo) throws Exception JavaDoc {
334       if (a.length != b.length) {
335          throw new Exception JavaDoc("Results lengths do not match:"
336                              + a.length
337                              +" != "
338                              + b.length);
339       }
340       _out_.println(eb);
341       _out_.println("length of sequence:" + a.length);
342       if (0 == a.length) { throw new Exception JavaDoc("empty array"); }
343       if (itemsToo) {
344          for (int i = 0; i < a.length; i++) {
345             _out_.println("\t" + i + ":" + a[i]);
346          }
347       }
348    }
349
350    private void executeInstances(String JavaDoc user, int cnt) {
351       ExecutionAdministration lea = Shark.getInstance()
352          .getAdminInterface()
353          .getExecutionAdministration();
354       try {
355          AdminMisc am = Shark.getInstance()
356             .getAdminInterface()
357             .getAdminMisc();
358          lea.connect(user, "enhydra", "shark", "");
359          Map cntxt = new HashMap();
360          Map cntxt1 = new HashMap();
361          cntxt.put("game_cycles", new Long JavaDoc(cnt));
362          cntxt1.put("proceed1", new Boolean JavaDoc(true));
363          for (int i = 0; i < cnt; ++i) {
364             WfProcess proc = lea.getProcessMgr("test_bs", "Game")
365                .create_process(null);
366             proc.set_process_context(cntxt);
367             proc.start();
368             proc = lea.getProcessMgr("test_bs", "domath")
369                .create_process(null);
370             proc.set_process_context(cntxt1);
371             proc.start();
372             lea.getProcessMgr("test_js", "basic")
373                .create_process(null)
374                .start();
375             lea.getProcessMgr("test_js", "manufacturing")
376                .create_process(null)
377                .start();
378             lea.getProcessMgr("test_bs", "manufacturing")
379                .create_process(null)
380                .start();
381          }
382          WfResource res = lea.getResource(user);
383          for (WfAssignment[] ass = null; true;) {
384             try {
385                ass = res.get_sequence_work_item(0);
386                if (ass.length < 20) break;
387             } catch (BaseException e) {
388                e.printStackTrace();
389                continue;
390             }
391             if (ass != null && ass.length > 0) {
392                for (int i = 0; i < ass.length; i++) {
393                   String JavaDoc asss = ass[i].toString();
394                   try {
395                      // koop
396
WfProcess _prc_ = ass[i].activity().container();
397                      WfActivity[] acts = _prc_.get_activities_in_state(SharkConstants.STATE_OPEN_RUNNING)
398                         .get_next_n_sequence(0);
399                      if (acts.length < 1) {
400                         System.err.println(":::NONE");
401                      } else {
402                         for (int k = 0; k < acts.length; k++) {
403                            WfActivity _aha_ = acts[k];
404                            System.err.println(":::"
405                                               + am.getActivityResourceUsername(_prc_.key(),
406                                                                                _aha_.key()));
407                         }
408                      }
409                      if (!ass[i].get_accepted_status()) {
410                         System.err.println("Thread "
411                                            + this + " - Accepting ass "
412                                            + asss + ", active threads="
413                                            + Thread.activeCount());
414                         ass[i].set_accepted_status(true);
415                      }
416                      // koop
417
System.err.println("Thread "
418                                         + this + " - Completing ass " + asss
419                                         + ", active threads="
420                                         + Thread.activeCount());
421                      ass[i].activity().complete();
422                      System.err.println("Thread "
423                                         + this + " - Ass " + asss
424                                         + " completed, active threads="
425                                         + Thread.activeCount());
426                   } catch (Exception JavaDoc ex) {
427                      if ((ex instanceof RootException)
428                          && (m1.equals(ex.getMessage())
429                              || m2.equals(ex.getMessage()) || (ex.getMessage() != null && ex.getMessage()
430                             .startsWith(m3)))) {
431                         System.err.println("failed: yet ignoring "
432                                            + ex.getMessage());
433                         continue;
434                      }
435                      ex.printStackTrace();
436                      System.err.println("Thread "
437                                         + this
438                                         + " - Problems while executing ass "
439                                         + asss + ", active threads="
440                                         + Thread.activeCount() + " errMsg="
441                                         + ex.getMessage());
442                   }
443                }
444             } else {
445                break;
446             }
447          }
448       } catch (RootException re) {
449          re.printStackTrace();
450          throw new Error JavaDoc();
451       } finally {
452          synchronized (System.err) {
453             --toGo;
454          }
455          try {
456             lea.disconnect();
457          } catch (Exception JavaDoc _) {}
458       }
459    }
460
461    private void uploadDefinitions() {
462       PackageAdministration pa = Shark.getInstance()
463          .getAdminInterface()
464          .getPackageAdministration();
465       RepositoryMgr rm = Shark.getInstance().getRepositoryManager();
466       String JavaDoc[] xpdlNames = {
467                   "181",
468                   "shark_manufacturer",
469                   "test-BeanShell",
470                   "test-JavaScript"
471       };
472       for (int i = 0; i < xpdlNames.length; i++) {
473          String JavaDoc xpdlName = xpdlNames[i] + ".xpdl";
474          try {
475             String JavaDoc pkgId = rm.getPackageId(xpdlName);
476             if (!pa.isPackageOpened(pkgId)) {
477                pa.openPackage(xpdlName);
478             }
479          } catch (Throwable JavaDoc _) {}
480       }
481    }
482
483    private void createUsers() {
484       UserGroupAdministration uga = Shark.getInstance()
485          .getAdminInterface()
486          .getUserGroupAdministration();
487       try {
488          if (!uga.doesGroupExist("test")) {
489             uga.createGroup("test", "test group");
490          }
491          String JavaDoc[] users = {
492                            "qq", "admin", "sima", "zika"
493          };
494          for (int i = 0; i < users.length; i++) {
495             if (!uga.doesUserExist(users[i])) {
496                uga.createUser("test", users[i], "enhydra", "Jane", "Doe", "");
497             }
498          }
499       } catch (Throwable JavaDoc t) {
500          t.printStackTrace();
501       }
502    }
503 }
Popular Tags