Source Code Cross Referenced for G_Relation_ombEC2.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » jtests » clients » entity » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.jtests.clients.entity 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * --------------------------------------------------------------------------
022:         * $Id: G_Relation_ombEC2.java 7533 2005-10-19 15:55:05Z durieuxp $
023:         * --------------------------------------------------------------------------
024:         */
025:
026:        package org.objectweb.jonas.jtests.clients.entity;
027:
028:        import java.util.ArrayList;
029:        import java.util.Collection;
030:        import java.util.Enumeration;
031:        import java.util.Hashtable;
032:        import java.util.Iterator;
033:
034:        import javax.naming.NamingException;
035:        import javax.rmi.PortableRemoteObject;
036:
037:        import junit.framework.Test;
038:        import junit.framework.TestSuite;
039:
040:        import org.objectweb.jonas.jtests.beans.relation.omb.AHomeRemote;
041:        import org.objectweb.jonas.jtests.beans.relation.omb.ARemote;
042:        import org.objectweb.jonas.jtests.beans.relation.omb.BHomeRemote;
043:        import org.objectweb.jonas.jtests.beans.relation.omb.BRemote;
044:
045:        /**
046:         * For testing one-to-many bidirectional relationships
047:         * @author Helene Joanin
048:         */
049:        public class G_Relation_ombEC2 extends A_Cmp2Util {
050:
051:            private static String BEAN_HOME_A = "relation_omb_AHome";
052:            protected static AHomeRemote ahome = null;
053:            private static String BEAN_HOME_B = "relation_omb_BHome";
054:            protected static BHomeRemote bhome = null;
055:
056:            static Hashtable a2b = new Hashtable();
057:            static String[][] b2a = { { "bs0", null }, { "bs1_1", "as1" },
058:                    { "bs2_1", "as2" }, { "bs2_2", "as2" }, { "bs2_3", "as2" },
059:                    { "bs3", null }, { "b0", null }, { "b1_1", "a1" },
060:                    { "b1_2", "a1" }, { "b1_3", "a1" }, { "b2_1", "a2" },
061:                    { "b2_2", "a2" }, { "b2_3", "a2" }, { "b3_1", "a3" } };
062:
063:            static {
064:                a2b.put("as0", new String[] {});
065:                a2b.put("as1", new String[] { "bs1_1" });
066:                a2b.put("as2", new String[] { "bs2_1", "bs2_2", "bs2_3" });
067:                a2b.put("a0", new String[] {});
068:                a2b.put("a1", new String[] { "b1_1", "b1_2", "b1_3" });
069:                a2b.put("a2", new String[] { "b2_1", "b2_2", "b2_3" });
070:                a2b.put("a3", new String[] { "b3_1" });
071:                a2b.put("ax0", new String[] {});
072:
073:                // Translate the String[] to a Collection of String
074:                for (Iterator it = a2b.keySet().iterator(); it.hasNext();) {
075:                    String aname = (String) (it.next());
076:                    String[] tb = (String[]) a2b.get(aname);
077:                    ArrayList col = new ArrayList(tb.length);
078:                    for (int i = 0; i < tb.length; i++) {
079:                        col.add(tb[i]);
080:                    }
081:                    a2b.put(aname, col);
082:                }
083:            }
084:
085:            public G_Relation_ombEC2(String name) {
086:                super (name);
087:            }
088:
089:            protected boolean isInit = false;
090:
091:            protected void setUp() {
092:                super .setUp();
093:                boolean ok = false;
094:                int nbtry = 0;
095:                while (!ok && nbtry < 3) {
096:                    if (!isInit) {
097:                        // load bean if not loaded yet
098:                        useBeans("omb", false);
099:                        try {
100:                            ahome = (AHomeRemote) PortableRemoteObject
101:                                    .narrow(ictx.lookup(BEAN_HOME_A),
102:                                            AHomeRemote.class);
103:                            bhome = (BHomeRemote) PortableRemoteObject
104:                                    .narrow(ictx.lookup(BEAN_HOME_B),
105:                                            BHomeRemote.class);
106:                            assertNotNull(ahome);
107:                            assertNotNull(bhome);
108:                        } catch (NamingException e) {
109:                            fail("Cannot get bean home: " + e.getMessage());
110:                        }
111:                        // check if tables have been initialized
112:                        try {
113:                            ahome.findByPrimaryKey("a0");
114:                        } catch (Exception e) {
115:                            try {
116:                                utx.begin();
117:                                bhome.create("bs0");
118:                                bhome.create("bs1_1");
119:                                bhome.create("bs2_1");
120:                                bhome.create("bs2_2");
121:                                bhome.create("bs2_3");
122:                                bhome.create("bs3");
123:                                bhome.create("b0");
124:                                bhome.create("b1_1");
125:                                bhome.create("b1_2");
126:                                bhome.create("b1_3");
127:                                bhome.create("b2_1");
128:                                bhome.create("b2_2");
129:                                bhome.create("b2_3");
130:                                bhome.create("b3_1");
131:                                ahome.create("as0");
132:                                ahome.create("as1").assignB(
133:                                        (Collection) a2b.get("as1"));
134:                                ahome.create("as2").assignB(
135:                                        (Collection) a2b.get("as2"));
136:                                ahome.create("a0");
137:                                ahome.create("a1").assignB(
138:                                        (Collection) a2b.get("a1"));
139:                                ahome.create("a2").assignB(
140:                                        (Collection) a2b.get("a2"));
141:                                ahome.create("a3").assignB(
142:                                        (Collection) a2b.get("a3"));
143:                                ahome.create("ax0");
144:                            } catch (Exception i) {
145:                                fail("InitialState creation problem: " + i);
146:                            } finally {
147:                                try {
148:                                    utx.commit();
149:                                } catch (Exception ii) {
150:                                }
151:                            }
152:                        }
153:                        isInit = true;
154:                    }
155:                    // Check that all is OK. Sometimes, a test has failed and has corrupted
156:                    // the bean state in the database. We must unload and reload the bean then.
157:                    nbtry++;
158:                    try {
159:                        if (initStateOK()) {
160:                            ok = true;
161:                        }
162:                    } catch (Exception e) {
163:                    }
164:                    if (!ok) {
165:                        isInit = false;
166:                        unloadBeans("omb");
167:                    }
168:                }
169:            }
170:
171:            /**
172:             * Check that we are in the same state as after the tables creation for thoses beans A and B
173:             * (ie if it is the initial state)
174:             */
175:            boolean initStateOK() throws Exception {
176:                boolean isOk = true;
177:                msgerror = new StringBuffer();
178:                // Check the relations A => B
179:                for (Enumeration ea = a2b.keys(); ea.hasMoreElements();) {
180:                    String aname = (String) (ea.nextElement());
181:                    ARemote a = ahome.findByPrimaryKey(aname);
182:                    Collection colActual = a.retrieveB();
183:                    Collection colExpected = (Collection) (a2b.get(aname));
184:                    if (!isCollectionEqual(colExpected, colActual)) {
185:                        isOk = false;
186:                        msgerror = msgerror.append("\nWrong relation for "
187:                                + aname + " (expected:" + colExpected
188:                                + ", found:" + colActual + ")");
189:                    }
190:                }
191:                // Check the relation B => A
192:                for (int i = 0; i < b2a.length; i++) {
193:                    BRemote b = bhome.findByPrimaryKey(b2a[i][0]);
194:                    String pkb = b.getId();
195:                    String pka = b.retrieveA();
196:                    if (b2a[i][1] == null && pka != null || b2a[i][1] != null
197:                            && !b2a[i][1].equals(pka)) {
198:                        isOk = false;
199:                        msgerror = msgerror.append("\nWrong relation for "
200:                                + pkb + " (expected:" + b2a[i][1] + ", found:"
201:                                + pka + ")");
202:                    }
203:
204:                }
205:                return isOk;
206:            }
207:
208:            /**
209:             * Check that the bean 'as0' has no relation.
210:             */
211:            public void tBasicGetEmptyA2B(int tx) throws Exception {
212:                Collection c = null;
213:                if ((tx == TX_CALL) || (tx == TX_RB)) {
214:                    utx.begin();
215:                }
216:                ARemote a = ahome.findByPrimaryKey("as0");
217:                if (tx == TX_CONT) {
218:                    c = a.retrieveBInNewTx();
219:                } else {
220:                    c = a.retrieveB();
221:                }
222:                if (tx == TX_CALL) {
223:                    utx.commit();
224:                } else if (tx == TX_RB) {
225:                    utx.rollback();
226:                }
227:                // checking
228:                checkIsInitialState();
229:            }
230:
231:            public void testBasicGetEmptyA2BTxNo() throws Exception {
232:                tBasicGetEmptyA2B(TX_NO);
233:            }
234:
235:            public void testBasicGetEmptyA2BTxCall() throws Exception {
236:                tBasicGetEmptyA2B(TX_CALL);
237:            }
238:
239:            public void testBasicGetEmptyA2BTxCont() throws Exception {
240:                tBasicGetEmptyA2B(TX_CONT);
241:            }
242:
243:            /**
244:             * Check that the bean 'bs0' has no relation.
245:             */
246:            public void tBasicGetEmptyB2A(int tx) throws Exception {
247:                if ((tx == TX_CALL) || (tx == TX_RB)) {
248:                    utx.begin();
249:                }
250:                String pka = null;
251:                BRemote b = bhome.findByPrimaryKey("bs0");
252:                if (tx == TX_CONT) {
253:                    pka = b.retrieveAInNewTx();
254:                } else {
255:                    pka = b.retrieveA();
256:                }
257:                if (tx == TX_CALL) {
258:                    utx.commit();
259:                } else if (tx == TX_RB) {
260:                    utx.rollback();
261:                }
262:                // checking
263:                checkIsInitialState();
264:            }
265:
266:            public void testBasicGetEmptyB2ANoTx() throws Exception {
267:                tBasicGetEmptyB2A(TX_NO);
268:            }
269:
270:            public void testBasicGetEmptyB2ATxCall() throws Exception {
271:                tBasicGetEmptyB2A(TX_CALL);
272:            }
273:
274:            public void testBasicGetEmptyB2ATxCont() throws Exception {
275:                tBasicGetEmptyB2A(TX_CONT);
276:            }
277:
278:            /**
279:             * Check that the bean 'as1' has only one relation with 'bs1_1'.
280:             */
281:            public void tBasicGetOneA2B(int tx) throws Exception {
282:
283:                Collection c = null;
284:                if ((tx == TX_CALL) || (tx == TX_RB)) {
285:                    utx.begin();
286:                }
287:                ARemote a = ahome.findByPrimaryKey("as1");
288:                if (tx == TX_CONT) {
289:                    c = a.retrieveBInNewTx();
290:                } else {
291:                    c = a.retrieveB();
292:                }
293:                if (tx == TX_CALL) {
294:                    utx.commit();
295:                } else if (tx == TX_RB) {
296:                    utx.rollback();
297:                }
298:                // checking
299:                assertEquals("Relations size of: ", 1, c.size());
300:                String s = "bs1_1";
301:                assertTrue("Relation missing " + s + ": ", c.contains(s));
302:                checkIsInitialState();
303:            }
304:
305:            /**
306:             * Check that the bean 'as2' has many relations with 'bs2_1', 'bs2_2', 'bs2_3'.
307:             */
308:            public void tBasicGetManyA2B(int tx) throws Exception {
309:
310:                Collection c = null;
311:                if ((tx == TX_CALL) || (tx == TX_RB)) {
312:                    utx.begin();
313:                }
314:                ARemote a = ahome.findByPrimaryKey("as2");
315:                if (tx == TX_CONT) {
316:                    c = a.retrieveBInNewTx();
317:                } else {
318:                    c = a.retrieveB();
319:                }
320:                if (tx == TX_CALL) {
321:                    utx.commit();
322:                } else if (tx == TX_RB) {
323:                    utx.rollback();
324:                }
325:                // checking
326:                assertEquals("Relations size of: ", 3, c.size());
327:                String s = "bs2_1";
328:                assertTrue("Relation missing " + s + ": ", c.contains(s));
329:                s = "bs2_2";
330:                assertTrue("Relation missing " + s + ": ", c.contains(s));
331:                s = "bs2_3";
332:                assertTrue("Relation missing " + s + ": ", c.contains(s));
333:                checkIsInitialState();
334:            }
335:
336:            /**
337:             * Set a A2B relation to empty.
338:             * Before: as1 <-> bs1_1
339:             * After:  as1     bs1_1
340:             * Check also the assignement rules for relationships.
341:             */
342:            public void tCohSetEmptyA2B(int tx) throws Exception {
343:                ArrayList ce = new ArrayList();
344:                if ((tx == TX_CALL) || (tx == TX_RB)) {
345:                    utx.begin();
346:                }
347:                ARemote a = ahome.findByPrimaryKey("as1");
348:                if (tx == TX_CONT) {
349:                    a.assignBInNewTx(ce);
350:                } else {
351:                    a.assignB(ce);
352:                }
353:                if (tx == TX_CALL) {
354:                    utx.commit();
355:                } else if (tx == TX_RB) {
356:                    utx.rollback();
357:                }
358:                if (tx != TX_RB) {
359:                    // checking
360:                    Collection ca = a.retrieveB();
361:                    assertEquals("Relations size for as1 ", 0, ca.size());
362:                    BRemote b = bhome.findByPrimaryKey("bs1_1");
363:                    assertNull("Bad relation for bs1_1", b.retrieveA());
364:                    // undo
365:                    a.addInB("bs1_1");
366:                }
367:                checkIsInitialState();
368:            }
369:
370:            /**
371:             * Set a A2B relation to empty by clearing the collection.
372:             * Before: as1 <-> bs1_1
373:             * After:  as1     bs1_1
374:             * Check also the assignement rules for relationships.
375:             */
376:            public void tCohClearA2B(int tx) throws Exception {
377:                if ((tx == TX_CALL) || (tx == TX_RB)) {
378:                    utx.begin();
379:                }
380:                ARemote a = ahome.findByPrimaryKey("as1");
381:                if (tx == TX_CONT) {
382:                    a.clearBInNewTx();
383:                } else {
384:                    a.clearB();
385:                }
386:                if (tx == TX_CALL) {
387:                    utx.commit();
388:                } else if (tx == TX_RB) {
389:                    utx.rollback();
390:                }
391:                if (tx != TX_RB) {
392:                    // checking
393:                    Collection c = a.retrieveB();
394:                    BRemote b = bhome.findByPrimaryKey("bs1_1");
395:                    String r = b.retrieveA();
396:                    assertEquals(
397:                            "Expected empty collection for as1 relation: ", 0,
398:                            c.size());
399:                    assertNull("Expected no relation for bs1_1", r);
400:                    // undo
401:                    b.assignA("as1");
402:                }
403:                checkIsInitialState();
404:            }
405:
406:            /**
407:             * Set an empty A2B relation with 1 element.
408:             * This added element was'nt already in an other relation.
409:             * Before: as0     b0
410:             * After:  as0 <-> b0
411:             * Check also the assignement rules for relationships.
412:             */
413:            public void tCohSetOneA2B(int tx) throws Exception {
414:                ArrayList ce = new ArrayList();
415:                ce.add("b0");
416:                if ((tx == TX_CALL) || (tx == TX_RB)) {
417:                    utx.begin();
418:                }
419:                ARemote a = ahome.findByPrimaryKey("as0");
420:                if (tx == TX_CONT) {
421:                    a.assignBInNewTx(ce);
422:                } else {
423:                    a.assignB(ce);
424:                }
425:                if (tx == TX_CALL) {
426:                    utx.commit();
427:                } else if (tx == TX_RB) {
428:                    utx.rollback();
429:                }
430:                if (tx != TX_RB) {
431:                    // checking
432:                    Collection ca = a.retrieveB();
433:                    BRemote b = bhome.findByPrimaryKey("b0");
434:                    String r = b.retrieveA();
435:                    assertTrue("Bad relations for as0 (required:" + ce
436:                            + ", found:" + ca + ")", isCollectionEqual(ce, ca));
437:                    assertEquals("Bad relation for b0: ", "as0", r);
438:                    // undo
439:                    a.assignB(new ArrayList());
440:                }
441:                checkIsInitialState();
442:            }
443:
444:            /**
445:             * Set a null B2A relation with 1 element.
446:             * This element was'nt already in an other relation.
447:             * Before: b0     as0
448:             * After:  b0 <-> as0
449:             * Check also the assignement rules for relationships.
450:             */
451:            public void tCohSet1B2A(int tx) throws Exception {
452:                if ((tx == TX_CALL) || (tx == TX_RB)) {
453:                    utx.begin();
454:                }
455:                BRemote b = bhome.findByPrimaryKey("b0");
456:                if (tx == TX_CONT) {
457:                    b.assignAInNewTx("as0");
458:                } else {
459:                    b.assignA("as0");
460:                }
461:                if (tx == TX_CALL) {
462:                    utx.commit();
463:                } else if (tx == TX_RB) {
464:                    utx.rollback();
465:                }
466:                // checking
467:                String r = b.retrieveA();
468:                ARemote a = ahome.findByPrimaryKey("as0");
469:                Collection ca = a.retrieveB();
470:                ArrayList ce = new ArrayList();
471:                ce.add("b0");
472:                if (tx != TX_RB) {
473:                    assertEquals("Bad relation for b0: ", "as0", r);
474:                    assertTrue("Bad relations for as0 (required:" + ce
475:                            + ", found:" + ca + ")", isCollectionEqual(ce, ca));
476:                    // undo
477:                    b.assignA(null);
478:                }
479:                checkIsInitialState();
480:            }
481:
482:            /**
483:             * Add an element to a empty relation.
484:             * Same as tCohSetOneA2B except that we add the element in the collection
485:             * instead of we set directly the collection.
486:             * Before: as0     bs0
487:             * After:  as0 <-> bs0
488:             * Check also the assignement rules for relationships.
489:             */
490:            public void tCohAddOneA2B(int tx) throws Exception {
491:                ArrayList ce = new ArrayList();
492:                ce.add("bs0");
493:                if ((tx == TX_CALL) || (tx == TX_RB)) {
494:                    utx.begin();
495:                }
496:                ARemote a = ahome.findByPrimaryKey("as0");
497:                if (tx == TX_CONT) {
498:                    a.addInBInNewTx("bs0");
499:                } else {
500:                    a.addInB("bs0");
501:                }
502:                if (tx == TX_CALL) {
503:                    utx.commit();
504:                } else if (tx == TX_RB) {
505:                    utx.rollback();
506:                }
507:                if (tx != TX_RB) {
508:                    // checking
509:                    Collection ca = a.retrieveB();
510:                    BRemote b = bhome.findByPrimaryKey("bs0");
511:                    String r = b.retrieveA();
512:                    assertTrue("Bad relations for as0 (required:" + ce
513:                            + ", found:" + ca + ")", isCollectionEqual(ce, ca));
514:                    assertEquals("Bad relation for bs0", "as0", r);
515:                    // undo
516:                    a.assignB(new ArrayList());
517:                }
518:                checkIsInitialState();
519:            }
520:
521:            /**
522:             * Re-Set a relation with N element.
523:             * Before: as1 <-> bs1_1
524:             *                 bs0
525:             *                 bs3
526:             * After:          bs1_1
527:             *         as1 <-> bs0
528:             *         as1 <-> bs3
529:             */
530:            public void tCohSetMultiA2B(int tx) throws Exception {
531:                ArrayList ce = new ArrayList(2);
532:                ce.add("bs0");
533:                ce.add("bs3");
534:                if ((tx == TX_CALL) || (tx == TX_RB)) {
535:                    utx.begin();
536:                }
537:                ARemote a = ahome.findByPrimaryKey("as1");
538:                Collection co = a.retrieveB();
539:                if (tx == TX_CONT) {
540:                    a.assignBInNewTx(ce);
541:                } else {
542:                    a.assignB(ce);
543:                }
544:                if (tx == TX_CALL) {
545:                    utx.commit();
546:                } else if (tx == TX_RB) {
547:                    utx.rollback();
548:                }
549:                if (tx != TX_RB) {
550:                    // checking
551:                    Collection ca = a.retrieveB();
552:                    BRemote bs1_1 = bhome.findByPrimaryKey("bs1_1");
553:                    BRemote bs0 = bhome.findByPrimaryKey("bs0");
554:                    BRemote bs3 = bhome.findByPrimaryKey("bs3");
555:                    assertTrue("Bad relations for as1 (required:" + ce
556:                            + ", found:" + ca + ")", isCollectionEqual(ce, ca));
557:                    assertNull("Not null relation for bs1_1", bs1_1.retrieveA());
558:                    assertEquals("Bad relation for bs0: ", "as1", bs0
559:                            .retrieveA());
560:                    assertEquals("Bad relation for bs3: ", "as1", bs3
561:                            .retrieveA());
562:                    // undo
563:                    a.assignB(co);
564:                }
565:                checkIsInitialState();
566:            }
567:
568:            /**
569:             * Set a B2A relation with 1 element.
570:             * This element wasn't already in an other relation.
571:             * Before: bs1_1 <-> as1
572:             *                   as0
573:             * Change: bs1_1.assignA(as0);
574:             * After:  bs1_1 <-> as0
575:             *                   as1
576:             * Check also the assignement rules for relationships.
577:             *
578:             */
579:            public void tCohSet2B2A(int tx) throws Exception {
580:                if ((tx == TX_CALL) || (tx == TX_RB)) {
581:                    utx.begin();
582:                }
583:                BRemote b = bhome.findByPrimaryKey("bs1_1");
584:                if (tx == TX_CONT) {
585:                    b.assignAInNewTx("as0");
586:                } else {
587:                    b.assignA("as0");
588:                }
589:                if (tx == TX_CALL) {
590:                    utx.commit();
591:                } else if (tx == TX_RB) {
592:                    utx.rollback();
593:                }
594:                if (tx != TX_RB) {
595:                    // checking
596:                    String r = b.retrieveA();
597:                    assertEquals("Bad relation for bs1_1: ", "as0", r);
598:                    ARemote as0 = ahome.findByPrimaryKey("as0");
599:                    Collection cas0 = as0.retrieveB();
600:                    ARemote as1 = ahome.findByPrimaryKey("as1");
601:                    Collection cas1 = as1.retrieveB();
602:                    ArrayList ce = new ArrayList();
603:                    ce.add("bs1_1");
604:                    assertTrue("Bad relations for as0 (required:" + ce
605:                            + ", found:" + cas0 + ")", isCollectionEqual(ce,
606:                            cas0));
607:                    ce = new ArrayList();
608:                    assertTrue("Bad relations for as1 (required:" + ce
609:                            + ", found:" + cas1 + ")", isCollectionEqual(ce,
610:                            cas1));
611:                    // undo
612:                    b.assignA("as1");
613:                }
614:                checkIsInitialState();
615:            }
616:
617:            /**
618:             * Set a B2A relation with 1 element.
619:             * This element was already in an other relation.
620:             * Before: bs1_1 <-> as1
621:             *         b3_1  <-> a3
622:             * Change: bs1_1.assignA(b3_1.retrieveA())
623:             * After:  bs1_1 <-> a3
624:             *         b3_1  <->
625:             *                   as1
626:             * Check also the assignement rules for relationships.
627:             *
628:             * See Spec chapter 10.3.7.3, change example: b2m.setA(b1n.getA())
629:             */
630:            public void tCohSet3B2A(int tx) throws Exception {
631:                if ((tx == TX_CALL) || (tx == TX_RB)) {
632:                    utx.begin();
633:                }
634:                BRemote bs1_1 = bhome.findByPrimaryKey("bs1_1");
635:                if (tx == TX_CONT) {
636:                    bs1_1.assignAInNewTx("a3");
637:                } else {
638:                    bs1_1.assignA("a3");
639:                }
640:                if (tx == TX_CALL) {
641:                    utx.commit();
642:                } else if (tx == TX_RB) {
643:                    utx.rollback();
644:                }
645:                if (tx != TX_RB) {
646:                    // checking
647:                    String rbs1_1 = bs1_1.retrieveA();
648:                    ARemote as1 = ahome.findByPrimaryKey("as1");
649:                    Collection cas1 = as1.retrieveB();
650:                    ARemote a3 = ahome.findByPrimaryKey("a3");
651:                    Collection ca3 = a3.retrieveB();
652:                    assertEquals("Bad relation for bs1_1: ", "a3", rbs1_1);
653:                    ArrayList ce = new ArrayList();
654:                    ce.add("bs1_1");
655:                    ce.add("b3_1");
656:                    assertTrue("Bad relations for a3 (required:" + ce
657:                            + ", found:" + ca3 + ")",
658:                            isCollectionEqual(ce, ca3));
659:                    ce = new ArrayList();
660:                    assertTrue("Bad relations for as1 (required:" + ce
661:                            + ", found:" + cas1 + ")", isCollectionEqual(ce,
662:                            cas1));
663:                    // undo
664:                    bs1_1.assignA("as1");
665:                }
666:                checkIsInitialState();
667:            }
668:
669:            /**
670:             * Remove an element in a relation.
671:             * Before: a3 <-> b3_1
672:             * Change: a3.retrieveB().remove(b3_1)
673:             * After:  a3     b3_1
674:             * See Spec chapter 10.3.7.3, change example: a1.getB().remove(b1n)
675:             */
676:            public void tCohRemoveInRelA2B(int tx) throws Exception {
677:                if ((tx == TX_CALL) || (tx == TX_RB)) {
678:                    utx.begin();
679:                }
680:                ARemote a = ahome.findByPrimaryKey("a3");
681:                if (tx == TX_CONT) {
682:                    a.removeFromBInNewTx("b3_1");
683:                } else {
684:                    a.removeFromB("b3_1");
685:                }
686:                if (tx == TX_CALL) {
687:                    utx.commit();
688:                } else if (tx == TX_RB) {
689:                    utx.rollback();
690:                }
691:                if (tx != TX_RB) {
692:                    // checking
693:                    Collection ca = a.retrieveB();
694:                    BRemote b = bhome.findByPrimaryKey("b3_1");
695:                    String r = b.retrieveA();
696:                    ArrayList ce = new ArrayList();
697:                    assertTrue("Bad relations for a3: (required:" + ce
698:                            + ", found:" + ca + ")", isCollectionEqual(ce, ca));
699:                    assertNull("Bad not null relation for b3_1", r);
700:                    // undo
701:                    a.addInB("b3_1");
702:                }
703:                // check to initial state
704:                checkIsInitialState();
705:
706:            }
707:
708:            /**
709:             * Remove a bean B which is in a relation.
710:             * Before: a3 <-> b3_1
711:             * change: B.remove(b3_1)
712:             * After:  a3
713:             *
714:             */
715:            public void tCohRemoveB(int tx) throws Exception {
716:                if (tx == TX_CONT) {
717:                    // The transaction attribute of the remove method is TX_SUPPORT,
718:                    // so the transaction cannot be initiate by the container
719:                    fail("Transaction cannot be initiate by the container for this test");
720:                }
721:                if ((tx == TX_CALL) || (tx == TX_RB)) {
722:                    utx.begin();
723:                }
724:                bhome.remove("b3_1");
725:                if (tx == TX_CALL) {
726:                    utx.commit();
727:                } else if (tx == TX_RB) {
728:                    utx.rollback();
729:                }
730:                if (tx != TX_RB) {
731:                    // checking
732:                    ARemote a = ahome.findByPrimaryKey("a3");
733:                    Collection ca = a.retrieveB();
734:                    assertEquals(
735:                            "Wrong relations size for a3: (expected: 0 found:"
736:                                    + ca.size(), 0, ca.size());
737:                    // undo
738:                    bhome.create("b3_1");
739:                    a.addInBInNewTx("b3_1");
740:                }
741:                checkIsInitialState();
742:            }
743:
744:            /**
745:             * Remove a bean B which is in a relation.
746:             * Same as tCohRemoveB except that the called remove method is on the bean
747:             * instead of the home.
748:             * Before: a3 <-> b3_1
749:             * change: B.remove(b3_1)
750:             * After:  a3
751:             *
752:             */
753:            public void tCohBeanRemoveB(int tx) throws Exception {
754:                if (tx == TX_CONT) {
755:                    // The transaction attribute of the remove method is TX_SUPPORT,
756:                    // so the transaction cannot be initiate by the container
757:                    fail("Transaction cannot be initiate by the container for this test");
758:                }
759:                if ((tx == TX_CALL) || (tx == TX_RB)) {
760:                    utx.begin();
761:                }
762:                BRemote b = bhome.findByPrimaryKey("b3_1");
763:                b.remove();
764:                if (tx == TX_CALL) {
765:                    utx.commit();
766:                } else if (tx == TX_RB) {
767:                    utx.rollback();
768:                }
769:                if (tx != TX_RB) {
770:                    // checking
771:                    ARemote a = ahome.findByPrimaryKey("a3");
772:                    Collection ca = a.retrieveB();
773:                    assertEquals(
774:                            "Wrong relations size for a3: (expected: 0 found:"
775:                                    + ca.size(), 0, ca.size());
776:                    // undo
777:                    bhome.create("b3_1");
778:                    a.addInBInNewTx("b3_1");
779:                }
780:                checkIsInitialState();
781:            }
782:
783:            /**
784:             * Set a empty relation A2B with 1 element.
785:             * This added element was already in a other relation.
786:             * Before: a0
787:             *         a3 <-> b3_1
788:             * Change: a0.assignB({b3_1})
789:             * After:  a0 <-> b3_1
790:             *         a3
791:             */
792:            public void tCohSetOne2A2B(int tx) throws Exception {
793:                ArrayList ce = new ArrayList();
794:                ce.add("b3_1");
795:                if ((tx == TX_CALL) || (tx == TX_RB)) {
796:                    utx.begin();
797:                }
798:                ARemote a = ahome.findByPrimaryKey("a0");
799:                if (tx == TX_CONT) {
800:                    a.assignBInNewTx(ce);
801:                } else {
802:                    a.assignB(ce);
803:                }
804:                if (tx == TX_CALL) {
805:                    utx.commit();
806:                } else if (tx == TX_RB) {
807:                    utx.rollback();
808:                }
809:                if (tx != TX_RB) {
810:                    // checking
811:                    Collection ca = a.retrieveB();
812:                    ARemote a3 = ahome.findByPrimaryKey("a3");
813:                    Collection ca3 = a3.retrieveB();
814:                    BRemote b = bhome.findByPrimaryKey("b3_1");
815:                    String r = b.retrieveA();
816:                    assertTrue("Wrong relations a0 (required:" + ce
817:                            + ", found:" + ca + ")", isCollectionEqual(ce, ca));
818:                    assertTrue("Wrong relations for b3_1" + ": found:" + ca3,
819:                            !ca3.contains("b3_1"));
820:                    assertEquals("Bad relation for b3_1: ", "a0", r);
821:                    // undo
822:                    a.assignB(new ArrayList());
823:                    a3.addInB("b3_1");
824:                }
825:                checkIsInitialState();
826:            }
827:
828:            /**
829:             * Add an element to a relation A2B already multiple.
830:             * The element was already in a relation.
831:             * Check also the assignement rules for relationships.
832:             * Before: a1 <-> b1_1
833:             *            <-> b1_2
834:             *            <-> b1_3
835:             *         a3 <-> b3_1
836:             * Change: a1.addInB(b3_1)
837:             * After:  a1 <-> b1_1
838:             *            <-> b1_2
839:             *            <-> b1_3
840:             *            <-> b3_1
841:             *         a3
842:             *
843:             * See Spec chapter 10.3.7.3, change example: a1.getB().add(b2m)
844:             *
845:             */
846:            public void tCohAddOneInMultiA2B(int tx) throws Exception {
847:
848:                ArrayList ce = new ArrayList();
849:                ce.add("b1_1");
850:                ce.add("b1_2");
851:                ce.add("b1_3");
852:                String bAdded = "b3_1";
853:                if ((tx == TX_CALL) || (tx == TX_RB)) {
854:                    utx.begin();
855:                }
856:                ARemote a = ahome.findByPrimaryKey("a1");
857:                if (tx == TX_CONT) {
858:                    a.addInBInNewTx(bAdded);
859:                } else {
860:                    a.addInB(bAdded);
861:                }
862:                ce.add(bAdded);
863:                if (tx == TX_CALL) {
864:                    utx.commit();
865:                } else if (tx == TX_RB) {
866:                    utx.rollback();
867:                }
868:                if (tx != TX_RB) {
869:                    // checking
870:                    Collection ca1 = a.retrieveB();
871:                    ARemote a3 = ahome.findByPrimaryKey("a3");
872:                    Collection ca3 = a3.retrieveB();
873:                    assertTrue("Bad relations for a1 (required:" + ce
874:                            + ", found:" + ca1 + ")",
875:                            isCollectionEqual(ce, ca1));
876:                    ce = new ArrayList();
877:                    assertTrue("Bad relations for a3 (required:" + ce
878:                            + ", found:" + ca3 + ")",
879:                            isCollectionEqual(ce, ca3));
880:                    BRemote b = bhome.findByPrimaryKey(bAdded);
881:                    assertEquals("Bad relation for b3_1: ", "a1", b.retrieveA());
882:                    // undo
883:                    a3.addInB(bAdded);
884:                }
885:
886:                checkIsInitialState();
887:            }
888:
889:            /*
890:             * Set a relation to multiple.
891:             * All of element was already in an other relation.
892:             * Check also the assignement rules for relationships.
893:             * Before: a1 <-> b1_1
894:             *            <-> b1_2
895:             *            <-> b1_3
896:             *         a2 <-> b2_1
897:             *            <-> b2_2
898:             *            <-> b2_3
899:             * Change: a1.assignB(a2.retrieveB())
900:             * After:  a1 <-> b2_1
901:             *            <-> b2_2
902:             *            <-> b2_3
903:             *         a2
904:             *               b1_1
905:             *               b1_2
906:             *               b1_3
907:             *
908:             * Spec chapter 10.3.7.3, change example: a1.setB(a2.getB())
909:             *
910:             */
911:            public void tCohSetMultiBisA2B(int tx) throws Exception {
912:                if ((tx == TX_CALL) || (tx == TX_RB)) {
913:                    utx.begin();
914:                }
915:                ARemote a1 = ahome.findByPrimaryKey("a1");
916:                Collection co1 = a1.retrieveB();
917:                ARemote a2 = ahome.findByPrimaryKey("a2");
918:                Collection co2 = a2.retrieveB();
919:                if (tx == TX_CONT) {
920:                    a1.assignBInNewTx(co2);
921:                } else {
922:                    a1.assignB(co2);
923:                }
924:                if (tx == TX_CALL) {
925:                    utx.commit();
926:                } else if (tx == TX_RB) {
927:                    utx.rollback();
928:                }
929:                if (tx != TX_RB) {
930:                    // checking
931:                    Collection ca1 = a1.retrieveB();
932:                    assertTrue("Wrong relations for a1 (required:" + co2
933:                            + ", found:" + ca1 + ")", isCollectionEqual(co2,
934:                            ca1));
935:                    Collection ca2 = a2.retrieveB();
936:                    assertEquals("Wrong relations size for a2: ", 0, ca2.size());
937:                    BRemote b;
938:                    b = bhome.findByPrimaryKey("b1_1");
939:                    assertNull("Wrong not null relation for b1_1: ", b
940:                            .retrieveA());
941:                    b = bhome.findByPrimaryKey("b1_2");
942:                    assertNull("Wrong not null relation for b1_2: ", b
943:                            .retrieveA());
944:                    b = bhome.findByPrimaryKey("b1_3");
945:                    assertNull("Wrong not null relation for b1_3: ", b
946:                            .retrieveA());
947:                    b = bhome.findByPrimaryKey("b2_1");
948:                    assertEquals("Wrong relation for b2_1: ", "a1", b
949:                            .retrieveA());
950:                    b = bhome.findByPrimaryKey("b2_2");
951:                    assertEquals("Wrong relation for b2_2: ", "a1", b
952:                            .retrieveA());
953:                    b = bhome.findByPrimaryKey("b2_3");
954:                    assertEquals("Wrong relation for b2_3: ", "a1", b
955:                            .retrieveA());
956:                    // Undo
957:                    a1.assignB(co1);
958:                    a2.assignB(co2);
959:                }
960:                checkIsInitialState();
961:            }
962:
963:            public static Test suite() {
964:                return new TestSuite(G_Relation_ombEC2.class);
965:            }
966:
967:            public static void main(String args[]) {
968:                String testtorun = null;
969:                // Get args
970:                for (int argn = 0; argn < args.length; argn++) {
971:                    String s_arg = args[argn];
972:                    Integer i_arg;
973:                    if (s_arg.equals("-n")) {
974:                        testtorun = args[++argn];
975:                    }
976:                }
977:                if (testtorun == null) {
978:                    junit.textui.TestRunner.run(suite());
979:                } else {
980:                    junit.textui.TestRunner
981:                            .run(new G_Relation_ombEC2(testtorun));
982:                }
983:            }
984:
985:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.