001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: * $Id: F_ConEnlist.java 7527 2005-10-19 12:25:32Z durieuxp $
022: * --------------------------------------------------------------------------
023: */
024:
025: package org.objectweb.jonas.jtests.clients.entity;
026:
027: import java.util.ArrayList;
028: import java.util.Calendar;
029: import java.util.Collection;
030: import java.util.Date;
031: import java.util.HashSet;
032: import java.util.Iterator;
033: import java.util.Set;
034: import javax.naming.NamingException;
035: import javax.rmi.PortableRemoteObject;
036: import junit.framework.Test;
037: import junit.framework.TestSuite;
038: import org.objectweb.jonas.jtests.beans.ejbql.*;
039: import org.objectweb.jonas.jtests.util.JTestCase;
040:
041: public class F_ConEnlist extends JTestCase {
042:
043: private static SessionTestHomeRemote sessiontesthome = null;
044: private static CustomerHomeRemote customerhome = null;
045: private static ReservationHomeRemote reservationhome = null;
046: private static CruiseHomeRemote cruisehome = null;
047: private static ShipHomeRemote shiphome = null;
048: private static CabinHomeRemote cabinhome = null;
049: private static AddressHomeRemote addresshome = null;
050: private static CreditCardHomeRemote creditcardhome = null;
051:
052: private boolean initialized = false;
053:
054: public F_ConEnlist(String name) {
055: super (name);
056: }
057:
058: protected void setUp() {
059: super .setUp();
060: if (!useBeans("ejbql", false)) {
061: if (!initialized) {
062: // The bean was already loaded by another test.
063: // Clean the tables first.
064: try {
065: tearDown();
066: } catch (Exception e) {
067: System.out.println("cannot init tables");
068: }
069: }
070: }
071: }
072:
073: protected void tearDown() throws Exception {
074: Collection c = getShipHome().findAllShips();
075: for (Iterator i = c.iterator(); i.hasNext();) {
076: ShipRemote s = (ShipRemote) PortableRemoteObject.narrow(i
077: .next(), ShipRemote.class);
078: s.remove();
079: }
080: c = getReservationHome().findAllReservations();
081: for (Iterator i = c.iterator(); i.hasNext();) {
082: ReservationRemote s = (ReservationRemote) PortableRemoteObject
083: .narrow(i.next(), ReservationRemote.class);
084: s.remove();
085: }
086: c = getCruiseHome().findAllCruises();
087: for (Iterator i = c.iterator(); i.hasNext();) {
088: CruiseRemote s = (CruiseRemote) PortableRemoteObject
089: .narrow(i.next(), CruiseRemote.class);
090: s.remove();
091: }
092: c = getCustomerHome().findCustomersAll();
093: for (Iterator i = c.iterator(); i.hasNext();) {
094: CustomerRemote s = (CustomerRemote) PortableRemoteObject
095: .narrow(i.next(), CustomerRemote.class);
096: s.remove();
097: }
098: for (int dl = 1; dl <= 7; dl++) {
099: c = getCabinHome().findAllOnDeckLevel(new Integer(dl));
100: for (Iterator i = c.iterator(); i.hasNext();) {
101: CabinRemote s = (CabinRemote) PortableRemoteObject
102: .narrow(i.next(), CabinRemote.class);
103: s.remove();
104: }
105: }
106: }
107:
108: private SessionTestHomeRemote getSessionTestHome() throws Exception {
109: if (sessiontesthome == null) {
110: sessiontesthome = (SessionTestHomeRemote) PortableRemoteObject
111: .narrow(ictx.lookup("EjbqlSessionTestHomeRemote"),
112: SessionTestHomeRemote.class);
113: }
114: assertTrue(sessiontesthome != null);
115: return sessiontesthome;
116: }
117:
118: private CustomerHomeRemote getCustomerHome() throws Exception {
119: if (customerhome == null) {
120: customerhome = (CustomerHomeRemote) PortableRemoteObject
121: .narrow(ictx.lookup("CustomerHomeRemote"),
122: CustomerHomeRemote.class);
123: }
124: assertTrue(customerhome != null);
125: return customerhome;
126: }
127:
128: private ShipHomeRemote getShipHome() throws Exception {
129: if (shiphome == null) {
130: shiphome = (ShipHomeRemote) PortableRemoteObject
131: .narrow(ictx.lookup("ShipHomeRemote"),
132: ShipHomeRemote.class);
133: }
134: assertTrue(shiphome != null);
135: return shiphome;
136: }
137:
138: private CruiseHomeRemote getCruiseHome() throws Exception {
139: if (cruisehome == null) {
140: cruisehome = (CruiseHomeRemote) PortableRemoteObject
141: .narrow(ictx.lookup("CruiseHomeRemote"),
142: CruiseHomeRemote.class);
143: }
144: assertTrue(cruisehome != null);
145: return cruisehome;
146: }
147:
148: private ReservationHomeRemote getReservationHome() throws Exception {
149: if (reservationhome == null) {
150: reservationhome = (ReservationHomeRemote) PortableRemoteObject
151: .narrow(ictx.lookup("ReservationHomeRemote"),
152: ReservationHomeRemote.class);
153: }
154: assertTrue(reservationhome != null);
155: return reservationhome;
156: }
157:
158: private CabinHomeRemote getCabinHome() throws Exception {
159: if (cabinhome == null) {
160: cabinhome = (CabinHomeRemote) PortableRemoteObject.narrow(
161: ictx.lookup("CabinHomeRemote"),
162: CabinHomeRemote.class);
163: }
164: assertTrue(cabinhome != null);
165: return cabinhome;
166: }
167:
168: private AddressHomeRemote getAddressHome() throws Exception {
169: if (addresshome == null) {
170: addresshome = (AddressHomeRemote) PortableRemoteObject
171: .narrow(ictx.lookup("AddressHomeRemote"),
172: AddressHomeRemote.class);
173: }
174: assertTrue(addresshome != null);
175: return addresshome;
176: }
177:
178: public void test1() throws Exception {
179: utx.begin();
180: ShipRemote shipA = getShipHome().create(new Integer(10772),
181: "Ship A", 30000.0);
182: CruiseRemote cruiseA = getCruiseHome().create("Cruise A",
183: new Integer(10772));
184: utx.commit();
185: }
186:
187: public void test2() throws Exception {
188: utx.begin();
189: ShipRemote shipA = getShipHome().create(new Integer(10772),
190: "Ship A", 30000.0);
191: CruiseRemote cruiseA = getCruiseHome().create("Cruise A",
192: new Integer(10772));
193: for (int j = 1; j <= 10; j++) {
194: getShipHome().create(new Integer(j), "Ship " + j,
195: 30000.0 + (10000.0 * j));
196: }
197: utx.commit();
198: }
199:
200: public void test3() throws Exception {
201: utx.begin();
202: ShipRemote shipA = getShipHome().create(new Integer(10772),
203: "Ship A", 30000.0);
204: CruiseRemote cruiseA = getCruiseHome().create("Cruise A",
205: new Integer(10772));
206: for (int j = 1; j <= 10; j++) {
207: getShipHome().create(new Integer(j), "Ship " + j,
208: 30000.0 + (10000.0 * j));
209: }
210: ShipRemote ship1 = getShipHome().findByPrimaryKey(
211: new Integer(1));
212: cruiseA = getCruiseHome().create("Alaska Cruise",
213: new Integer(1));
214: CruiseRemote cruiseB = getCruiseHome().create(
215: "Bohemian Cruise", new Integer(1));
216: for (int i = 80; i <= 99; i++) {
217: CustomerRemote customer = getCustomerHome().create(
218: new Integer(i));
219: customer.setName(new Name("Smith" + i, "John"));
220: customer.addPhoneNumber("612-555-12" + i, (byte) 1);
221: customer.setAddress("10" + i + " Elm Street", "LONDON",
222: "MN", "5540");
223: customer.setHasGoodCredit(i % 4 == 0);
224: if (i % 4 == 0) {
225: customer.setCreditCard(new Date(), "100001" + i,
226: "MasterCard", "CA", "CETELEM", "rue de jeunet",
227: "PARIS", "France", "3888");
228: }
229: }
230: utx.commit();
231: }
232:
233: public void test4() throws Exception {
234: utx.begin();
235: ShipRemote shipA = getShipHome().create(new Integer(10772),
236: "Ship A", 30000.0);
237: CruiseRemote cruiseA = getCruiseHome().create("Cruise A",
238: new Integer(10772));
239: for (int j = 1; j <= 10; j++) {
240: getShipHome().create(new Integer(j), "Ship " + j,
241: 30000.0 + (10000.0 * j));
242: }
243: ShipRemote ship1 = getShipHome().findByPrimaryKey(
244: new Integer(1));
245: cruiseA = getCruiseHome().create("Alaska Cruise",
246: new Integer(1));
247: CruiseRemote cruiseB = getCruiseHome().create(
248: "Bohemian Cruise", new Integer(1));
249: for (int i = 80; i <= 99; i++) {
250: CustomerRemote customer = getCustomerHome().create(
251: new Integer(i));
252: customer.setName(new Name("Smith" + i, "John"));
253: customer.addPhoneNumber("612-555-12" + i, (byte) 1);
254: customer.setAddress("10" + i + " Elm Street", "LONDON",
255: "MN", "5540");
256: customer.setHasGoodCredit(i % 4 == 0);
257: if (i % 4 == 0) {
258: customer.setCreditCard(new Date(), "100001" + i,
259: "MasterCard", "CA", "CETELEM", "rue de jeunet",
260: "PARIS", "France", "3888");
261: }
262: }
263: Calendar date = Calendar.getInstance();
264: date.set(2004, 04, 01);
265: for (int i = 201; i <= 207; i++) {
266: Collection customers = new ArrayList();
267: CustomerRemote cust = getCustomerHome().create(
268: new Integer(i));
269: cust.setName(new Name("Customer " + i, "Mike"));
270: cust.setHasGoodCredit(i % 2 == 0);
271: cust.setAddress("50" + i + " Main Street", "Minneapolis",
272: "MN", "5510" + i);
273: customers.add(new Integer(i)); // put this single customer in the collection
274:
275: Collection reservations = new ArrayList();
276: for (int j = 0; j < 2; j++) {
277: ReservationRemote reservation = getReservationHome()
278: .create(cruiseA.getId(), customers);
279: reservation.setDate(date.getTime());
280: reservation.setAmountPaid(1000 * i + 100 * j + 2000);
281: date.add(Calendar.DAY_OF_MONTH, 7);
282:
283: Set cabins = new HashSet();
284: CabinRemote cabin = getCabinHome().create(
285: new Integer(1000 + i * 100 + j));
286: cabin.setDeckLevel(i - 200);
287: cabin.setName("Cabin " + i + "0" + j + "1");
288: cabins.add(cabin.getId());
289: cabin = getCabinHome().create(
290: new Integer(1000 + i * 100 + 10 + j));
291: cabin.setDeckLevel(i - 200);
292: cabin.setName("Cabin " + i + "0" + j + "2");
293: cabins.add(cabin.getId());
294: reservation.setAllCabins(cabins); // this reservation has 2 cabins
295: }
296: }
297: utx.commit();
298: }
299:
300: public void test35() throws Exception {
301: utx.begin();
302: ShipRemote shipA = getShipHome().create(new Integer(10772),
303: "Ship A", 30000.0);
304: CruiseRemote cruiseA = getCruiseHome().create("Cruise A",
305: new Integer(10772));
306: for (int j = 1; j <= 10; j++) {
307: getShipHome().create(new Integer(j), "Ship " + j,
308: 30000.0 + (10000.0 * j));
309: }
310: ShipRemote ship1 = getShipHome().findByPrimaryKey(
311: new Integer(1));
312: cruiseA = getCruiseHome().create("Alaska Cruise",
313: new Integer(1));
314: CruiseRemote cruiseB = getCruiseHome().create(
315: "Bohemian Cruise", new Integer(1));
316: for (int i = 80; i <= 99; i++) {
317: CustomerRemote customer = getCustomerHome().create(
318: new Integer(i));
319: customer.setName(new Name("Smith" + i, "John"));
320: customer.addPhoneNumber("612-555-12" + i, (byte) 1);
321: customer.setAddress("10" + i + " Elm Street", "LONDON",
322: "MN", "5540");
323: customer.setHasGoodCredit(i % 4 == 0);
324: if (i % 4 == 0) {
325: customer.setCreditCard(new Date(), "100001" + i,
326: "MasterCard", "CA", "CETELEM", "rue de jeunet",
327: "PARIS", "France", "3888");
328: }
329: }
330: Calendar date = Calendar.getInstance();
331: date.set(2004, 04, 01);
332: int i = 201;
333: Collection customers = new ArrayList();
334: CustomerRemote cust = getCustomerHome().create(new Integer(i));
335: cust.setName(new Name("Customer " + i, "Mike"));
336: cust.setHasGoodCredit(i % 2 == 0);
337: cust.setAddress("50" + i + " Main Street", "Minneapolis", "MN",
338: "5510" + i);
339: customers.add(new Integer(i)); // put this single customer in the collection
340:
341: Collection reservations = new ArrayList();
342: int j = 0;
343: ReservationRemote reservation = getReservationHome().create(
344: cruiseA.getId(), customers);
345: reservation.setDate(date.getTime());
346: reservation.setAmountPaid(1000 * i + 100 * j + 2000);
347: date.add(Calendar.DAY_OF_MONTH, 7);
348:
349: Set cabins = new HashSet();
350: CabinRemote cabin = getCabinHome().create(
351: new Integer(1000 + i * 100 + j));
352: cabin.setDeckLevel(i - 200);
353: cabin.setName("Cabin " + i + "0" + j + "1");
354: cabins.add(cabin.getId());
355: cabin = getCabinHome().create(
356: new Integer(1000 + i * 100 + 10 + j));
357: cabin.setDeckLevel(i - 200);
358: cabin.setName("Cabin " + i + "0" + j + "2");
359: cabins.add(cabin.getId());
360: reservation.setAllCabins(cabins); // this reservation has 2 cabins
361: utx.commit();
362: }
363:
364: public void test33() throws Exception {
365: utx.begin();
366: ShipRemote shipA = getShipHome().create(new Integer(10772),
367: "Ship A", 30000.0);
368: CruiseRemote cruiseA = getCruiseHome().create("Cruise A",
369: new Integer(10772));
370: for (int j = 1; j <= 10; j++) {
371: getShipHome().create(new Integer(j), "Ship " + j,
372: 30000.0 + (10000.0 * j));
373: }
374: ShipRemote ship1 = getShipHome().findByPrimaryKey(
375: new Integer(1));
376: cruiseA = getCruiseHome().create("Alaska Cruise",
377: new Integer(1));
378: CruiseRemote cruiseB = getCruiseHome().create(
379: "Bohemian Cruise", new Integer(1));
380: for (int i = 80; i <= 82; i++) {
381: CustomerRemote customer = getCustomerHome().create(
382: new Integer(i));
383: customer.setName(new Name("Smith" + i, "John"));
384: customer.addPhoneNumber("612-555-12" + i, (byte) 1);
385: customer.setAddress("10" + i + " Elm Street", "LONDON",
386: "MN", "5540");
387: customer.setHasGoodCredit(i % 4 == 0);
388: if (i % 4 == 0) {
389: customer.setCreditCard(new Date(), "100001" + i,
390: "MasterCard", "CA", "CETELEM", "rue de jeunet",
391: "PARIS", "France", "3888");
392: }
393: }
394: utx.commit();
395:
396: utx.begin();
397: Calendar date = Calendar.getInstance();
398: date.set(2004, 04, 01);
399: int i = 201;
400: Collection customers = new ArrayList();
401: CustomerRemote cust = getCustomerHome().create(new Integer(i));
402: cust.setName(new Name("Customer " + i, "Mike"));
403: cust.setHasGoodCredit(i % 2 == 0);
404: cust.setAddress("50" + i + " Main Street", "Minneapolis", "MN",
405: "5510" + i);
406: customers.add(new Integer(i)); // put this single customer in the collection
407: utx.commit();
408:
409: utx.begin();
410: Collection reservations = new ArrayList();
411: int j = 0;
412: ReservationRemote reservation = getReservationHome().create(
413: cruiseA.getId(), customers);
414: reservation.setDate(date.getTime());
415: reservation.setAmountPaid(1000 * i + 100 * j + 2000);
416: date.add(Calendar.DAY_OF_MONTH, 7);
417:
418: CabinRemote cabin = getCabinHome().create(
419: new Integer(1000 + i * 100 + j));
420: cabin.setDeckLevel(i - 200);
421: cabin.setName("Cabin " + i + "0" + j + "1");
422: cabin = getCabinHome().create(
423: new Integer(1000 + i * 100 + 10 + j));
424: cabin.setDeckLevel(i - 200);
425: cabin.setName("Cabin " + i + "0" + j + "2");
426: utx.commit();
427: }
428:
429: public void test11() throws Exception {
430: test3();
431: for (int i = 0; i < 10; i++) {
432: Collection c = getShipHome().findAllShips();
433: }
434: }
435:
436: public void test100() throws Exception {
437:
438: // check if tables have been initialized
439: try {
440: getCustomerHome().findByPrimaryKey(new Integer(80));
441: } catch (Exception e) {
442: // Make the initialization needed for the tests
443: try {
444: utx.begin();
445: String cities[] = new String[6];
446: cities[0] = "Minneapolis";
447: cities[1] = "St. Paul";
448: cities[2] = "Rochester";
449: cities[3] = "Winona";
450: cities[4] = "Wayzata";
451: cities[5] = "Eagan";
452: ShipRemote shipA = getShipHome().create(
453: new Integer(10772), "Ship A", 30000.0);
454: CruiseRemote cruiseA = getCruiseHome().create(
455: "Cruise A", new Integer(10772));
456: for (int jj = 1; jj <= 10; jj++) {
457: ShipRemote ship = getShipHome().create(
458: new Integer(jj), "Ship " + jj,
459: 30000.0 + (10000.0 * jj));
460: }
461: cruiseA = null;
462: CruiseRemote cruiseB = null;
463: ShipRemote ship1 = getShipHome().findByPrimaryKey(
464: new Integer(1));
465: cruiseA = getCruiseHome().create("Alaska Cruise",
466: new Integer(1));
467: cruiseB = getCruiseHome().create("Bohemian Cruise",
468: new Integer(1));
469: for (int kk = 80; kk <= 99; kk++) {
470: CustomerRemote customer = null;
471:
472: customer = getCustomerHome()
473: .create(new Integer(kk));
474: customer.setName(new Name("Smith" + kk, "John"));
475: customer
476: .addPhoneNumber("612-555-12" + kk, (byte) 1);
477:
478: customer.setAddress("10" + kk + " Elm Street",
479: cities[(kk - 80) % 6], (kk % 2 == 0 ? "MN"
480: : "CA"), "5540" + (kk % 5 + 1));
481: customer.setHasGoodCredit((kk % 4 == 0));
482: if (kk % 4 == 0)
483: customer.setCreditCard(new Date(System
484: .currentTimeMillis() + 100000),
485: "100001" + kk, "MasterCard", "CA",
486: "CETELEM", "rue de jeunet", cities[0],
487: "France", "3888");
488: }
489: // Creating Customers 1-6, each with 2 reservations for 2 cabins
490: Calendar date = Calendar.getInstance();
491: date.set(2002, 10, 1);
492:
493: for (int kk = 201; kk < 207; kk++) {
494: Collection customers = new ArrayList();
495: CustomerRemote cust = getCustomerHome().create(
496: new Integer(kk));
497: cust.setName(new Name("Customer " + kk, "Mike"));
498: cust.setHasGoodCredit((kk % 2 == 0)); // odds are bums
499: cust.setAddress("50" + kk + " Main Street",
500: "Minneapolis", "MN", "5510" + kk);
501: customers.add(new Integer(kk)); // put this single customer in the collection
502:
503: Collection reservations = new ArrayList();
504:
505: for (int jj = 0; jj < 2; jj++) {
506:
507: ReservationRemote reservation = getReservationHome()
508: .create(cruiseA.getId(), customers);
509: reservation.setDate(date.getTime());
510: reservation.setAmountPaid(1000 * kk + 100 * jj
511: + 2000);
512:
513: date.add(Calendar.DAY_OF_MONTH, 7);
514:
515: Set cabins = new HashSet();
516: CabinRemote cabin = getCabinHome().create(
517: new Integer(1000 + kk * 100 + jj));
518: cabin.setDeckLevel(kk - 200);
519: cabin.setName("Cabin " + kk + "0" + jj + "1");
520:
521: cabins.add(cabin.getId());
522: cabin = getCabinHome().create(
523: new Integer(1000 + kk * 100 + 10 + jj));
524: cabin.setDeckLevel(kk - 200);
525: cabin.setName("Cabin " + kk + "0" + jj + "2");
526:
527: cabins.add(cabin.getId());
528:
529: reservation.setAllCabins(cabins); // this reservation has 2 cabins
530:
531: }
532: }
533: String fnames[] = new String[5];
534: fnames[0] = "John";
535: fnames[1] = "Paul";
536: fnames[2] = "Ringo";
537: fnames[3] = "Joe";
538: fnames[4] = "Roger";
539:
540: String lnames[] = new String[3];
541: lnames[0] = "Smith";
542: lnames[1] = "Johnson";
543: lnames[2] = "Star";
544: // Creating Customers 50-69
545: for (int kk = 50; kk <= 69; kk++) {
546: CustomerRemote customer = getCustomerHome().create(
547: new Integer(kk));
548: customer.setName(new Name(lnames[(kk - 50) % 3],
549: fnames[(kk - 50) % 5]));
550: customer
551: .addPhoneNumber("612-555-12" + kk, (byte) 1);
552: customer.setAddress("10" + kk + " Elm Street",
553: cities[(kk - 50) % 6], (kk % 2 == 0 ? "MN"
554: : "CA"), "5540" + (kk % 5 + 1));
555: customer.setHasGoodCredit((kk % 4 == 0));
556:
557: // Some customers will have reservations already on one of the two cruises..
558: if (kk % 3 != 0) {
559: Collection customers = new ArrayList();
560: customers.add(customer.getId()); // put this single customer in the collection
561: ReservationRemote reservation = getReservationHome()
562: .create(
563: (kk % 3 == 1 ? cruiseA.getId()
564: : cruiseB.getId()),
565: customers);
566: reservation.setDate(date.getTime());
567: reservation.setAmountPaid(10 * kk + 2000);
568: date.add(Calendar.DAY_OF_MONTH, 7);
569: }
570: }
571:
572: // Creating Customers 100-109
573:
574: for (int kk = 100; kk <= 109; kk++) {
575: CustomerRemote customer = getCustomerHome().create(
576: new Integer(kk));
577: customer.setName(new Name("Lennon" + kk, "Paul"));
578: customer
579: .addPhoneNumber("666-543-12" + kk, (byte) 1);
580:
581: customer.setAddress("10" + kk + " Abbey Road",
582: cities[(kk - 100) % 6], (kk % 2 == 0 ? "FL"
583: : "WA"), "5540" + (kk % 5 + 1));
584: customer.setHasGoodCredit((kk % 4 == 0));
585: }
586:
587: } catch (Exception i) {
588: i.printStackTrace(System.out);
589: fail("InitialState creation problem: " + i);
590: } finally {
591: try {
592: utx.commit();
593: } catch (Exception ii) {
594: }
595: }
596: }
597: }
598:
599: public static Test suite() {
600: return new TestSuite(F_ConEnlist.class);
601: }
602:
603: public static void main(String args[]) {
604: String testtorun = null;
605: // Get args
606: for (int argn = 0; argn < args.length; argn++) {
607: String s_arg = args[argn];
608: Integer i_arg;
609: if (s_arg.equals("-n")) {
610: testtorun = args[++argn];
611: }
612: }
613: if (testtorun == null) {
614: junit.textui.TestRunner.run(suite());
615: } else {
616: junit.textui.TestRunner.run(new F_ConEnlist(testtorun));
617: }
618: }
619: }
|