| 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 16 public class BasicSearchQueryTest extends TestCase { 17 18 19 private BasicSearchQuery instance1; 20 21 22 private BasicSearchQuery instance2; 23 24 25 private MMBase mmbase = null; 26 27 28 private MMObjectBuilder images = null; 29 30 31 private InsRel insrel = null; 32 33 public BasicSearchQueryTest(java.lang.String testName) { 34 super(testName); 35 } 36 37 public static void main(java.lang.String [] args) { 38 junit.textui.TestRunner.run(suite()); 39 } 40 41 44 public void setUp() throws Exception { 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 57 public void tearDown() throws Exception {} 58 59 60 public void testSetDistinct() { 61 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 75 public void testSetMaxNumber() { 76 assertTrue(instance1.getMaxNumber() == -1); 78 79 BasicSearchQuery result = instance1.setMaxNumber(12345); 80 assertTrue(instance1.getMaxNumber() == 12345); 81 assertTrue(result == instance1); 82 83 try { 85 instance1.setMaxNumber(-2); 86 fail("Value less than -1, should throw IllegalArgumentException."); 87 } catch (IllegalArgumentException e) {} 88 } 89 90 91 public void testSetOffset() { 92 assertTrue(instance1.getOffset() == 0); 94 95 try { 97 instance1.setOffset(-789); 98 fail("Invalid offset, should throw IllegalArgumentException."); 99 } catch (IllegalArgumentException e) {} 100 101 BasicSearchQuery result = instance1.setOffset(123456); 102 assertTrue(instance1.getOffset() == 123456); 103 assertTrue(result == instance1); 104 } 105 106 107 public void testAddStep() { 108 try { 110 instance1.addStep(null); 111 fail("Null argument, should throw IllegalArgumentException"); 112 } catch (IllegalArgumentException 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 128 public void testAddRelationStep() { 129 try { 131 instance1.addRelationStep(insrel, images); 132 fail("No previous step, should throw IllegalStateException"); 133 } catch (IllegalStateException e) {} 134 135 instance1.addStep(images); 136 137 try { 139 instance1.addRelationStep(null, images); 140 fail("Null builder argument, should throw IllegalArgumentException"); 141 } catch (IllegalArgumentException e) {} 142 143 try { 145 instance1.addRelationStep(insrel, null); 146 fail("Null nextBuilder argument, should throw IllegalArgumentException"); 147 } catch (IllegalArgumentException 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 160 public void testAddField() { 161 Step step = instance1.addStep(images); 162 FieldDefs imagesTitle = images.getField("title"); 163 FieldDefs imagesDescription = images.getField("description"); 164 165 try { 167 instance1.addField(null, imagesTitle); 168 fail("Null step argument, should throw IllegalArgumentException"); 169 } catch (IllegalArgumentException e) {} 170 171 try { 173 instance1.addField(step, null); 174 fail("Null fieldDefs argument, should throw IllegalArgumentException"); 175 } catch (IllegalArgumentException 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 step = instance2.addStep(images); 191 try { 192 instance2.addField(step, imagesTitle); 194 fail("Adding non-aggregated step to aggregating query, should throw UnsupportedOperationException."); 195 } catch (UnsupportedOperationException e) {} 196 } 197 198 199 public void testAddAggregatedField() { 200 Step step = instance2.addStep(images); 201 FieldDefs imagesTitle = images.getField("title"); 202 FieldDefs imagesDescription = images.getField("description"); 203 204 try { 206 instance2.addAggregatedField( 207 null, imagesTitle, AggregatedField.AGGREGATION_TYPE_COUNT); 208 fail("Null step argument, should throw IllegalArgumentException"); 209 } catch (IllegalArgumentException e) {} 210 211 try { 213 instance2.addAggregatedField( 214 step, null, AggregatedField.AGGREGATION_TYPE_COUNT); 215 fail("Null fieldDefs argument, should throw IllegalArgumentException"); 216 } catch (IllegalArgumentException e) {} 217 218 try { 220 instance2.addAggregatedField( 221 step, imagesTitle, 0); 222 fail("Invalid aggregation type argument, should throw IllegalArgumentException"); 223 } catch (IllegalArgumentException 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 step = instance1.addStep(images); 241 try { 242 instance1.addAggregatedField( 244 step, imagesTitle, AggregatedField.AGGREGATION_TYPE_COUNT); 245 fail("Adding aggregated step to non-aggregating query, should throw UnsupportedOperationException."); 246 } catch (UnsupportedOperationException e) {} 247 } 248 249 250 public void testAddSortOrder() { 251 try { 253 instance1.addSortOrder(null); 254 fail("Null step argument, should throw IllegalArgumentException"); 255 } catch (IllegalArgumentException 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 277 public void testSetConstraint() { 278 assertTrue(instance1.getConstraint() == null); 280 281 BasicConstraint constraint = new BasicConstraint(); 282 instance1.setConstraint(constraint); 283 assertTrue(instance1.getConstraint() == constraint); 284 285 instance1.setConstraint(null); 287 assertTrue(instance1.getConstraint() == null); 288 } 289 290 291 public void testIsDistinct() { 292 testSetDistinct(); 294 } 295 296 297 public void testGetSortOrders() { 298 testAddSortOrder(); 300 301 List sortOrders = instance1.getSortOrders(); 302 Object item = sortOrders.get(0); 303 304 try { 306 sortOrders.add(item); 307 fail("Attempt to modify list, must throw UnsupportedOperationException"); 308 } catch (UnsupportedOperationException e) {} 309 try { 310 sortOrders.clear(); 311 fail("Attempt to modify list, must throw UnsupportedOperationException"); 312 } catch (UnsupportedOperationException e) {} 313 } 314 315 316 public void testGetSteps() { 317 testAddStep(); 319 320 List steps = instance1.getSteps(); 321 Object item = steps.get(0); 322 323 try { 325 steps.add(item); 326 fail("Attempt to modify list, must throw UnsupportedOperationException"); 327 } catch (UnsupportedOperationException e) {} 328 try { 329 steps.clear(); 330 fail("Attempt to modify list, must throw UnsupportedOperationException"); 331 } catch (UnsupportedOperationException e) {} 332 } 333 334 335 public void testGetFields() { 336 testAddField(); 338 339 List fields = instance1.getFields(); 340 Object item = fields.get(0); 341 342 try { 344 fields.add(item); 345 fail("Attempt to modify list, must throw UnsupportedOperationException"); 346 } catch (UnsupportedOperationException e) {} 347 try { 348 fields.clear(); 349 fail("Attempt to modify list, must throw UnsupportedOperationException"); 350 } catch (UnsupportedOperationException e) {} 351 } 352 353 354 public void testGetConstraint() { 355 testSetConstraint(); 357 } 358 359 360 public void testGetMaxNumber() { 361 testSetMaxNumber(); 363 } 364 365 366 public void testGetOffset() { 367 testSetOffset(); 369 } 370 371 372 public void testEquals() { 373 } 375 376 377 public void testHashCode() { 378 } 380 381 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 |