KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > jdbcra > F_JdbcRATest


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_JdbcRATest.java,v 1.3 2005/05/16 13:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.jdbcra;
27
28 import java.util.Enumeration JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37
38 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA1;
39 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA1Home;
40 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA2;
41 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA2Home;
42 import org.objectweb.jonas.jtests.util.JTestCase;
43
44 /**
45  * JdbcRa tests on Jdbc Resource Adapter
46  */

47
48 public class F_JdbcRATest extends JTestCase {
49
50     private static UserTransaction JavaDoc utx = null;
51
52     static Context JavaDoc ctx = null;
53
54     // Lookup bean home
55

56     protected static String JavaDoc BEAN_HOME1 = "JdbcRA1EBRHome";
57     protected static JdbcRA1Home ra1home = null;
58
59     protected static String JavaDoc BEAN_HOME2 = "JdbcRA2EBRHome";
60     protected static JdbcRA2Home ra2home = null;
61
62     Enumeration JavaDoc ra1list;
63     static JdbcRA1 ra1acc;
64
65     Enumeration JavaDoc ra2list;
66     static JdbcRA2 ra2acc;
67
68     public F_JdbcRATest(String JavaDoc name) {
69         super(name);
70     }
71
72     protected void setUp() {
73         super.setUp();
74
75     // Get InitialContext
76

77         try {
78
79     // get JNDI initial context
80

81         if (ctx == null) {
82         ctx = new InitialContext JavaDoc();
83         }
84
85     // We want to start transactions from client: get UserTransaction
86

87         if (utx == null) {
88         utx = (UserTransaction JavaDoc) ctx.lookup("javax.transaction.UserTransaction");
89         }
90
91         if (ra1home == null && ra2home == null) {
92         useBeans("jdbcra", true);
93         }
94
95     // Connecting to ra1transHome thru JNDI
96

97         if (ra1home == null) {
98         ra1home = (JdbcRA1Home)PortableRemoteObject.narrow(
99                                 ctx.lookup(BEAN_HOME1),
100                                 JdbcRA1Home.class);
101         }
102
103     // Connecting to ra2Home thru JNDI
104

105         if (ra2home == null) {
106         ra2home = (JdbcRA2Home)PortableRemoteObject.narrow(
107                                 ctx.lookup(BEAN_HOME2),
108                                 JdbcRA2Home.class);
109         }
110
111         assertTrue (5 == 5);
112
113     } catch (Exception JavaDoc e) {
114         fail("Cannot lookup UserTransaction in setUp: " +e);
115         }
116     }
117
118     public void testRaCleanUpCommit() {
119
120     // Clean up databases just in case there was a failure
121
// in an earlier execution of these tests
122

123         ra1acc = null;
124         ra2acc = null;
125         int dlaccno;
126
127         try {
128
129         utx.begin();
130
131         // Removing accounts 201-205, 209 from First database
132

133         // Getting the list of existing accounts in First database
134

135         ra1list = ra1home.findAllAccounts();
136
137         while (ra1list.hasMoreElements()) {
138                 ra1acc = (JdbcRA1) ra1list.nextElement();
139                 dlaccno = ra1acc.getNumber();
140
141                 if ((dlaccno >= 201 && dlaccno <= 205) || dlaccno == 209) {
142                     ra1acc.remove();
143                 }
144             }
145
146         // Getting the list of existing accounts in Second database
147

148         ra2list = ra2home.findAllAccounts();
149
150         while (ra2list.hasMoreElements()) {
151                 ra2acc = (JdbcRA2) ra2list.nextElement();
152                 dlaccno = ra2acc.getNumber();
153
154                 if ((dlaccno >= 201 && dlaccno <= 205) || dlaccno == 209) {
155                     ra2acc.remove();
156                 }
157         }
158
159         utx.commit();
160
161             assertTrue (5 == 5);
162
163     } catch (Exception JavaDoc e) {
164             // Not an error, accounts previously removed by last test
165
try {
166                  utx.rollback();
167             }
168             catch (Exception JavaDoc f) {
169                  assertTrue (5 == 5);
170             }
171         assertTrue (5 == 5);
172     }
173     }
174
175     public void testRaBatchInsert() {
176
177     // Store five accounts using executeBatch
178

179         ra1acc = null;
180         ra2acc = null;
181
182         String JavaDoc str1 = null;
183         String JavaDoc str2 = null;
184
185         try {
186
187         utx.begin();
188
189         // Store accounts 201-205 in First database
190

191         ra1acc = ra1home.create();
192
193         // Store accounts 201-205 in Second database
194

195         ra2acc = ra2home.create();
196
197         // Find account 203 created in First database
198

199         ra1acc = ra1home.findByNumber(203);
200
201         // Find account 203 created in Second database
202

203         ra2acc = ra2home.findByNumber(203);
204
205         utx.commit();
206
207             assertEquals (String.valueOf(ra1acc.getBalance()),
208                           String.valueOf(ra2acc.getBalance()));
209
210     } catch (Exception JavaDoc e) {
211
212             try {
213                  utx.rollback();
214              fail("Cannot store accounts using batch" +e);
215
216             }
217             catch (Exception JavaDoc f) {
218                  fail("Cannot rollback testRaBatchInsert: " +f);
219             }
220     }
221     }
222
223     public void testRaSelectAll() {
224
225         try {
226
227         // List existing Accounts
228

229         utx.begin();
230
231         // Getting the list of existing accounts in First database
232

233         ra1list = ra1home.findAllAccounts();
234
235         while (ra1list.hasMoreElements()) {
236                 ra1acc = (JdbcRA1) PortableRemoteObject.narrow(ra1list.nextElement(), JdbcRA1.class);
237             }
238
239         // Getting the list of existing accounts in Second database
240

241         ra2list = ra2home.findAllAccounts();
242
243         while (ra2list.hasMoreElements()) {
244                 ra2acc = (JdbcRA2) PortableRemoteObject.narrow(ra2list.nextElement(), JdbcRA2.class);
245         }
246
247         utx.commit();
248
249             assertTrue (5 == 5);
250
251     } catch (Exception JavaDoc e) {
252
253             try {
254                  utx.rollback();
255                  fail("Cannot retrieve all accounts: " +e);
256             }
257             catch (Exception JavaDoc f) {
258                  fail("Cannot rollback testRaSelectAll: " +f);
259             }
260     }
261     }
262
263     public void testRaSelectOne() {
264
265     // Find a second Account
266

267     ra1acc = null;
268         ra2acc = null;
269
270         try {
271
272         // Finding an Account 202 in First database
273

274         ra1acc = ra1home.findByNumber(202);
275
276         // Finding an Account 202 in Second database
277

278         ra2acc = ra2home.findByNumber(202);
279
280             assertEquals (ra1acc.getNumber(), ra2acc.getNumber());
281
282     } catch (Exception JavaDoc e) {
283         fail("Cannot find account 202 " +e);
284     }
285     }
286
287     public void testRaUpdateCommit() {
288
289     // First transaction (committed):
290
// transfert 100 from ra1acc to ra2acc
291

292     // Starting a first transaction, that will be committed
293

294         String JavaDoc str1 = null;
295         String JavaDoc str2 = null;
296
297     double value = 100;
298
299         try {
300
301         utx.begin();
302
303         // Finding an Account 202 in First database
304

305         ra1acc = ra1home.findByNumber(202);
306
307         // Finding an Account 202 in Second database
308

309         ra2acc = ra2home.findByNumber(202);
310
311         // Deduct 100 from Account 202 in First database
312

313         ra1acc.setBalance(-value);
314
315         // Increment 100 to Account 202 in Second database
316

317         ra2acc.setBalance(value);
318
319         utx.commit();
320
321             assertEquals (String.valueOf(ra1acc.getBalance() + 100),
322                           String.valueOf(ra2acc.getBalance() - 100));
323
324     } catch (Exception JavaDoc e) {
325
326             try {
327                  utx.rollback();
328              fail("Cannot update balance by 100 for account 202 " +e);
329
330             }
331             catch (Exception JavaDoc f) {
332                  fail("Cannot rollback TestRaUpdateCommit: " +f);
333             }
334     }
335     }
336
337     public void testRaUpdateRollback() {
338
339     // Start another transaction (rolled back):
340
// transfert 20 from ra1acc to ra2acc
341

342     // Starting a second transaction, that will be rolled back
343

344         String JavaDoc str1 = null;
345         String JavaDoc str2 = null;
346
347     double value = 20;
348
349         try {
350
351         utx.begin();
352
353         // Finding an Account 202 in First database
354

355         ra1acc = ra1home.findByNumber(202);
356
357         // Finding an Account 202 in Second database
358

359         ra2acc = ra2home.findByNumber(202);
360
361         // Deduct 20 from Account 202 in First database
362

363         ra1acc.setBalance(-value);
364
365         // Increment 20 to Account 202 in Second database
366

367         ra2acc.setBalance(value);
368
369         utx.rollback();
370
371             assertEquals (String.valueOf(ra1acc.getBalance() + 100),
372                           String.valueOf(ra2acc.getBalance() - 100));
373
374     } catch (Exception JavaDoc e) {
375             try {
376                  utx.rollback();
377              fail("Cannot update balance by 20 for account 202 " +e);
378             }
379             catch (Exception JavaDoc f) {
380                  fail("Cannot rollback testRaUpdateRollback: " +f);
381             }
382     }
383     }
384
385     public void testRaInsertCommit() {
386
387     // Create a first Account
388

389     ra1acc = null;
390         ra2acc = null;
391
392         try {
393
394         // Creating a new Account in First database
395

396         utx.begin();
397
398         ra1acc = ra1home.create(209, "John Smith", 100);
399
400         // Creating a new Account in Second database
401

402         ra2acc = ra2home.create(209, "John Smith", 100);
403
404         utx.commit();
405
406             assertEquals (ra1acc.getNumber(), ra2acc.getNumber());
407
408     } catch (Exception JavaDoc e) {
409             try {
410                  utx.rollback();
411              fail("Cannot create account 209: " +e);
412             }
413             catch (Exception JavaDoc f) {
414                  fail("Cannot rollback testRaInsertCommit: " +f);
415             }
416     }
417     }
418
419     public void testRaInsertRollback() {
420
421     // Create a first Account
422

423     ra1acc = null;
424         ra2acc = null;
425
426         try {
427
428         // Creating a new Account in First database
429

430         utx.begin();
431
432         ra1acc = ra1home.create(210, "Mike Smith", 100);
433
434         // Creating a new Account in Second database
435

436         ra2acc = ra2home.create(210, "Mike Smith", 100);
437
438         utx.rollback();
439
440     } catch (Exception JavaDoc e) {
441             try {
442                  utx.rollback();
443              fail("Cannot create account 210: " +e);
444             }
445             catch (Exception JavaDoc f) {
446                  fail("Cannot rollback testRaInsertCommit: " +f);
447             }
448     }
449
450         try {
451
452             ra1acc = ra1home.findByNumber(210);
453             assertTrue (5 == 6);
454
455         }
456         catch (Exception JavaDoc g) {
457             // Okay should not find 210 in ra1acc
458

459             try {
460                 ra2acc = ra2home.findByNumber(210);
461                 assertTrue (5 == 6);
462             }
463             catch (Exception JavaDoc h) {
464                 // Okay should not find 210 ins ra2acc
465
assertTrue (5 == 5);
466             }
467         }
468     }
469
470     public void testRaDeleteRollback() {
471
472     // Delete account
473

474         ra1acc = null;
475         ra2acc = null;
476
477         try {
478
479         utx.begin();
480
481         // Removing account 201 previously created in First database
482

483         ra1acc = ra1home.findByNumber(201);
484             ra1acc.remove();
485
486         // Removing account 201 previously created in Second database
487

488         ra2acc = ra2home.findByNumber(201);
489             ra2acc.remove();
490
491         utx.rollback();
492
493             utx.begin();
494
495         // Finding an Account 201 in First database
496

497         ra1acc = ra1home.findByNumber(201);
498
499         // Finding an Account 201 in Second database
500

501         ra2acc = ra2home.findByNumber(201);
502
503             utx.commit();
504
505             assertEquals (ra1acc.getNumber(), ra2acc.getNumber());
506
507     } catch (Exception JavaDoc e) {
508             try {
509                  utx.rollback();
510              fail("Cannot delete accounts 201-205, 209: " +e);
511             }
512             catch (Exception JavaDoc f) {
513                  fail("Cannot rollback testRaDeleteCommit: " +f);
514             }
515     }
516     }
517
518     public void testRaDeleteCommit() {
519
520     // Delete account
521

522         ra1acc = null;
523         ra2acc = null;
524
525         try {
526
527         utx.begin();
528
529         // Removing account 201-205 previously created in First database
530

531         ra1acc = ra1home.findByNumber(201);
532             ra1acc.remove();
533         ra1acc = ra1home.findByNumber(202);
534             ra1acc.remove();
535         ra1acc = ra1home.findByNumber(203);
536             ra1acc.remove();
537         ra1acc = ra1home.findByNumber(204);
538             ra1acc.remove();
539         ra1acc = ra1home.findByNumber(205);
540             ra1acc.remove();
541         ra1acc = ra1home.findByNumber(209);
542             ra1acc.remove();
543
544         // Removing account 201-205 previously created in Second database
545

546         ra2acc = ra2home.findByNumber(201);
547             ra2acc.remove();
548         ra2acc = ra2home.findByNumber(202);
549             ra2acc.remove();
550         ra2acc = ra2home.findByNumber(203);
551             ra2acc.remove();
552         ra2acc = ra2home.findByNumber(204);
553             ra2acc.remove();
554         ra2acc = ra2home.findByNumber(205);
555             ra2acc.remove();
556         ra2acc = ra2home.findByNumber(209);
557             ra2acc.remove();
558
559         utx.commit();
560
561             assertTrue (5 == 5);
562
563     } catch (Exception JavaDoc e) {
564             try {
565                  utx.rollback();
566              fail("Cannot delete accounts 201-205, 209: " +e);
567             }
568             catch (Exception JavaDoc f) {
569                  fail("Cannot rollback testRaDeleteCommit: " +f);
570             }
571     }
572     }
573
574     public static Test suite() {
575         return new TestSuite(F_JdbcRATest.class);
576     }
577
578     public static void main (String JavaDoc args[]) {
579
580         String JavaDoc testtorun = null;
581
582         // Get args
583

584         for (int argn = 0; argn < args.length; argn++) {
585
586             String JavaDoc s_arg = args[argn];
587             Integer JavaDoc i_arg;
588
589             if (s_arg.equals("-n")) {
590                 testtorun = args[++argn];
591             }
592         }
593
594         if (testtorun == null) {
595             junit.textui.TestRunner.run(suite());
596         } else {
597             junit.textui.TestRunner.run(new F_JdbcRATest(testtorun));
598         }
599     }
600 }
601
Popular Tags