001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: F_JdbcRATest.java 7291 2005-08-24 19:03:58Z ashah $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jtests.clients.jdbcra;
025:
026: import java.util.Enumeration;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.rmi.PortableRemoteObject;
031: import javax.transaction.UserTransaction;
032:
033: import junit.framework.Test;
034: import junit.framework.TestSuite;
035:
036: import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA1;
037: import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA1Home;
038: import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA2;
039: import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA2Home;
040: import org.objectweb.jonas.jtests.util.JTestCase;
041:
042: /**
043: * JdbcRa tests on Jdbc Resource Adapter
044: */
045:
046: public class F_JdbcRATest extends JTestCase {
047:
048: private static UserTransaction utx = null;
049:
050: static Context ctx = null;
051:
052: // Lookup bean home
053:
054: protected static String BEAN_HOME1 = "JdbcRA1EBRHome";
055: protected static JdbcRA1Home ra1home = null;
056:
057: protected static String BEAN_HOME2 = "JdbcRA2EBRHome";
058: protected static JdbcRA2Home ra2home = null;
059:
060: Enumeration ra1list;
061: static JdbcRA1 ra1acc;
062:
063: Enumeration ra2list;
064: static JdbcRA2 ra2acc;
065:
066: public F_JdbcRATest(String name) {
067: super (name);
068: }
069:
070: protected void setUp() {
071: super .setUp();
072:
073: // Get InitialContext
074:
075: try {
076:
077: // get JNDI initial context
078:
079: if (ctx == null) {
080: ctx = new InitialContext();
081: }
082:
083: // We want to start transactions from client: get UserTransaction
084:
085: if (utx == null) {
086: utx = (UserTransaction) ctx
087: .lookup("javax.transaction.UserTransaction");
088: }
089:
090: if (ra1home == null && ra2home == null) {
091: useBeans("jdbcra", true);
092: }
093:
094: // Connecting to ra1transHome thru JNDI
095:
096: if (ra1home == null) {
097: ra1home = (JdbcRA1Home) PortableRemoteObject.narrow(ctx
098: .lookup(BEAN_HOME1), JdbcRA1Home.class);
099: }
100:
101: // Connecting to ra2Home thru JNDI
102:
103: if (ra2home == null) {
104: ra2home = (JdbcRA2Home) PortableRemoteObject.narrow(ctx
105: .lookup(BEAN_HOME2), JdbcRA2Home.class);
106: }
107:
108: assertTrue(5 == 5);
109:
110: } catch (Exception e) {
111: fail("Cannot lookup UserTransaction in setUp: " + e);
112: }
113: }
114:
115: public void testRaCleanUpCommit() {
116:
117: // Clean up databases just in case there was a failure
118: // in an earlier execution of these tests
119:
120: ra1acc = null;
121: ra2acc = null;
122: int dlaccno;
123:
124: try {
125:
126: utx.begin();
127:
128: // Removing accounts 201-205, 209 from First database
129:
130: // Getting the list of existing accounts in First database
131:
132: ra1list = ra1home.findAllAccounts();
133:
134: while (ra1list.hasMoreElements()) {
135: ra1acc = (JdbcRA1) ra1list.nextElement();
136: dlaccno = ra1acc.getNumber();
137:
138: if ((dlaccno >= 201 && dlaccno <= 205)
139: || dlaccno == 209) {
140: ra1acc.remove();
141: }
142: }
143:
144: // Getting the list of existing accounts in Second database
145:
146: ra2list = ra2home.findAllAccounts();
147:
148: while (ra2list.hasMoreElements()) {
149: ra2acc = (JdbcRA2) ra2list.nextElement();
150: dlaccno = ra2acc.getNumber();
151:
152: if ((dlaccno >= 201 && dlaccno <= 205)
153: || dlaccno == 209) {
154: ra2acc.remove();
155: }
156: }
157:
158: utx.commit();
159:
160: assertTrue(5 == 5);
161:
162: } catch (Exception e) {
163: // Not an error, accounts previously removed by last test
164: try {
165: utx.rollback();
166: } catch (Exception f) {
167: assertTrue(5 == 5);
168: }
169: assertTrue(5 == 5);
170: }
171: }
172:
173: public void testRaBatchInsert() {
174:
175: // Store five accounts using executeBatch
176:
177: ra1acc = null;
178: ra2acc = null;
179:
180: String str1 = null;
181: String str2 = null;
182:
183: try {
184:
185: utx.begin();
186:
187: // Store accounts 201-205 in First database
188:
189: ra1acc = ra1home.create();
190:
191: // Store accounts 201-205 in Second database
192:
193: ra2acc = ra2home.create();
194:
195: // Find account 203 created in First database
196:
197: ra1acc = ra1home.findByNumber(203);
198:
199: // Find account 203 created in Second database
200:
201: ra2acc = ra2home.findByNumber(203);
202:
203: utx.commit();
204:
205: assertEquals(String.valueOf(ra1acc.getBalance()), String
206: .valueOf(ra2acc.getBalance()));
207:
208: } catch (Exception e) {
209:
210: try {
211: utx.rollback();
212: fail("Cannot store accounts using batch" + e);
213:
214: } catch (Exception f) {
215: fail("Cannot rollback testRaBatchInsert: " + f);
216: }
217: }
218: }
219:
220: public void testRaSelectAll() {
221:
222: try {
223:
224: // List existing Accounts
225:
226: utx.begin();
227:
228: // Getting the list of existing accounts in First database
229:
230: ra1list = ra1home.findAllAccounts();
231:
232: while (ra1list.hasMoreElements()) {
233: ra1acc = (JdbcRA1) PortableRemoteObject.narrow(ra1list
234: .nextElement(), JdbcRA1.class);
235: }
236:
237: // Getting the list of existing accounts in Second database
238:
239: ra2list = ra2home.findAllAccounts();
240:
241: while (ra2list.hasMoreElements()) {
242: ra2acc = (JdbcRA2) PortableRemoteObject.narrow(ra2list
243: .nextElement(), JdbcRA2.class);
244: }
245:
246: utx.commit();
247:
248: assertTrue(5 == 5);
249:
250: } catch (Exception e) {
251:
252: try {
253: utx.rollback();
254: fail("Cannot retrieve all accounts: " + e);
255: } catch (Exception f) {
256: fail("Cannot rollback testRaSelectAll: " + f);
257: }
258: }
259: }
260:
261: public void testRaSelectOne() {
262:
263: // Find a second Account
264:
265: ra1acc = null;
266: ra2acc = null;
267:
268: try {
269:
270: // Finding an Account 202 in First database
271:
272: ra1acc = ra1home.findByNumber(202);
273:
274: // Finding an Account 202 in Second database
275:
276: ra2acc = ra2home.findByNumber(202);
277:
278: assertEquals(ra1acc.getNumber(), ra2acc.getNumber());
279:
280: } catch (Exception e) {
281: fail("Cannot find account 202 " + e);
282: }
283: }
284:
285: public void testRaUpdateCommit() {
286:
287: // First transaction (committed):
288: // transfert 100 from ra1acc to ra2acc
289:
290: // Starting a first transaction, that will be committed
291:
292: String str1 = null;
293: String str2 = null;
294:
295: double value = 100;
296:
297: try {
298:
299: utx.begin();
300:
301: // Finding an Account 202 in First database
302:
303: ra1acc = ra1home.findByNumber(202);
304:
305: // Finding an Account 202 in Second database
306:
307: ra2acc = ra2home.findByNumber(202);
308:
309: // Deduct 100 from Account 202 in First database
310:
311: ra1acc.setBalance(-value);
312:
313: // Increment 100 to Account 202 in Second database
314:
315: ra2acc.setBalance(value);
316:
317: utx.commit();
318:
319: assertEquals(String.valueOf(ra1acc.getBalance() + 100),
320: String.valueOf(ra2acc.getBalance() - 100));
321:
322: } catch (Exception e) {
323:
324: try {
325: utx.rollback();
326: fail("Cannot update balance by 100 for account 202 "
327: + e);
328:
329: } catch (Exception f) {
330: fail("Cannot rollback TestRaUpdateCommit: " + f);
331: }
332: }
333: }
334:
335: public void testRaUpdateRollback() {
336:
337: // Start another transaction (rolled back):
338: // transfert 20 from ra1acc to ra2acc
339:
340: // Starting a second transaction, that will be rolled back
341:
342: String str1 = null;
343: String str2 = null;
344:
345: double value = 20;
346:
347: try {
348:
349: utx.begin();
350:
351: // Finding an Account 202 in First database
352:
353: ra1acc = ra1home.findByNumber(202);
354:
355: // Finding an Account 202 in Second database
356:
357: ra2acc = ra2home.findByNumber(202);
358:
359: // Deduct 20 from Account 202 in First database
360:
361: ra1acc.setBalance(-value);
362:
363: // Increment 20 to Account 202 in Second database
364:
365: ra2acc.setBalance(value);
366:
367: utx.rollback();
368:
369: assertEquals(String.valueOf(ra1acc.getBalance() + 100),
370: String.valueOf(ra2acc.getBalance() - 100));
371:
372: } catch (Exception e) {
373: try {
374: utx.rollback();
375: fail("Cannot update balance by 20 for account 202 " + e);
376: } catch (Exception f) {
377: fail("Cannot rollback testRaUpdateRollback: " + f);
378: }
379: }
380: }
381:
382: public void testRaInsertCommit() {
383:
384: // Create a first Account
385:
386: ra1acc = null;
387: ra2acc = null;
388:
389: try {
390:
391: // Creating a new Account in First database
392:
393: utx.begin();
394:
395: ra1acc = ra1home.create(209, "John Smith", 100);
396:
397: // Creating a new Account in Second database
398:
399: ra2acc = ra2home.create(209, "John Smith", 100);
400:
401: utx.commit();
402:
403: assertEquals(ra1acc.getNumber(), ra2acc.getNumber());
404:
405: } catch (Exception e) {
406: try {
407: utx.rollback();
408: fail("Cannot create account 209: " + e);
409: } catch (Exception f) {
410: fail("Cannot rollback testRaInsertCommit: " + f);
411: }
412: }
413: }
414:
415: public void testRaInsertRollback() {
416:
417: // Create a first Account
418:
419: ra1acc = null;
420: ra2acc = null;
421:
422: try {
423:
424: // Creating a new Account in First database
425:
426: utx.begin();
427:
428: ra1acc = ra1home.create(210, "Mike Smith", 100);
429:
430: // Creating a new Account in Second database
431:
432: ra2acc = ra2home.create(210, "Mike Smith", 100);
433:
434: utx.rollback();
435:
436: } catch (Exception e) {
437: try {
438: utx.rollback();
439: fail("Cannot create account 210: " + e);
440: } catch (Exception f) {
441: fail("Cannot rollback testRaInsertCommit: " + f);
442: }
443: }
444:
445: try {
446:
447: ra1acc = ra1home.findByNumber(210);
448: assertTrue(5 == 6);
449:
450: } catch (Exception g) {
451: // Okay should not find 210 in ra1acc
452:
453: try {
454: ra2acc = ra2home.findByNumber(210);
455: assertTrue(5 == 6);
456: } catch (Exception h) {
457: // Okay should not find 210 ins ra2acc
458: assertTrue(5 == 5);
459: }
460: }
461: }
462:
463: public void testRaDeleteRollback() {
464:
465: // Delete account
466:
467: ra1acc = null;
468: ra2acc = null;
469:
470: try {
471:
472: utx.begin();
473:
474: // Removing account 201 previously created in First database
475:
476: ra1acc = ra1home.findByNumber(201);
477: ra1acc.remove();
478:
479: // Removing account 201 previously created in Second database
480:
481: ra2acc = ra2home.findByNumber(201);
482: ra2acc.remove();
483:
484: utx.rollback();
485:
486: utx.begin();
487:
488: // Finding an Account 201 in First database
489:
490: ra1acc = ra1home.findByNumber(201);
491:
492: // Finding an Account 201 in Second database
493:
494: ra2acc = ra2home.findByNumber(201);
495:
496: utx.commit();
497:
498: assertEquals(ra1acc.getNumber(), ra2acc.getNumber());
499:
500: } catch (Exception e) {
501: try {
502: utx.rollback();
503: fail("Cannot delete accounts 201-205, 209: " + e);
504: } catch (Exception f) {
505: fail("Cannot rollback testRaDeleteCommit: " + f);
506: }
507: }
508: }
509:
510: public void testRaDeleteCommit() {
511:
512: // Delete account
513:
514: ra1acc = null;
515: ra2acc = null;
516:
517: try {
518:
519: utx.begin();
520:
521: // Removing account 201-205 previously created in First database
522:
523: ra1acc = ra1home.findByNumber(201);
524: ra1acc.remove();
525: ra1acc = ra1home.findByNumber(202);
526: ra1acc.remove();
527: ra1acc = ra1home.findByNumber(203);
528: ra1acc.remove();
529: ra1acc = ra1home.findByNumber(204);
530: ra1acc.remove();
531: ra1acc = ra1home.findByNumber(205);
532: ra1acc.remove();
533: ra1acc = ra1home.findByNumber(209);
534: ra1acc.remove();
535:
536: // Removing account 201-205 previously created in Second database
537:
538: ra2acc = ra2home.findByNumber(201);
539: ra2acc.remove();
540: ra2acc = ra2home.findByNumber(202);
541: ra2acc.remove();
542: ra2acc = ra2home.findByNumber(203);
543: ra2acc.remove();
544: ra2acc = ra2home.findByNumber(204);
545: ra2acc.remove();
546: ra2acc = ra2home.findByNumber(205);
547: ra2acc.remove();
548: ra2acc = ra2home.findByNumber(209);
549: ra2acc.remove();
550:
551: utx.commit();
552:
553: assertTrue(5 == 5);
554:
555: } catch (Exception e) {
556: try {
557: utx.rollback();
558: fail("Cannot delete accounts 201-205, 209: " + e);
559: } catch (Exception f) {
560: fail("Cannot rollback testRaDeleteCommit: " + f);
561: }
562: }
563: }
564:
565: public static Test suite() {
566: TestSuite ts = new TestSuite();
567: ts.addTest(new F_JdbcRATest("testRaCleanUpCommit"));
568: ts.addTest(new F_JdbcRATest("testRaBatchInsert"));
569: ts.addTest(new F_JdbcRATest("testRaSelectAll"));
570: ts.addTest(new F_JdbcRATest("testRaSelectOne"));
571: ts.addTest(new F_JdbcRATest("testRaUpdateCommit"));
572: ts.addTest(new F_JdbcRATest("testRaUpdateRollback"));
573: ts.addTest(new F_JdbcRATest("testRaInsertCommit"));
574: ts.addTest(new F_JdbcRATest("testRaInsertRollback"));
575: ts.addTest(new F_JdbcRATest("testRaDeleteRollback"));
576: ts.addTest(new F_JdbcRATest("testRaDeleteCommit"));
577: return ts;
578: }
579:
580: public static void main(String args[]) {
581:
582: String testtorun = null;
583:
584: // Get args
585:
586: for (int argn = 0; argn < args.length; argn++) {
587:
588: String s_arg = args[argn];
589: Integer i_arg;
590:
591: if (s_arg.equals("-n")) {
592: testtorun = args[++argn];
593: }
594: }
595:
596: if (testtorun == null) {
597: junit.textui.TestRunner.run(suite());
598: } else {
599: junit.textui.TestRunner.run(new F_JdbcRATest(testtorun));
600: }
601: }
602: }
|