1 package org.apache.torque.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import java.util.Calendar; 58 import java.util.Date; 59 import java.util.List; 60 61 import org.apache.commons.configuration.BaseConfiguration; 62 import org.apache.commons.configuration.Configuration; 63 64 import org.apache.torque.BaseTestCase; 65 import org.apache.torque.TorqueException; 66 import org.apache.torque.adapter.DBFactory; 67 68 /*** 69 * Test class for Criteria. 70 * 71 * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a> 72 * @author <a href="mailto:sam@neurogrid.com">Sam Joseph</a> 73 * @version $Id: CriteriaTest.java,v 1.20 2003/07/18 20:11:48 mpoeschl Exp $ 74 */ 75 public class CriteriaTest extends BaseTestCase 76 { 77 78 /*** The criteria to use in the test. */ 79 private Criteria c; 80 81 /*** 82 * Creates a new instance. 83 * 84 * @param name the name of the test to run 85 */ 86 public CriteriaTest(String name) 87 { 88 super(name); 89 } 90 91 /*** 92 * Initializes the criteria. 93 */ 94 public void setUp() 95 { 96 super.setUp(); 97 c = new Criteria(); 98 } 99 100 /*** 101 * Test basic adding of strings. 102 */ 103 public void testAddString() 104 { 105 final String table = "myTable"; 106 final String column = "myColumn"; 107 final String value = "myValue"; 108 109 // Add the string 110 c.add(table, column, (Object) value); 111 112 // Verify that the key exists 113 assertTrue(c.containsKey(table, column)); 114 115 // Verify that what we get out is what we put in 116 assertTrue(c.getString(table, column).equals(value)); 117 } 118 119 /*** 120 * test various properties of Criterion and nested criterion 121 */ 122 public void testNestedCriterion() 123 { 124 final String table2 = "myTable2"; 125 final String column2 = "myColumn2"; 126 final String value2 = "myValue2"; 127 128 final String table3 = "myTable3"; 129 final String column3 = "myColumn3"; 130 final String value3 = "myValue3"; 131 132 final String table4 = "myTable4"; 133 final String column4 = "myColumn4"; 134 final String value4 = "myValue4"; 135 136 final String table5 = "myTable5"; 137 final String column5 = "myColumn5"; 138 final String value5 = "myValue5"; 139 140 Criteria.Criterion crit2 = 141 c.getNewCriterion(table2, column2, (Object) value2, Criteria.EQUAL); 142 Criteria.Criterion crit3 = 143 c.getNewCriterion(table3, column3, (Object) value3, Criteria.EQUAL); 144 Criteria.Criterion crit4 = 145 c.getNewCriterion(table4, column4, (Object) value4, Criteria.EQUAL); 146 Criteria.Criterion crit5 = 147 c.getNewCriterion(table5, column5, (Object) value5, Criteria.EQUAL); 148 149 crit2.and(crit3).or(crit4.and(crit5)); 150 String expect = 151 "((myTable2.myColumn2='myValue2' " 152 + "AND myTable3.myColumn3='myValue3') " 153 + "OR (myTable4.myColumn4='myValue4' " 154 + "AND myTable5.myColumn5='myValue5'))"; 155 String result = crit2.toString(); 156 assertEquals(expect, result); 157 158 Criteria.Criterion crit6 = 159 c.getNewCriterion(table2, column2, (Object) value2, Criteria.EQUAL); 160 Criteria.Criterion crit7 = 161 c.getNewCriterion(table3, column3, (Object) value3, Criteria.EQUAL); 162 Criteria.Criterion crit8 = 163 c.getNewCriterion(table4, column4, (Object) value4, Criteria.EQUAL); 164 Criteria.Criterion crit9 = 165 c.getNewCriterion(table5, column5, (Object) value5, Criteria.EQUAL); 166 167 crit6.and(crit7).or(crit8).and(crit9); 168 expect = 169 "(((myTable2.myColumn2='myValue2' " 170 + "AND myTable3.myColumn3='myValue3') " 171 + "OR myTable4.myColumn4='myValue4') " 172 + "AND myTable5.myColumn5='myValue5')"; 173 result = crit6.toString(); 174 assertEquals(expect, result); 175 176 // should make sure we have tests for all possibilities 177 178 Criteria.Criterion[] crita = crit2.getAttachedCriterion(); 179 180 assertEquals(crit2, crita[0]); 181 assertEquals(crit3, crita[1]); 182 assertEquals(crit4, crita[2]); 183 assertEquals(crit5, crita[3]); 184 185 List tables = crit2.getAllTables(); 186 187 assertEquals(crit2.getTable(), tables.get(0)); 188 assertEquals(crit3.getTable(), tables.get(1)); 189 assertEquals(crit4.getTable(), tables.get(2)); 190 assertEquals(crit5.getTable(), tables.get(3)); 191 192 // simple confirmations that equality operations work 193 assertTrue(crit2.hashCode() == crit2.hashCode()); 194 assertEquals(crit2.toString(), crit2.toString()); 195 } 196 197 /*** 198 * Tests <= and =>. 199 */ 200 public void testBetweenCriterion() 201 { 202 Criteria.Criterion cn1 = 203 c.getNewCriterion( 204 "INVOICE.COST", 205 new Integer(1000), 206 Criteria.GREATER_EQUAL); 207 Criteria.Criterion cn2 = 208 c.getNewCriterion( 209 "INVOICE.COST", 210 new Integer(5000), 211 Criteria.LESS_EQUAL); 212 c.add(cn1.and(cn2)); 213 String expect = 214 "SELECT FROM INVOICE WHERE " 215 + "(INVOICE.COST>=1000 AND INVOICE.COST<=5000)"; 216 String result = null; 217 try 218 { 219 result = BasePeer.createQueryString(c); 220 } 221 catch (TorqueException e) 222 { 223 fail("TorqueException thrown in BasePeer.createQueryString()"); 224 } 225 226 assertEquals(expect, result); 227 } 228 229 /*** 230 * Verify that AND and OR criterion are nested correctly. 231 */ 232 public void testPrecedence() 233 { 234 Criteria.Criterion cn1 = 235 c.getNewCriterion("INVOICE.COST", "1000", Criteria.GREATER_EQUAL); 236 Criteria.Criterion cn2 = 237 c.getNewCriterion("INVOICE.COST", "2000", Criteria.LESS_EQUAL); 238 Criteria.Criterion cn3 = 239 c.getNewCriterion("INVOICE.COST", "8000", Criteria.GREATER_EQUAL); 240 Criteria.Criterion cn4 = 241 c.getNewCriterion("INVOICE.COST", "9000", Criteria.LESS_EQUAL); 242 c.add(cn1.and(cn2)); 243 c.or(cn3.and(cn4)); 244 245 String expect = 246 "SELECT FROM INVOICE WHERE " 247 + "((INVOICE.COST>='1000' AND INVOICE.COST<='2000') " 248 + "OR (INVOICE.COST>='8000' AND INVOICE.COST<='9000'))"; 249 250 String result = null; 251 try 252 { 253 result = BasePeer.createQueryString(c); 254 } 255 catch (TorqueException e) 256 { 257 fail("TorqueException thrown in BasePeer.createQueryString()"); 258 } 259 260 assertEquals(expect, result); 261 } 262 263 /*** 264 * Test Criterion.setIgnoreCase(). 265 * As the output is db specific the test just prints the result to 266 * System.out 267 */ 268 public void testCriterionIgnoreCase() 269 { 270 Criteria myCriteria = new Criteria(); 271 272 Criteria.Criterion expected = myCriteria.getNewCriterion( 273 "TABLE.COLUMN", (Object)"FoObAr", Criteria.LIKE); 274 Criteria.Criterion result = expected.setIgnoreCase(true); 275 assertEquals("Criterion mis-match after calling setIgnoreCase(true)", 276 expected.toString(), result.toString()); 277 } 278 279 /*** 280 * Test that true is evaluated correctly. 281 */ 282 public void testBoolean() 283 { 284 Criteria c = new Criteria().add("TABLE.COLUMN", true); 285 286 String expect = "SELECT FROM TABLE WHERE TABLE.COLUMN=1"; 287 288 String result = null; 289 try 290 { 291 result = BasePeer.createQueryString(c); 292 } 293 catch (TorqueException e) 294 { 295 fail("TorqueException thrown in BasePeer.createQueryString()"); 296 } 297 298 assertEquals(expect, result); 299 300 // test the postgresql variation 301 c = new Criteria(); 302 Criteria.Criterion cc = 303 c.getNewCriterion("TABLE.COLUMN", Boolean.TRUE, Criteria.EQUAL); 304 305 Configuration conf = new BaseConfiguration(); 306 conf.addProperty("driver", "org.postgresql.Driver"); 307 try 308 { 309 cc.setDB(DBFactory.create("org.postgresql.Driver")); 310 } 311 catch (Exception e) 312 { 313 fail("Exception thrown in DBFactory"); 314 } 315 316 assertEquals("TABLE.COLUMN=1", cc.toString()); 317 } 318 319 /*** 320 * testcase for addDate() 321 */ 322 public void testAddDate() 323 { 324 Criteria c = new Criteria(); 325 c.addDate("TABLE.DATE_COLUMN", 2003, 0, 22); 326 327 String expect = "SELECT FROM TABLE WHERE TABLE.DATE_COLUMN='20030122000000'"; 328 329 String result = null; 330 try 331 { 332 result = BasePeer.createQueryString(c); 333 } 334 catch (TorqueException e) 335 { 336 e.printStackTrace(); 337 fail("TorqueException thrown in BasePeer.createQueryString()"); 338 } 339 assertEquals(expect, result); 340 } 341 342 /*** 343 * testcase for add(Date) 344 */ 345 public void testDateAdd() 346 { 347 Calendar cal = Calendar.getInstance(); 348 cal.set(2003, 0, 22, 0, 0, 0); 349 Date date = cal.getTime(); 350 Criteria c = new Criteria(); 351 c.add("TABLE.DATE_COLUMN", date); 352 353 String expect = "SELECT FROM TABLE WHERE TABLE.DATE_COLUMN='20030122000000'"; 354 355 String result = null; 356 try 357 { 358 result = BasePeer.createQueryString(c); 359 } 360 catch (TorqueException e) 361 { 362 e.printStackTrace(); 363 fail("TorqueException thrown in BasePeer.createQueryString()"); 364 } 365 assertEquals(expect, result); 366 } 367 368 public void testCurrentDate() 369 { 370 Criteria c = new Criteria() 371 .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE) 372 .add("TABLE.TIME_COLUMN", Criteria.CURRENT_TIME); 373 374 String expect = "SELECT FROM TABLE WHERE TABLE.TIME_COLUMN=CURRENT_TIME AND TABLE.DATE_COLUMN=CURRENT_DATE"; 375 376 String result = null; 377 try 378 { 379 result = BasePeer.createQueryString(c); 380 } 381 catch (TorqueException e) 382 { 383 e.printStackTrace(); 384 fail("TorqueException thrown in BasePeer.createQueryString()"); 385 } 386 387 assertEquals(expect,result); 388 } 389 390 public void testCountAster() 391 { 392 Criteria c = new Criteria() 393 .addSelectColumn("COUNT(*)") 394 .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE) 395 .add("TABLE.TIME_COLUMN", Criteria.CURRENT_TIME); 396 397 String expect = "SELECT COUNT(*) FROM TABLE WHERE TABLE.TIME_COLUMN=CURRENT_TIME AND TABLE.DATE_COLUMN=CURRENT_DATE"; 398 399 String result = null; 400 try 401 { 402 result = BasePeer.createQueryString(c); 403 } 404 catch (TorqueException e) 405 { 406 e.printStackTrace(); 407 fail("TorqueException thrown in BasePeer.createQueryString()"); 408 } 409 410 assertEquals(expect,result); 411 412 } 413 414 /*** 415 * This test case has been written to try out the fix applied to resolve 416 * TRQS73 - i.e. ensuring that Criteria.toString() does not alter any limit 417 * or offset that may be stored in the Criteria object. This testcase 418 * could actually pass without the fix if the database in use does not 419 * support native limits and offsets. 420 */ 421 public void testCriteriaToStringOffset() 422 { 423 Criteria c = new Criteria() 424 .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE) 425 .setOffset(3) 426 .setLimit(5); 427 428 String toStringExpect = "Criteria:: TABLE.DATE_COLUMN<=>TABLE.DATE_COLUMN=CURRENT_DATE: " 429 + "\nCurrent Query SQL (may not be complete or applicable): " 430 + "SELECT FROM TABLE WHERE TABLE.DATE_COLUMN=CURRENT_DATE LIMIT 3, 5"; 431 432 String cString = c.toString(); 433 //System.out.println(cString); 434 assertEquals(cString, toStringExpect); 435 436 // Note that this is intentially the same as above as the behaviour is 437 // only observed on subsequent invocations of toString(). 438 cString = c.toString(); 439 //System.out.println(cString); 440 assertEquals(cString, toStringExpect); 441 } 442 443 /*** 444 * This test case has been written to try out the fix applied to resolve 445 * TRQS73 - i.e. ensuring that Criteria.toString() does not alter any limit 446 * or offset that may be stored in the Criteria object. This testcase 447 * could actually pass without the fix if the database in use does not 448 * support native limits and offsets. 449 */ 450 public void testCriteriaToStringLimit() 451 { 452 Criteria c = new Criteria() 453 .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE) 454 .setLimit(5); 455 456 String toStringExpect = "Criteria:: TABLE.DATE_COLUMN<=>TABLE.DATE_COLUMN=CURRENT_DATE: " 457 + "\nCurrent Query SQL (may not be complete or applicable): " 458 + "SELECT FROM TABLE WHERE TABLE.DATE_COLUMN=CURRENT_DATE LIMIT 5"; 459 460 String cString = c.toString(); 461 //System.out.println(cString); 462 assertEquals(cString, toStringExpect); 463 464 // Note that this is intentially the same as above as the behaviour is 465 // only observed on subsequent invocations of toString(). 466 cString = c.toString(); 467 //System.out.println(cString); 468 assertEquals(cString, toStringExpect); 469 } 470 471 /*** 472 * test for TRQS25 473 */ 474 /* 475 public void testCriteriaAndString() 476 { 477 Criteria c = new Criteria() 478 .add("TABLE.COLUMN1", "string") 479 .and("TABLE.COLUMN2", "string", Criteria.LIKE); 480 } 481 */ 482 }

This page was automatically generated by Maven