KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > tutorial > atm > AtmTest


1 package org.jbpm.bpel.tutorial.atm;
2
3 import java.util.Properties JavaDoc;
4
5 import javax.naming.Context JavaDoc;
6 import javax.naming.InitialContext JavaDoc;
7 import javax.naming.NamingException JavaDoc;
8
9 import junit.framework.TestCase;
10
11 import org.jbpm.bpel.tutorial.atm.types.OperationRequestType;
12
13 /**
14  * @author Juan Cantu
15  * @version $Revision: 1.2 $ $Date: 2005/06/23 20:27:18 $
16  */

17 public class AtmTest extends TestCase {
18   
19   private Atm atm;
20
21   public AtmTest(String JavaDoc name) {
22     super(name);
23   }
24   
25   protected void setUp() throws Exception JavaDoc {
26     Context JavaDoc initialContext = getInitialContext();
27     // lookup the service interface in the environment context
28
AtmService service = (AtmService) initialContext.lookup("java:comp/env/service/ATM");
29     // obtain the dynamic proxy for the web service port
30
atm = service.getAtmPort();
31   }
32   
33   public void testAtmSimpleScenario() throws Exception JavaDoc {
34     // connect to server
35
int sessionId = atm.connect();
36     assertTrue(sessionId > 0);
37     
38     // check the atm is connected
39
String JavaDoc status = atm.status(sessionId);
40     assertEquals("connected", status);
41     
42     // logon as identified customer
43
atm.logon(sessionId, "Pete");
44     
45     /* check the customer is logged on -
46      * logon is an one-way operation, so the status change might not be
47      * reflected immediately */

48     Thread.sleep(500);
49     status = atm.status(sessionId);
50     assertEquals("loggedon", status);
51
52     // get current balance
53
double balance = atm.getBalance("Pete");
54     
55     // deposit some funds
56
OperationRequestType operation = new OperationRequestType();
57     operation.setCustomerName("Pete");
58     operation.setAmount(300);
59     double oldBalance = balance;
60     balance = atm.deposit(operation);
61     // check the new balance is correct
62
assertEquals(oldBalance + 300, balance, 0);
63     
64     // withdraw some funds
65
operation.setAmount(100);
66     oldBalance = balance;
67     balance = atm.withdraw(operation);
68     // check the new balance is correct
69
assertEquals(oldBalance - 100, balance, 0);
70     
71     // logoff
72
atm.logoff("Pete");
73     
74     /* check the customer is logged off -
75      * logon is an one-way operation, so the status change might not be
76      * reflected immediately */

77     Thread.sleep(500);
78     status = atm.status(sessionId);
79     assertEquals("connected", status);
80     
81     // disconnect
82
atm.disconnect(sessionId);
83   }
84   
85   protected InitialContext JavaDoc getInitialContext() throws NamingException JavaDoc {
86     // prepare the environment
87
Properties JavaDoc env = new Properties JavaDoc();
88     // set the JNDI name bound to the client's environment context
89
env.setProperty("j2ee.clientName", "atm-client");
90     // properties in jndi.properties *and* env are placed in the environment
91
return new InitialContext JavaDoc(env);
92   }
93 }
Popular Tags