KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > client > Client


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright © Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33 //
34
//
35
// History:
36
// 10/08/2001 Shiping Initial coding based on the existing code
37
//
38
// 09/11/2001 Shiping Change the update transaction amount (20000)
39
// with (tranInfo.stockID*tranInfo.accountNo).
40
//
41
// 12/11/2001 Shiping Rule off ResetDB.
42
//
43
// 12/11/2001 Shiping Let each thread sleep for a random time to avoid
44
// the contention on the container to create instances of beans.
45
// (There is such a problem in IBM WebSphere)
46
//
47
// 20/11/2001 Shiping Add code for warm-up and cool-down for critical performance measuring.
48
// 20/03/2003 Shiping Removed the code for performance monitor for open source.
49
//
50
//
51
package stockonline.client;
52
53 import javax.ejb.*;
54 import javax.naming.*;
55 import java.rmi.*;
56 import java.util.*;
57 import java.io.*;
58
59 import stockonline.util.*;
60 import stockonline.ejb.session.interf.*;
61
62 public class Client implements Runnable JavaDoc
63 {
64     final static boolean verbose = false;
65
66     // Result log instances
67
//
68
ResultLog resultLogByID = new ResultLog();
69     ResultLog resultLogNewAccount = new ResultLog();
70     ResultLog resultLogBuyStock = new ResultLog();
71     ResultLog resultLogUpdateAccount = new ResultLog();
72     ResultLog resultLogSellStock = new ResultLog();
73     ResultLog resultLogGetHolding = new ResultLog();
74     ResultLog resultLogTPS = new ResultLog();
75
76     static Synch synch;
77     static Properties runProperties = new Properties();
78
79     BrokerHome home = null;
80     int threadNum = 0;
81
82     int testIteration = 0;
83     int warmupIteration = 0;
84     int cooldownIteration = 0;
85     int numRetry = 0;
86
87     public Client(Synch synch, BrokerHome home, int threadNum)
88     {
89         this.synch = synch;
90         this.home = home;
91         this.threadNum = threadNum;
92
93         this.testIteration = Integer.parseInt(runProperties.getProperty("client.test.iteration", "10"));
94         if(verbose) System.out.println(Thread.currentThread().getName() + ": testIteration = " + this.testIteration);
95
96         this.warmupIteration = Integer.parseInt(runProperties.getProperty("client.warmup.iteration", "2"));
97         if(verbose) System.out.println(Thread.currentThread().getName() + ": warmupIteration = " + this.warmupIteration);
98
99         this.cooldownIteration = Integer.parseInt(runProperties.getProperty("client.cooldown.iteration", "2"));
100         if(verbose) System.out.println(Thread.currentThread().getName() + ": cooldownIteration = " + this.cooldownIteration);
101
102         this.numRetry = Integer.parseInt(runProperties.getProperty("client.create.retry", "9"));
103         if(verbose) System.out.println(Thread.currentThread().getName() + ": numRetry = " + this.numRetry);
104     }
105
106     public static void main(String JavaDoc[] args)
107     {
108         // To process args
109
//
110
args_proc (args);
111
112         // To get the bean home
113
//
114
BrokerHome myHome = getBeanHome();
115
116         // To check if this is functional test
117
//
118
String JavaDoc testType = runProperties.getProperty("client.test.type");
119         if(testType.equalsIgnoreCase("f"))
120             functionTest_proc(myHome);
121
122         // -----------------------------------
123
// The follows is for performance test
124
// -----------------------------------
125

126         // To get the number of threads (concurrent clients)
127
//
128
int threadNum = Integer.parseInt(runProperties.getProperty("client.test.P1"));
129         if(verbose) System.out.println("threadNum = " + threadNum);
130
131         // Create a synchronisation object, which ensures
132
// all threads start testing at the same time.
133
//
134
Synch mySynch = new Synch(threadNum);
135
136         // Note: We only create one object of the Runnable (Client) class;
137
// and we pass this object to all the Thread instances.
138
Client t = new Client(mySynch, myHome, threadNum);
139
140         // To create an array to keep the thread objects,
141
//
142
Thread JavaDoc threadArray[] = new Thread JavaDoc[threadNum];
143
144         System.out.println("\nTo start testing......");
145         //
146
for (int i = 0; i < threadNum; i++)
147         {
148             threadArray[i] = new Thread JavaDoc(t);
149             threadArray[i].start();
150         }
151
152         // To wait until all threads are ready
153
while(!synch.setGo()) Synch.sleep(100);
154
155         // To wait for all threads to finish
156
//
157
for (int i = 0; i < threadNum; i++)
158         {
159             try {
160                 threadArray[i].join();
161                 if(verbose) System.out.println("Thread-" + i + " : exited");
162             }
163             catch (InterruptedException JavaDoc e) {}
164         }
165
166         System.out.println("\n--- All threads are finished ---\n");
167
168         // To collect the test results
169
//
170
collectTestResult (t);
171     }
172
173     private static void collectTestResult ( Client t )
174     {
175         try
176         {
177             // To print out the result
178
//
179
System.out.println();
180             System.out.println("----- Test Result --------");
181             System.out.println("Buy (" + t.resultLogBuyStock.getCount() + ") " + t.resultLogBuyStock.getAverage());
182             System.out.println("Create (" + t.resultLogNewAccount.getCount() + ") " + t.resultLogNewAccount.getAverage());
183             System.out.println("Holding (" + t.resultLogGetHolding.getCount() + ") " + t.resultLogGetHolding.getAverage());
184             System.out.println("Query (" + t.resultLogByID.getCount() + ") " + t.resultLogByID.getAverage());
185             System.out.println("Sell (" + t.resultLogSellStock.getCount() + ") " + t.resultLogSellStock.getAverage());
186             System.out.println("Update (" + t.resultLogUpdateAccount.getCount() + ") " + t.resultLogUpdateAccount.getAverage());
187             System.out.println();
188             System.out.println("TPS (" + t.resultLogTPS.getCount() + ") " + t.resultLogTPS.getTotal());
189             System.out.println("--------------------------");
190
191             // To check if the test results need logging
192
//
193
String JavaDoc fileName = runProperties.getProperty("client.logfile");
194             if(fileName==null) return;
195
196             // To print out on screen without names for copy/past
197
//
198
System.out.println( t.resultLogBuyStock.getAverage());
199             System.out.println( t.resultLogNewAccount.getAverage());
200             System.out.println( t.resultLogGetHolding.getAverage());
201             System.out.println( t.resultLogByID.getAverage());
202             System.out.println( t.resultLogSellStock.getAverage());
203             System.out.println( t.resultLogUpdateAccount.getAverage());
204             System.out.println();
205             System.out.println( t.resultLogTPS.getTotal());
206
207             if(verbose) System.out.println("The test result is to be written to: " + fileName);
208             FileOutputStream fos = new FileOutputStream(fileName);
209             PrintWriter pw = new PrintWriter(fos);
210
211             pw.println( t.resultLogBuyStock.getAverage());
212             pw.println( t.resultLogNewAccount.getAverage());
213             pw.println( t.resultLogGetHolding.getAverage());
214             pw.println( t.resultLogByID.getAverage());
215             pw.println( t.resultLogSellStock.getAverage());
216             pw.println( t.resultLogUpdateAccount.getAverage());
217             pw.println( t.resultLogTPS.getTotal());
218
219             pw.close ();
220             fos.close();
221         }
222         catch(Exception JavaDoc e)
223         {
224             System.err.println("File I/O exception: " + e.toString());
225         }
226     }
227
228     private static BrokerHome getBeanHome()
229     {
230         try
231         {
232             // To get the client type
233
//
234
String JavaDoc clientType = runProperties.getProperty("client.type");
235             if(verbose) System.out.println("clientType = " + clientType);
236
237             // To create the initial context according to the client type
238
//
239
Context ctx = null;
240             ctx = new InitialContext(runProperties);
241             // ctx = new InitialContext();
242

243             // To get the EJB's JNDI name
244
//
245
String JavaDoc ejbJNDIName = runProperties.getProperty("ejb.JNDIName");
246             if(verbose) System.out.println("ejbJNDIName = " + ejbJNDIName);
247
248             // To look up the bean via the JNDI
249
//
250

251             Object JavaDoc obj = ctx.lookup(ejbJNDIName);
252             BrokerHome myHome = (BrokerHome)javax.rmi.PortableRemoteObject.narrow(obj, BrokerHome.class);
253
254             if(verbose) System.out.println("getBeanHome()............OK");
255             ctx.close();
256
257             return myHome;
258         }
259         catch (javax.naming.NamingException JavaDoc e)
260         {
261             System.err.println("Unable to find the object, due to: " + e.toString());
262             return null;
263         }
264         catch (Exception JavaDoc e)
265         {
266             System.err.println(e.toString());
267             return null;
268         }
269     }
270
271     public void run()
272     {
273         Broker broker = null;
274
275         int accountID;
276         QueryResult qr = null;
277         Collection list = null;
278         TranDeck tranDeck = null;
279         TranInfo tranInfo = null;
280         long startTime, endTime;
281
282         stockonline.util.Timer timer = new stockonline.util.Timer();
283         int _testIteration = 0;
284         int _warmupIteration = warmupIteration;
285         int _cooldownIteration = cooldownIteration;
286         int _numRetry = numRetry;
287
288         // Total itetrations, including warm-up and cool-down.
289
// But only the test results between warm-up and cool-down will be recorded.
290
///
291
_testIteration = testIteration + _warmupIteration + _cooldownIteration;
292
293         // Local Result log instances within a thread.
294
// After testing, add them to the "globle" log.
295
// In this way, the threads will not interfere
296
// with each other too much.
297
//
298
ResultLog _resultLogByID = new ResultLog();
299         ResultLog _resultLogNewAccount = new ResultLog();
300         ResultLog _resultLogBuyStock = new ResultLog();
301         ResultLog _resultLogUpdateAccount = new ResultLog();
302         ResultLog _resultLogSellStock = new ResultLog();
303         ResultLog _resultLogGetHolding = new ResultLog();
304         ResultLog _resultLogTPS = new ResultLog();
305
306         try
307         {
308             tranDeck = new TranDeck();
309
310             // To sleep for random time to avoid too much concurrent access to JNDI
311
//
312
tranDeck.shuffle();
313             tranInfo = tranDeck.getNext();
314             sleep(tranInfo.accountNo);
315
316             // To create the Broker EJB Object via the bean home interf
317
for(int i=0; i<_numRetry; i++)
318             {
319                 broker = home.create();
320                 if(broker!=null) break;
321                 else System.out.println(Thread.currentThread().getName() + " : To retry " + i);
322             }
323
324             // To be ready to go for test
325
//
326
synch.increase();
327             while(!synch.getGo()) Thread.sleep(99);
328
329             // If the bean is NULL, return to let other threads go on.
330
// But make it sure there are NOT many threads failed here!
331
//
332
if(broker==null)
333             {
334                 System.err.println(Thread.currentThread().getName() + " : failed to create the bean");
335                 return;
336             }
337         }
338         catch (Exception JavaDoc e)
339         {
340             System.err.println("Got exception: " + e.toString());
341             e.printStackTrace();
342         }
343
344         // For warm-up
345
//
346
for (int i = 0; i < _warmupIteration; i++)
347         {
348             // Shuffle the deck first
349
tranDeck.shuffle();
350             if(verbose) System.out.println(Thread.currentThread().getName() + " - Loop " + i);
351
352             for (int j = 0; j < TranDeck.TRAN_SIZE; j++)
353             {
354                 // Get next transaction descriptor
355
tranInfo = tranDeck.getNext();
356
357                 try
358                 {
359                     switch (tranInfo.tranType)
360                     {
361                     case TranDeck.TRAN_BUY:
362                         startTime = ResultLog.getCurrentTime();
363                         broker.buyStock(tranInfo.accountNo, tranInfo.stockID, 5);
364                         endTime = ResultLog.getCurrentTime();
365                         // _resultLogBuyStock.addSample(endTime - startTime);
366
break;
367
368                     case TranDeck.TRAN_SELL:
369                         startTime = ResultLog.getCurrentTime();
370                         broker.sellStock(tranInfo.accountNo, tranInfo.stockID, 5);
371                         endTime = ResultLog.getCurrentTime();
372                         // _resultLogSellStock.addSample(endTime - startTime);
373
break;
374
375                     case TranDeck.TRAN_QUERY_ID:
376                         startTime = ResultLog.getCurrentTime();
377                         qr = broker.queryStockValueByID(tranInfo.stockID);
378                         endTime = ResultLog.getCurrentTime();
379                         // _resultLogByID.addSample(endTime - startTime);
380
break;
381
382                     case TranDeck.TRAN_QUERY_CODE:
383                         startTime = ResultLog.getCurrentTime();
384                         qr = broker.queryStockValueByID(tranInfo.stockID);
385                         endTime = ResultLog.getCurrentTime();
386                         // _resultLogByID.addSample(endTime - startTime);
387
break;
388
389                     case TranDeck.TRAN_GETHOLDING:
390                         startTime = ResultLog.getCurrentTime();
391                         list = broker.getHoldingStatement(tranInfo.accountNo, 0);
392                         endTime = ResultLog.getCurrentTime();
393                         // _resultLogGetHolding.addSample(endTime - startTime);
394
break;
395
396                     case TranDeck.TRAN_CREATE:
397                         startTime = ResultLog.getCurrentTime();
398                         accountID = broker.newAccount("myName", "myAddress", 123456);
399                         endTime = ResultLog.getCurrentTime();
400                         // _resultLogNewAccount.addSample(endTime - startTime);
401
break;
402
403                     case TranDeck.TRAN_UPDATE:
404                         startTime = ResultLog.getCurrentTime();
405                         broker.updateAccount(tranInfo.accountNo, tranInfo.stockID*tranInfo.accountNo);
406                         endTime = ResultLog.getCurrentTime();
407                         // _resultLogUpdateAccount.addSample(endTime - startTime);
408
break;
409                     }
410
411                     // To update the performance monitor
412
// monTPS.incCount(Monitor.COUNT_TYPE.CNTTYPE_RAW, 1);
413
// monTPS.incCount(Monitor.COUNT_TYPE.CNTTYPE_PERSEC, 1);
414

415                     // To give other threads a chance to run
416
Thread.yield();
417                 }
418                 catch (Exception JavaDoc e)
419                 {
420                     // We should record number of exceptions here.
421
//
422
e.printStackTrace();
423                 } // end of try
424
} // end of loop j
425
} // end of loop i
426

427         // For testing
428
//
429
timer.start();
430             
431         for (int i = 0; i < _testIteration; i++)
432         {
433             // Shuffle the deck first
434
tranDeck.shuffle();
435             if(verbose) System.out.println(Thread.currentThread().getName() + " - Loop " + i);
436
437             for (int j = 0; j < TranDeck.TRAN_SIZE; j++)
438             {
439                 // Get next transaction descriptor
440
tranInfo = tranDeck.getNext();
441
442                 try
443                 {
444                     switch (tranInfo.tranType)
445                     {
446                     case TranDeck.TRAN_BUY:
447                         startTime = ResultLog.getCurrentTime();
448                         broker.buyStock(tranInfo.accountNo, tranInfo.stockID, 5);
449                         endTime = ResultLog.getCurrentTime();
450                         _resultLogBuyStock.addSample(endTime - startTime);
451                         break;
452
453                     case TranDeck.TRAN_SELL:
454                         startTime = ResultLog.getCurrentTime();
455                         broker.sellStock(tranInfo.accountNo, tranInfo.stockID, 5);
456                         endTime = ResultLog.getCurrentTime();
457                         _resultLogSellStock.addSample(endTime - startTime);
458                         break;
459
460                     case TranDeck.TRAN_QUERY_ID:
461                         startTime = ResultLog.getCurrentTime();
462                         qr = broker.queryStockValueByID(tranInfo.stockID);
463                         endTime = ResultLog.getCurrentTime();
464                         _resultLogByID.addSample(endTime - startTime);
465                         break;
466
467                     case TranDeck.TRAN_QUERY_CODE:
468                         startTime = ResultLog.getCurrentTime();
469                         qr = broker.queryStockValueByID(tranInfo.stockID);
470                         endTime = ResultLog.getCurrentTime();
471                         _resultLogByID.addSample(endTime - startTime);
472                         break;
473
474                     case TranDeck.TRAN_GETHOLDING:
475                         startTime = ResultLog.getCurrentTime();
476                         list = broker.getHoldingStatement(tranInfo.accountNo, 0);
477                         endTime = ResultLog.getCurrentTime();
478                         _resultLogGetHolding.addSample(endTime - startTime);
479                         break;
480
481                     case TranDeck.TRAN_CREATE:
482                         startTime = ResultLog.getCurrentTime();
483                         accountID = broker.newAccount("myName", "myAddress", 123456);
484                         endTime = ResultLog.getCurrentTime();
485                         _resultLogNewAccount.addSample(endTime - startTime);
486                         break;
487
488                     case TranDeck.TRAN_UPDATE:
489                         startTime = ResultLog.getCurrentTime();
490                         broker.updateAccount(tranInfo.accountNo, tranInfo.stockID*tranInfo.accountNo);
491                         endTime = ResultLog.getCurrentTime();
492                         _resultLogUpdateAccount.addSample(endTime - startTime);
493                         break;
494                     }
495
496                     // To update the performance monitor
497
// monTPS.incCount(Monitor.COUNT_TYPE.CNTTYPE_RAW, 1);
498
// monTPS.incCount(Monitor.COUNT_TYPE.CNTTYPE_PERSEC, 1);
499

500                     // To give other threads a chance to run
501
// Thread.yield();
502
}
503                 catch (Exception JavaDoc e)
504                 {
505                     // We should record number of exceptions here.
506
//
507
e.printStackTrace();
508                 } // end of try
509
} // end of loop j
510
} // end of loop i
511

512         timer.stop();
513
514         // For cool-dowm
515
for (int i = 0; i < _warmupIteration; i++)
516         {
517             // Shuffle the deck first
518
tranDeck.shuffle();
519             if(verbose) System.out.println(Thread.currentThread().getName() + " - Loop " + i);
520
521             for (int j = 0; j < TranDeck.TRAN_SIZE; j++)
522             {
523                 // Get next transaction descriptor
524
tranInfo = tranDeck.getNext();
525
526                 try
527                 {
528                     switch (tranInfo.tranType)
529                     {
530                     case TranDeck.TRAN_BUY:
531                         startTime = ResultLog.getCurrentTime();
532                         broker.buyStock(tranInfo.accountNo, tranInfo.stockID, 5);
533                         endTime = ResultLog.getCurrentTime();
534                         // _resultLogBuyStock.addSample(endTime - startTime);
535
break;
536
537                     case TranDeck.TRAN_SELL:
538                         startTime = ResultLog.getCurrentTime();
539                         broker.sellStock(tranInfo.accountNo, tranInfo.stockID, 5);
540                         endTime = ResultLog.getCurrentTime();
541                         // _resultLogSellStock.addSample(endTime - startTime);
542
break;
543
544                     case TranDeck.TRAN_QUERY_ID:
545                         startTime = ResultLog.getCurrentTime();
546                         qr = broker.queryStockValueByID(tranInfo.stockID);
547                         endTime = ResultLog.getCurrentTime();
548                         // _resultLogByID.addSample(endTime - startTime);
549
break;
550
551                     case TranDeck.TRAN_QUERY_CODE:
552                         startTime = ResultLog.getCurrentTime();
553                         qr = broker.queryStockValueByID(tranInfo.stockID);
554                         endTime = ResultLog.getCurrentTime();
555                         // _resultLogByID.addSample(endTime - startTime);
556
break;
557
558                     case TranDeck.TRAN_GETHOLDING:
559                         startTime = ResultLog.getCurrentTime();
560                         list = broker.getHoldingStatement(tranInfo.accountNo, 0);
561                         endTime = ResultLog.getCurrentTime();
562                         // _resultLogGetHolding.addSample(endTime - startTime);
563
break;
564
565                     case TranDeck.TRAN_CREATE:
566                         startTime = ResultLog.getCurrentTime();
567                         accountID = broker.newAccount("myName", "myAddress", 123456);
568                         endTime = ResultLog.getCurrentTime();
569                         // _resultLogNewAccount.addSample(endTime - startTime);
570
break;
571
572                     case TranDeck.TRAN_UPDATE:
573                         startTime = ResultLog.getCurrentTime();
574                         broker.updateAccount(tranInfo.accountNo, tranInfo.stockID*tranInfo.accountNo);
575                         endTime = ResultLog.getCurrentTime();
576                         // _resultLogUpdateAccount.addSample(endTime - startTime);
577
break;
578                     }
579
580                     // To update the performance monitor
581
// monTPS.incCount(Monitor.COUNT_TYPE.CNTTYPE_RAW, 1);
582
// monTPS.incCount(Monitor.COUNT_TYPE.CNTTYPE_PERSEC, 1);
583

584                     // To give other threads a chance to run
585
Thread.yield();
586                 }
587                 catch (Exception JavaDoc e)
588                 {
589                     // We should record number of exceptions here.
590
//
591
e.printStackTrace();
592                 } // end of try
593
} // end of loop j
594
} // end of loop i
595

596         // To remove the bean after testing
597
//
598
try
599         {
600             broker.remove();
601         }
602         catch (Exception JavaDoc e)
603         {
604             System.err.println("fail to remove the bean, due to: " + e.toString());
605             e.printStackTrace();
606
607         }
608
609         // To calculate TPS for this thread
610
//
611
long numTrans = testIteration*43;
612         long TPS = 1000*numTrans/timer.getTime();
613         _resultLogTPS.addSample(TPS);
614
615         // Add the result to global
616
//
617
resultLogByID.add(_resultLogByID);
618         resultLogNewAccount.add(_resultLogNewAccount);
619         resultLogBuyStock.add(_resultLogBuyStock);
620         resultLogUpdateAccount.add(_resultLogUpdateAccount);
621         resultLogSellStock.add(_resultLogSellStock);
622         resultLogGetHolding.add(_resultLogGetHolding);
623         resultLogTPS.add(_resultLogTPS);
624
625         return;
626     }
627     
628     // To process args
629
//
630
private static void args_proc(String JavaDoc[] args)
631     {
632         if (args.length < 1)
633         {
634             System.err.println("Usage: java stockonline.client.Client -test type -env run.properties [-log testResult.log] -param P1 [P2] [3] [P4]");
635             System.err.println("where: type = f | p");
636             System.err.println(" run.properties = name of the file where testing environment properties are set");
637             System.err.println(" testResult.log = name of the file where test results will be put into, only for performance test");
638             System.err.println(" P1 depending on test type:");
639             System.err.println(" if type = p, P1 = numner of threads, no [P2] [3] and [P4] are required.");
640             System.err.println(" if type = f, P1 = create | query | buy | sell | holding | update | resetdb | testrollback");
641             System.err.println(" [P2] [P3] [P4] are optional, depending on P1:");
642             System.err.println(" if P1 = create, P2 = name, P3 = address and P4 = credit");
643             System.err.println(" if P1 = update, P2 = accountID and P3 = credit");
644             System.err.println(" if P1 = holding, P2 = accountID");
645             System.err.println(" if P1 = query, P2 = stockID (1-3000)");
646             System.err.println(" if P1 = buy | sell, P2 = accountID, P3 = stockID and P4 = amount ");
647             System.err.println(" if P1 = resetDB | testrollback, no [P2] [3] and [P4] are required. ");
648             System.err.println(" Note: For all funcational test, [P2] [3] and [P4] are optional. ");
649             System.err.println(" This means valid default parameters can be applied if they are not provided.");
650
651             System.exit(1);
652         }
653
654         try
655         {
656             String JavaDoc key = null;
657             int k = 2;
658
659             for(int i=0; i<args.length; i += k)
660             {
661                 if(args[i].equalsIgnoreCase("-test") || args[i].equalsIgnoreCase("-t"))
662                 {
663                     key = "client.test.type";
664                     if(verbose) System.out.println(key + " = " + args[i+1]);
665                     runProperties.setProperty(key, args[i+1]);
666                 }
667                 else if(args[i].equalsIgnoreCase("-env") || args[i].equalsIgnoreCase("-e"))
668                 {
669                     if(verbose) System.out.println("test properties fileName = " + args[i+1]);
670                     java.io.FileInputStream JavaDoc in = new java.io.FileInputStream JavaDoc(args[i+1]);
671                     runProperties.load(in);
672                     if(verbose) runProperties.list( System.out );
673                 }
674                 else if(args[i].equalsIgnoreCase("-log") || args[i].equalsIgnoreCase("-l"))
675                 {
676                     key = "client.logfile";
677                     if(verbose) System.out.println(key + " = " + args[i+1]);
678                     runProperties.setProperty(key, args[i+1]);
679                 }
680                 else if(args[i].equalsIgnoreCase("-param") || args[i].equalsIgnoreCase("-p"))
681                 {
682                     for(k=1; k<args.length - i; k++)
683                     {
684                         key = "client.test.P" + k;
685                         if(verbose) System.out.println(key + " = " + args[i+k]);
686                         runProperties.setProperty(key, args[i+k]);
687                     }
688                 }
689             }
690         }
691         catch(java.io.FileNotFoundException JavaDoc e1)
692         {
693             e1.printStackTrace();
694             System.exit(1);
695         }
696         catch(java.io.IOException JavaDoc e2)
697         {
698             e2.printStackTrace();
699             System.exit(1);
700         }
701         catch(Exception JavaDoc e3)
702         {
703             e3.printStackTrace();
704             System.exit(1);
705         }
706     }
707
708     private static void functionTest_proc(BrokerHome myHome)
709     {
710         System.out.println("\n To perform functional Test:\n");
711
712         String JavaDoc testName = runProperties.getProperty("client.test.P1");
713             System.out.println("To test: " + testName);
714
715             try
716         {
717                 System.out.println("To create a broker bean instance");
718             Broker broker = myHome.create();
719
720             if ( testName.equalsIgnoreCase("create") )
721             {
722                 String JavaDoc name = runProperties.getProperty("client.test.P2", "test");
723                 String JavaDoc addr = runProperties.getProperty("client.test.P3", "CMIS");
724                 int credit = Integer.parseInt(runProperties.getProperty("P4", "10000"));
725
726                 System.out.println("\t name = " + name);
727                 System.out.println("\t addr = " + addr);
728                 System.out.println("\t credit = " + credit);
729  
730                 int accountID = broker.newAccount(name, addr, credit);
731                 System.out.println("the new accountID = " + accountID);
732             }
733             else if ( testName.equalsIgnoreCase("update") )
734             {
735                 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1"));
736                 int credit = Integer.parseInt(runProperties.getProperty("client.test.P3", "10000"));
737                 System.out.println("\t accounID = " + accountID);
738                 System.out.println("\t credit = " + credit);
739
740                 broker.updateAccount(accountID, credit);
741                 System.out.println("........................OK");
742             }
743             else if ( testName.equalsIgnoreCase("query") )
744             {
745                 int stockID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1"));
746                     System.out.println("\t stockID = " + stockID);
747
748                 QueryResult rs = (QueryResult) broker.queryStockValueByID(stockID);
749
750                 System.out.println("current_val = " + rs.current_val);
751                 System.out.println(" high_val = " + rs.high_val);
752                 System.out.println(" low_val = " + rs.low_val);
753             }
754             else if ( testName.equalsIgnoreCase("buy") )
755             {
756                 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1"));
757                 int stockID = Integer.parseInt(runProperties.getProperty("client.test.P3", "10"));
758                 int amount = Integer.parseInt(runProperties.getProperty("client.test.P4", "10"));
759     
760                 System.out.println("\t accountID = " + accountID);
761                 System.out.println("\t stockID = " + stockID);
762                 System.out.println("\t amount = " + amount);
763
764                 broker.buyStock(accountID, stockID, amount);
765                 System.out.println("Buy.........................OK");
766             }
767             else if ( testName.equalsIgnoreCase("sell") )
768             {
769                 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1"));
770                 int stockID = Integer.parseInt(runProperties.getProperty("client.test.P3", "10"));
771                 int amount = Integer.parseInt(runProperties.getProperty("client.test.P4", "10"));
772     
773                 System.out.println("\t accountID = " + accountID);
774                 System.out.println("\t stockID = " + stockID);
775                 System.out.println("\t amount = " + amount);
776
777                 broker.sellStock(accountID, stockID, amount);
778                 System.out.println("Sell........................OK");
779             }
780             else if ( testName.equalsIgnoreCase("holding") )
781             {
782                 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1"));
783                 System.out.println("\t accountID = " + accountID);
784
785                 Collection list = broker.getHoldingStatement(accountID, 0);
786                 Iterator iterator = list.iterator();
787
788                 for(int i=0; iterator.hasNext(); i++)
789                 {
790                     Holding hold = (Holding) iterator.next();
791                     System.out.println("stockID = " + hold.stock_id + " amount = " + hold.amount);
792                 }
793                 }
794             else if ( testName.equalsIgnoreCase("testrollback") )
795             {
796                 int accountID = broker.newAccountForTestRollback("test", "rollback", 8888);
797                 System.out.println("accountID = " + accountID);
798
799                 if(broker.testRollback(accountID))
800                         System.out.println("Rollback................OK");
801                 else
802                         System.out.println("Rollback................Failed");
803                 }
804             else
805             {
806                 throw new Exception JavaDoc("Invalid test name. Run the program with no args to get help.");
807             }
808
809             // To remove the bean after testing
810
broker.remove();
811         }
812         catch (Exception JavaDoc ex)
813         {
814             System.err.println(ex.toString());
815             }
816         
817         System.exit(0);
818     }
819
820     private static void sleep(int inval)
821         {
822         try
823         {
824             Thread.sleep(inval);
825         } catch(Exception JavaDoc ex1) {}
826     }
827 }
828
Popular Tags