KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > storage > search > implementation > BasicSearchQueryTest


1 package org.mmbase.storage.search.implementation;
2
3 import junit.framework.*;
4 import java.util.*;
5 import org.mmbase.module.core.MMObjectBuilder;
6 import org.mmbase.module.core.*;
7 import org.mmbase.module.corebuilders.*;
8 import org.mmbase.storage.search.*;
9
10 /**
11  * JUnit tests.
12  *
13  * @author Rob van Maris
14  * @version $Revision: 1.3 $
15  */

16 public class BasicSearchQueryTest extends TestCase {
17     
18     /** Test instance (non-aggregating). */
19     private BasicSearchQuery instance1;
20     
21     /** Test instance (aggregating). */
22     private BasicSearchQuery instance2;
23     
24     /** MMBase instance. */
25     private MMBase mmbase = null;
26     
27     /** Images builder, used as builder example. */
28     private MMObjectBuilder images = null;
29     
30     /** Insrel builder, used as relation builder example. */
31     private InsRel insrel = null;
32     
33     public BasicSearchQueryTest(java.lang.String JavaDoc testName) {
34         super(testName);
35     }
36     
37     public static void main(java.lang.String JavaDoc[] args) {
38         junit.textui.TestRunner.run(suite());
39     }
40     
41     /**
42      * Sets up before each test.
43      */

44     public void setUp() throws Exception JavaDoc {
45         MMBaseContext.init();
46         mmbase = MMBase.getMMBase();
47         images = mmbase.getBuilder("images");
48         insrel = mmbase.getInsRel();
49         mmbase.getBuilder("pools");
50         instance1 = new BasicSearchQuery();
51         instance2 = new BasicSearchQuery(true);
52     }
53     
54     /**
55      * Tears down after each test.
56      */

57     public void tearDown() throws Exception JavaDoc {}
58     
59     /** Test of setDistinct method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
60     public void testSetDistinct() {
61         // Default is false.
62
assertTrue(!instance1.isDistinct());
63         
64         BasicSearchQuery result = instance1.setDistinct(true);
65         assertTrue(instance1.isDistinct());
66         assertTrue(result == instance1);
67     }
68     
69     public void testIsAggregating() {
70         assertTrue(!instance1.isAggregating());
71         assertTrue(instance2.isAggregating());
72     }
73     
74     /** Test of setMaxNumber method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
75     public void testSetMaxNumber() {
76         // Default is max integer value.
77
assertTrue(instance1.getMaxNumber() == -1);
78         
79         BasicSearchQuery result = instance1.setMaxNumber(12345);
80         assertTrue(instance1.getMaxNumber() == 12345);
81         assertTrue(result == instance1);
82         
83         // Value less than -1, should throw IllegalArgumentException.
84
try {
85             instance1.setMaxNumber(-2);
86             fail("Value less than -1, should throw IllegalArgumentException.");
87         } catch (IllegalArgumentException JavaDoc e) {}
88     }
89     
90     /** Test of setOffset method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
91     public void testSetOffset() {
92         // Default is zero.
93
assertTrue(instance1.getOffset() == 0);
94         
95         // Invalid offset, should throw IllegalArgumentException.
96
try {
97             instance1.setOffset(-789);
98             fail("Invalid offset, should throw IllegalArgumentException.");
99         } catch (IllegalArgumentException JavaDoc e) {}
100         
101         BasicSearchQuery result = instance1.setOffset(123456);
102         assertTrue(instance1.getOffset() == 123456);
103         assertTrue(result == instance1);
104     }
105     
106     /** Test of addStep method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
107     public void testAddStep() {
108         // Null argument, should throw IllegalArgumentException
109
try {
110             instance1.addStep(null);
111             fail("Null argument, should throw IllegalArgumentException");
112         } catch (IllegalArgumentException JavaDoc e) {}
113         
114         List steps = instance1.getSteps();
115         assertTrue(steps.size() == 0);
116         Step step0 = instance1.addStep(images);
117         steps = instance1.getSteps();
118         assertTrue(steps.size() == 1);
119         assertTrue(steps.get(0) == step0);
120         Step step1 = instance1.addStep(images);
121         steps = instance1.getSteps();
122         assertTrue(steps.size() == 2);
123         assertTrue(steps.get(0) == step0);
124         assertTrue(steps.get(1) == step1);
125     }
126     
127     /** Test of addRelationStep method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
128     public void testAddRelationStep() {
129         // No previous step, should throw IllegalStateException
130
try {
131             instance1.addRelationStep(insrel, images);
132             fail("No previous step, should throw IllegalStateException");
133         } catch (IllegalStateException JavaDoc e) {}
134         
135         instance1.addStep(images);
136         
137         // Null builder argument, should throw IllegalArgumentException
138
try {
139             instance1.addRelationStep(null, images);
140             fail("Null builder argument, should throw IllegalArgumentException");
141         } catch (IllegalArgumentException JavaDoc e) {}
142         
143         // Null nextBuilder argument, should throw IllegalArgumentException
144
try {
145             instance1.addRelationStep(insrel, null);
146             fail("Null nextBuilder argument, should throw IllegalArgumentException");
147         } catch (IllegalArgumentException JavaDoc e) {}
148         
149         List steps = instance1.getSteps();
150         assertTrue(steps.size() == 1);
151         RelationStep relationStep = instance1.addRelationStep(insrel, images);
152         steps = instance1.getSteps();
153         assertTrue(steps.size() == 3);
154         assertTrue(steps.get(1) == relationStep);
155         Step next = relationStep.getNext();
156         assertTrue(steps.get(2) == next);
157     }
158     
159     /** Test of addField method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
160     public void testAddField() {
161         Step step = instance1.addStep(images);
162         FieldDefs imagesTitle = images.getField("title");
163         FieldDefs imagesDescription = images.getField("description");
164         
165         // Null step argument, should throw IllegalArgumentException
166
try {
167             instance1.addField(null, imagesTitle);
168             fail("Null step argument, should throw IllegalArgumentException");
169         } catch (IllegalArgumentException JavaDoc e) {}
170         
171         // Null fieldDefs argument, should throw IllegalArgumentException
172
try {
173             instance1.addField(step, null);
174             fail("Null fieldDefs argument, should throw IllegalArgumentException");
175         } catch (IllegalArgumentException JavaDoc e) {}
176         
177         List fields = instance1.getFields();
178         assertTrue(fields.size() == 0);
179         StepField field0 = instance1.addField(step, imagesTitle);
180         fields = instance1.getFields();
181         assertTrue(fields.size() == 1);
182         assertTrue(fields.indexOf(field0) == 0);
183         StepField field1 = instance1.addField(step,imagesDescription);
184         fields = instance1.getFields();
185         assertTrue(fields.size() == 2);
186         assertTrue(fields.indexOf(field0) == 0);
187         assertTrue(fields.indexOf(field1) == 1);
188         
189         // Aggregating query:
190
step = instance2.addStep(images);
191         try {
192             // Adding non-aggregatedg step to aggregating query, should throw UnsupportedOperationException.
193
instance2.addField(step, imagesTitle);
194             fail("Adding non-aggregated step to aggregating query, should throw UnsupportedOperationException.");
195         } catch (UnsupportedOperationException JavaDoc e) {}
196     }
197     
198     /** Test of addAggregatedField method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
199     public void testAddAggregatedField() {
200         Step step = instance2.addStep(images);
201         FieldDefs imagesTitle = images.getField("title");
202         FieldDefs imagesDescription = images.getField("description");
203         
204         // Null step argument, should throw IllegalArgumentException
205
try {
206             instance2.addAggregatedField(
207             null, imagesTitle, AggregatedField.AGGREGATION_TYPE_COUNT);
208             fail("Null step argument, should throw IllegalArgumentException");
209         } catch (IllegalArgumentException JavaDoc e) {}
210         
211         // Null fieldDefs argument, should throw IllegalArgumentException
212
try {
213             instance2.addAggregatedField(
214             step, null, AggregatedField.AGGREGATION_TYPE_COUNT);
215             fail("Null fieldDefs argument, should throw IllegalArgumentException");
216         } catch (IllegalArgumentException JavaDoc e) {}
217         
218         // Invalid aggregation type argument, should throw IllegalArgumentException
219
try {
220             instance2.addAggregatedField(
221             step, imagesTitle, 0);
222             fail("Invalid aggregation type argument, should throw IllegalArgumentException");
223         } catch (IllegalArgumentException JavaDoc e) {}
224         
225         List fields = instance2.getFields();
226         assertTrue(fields.size() == 0);
227         StepField field0 = instance2.addAggregatedField(
228         step, imagesTitle, AggregatedField.AGGREGATION_TYPE_COUNT);
229         fields = instance2.getFields();
230         assertTrue(fields.size() == 1);
231         assertTrue(fields.indexOf(field0) == 0);
232         StepField field1 = instance2.addAggregatedField(
233         step,imagesDescription, AggregatedField.AGGREGATION_TYPE_COUNT);
234         fields = instance2.getFields();
235         assertTrue(fields.size() == 2);
236         assertTrue(fields.indexOf(field0) == 0);
237         assertTrue(fields.indexOf(field1) == 1);
238         
239         // Non-aggregating query:
240
step = instance1.addStep(images);
241         try {
242             // Adding aggregated step to non-aggregating query, should throw UnsupportedOperationException.
243
instance1.addAggregatedField(
244             step, imagesTitle, AggregatedField.AGGREGATION_TYPE_COUNT);
245             fail("Adding aggregated step to non-aggregating query, should throw UnsupportedOperationException.");
246         } catch (UnsupportedOperationException JavaDoc e) {}
247     }
248     
249     /** Test of addSortOrder method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
250     public void testAddSortOrder() {
251         // Null step argument, should throw IllegalArgumentException
252
try {
253             instance1.addSortOrder(null);
254             fail("Null step argument, should throw IllegalArgumentException");
255         } catch (IllegalArgumentException JavaDoc e) {}
256         
257         Step step = instance1.addStep(images);
258         FieldDefs imagesTitle = images.getField("title");
259         FieldDefs imagesDescription = images.getField("description");
260         StepField field0 = instance1.addField(step, imagesTitle);
261         StepField field1 = instance1.addField(step, imagesDescription);
262         
263         List sortOrders = instance1.getSortOrders();
264         assertTrue(sortOrders.size() == 0);
265         SortOrder sortOrder0 = instance1.addSortOrder(field0);
266         sortOrders = instance1.getSortOrders();
267         assertTrue(sortOrders.size() == 1);
268         assertTrue(sortOrders.indexOf(sortOrder0) == 0);
269         SortOrder sortOrder1 = instance1.addSortOrder(field1);
270         sortOrders = instance1.getSortOrders();
271         assertTrue(sortOrders.size() == 2);
272         assertTrue(sortOrders.indexOf(sortOrder0) == 0);
273         assertTrue(sortOrders.indexOf(sortOrder1) == 1);
274     }
275     
276     /** Test of setConstraint method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
277     public void testSetConstraint() {
278         // Default is null.
279
assertTrue(instance1.getConstraint() == null);
280         
281         BasicConstraint constraint = new BasicConstraint();
282         instance1.setConstraint(constraint);
283         assertTrue(instance1.getConstraint() == constraint);
284         
285         // Null value allowed.
286
instance1.setConstraint(null);
287         assertTrue(instance1.getConstraint() == null);
288    }
289     
290     /** Test of isDistinct method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
291     public void testIsDistinct() {
292         // Same as:
293
testSetDistinct();
294     }
295     
296     /** Test of getSortOrders method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
297     public void testGetSortOrders() {
298         // See:
299
testAddSortOrder();
300         
301         List sortOrders = instance1.getSortOrders();
302         Object JavaDoc item = sortOrders.get(0);
303         
304         // List returned must be unmodifiable.
305
try {
306             sortOrders.add(item);
307             fail("Attempt to modify list, must throw UnsupportedOperationException");
308         } catch (UnsupportedOperationException JavaDoc e) {}
309         try {
310             sortOrders.clear();
311             fail("Attempt to modify list, must throw UnsupportedOperationException");
312         } catch (UnsupportedOperationException JavaDoc e) {}
313     }
314     
315     /** Test of getSteps method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
316     public void testGetSteps() {
317         // See:
318
testAddStep();
319         
320         List steps = instance1.getSteps();
321         Object JavaDoc item = steps.get(0);
322         
323         // List returned must be unmodifiable.
324
try {
325             steps.add(item);
326             fail("Attempt to modify list, must throw UnsupportedOperationException");
327         } catch (UnsupportedOperationException JavaDoc e) {}
328         try {
329             steps.clear();
330             fail("Attempt to modify list, must throw UnsupportedOperationException");
331         } catch (UnsupportedOperationException JavaDoc e) {}
332     }
333     
334     /** Test of getFields method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
335     public void testGetFields() {
336         // See:
337
testAddField();
338         
339         List fields = instance1.getFields();
340         Object JavaDoc item = fields.get(0);
341         
342         // List returned must be unmodifiable.
343
try {
344             fields.add(item);
345             fail("Attempt to modify list, must throw UnsupportedOperationException");
346         } catch (UnsupportedOperationException JavaDoc e) {}
347         try {
348             fields.clear();
349             fail("Attempt to modify list, must throw UnsupportedOperationException");
350         } catch (UnsupportedOperationException JavaDoc e) {}
351     }
352     
353     /** Test of getConstraint method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
354     public void testGetConstraint() {
355         // Same as:
356
testSetConstraint();
357     }
358     
359     /** Test of getMaxNumber method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
360     public void testGetMaxNumber() {
361         // Same as:
362
testSetMaxNumber();
363     }
364     
365     /** Test of getOffset method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
366     public void testGetOffset() {
367         // Same as:
368
testSetOffset();
369     }
370     
371     /** Test of equals method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
372     public void testEquals() {
373         // TODO: implement test
374
}
375     
376     /** Test of hashCode method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
377     public void testHashCode() {
378         // TODO: implement test
379
}
380     
381     /** Test of toString method, of class org.mmbase.storage.search.implementation.BasicSearchQuery. */
382     public void testToString() {
383         
384         BasicStep step1 = instance1.addStep(images);
385         BasicStepField field1a = instance1.addField(step1, images.getField("title"));
386         instance1.setConstraint(new BasicFieldNullConstraint(field1a));
387         instance1.addSortOrder(field1a);
388
389         assertTrue(instance1.toString(),
390         instance1.toString().equals(
391         "SearchQuery(distinct:" + instance1.isDistinct()
392         + ", steps:" + instance1.getSteps()
393         + ", fields:" + instance1.getFields()
394         + ", constraint:" + instance1.getConstraint()
395         + ", sortorders:" + instance1.getSortOrders()
396         + ", max:" + instance1.getMaxNumber()
397         + ", offset:" + instance1.getOffset() + ")"));
398
399         instance1.setDistinct(true);
400         assertTrue(instance1.toString(),
401         instance1.toString().equals(
402         "SearchQuery(distinct:" + instance1.isDistinct()
403         + ", steps:" + instance1.getSteps()
404         + ", fields:" + instance1.getFields()
405         + ", constraint:" + instance1.getConstraint()
406         + ", sortorders:" + instance1.getSortOrders()
407         + ", max:" + instance1.getMaxNumber()
408         + ", offset:" + instance1.getOffset() + ")"));
409
410         instance1.setMaxNumber(100)
411             .setOffset(50);
412         assertTrue(instance1.toString(),
413         instance1.toString().equals(
414         "SearchQuery(distinct:" + instance1.isDistinct()
415         + ", steps:" + instance1.getSteps()
416         + ", fields:" + instance1.getFields()
417         + ", constraint:" + instance1.getConstraint()
418         + ", sortorders:" + instance1.getSortOrders()
419         + ", max:" + instance1.getMaxNumber()
420         + ", offset:" + instance1.getOffset() + ")"));
421     }
422     
423     public static Test suite() {
424         TestSuite suite = new TestSuite(BasicSearchQueryTest.class);
425         
426         return suite;
427     }
428     
429 }
430
Popular Tags