KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 public class NodeSearchQueryTest extends TestCase {
19     
20     /** Test instance. */
21     private NodeSearchQuery instance = null;
22     
23     /** MMBase instance. */
24     private MMBase mmbase = null;
25     
26     /** Exampler builders. */
27     private MMObjectBuilder images = null;
28     private MMObjectBuilder news = null;
29     private InsRel insrel = null;
30     
31     /** Example fields. */
32     private CoreField imagesTitle = null;
33     private CoreField newsTitle = null;
34     
35     public NodeSearchQueryTest(java.lang.String JavaDoc testName) {
36         super(testName);
37     }
38     
39     public static void main(java.lang.String JavaDoc[] args) {
40         junit.textui.TestRunner.run(suite());
41     }
42     
43     /**
44      * Sets up before each test.
45      */

46     public void setUp() throws Exception JavaDoc {
47         MMBaseContext.init();
48         mmbase = MMBase.getMMBase();
49         images = mmbase.getBuilder("images");
50         news = mmbase.getBuilder("news");
51         insrel = mmbase.getInsRel();
52         imagesTitle = images.getField("title");
53         newsTitle = news.getField("title");
54         instance = new NodeSearchQuery(images);
55     }
56     
57     /**
58      * Tears down after each test.
59      */

60     public void tearDown() throws Exception JavaDoc {}
61     
62     /** Test of constructor. */
63     public void testConstructor() {
64         try {
65             // Null builder, should throw IllegalArgumentException.
66
new NodeSearchQuery(null);
67             fail("Null builder, should throw IllegalArgumentException.");
68         } catch (IllegalArgumentException JavaDoc e) {}
69             
70         try {
71             // Virtual builder, should throw IllegalArgumentException.
72
new NodeSearchQuery(new ClusterBuilder(mmbase));
73             fail("Virtual builder, should throw IllegalArgumentException.");
74         } catch (IllegalArgumentException JavaDoc e) {}
75             
76         Collection fields = images.getFields();
77         List stepFields = instance.getFields();
78         Iterator iStepFields = stepFields.iterator();
79         // Test all elements in stepFields are persistent fields from images.
80
while (iStepFields.hasNext()) {
81             StepField stepField = (StepField) iStepFields.next();
82             CoreField field = images.getField(stepField.getFieldName());
83             //assertTrue("" + fields + " does not contain " + field, fields.contains(field));
84
//assertTrue(field.getType() != Field.TYPE_BINARY); // NodeSearchQuery is not in 'database', so it should not whine!
85
assertTrue(field.inStorage());
86         }
87         // Test all persistent fields from images are in query.
88
Iterator iFields = fields.iterator();
89         while (iFields.hasNext()) {
90             CoreField field = (CoreField) iFields.next();
91             if (field.getType() != Field.TYPE_BINARY && field.inStorage()) {
92                 assertTrue(instance.getField(field) != null);
93             }
94         }
95     }
96     
97     /** Test of getField method, of class org.mmbase.storage.search.implementation.NodeSearchQuery. */
98     public void testGetField() {
99         Step step = (Step) instance.getSteps().get(0);
100         Collection fields = images.getFields();
101         for (Iterator iFields = fields.iterator(); iFields.hasNext();) {
102             CoreField field = (CoreField) iFields.next();
103             if (field.inStorage()) {
104                 StepField stepField = instance.getField(field);
105                 assertTrue(stepField != null);
106                 assertTrue(stepField.getFieldName().equals(field.getName()));
107                 assertTrue(stepField.getAlias() == null);
108                 assertTrue(stepField.getType() == field.getType());
109                 assertTrue(stepField.getStep().equals(step));
110             } else {
111                 // Non-persistent field: should throw IllegalArgumentException.
112
try {
113                     instance.getField(field);
114                     fail("Non-persistent field: '" + field + "' should throw IllegalArgumentException.");
115                 } catch (IllegalArgumentException JavaDoc e) {}
116             }
117         }
118         
119         // Field not belonging to images: should throw IllegalArgumentException.
120
try {
121             instance.getField(newsTitle);
122             fail("Field not belonging to images: should throw IllegalArgumentException.");
123         } catch (IllegalArgumentException JavaDoc e) {}
124     }
125     
126     /** Test of addStep method, of class org.mmbase.storage.search.implementation.NodeSearchQuery. */
127     public void testAddStep() {
128         // Adding step, should throw UnsupportedOperationException.
129
try {
130             instance.addStep(news);
131             fail("Adding step, should throw UnsupportedOperationException.");
132         } catch (UnsupportedOperationException JavaDoc e) {}
133     }
134     
135     /** Test of addRelationStep method, of class org.mmbase.storage.search.implementation.NodeSearchQuery. */
136     public void testAddRelationStep() {
137         // Adding relation step, should throw UnsupportedOperationException.
138
try {
139             instance.addRelationStep(insrel, news);
140             fail("Adding relation step, should throw UnsupportedOperationException.");
141         } catch (UnsupportedOperationException JavaDoc e) {}
142     }
143     
144     /** Test of addField method, of class org.mmbase.storage.search.implementation.NodeSearchQuery. */
145     public void testAddField() {
146         Step step = (Step) instance.getSteps().get(0);
147
148         // Adding field, should throw UnsupportedOperationException.
149
try {
150             instance.addField(step, imagesTitle);
151             fail("Adding field, should throw UnsupportedOperationException.");
152         } catch (UnsupportedOperationException JavaDoc e) {}
153     }
154     
155     /** Test of addAggregatedField method, of class org.mmbase.storage.search.implementation.NodeSearchQuery. */
156     public void testAddAggregatedField() {
157         Step step = (Step) instance.getSteps().get(0);
158
159         // Adding field, should throw UnsupportedOperationException.
160
try {
161             instance.addAggregatedField(step, imagesTitle,
162                 AggregatedField.AGGREGATION_TYPE_MIN);
163             fail("Adding field, should throw UnsupportedOperationException.");
164         } catch (UnsupportedOperationException JavaDoc e) {}
165     }
166     
167     /** Test of getBuilder method, of class org.mmbase.storage.search.implementation.NodeSearchQuery. */
168     public void testGetBuilder() {
169         assertTrue(new NodeSearchQuery(images).getBuilder() == images);
170         assertTrue(new NodeSearchQuery(news).getBuilder() == news);
171     }
172     
173     public static Test suite() {
174         TestSuite suite = new TestSuite(NodeSearchQueryTest.class);
175         
176         return suite;
177     }
178     
179 }
180
Popular Tags