1 64 65 package com.jcorporate.expresso.core.dbobj.tests; 66 67 import com.jcorporate.expresso.core.db.DBConnection; 68 import com.jcorporate.expresso.core.db.DBConnectionPool; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.db.TableCreator; 71 import com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException; 72 import com.jcorporate.expresso.core.misc.ConfigManager; 73 import com.jcorporate.expresso.core.misc.ConfigurationException; 74 import com.jcorporate.expresso.core.registry.RequestRegistry; 75 import com.jcorporate.expresso.core.security.User; 76 import com.jcorporate.expresso.services.test.TestSystemInitializer; 77 import junit.framework.Test; 78 import junit.framework.TestCase; 79 import junit.framework.TestSuite; 80 import org.apache.log4j.Logger; 81 82 import java.util.Enumeration ; 83 import java.util.Vector ; 84 85 91 public class RowSecuredDBObjectTest 92 extends TestCase { 93 94 private static Logger log = Logger.getLogger(RowSecuredDBObjectTest.class); 95 96 101 public RowSecuredDBObjectTest(String name) { 102 super(name); 103 } 104 105 106 public static void main(String [] args) 107 throws Exception { 108 109 junit.textui.TestRunner.run(suite()); 111 ConfigManager.destroy(); 112 } 113 114 120 public void setUp() 121 throws Exception { 122 TestSystemInitializer.setUp(); 123 124 try { 125 ConfigManager.getContext(TestSystemInitializer.getTestContext()); 126 } catch (ConfigurationException ce) { 127 fail( 128 "Specified context to test:" + TestSystemInitializer.getTestContext() + " but couldn't find that context"); 129 } 130 131 RowSecuredDBObjectTestSchema ts = new RowSecuredDBObjectTestSchema(); 132 Vector v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext()); 133 134 for (Enumeration ev = v.elements(); ev.hasMoreElements();) { 135 log.info((String ) ev.nextElement()); 136 } 137 138 RequestRegistry.superUser(User.getAdmin(TestSystemInitializer.getTestContext())); 139 } 140 141 142 148 public void tearDown() 149 throws Exception { 150 try { 151 152 153 RowSecuredDBObjectTest1 test1List = new RowSecuredDBObjectTest1(); 154 test1List.setDataContext(TestSystemInitializer.getTestContext()); 155 test1List.deleteAll(); 156 RequestRegistry.revertUser(); 157 158 } catch (DBException de) { 159 de.printStackTrace(); 160 log.error(de); 161 } 162 } 163 164 170 public void testRequestRegistryWithRowSecuredDBObject() throws DBException { 171 RowSecuredDBObjectTest1 test = new RowSecuredDBObjectTest1(); 172 assertEquals(TestSystemInitializer.getTestContext(), 173 test.getDataContext()); 174 assertEquals(User.getAdminId(TestSystemInitializer.getTestContext()), 175 test.getRequestingUid()); 176 } 177 178 179 183 public void testGetSetFields() { 184 try { 185 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 186 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 187 oneTest.setField("TestKey", "1"); 188 189 String value = oneTest.getField("CharTest"); 190 assertTrue("Should be getting blank from value", 191 value == null || value.length() == 0); 192 193 value = oneTest.getField("DecimalTest"); 194 assertTrue("Should be getting blank from value", 195 value == null || value.length() == 0); 196 197 value = oneTest.getField("IntTest"); 198 assertTrue("Should be getting blank from value", 199 value == null || value.length() == 0); 200 201 int testInt = oneTest.getFieldInt("IntTest"); 202 assertTrue("Must get zero for blank field IntTest", testInt == 0); 203 204 boolean testBoolean = oneTest.getFieldBoolean("BooleanTest"); 205 assertTrue("Must get false for blank field BooleanTest", testBoolean == false); 206 207 java.util.Date dt = oneTest.getFieldDate("DateTest"); 208 assertTrue("Should get null date for blank field", dt == null); 209 210 float fl = oneTest.getFieldFloat("FloatTest"); 211 assertTrue("Should get zero back for a blank float field", fl == 0.0); 212 213 value = oneTest.getFieldDecimalFormatted("DecimalTest", "######"); 214 assertTrue("Should get null back for decimal formatted", value == null); 215 216 217 oneTest.setField("TextTest", "abcdefg"); 218 oneTest.setField("BooleanTest", true); 219 oneTest.setField("CharTest", "abcd"); 220 oneTest.setField("DecimalTest", "3"); 221 oneTest.setField("FloatTest", "3.141"); 222 oneTest.setField("IntTest", 123985); 223 oneTest.setField("VarCharTest", "lmnop"); 224 oneTest.setField("DateTest", com.jcorporate.expresso.core.misc 225 .DateTime.getDateForDB()); 226 227 228 value = oneTest.getField("CharTest"); 229 assertTrue("Should be getting abcd from CharTest got" 230 + value + " instead", value.equals("abcd")); 231 232 value = oneTest.getField("DecimalTest"); 233 assertTrue("Should be getting 3 from value got + " + value + " instead", 234 value.equals("3")); 235 236 testInt = oneTest.getFieldInt("IntTest"); 237 assertTrue("Must get 123985 for blank field IntTest got " 238 + testInt + " instead", testInt == 123985); 239 240 testBoolean = oneTest.getFieldBoolean("BooleanTest"); 241 assertTrue("Must get true for set field BooleanTest", testBoolean = true); 242 243 dt = oneTest.getFieldDate("DateTest"); 244 assertTrue("Should not get a null date for set field", dt != null); 245 246 fl = oneTest.getFieldFloat("FloatTest"); 247 248 assertTrue("Should get 3.141 for float value got " + fl + " instead", 249 java.lang.Math.abs(fl - 3.141) < .001); 250 251 } catch (DBException ex) { 252 ex.printStackTrace(); 253 fail("Caught exception testing update support" + ex.getMessage()); 254 } 255 } 256 257 258 262 public void testAdd() { 263 264 try { 265 266 267 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 268 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 269 oneTest.setField("TestKey", "1"); 270 oneTest.setField("DateTest", "1999-10-01"); 271 oneTest.setField("BooleanTest", true); 272 oneTest.setField("CharTest", "abcd"); 273 oneTest.setField("DecimalTest", "3"); 274 oneTest.setField("FloatTest", "3.141"); 275 oneTest.setField("IntTest", 123985); 276 oneTest.setField("VarCharTest", "lmnop"); 277 278 oneTest.add(); 279 oneTest.clear(); 280 } catch (DBException ce) { 281 log.error(ce); 282 fail("DB exception occurred - see log"); 283 } 284 285 try { 286 287 288 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 289 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 290 oneTest.setField("TestKey", "1"); 291 log.warn( 292 "About to throw intentional exception having to do with duplicate key. \nIgnore this dup-key exception."); 293 oneTest.add(); 294 fail("Test add should have thrown exception on duplicate key here"); 295 } catch (DBException ce) { 296 297 298 } 299 300 try { 301 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 302 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 303 oneTest.setField("TestKey", "1"); 304 oneTest.retrieve(); 305 } catch (DBException ce) { 306 log.error(ce); 307 fail("DB exception occurred on retrieve test - see log"); 308 } 309 try { 310 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 311 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 312 oneTest.setField("TestKey", "1"); 313 314 if (!oneTest.find()) { 315 fail("DB exception occurred on find test - see log"); 316 } 317 } catch (DBException ce) { 318 ce.printStackTrace(); 319 log.error(ce); 320 fail("DB exception occurred on find test - see log"); 321 } 322 } 323 324 328 public void testCustomWhereClause() { 329 try { 330 331 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 332 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 333 oneTest.setField("TestKey", "1"); 334 oneTest.add(); 335 336 oneTest.clear(); 337 oneTest.setCustomWhereClause("TESTKEY = 1"); 338 boolean found = oneTest.find(); 339 assertTrue("Must find a record", found == true); 340 341 oneTest.clear(); 342 oneTest.setCustomWhereClause("TESTKEY = 1"); 343 int numRecords = oneTest.count(); 344 assertTrue("Must have one record in the database table", numRecords == 1); 345 } catch (DBException ce) { 346 log.error(ce); 347 fail("DB exception occurred - see log"); 348 } 349 350 } 351 352 356 public void testRetrieve() { 357 try { 358 359 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 360 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 361 oneTest.setField("TestKey", "1"); 362 oneTest.add(); 363 364 oneTest.clear(); 365 oneTest.setField("TestKey", "1"); 366 oneTest.retrieve(); 367 assertTrue("Should have proper return value for testkey", 368 "1".equals(oneTest.getField("TestKey"))); 369 370 371 oneTest.clear(); 372 oneTest.setField("TestKey", "232"); 373 try { 374 oneTest.retrieve(); 375 fail("Retrieve for bogus key should have thrown DBRecordNotFoundException"); 376 } catch (DBRecordNotFoundException ex) { 377 } 379 380 oneTest.clear(); 381 try { 382 oneTest.retrieve(); 383 fail("Retrieve with no keys set should have thrown an exception"); 384 } catch (DBException dbe) { 385 } 387 } catch (DBException ce) { 388 log.error(ce); 389 fail("DB exception occurred - see log"); 390 } 391 } 392 393 396 public void testFieldsToRetrieve() { 397 try { 398 399 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 400 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 401 oneTest.setField("TestKey", "1"); 402 oneTest.setField("IntTest", 24); 403 oneTest.setField("FloatTest", "3.141592"); 404 oneTest.add(); 405 406 oneTest.clear(); 407 oneTest.setField("TestKey", "1"); 408 oneTest.setFieldsToRetrieve("TestKey|IntTest"); 409 oneTest.retrieve(); 410 assertTrue("Should have proper return value for testkey", 411 "1".equals(oneTest.getField("TestKey"))); 412 413 assertTrue("Should have retrieved TestInt field:", 414 24 == oneTest.getFieldInt("IntTest")); 415 416 assertTrue("Should not have retrieved FloatTest:", 417 oneTest.getDataField("FloatTest").isNull()); 418 419 oneTest.clear(); 421 oneTest.setField("TestKey", "1"); 422 oneTest.clearFieldsToRetrieve(); 423 oneTest.retrieve(); 424 assertTrue("Should have proper return value for testkey", 425 "1".equals(oneTest.getField("TestKey"))); 426 427 assertTrue("Should have retrieved TestInt field:", 428 24 == oneTest.getFieldInt("IntTest")); 429 430 assertTrue("Should have retrieved FloatTest:", 431 !oneTest.getDataField("FloatTest").isNull()); 432 433 434 } catch (DBException ce) { 435 log.error(ce); 436 fail("DB exception occurred - see log"); 437 } 438 439 } 440 441 446 public void testCLOBSupport() { 447 final String testString = "LONG Character Update Test"; 448 final String testKey = "1"; 449 try { 450 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 451 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 452 oneTest.setField("TestKey", testKey); 453 oneTest.setField("TextTest", testString); 454 oneTest.add(); 455 456 oneTest.clear(); 457 oneTest.setField("TestKey", testKey); 458 oneTest.retrieve(); 459 String fieldData = oneTest.getField("TextTest"); 460 assertTrue("CLOB Field data must not be null", fieldData != null); 461 462 assertTrue("CLOB field data must be equal to the original data. Got " 463 + fieldData + " instead", testString.equals(fieldData)); 464 465 } catch (Throwable t) { 466 t.printStackTrace(); 467 fail("Caught exception testing CLOB Support: " + t.getMessage()); 468 } 469 } 470 471 475 public void testSpecialCharacters() { 476 final String testString = "' \\ abcdefg"; 477 final String testKey = "1"; 478 479 try { 480 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 481 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 482 oneTest.setField("TestKey", testKey); 483 oneTest.setField("VarCharTest", testString); 484 oneTest.setField("CharTest", testString); 485 oneTest.add(); 486 487 oneTest.clear(); 488 oneTest.setField("TestKey", testKey); 489 oneTest.retrieve(); 490 String fieldData = oneTest.getField("VarCharTest"); 491 assertTrue("Varchar Field data must not be null", fieldData != null); 492 493 assertTrue("Varchar field data must be equal to the original data. Got " 494 + fieldData + " instead", testString.equals(fieldData)); 495 fieldData = oneTest.getField("CharTest"); 496 assertTrue("CharTest Field data must not be null", fieldData != null); 497 498 assertTrue("CharTest field data must be equal to the original data. Got " 499 + fieldData + " instead", testString.equals(fieldData)); 500 } catch (Exception ex) { 501 ex.printStackTrace(); 502 fail("Caught exception processing Special Characters " 503 + ex.getMessage()); 504 } 505 506 } 507 508 509 512 public void testTransactions() { 513 try { 514 DBConnectionPool pool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext()); 515 DBConnection oneConnection = pool.getConnection("Test Transaction"); 516 if (!oneConnection.supportsTransactions()) { 517 log.warn("Cannot test transactions because this DB does not support them."); 518 oneConnection.release(); 519 return; 520 } 521 522 523 try { 524 525 RowSecuredDBObjectTest1 test1 = new RowSecuredDBObjectTest1(oneConnection, 527 User.getAdminId(TestSystemInitializer.getTestContext())); 528 test1.setField("TestKey", 1); 529 if (test1.find()) test1.delete(); 530 531 oneConnection.setAutoCommit(false); 535 536 test1 = new RowSecuredDBObjectTest1(oneConnection, 538 User.getAdminId(TestSystemInitializer.getTestContext())); 539 test1.setField("TestKey", 1); 540 test1.setField("TextTest", "Long Test"); 541 test1.add(); 542 543 oneConnection.rollback(); 547 548 assertTrue(test1.count() == 0); 549 550 test1.setField("TestKey", 1); 554 test1.setField("TextTest", "Long Test"); 555 test1.add(); 556 557 oneConnection.commit(); 558 assertTrue(test1.count() == 1); 559 560 test1.clear(); 564 test1.setField("TestKey", 1); 565 test1.retrieve(); 566 567 test1.setField("TextTest", "Some other value"); 568 test1.update(); 569 570 test1.clear(); 571 test1.setField("TestKey", 1); 572 test1.setField("TextTest", "Yet another Value"); 573 test1.update(); 574 575 test1.clear(); 576 test1.setField("TestKey", 1); 577 test1.retrieve(); 578 579 580 } catch (DBException dbe) { 581 try { 582 oneConnection.rollback(); 583 } catch (DBException ex) { 584 log.error("Error rolling back transaction"); 585 } 586 dbe.printStackTrace(); 587 fail("Caught DBException attempting to execute statements"); 588 } finally { 589 pool.release(oneConnection); 590 } 591 592 } catch (Exception ex) { 593 ex.printStackTrace(); 594 fail("Caught exception processing TestTransaction " 595 + ex.getMessage()); 596 } 597 } 598 599 600 603 public void testUpdate() { 604 try { 605 606 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 607 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 608 oneTest.setField("TestKey", "1"); 609 oneTest.add(); 610 611 oneTest.setField("TextTest", "abcdefg"); 612 oneTest.setField("BooleanTest", true); 613 oneTest.setField("CharTest", "abcd"); 614 oneTest.setField("DecimalTest", "3"); 615 oneTest.setField("FloatTest", "3.141"); 616 oneTest.setField("IntTest", 123985); 617 oneTest.setField("VarCharTest", "lmnop"); 618 oneTest.setField("DateTest", "1999-10-01"); 619 oneTest.update(); 620 621 oneTest.clear(); 623 oneTest.setField("TestKey", "1"); 624 if (!oneTest.find()) { 625 fail("Unable to locate record we just updated"); 626 } 627 oneTest.delete(); 628 } catch (DBException ex) { 629 ex.printStackTrace(); 630 fail("Caught exception testing update support" + ex.getMessage()); 631 } 632 633 } 634 635 639 public void testDateAdd() { 640 final String testStringValue = ""; 641 final String testKey = "1"; 642 try { 643 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 644 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 645 oneTest.setField("TestKey", testKey); 646 oneTest.setField("DateTest", testStringValue); 647 oneTest.add(); 648 649 oneTest.clear(); 650 oneTest.setField("TestKey", testKey); 651 oneTest.retrieve(); 652 653 String fieldData = oneTest.getField("DateTest"); 654 assertTrue(fieldData.equals(testStringValue)); 655 656 } catch (Throwable t) { 657 t.printStackTrace(); 658 fail("Caught exception testing CLOB Support: " + t.getMessage()); 659 } 660 } 661 662 public void testDateSetField() { 663 final String testKey = "1"; 664 try { 665 RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1(); 666 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 667 oneTest.setField("TestKey", testKey); 668 oneTest.setField("DateTest", new java.util.Date ()); 669 oneTest.setField("DateTimeTest", new java.util.Date ()); 670 oneTest.setField("TimeTest", new java.util.Date ()); 671 oneTest.add(); 672 673 oneTest.clear(); 674 oneTest.setField("TestKey", testKey); 675 oneTest.retrieve(); 676 677 System.out.println("Date: " + oneTest.getField("DateTest")); 678 System.out.println("DateTime: " + oneTest.getField("DateTimeTest")); 679 System.out.println("Time: " + oneTest.getField("TimeTest")); 680 682 } catch (Throwable t) { 683 t.printStackTrace(); 684 fail("Caught exception testing CLOB Support: " + t.getMessage()); 685 } 686 } 687 688 689 694 public static Test suite() { 695 TestSuite suite = new TestSuite(RowSecuredDBObjectTest.class); 696 697 return suite; 698 } 699 700 } 701 | Popular Tags |