KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import junit.framework.TestCase;
8 import org.apache.commons.lang.builder.ToStringBuilder;
9 import org.apache.ojb.broker.query.Criteria;
10 import org.apache.ojb.broker.query.Query;
11 import org.apache.ojb.broker.query.QueryFactory;
12
13 /**
14  *
15  *
16  * @author <a HREF="mailto:om@ppi.de">Oliver Matz</a>
17  * @version $Id: AbstractExtentClassTest.java,v 1.7.2.1 2005/08/16 15:11:33 aclute Exp $
18  */

19 public class AbstractExtentClassTest extends TestCase
20 {
21     private PersistenceBroker broker;
22
23     public AbstractExtentClassTest(String JavaDoc name)
24     {
25         super(name);
26     }
27
28     public static void main(String JavaDoc args[])
29     {
30         String JavaDoc[] arr = {AbstractExtentClassTest.class.getName()};
31         junit.textui.TestRunner.main(arr);
32     }
33
34     public void setUp() throws Exception JavaDoc
35     {
36         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
37     }
38
39     public void tearDown()
40     {
41         try
42         {
43             broker.clearCache();
44             broker.close();
45         }
46         catch (PersistenceBrokerException e)
47         {
48         }
49     }
50
51     /**
52      * Used data model:
53      * AbstractIF_X <-- AbstractIF_Y <-- ConcreteZ
54      * AbstractIF_X <-- AbstractClassX <-- AbstractClassY <-- ConcreteZZ
55      */

56     public void testStoreRetrieveQueryUsingInterface() throws Exception JavaDoc
57     {
58         String JavaDoc name = "interface_test_"+System.currentTimeMillis();
59         broker.clearCache();
60         broker.beginTransaction();
61         // create new XContainer with ConcreteZ and ConreteZZ references
62
XContainer container = new XContainer();
63         container.addX(new ConcreteZ(name));
64         container.addX(new ConcreteZ(name));
65         // we a set additional field 'zzName' in ConcreteZZ
66
container.addX(new ConcreteZZ(name, "ZZ"));
67         container.addX(new ConcreteZZ(name, "ZZ"));
68         container.addX(new ConcreteZZ(name, "ZZ"));
69
70         broker.store(container);
71         broker.commitTransaction();
72         broker.clearCache();
73
74         Identity cont = new Identity(container, broker);
75         broker.beginTransaction();
76         XContainer retContainer = (XContainer) broker.getObjectByIdentity(cont);
77         broker.commitTransaction();
78         Collection JavaDoc res = retContainer.getXReferences();
79         assertNotNull(res);
80         assertEquals(5, res.size());
81         boolean found = false;
82         for (Iterator JavaDoc iterator = res.iterator(); iterator.hasNext();)
83         {
84             Object JavaDoc o = iterator.next();
85             if(o instanceof ConcreteZZ)
86             {
87                 ConcreteZZ zz = (ConcreteZZ) o;
88                 assertNotNull(zz.getConcreteZZName());
89                 assertEquals("ZZ", zz.getConcreteZZName());
90                 found = true;
91             }
92         }
93         assertTrue("No ConcreteZZ instances be returned",found);
94
95         broker.clearCache();
96
97         // test query base interface
98
Criteria crit = new Criteria();
99         crit.addLike("name", name);
100         Query q = QueryFactory.newQuery(AbstractIF_X.class, crit);
101         Collection JavaDoc results = broker.getCollectionByQuery(q);
102         assertNotNull(results);
103         assertEquals(5, results.size());
104         found = false;
105         for (Iterator JavaDoc iterator = results.iterator(); iterator.hasNext();)
106         {
107             Object JavaDoc o = iterator.next();
108             if(o instanceof ConcreteZZ)
109             {
110                 ConcreteZZ zz = (ConcreteZZ) o;
111                 assertNotNull(zz.getConcreteZZName());
112                 assertEquals("ZZ", zz.getConcreteZZName());
113                 found = true;
114             }
115         }
116         assertTrue("No ConcreteZZ instances be returned",found);
117
118         // test query abstract class
119
broker.clearCache();
120         crit = new Criteria();
121         crit.addLike("name", name);
122         q = QueryFactory.newQuery(AbstractClassX.class, crit);
123         results = broker.getCollectionByQuery(q);
124         assertNotNull(results);
125         assertEquals(3, results.size());
126         for (Iterator JavaDoc iterator = results.iterator(); iterator.hasNext();)
127         {
128             Object JavaDoc o = iterator.next();
129             ConcreteZZ zz = (ConcreteZZ) o;
130             assertNotNull(zz.getConcreteZZName());
131             assertEquals("ZZ", zz.getConcreteZZName());
132         }
133
134         // test query abstract class
135
broker.clearCache();
136         crit = new Criteria();
137         crit.addLike("name", name);
138         q = QueryFactory.newQuery(AbstractClassY.class, crit);
139         results = broker.getCollectionByQuery(q);
140         assertNotNull(results);
141         assertEquals(3, results.size());
142         for (Iterator JavaDoc iterator = results.iterator(); iterator.hasNext();)
143         {
144             Object JavaDoc o = iterator.next();
145             ConcreteZZ zz = (ConcreteZZ) o;
146             assertNotNull(zz.getConcreteZZName());
147             assertEquals("ZZ", zz.getConcreteZZName());
148         }
149
150         // test query extended interface
151
broker.clearCache();
152         crit = new Criteria();
153         crit.addLike("name", name);
154         q = QueryFactory.newQuery(AbstractIF_Y.class, crit);
155         results = broker.getCollectionByQuery(q);
156         assertNotNull(results);
157         assertEquals(2, results.size());
158
159         // test query concrete class
160
broker.clearCache();
161         crit = new Criteria();
162         crit.addLike("name", name);
163         q = QueryFactory.newQuery(ConcreteZ.class, crit);
164         results = broker.getCollectionByQuery(q);
165         assertNotNull(results);
166         assertEquals(2, results.size());
167
168         // test query concrete class
169
broker.clearCache();
170         crit = new Criteria();
171         crit.addLike("name", name);
172         q = QueryFactory.newQuery(ConcreteZZ.class, crit);
173         results = broker.getCollectionByQuery(q);
174         assertNotNull(results);
175         assertEquals(3, results.size());
176         for (Iterator JavaDoc iterator = results.iterator(); iterator.hasNext();)
177         {
178             Object JavaDoc o = iterator.next();
179             ConcreteZZ zz = (ConcreteZZ) o;
180             assertNotNull(zz.getConcreteZZName());
181             assertEquals("ZZ", zz.getConcreteZZName());
182         }
183     }
184
185
186
187     //******************************************************************
188
// inner classes / persistent objects
189
//******************************************************************
190

191     public static abstract interface AbstractIF_X
192     {
193         public int getId();
194
195         public int getContainerId();
196
197         public void setContainerId(int containerId);
198
199         public void setName(String JavaDoc name);
200
201         public String JavaDoc getName();
202     }
203
204     public static abstract interface AbstractIF_Y extends AbstractIF_X
205     {
206     }
207
208     public static class ConcreteZ implements AbstractIF_Y
209     {
210         private int containerId;
211         private int someValue;
212         private int id;
213         private String JavaDoc name;
214
215         public ConcreteZ()
216         {
217         }
218
219         public ConcreteZ(String JavaDoc name)
220         {
221             this.name = name;
222         }
223
224         ConcreteZ(int no)
225         {
226             someValue = no;
227         }
228
229         public int getContainerId()
230         {
231             return containerId;
232         }
233
234         public void setContainerId(int containerId)
235         {
236             this.containerId = containerId;
237         }
238
239         public int getId()
240         {
241             return id;
242         }
243
244         public void setId(int id)
245         {
246             this.id = id;
247         }
248
249         public String JavaDoc getName()
250         {
251             return name;
252         }
253
254         public void setName(String JavaDoc name)
255         {
256             this.name = name;
257         }
258
259         public int getSomeValue()
260         {
261             return someValue;
262         }
263
264         public void setSomeValue(int someValue)
265         {
266             this.someValue = someValue;
267         }
268
269         public String JavaDoc toString()
270         {
271             return (new ToStringBuilder(this)).
272                     append("id", id).
273                     append("someValue", someValue).toString();
274         }
275     }
276
277     public static class XContainer
278     {
279         private int id;
280         private Collection JavaDoc myXReferences;
281
282         public XContainer()
283         {
284         }
285
286         public XContainer(int id)
287         {
288             this.id = id;
289         }
290
291         // make javabean conform
292
public Collection JavaDoc getMyXReferences()
293         {
294             return myXReferences;
295         }
296
297         public void setMyXReferences(Collection JavaDoc myXReferences)
298         {
299             this.myXReferences = myXReferences;
300         }
301
302         public void addX(AbstractIF_X someX)
303         {
304             if (myXReferences == null) myXReferences = new ArrayList JavaDoc();
305             myXReferences.add(someX);
306         }
307
308         public Collection JavaDoc getXReferences()
309         {
310             return myXReferences;
311         }
312
313         public int getId()
314         {
315             return id;
316         }
317
318         public void setId(int id)
319         {
320             this.id = id;
321         }
322
323         public String JavaDoc toString()
324         {
325             return (new ToStringBuilder(this)).
326                     append("id", id).
327                     append("myXReferences", myXReferences).toString();
328         }
329     }
330
331     public static abstract class AbstractClassX implements AbstractIF_X
332     {
333         private int containerId;
334         private String JavaDoc name;
335
336         public AbstractClassX()
337         {
338         }
339
340         public AbstractClassX(String JavaDoc name)
341         {
342             this.name = name;
343         }
344
345         public int getContainerId()
346         {
347             return containerId;
348         }
349
350         public void setContainerId(int containerId)
351         {
352             this.containerId = containerId;
353         }
354
355         public String JavaDoc getName()
356         {
357             return name;
358         }
359
360         public void setName(String JavaDoc name)
361         {
362             this.name = name;
363         }
364     }
365
366     public static abstract class AbstractClassY extends AbstractClassX
367     {
368         public AbstractClassY()
369         {
370         }
371
372         public AbstractClassY(String JavaDoc name)
373         {
374             super(name);
375         }
376     }
377
378     public static class ConcreteZZ extends AbstractClassY
379     {
380
381         private int someValue;
382         private int id;
383         private String JavaDoc concreteZZName;
384
385         public ConcreteZZ()
386         {
387         }
388
389         public ConcreteZZ(String JavaDoc name, String JavaDoc zzName)
390         {
391             super(name);
392             this.concreteZZName = zzName;
393         }
394
395         public int getId()
396         {
397             return id;
398         }
399
400         public void setId(int id)
401         {
402             this.id = id;
403         }
404
405         public String JavaDoc getConcreteZZName()
406         {
407             return concreteZZName;
408         }
409
410         public void setConcreteZZName(String JavaDoc concreteZZName)
411         {
412             this.concreteZZName = concreteZZName;
413         }
414
415         public int getSomeValue()
416         {
417             return someValue;
418         }
419
420         public void setSomeValue(int someValue)
421         {
422             this.someValue = someValue;
423         }
424
425         public String JavaDoc toString()
426         {
427             return (new ToStringBuilder(this)).
428                     append("id", id).
429                     append("someValue", someValue).toString();
430         }
431     }
432 }
433
Popular Tags