KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > InheritanceMultipleTableTest


1 package org.apache.ojb.broker;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.apache.commons.lang.SerializationUtils;
11 import org.apache.commons.lang.builder.EqualsBuilder;
12 import org.apache.commons.lang.builder.ToStringBuilder;
13 import org.apache.commons.lang.builder.ToStringStyle;
14 import org.apache.ojb.broker.metadata.ClassDescriptor;
15 import org.apache.ojb.broker.query.Criteria;
16 import org.apache.ojb.broker.query.Query;
17 import org.apache.ojb.broker.query.QueryByCriteria;
18 import org.apache.ojb.broker.query.QueryFactory;
19 import org.apache.ojb.broker.util.ObjectModification;
20 import org.apache.ojb.junit.PBTestCase;
21
22 /**
23  * These tests check inheritance using multiple tables via 1:1 reference and "super" keyword in
24  * reference descriptor. The test objects use a composite PK.
25  * One autoincrement PK field - Integer. One non-autoincrement PK field with manually set PK- Long.
26  *
27  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
28  * @version $Id: InheritanceMultipleTableTest.java,v 1.7.2.20 2005/12/29 22:18:21 arminw Exp $
29  */

30 public class InheritanceMultipleTableTest extends PBTestCase
31 {
32     public static void main(String JavaDoc[] args)
33     {
34         junit.textui.TestRunner.main(new String JavaDoc[]{InheritanceMultipleTableTest.class.getName()});
35     }
36
37     public void testLookupByIdentity()
38     {
39         long timestamp = System.currentTimeMillis();
40         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
41         String JavaDoc name = "testLookupByIdentity_" + timestamp;
42         Employee em1 = new Employee(id_2, "employee_" + name);
43         Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null);
44         Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null);
45         ArrayList JavaDoc list = new ArrayList JavaDoc();
46         list.add(ex1);
47         list.add(ex2);
48         Manager m1 = new Manager(id_2, "manager_" + name);
49         m1.setExecutives(list);
50
51         broker.beginTransaction();
52         broker.store(em1);
53         broker.store(m1);
54         broker.commitTransaction();
55
56         Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
57         Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
58         Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
59
60         broker.clearCache();
61
62         Employee newEm1 = (Employee) broker.getObjectByIdentity(em1_oid);
63         Executive newEx1 = (Executive) broker.getObjectByIdentity(ex1_oid);
64         Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
65
66         assertNotNull(newEm1);
67         assertNotNull(newEx1);
68         assertNotNull(newM1);
69
70         assertEquals(em1.getId(), newEm1.getId());
71         assertEquals(em1.getId_2(), newEm1.getId_2());
72         assertEquals(2, newM1.getExecutives().size());
73
74         assertEquals(m1.getId(), newM1.getId());
75         assertEquals(m1.getId_2(), newM1.getId_2());
76         assertEquals(2, newM1.getExecutives().size());
77
78         assertEquals(ex1.getId(), newEx1.getId());
79         assertEquals(ex1.getId_2(), newEx1.getId_2());
80         assertEquals(ex1.getDepartment(), newEx1.getDepartment());
81     }
82
83     public void testLookupByQuery()
84     {
85         long timestamp = System.currentTimeMillis();
86         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
87         String JavaDoc name = "testLookupByIdentity_" + timestamp;
88         Employee em1 = new Employee(id_2, "employee_" + name);
89         Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null);
90         Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null);
91         ArrayList JavaDoc list = new ArrayList JavaDoc();
92         list.add(ex1);
93         list.add(ex2);
94         Manager m1 = new Manager(id_2, "manager_" + name);
95         m1.setExecutives(list);
96
97         broker.beginTransaction();
98         broker.store(em1);
99         broker.store(m1);
100         broker.commitTransaction();
101
102         Criteria crit = new Criteria();
103         crit.addEqualTo("name", "employee_" + name);
104         Query q = QueryFactory.newQuery(Employee.class, crit);
105         Employee newEm1 = (Employee) broker.getObjectByQuery(q);
106
107         crit = new Criteria();
108         crit.addEqualTo("name", "executive_" + name);
109         q = QueryFactory.newQuery(Employee.class, crit);
110         Executive newEx1 = (Executive) broker.getObjectByQuery(q);
111
112         crit = new Criteria();
113         crit.addEqualTo("name", "manager_" + name);
114         q = QueryFactory.newQuery(Employee.class, crit);
115         Manager newM1 = (Manager) broker.getObjectByQuery(q);
116         
117         broker.clearCache();
118
119         assertNotNull(newEm1);
120         assertNotNull(newEx1);
121         assertNotNull(newM1);
122         assertEquals(2, newM1.getExecutives().size());
123         assertEquals(em1.getId(), newEm1.getId());
124         assertEquals(em1.getId_2(), newEm1.getId_2());
125
126         assertEquals(m1.getId(), newM1.getId());
127         assertEquals(m1.getId_2(), newM1.getId_2());
128
129         assertEquals(ex1.getId(), newEx1.getId());
130         assertEquals(ex1.getId_2(), newEx1.getId_2());
131     }
132
133     public void testQueryInheritancedObjects()
134     {
135         if(ojbSkipKnownIssueProblem("Classes mapped to multiple joined tables will always be instantiated " +
136                 " with the class type of the query, instead of the real type"))
137         {
138             return;
139         }
140
141         long timestamp = System.currentTimeMillis();
142         String JavaDoc name = "testQueryInheritancedObjects_" + timestamp;
143         // store company with Employee/Executive/Manager
144
Company company = prepareTestDataWithCompany(name);
145         Long JavaDoc id_2 = company.getId();
146         
147         // add Shareholder too
148
Shareholder shareholder = new Shareholder(id_2, name);
149         shareholder.setShare(77);
150         shareholder.setDepartment("none");
151         AddressIF ad = new Address(name);
152         shareholder.setAddress(ad);
153
154         broker.beginTransaction();
155         broker.store(shareholder);
156         broker.commitTransaction();
157
158         broker.clearCache();
159         // now we expect 7 objects when query for all Employee (this is the base class)
160
Criteria crit = new Criteria();
161         crit.addEqualTo("id_2", id_2);
162         crit.addLike("name", "%" + name + "%");
163         Query query = QueryFactory.newQuery(Employee.class, crit);
164         Collection JavaDoc result = broker.getCollectionByQuery(query);
165         assertEquals(7, result.size());
166         int employeeCount = 0;
167         int executiveCount = 0;
168         int managerCount = 0;
169         int shareholderCount = 0;
170         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
171         {
172             Object JavaDoc obj = iterator.next();
173             if(obj instanceof Employee) ++employeeCount;
174             if(obj instanceof Executive) ++executiveCount;
175             if(obj instanceof Manager) ++managerCount;
176             if(obj instanceof Shareholder) ++shareholderCount;
177         }
178         assertEquals(7, employeeCount);
179         assertEquals(6, executiveCount);
180         assertEquals(4, managerCount);
181         assertEquals(1, shareholderCount);
182
183         broker.clearCache();
184         // now we expect 4 objects when query for all Manager
185
crit = new Criteria();
186         crit.addEqualTo("id_2", id_2);
187         crit.addLike("name", "%" + name + "%");
188         query = QueryFactory.newQuery(Manager.class, crit);
189         result = broker.getCollectionByQuery(query);
190         assertEquals(4, result.size());
191         employeeCount = 0;
192         executiveCount = 0;
193         managerCount = 0;
194         shareholderCount = 0;
195         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
196         {
197             Object JavaDoc obj = iterator.next();
198             if(obj instanceof Employee) ++employeeCount;
199             if(obj instanceof Executive) ++executiveCount;
200             if(obj instanceof Manager) ++managerCount;
201             if(obj instanceof Shareholder) ++shareholderCount;
202         }
203         assertEquals(4, employeeCount);
204         assertEquals(4, executiveCount);
205         assertEquals(4, managerCount);
206         assertEquals(1, shareholderCount);
207
208         broker.clearCache();
209         // now we expect 1 objects when query for all Shareholder
210
crit = new Criteria();
211         crit.addEqualTo("id_2", id_2);
212         crit.addLike("name", "%" + name + "%");
213         query = QueryFactory.newQuery(Shareholder.class, crit);
214         result = broker.getCollectionByQuery(query);
215         assertEquals(1, result.size());
216         employeeCount = 0;
217         executiveCount = 0;
218         managerCount = 0;
219         shareholderCount = 0;
220         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
221         {
222             Object JavaDoc obj = iterator.next();
223             if(obj instanceof Employee) ++employeeCount;
224             if(obj instanceof Executive) ++executiveCount;
225             if(obj instanceof Manager) ++managerCount;
226             if(obj instanceof Shareholder) ++shareholderCount;
227         }
228         assertEquals(1, employeeCount);
229         assertEquals(1, executiveCount);
230         assertEquals(1, managerCount);
231         assertEquals(1, shareholderCount);
232     }
233
234     public void testQueryInheritancedObjectsById()
235     {
236         long timestamp = System.currentTimeMillis();
237         String JavaDoc name = "testQueryInheritancedObjectsByPk_" + timestamp;
238         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
239
240         List JavaDoc insertedObjs = prepareForQueryTests(id_2, name);
241
242         // add Shareholder
243
Shareholder shareholder = new Shareholder(id_2, name);
244         shareholder.setShare(77);
245         shareholder.setDepartment("none");
246         AddressIF ad = new Address(name);
247         shareholder.setAddress(ad);
248
249         broker.beginTransaction();
250         broker.store(shareholder);
251         broker.commitTransaction();
252            
253         broker.clearCache();
254
255         Employee emp1;
256         Identity ident;
257         Employee retrievedEmp;
258
259         // retrieve Manager by pk
260
emp1 = (Employee) insertedObjs.get(0);
261         ident = broker.serviceIdentity().buildIdentity(emp1);
262         retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
263         assertNotNull(retrievedEmp);
264         assertSame(Manager.class, retrievedEmp.getClass());
265         assertEquals(emp1, retrievedEmp);
266
267         // retrieve Executive by pk
268
emp1 = (Employee) insertedObjs.get(3);
269         ident = broker.serviceIdentity().buildIdentity(emp1);
270         retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
271         assertNotNull(retrievedEmp);
272         assertSame(Executive.class, retrievedEmp.getClass());
273         assertEquals(emp1, retrievedEmp);
274         
275         // retrieve Employee by pk
276
emp1 = (Employee) insertedObjs.get(5);
277         ident = broker.serviceIdentity().buildIdentity(emp1);
278         retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
279         assertNotNull(retrievedEmp);
280         assertSame(Employee.class, retrievedEmp.getClass());
281         assertEquals(emp1, retrievedEmp);
282
283         // retrieve Shareholder by pk
284
emp1 = shareholder;
285         ident = broker.serviceIdentity().buildIdentity(emp1);
286         retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
287         assertNotNull(retrievedEmp);
288         assertSame(Shareholder.class, retrievedEmp.getClass());
289         assertEquals(emp1, retrievedEmp);
290     }
291
292     public void testJavaInheritance()
293     {
294         ojbSkipKnownIssueProblem("Declared inheritance (without java inheritance)" +
295                 "of classes is currently not supported and will be difficult to implement");
296
297         String JavaDoc name = "testWithoutJavaInheritance_tmp" + System.currentTimeMillis();
298         Animal animal = new Animal(name, 55);
299         Food f1 = new Food(name + "fruit1");
300         Food f2 = new Food(name + "fruit2");
301         animal.addFood(f1);
302         animal.addFood(f2);
303         // animal.setParent(animal);
304

305         broker.beginTransaction();
306         broker.store(animal);
307         broker.commitTransaction();
308         Identity oid = broker.serviceIdentity().buildIdentity(animal);
309
310         broker.clearCache();
311         Animal newAnimal = (Animal) broker.getObjectByIdentity(oid);
312         assertTrue(animal.equals(newAnimal));
313
314         Criteria crit = new Criteria();
315         crit.addEqualTo("name", name);
316         Query q = QueryFactory.newQuery(Animal.class, crit);
317         Collection JavaDoc result = broker.getCollectionByQuery(q);
318         assertNotNull(result);
319         assertEquals(1, result.size());
320         newAnimal = (Animal) result.iterator().next();
321         assertTrue(animal.equals(newAnimal));
322     }
323
324     public void testInheritancedObjectsInCollectionReferences()
325     {
326         if(ojbSkipKnownIssueProblem("References of classes (1:1, 1:n) mapped to multiple joined tables only" +
327                 " return base class type instances"))
328         {
329             return;
330         }
331
332         long timestamp = System.currentTimeMillis();
333         String JavaDoc name = "testInheritancedObjectsInCollectionReferences_" + timestamp;
334         Company company = prepareTestDataWithCompany(name);
335         Long JavaDoc id_2 = company.getId();
336
337         broker.clearCache();
338         Criteria crit = new Criteria();
339         crit.addEqualTo("id", id_2);
340         Query query = QueryFactory.newQuery(Company.class, crit);
341         Collection JavaDoc result = broker.getCollectionByQuery(query);
342         assertEquals(1, result.size());
343         Company newCompany = (Company) result.iterator().next();
344         List JavaDoc newEmployees = newCompany.getEmployees();
345         assertNotNull(newEmployees);
346         assertEquals(company.getEmployees().size(), newEmployees.size());
347         
348         List JavaDoc newExecutives = newCompany.getExecutives();
349         assertNotNull(newExecutives);
350         assertEquals(company.getExecutives().size(), newExecutives.size());
351
352         int countEmployee = 0;
353         int countExecutive = 0;
354         int countManager = 0;
355         for(int i = 0; i < newEmployees.size(); i++)
356         {
357             Object JavaDoc o = newEmployees.get(i);
358             if(o instanceof Employee)
359             {
360                 ++countEmployee;
361             }
362             if(o instanceof Executive)
363             {
364                 ++countExecutive;
365             }
366             if(o instanceof Manager)
367             {
368                 ++countManager;
369             }
370         }
371         assertEquals(6, countEmployee);
372         assertEquals(5, countExecutive);
373         assertEquals(3, countManager);
374     }
375
376     public void testInheritedReferences() throws Exception JavaDoc
377     {
378         // TODO: fix this bug
379
if(ojbSkipKnownIssueProblem("[OJB-84] Will be fixed in next version")) return;
380
381         long timestamp = System.currentTimeMillis();
382         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
383         String JavaDoc name = "testInheritedReferences_" + timestamp;
384         Shareholder s1 = new Shareholder(id_2, name + "_shareholder");
385         s1.setShare(23);
386         Shareholder s2 = new Shareholder(id_2, name + "_shareholder");
387         s2.setShare(24);
388         List JavaDoc sh = new ArrayList JavaDoc();
389         sh.add(s1);
390         sh.add(s2);
391         Consortium consortium = new Consortium();
392         consortium.setName(name);
393         consortium.setShareholders(sh);
394
395         Identity oidCon;
396         Identity oidSH;
397         broker.beginTransaction();
398         broker.store(consortium);
399         broker.commitTransaction();
400         oidCon = broker.serviceIdentity().buildIdentity(consortium);
401         oidSH = broker.serviceIdentity().buildIdentity(s1);
402         broker.clearCache();
403         Consortium con = (Consortium) broker.getObjectByIdentity(oidCon);
404         assertNotNull(con);
405         assertNotNull(con.getShareholders());
406         assertEquals(2, con.getShareholders().size());
407
408         broker.clearCache();
409         Shareholder s1_new = (Shareholder) broker.getObjectByIdentity(oidSH);
410         assertNotNull(s1_new.getConsortiumKey());
411
412         broker.clearCache();
413         Criteria crit = new Criteria();
414         crit.addEqualTo("name", consortium.getName());
415         crit.addEqualTo("shareholders.share", new Integer JavaDoc(24));
416         crit.addEqualTo("shareholders.name", name + "_shareholder");
417         Query q = QueryFactory.newQuery(Consortium.class, crit);
418         Collection JavaDoc result = broker.getCollectionByQuery(q);
419         assertEquals(1, result.size());
420         assertEquals(consortium, result.iterator().next());
421     }
422
423     public void testQuery()
424     {
425         long timestamp = System.currentTimeMillis();
426         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
427         String JavaDoc name = "testQuery_" + timestamp;
428         String JavaDoc s_name = name + "_Shareholder_3";
429
430         Shareholder shareholder = new Shareholder(id_2, s_name);
431         shareholder.setName(name);
432         shareholder.setShare(77);
433         shareholder.setDepartment("none");
434         AddressIF ad = new Address(name);
435         shareholder.setAddress(ad);
436
437         broker.beginTransaction();
438         broker.store(shareholder);
439         broker.commitTransaction();
440
441         Identity oid_shareholder = broker.serviceIdentity().buildIdentity(shareholder);
442         broker.clearCache();
443
444         Shareholder new_shareholder = (Shareholder) broker.getObjectByIdentity(oid_shareholder);
445         assertNotNull(new_shareholder);
446         assertEquals(shareholder, new_shareholder);
447
448         Criteria c = new Criteria();
449         c.addEqualTo("name", shareholder.getName());
450         c.addEqualTo("share", new Integer JavaDoc(shareholder.getShare()));
451         c.addEqualTo("department", shareholder.getDepartment());
452         c.addEqualTo("address.street", shareholder.getAddress().getStreet());
453         Query q = QueryFactory.newQuery(Shareholder.class, c);
454         Collection JavaDoc result = broker.getCollectionByQuery(q);
455         assertEquals(1, result.size());
456         assertEquals(shareholder, result.iterator().next());
457     }
458
459     public void testStoreDelete_2()
460     {
461         long timestamp = System.currentTimeMillis();
462         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
463         String JavaDoc name = "testStoreDelete_" + timestamp;
464         String JavaDoc s_name = name + "_Shareholder_3";
465
466         Shareholder shareholder = new Shareholder(id_2, s_name);
467         shareholder.setShare(77);
468         shareholder.setDepartment("none");
469         AddressIF ad = new Address(name);
470         shareholder.setAddress(ad);
471
472         broker.beginTransaction();
473         broker.store(shareholder);
474         broker.commitTransaction();
475
476         Identity oid_shareholder = broker.serviceIdentity().buildIdentity(shareholder);
477         broker.clearCache();
478
479         Shareholder new_shareholder = (Shareholder) broker.getObjectByIdentity(oid_shareholder);
480         assertNotNull(new_shareholder);
481
482         assertEquals(s_name, new_shareholder.getName());
483         assertNotNull(new_shareholder.getAddress());
484         assertEquals(name, new_shareholder.getAddress().getStreet());
485         assertEquals(77, new_shareholder.getShare());
486
487         shareholder.getAddress().setStreet(name + "_updated");
488         shareholder.setShare(1313);
489         shareholder.setName(name + "_updated");
490
491         // use serialized version of object
492
shareholder = (Shareholder) SerializationUtils.clone(shareholder);
493         broker.beginTransaction();
494         broker.store(shareholder);
495         broker.commitTransaction();
496
497         oid_shareholder = broker.serviceIdentity().buildIdentity(shareholder);
498         broker.clearCache();
499
500         new_shareholder = (Shareholder) broker.getObjectByIdentity(oid_shareholder);
501         assertNotNull(new_shareholder);
502
503         assertEquals(1313, new_shareholder.getShare());
504         assertEquals(name + "_updated", new_shareholder.getName());
505         assertNotNull(new_shareholder.getAddress());
506         assertEquals(name + "_updated", new_shareholder.getAddress().getStreet());
507
508         broker.beginTransaction();
509         broker.delete(shareholder);
510         broker.commitTransaction();
511
512         new_shareholder = (Shareholder) broker.getObjectByIdentity(oid_shareholder);
513         assertNull(new_shareholder);
514     }
515
516     public void testStoreDelete()
517     {
518
519         long timestamp = System.currentTimeMillis();
520         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
521         String JavaDoc name = "testInheritancedObjectsInCollectionReferences_" + timestamp;
522         String JavaDoc m_name = name + "_manager_3";
523
524         Manager m = new Manager(id_2, m_name);
525         m.setDepartment("none");
526         AddressIF ad = new Address(name);
527         m.setAddress(ad);
528
529         String JavaDoc ex_name = name + "_executive";
530         Executive ex = new Executive(id_2, ex_name, "department_1", null);
531
532         String JavaDoc em_name = name + "_employee";
533         Employee em = new Employee(id_2, em_name);
534
535         broker.beginTransaction();
536         broker.store(em);
537         broker.store(ex);
538         broker.store(m);
539         broker.commitTransaction();
540
541         Identity oid_em = broker.serviceIdentity().buildIdentity(em);
542         Identity oid_ex = broker.serviceIdentity().buildIdentity(ex);
543         Identity oid_m = broker.serviceIdentity().buildIdentity(m);
544         broker.clearCache();
545
546         Employee new_em = (Employee) broker.getObjectByIdentity(oid_em);
547         Executive new_ex = (Executive) broker.getObjectByIdentity(oid_ex);
548         Manager new_m = (Manager) broker.getObjectByIdentity(oid_m);
549
550         assertNotNull(new_em);
551         assertNotNull(new_ex);
552         assertNotNull(new_m);
553
554         assertEquals(em_name, new_em.getName());
555         assertEquals(ex_name, new_ex.getName());
556         assertEquals(m_name, new_m.getName());
557         assertNotNull(new_m.getAddress());
558         assertEquals(name, new_m.getAddress().getStreet());
559
560         broker.beginTransaction();
561         broker.delete(m);
562         broker.delete(ex);
563         broker.delete(em);
564         broker.commitTransaction();
565
566         new_em = (Employee) broker.getObjectByIdentity(oid_em);
567         new_ex = (Executive) broker.getObjectByIdentity(oid_ex);
568         new_m = (Manager) broker.getObjectByIdentity(oid_m);
569
570         assertNull(new_em);
571         assertNull(new_ex);
572         assertNull(new_m);
573     }
574
575     public void testStoreUpdateQuerySerialized_2()
576     {
577         long timestamp = System.currentTimeMillis();
578         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
579         String JavaDoc name = "testStoreUpdateQuerySerialized_" + timestamp;
580         Manager m_1 = new Manager(id_2, name + "_manager_1");
581         Manager m_2 = new Manager(id_2, name + "_manager_2");
582         Manager m_3 = new Manager(id_2, name + "_manager_3");
583         m_3.setDepartment("none");
584
585         Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null);
586         Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null);
587
588         Employee em = new Employee(id_2, name + "_employee");
589
590         broker.beginTransaction();
591         broker.store(em);
592         broker.store(m_1);
593         broker.store(m_3);
594         broker.store(ex_1);
595         broker.store(m_2);
596         broker.store(ex_2);
597         broker.commitTransaction();
598
599         broker.clearCache();
600         Criteria crit = new Criteria();
601         crit.addLike("name", name + "%");
602         crit.addLike("department", "none");
603         Query query = QueryFactory.newQuery(Manager.class, crit);
604         Collection JavaDoc result = broker.getCollectionByQuery(query);
605         assertEquals(1, result.size());
606
607         crit = new Criteria();
608         crit.addLike("name", name + "%");
609         query = QueryFactory.newQuery(Employee.class, crit);
610         result = broker.getCollectionByQuery(query);
611         assertEquals(6, result.size());
612
613         crit = new Criteria();
614         crit.addLike("name", name + "%");
615         query = QueryFactory.newQuery(Executive.class, crit);
616         result = broker.getCollectionByQuery(query);
617         assertEquals(5, result.size());
618
619         crit = new Criteria();
620         crit.addLike("name", name + "%");
621         query = QueryFactory.newQuery(Manager.class, crit);
622         result = broker.getCollectionByQuery(query);
623         assertEquals(3, result.size());
624
625         em = (Employee) SerializationUtils.clone(em);
626         m_1 = (Manager) SerializationUtils.clone(m_1);
627         m_2 = (Manager) SerializationUtils.clone(m_2);
628         m_3 = (Manager) SerializationUtils.clone(m_3);
629         ex_1 = (Executive) SerializationUtils.clone(ex_1);
630         ex_2 = (Executive) SerializationUtils.clone(ex_2);
631
632         em.setName(em.getName() + "_updated");
633         m_1.setName(m_1.getName() + "_updated");
634         m_1.setDepartment("_updated_Dep");
635         m_2.setName(m_2.getName() + "_updated");
636         m_3.setName(m_3.getName() + "_updated");
637         ex_1.setName(ex_1.getName() + "_updated");
638         ex_2.setName(ex_2.getName() + "_updated");
639
640         broker.clearCache();
641         broker.beginTransaction();
642         //========================================
643
// update fields
644
broker.store(em, ObjectModification.UPDATE);
645         broker.store(m_1, ObjectModification.UPDATE);
646         broker.store(m_3, ObjectModification.UPDATE);
647         broker.store(ex_1, ObjectModification.UPDATE);
648         broker.store(m_2, ObjectModification.UPDATE);
649         broker.store(ex_2, ObjectModification.UPDATE);
650         //========================================
651
broker.commitTransaction();
652
653         /*
654         after de/serialization and update we expect the same row count in
655         each table
656         */

