KickJava   Java API By Example, From Geeks To Geeks.

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


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.naming.InitialContext JavaDoc;
29 import javax.rmi.PortableRemoteObject JavaDoc;
30 import javax.transaction.RollbackException JavaDoc;
31 import javax.transaction.TransactionRolledbackException JavaDoc;
32 import javax.transaction.UserTransaction JavaDoc;
33
34 import junit.framework.Test;
35
36 public class T04MixedOTSDTMUnitTestCase
37 extends JBossTestCase
38 {
39    // Attributes ----------------------------------------------------
40
private java.util.Properties JavaDoc jndiProps;
41    
42    // Constructors --------------------------------------------------
43
public T04MixedOTSDTMUnitTestCase(String JavaDoc name)
44       throws java.io.IOException JavaDoc
45    {
46       super(name);
47       java.net.URL JavaDoc url
48           = ClassLoader.getSystemResource("host0.cosnaming.jndi.properties");
49       jndiProps = new java.util.Properties JavaDoc();
50       jndiProps.load(url.openStream());
51    }
52
53    // Override getInitialContext() ----------------------------------
54
protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
55    {
56       return new InitialContext JavaDoc(jndiProps);
57    }
58
59    // Public --------------------------------------------------------
60

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