001: package org.apache.ojb.broker;
002:
003: import java.util.Iterator;
004: import java.util.List;
005:
006: import org.apache.ojb.broker.query.Criteria;
007: import org.apache.ojb.broker.query.QueryByCriteria;
008: import org.apache.ojb.broker.query.QueryFactory;
009: import org.apache.ojb.junit.PBTestCase;
010:
011: /**
012: * Tests for extent aware path expressions
013: *
014: * @author <a href="leandro@ibnetwork.com.br">Leandro Rodrigo Saad Cruz</a>
015: *
016: * $Id: ExtentAwarePathExpressionsTest.java,v 1.7.2.5 2005/12/03 11:19:44 arminw Exp $
017: */
018: public class ExtentAwarePathExpressionsTest extends PBTestCase {
019: public static void main(String[] args) {
020: String[] arr = { ExtentAwarePathExpressionsTest.class.getName() };
021: junit.textui.TestRunner.main(arr);
022: }
023:
024: public ExtentAwarePathExpressionsTest(String testName) {
025: super (testName);
026: }
027:
028: public void testWithoutHintClass1() throws Exception {
029: Criteria criteria = new Criteria();
030: criteria.addEqualTo("qualifiers.name", "Cars");
031: QueryByCriteria query = new QueryByCriteria(News.class,
032: criteria);
033:
034: broker.clearCache();
035: List content = (List) broker.getCollectionByQuery(query);
036: assertEquals(1, content.size());
037: }
038:
039: public void testWithoutHintClass2() throws Exception {
040: Criteria criteria = new Criteria();
041: criteria.addLike("qualifiers.name", "%ers%");
042: QueryByCriteria query = new QueryByCriteria(
043: BaseContentImpl.class, criteria, true);
044:
045: broker.clearCache();
046: List content = (List) broker.getCollectionByQuery(query);
047: assertEquals(2, content.size());
048: }
049:
050: public void testNotNullPathElement() {
051: Criteria criteria = new Criteria();
052: criteria.addNotNull("qualifiers.name");
053: QueryByCriteria query = new QueryByCriteria(
054: BaseContentImpl.class, criteria, true);
055: List content = (List) broker.getCollectionByQuery(query);
056: assertEquals(4, content.size());
057: }
058:
059: public void testSetPathClass() {
060: Criteria criteria = new Criteria();
061: criteria.addNotNull("qualifiers.name");
062: QueryByCriteria query = new QueryByCriteria(
063: BaseContentImpl.class, criteria, true);
064: query.setPathClass("qualifiers", Category.class);
065: List content = (List) broker.getCollectionByQuery(query);
066: assertEquals(2, content.size());
067: for (Iterator iter = content.iterator(); iter.hasNext();) {
068: BaseContentImpl element = (BaseContentImpl) iter.next();
069: assertTrue(element.getId() <= 3 && element.getId() >= 2);
070: }
071:
072: }
073:
074: public void testSetPathClassInCriteria() {
075: Criteria criteria = new Criteria();
076:
077: criteria.addNotNull("qualifiers.name");
078: criteria.setPathClass("qualifiers", Category.class);
079:
080: QueryByCriteria query = new QueryByCriteria(
081: BaseContentImpl.class, criteria, true);
082: List content = (List) broker.getCollectionByQuery(query);
083:
084: assertEquals(2, content.size());
085: for (Iterator iter = content.iterator(); iter.hasNext();) {
086: BaseContentImpl element = (BaseContentImpl) iter.next();
087:
088: assertTrue(element.getId() <= 3 && element.getId() >= 2);
089: }
090:
091: }
092:
093: public void testAddPathClass() {
094: Criteria criteria = new Criteria();
095: criteria.addNotNull("qualifiers.name");
096: QueryByCriteria query = new QueryByCriteria(
097: BaseContentImpl.class, criteria, true);
098: query.addPathClass("qualifiers", Qualifier.class);
099: query.addPathClass("qualifiers", Area.class);
100: List content = (List) broker.getCollectionByQuery(query);
101: assertEquals(1, content.size());
102: assertEquals(10, ((Paper) content.get(0)).getId());
103: }
104:
105: public void testAddPathClassInCriteria() {
106: Criteria criteria = new Criteria();
107:
108: criteria.addNotNull("qualifiers.name");
109: criteria.addPathClass("qualifiers", Qualifier.class);
110: criteria.addPathClass("qualifiers", Area.class);
111:
112: QueryByCriteria query = new QueryByCriteria(
113: BaseContentImpl.class, criteria, true);
114: List content = (List) broker.getCollectionByQuery(query);
115:
116: assertEquals(1, content.size());
117: assertEquals(10, ((Paper) content.get(0)).getId());
118: }
119:
120: /**
121: * The order of criteria elements influences the use of parenthesis,
122: * If we add the Like element before the NotNull one we get the wrong query
123: * Right clause : WHERE WHERE (A2.NAME IS NOT NULL OR A2E1.NAME IS NOT NULL ) AND A0.HEADLINE LIKE 'Bra%
124: * Wrong clause : WHERE (A0.HEADLINE LIKE 'Bra%' ) AND A2.NAME IS NOT NULL OR A2E1.NAME IS NOT NULL
125: */
126: public void testAddPathClasses() {
127: Criteria criteria = new Criteria();
128:
129: criteria.addLike("headline", "Bra%");
130: criteria.addNotNull("qualifiers.name");
131: QueryByCriteria query = new QueryByCriteria(
132: BaseContentImpl.class, criteria, true);
133: query.addPathClass("qualifiers", Qualifier.class);
134: query.addPathClass("qualifiers", Topic.class);
135: query.addPathClass("qualifiers", Category.class);
136:
137: List content = (List) broker.getCollectionByQuery(query);
138: assertEquals(1, content.size());
139: assertEquals(3, ((News) content.get(0)).getId());
140: }
141:
142: public void testAddPathClassesInCriteria() {
143: Criteria criteria = new Criteria();
144:
145: criteria.addLike("headline", "Bra%");
146: criteria.addNotNull("qualifiers.name");
147: criteria.addPathClass("qualifiers", Qualifier.class);
148: criteria.addPathClass("qualifiers", Topic.class);
149: criteria.addPathClass("qualifiers", Category.class);
150:
151: QueryByCriteria query = new QueryByCriteria(
152: BaseContentImpl.class, criteria, true);
153: List content = (List) broker.getCollectionByQuery(query);
154:
155: assertEquals(1, content.size());
156: assertEquals(3, ((News) content.get(0)).getId());
157: }
158:
159: // Test for OJB-50
160: public void testComplexCriteriaWithPathClasses_1() {
161: Criteria criteria = new Criteria();
162: Criteria categoryCriteria = new Criteria();
163: Criteria topicCriteria = new Criteria();
164:
165: topicCriteria.addEqualTo("qualifiers.importance", "important");
166: topicCriteria.addPathClass("qualifiers", Topic.class);
167: criteria.addOrCriteria(topicCriteria);
168: categoryCriteria.addNotNull("qualifiers.description");
169: categoryCriteria.addPathClass("qualifiers", Category.class);
170: criteria.addOrCriteria(categoryCriteria);
171:
172: QueryByCriteria query = QueryFactory.newQuery(
173: BaseContentImpl.class, criteria, true);
174: List content = (List) broker.getCollectionByQuery(query);
175:
176: assertEquals(1, content.size());
177: assertEquals(2, ((News) content.get(0)).getId());
178: }
179:
180: // Test for OJB-50
181: public void testComplexCriteriaWithPathClasses_2() {
182: Criteria criteria = new Criteria();
183: Criteria categoryCriteria = new Criteria();
184: Criteria topicCriteria = new Criteria();
185:
186: categoryCriteria.addLike("qualifiers.description", "The%");
187: categoryCriteria.addPathClass("qualifiers", Category.class);
188: categoryCriteria.addPathClass("qualifiers", TopicExt.class);
189:
190: topicCriteria.addEqualTo("qualifiers.importance", "important");
191: topicCriteria.addPathClass("qualifiers", Topic.class);
192: topicCriteria.addPathClass("qualifiers", TopicExt.class);
193:
194: criteria.addOrCriteria(categoryCriteria);
195: criteria.addOrCriteria(topicCriteria);
196:
197: QueryByCriteria query = QueryFactory.newQuery(
198: BaseContentImpl.class, criteria, true);
199: List content = (List) broker.getCollectionByQuery(query);
200:
201: assertEquals(1, content.size());
202: assertEquals(2, ((News) content.get(0)).getId());
203: assertNotNull(((News) content.get(0)).getQualifiers());
204: assertEquals(3, ((News) content.get(0)).getQualifiers().size());
205: }
206:
207: // Test for OJB-50
208: public void testComplexCriteriaWithPathClasses_3() {
209: Criteria criteria = new Criteria();
210: Criteria criteriaTopicExtAndCategory = new Criteria();
211: Criteria categoryCriteria = new Criteria();
212: Criteria topicCriteria = new Criteria();
213: Criteria topicExtCriteria = new Criteria();
214:
215: categoryCriteria.addLike("qualifiers.description", "The buyer");
216: categoryCriteria.addPathClass("qualifiers", Category.class);
217:
218: topicExtCriteria.addLike("qualifiers.description", "The buyer");
219: topicExtCriteria.addPathClass("qualifiers", TopicExt.class);
220:
221: criteriaTopicExtAndCategory.addOrCriteria(categoryCriteria);
222: criteriaTopicExtAndCategory.addOrCriteria(topicExtCriteria);
223:
224: topicCriteria.addEqualTo("qualifiers.importance", "important");
225: topicCriteria.addPathClass("qualifiers", Topic.class);
226: topicCriteria.addPathClass("qualifiers", TopicExt.class);
227:
228: criteria.addAndCriteria(criteriaTopicExtAndCategory);
229: criteria.addAndCriteria(topicCriteria);
230:
231: QueryByCriteria query = QueryFactory.newQuery(
232: BaseContentImpl.class, criteria, true);
233: List content = (List) broker.getCollectionByQuery(query);
234:
235: assertEquals(1, content.size());
236: assertEquals(2, ((News) content.get(0)).getId());
237: assertNotNull(((News) content.get(0)).getQualifiers());
238: assertEquals(3, ((News) content.get(0)).getQualifiers().size());
239: }
240:
241: // Test for OJB-62
242: public void testAddClassPathOnQuery_1() {
243: Criteria criteria = new Criteria();
244: criteria.addLike("qualifiers.importance", "impor%");
245: QueryByCriteria query = QueryFactory.newQuery(
246: BaseContentImpl.class, criteria, true);
247: query.addPathClass("qualifiers", Topic.class);
248: List content = (List) broker.getCollectionByQuery(query);
249:
250: assertEquals(1, content.size());
251: assertEquals(2, ((News) content.get(0)).getId());
252: assertNotNull(((News) content.get(0)).getQualifiers());
253: assertEquals(3, ((News) content.get(0)).getQualifiers().size());
254: }
255:
256: // Test for OJB-62
257: public void testAddClassPathOnQuery_2() {
258: Criteria criteria = new Criteria();
259: criteria.addLike("qualifiers.importance", "NO_MATCH%");
260: QueryByCriteria query = QueryFactory.newQuery(
261: BaseContentImpl.class, criteria, true);
262: query.addPathClass("qualifiers", Topic.class);
263: List content = (List) broker.getCollectionByQuery(query);
264:
265: assertEquals(0, content.size());
266: }
267:
268: // Test multiple path class on query object
269: public void testMultipleClassPath_1a() {
270: Criteria criteria = new Criteria();
271: criteria.addLike("headline", "SAL%");
272: criteria.addEqualTo("qualifiers.importance", "unimportant");
273: criteria.addEqualTo("qualifiers.name", "Sellers");
274: QueryByCriteria query = QueryFactory.newQuery(
275: BaseContentImpl.class, criteria, true);
276: query.addPathClass("qualifiers", Qualifier.class);
277: query.addPathClass("qualifiers", Topic.class);
278: query.addPathClass("qualifiers", TopicExt.class);
279: List content = (List) broker.getCollectionByQuery(query);
280:
281: assertEquals(1, content.size());
282: assertEquals(11, ((Paper) content.get(0)).getId());
283: assertNotNull(((Paper) content.get(0)).getQualifiers());
284: assertEquals(2, ((Paper) content.get(0)).getQualifiers().size());
285: }
286:
287: // Test multiple path class on query object
288: public void testMultipleClassPath_1b() {
289: Criteria criteria = new Criteria();
290: criteria.addLike("headline", "SAL%");
291: criteria.addEqualTo("qualifiers.importance", "unimportant");
292: criteria.addEqualTo("qualifiers.name", "Sellers");
293: QueryByCriteria query = QueryFactory.newQuery(
294: BaseContentImpl.class, criteria, true);
295: //query.addPathClass("qualifiers", Qualifier.class);
296: query.addPathClass("qualifiers", Topic.class);
297: query.addPathClass("qualifiers", TopicExt.class);
298: List content = (List) broker.getCollectionByQuery(query);
299:
300: assertEquals(1, content.size());
301: assertEquals(11, ((Paper) content.get(0)).getId());
302: assertNotNull(((Paper) content.get(0)).getQualifiers());
303: assertEquals(2, ((Paper) content.get(0)).getQualifiers().size());
304: }
305:
306: // Test multiple path class on criteria object
307: public void testMultipleClassPath_2a() {
308: Criteria criteria = new Criteria();
309: criteria.addLike("headline", "SAL%");
310: criteria.addEqualTo("qualifiers.importance", "unimportant");
311: criteria.addEqualTo("qualifiers.name", "Sellers");
312: criteria.addPathClass("qualifiers", Qualifier.class);
313: criteria.addPathClass("qualifiers", TopicExt.class);
314: criteria.addPathClass("qualifiers", Topic.class);
315: QueryByCriteria query = QueryFactory.newQuery(
316: BaseContentImpl.class, criteria, true);
317: List content = (List) broker.getCollectionByQuery(query);
318:
319: assertEquals(1, content.size());
320: assertEquals(11, ((Paper) content.get(0)).getId());
321: assertNotNull(((Paper) content.get(0)).getQualifiers());
322: assertEquals(2, ((Paper) content.get(0)).getQualifiers().size());
323: }
324:
325: // Test multiple path class on query object
326: public void testMultipleClassPath_2b() {
327: Criteria criteria = new Criteria();
328: criteria.addLike("headline", "SAL%");
329: criteria.addEqualTo("qualifiers.importance", "unimportant");
330: criteria.addEqualTo("qualifiers.name", "Sellers");
331: //criteria.addPathClass("qualifiers", Qualifier.class);
332: criteria.addPathClass("qualifiers", TopicExt.class);
333: criteria.addPathClass("qualifiers", Topic.class);
334: QueryByCriteria query = QueryFactory.newQuery(
335: BaseContentImpl.class, criteria, true);
336: List content = (List) broker.getCollectionByQuery(query);
337:
338: assertEquals(1, content.size());
339: assertEquals(11, ((Paper) content.get(0)).getId());
340: assertNotNull(((Paper) content.get(0)).getQualifiers());
341: assertEquals(2, ((Paper) content.get(0)).getQualifiers().size());
342: }
343: }
|