657         broker.clearCache();
658
659         crit = new Criteria();
660         crit.addLike("name", name + "%");
661         crit.addLike("department", "_updated_Dep");
662         query = QueryFactory.newQuery(Manager.class, crit);
663         result = broker.getCollectionByQuery(query);
664         assertEquals("Expect the same number of objects as before update", 1, result.size());
665         Manager newMan = (Manager) result.iterator().next();
666         assertEquals(m_1.getName(), newMan.getName());
667         assertEquals(m_1.getDepartment(), newMan.getDepartment());
668
669         crit = new Criteria();
670         crit.addLike("name", name + "%");
671         crit.addLike("department", "none");
672         query = QueryFactory.newQuery(Manager.class, crit);
673         result = broker.getCollectionByQuery(query);
674         assertEquals("Expect the same number of objects as before update", 1, result.size());
675
676         crit = new Criteria();
677         crit.addLike("name", name + "%");
678         query = QueryFactory.newQuery(Employee.class, crit);
679         result = broker.getCollectionByQuery(query);
680         assertEquals("Expect the same number of objects as before update", 6, result.size());
681
682         crit = new Criteria();
683         crit.addLike("name", name + "%");
684         query = QueryFactory.newQuery(Executive.class, crit);
685         result = broker.getCollectionByQuery(query);
686         assertEquals("Expect the same number of objects as before update", 5, result.size());
687
688         crit = new Criteria();
689         crit.addLike("name", name + "%");
690         query = QueryFactory.newQuery(Manager.class, crit);
691         result = broker.getCollectionByQuery(query);
692         assertEquals("Expect the same number of objects as before update", 3, result.size());
693     }
694
695     public void testObjectExistence()
696     {
697         Manager target_1 = new Manager(new Long JavaDoc(1), "testObjectExistence");
698         Manager target_2 = new Manager(new Long JavaDoc(System.currentTimeMillis()), "testObjectExistence");
699
700         Identity oid_1 = broker.serviceIdentity().buildIdentity(target_1);
701         Identity oid_2 = broker.serviceIdentity().buildIdentity(target_2);
702
703         ClassDescriptor cld = broker.getClassDescriptor(Manager.class);
704
705         boolean b_1 = broker.serviceBrokerHelper().doesExist(cld, oid_1, target_1);
706         boolean b_2 = broker.serviceBrokerHelper().doesExist(cld, oid_2, target_2);
707         assertFalse(b_1);
708         assertFalse(b_2);
709     }
710
711     public void testStoreUpdateQuerySerialized()
712     {
713         long timestamp = System.currentTimeMillis();
714         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
715         String JavaDoc name = "testStoreUpdateQuerySerialized_" + timestamp;
716         Manager m_1 = new Manager(id_2, name + "_manager_1");
717         Manager m_2 = new Manager(id_2, name + "_manager_2");
718         Manager m_3 = new Manager(id_2, name + "_manager_3");
719         m_3.setDepartment("none");
720
721         Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null);
722         Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null);
723
724         Employee em = new Employee(id_2, name + "_employee");
725
726         broker.beginTransaction();
727         broker.store(em);
728         broker.store(m_1);
729         broker.store(m_3);
730         broker.store(ex_1);
731         broker.store(m_2);
732         broker.store(ex_2);
733         broker.commitTransaction();
734
735         broker.clearCache();
736         Criteria crit = new Criteria();
737         crit.addLike("name", name + "%");
738         crit.addLike("department", "none");
739         Query query = QueryFactory.newQuery(Manager.class, crit);
740         Collection JavaDoc result = broker.getCollectionByQuery(query);
741         assertEquals(1, result.size());
742
743         crit = new Criteria();
744         crit.addLike("name", name + "%");
745         query = QueryFactory.newQuery(Employee.class, crit);
746         result = broker.getCollectionByQuery(query);
747         assertEquals(6, result.size());
748
749         crit = new Criteria();
750         crit.addLike("name", name + "%");
751         query = QueryFactory.newQuery(Executive.class, crit);
752         result = broker.getCollectionByQuery(query);
753         assertEquals(5, result.size());
754
755         crit = new Criteria();
756         crit.addLike("name", name + "%");
757         query = QueryFactory.newQuery(Manager.class, crit);
758         result = broker.getCollectionByQuery(query);
759         assertEquals(3, result.size());
760
761         em = (Employee) SerializationUtils.clone(em);
762         m_1 = (Manager) SerializationUtils.clone(m_1);
763         m_2 = (Manager) SerializationUtils.clone(m_2);
764         m_3 = (Manager) SerializationUtils.clone(m_3);
765         ex_1 = (Executive) SerializationUtils.clone(ex_1);
766         ex_2 = (Executive) SerializationUtils.clone(ex_2);
767
768         em.setName(em.getName() + "_updated");
769         m_1.setName(m_1.getName() + "_updated");
770         m_2.setName(m_2.getName() + "_updated");
771         m_3.setName(m_3.getName() + "_updated");
772         ex_1.setName(ex_1.getName() + "_updated");
773         ex_2.setName(ex_2.getName() + "_updated");
774
775         broker.clearCache();
776         broker.beginTransaction();
777         broker.store(em);
778         broker.store(m_1);
779         broker.store(m_3);
780         broker.store(ex_1);
781         broker.store(m_2);
782         broker.store(ex_2);
783         broker.commitTransaction();
784
785         /*
786         after de/serialization and update we expect the same row count in
787         each table
788         */

