KickJava   Java API By Example, From Geeks To Geeks.

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


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