KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > interfaces > UserTransactionTester


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.cts.interfaces;
23
24
25 import javax.ejb.*;
26
27 import javax.transaction.Status JavaDoc;
28 import javax.transaction.UserTransaction JavaDoc;
29 import javax.transaction.RollbackException JavaDoc;
30
31 import org.jboss.test.cts.keys.AccountPK;
32
33 /**
34  * A utility class for testing UserTransaction in a stand-alone
35  * client, or a BMT enterprise bean.
36  * This is not part of any interface, just shared code used in
37  * client and server.
38  * This does not depend on JUnit, as JUnit is not available on the server.
39  *
40  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
41  * @version $Revision: 58115 $
42  */

43 public class UserTransactionTester
44 {
45    static org.jboss.logging.Logger log =
46       org.jboss.logging.Logger.getLogger(UserTransactionTester.class);
47
48    /**
49     * Home of entity used as a resource for testing.
50     */

51    private CtsBmpHome home;
52
53    /**
54     * UserTransaction to test.
55     */

56    private UserTransaction JavaDoc ut;
57
58    /**
59     * First resource for testing.
60     */

61    private CtsBmp bean1;
62
63    /**
64     * Second resource for testing.
65     */

66    private CtsBmp bean2;
67
68    /**
69     * Create a new UserTransaction test instance.
70     */

71    public UserTransactionTester(CtsBmpHome home,
72                                 UserTransaction JavaDoc userTransaction)
73    {
74       this.home = home;
75       this.ut = userTransaction;
76    }
77
78    /**
79     * Run all the UserTransaction tests.
80     */

81    public boolean runAllTests()
82    {
83       // No resource tests
84
if (!testBeginRollback())
85          return false;
86       if (!testBeginCommit())
87          return false;
88       if (!testBeginSetrollbackonlyRollback())
89          return false;
90       if (!testBeginSetrollbackonlyCommit())
91          return false;
92
93       // Create first instance
94
try {
95          bean1 = home.create(new AccountPK("UT_TestBean1"), "Ole1");
96       } catch (Exception JavaDoc ex) {
97          log.debug("failed", ex);
98          return false;
99       }
100
101       // Single resource tests
102
if (!testSingleRollback())
103          return false;
104       if (!testSingleCommit())
105          return false;
106       if (!testSingleSetrollbackonlyCommit())
107          return false;
108
109       // Can second instance be created in a tx that is rolled back?
110
try {
111          ut.begin();
112          bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2");
113          ut.rollback();
114          
115          // Should no longer exist
116
boolean gotException = false;
117          try {
118             bean2.setPersonsName("Ole");
119          } catch (Exception JavaDoc e) {
120        log.info("IGNORE PREVIOUS NoSuchEntityException - it is intentional");
121             gotException = true;
122          }
123          if (!gotException)
124             throw new RuntimeException JavaDoc("Rollback didn't rollback create.");
125       } catch (Exception JavaDoc ex) {
126          log.debug("failed", ex);
127          return false;
128       }
129
130       // Create second instance
131
try {
132          bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2");
133       } catch (Exception JavaDoc ex) {
134          log.debug("failed", ex);
135          return false;
136       }
137
138       return true;
139    }
140
141    //
142
// No resource tests.
143
//
144

145    /**
146     * Simple begin/rollback test.
147     */

148    private boolean testBeginRollback()
149    {
150       try {
151          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
152             throw new RuntimeException JavaDoc("No tx should be active.");
153
154          ut.begin();
155          if (ut.getStatus() != Status.STATUS_ACTIVE)
156             throw new RuntimeException JavaDoc("New tx not active.");
157          ut.rollback();
158
159          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
160             throw new RuntimeException JavaDoc("No tx should be active.");
161       } catch (Exception JavaDoc ex) {
162          log.debug("failed", ex);
163          return false;
164       }
165       return true;
166    }
167
168    /**
169     * Simple begin/commit test.
170     */

171    private boolean testBeginCommit()
172    {
173       try {
174          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
175             throw new RuntimeException JavaDoc("No tx should be active.");
176
177          ut.begin();
178          if (ut.getStatus() != Status.STATUS_ACTIVE)
179             throw new RuntimeException JavaDoc("New tx not active.");
180          ut.commit();
181
182          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
183             throw new RuntimeException JavaDoc("No tx should be active.");
184       } catch (Exception JavaDoc ex) {
185          log.debug("failed", ex);
186          return false;
187       }
188       return true;
189    }
190
191    /**
192     * Simple begin/setRollbackOnly/rollback test.
193     */

194    private boolean testBeginSetrollbackonlyRollback()
195    {
196       try {
197          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
198             throw new RuntimeException JavaDoc("No tx should be active.");
199
200          ut.begin();
201          if (ut.getStatus() != Status.STATUS_ACTIVE)
202             throw new RuntimeException JavaDoc("New tx not active.");
203          ut.setRollbackOnly();
204          if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
205             throw new RuntimeException JavaDoc("Tx not marked for rollback.");
206          ut.rollback();
207
208          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
209             throw new RuntimeException JavaDoc("No tx should be active.");
210       } catch (Exception JavaDoc ex) {
211          log.debug("failed", ex);
212          return false;
213       }
214       return true;
215    }
216
217    /**
218     * Simple begin/setRollbackOnly/commit test.
219     */

220    private boolean testBeginSetrollbackonlyCommit()
221    {
222       try {
223          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
224             throw new RuntimeException JavaDoc("No tx should be active.");
225
226          ut.begin();
227          if (ut.getStatus() != Status.STATUS_ACTIVE)
228             throw new RuntimeException JavaDoc("New tx not active.");
229          ut.setRollbackOnly();
230          if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
231             throw new RuntimeException JavaDoc("Tx not marked for rollback.");
232
233          boolean gotException = false;
234          try {
235             ut.commit();
236          } catch (RollbackException JavaDoc rbe) {
237             gotException = true;
238          }
239          if (!gotException)
240             throw new RuntimeException JavaDoc("Didn't get expected RollbackException.");
241
242          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
243             throw new RuntimeException JavaDoc("No tx should be active.");
244       } catch (Exception JavaDoc ex) {
245          log.debug("failed", ex);
246          return false;
247       }
248       return true;
249    }
250
251
252    //
253
// Single resource tests.
254
//
255

256    /**
257     * Tests if a rollback really rolls back.
258     */

259    private boolean testSingleRollback()
260    {
261       try {
262          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
263             throw new RuntimeException JavaDoc("No tx should be active.");
264
265          bean1.setPersonsName("Ole");
266          if (!bean1.getPersonsName().equals("Ole"))
267             throw new RuntimeException JavaDoc("Unable to set property.");
268
269          ut.begin();
270          if (ut.getStatus() != Status.STATUS_ACTIVE)
271             throw new RuntimeException JavaDoc("New tx not active.");
272          if (!bean1.getPersonsName().equals("Ole"))
273             throw new RuntimeException JavaDoc("Property changes after begin.");
274
275          bean1.setPersonsName("Peter");
276          if (!bean1.getPersonsName().equals("Peter"))
277              throw new RuntimeException JavaDoc("Unable to set property.");
278
279          ut.rollback();
280          if (!bean1.getPersonsName().equals("Ole"))
281              throw new RuntimeException JavaDoc("Rollback doesn't work.");
282
283
284          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
285             throw new RuntimeException JavaDoc("No tx should be active.");
286       } catch (Exception JavaDoc ex) {
287          log.debug("failed", ex);
288          return false;
289       }
290       return true;
291    }
292
293    /**
294     * Tests if a commit really commits.
295     */

296    private boolean testSingleCommit()
297    {
298       try {
299          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
300             throw new RuntimeException JavaDoc("No tx should be active.");
301
302          bean1.setPersonsName("Ole");
303          if (!bean1.getPersonsName().equals("Ole"))
304             throw new RuntimeException JavaDoc("Unable to set property.");
305
306          ut.begin();
307          if (ut.getStatus() != Status.STATUS_ACTIVE)
308             throw new RuntimeException JavaDoc("New tx not active.");
309          if (!bean1.getPersonsName().equals("Ole"))
310             throw new RuntimeException JavaDoc("Property changes after begin.");
311
312          bean1.setPersonsName("Peter");
313          if (!bean1.getPersonsName().equals("Peter"))
314              throw new RuntimeException JavaDoc("Unable to set property.");
315
316          ut.commit();
317          if (!bean1.getPersonsName().equals("Peter"))
318              throw new RuntimeException JavaDoc("Property not set after commit.");
319
320
321          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
322             throw new RuntimeException JavaDoc("No tx should be active.");
323       } catch (Exception JavaDoc ex) {
324          log.debug("failed", ex);
325          return false;
326       }
327       return true;
328    }
329
330    /**
331     * Tests if a setRollbackOnly really makes the transaction rollback.
332     */

333    private boolean testSingleSetrollbackonlyCommit()
334    {
335       try {
336          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
337             throw new RuntimeException JavaDoc("No tx should be active.");
338
339          bean1.setPersonsName("Ole");
340          if (!bean1.getPersonsName().equals("Ole"))
341             throw new RuntimeException JavaDoc("Unable to set property.");
342
343          ut.begin();
344          if (ut.getStatus() != Status.STATUS_ACTIVE)
345             throw new RuntimeException JavaDoc("New tx not active.");
346          if (!bean1.getPersonsName().equals("Ole"))
347             throw new RuntimeException JavaDoc("Property changes after begin.");
348
349          bean1.setPersonsName("Peter");
350          if (!bean1.getPersonsName().equals("Peter"))
351              throw new RuntimeException JavaDoc("Unable to set property.");
352
353          ut.setRollbackOnly();
354          if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
355             throw new RuntimeException JavaDoc("Tx not marked for rollback.");
356
357          boolean gotException = false;
358          try {
359             ut.commit();
360          } catch (RollbackException JavaDoc rbe) {
361             gotException = true;
362          }
363          if (!gotException)
364             throw new RuntimeException JavaDoc("Didn't get expected RollbackException.");
365
366          if (!bean1.getPersonsName().equals("Ole"))
367              throw new RuntimeException JavaDoc("Didn't roll back.");
368
369          if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
370             throw new RuntimeException JavaDoc("No tx should be active.");
371       } catch (Exception JavaDoc ex) {
372          log.debug("failed", ex);
373          return false;
374       }
375       return true;
376    }
377 }
378
Popular Tags