789         broker.clearCache();
790         crit = new Criteria();
791         crit.addLike("name", name + "%");
792         crit.addLike("department", "none");
793         query = QueryFactory.newQuery(Manager.class, crit);
794         result = broker.getCollectionByQuery(query);
795         assertEquals("Expect the same number of objects as before update", 1, result.size());
796
797         crit = new Criteria();
798         crit.addLike("name", name + "%");
799         query = QueryFactory.newQuery(Employee.class, crit);
800         result = broker.getCollectionByQuery(query);
801         assertEquals("Expect the same number of objects as before update", 6, result.size());
802
803         crit = new Criteria();
804         crit.addLike("name", name + "%");
805         query = QueryFactory.newQuery(Executive.class, crit);
806         result = broker.getCollectionByQuery(query);
807         assertEquals("Expect the same number of objects as before update", 5, result.size());
808
809         crit = new Criteria();
810         crit.addLike("name", name + "%");
811         query = QueryFactory.newQuery(Manager.class, crit);
812         result = broker.getCollectionByQuery(query);
813         assertEquals("Expect the same number of objects as before update", 3, result.size());
814     }
815
816     private List JavaDoc prepareForQueryTests(Long JavaDoc id_2, String JavaDoc name)
817     {
818         List JavaDoc result = new ArrayList JavaDoc();
819         
820         Manager m_1 = new Manager(id_2, name + "_manager_1");
821         Manager m_2 = new Manager(id_2, name + "_manager_2");
822         Manager m_3 = new Manager(id_2, name + "_manager_3");
823         m_3.setDepartment("none");
824         Address a_1 = new Address("snob allee");
825         m_1.setAddress(a_1);
826
827         Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null);
828         Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null);
829
830         Employee em = new Employee(id_2, name + "_employee");
831         Address a_2 = new Address("cockroaches valley");
832         em.setAddress(a_2);
833
834         result.add(m_1);
835         result.add(m_2);
836         result.add(m_3);
837         result.add(ex_1);
838         result.add(ex_2);
839         result.add(em);
840
841         broker.beginTransaction();
842         broker.store(m_1);
843         broker.store(m_2);
844         broker.store(m_3);
845         broker.store(ex_1);
846         broker.store(ex_2);
847         broker.store(em);
848         broker.commitTransaction();
849         
850         return result;
851     }
852
853     private Company prepareTestDataWithCompany(String JavaDoc name)
854     {
855         Long JavaDoc id_2 = null;
856         Manager m_1 = new Manager(id_2, name + "_manager_1");
857         Manager m_2 = new Manager(id_2, name + "_manager_2");
858         Manager m_3 = new Manager(id_2, name + "_manager_3");
859         m_3.setDepartment("none");
860         Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null);
861         Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null);
862         Employee em = new Employee(id_2, name + "_employee");
863
864         ArrayList JavaDoc employees = new ArrayList JavaDoc();
865         employees.add(m_1);
866         employees.add(m_2);
867         employees.add(m_3);
868         employees.add(ex_1);
869         employees.add(ex_2);
870         employees.add(em);
871
872         ArrayList JavaDoc executives = new ArrayList JavaDoc();
873         executives.add(m_1);
874         executives.add(m_2);
875         executives.add(m_3);
876         executives.add(ex_1);
877         executives.add(ex_2);
878         
879         Company company = new Company(null, name, employees, executives);
880         broker.beginTransaction();
881         broker.store(company);
882         broker.commitTransaction();
883         return company;
884     }
885     
886     public void testQuery_InheritedObjects()
887     {
888         long timestamp = System.currentTimeMillis();
889         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
890         String JavaDoc name = "testQuery_InheritedObjects" + timestamp;
891         prepareForQueryTests(id_2, name);
892         broker.clearCache();
893
894         Criteria crit = new Criteria();
895         crit.addLike("name", name + "%");
896         Query query = QueryFactory.newQuery(Employee.class, crit);
897         Collection JavaDoc result = broker.getCollectionByQuery(query);
898         assertEquals(6, result.size());
899         for (Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
900         {
901             Employee obj = (Employee) iterator.next();
902             assertNotNull(obj.getName());
903         }
904
905         broker.clearCache();
906         crit = new Criteria();
907         crit.addLike("name", name + "%");
908         query = QueryFactory.newQuery(Executive.class, crit);
909         result = broker.getCollectionByQuery(query);
910         assertEquals(5, result.size());
911         for (Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
912         {
913             Executive obj = (Executive) iterator.next();
914             assertNotNull(obj.getName());
915         }
916
917         broker.clearCache();
918         crit = new Criteria();
919         crit.addLike("name", name + "%");
920         query = QueryFactory.newQuery(Manager.class, crit);
921         result = broker.getCollectionByQuery(query);
922         assertEquals(3, result.size());
923         for (Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
924         {
925             Manager obj = (Manager) iterator.next();
926             assertNotNull(obj.getName());
927         }
928     }
929
930     public void testQuery_InheritedField()
931     {
932         long timestamp = System.currentTimeMillis();
933         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
934         String JavaDoc name = "testQuery_InheritedField" + timestamp;
935         prepareForQueryTests(id_2, name);
936         broker.clearCache();
937
938         broker.clearCache();
939         Criteria crit = new Criteria();
940         crit.addLike("name", name + "%");
941         crit.addLike("department", "none");
942         Query query = QueryFactory.newQuery(Manager.class, crit);
943         Collection JavaDoc result = broker.getCollectionByQuery(query);
944         assertEquals(1, result.size());
945     }
946
947     public void testQuery_Reference()
948     {
949         long timestamp = System.currentTimeMillis();
950         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
951         String JavaDoc name = "testQuery_Reference" + timestamp;
952         prepareForQueryTests(id_2, name);
953         broker.clearCache();
954
955         Criteria crit = new Criteria();
956         crit.addLike("name", name + "%");
957         crit.addLike("address.street", "%valley");
958         Query query = QueryFactory.newQuery(Employee.class, crit);
959         Collection JavaDoc result = broker.getCollectionByQuery(query);
960         assertEquals(1, result.size());
961         Employee emp = (Employee) result.iterator().next();
962         assertNotNull(emp.getAddress());
963         assertEquals("cockroaches valley", emp.getAddress().getStreet());
964     }
965
966     public void testQuery_InheritedReference_1()
967     {
968         long timestamp = System.currentTimeMillis();
969         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
970         String JavaDoc name = "testQuery_InheritedReference_1" + timestamp;
971         prepareForQueryTests(id_2, name);
972         broker.clearCache();
973
974         Criteria crit = new Criteria();
975         crit.addLike("name", name + "%");
976         crit.addEqualTo("address.street", "snob allee");
977         Query query = QueryFactory.newQuery(Manager.class, crit);
978         Collection JavaDoc result = broker.getCollectionByQuery(query);
979         assertEquals(1, result.size());
980         Manager retManager = (Manager) result.iterator().next();
981         assertNotNull(retManager);
982         assertEquals(name + "_manager_1", retManager.getName());
983         assertNotNull(retManager.getAddress());
984         assertEquals("snob allee", retManager.getAddress().getStreet());
985     }
986
987     public void testQuery_InheritedReference_2()
988     {
989         long timestamp = System.currentTimeMillis();
990         Long JavaDoc id_2 = new Long JavaDoc(timestamp);
991         String JavaDoc name = "testQuery_InheritedReference_2" + timestamp;
992         prepareForQueryTests(id_2, name);
993         broker.clearCache();
994
995         Criteria crit = new Criteria();
996         crit.addLike("name", name + "%");
997         crit.addEqualTo("address.street", "snob allee");
998         Query query = QueryFactory.newQuery(Executive.class, crit);
999         Collection JavaDoc result = broker.getCollectionByQuery(query);
1000        assertEquals(1, result.size());
1001
1002        Executive retManager = (Executive) result.iterator().next();
1003        assertNotNull(retManager);
1004        assertEquals(name + "_manager_1", retManager.getName());
1005    }
1006
1007    public void testQuery_InheritedReference_3()
1008    {
1009        long timestamp = System.currentTimeMillis();
1010        Long JavaDoc id_2 = new Long JavaDoc(timestamp);
1011        String JavaDoc name = "testQuery_InheritedReference_3" + timestamp;
1012        prepareForQueryTests(id_2, name);
1013        broker.clearCache();
1014
1015        Criteria crit = new Criteria();
1016        crit.addLike("name", name + "%");
1017        crit.addEqualTo("address.street", "snob allee");
1018        Query query = QueryFactory.newQuery(Employee.class, crit);
1019        Collection JavaDoc result = broker.getCollectionByQuery(query);
1020        assertEquals(1, result.size());
1021    }
1022
1023    public void testQuery_ReferenceOuterJoin()
1024    {
1025        long timestamp = System.currentTimeMillis();
1026        String JavaDoc name = "testQuery_ReferenceOuterJoin_" + timestamp;
1027        prepareTestDataWithCompany(name);
1028        //Long id_2 = company.getId();
1029

1030        // Store a dummy company
1031
Company dummyComp = new Company(null, name + "_dummy", Collections.EMPTY_LIST, Collections.EMPTY_LIST);
1032        broker.beginTransaction();
1033        broker.store(dummyComp);
1034        broker.commitTransaction();
1035        
1036        broker.clearCache();
1037
1038        Criteria crit = new Criteria();
1039        crit.addLike("name", name + "%");
1040        QueryByCriteria query = QueryFactory.newQuery(Company.class, crit, true);
1041        Collection JavaDoc result = broker.getCollectionByQuery(query);
1042        // retrieve both companies
1043
assertEquals(2, result.size());
1044
1045        crit = new Criteria();
1046        crit.addLike("name", name + "%");
1047
1048        Criteria nameCrit1 = new Criteria();
1049        nameCrit1.addLike("executives.name", name + "%");
1050        Criteria nameCrit2 = new Criteria();
1051        nameCrit2.addIsNull("executives.name");
1052        nameCrit1.addOrCriteria(nameCrit2);
1053        crit.addAndCriteria(nameCrit1);
1054
1055        query = QueryFactory.newQuery(Company.class, crit, true);
1056        query.addOrderByAscending("id");
1057        query.setPathOuterJoin("executives");
1058        result = broker.getCollectionByQuery(query);
1059        // should retrieve both companies
1060
assertEquals(2, result.size());
1061     }
1062    
1063    public void testInsertQuery()
1064    {
1065        long timestamp = System.currentTimeMillis();
1066        Long JavaDoc id_2 = new Long JavaDoc(timestamp);
1067        String JavaDoc name = "testInsert" + timestamp;
1068        Employee em1 = new Employee(id_2, name);
1069        Executive ex1 = new Executive(id_2, name, "department_1", null);
1070        Executive ex2 = new Executive(id_2, name, "department_2", null);
1071        ArrayList JavaDoc list = new ArrayList JavaDoc();
1072        list.add(ex1);
1073        list.add(ex2);
1074        Manager m1 = new Manager(id_2, name);
1075        m1.setExecutives(list);
1076
1077        broker.beginTransaction();
1078        broker.store(em1);
1079        broker.store(m1);
1080        broker.commitTransaction();
1081
1082        Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
1083        Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
1084        Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
1085
1086        broker.clearCache();
1087
1088        Employee newEm1 = (Employee) broker.getObjectByIdentity(em1_oid);
1089        Executive newEx1 = (Executive) broker.getObjectByIdentity(ex1_oid);
1090        Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1091
1092        assertEquals(em1, newEm1);
1093        assertEquals(ex1, newEx1);
1094        assertEquals(m1, newM1);
1095        assertEquals(name, newEx1.getName());
1096        assertEquals(name, newM1.getName());
1097
1098        assertEquals(2, newM1.getExecutives().size());
1099
1100        Criteria crit = new Criteria();
1101        crit.addEqualTo("name", name);
1102        Query queryEmployee = QueryFactory.newQuery(Employee.class, crit);
1103        Query queryExecutive = QueryFactory.newQuery(Executive.class, crit);
1104        Query queryManager = QueryFactory.newQuery(Manager.class, crit);
1105
1106        Collection JavaDoc result = broker.getCollectionByQuery(queryEmployee);
1107        assertEquals(4, result.size());
1108
1109        result = broker.getCollectionByQuery(queryExecutive);
1110        assertEquals(3, result.size());
1111
1112        result = broker.getCollectionByQuery(queryManager);
1113        assertEquals(1, result.size());
1114    }
1115
1116    public void testUpdate()
1117    {
1118        long timestamp = System.currentTimeMillis();
1119        Long JavaDoc id_2 = new Long JavaDoc(timestamp);
1120        String JavaDoc name = "testUpdate_" + timestamp;
1121        Employee em1 = new Employee(id_2, "employee_" + name);
1122        Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null);
1123        Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null);
1124        ArrayList JavaDoc list = new ArrayList JavaDoc();
1125        list.add(ex1);
1126        list.add(ex2);
1127        Manager m1 = new Manager(id_2, "manager_" + name);
1128        m1.setExecutives(list);
1129
1130        broker.beginTransaction();
1131        broker.store(em1);
1132        broker.store(m1);
1133        broker.commitTransaction();
1134
1135        Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
1136        Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
1137        Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
1138
1139        broker.clearCache();
1140
1141        Employee newEm1 = (Employee) broker.getObjectByIdentity(em1_oid);
1142        Executive newEx1 = (Executive) broker.getObjectByIdentity(ex1_oid);
1143        Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1144
1145        assertEquals(2, newM1.getExecutives().size());
1146
1147        newEm1.setName("**updated_" + name);
1148        newM1.setName("**updated_" + name);
1149        ((Executive) newM1.getExecutives().get(0)).setName("**updated_" + name);
1150
1151        broker.beginTransaction();
1152        broker.store(newEm1);
1153        broker.store(newM1);
1154        broker.store(newEx1);
1155        broker.commitTransaction();
1156
1157        broker.clearCache();
1158
1159        em1 = (Employee) broker.getObjectByIdentity(em1_oid);
1160        ex1 = (Executive) broker.getObjectByIdentity(ex1_oid);
1161        m1 = (Manager) broker.getObjectByIdentity(m1_oid);
1162
1163        assertEquals(newEm1, em1);
1164        assertEquals(newEx1, ex1);
1165        assertEquals(newM1, m1);
1166
1167        assertEquals(2, m1.getExecutives().size());
1168    }
1169
1170    public void testDelete()
1171    {
1172        long timestamp = System.currentTimeMillis();
1173        Long JavaDoc id_2 = new Long JavaDoc(timestamp);
1174        String JavaDoc name = "testDelete_" + timestamp;
1175        Employee em1 = new Employee(id_2, "employee_" + name);
1176        Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null);
1177        Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null);
1178        ArrayList JavaDoc list = new ArrayList JavaDoc();
1179        list.add(ex1);
1180        list.add(ex2);
1181        Manager m1 = new Manager(id_2, "manager_" + name);
1182        m1.setExecutives(list);
1183
1184        broker.beginTransaction();
1185        broker.store(em1);
1186        broker.store(m1);
1187        broker.commitTransaction();
1188
1189        Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
1190        Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
1191        Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
1192
1193        broker.clearCache();
1194
1195        Employee newEm1 = (Employee) broker.getObjectByIdentity(em1_oid);
1196        Executive newEx1 = (Executive) broker.getObjectByIdentity(ex1_oid);
1197        Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1198
1199        assertNotNull(newEm1);
1200        assertNotNull(newEx1);
1201        assertNotNull(newM1);
1202        assertEquals(2, newM1.getExecutives().size());
1203
1204        broker.beginTransaction();
1205        broker.delete(newEm1);
1206        broker.delete(newEx1);
1207        broker.delete(newM1);
1208        broker.commitTransaction();
1209
1210        newEm1 = (Employee) broker.getObjectByIdentity(em1_oid);
1211        newEx1 = (Executive) broker.getObjectByIdentity(ex1_oid);
1212        newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1213
1214        assertNull(newEm1);
1215        assertNull(newEx1);
1216        assertNull(newM1);
1217    }
1218
1219    /**
1220     * Check backward compatibility with 'old' super-reference handling using explicite
1221     * anonymous field for FK to super class/table.
1222     */

