KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ExpressionBuilderTestCase.java */
2 package org.enhydra.shark.test;
3
4 import java.io.PrintStream JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import junit.framework.TestCase;
8
9 import org.enhydra.shark.api.common.SharkConstants;
10 import org.enhydra.shark.corba.ExpressionBuilders.*;
11 import org.enhydra.shark.corba.WorkflowService.*;
12 import org.omg.CORBA.ORB JavaDoc;
13 import org.omg.CosNaming.*;
14 import org.omg.WfBase.BaseException;
15 import org.omg.WfBase.NameValue;
16 import org.omg.WorkflowModel.*;
17
18 /**
19  * ExpressionBuilderTestCase
20  *
21  * @author V.Puskas
22  * @version 0.1
23  */

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

50    protected void setUp() throws Exception JavaDoc {
51       super.setUp();
52       aha = findWorkflowServer("localhost", "10123", "Shark");
53       if (!alreadyConfigured) {
54          createUsers();
55          uploadDefinitions();
56          new Thread JavaDoc() {
57             public void run() {
58                executeInstances("admin", 3);
59             }
60          }.start();
61          new Thread JavaDoc() {
62             public void run() {
63                executeInstances("qq", 3);
64             }
65          }.start();
66          do
67             Thread.sleep(1200);
68          while (0 != toGo);
69          alreadyConfigured = true;
70       }
71       ea = aha.getExecutionAdministration();
72       ea.connect("qq", "enhydra", "shark", "");
73    }
74
75    /*
76     * @see TestCase#tearDown()
77     */

