KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 T06OTSInterpositionUnitTestCase
37 extends JBossTestCase
38 {
39    // Attributes ----------------------------------------------------
40
private java.util.Properties JavaDoc jndiProps;
41    
42    public T06OTSInterpositionUnitTestCase(String JavaDoc name)
43       throws java.io.IOException JavaDoc
44    {
45       super(name);
46       java.net.URL JavaDoc url
47           = ClassLoader.getSystemResource("host0.cosnaming.jndi.properties");
48       jndiProps = new java.util.Properties JavaDoc();
49       jndiProps.load(url.openStream());
50    }
51
52    // Override getInitialContext() ----------------------------------
53
protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
54    {
55       return new InitialContext JavaDoc(jndiProps);
56    }
57
58    // Public --------------------------------------------------------
59

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