1223    public void testInheritanceViaAnonymousField()
1224    {
1225        ObjectRepository.G obj_1 = new ObjectRepository.G();
1226        obj_1.setSomeValue(1);
1227        obj_1.setSomeSuperValue(2);
1228        obj_1.setSomeSubValue(3);
1229
1230        broker.beginTransaction();
1231        broker.store(obj_1);
1232        broker.commitTransaction();
1233
1234        Identity oid = broker.serviceIdentity().buildIdentity(obj_1);
1235        broker.clearCache();
1236
1237        ObjectRepository.G obj_2 = (ObjectRepository.G) broker.getObjectByIdentity(oid);
1238        assertEquals(obj_1.getId(), obj_2.getId());
1239        assertEquals(obj_1.getSomeSubValue(), obj_2.getSomeSubValue());
1240        assertEquals(obj_1.getSomeSuperValue(), obj_2.getSomeSuperValue());
1241        assertEquals(obj_1.getSomeValue(), obj_2.getSomeValue());
1242
1243        broker.beginTransaction();
1244        obj_1.setSomeValue(11);
1245        obj_1.setSomeSuperValue(22);
1246        obj_1.setSomeSubValue(33);
1247        broker.store(obj_1);
1248        broker.commitTransaction();
1249
1250        broker.clearCache();
1251
1252        ObjectRepository.G obj_3 = (ObjectRepository.G) broker.getObjectByIdentity(oid);
1253        assertEquals(obj_1.getId(), obj_3.getId());
1254        assertEquals(obj_1.getSomeSubValue(), obj_3.getSomeSubValue());
1255        assertEquals(obj_1.getSomeSuperValue(), obj_3.getSomeSuperValue());
1256        assertEquals(obj_1.getSomeValue(), obj_3.getSomeValue());
1257
1258        assertEquals(obj_2.getId(), obj_3.getId());
1259        assertFalse(obj_2.getSomeSubValue() == obj_3.getSomeSubValue());
1260        assertFalse(obj_2.getSomeSuperValue() == obj_3.getSomeSuperValue());
1261        assertFalse(obj_2.getSomeValue() == obj_3.getSomeValue());
1262    }
1263
1264// /**
1265
// * TODO: Should we support some kind of "declarative inheritance"? This test
1266
// * try to use this kind of inheritance as class Dog expects some fields from a
1267
// * "declarated super class" (no java inheritance, only declared in metadata).
1268
// * In class {@link org.apache.ojb.broker.metadata.SuperReferenceDescriptor} the
1269
// * support is comment out (but only works for simple objects without references).
1270
// */
1271
// public void YYYtestWithoutJavaInheritance_1()
1272
// {
1273
// if (ojbSkipKnownIssueProblem("Declarative Inheritance not supported"))
1274
// {
1275
// return;
1276
// }
1277
//
1278
// String name = "testWithoutJavaInheritance_1" + System.currentTimeMillis();
1279
// Dog dog = new Dog(name, 35, 4);
1280
// broker.beginTransaction();
1281
// broker.store(dog);
1282
// broker.commitTransaction();
1283
//
1284
// broker.clearCache();
1285
// Criteria crit = new Criteria();
1286
// crit.addEqualTo("name", name);
1287
// Query q = QueryFactory.newQuery(Dog.class, crit);
1288
// Collection result = broker.getCollectionByQuery(q);
1289
// assertNotNull(result);
1290
// assertEquals(1, result.size());
1291
// Dog newDog = (Dog) result.iterator().next();
1292
// assertTrue(dog.equals(newDog));
1293
//
1294
// broker.beginTransaction();
1295
// newDog.setWeight(1000);
1296
// newDog.setLegs(10);
1297
// broker.store(newDog);
1298
// broker.commitTransaction();
1299
//
1300
// broker.clearCache();
1301
// result = broker.getCollectionByQuery(q);
1302
// assertNotNull(result);
1303
// assertEquals(1, result.size());
1304
// Dog newDog2 = (Dog) result.iterator().next();
1305
// assertTrue(newDog.equals(newDog2));
1306
//
1307
// broker.beginTransaction();
1308
// broker.delete(dog);
1309
// broker.commitTransaction();
1310
// }
1311
//
1312
// /**
1313
// * TODO: Should we support some kind of "declarative inheritance"? This test
1314
// * try to use this kind of inheritance as class Dog expects some fields from a
1315
// * "declarated super class" (no java inheritance, only declared in metadata).
1316
// * In class {@link org.apache.ojb.broker.metadata.SuperReferenceDescriptor} the
1317
// * support is comment out (but only works for simple objects without references).
1318
// */
1319
// public void YYYtestWithoutJavaInheritance_2()
1320
// {
1321
// if (ojbSkipKnownIssueProblem("Declarative Inheritance not supported"))
1322
// {
1323
// return;
1324
// }
1325
//
1326
// String name = "testWithoutJavaInheritance_2" + System.currentTimeMillis();
1327
// Dog dog = new Dog(name, 35, 4);
1328
// Animal parent = new Animal(name + "_parent", 55);
1329
// Food f1 = new Food(name + "fruit1");
1330
// Food f2 = new Food(name + "fruit2");
1331
// dog.addFood(f1);
1332
// dog.addFood(f2);
1333
// dog.setParent(parent);
1334
//
1335
// broker.beginTransaction();
1336
// broker.store(dog);
1337
// broker.commitTransaction();
1338
//
1339
// broker.clearCache();
1340
// Criteria crit = new Criteria();
1341
// crit.addEqualTo("name", name);
1342
// Query q = QueryFactory.newQuery(Dog.class, crit);
1343
// Collection result = broker.getCollectionByQuery(q);
1344
// assertNotNull(result);
1345
// assertEquals(1, result.size());
1346
// Dog newDog = (Dog) result.iterator().next();
1347
// assertEquals(dog, newDog);
1348
//
1349
// broker.beginTransaction();
1350
// newDog.setWeight(1000);
1351
// newDog.setLegs(10);
1352
// newDog.addFood(new Food(name + "_new"));
1353
// broker.store(newDog);
1354
// broker.commitTransaction();
1355
//
1356
// broker.clearCache();
1357
// result = broker.getCollectionByQuery(q);
1358
// assertNotNull(result);
1359
// assertEquals(1, result.size());
1360
// Dog newDog2 = (Dog) result.iterator().next();
1361
// assertTrue(newDog.equals(newDog2));
1362
//
1363
// broker.beginTransaction();
1364
// broker.delete(dog);
1365
// broker.commitTransaction();
1366
// }
1367

