KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > dtm > test > T09DTMXAUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.dtm.test;
23
24 import org.jboss.test.JBossTestCase;
25 import org.jboss.test.dtm.interfaces.FrontEnd;
26 import org.jboss.test.dtm.interfaces.FrontEndHome;
27 import javax.naming.Context JavaDoc;
28 import javax.rmi.PortableRemoteObject JavaDoc;
29 import javax.transaction.RollbackException JavaDoc;
30 import javax.transaction.TransactionRolledbackException JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
32
33 import junit.framework.Test;
34
35 public class T09DTMXAUnitTestCase
36 extends JBossTestCase
37 {
38    public T09DTMXAUnitTestCase(String JavaDoc name)
39       throws java.io.IOException JavaDoc
40    {
41       super(name);
42    }
43
44    // Public --------------------------------------------------------
45

46    public void testCommittedTx()
47       throws Exception JavaDoc
48    {
49       getLog().debug("+++ testCommittedTx");
50
51       Context JavaDoc ctx = getInitialContext();
52
53       getLog().debug("Obtain UserTransaction instance");
54       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
55    
56       getLog().debug("Obtain home interface");
57       Object JavaDoc objref = ctx.lookup("dtmtest/FrontEndEJB");
58       FrontEndHome home =
59          (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class);
60       
61       getLog().debug("Create FrontEnd bean");
62       FrontEnd frontEnd = home.create("testCommittedTx");
63
64       getLog().debug("Set balances");
65       userTx.begin();
66       frontEnd.setBalances(101, 102, 103);
67       userTx.commit();
68    
69       int balances[];
70
71       getLog().debug("Get balances");
72       userTx.begin();
73       balances = frontEnd.getBalances();
74       userTx.commit();
75    
76       assertTrue("first balance == 101", balances[0] == 101);
77       assertTrue("second balance == 102", balances[1] == 102);
78       assertTrue("third balance == 103", balances[2] == 103);
79    
80       getLog().debug("Update balances");
81       userTx.begin();
82       frontEnd.addToBalances(100);
83       userTx.commit();
84    
85       getLog().debug("Check updated balances");
86       userTx.begin();
87       balances = frontEnd.getBalances();
88       userTx.commit();
89    
90       assertTrue("first balance == 201", balances[0] == 201);
91       assertTrue("second balance == 202", balances[1] == 202);
92       assertTrue("third balance == 203", balances[2] == 203);
93    
94       getLog().debug("Remove FrontEnd bean");
95       frontEnd.remove();
96    }
97
98    public void testRolledbackTx()
99       throws Exception JavaDoc
100    {
101       getLog().debug("+++ testRolledbackTx");
102
103       Context JavaDoc ctx = getInitialContext();
104
105       getLog().debug("Obtain UserTransaction instance");
106       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
107    
108       getLog().debug("Obtain home interface");
109       Object JavaDoc objref = ctx.lookup("dtmtest/FrontEndEJB");
110       FrontEndHome home =
111          (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class);
112
113       getLog().debug("Create FrontEnd bean");
114       FrontEnd frontEnd = home.create("testRolledbackTx");
115
116       getLog().debug("Set balances");
117       userTx.begin();
118       frontEnd.setBalances(101, 102, 103);
119       userTx.commit();
120    
121       int balances[];
122
123       getLog().debug("Get balances");
124       userTx.begin();
125       balances = frontEnd.getBalances();
126       userTx.commit();
127    
128       assertTrue("first balance == 101", balances[0] == 101);
129       assertTrue("second balance == 102", balances[1] == 102);
130       assertTrue("third balance == 103", balances[2] == 103);
131    
132       getLog().debug("Update balances and rollback");
133       userTx.begin();
134       frontEnd.addToBalances(100);
135       userTx.rollback();
136
137       getLog().debug("Check that all balances remain the same");
138       userTx.begin();
139       balances = frontEnd.getBalances();
140       userTx.commit();
141    
142       assertTrue("first balance == 101", balances[0] == 101);
143       assertTrue("second balance == 102", balances[1] == 102);
144       assertTrue("third balance == 103", balances[2] == 103);
145    
146       frontEnd.remove();
147    }
148
149    public void testSetRollbackOnly()
150       throws Exception JavaDoc
151    {
152       getLog().debug("+++ testSetRollbackOnly");
153
154       Context JavaDoc ctx = getInitialContext();
155
156       getLog().debug("Obtain UserTransaction instance");
157       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
158       
159       getLog().debug("Obtain home interface");
160       Object JavaDoc objref = ctx.lookup("dtmtest/FrontEndEJB");
161       FrontEndHome home =
162          (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class);
163
164       getLog().debug("Create FrontEnd bean");
165       FrontEnd frontEnd = home.create("testSetRollbackOnly");
166
167       getLog().debug("Set balances");
168       userTx.begin();
169       frontEnd.setBalances(101, 102, 103);
170       userTx.commit();
171       
172       int balances[];
173
174       getLog().debug("Get balances");
175       userTx.begin();
176       balances = frontEnd.getBalances();
177       userTx.commit();
178       
179       assertTrue("first balance == 101", balances[0] == 101);
180       assertTrue("second balance == 102", balances[1] == 102);
181       assertTrue("third balance == 103", balances[2] == 103);
182       
183       getLog().debug("Update balances, set rollback only and try to commit");
184       try
185       {
186          userTx.begin();
187          frontEnd.addToBalances(100);
188          userTx.setRollbackOnly();
189          userTx.commit();
190          fail("RollbackException should have been thrown");
191       }
192       catch (RollbackException JavaDoc e)
193       {
194          getLog().debug("Expected exception: " + e);
195       }
196
197       getLog().debug("Check that all balances remain the same");
198       userTx.begin();
199       balances = frontEnd.getBalances();
200       userTx.commit();
201       
202       assertTrue("first balance == 101", balances[0] == 101);
203       assertTrue("second balance == 102", balances[1] == 102);
204       assertTrue("third balance == 103", balances[2] == 103);
205       
206       getLog().debug("Update balances, set rollback only and try to commit");
207       try
208       {
209          userTx.begin();
210          frontEnd.addToBalances(100);
211          userTx.setRollbackOnly();
212          frontEnd.addToBalances(50);
213          userTx.commit();
214          fail("RollbackException should have been thrown");
215       }
216       catch (RollbackException JavaDoc e)
217       {
218          getLog().debug("Expected exception: " + e);
219       }
220
221       getLog().debug("Check that all balances remain the same");
222       userTx.begin();
223       balances = frontEnd.getBalances();
224       userTx.commit();
225       
226       assertTrue("first balance == 101", balances[0] == 101);
227       assertTrue("second balance == 102", balances[1] == 102);
228       assertTrue("third balance == 103", balances[2] == 103);
229       
230       frontEnd.remove();
231    }
232
233    public void testSetRollbackOnlyAtTheFrontEnd()
234       throws Exception JavaDoc
235       {
236       getLog().debug("+++ testSetRollbackOnlyAtTheFrontEnd");
237
238       Context JavaDoc ctx = getInitialContext();
239
240       getLog().debug("Obtain UserTransaction instance");
241       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
242       
243       getLog().debug("Obtain home interface");
244       Object JavaDoc objref = ctx.lookup("dtmtest/FrontEndEJB");
245       FrontEndHome home =
246          (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class);
247
248       getLog().debug("Create FrontEnd bean");
249       FrontEnd frontEnd = home.create("testSetRollbackOnlyAtTheFrontEnd");
250
251       getLog().debug("Set balances");
252       userTx.begin();
253       frontEnd.setBalances(101, 102, 103);
254       userTx.commit();
255    
256       int balances[];
257
258       getLog().debug("Get balances");
259       userTx.begin();
260       balances = frontEnd.getBalances();
261       userTx.commit();
262    
263       assertTrue("first balance == 101", balances[0] == 101);
264       assertTrue("second balance == 102", balances[1] == 102);
265       assertTrue("third balance == 103", balances[2] == 103);
266    
267       getLog().debug("Update balances, set rollback only at the front end, " +
268                      "and try to commit");
269       try
270       {
271          userTx.begin();
272          frontEnd.addToBalances(100);
273          frontEnd.setRollbackOnly();
274          frontEnd.addToBalances(50);
275          userTx.commit();
276          fail("RollbackException should have been thrown");
277       }
278       catch (RollbackException JavaDoc e)
279       {
280          getLog().debug("Expected exception: " + e);
281       }
282
283       getLog().debug("Check that all balances remain the same");
284       userTx.begin();
285       balances = frontEnd.getBalances();
286       userTx.commit();
287       
288       assertTrue("first balance == 101", balances[0] == 101);
289       assertTrue("second balance == 102", balances[1] == 102);
290       assertTrue("third balance == 103", balances[2] == 103);
291    
292       frontEnd.remove();
293       }
294
295    public void testSetRollbackOnlyAtTheFirstAccount()
296       throws Exception JavaDoc
297    {
298       getLog().debug("+++ testSetRollbackOnlyAtTheFirstAccount");
299
300       Context JavaDoc ctx = getInitialContext();
301
302       getLog().debug("Obtain UserTransaction instance");
303       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
304       
305       getLog().debug("Obtain home interface");
306       Object JavaDoc objref = ctx.lookup("dtmtest/FrontEndEJB");
307       FrontEndHome home =
308          (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class);
309
310       getLog().debug("Create FrontEnd bean");
311       FrontEnd frontEnd = home.create("testSetRollbackOnlyAtTheFirstAccount");
312
313       getLog().debug("Set balances");
314       userTx.begin();
315       frontEnd.setBalances(101, 102, 103);
316       userTx.commit();
317       
318       int balances[];
319
320       getLog().debug("Get balances");
321       userTx.begin();
322       balances = frontEnd.getBalances();
323       userTx.commit();
324       
325       assertTrue("first balance == 101", balances[0] == 101);
326       assertTrue("second balance == 102", balances[1] == 102);
327       assertTrue("third balance == 103", balances[2] == 103);
328       
329       getLog().debug("Update balances, set rollback only at " +
330                      "the first account, and try to commit");
331       try
332       {
333          userTx.begin();
334          frontEnd.addToBalances(100);
335          frontEnd.tellFirstAccountToSetRollbackOnly();
336          frontEnd.addToBalances(50);
337          userTx.commit();
338          fail("TransactionRolledbackException should have been thrown");
339       }
340       catch (TransactionRolledbackException JavaDoc e)
341       {
342          getLog().debug("Expected exception: " + e);
343
344      // dissociate this thread from the transaction
345
// that has already been rolled back
346
userTx.rollback();
347
348      // recreate the FrontEnd session instance, which was destroyed
349
// by the TransactionRolledbackException at the server side
350
frontEnd = home.create("testSetRollbackOnlyAtTheFirstAccount", true);
351       }
352
353       getLog().debug("Check that all balances remain the same");
354       userTx.begin();
355       balances = frontEnd.getBalances();
356       userTx.commit();
357       
358       assertTrue("first balance == 101", balances[0] == 101);
359       assertTrue("second balance == 102", balances[1] == 102);
360       assertTrue("third balance == 103", balances[2] == 103);
361       
362       frontEnd.remove();
363    }
364
365    public static Test suite() throws Exception JavaDoc
366    {
367       java.net.URL JavaDoc url
368           = ClassLoader.getSystemResource("host0.jndi.properties");
369       java.util.Properties JavaDoc host0JndiProps = new java.util.Properties JavaDoc();
370       host0JndiProps.load(url.openStream());
371
372       java.util.Properties JavaDoc systemProps = System.getProperties();
373       systemProps.putAll(host0JndiProps);
374       System.setProperties(systemProps);
375
376       return getDeploySetup(T09DTMXAUnitTestCase.class,
377                             "dtmfrontend2entities.jar");
378    }
379
380 }
381
Popular Tags