78    protected void tearDown() throws Exception JavaDoc {
79       super.tearDown();
80       ea.disconnect();
81    }
82
83    public void testProcessMgrIteratorExpressionBuilder() throws Exception JavaDoc {
84       ProcessMgrIteratorExpressionBuilder eb = aha.getExpressionBuilderManager()
85          .getProcessMgrIteratorExpressionBuilder();
86       eb.not()
87          .addPackageIdEquals("test_js")
88          .or()
89          .addNameEquals("test_js#1#basic");
90       WfProcessMgrIterator it1 = ea.get_iterator_processmgr();
91       WfProcessMgrIterator it2 = ea.get_iterator_processmgr();
92       it1.set_query_expression(eb.toExpression());
93       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
94       report(System.out,
95              eb,
96              it1.get_next_n_sequence(0),
97              it2.get_next_n_sequence(0),
98              false);
99       System.out.println("---");
100       eb = aha.getExpressionBuilderManager()
101          .getProcessMgrIteratorExpressionBuilder();
102       eb.addProcessDefIdEquals("basic")
103          .and()
104          .not()
105          .addPackageIdEquals("test_bs")
106          /**/
107          .and()
108          .addIsEnabled()/**/
109          .setOrderByEnabled(false);
110       it1 = ea.get_iterator_processmgr();
111       it1.set_query_expression(eb.toExpression());
112       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
113       report(System.out,
114              eb,
115              it1.get_next_n_sequence(0),
116              it2.get_next_n_sequence(0),
117              false);
118    }
119
120    public void testResourceIteratorExpressionBuilder() throws Exception JavaDoc {
121       ResourceIteratorExpressionBuilder eb = aha.getExpressionBuilderManager()
122          .getResourceIteratorExpressionBuilder();
123       eb.addUsernameEquals("qq");
124       WfResourceIterator it1 = ea.get_iterator_resource();
125       WfResourceIterator it2 = ea.get_iterator_resource();
126       it1.set_query_expression(eb.toExpression());
127       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
128       report(System.out,
129              eb,
130              it1.get_next_n_sequence(0),
131              it2.get_next_n_sequence(0),
132              false);
133       System.out.println("---");
134       eb = aha.getExpressionBuilderManager()
135          .getResourceIteratorExpressionBuilder();
136       eb.addAssignemtCountGreaterThan(1)
137          .and()
138          .not()
139          .addUsernameEquals("admin")
140          .setOrderByUsername(true);
141       it1.set_query_expression(eb.toExpression());
142       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
143       report(System.out,
144              eb,
145              it1.get_next_n_sequence(0),
146              it2.get_next_n_sequence(0),
147              false);
148    }
149
150    public void testAssignmentIteratorExpressionBuilder() throws Exception JavaDoc {
151       /**/
152       AssignmentIteratorExpressionBuilder eb = aha.getExpressionBuilderManager()
153          .getAssignmentIteratorExpressionBuilder();
154       eb.addUsernameEquals("qq").setOrderByCreatedTime(true);
155       WfAssignmentIterator it1 = ea.get_iterator_assignment();
156       WfAssignmentIterator it2 = ea.get_iterator_assignment();
157       it1.set_query_expression(eb.toExpression());
158       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
159       report(System.out,
160              eb,
161              it1.get_next_n_sequence(0),
162              it2.get_next_n_sequence(0),
163              false);
164       System.out.println("---");
165       /**/
166       eb = aha.getExpressionBuilderManager()
167          .getAssignmentIteratorExpressionBuilder();
168       eb.not()
169          .addProcessDefIdEquals("Game")
170          .and()
171          .not()
172          .addPackageIdEquals("test_bs")
173          .setOrderByProcessId(false);
174       it1.set_query_expression(eb.toExpression());
175       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
176       report(System.out,
177              eb,
178              it1.get_next_n_sequence(0),
179              it2.get_next_n_sequence(0),
180              false);
181       /**/
182    }
183
184    public void testProcessIteratorExpressionBuilder() throws Exception JavaDoc {
185       /**/
186       ProcessIteratorExpressionBuilder eb = aha.getExpressionBuilderManager()
187          .getProcessIteratorExpressionBuilder();
188       eb.not()
189          .addPackageIdEquals("test_bs")
190          .or()
191          .addMgrNameEquals("test_bs#1#basic")
192          .setOrderByLastStateTime(true);
193       WfProcessIterator it1 = ea.get_iterator_process();
194       WfProcessIterator it2 = ea.get_iterator_process();
195       it1.set_query_expression(eb.toExpression());
196       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
197       report(System.out,
198              eb,
199              it1.get_next_n_sequence(0),
200              it2.get_next_n_sequence(0),
201              false);
202       System.out.println("---");
203       /**/
204       eb = aha.getExpressionBuilderManager()
205          .getProcessIteratorExpressionBuilder();
206       eb.addProcessDefIdEquals("basic")
207          .and()
208          .not()
209          .addPackageIdEquals("test_bs")
210          /**/
211          .and()
212          .addStateStartsWith("open")/**/;
213       it1.set_query_expression(eb.toExpression());
214       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
215       report(System.out,
216              eb,
217              it1.get_next_n_sequence(0),
218              it2.get_next_n_sequence(0),
219              false);
220       /**/
221       System.out.println("---");
222       /**/
223       eb = aha.getExpressionBuilderManager()
224          .getProcessIteratorExpressionBuilder();
225       eb.addExpression(aha.getExpressionBuilderManager()
226          .getProcessIteratorExpressionBuilder()
227          .not()
228          .addProcessDefIdEquals("basic")
229       /*****************************************************************
230        * / .and() .addVariableGreaterThan("game_cycles", 2)/
231        ****************************************************************/

232       ).or().addExpression(aha.getExpressionBuilderManager()
233          .getProcessIteratorExpressionBuilder()
234          .addProcessDefIdEquals("basic")
235          .and()
236          .addStateStartsWith("closed"))
237          .setOrderByPriority(false);
238       it1.set_query_expression(eb.toExpression());
239       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
240       report(System.out,
241              eb,
242              it1.get_next_n_sequence(0),
243              it2.get_next_n_sequence(0),
244              false);
245       /**/
246       System.out.println("---");
247       /**/
248       eb = aha.getExpressionBuilderManager()
249          .getProcessIteratorExpressionBuilder();
250       eb.not().addProcessDefIdEquals("basic")
251       /**/
252       .and().addRequesterIdEquals("qq")/**/;
253       it1.set_query_expression(eb.toExpression());
254       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
255       report(System.out,
256              eb,
257              it1.get_next_n_sequence(0),
258              it2.get_next_n_sequence(0),
259              false);
260    }
261
262    public void testActivityIteratorExpressionBuilder() throws Exception JavaDoc {
263       /**/
264       ActivityIteratorExpressionBuilder eb = aha.getExpressionBuilderManager()
265          .getActivityIteratorExpressionBuilder();
266       eb.not()
267          .addPackageIdEquals("test_bs")
268          .or()
269          .addMgrNameEquals("test_bs#1#basic");
270       WfActivityIterator it1 = ea.get_iterator_activity();
271       WfActivityIterator it2 = ea.get_iterator_activity();
272       it1.set_query_expression(eb.toExpression());
273       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
274       report(System.out,
275              eb,
276              it1.get_next_n_sequence(0),
277              it2.get_next_n_sequence(0),
278              false);
279       System.out.println("---");
280       /**/
281       eb = aha.getExpressionBuilderManager()
282          .getActivityIteratorExpressionBuilder();
283       eb.addProcessDefIdEquals("basic")
284          .and()
285          .not()
286          .addPackageIdEquals("test_bs")
287          /**/
288          .and()
289          .addStateStartsWith("open")/**/;
290       it1.set_query_expression(eb.toExpression());
291       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
292       report(System.out,
293              eb,
294              it1.get_next_n_sequence(0),
295              it2.get_next_n_sequence(0),
296              false);
297       /**/
298       System.out.println("---");
299       /**/
300       eb = aha.getExpressionBuilderManager()
301          .getActivityIteratorExpressionBuilder();
302       eb.addExpression(aha.getExpressionBuilderManager()
303          .getActivityIteratorExpressionBuilder()
304          .not()
305          .addProcessDefIdEquals("basic")
306       /*****************************************************************
307        * / .and() .addProcessVariableGreaterThan("game_cycles", 2)/
308        ****************************************************************/

309       ).or().addExpression(aha.getExpressionBuilderManager()
310          .getActivityIteratorExpressionBuilder()
311          .addProcessDefIdEquals("basic")
312          .and()
313          .addProcessStateStartsWith("closed"));
314       it1.set_query_expression(eb.toExpression());
315       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
316       report(System.out,
317              eb,
318              it1.get_next_n_sequence(0),
319              it2.get_next_n_sequence(0),
320              false);
321       /**/
322       System.out.println("---");
323       /**/
324       eb = aha.getExpressionBuilderManager()
325          .getActivityIteratorExpressionBuilder();
326       eb.not().addProcessDefIdEquals("basic")
327       /**/
328       .and().addProcessRequesterIdEquals("qq")/**/;
329       it1.set_query_expression(eb.toExpression());
330       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
331       report(System.out,
332              eb,
333              it1.get_next_n_sequence(0),
334              it2.get_next_n_sequence(0),
335              false);
336       /**/
337       System.out.println("---");
338       /**/
339       eb = aha.getExpressionBuilderManager()
340          .getActivityIteratorExpressionBuilder();
341       eb.not().addProcessDefIdEquals("basic")
342       /**/
343       .and().not().addProcessRequesterIdEquals("qq")/**/;
344       it1.set_query_expression(eb.toExpression());
345       it2.set_query_expression("/*FORCE*/ " + eb.toExpression());
346       report(System.out,
347              eb,
348              it1.get_next_n_sequence(0),
349              it2.get_next_n_sequence(0),
350              false);
351    }
352
353    private void report(PrintStream JavaDoc _out_,
354                        Object JavaDoc eb,
355                        Object JavaDoc[] a,
356                        Object JavaDoc[] b,
357                        boolean itemsToo) throws Exception JavaDoc {
358       if (a.length != b.length) { throw new Exception JavaDoc("Results lengths do not match:"
359                                                       + a.length
360                                                       + " != "
361                                                       + b.length); }
362       _out_.println(eb.getClass().getName());
363       _out_.println("length of sequence:" + a.length);
364       if (0 == a.length) { throw new Exception JavaDoc("empty array"); }
365       if (itemsToo) {
366          for (int i = 0; i < a.length; i++) {
367             _out_.println("\t" + i + ":" + a[i]);
368          }
369       }
370    }
371
372    private void executeInstances(String JavaDoc user, int cnt) {
373       SharkConnection lea = aha.getSharkConnection();
374       try {
375          AdminMisc am = aha.getAdminMisc();
376          lea.connect(user, "enhydra", "shark", "");
377          NameValue[] cntxt = new NameValue[] {
378             new NameValue("game_cycles", orb.create_any())
379          };
380          cntxt[0].the_value.insert_longlong(cnt);
381          for (int i = 0; i < cnt; ++i) {
382             WfProcess proc = lea.createProcess("test_bs", "Game");
383             proc.set_process_context(cntxt);
384             proc.start();
385             lea.createProcess("test_js", "basic")
386                .start();
387             lea.createProcess("test_js", "manufacturing")
388                .start();
389             lea.createProcess("test_bs", "manufacturing")
390                .start();
391          }
392          WfResource res = lea.getResourceObject();
393          for (WfAssignment[] ass = null; true;) {
394             try {
395                ass = res.get_sequence_work_item(0);
396                if (ass.length < 20) break;
397             } catch (BaseException e) {
398                e.printStackTrace();
399                continue;
400             }
401             if (ass != null && ass.length > 0) {
402                for (int i = 0; i < ass.length; i++) {
403                   String JavaDoc asss = ass[i].toString();
404                   try {
405                      // koop
406
WfProcess _prc_ = ass[i].activity().container();
407                      WfActivity[] acts = _prc_.get_activities_in_state(SharkConstants.STATE_OPEN_RUNNING)
408                         .get_next_n_sequence(0);
409                      if (acts.length < 1) {
410                         System.err.println(":::NONE");
411 // } else {
412
// for (int k = 0; k < acts.length; k++) {
413
// WfActivity _aha_ = acts[k];
414
// System.err.println(":::"
415
// + am.getActivityResourceUsername(_prc_.key(),
416
// _aha_.key()));
417
// }
418
}
419                      if (!ass[i].get_accepted_status()) {
420                         System.err.println("Thread "
421                                            + this + " - Accepting ass "
422                                            + asss + ", active threads="
423                                            + Thread.activeCount());
424                         ass[i].set_accepted_status(true);
425                      }
426                      // koop
427
System.err.println("Thread "
428                                         + this + " - Completing ass " + asss
429                                         + ", active threads="
430                                         + Thread.activeCount());
431                      ass[i].activity().complete();
432                      System.err.println("Thread "
433                                         + this + " - Ass " + asss
434                                         + " completed, active threads="
435                                         + Thread.activeCount());
436                   } catch (Exception JavaDoc ex) {
437                      ex.printStackTrace();
438                      System.err.println("Thread "
439                                         + this
440                                         + " - Problems while executing ass "
441                                         + asss + ", active threads="
442                                         + Thread.activeCount() + " errMsg="
443                                         + ex.getMessage());
444                   }
445                }
446             } else {
447                break;
448             }
449          }
450       } catch (Exception JavaDoc re) {
451          re.printStackTrace();
452          throw new Error JavaDoc();
453       } finally {
454          synchronized (System.err) {
455             --toGo;
456          }
457          try {
458             lea.disconnect();
459          } catch (Exception JavaDoc _) {}
460       }
461    }
462
463    private void uploadDefinitions() {
464       PackageAdministration pa = aha.getPackageAdministration();
465       RepositoryMgr rm = aha.getRepositoryManager();
466       try {
467          pa.connect("admin","enhydra","Shark","");
468          rm.connect("admin","enhydra","Shark","");
469       } catch (Exception JavaDoc e) {
470          e.printStackTrace();
471       }
472       String JavaDoc[] xpdlNames = {
473                   "181",
474                   "shark_manufacturer",
475                   "test-BeanShell",
476                   "test-JavaScript"
477       };
478       for (int i = 0; i < xpdlNames.length; i++) {
479          String JavaDoc xpdlName = xpdlNames[i] + ".xpdl";
480          try {
481             String JavaDoc pkgId = rm.getPackageId(xpdlName);
482             if (!pa.isPackageOpened(pkgId)) {
483                pa.openPkg(xpdlName);
484             }
485          } catch (Throwable JavaDoc _) {}
486       }
487    }
488
489    private void createUsers() {
490       UserGroupAdministration uga = aha.getUserGroupAdministration();
491       try {
492          uga.connect("admin", "enhydra", "Shark", "");
493          if (!uga.doesGroupExist("test")) {
494             uga.createGroup("test", "test group");
495          }
496          String JavaDoc[] users = {
497                            "qq", "admin", "sima", "zika"
498          };
499          for (int i = 0; i < users.length; i++) {
500             if (!uga.doesUserExist(users[i])) {
501                uga.createUser("test", users[i], "enhydra", "Jane", "Doe", "");
502             }
503          }
504       } catch (Throwable JavaDoc t) {
505          t.printStackTrace();
506       } finally {
507          try {
508             uga.disconnect();
509          } catch (Exception JavaDoc e) {
510             // TODO Auto-generated catch block
511
e.printStackTrace();
512          }
513       }
514    }
515
516    public static SharkInterface findWorkflowServer(String JavaDoc host,
517                                                    String JavaDoc port,
518                                                    String JavaDoc workflowServerName) throws Exception JavaDoc {
519       // String[] args={"-ORBInitialHost",host,"-ORBInitialPort",port};
520
String JavaDoc[] args = {
521                   "-ORBInitRef",
522                   "NameService=corbaloc::"
523                                                       + host + ":" + port
524                                                       + "/NameService"
525       };
526
527       // Create and initialize the ORB
528
orb = ORB.init(args, null);
529       // Get the root naming context
530
org.omg.CORBA.Object JavaDoc objRef = orb.resolve_initial_references("NameService");
531       NamingContext ncRef = NamingContextHelper.narrow(objRef);
532
533       // Resolve the object reference in naming
534
NameComponent nc = new NameComponent(workflowServerName, "");
535       NameComponent path[] = {
536          nc
537       };
538       SharkInterface si = SharkInterfaceHelper.narrow(ncRef.resolve(path));
539       //objRef._release();
540
//ncRef._release();
541
return si;
542    }
543 }
Popular Tags