1368    //************************************************************
1369
// inner classes used for test
1370
//************************************************************
1371
public static class Shareholder extends Manager
1372    {
1373        private int share;
1374
1375        public Shareholder()
1376        {
1377        }
1378
1379        public Shareholder(Long JavaDoc id_2, String JavaDoc name)
1380        {
1381            super(id_2, name);
1382        }
1383
1384        public int getShare()
1385        {
1386            return share;
1387        }
1388
1389        public void setShare(int share)
1390        {
1391            this.share = share;
1392        }
1393
1394        public boolean equals(Object JavaDoc obj)
1395        {
1396            if (!(obj instanceof Shareholder))
1397            {
1398                return false;
1399            }
1400            Shareholder s = (Shareholder) obj;
1401            return new EqualsBuilder().append(getShare(), s.getShare()).isEquals() && super.equals(obj);
1402        }
1403    }
1404
1405
1406    public static class Manager extends Executive
1407    {
1408        private List JavaDoc executives;
1409        private Integer JavaDoc consortiumKey;
1410
1411        public Manager()
1412        {
1413        }
1414
1415        public Manager(Long JavaDoc id_2, String JavaDoc name)
1416        {
1417            super(id_2, name, null, null);
1418        }
1419
1420        public List JavaDoc getExecutives()
1421        {
1422            return executives;
1423        }
1424
1425        public void setExecutives(List JavaDoc executives)
1426        {
1427            this.executives = executives;
1428        }
1429
1430        public Integer JavaDoc getConsortiumKey()
1431        {
1432            return consortiumKey;
1433        }
1434
1435        public void setConsortiumKey(Integer JavaDoc consortiumKey)
1436        {
1437            this.consortiumKey = consortiumKey;
1438        }
1439
1440        public boolean equals(Object JavaDoc obj)
1441        {
1442            if (!(obj instanceof Manager))
1443            {
1444                return false;
1445            }
1446            Manager m = (Manager) obj;
1447            return new EqualsBuilder().append(getConsortiumKey(), m.getConsortiumKey()).isEquals() && super.equals(obj);
1448        }
1449    }
1450
1451    public static class Executive extends Employee
1452    {
1453        private String JavaDoc department;
1454        private Manager manager;
1455
1456        public Executive()
1457        {
1458        }
1459
1460        public Executive(Long JavaDoc id_2, String JavaDoc name, String JavaDoc department, Manager manager)
1461        {
1462            super(id_2, name);
1463            this.department = department;
1464            this.manager = manager;
1465        }
1466
1467        public String JavaDoc getDepartment()
1468        {
1469            return department;
1470        }
1471
1472        public void setDepartment(String JavaDoc department)
1473        {
1474            this.department = department;
1475        }
1476
1477        public Manager getManager()
1478        {
1479            return manager;
1480        }
1481
1482        public void setManager(Manager manager)
1483        {
1484            this.manager = manager;
1485        }
1486
1487        public boolean equals(Object JavaDoc obj)
1488        {
1489            if (!(obj instanceof Executive))
1490            {
1491                return false;
1492            }
1493            Executive ex = (Executive) obj;
1494            return new EqualsBuilder().append(getDepartment(), ex.getDepartment()).isEquals() && super.equals(obj);
1495        }
1496    }
1497
1498    public static class Employee implements Serializable JavaDoc
1499    {
1500        private Integer JavaDoc id;
1501        private Long JavaDoc id_2;
1502        private String JavaDoc name;
1503        private AddressIF address;
1504
1505        public Employee()
1506        {
1507        }
1508
1509        public Employee(Long JavaDoc id_2, String JavaDoc name)
1510        {
1511            this.id_2 = id_2;
1512            this.name = name;
1513        }
1514
1515        public Integer JavaDoc getId()
1516        {
1517            return id;
1518        }
1519
1520        public Long JavaDoc getId_2()
1521        {
1522            return id_2;
1523        }
1524
1525        public void setId_2(Long JavaDoc id_2)
1526        {
1527            this.id_2 = id_2;
1528        }
1529
1530        public void setId(Integer JavaDoc id)
1531        {
1532            this.id = id;
1533        }
1534
1535        public AddressIF getAddress()
1536        {
1537            return address;
1538        }
1539
1540        public void setAddress(AddressIF address)
1541        {
1542            this.address = address;
1543        }
1544
1545        public String JavaDoc getName()
1546        {
1547            return name;
1548        }
1549
1550        public void setName(String JavaDoc name)
1551        {
1552            this.name = name;
1553        }
1554
1555        public boolean equals(Object JavaDoc obj)
1556        {
1557            if (!(obj instanceof Employee))
1558            {
1559                return false;
1560            }
1561            Employee em = (Employee) obj;
1562            return new EqualsBuilder()
1563                    .append(getId(), em.getId())
1564                    .append(getId_2(), em.getId_2())
1565                    .append(getName(), em.getName())
1566                    .append(getAddress(), em.getAddress())
1567                    .isEquals();
1568        }
1569
1570        public String JavaDoc toString()
1571        {
1572            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE, false, Employee.class);
1573        }
1574    }
1575
1576    public static class Address implements AddressIF
1577    {
1578        private Integer JavaDoc id;
1579        private String JavaDoc street;
1580
1581        public Address()
1582        {
1583        }
1584
1585        public Address(String JavaDoc street)
1586        {
1587            this.street = street;
1588        }
1589
1590        public Integer JavaDoc getId()
1591        {
1592            return id;
1593        }
1594
1595        public void setId(Integer JavaDoc id)
1596        {
1597            this.id = id;
1598        }
1599
1600        public String JavaDoc getStreet()
1601        {
1602            return street;
1603        }
1604
1605        public void setStreet(String JavaDoc street)
1606        {
1607            this.street = street;
1608        }
1609
1610        public boolean equals(Object JavaDoc obj)
1611        {
1612            if (!(obj instanceof Address))
1613            {
1614                return false;
1615            }
1616            Address adr = (Address) obj;
1617            return new EqualsBuilder()
1618                    .append(getId(), adr.getId())
1619                    .append(getStreet(), adr.getStreet())
1620                    .isEquals();
1621        }
1622
1623        public String JavaDoc toString()
1624        {
1625            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE, false, Address.class);
1626        }
1627    }
1628
1629    public static interface AddressIF extends Serializable JavaDoc
1630    {
1631        public Integer JavaDoc getId();
1632
1633        public void setId(Integer JavaDoc id);
1634
1635        public String JavaDoc getStreet();
1636
1637        public void setStreet(String JavaDoc street);
1638    }
1639
1640    public static class Company
1641    {
1642        private Long JavaDoc id;
1643        private String JavaDoc name;
1644        private List JavaDoc employees;
1645        private List JavaDoc executives;
1646
1647        public Company()
1648        {
1649        }
1650
1651        public Company(Long JavaDoc id, String JavaDoc name, List JavaDoc employees,List JavaDoc executives)
1652        {
1653            this.id = id;
1654            this.name = name;
1655            this.employees = employees;
1656            this.executives = executives;
1657        }
1658
1659        public Long JavaDoc getId()
1660        {
1661            return id;
1662        }
1663
1664        public void setId(Long JavaDoc id)
1665        {
1666            this.id = id;
1667        }
1668
1669        public String JavaDoc getName()
1670        {
1671            return name;
1672        }
1673
1674        public void setName(String JavaDoc name)
1675        {
1676            this.name = name;
1677        }
1678
1679        public List JavaDoc getEmployees()
1680        {
1681            return employees;
1682        }
1683
1684        public void setEmployees(List JavaDoc employees)
1685        {
1686            this.employees = employees;
1687        }
1688
1689        public List JavaDoc getExecutives()
1690        {
1691            return executives;
1692        }
1693
1694        public void setExecutives(List JavaDoc executives)
1695        {
1696            this.executives = executives;
1697        }
1698    }
1699
1700    public static class Consortium
1701    {
1702        private Integer JavaDoc id;
1703        private String JavaDoc name;
1704        private List JavaDoc shareholders;
1705
1706        public Consortium()
1707        {
1708        }
1709
1710        public Integer JavaDoc getId()
1711        {
1712            return id;
1713        }
1714
1715        public void setId(Integer JavaDoc id)
1716        {
1717            this.id = id;
1718        }
1719
1720        public String JavaDoc getName()
1721        {
1722            return name;
1723        }
1724
1725        public void setName(String JavaDoc name)
1726        {
1727            this.name = name;
1728        }
1729
1730        public List JavaDoc getShareholders()
1731        {
1732            return shareholders;
1733        }
1734
1735        public void setShareholders(List JavaDoc shareholders)
1736        {
1737            this.shareholders = shareholders;
1738        }
1739
1740        public boolean equals(Object JavaDoc obj)
1741        {
1742            if (!(obj instanceof Consortium))
1743            {
1744                return false;
1745            }
1746            Consortium c = (Consortium) obj;
1747            return new EqualsBuilder()
1748                    .append(getId(), c.getId())
1749                    // todo: this could be problematic, if so remove it
1750
.append(getShareholders(), c.getShareholders())
1751                    .append(getName(), c.getName()).isEquals();
1752        }
1753
1754        public String JavaDoc toString()
1755        {
1756            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE, false, Consortium.class);
1757        }
1758    }
1759
1760    public static class Entity
1761    {
1762        private Integer JavaDoc id;
1763        private String JavaDoc name;
1764
1765        public Entity()
1766        {
1767        }
1768
1769        public Entity(String JavaDoc name)
1770        {
1771            this.name = name;
1772        }
1773
1774        public boolean equals(Object JavaDoc obj)
1775        {
1776            if (!(obj instanceof Entity))
1777            {
1778                return false;
1779            }
1780            Entity other = (Entity) obj;
1781            return new EqualsBuilder()
1782                    .append(getId(), other.getId())
1783                    .append(getName(), other.getName())
1784                    .isEquals();
1785        }
1786
1787        public Integer JavaDoc getId()
1788        {
1789            return id;
1790        }
1791
1792        public void setId(Integer JavaDoc id)
1793        {
1794            this.id = id;
1795        }
1796
1797        public String JavaDoc getName()
1798        {
1799            return name;
1800        }
1801
1802        public void setName(String JavaDoc name)
1803        {
1804            this.name = name;
1805        }
1806    }
1807
1808    public static class Animal
1809    {
1810        private Integer JavaDoc id;
1811        private int weight;
1812        private String JavaDoc name;
1813        private Animal parent;
1814        private List JavaDoc foods = new ArrayList JavaDoc();
1815
1816        public Animal()
1817        {
1818        }
1819
1820        public Animal(String JavaDoc name, int weight)
1821        {
1822            this.name = name;
1823            this.weight = weight;
1824        }
1825
1826        public boolean equals(Object JavaDoc obj)
1827        {
1828            if (!(obj instanceof Animal))
1829            {
1830                return false;
1831            }
1832            Animal other = (Animal) obj;
1833            return new EqualsBuilder()
1834                    .append(getId(), other.getId())
1835                    .append(getName(), other.getName())
1836                    .append(getWeight(), other.getWeight())
1837                    .append(getParent(), other.getParent())
1838                    .append(getFoods(), other.getFoods())
1839                    .isEquals();
1840        }
1841
1842        public String JavaDoc toString()
1843        {
1844            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE, false, Entity.class);
1845        }
1846
1847        public Integer JavaDoc getId()
1848        {
1849            return id;
1850        }
1851
1852        public void setId(Integer JavaDoc id)
1853        {
1854            this.id = id;
1855        }
1856
1857        public String JavaDoc getName()
1858        {
1859            return name;
1860        }
1861
1862        public void setName(String JavaDoc name)
1863        {
1864            this.name = name;
1865        }
1866
1867        public int getWeight()
1868        {
1869            return weight;
1870        }
1871
1872        public void setWeight(int weight)
1873        {
1874            this.weight = weight;
1875        }
1876
1877        public Animal getParent()
1878        {
1879            return parent;
1880        }
1881
1882        public void setParent(Animal parent)
1883        {
1884            this.parent = parent;
1885        }
1886
1887        public void addFood(Food food)
1888        {
1889            foods.add(food);
1890        }
1891
1892        public List JavaDoc getFoods()
1893        {
1894            return foods;
1895        }
1896
1897        public void setFoods(List JavaDoc foods)
1898        {
1899            this.foods = foods;
1900        }
1901    }
1902
1903    public static class Dog
1904    {
1905        private Integer JavaDoc id;
1906        private int legs;
1907
1908        // these fields should be mapped to a super table
1909
private String JavaDoc name;
1910        private int weight;
1911        private Animal parent;
1912        private List JavaDoc foods = new ArrayList JavaDoc();
1913
1914        public Dog()
1915        {
1916        }
1917
1918        public Dog(String JavaDoc name, int weight, int legs)
1919        {
1920            this.name = name;
1921            this.weight = weight;
1922            this.legs = legs;
1923        }
1924
1925        public boolean equals(Object JavaDoc obj)
1926        {
1927            if (!(obj instanceof Dog))
1928            {
1929                return false;
1930            }
1931            Dog other = (Dog) obj;
1932            return new EqualsBuilder()
1933                    .append(getId(), other.getId())
1934                    .append(getName(), other.getName())
1935                    .append(getLegs(), other.getLegs())
1936                    .append(getWeight(), other.getWeight())
1937                    .append(getParent(), other.getParent())
1938                    .append((getFoods() != null ? new Integer JavaDoc(getFoods().size()) : null),
1939                            ((other.getFoods() != null ? new Integer JavaDoc(other.getFoods().size()) : null)))
1940                    .isEquals();
1941        }
1942
1943        public String JavaDoc toString()
1944        {
1945            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE, false, Dog.class);
1946        }
1947
1948        public Integer JavaDoc getId()
1949        {
1950            return id;
1951        }
1952
1953        public void setId(Integer JavaDoc id)
1954        {
1955            this.id = id;
1956        }
1957
1958        public String JavaDoc getName()
1959        {
1960            return name;
1961        }
1962
1963        public void setName(String JavaDoc name)
1964        {
1965            this.name = name;
1966        }
1967
1968        public int getWeight()
1969        {
1970            return weight;
1971        }
1972
1973        public void setWeight(int weight)
1974        {
1975            this.weight = weight;
1976        }
1977
1978        public int getLegs()
1979        {
1980            return legs;
1981        }
1982
1983        public void setLegs(int legs)
1984        {
1985            this.legs = legs;
1986        }
1987
1988        public Animal getParent()
1989        {
1990            return parent;
1991        }
1992
1993        public void setParent(Animal parent)
1994        {
1995            this.parent = parent;
1996        }
1997
1998        public void addFood(Food food)
1999        {
2000            this.foods.add(food);
2001        }
2002
2003        public List JavaDoc getFoods()
2004        {
2005            return foods;
2006        }
2007
2008        public void setFoods(List JavaDoc foods)
2009        {
2010            this.foods = foods;
2011        }
2012    }
2013
2014    public static class Food extends Entity
2015    {
2016        private Integer JavaDoc fkAnimal;
2017
2018        public Food()
2019        {
2020        }
2021
2022        public boolean equals(Object JavaDoc obj)
2023        {
2024            if (!(obj instanceof Food))
2025            {
2026                return false;
2027            }
2028            Food other = (Food) obj;
2029            return new EqualsBuilder()
2030                    .append(getFkAnimal(), other.getFkAnimal())
2031                    .isEquals() && super.equals(obj);
2032        }
2033
2034        public String JavaDoc toString()
2035        {
2036            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE, false, Food.class);
2037        }
2038
2039        public Food(String JavaDoc name)
2040        {
2041            super(name);
2042        }
2043
2044        public Integer JavaDoc getFkAnimal()
2045        {
2046            return fkAnimal;
2047        }
2048
2049        public void setFkAnimal(Integer JavaDoc fkAnimal)
2050        {
2051            this.fkAnimal = fkAnimal;
2052        }
2053    }
2054}
2055
Popular Tags