KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.apache.ojb.broker.query.Criteria;
7 import org.apache.ojb.broker.query.QueryByCriteria;
8 import org.apache.ojb.broker.query.QueryFactory;
9 import org.apache.ojb.junit.PBTestCase;
10
11 /**
12  * Tests for extent aware path expressions
13  *
14  * @author <a HREF="leandro@ibnetwork.com.br">Leandro Rodrigo Saad Cruz</a>
15  *
16  * $Id: ExtentAwarePathExpressionsTest.java,v 1.7.2.5 2005/12/03 11:19:44 arminw Exp $
17  */

18 public class ExtentAwarePathExpressionsTest extends PBTestCase
19 {
20     public static void main(String JavaDoc[] args)
21     {
22         String JavaDoc[] arr = {ExtentAwarePathExpressionsTest.class.getName()};
23         junit.textui.TestRunner.main(arr);
24     }
25
26     public ExtentAwarePathExpressionsTest(String JavaDoc testName)
27     {
28         super(testName);
29     }
30
31     public void testWithoutHintClass1() throws Exception JavaDoc
32     {
33         Criteria criteria = new Criteria();
34         criteria.addEqualTo("qualifiers.name", "Cars");
35         QueryByCriteria query = new QueryByCriteria(News.class, criteria);
36
37         broker.clearCache();
38         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
39         assertEquals(1, content.size());
40     }
41
42     public void testWithoutHintClass2() throws Exception JavaDoc
43     {
44         Criteria criteria = new Criteria();
45         criteria.addLike("qualifiers.name", "%ers%");
46         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
47
48         broker.clearCache();
49         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
50         assertEquals(2, content.size());
51     }
52
53     public void testNotNullPathElement()
54     {
55         Criteria criteria = new Criteria();
56         criteria.addNotNull("qualifiers.name");
57         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
58         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
59         assertEquals(4,content.size());
60     }
61
62     public void testSetPathClass()
63     {
64         Criteria criteria = new Criteria();
65         criteria.addNotNull("qualifiers.name");
66         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
67         query.setPathClass("qualifiers",Category.class);
68         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
69         assertEquals(2,content.size());
70         for (Iterator JavaDoc iter = content.iterator(); iter.hasNext();)
71         {
72             BaseContentImpl element = (BaseContentImpl) iter.next();
73             assertTrue(element.getId() <=3 && element.getId() >=2);
74         }
75
76     }
77
78     public void testSetPathClassInCriteria()
79     {
80         Criteria criteria = new Criteria();
81
82         criteria.addNotNull("qualifiers.name");
83         criteria.setPathClass("qualifiers", Category.class);
84
85         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
86         List JavaDoc content = (List JavaDoc)broker.getCollectionByQuery(query);
87
88         assertEquals(2,content.size());
89         for (Iterator JavaDoc iter = content.iterator(); iter.hasNext();)
90         {
91             BaseContentImpl element = (BaseContentImpl) iter.next();
92
93             assertTrue(element.getId() <=3 && element.getId() >=2);
94         }
95
96     }
97
98     public void testAddPathClass()
99     {
100         Criteria criteria = new Criteria();
101         criteria.addNotNull("qualifiers.name");
102         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
103         query.addPathClass("qualifiers",Qualifier.class);
104         query.addPathClass("qualifiers",Area.class);
105         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
106         assertEquals(1,content.size());
107         assertEquals(10,((Paper)content.get(0)).getId());
108     }
109
110     public void testAddPathClassInCriteria()
111     {
112         Criteria criteria = new Criteria();
113
114         criteria.addNotNull("qualifiers.name");
115         criteria.addPathClass("qualifiers", Qualifier.class);
116         criteria.addPathClass("qualifiers", Area.class);
117
118         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
119         List JavaDoc content = (List JavaDoc)broker.getCollectionByQuery(query);
120
121         assertEquals(1, content.size());
122         assertEquals(10, ((Paper)content.get(0)).getId());
123     }
124
125     /**
126      * The order of criteria elements influences the use of parenthesis,
127      * If we add the Like element before the NotNull one we get the wrong query
128      * Right clause : WHERE WHERE (A2.NAME IS NOT NULL OR A2E1.NAME IS NOT NULL ) AND A0.HEADLINE LIKE 'Bra%
129      * Wrong clause : WHERE (A0.HEADLINE LIKE 'Bra%' ) AND A2.NAME IS NOT NULL OR A2E1.NAME IS NOT NULL
130      */

131     public void testAddPathClasses()
132     {
133         Criteria criteria = new Criteria();
134
135         criteria.addLike("headline","Bra%");
136         criteria.addNotNull("qualifiers.name");
137         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
138         query.addPathClass("qualifiers",Qualifier.class);
139         query.addPathClass("qualifiers",Topic.class);
140         query.addPathClass("qualifiers",Category.class);
141
142         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
143         assertEquals(1,content.size());
144         assertEquals(3,((News)content.get(0)).getId());
145     }
146
147     public void testAddPathClassesInCriteria()
148     {
149         Criteria criteria = new Criteria();
150
151         criteria.addLike("headline", "Bra%");
152         criteria.addNotNull("qualifiers.name");
153         criteria.addPathClass("qualifiers", Qualifier.class);
154         criteria.addPathClass("qualifiers", Topic.class);
155         criteria.addPathClass("qualifiers", Category.class);
156
157         QueryByCriteria query = new QueryByCriteria(BaseContentImpl.class, criteria, true);
158         List JavaDoc content = (List JavaDoc)broker.getCollectionByQuery(query);
159
160         assertEquals(1, content.size());
161         assertEquals(3, ((News)content.get(0)).getId());
162     }
163
164     // Test for OJB-50
165
public void testComplexCriteriaWithPathClasses_1()
166     {
167         Criteria criteria = new Criteria();
168         Criteria categoryCriteria = new Criteria();
169         Criteria topicCriteria = new Criteria();
170
171         topicCriteria.addEqualTo("qualifiers.importance", "important");
172         topicCriteria.addPathClass("qualifiers", Topic.class);
173         criteria.addOrCriteria(topicCriteria);
174         categoryCriteria.addNotNull("qualifiers.description");
175         categoryCriteria.addPathClass("qualifiers", Category.class);
176         criteria.addOrCriteria(categoryCriteria);
177
178         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
179         List JavaDoc content = (List JavaDoc)broker.getCollectionByQuery(query);
180
181         assertEquals(1, content.size());
182         assertEquals(2, ((News)content.get(0)).getId());
183     }
184
185     // Test for OJB-50
186
public void testComplexCriteriaWithPathClasses_2()
187     {
188         Criteria criteria = new Criteria();
189         Criteria categoryCriteria = new Criteria();
190         Criteria topicCriteria = new Criteria();
191
192         categoryCriteria.addLike("qualifiers.description", "The%");
193         categoryCriteria.addPathClass("qualifiers", Category.class);
194         categoryCriteria.addPathClass("qualifiers", TopicExt.class);
195
196         topicCriteria.addEqualTo("qualifiers.importance", "important");
197         topicCriteria.addPathClass("qualifiers", Topic.class);
198         topicCriteria.addPathClass("qualifiers", TopicExt.class);
199
200         criteria.addOrCriteria(categoryCriteria);
201         criteria.addOrCriteria(topicCriteria);
202
203         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
204         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
205
206         assertEquals(1, content.size());
207         assertEquals(2, ((News)content.get(0)).getId());
208         assertNotNull(((News)content.get(0)).getQualifiers());
209         assertEquals(3, ((News)content.get(0)).getQualifiers().size());
210     }
211
212     // Test for OJB-50
213
public void testComplexCriteriaWithPathClasses_3()
214     {
215         Criteria criteria = new Criteria();
216         Criteria criteriaTopicExtAndCategory = new Criteria();
217         Criteria categoryCriteria = new Criteria();
218         Criteria topicCriteria = new Criteria();
219         Criteria topicExtCriteria = new Criteria();
220
221         categoryCriteria.addLike("qualifiers.description", "The buyer");
222         categoryCriteria.addPathClass("qualifiers", Category.class);
223
224         topicExtCriteria.addLike("qualifiers.description", "The buyer");
225         topicExtCriteria.addPathClass("qualifiers", TopicExt.class);
226
227         criteriaTopicExtAndCategory.addOrCriteria(categoryCriteria);
228         criteriaTopicExtAndCategory.addOrCriteria(topicExtCriteria);
229
230         topicCriteria.addEqualTo("qualifiers.importance", "important");
231         topicCriteria.addPathClass("qualifiers", Topic.class);
232         topicCriteria.addPathClass("qualifiers", TopicExt.class);
233
234         criteria.addAndCriteria(criteriaTopicExtAndCategory);
235         criteria.addAndCriteria(topicCriteria);
236
237         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
238         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
239
240         assertEquals(1, content.size());
241         assertEquals(2, ((News)content.get(0)).getId());
242         assertNotNull(((News)content.get(0)).getQualifiers());
243         assertEquals(3, ((News)content.get(0)).getQualifiers().size());
244     }
245
246     // Test for OJB-62
247
public void testAddClassPathOnQuery_1()
248     {
249         Criteria criteria = new Criteria();
250         criteria.addLike("qualifiers.importance", "impor%");
251         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
252         query.addPathClass("qualifiers", Topic.class);
253         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
254
255         assertEquals(1, content.size());
256         assertEquals(2, ((News)content.get(0)).getId());
257         assertNotNull(((News)content.get(0)).getQualifiers());
258         assertEquals(3, ((News)content.get(0)).getQualifiers().size());
259     }
260
261     // Test for OJB-62
262
public void testAddClassPathOnQuery_2()
263     {
264         Criteria criteria = new Criteria();
265         criteria.addLike("qualifiers.importance", "NO_MATCH%");
266         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
267         query.addPathClass("qualifiers", Topic.class);
268         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
269
270         assertEquals(0, content.size());
271     }
272
273     // Test multiple path class on query object
274
public void testMultipleClassPath_1a()
275     {
276         Criteria criteria = new Criteria();
277         criteria.addLike("headline", "SAL%");
278         criteria.addEqualTo("qualifiers.importance", "unimportant");
279         criteria.addEqualTo("qualifiers.name", "Sellers");
280         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
281         query.addPathClass("qualifiers", Qualifier.class);
282         query.addPathClass("qualifiers", Topic.class);
283         query.addPathClass("qualifiers", TopicExt.class);
284         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
285
286         assertEquals(1, content.size());
287         assertEquals(11, ((Paper)content.get(0)).getId());
288         assertNotNull(((Paper)content.get(0)).getQualifiers());
289         assertEquals(2, ((Paper)content.get(0)).getQualifiers().size());
290     }
291
292     // Test multiple path class on query object
293
public void testMultipleClassPath_1b()
294     {
295         Criteria criteria = new Criteria();
296         criteria.addLike("headline", "SAL%");
297         criteria.addEqualTo("qualifiers.importance", "unimportant");
298         criteria.addEqualTo("qualifiers.name", "Sellers");
299         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
300         //query.addPathClass("qualifiers", Qualifier.class);
301
query.addPathClass("qualifiers", Topic.class);
302         query.addPathClass("qualifiers", TopicExt.class);
303         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
304
305         assertEquals(1, content.size());
306         assertEquals(11, ((Paper)content.get(0)).getId());
307         assertNotNull(((Paper)content.get(0)).getQualifiers());
308         assertEquals(2, ((Paper)content.get(0)).getQualifiers().size());
309     }
310
311     // Test multiple path class on criteria object
312
public void testMultipleClassPath_2a()
313     {
314         Criteria criteria = new Criteria();
315         criteria.addLike("headline", "SAL%");
316         criteria.addEqualTo("qualifiers.importance", "unimportant");
317         criteria.addEqualTo("qualifiers.name", "Sellers");
318         criteria.addPathClass("qualifiers", Qualifier.class);
319         criteria.addPathClass("qualifiers", TopicExt.class);
320         criteria.addPathClass("qualifiers", Topic.class);
321         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
322         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
323
324         assertEquals(1, content.size());
325         assertEquals(11, ((Paper)content.get(0)).getId());
326         assertNotNull(((Paper)content.get(0)).getQualifiers());
327         assertEquals(2, ((Paper)content.get(0)).getQualifiers().size());
328     }
329
330     // Test multiple path class on query object
331
public void testMultipleClassPath_2b()
332     {
333         Criteria criteria = new Criteria();
334         criteria.addLike("headline", "SAL%");
335         criteria.addEqualTo("qualifiers.importance", "unimportant");
336         criteria.addEqualTo("qualifiers.name", "Sellers");
337         //criteria.addPathClass("qualifiers", Qualifier.class);
338
criteria.addPathClass("qualifiers", TopicExt.class);
339         criteria.addPathClass("qualifiers", Topic.class);
340         QueryByCriteria query = QueryFactory.newQuery(BaseContentImpl.class, criteria, true);
341         List JavaDoc content = (List JavaDoc) broker.getCollectionByQuery(query);
342
343         assertEquals(1, content.size());
344         assertEquals(11, ((Paper)content.get(0)).getId());
345         assertNotNull(((Paper)content.get(0)).getQualifiers());
346         assertEquals(2, ((Paper)content.get(0)).getQualifiers().size());
347     }
348 }
349
Popular Tags