1 25 package org.ofbiz.entity.test; 26 27 import java.sql.Timestamp ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.lang.Thread ; 35 36 import junit.framework.TestCase; 37 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.base.util.UtilDateTime; 40 import org.ofbiz.base.util.UtilMisc; 41 import org.ofbiz.entity.GenericDelegator; 42 import org.ofbiz.entity.GenericEntity; 43 import org.ofbiz.entity.GenericEntityException; 44 import org.ofbiz.entity.GenericPK; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.entity.condition.EntityCondition; 47 import org.ofbiz.entity.condition.EntityConditionList; 48 import org.ofbiz.entity.condition.EntityExpr; 49 import org.ofbiz.entity.condition.EntityOperator; 50 import org.ofbiz.entity.util.EntityFindOptions; 51 import org.ofbiz.entity.util.EntityListIterator; 52 import org.ofbiz.entity.transaction.GenericTransactionException; 53 import org.ofbiz.entity.transaction.TransactionUtil; 54 55 63 public class EntityTestSuite extends TestCase { 64 65 public static final String module = EntityTestSuite.class.getName(); 66 public static final String DELEGATOR_NAME = "test"; 67 public GenericDelegator delegator = null; 68 72 public static final long TEST_COUNT = 10000; 73 74 public EntityTestSuite(String name) { 75 super(name); 76 } 77 78 final static private int _level1max = 3; 80 protected void setUp() throws Exception { 81 this.delegator = GenericDelegator.getGenericDelegator(DELEGATOR_NAME); 82 } 83 84 87 public void testMakeValue() throws Exception { 88 try { 89 delegator.create("TestingType", UtilMisc.toMap("testingTypeId", "TEST-1", "description", "Testing Type #1")); 91 92 List newValues = new LinkedList (); 94 95 newValues.add(delegator.makeValue("TestingType", UtilMisc.toMap("testingTypeId", "TEST-2", "description", "Testing Type #2"))); 96 newValues.add(delegator.makeValue("TestingType", UtilMisc.toMap("testingTypeId", "TEST-3", "description", "Testing Type #3"))); 97 newValues.add(delegator.makeValue("TestingType", UtilMisc.toMap("testingTypeId", "TEST-4", "description", "Testing Type #4"))); 98 delegator.storeAll(newValues); 99 100 List newlyCreatedValues = delegator.findAll("TestingType", UtilMisc.toList("testingTypeId")); 102 TestCase.assertEquals("4 TestingTypes found", 4, newlyCreatedValues.size()); 103 } catch (GenericEntityException ex) { 104 TestCase.fail(ex.getMessage()); 105 } 106 } 107 108 111 public void testUpdateValue() throws Exception { 112 try { 113 114 GenericValue testValue = delegator.findByPrimaryKey("TestingType", UtilMisc.toMap("testingTypeId", "TEST-1")); 116 TestCase.assertEquals("Retrieved value has the correct description", testValue.getString("description"), "Testing Type #1"); 117 118 testValue.put("description", "New Testing Type #1"); 120 testValue.store(); 121 122 testValue = delegator.findByPrimaryKey("TestingType", UtilMisc.toMap("testingTypeId", "TEST-1")); 124 TestCase.assertEquals("Retrieved value has the correct description", testValue.getString("description"), "New Testing Type #1"); 125 126 } catch (GenericEntityException ex) { 127 TestCase.fail(ex.getMessage()); 128 } 129 } 130 131 134 public void testCreateTree() throws Exception { 135 try { 136 EntityCondition isChild = new EntityExpr("primaryParentNodeId", EntityOperator.NOT_EQUAL, GenericEntity.NULL_FIELD); 138 long alreadyStored = delegator.findCountByCondition("TestingNode", isChild, null); 139 140 144 GenericValue root = delegator.create("TestingNode", 146 UtilMisc.toMap( 147 "testingNodeId", delegator.getNextSeqId("testingNodeId"), 148 "primaryParentNodeId", GenericEntity.NULL_FIELD, 149 "description", "root") 150 ); 151 int level1; 152 for(level1 = 0; level1 < _level1max; level1++) { 153 String nextSeqId = delegator.getNextSeqId("testingNodeId"); 154 GenericValue v = 155 delegator.create("TestingNode", 156 UtilMisc.toMap("testingNodeId", nextSeqId, 157 "primaryParentNodeId", (String )root.get("testingNodeId"), 158 "description", "node-level #1") 159 ); 160 } 161 162 long created = level1; 163 long newlyStored = delegator.findCountByCondition("TestingNode", isChild, null); 164 165 TestCase.assertEquals("Created/Stored Nodes", newlyStored, created + alreadyStored); 167 } catch(GenericEntityException e) { 168 Debug.logInfo(e.getMessage(), module); 169 } 170 } 171 172 175 public void testAddMembersToTree() throws Exception { 176 EntityCondition isLevel1 = new EntityExpr("primaryParentNodeId", EntityOperator.NOT_EQUAL, GenericEntity.NULL_FIELD); 178 List nodeLevel1 = delegator.findByCondition("TestingNode", isLevel1, null, null); 179 180 List newValues = new LinkedList (); 181 Timestamp now = UtilDateTime.nowTimestamp(); 182 183 Iterator nodeIterator = nodeLevel1.iterator(); 184 while(nodeIterator.hasNext()) { 185 GenericValue node = (GenericValue)nodeIterator.next(); 186 GenericValue testing = delegator.makeValue("Testing", 187 UtilMisc.toMap( 188 "testingId", delegator.getNextSeqId("testing"), 189 "testingTypeId", "TEST-1" 190 ) 191 ); 192 testing.put("testingName", "leaf-#" + node.getString("testingNodeId")); 193 testing.put("description", "level1 leaf"); 194 testing.put("comments", "No-comments"); 195 testing.put("testingSize", new Long (10)); 196 testing.put("testingDate", now); 197 198 newValues.add(testing); 199 GenericValue member = delegator.makeValue("TestingNodeMember", 200 UtilMisc.toMap( 201 "testingNodeId", node.get("testingNodeId"), 202 "testingId", testing.get("testingId") 203 ) 204 ); 205 206 member.put("fromDate", now); 207 member.put("thruDate", UtilDateTime.getNextDayStart(now)); 208 209 newValues.add(member); 210 } 211 int n = delegator.storeAll(newValues); 212 TestCase.assertEquals("Created/Stored Nodes", n, newValues.size()); 213 } 214 215 218 public void testCountViews() throws Exception { 219 EntityCondition isNodeWithMember = new EntityExpr("testingId", EntityOperator.NOT_EQUAL, GenericEntity.NULL_FIELD); 220 List nodeWithMembers = delegator.findByCondition("TestingNodeAndMember", isNodeWithMember, null, null); 221 222 Iterator it; 223 it = nodeWithMembers.iterator(); 224 225 while(it.hasNext()) { 226 GenericValue v = (GenericValue)it.next(); 227 Map fields = v.getAllFields(); 228 Debug.logInfo("--------------------------", module); 229 for(Iterator it1 = fields.keySet().iterator(); it1.hasNext(); ) { 231 Object field = it1.next(); 232 Object value = fields.get(field); 233 Debug.logInfo(field.toString() + " = " + ((value == null) ? "[null]" : value.toString()), module); 234 } 235 } 236 long testingcount = delegator.findCountByCondition("Testing", null, null); 237 TestCase.assertEquals("Number of views should equal number of created entities in the test.", nodeWithMembers.size(), testingcount); 238 } 239 240 243 public void testFindDistinct() throws Exception { 244 List exprList = UtilMisc.toList( 245 new EntityExpr("testingSize", EntityOperator.EQUALS, new Long (10)), 246 new EntityExpr("comments", EntityOperator.EQUALS, "No-comments") 247 ); 248 EntityConditionList condition = new EntityConditionList(exprList, EntityOperator.AND); 249 250 EntityFindOptions findOptions = new EntityFindOptions(); 251 findOptions.setDistinct(true); 252 253 List testingSize10 = delegator.findByCondition("Testing", condition, null, UtilMisc.toList("testingSize", "comments"), null, findOptions); 254 Debug.logInfo("testingSize10 is " + testingSize10.size(), module); 255 256 TestCase.assertEquals("There should only be 1 result found by findDistinct()", testingSize10.size(), 1); 257 } 258 259 262 public void testNotLike() throws Exception { 263 EntityCondition cond = new EntityExpr("description", EntityOperator.NOT_LIKE, "root%"); 264 List nodes = delegator.findByCondition("TestingNode", cond, null, null); 265 TestCase.assertTrue("Found nodes", nodes != null); 266 267 Iterator i = nodes.iterator(); 268 while (i.hasNext()) { 269 GenericValue product = (GenericValue) i.next(); 270 String nodeId = product.getString("description"); 271 Debug.logInfo("Testing name - " + nodeId, module); 272 TestCase.assertTrue("No nodes starting w/ root", !nodeId.startsWith("root")); 273 } 274 } 275 276 279 public void testForeignKeyCreate() throws Exception { 280 try { 281 delegator.create("Testing", UtilMisc.toMap("testingId", delegator.getNextSeqId("Testing"), "testingTypeId", "NO-SUCH-KEY")); 282 } catch(GenericEntityException e) { 283 Debug.logInfo(e.toString(), module); 284 return; 285 } 286 TestCase.fail("Foreign key referential integrity is not observed for create (INSERT)"); 287 } 288 289 292 public void testForeignKeyRemove() throws Exception { 293 try { 294 EntityCondition isLevel1 = new EntityExpr("description", EntityOperator.EQUALS, "node-level #1"); 295 delegator.removeByCondition("TestingNode", isLevel1); 296 } catch(GenericEntityException e) { 297 Debug.logInfo(e.toString(), module); 298 return; 299 } 300 TestCase.fail("Foreign key referential integrity is not observed for remove (DELETE)"); 301 } 302 303 306 public void testRemoveNodeMemberAndTesting() throws Exception { 307 List values = delegator.findAll("TestingNodeMember"); 311 Iterator i = values.iterator(); 312 313 ArrayList testings = new ArrayList (); 314 315 while(i.hasNext()) { 316 GenericValue nodeMember = (GenericValue)i.next(); 317 testings.add(nodeMember.getRelatedOne("Testing")); 318 } 319 delegator.removeAll(values); 321 values = delegator.findAll("TestingNodeMember"); 322 TestCase.assertTrue("No more Node Member entities", values.size() == 0); 323 324 delegator.removeAll(testings); 325 values = delegator.findAll("Testing"); 326 TestCase.assertTrue("No more Testing entities", values.size() == 0); 327 } 328 329 332 public void testStoreByCondition() throws Exception { 333 EntityCondition isLevel1 = new EntityExpr("description", EntityOperator.EQUALS, "node-level #1"); 335 Map fieldsToSet = UtilMisc.toMap("description", "node-level #1 (updated)"); 336 int n = 0; 337 try { 338 delegator.storeByCondition("TestingNode", fieldsToSet, isLevel1); 339 List updatedNodes = delegator.findByAnd("TestingNode", fieldsToSet); 340 n = updatedNodes.size(); 341 } catch (GenericEntityException e) { 342 TestCase.fail("testStoreByCondition threw an exception"); 343 } 344 345 TestCase.assertTrue("testStoreByCondition updated nodes > 0", n > 0); 346 } 347 348 351 public void testRemoveByCondition() throws Exception { 352 EntityCondition isLevel1 = new EntityExpr("description", EntityOperator.EQUALS, "node-level #1 (updated)"); 356 int n = 0; 357 358 try { 359 n = delegator.removeByCondition("TestingNode", isLevel1); 360 } catch (GenericEntityException e) { 361 TestCase.fail("testRemoveByCondition threw an exception"); 362 } 363 364 TestCase.assertTrue("testRemoveByCondition nodes > 0", n > 0); 365 } 366 367 370 public void testRemoveByPK() throws Exception { 371 EntityCondition isRoot = new EntityExpr("primaryParentNodeId", EntityOperator.EQUALS, GenericEntity.NULL_FIELD); 376 List rootValues = delegator.findByCondition("TestingNode", isRoot, UtilMisc.toList("testingNodeId"), null); 377 378 Iterator it = rootValues.iterator(); 379 while(it.hasNext()) { 380 GenericPK pk = ((GenericValue)it.next()).getPrimaryKey(); 381 int del = delegator.removeByPrimaryKey(pk); 382 TestCase.assertEquals("Removing Root by primary key", del, 1); 383 } 384 385 387 List testingNodes = delegator.findAll("TestingNode"); 388 TestCase.assertEquals("No more TestingNode after removing the roots", testingNodes.size(), 0); 389 } 390 391 394 public void testRemoveType() throws Exception { 395 List values = delegator.findAll("TestingType"); 396 delegator.removeAll(values); 397 398 values = delegator.findAll("TestingType"); 400 TestCase.assertEquals("No more TestingTypes after remove all", values.size(), 0); 401 } 402 403 406 public void testCreateManyAndStoreAtOnce() throws Exception { 407 try { 408 List newValues = new LinkedList (); 409 for (int i = 0; i < TEST_COUNT; i++) { 410 newValues.add(delegator.makeValue("Testing", UtilMisc.toMap("testingId", getTestId("T1-", i)))); 411 } 412 delegator.storeAll(newValues); 413 List newlyCreatedValues = delegator.findAll("Testing", UtilMisc.toList("testingId")); 414 TestCase.assertEquals("Test to create " + TEST_COUNT + " and store all at once", TEST_COUNT, newlyCreatedValues.size()); 415 } catch (GenericEntityException e) { 416 assertTrue("GenericEntityException:" + e.toString(), false); 417 return; 418 } finally { 419 List newlyCreatedValues = delegator.findAll("Testing", UtilMisc.toList("testingId")); 420 delegator.removeAll(newlyCreatedValues); 421 } 422 } 423 424 427 public void testCreateManyAndStoreOneAtATime() throws Exception { 428 try { 429 for (int i = 0; i < TEST_COUNT; i++){ 430 delegator.create(delegator.makeValue("Testing", UtilMisc.toMap("testingId", getTestId("T2-", i)))); 431 } 432 List newlyCreatedValues = delegator.findAll("Testing", UtilMisc.toList("testingId")); 433 TestCase.assertEquals("Test to create " + TEST_COUNT + " and store one at a time: ", TEST_COUNT, newlyCreatedValues.size()); 434 } catch (GenericEntityException e){ 435 assertTrue("GenericEntityException:" + e.toString(), false); 436 return; 437 } 438 } 439 440 443 public void testEntityListIterator() throws Exception { 444 try { 445 EntityListIterator iterator = delegator.findListIteratorByCondition("Testing", new EntityExpr("testingId", EntityOperator.LIKE, "T2-%"), null, null); 446 assertTrue("Test if EntityListIterator was created: ", iterator != null); 447 448 int i = 0; 449 GenericValue item = (GenericValue) iterator.next(); 450 while (item != null) { 451 assertTrue("Testing if iterated data matches test data (row " + i + "): ", item.getString("testingId").equals(getTestId("T2-", i))); 452 item = (GenericValue) iterator.next(); 453 i++; 454 } 455 assertTrue("Test if EntitlyListIterator iterates exactly " + TEST_COUNT + " times: " , i == TEST_COUNT); 456 iterator.close(); 457 } catch (GenericEntityException e) { 458 assertTrue("GenericEntityException:" + e.toString(), false); 459 return; 460 } finally { 461 List entitiesToRemove = delegator.findByCondition("Testing", new EntityExpr("testingId", EntityOperator.LIKE, "T2-%"), null, null); 462 delegator.removeAll(entitiesToRemove); 463 } 464 } 465 466 469 public void testTransactionUtilRollback() throws Exception { 470 try { 471 GenericValue testValue = delegator.makeValue("Testing", UtilMisc.toMap("testingId", "rollback-test")); 472 boolean transBegin = TransactionUtil.begin(); 473 delegator.create(testValue); 474 TransactionUtil.rollback(transBegin, null, null); 475 GenericValue testValueOut = delegator.findByPrimaryKey("Testing", UtilMisc.toMap("testingId", "rollback-test")); 476 assertEquals("Test that transaction rollback removes value: ", testValueOut, null); 477 } catch (GenericEntityException e) { 478 assertTrue("GenericEntityException:" + e.toString(), false); 479 return; 480 } 481 } 482 483 486 public void testTransactionUtilMoreThanTimeout() throws Exception { 487 try { 488 GenericValue testValue = delegator.makeValue("Testing", UtilMisc.toMap("testingId", "timeout-test")); 489 boolean transBegin = TransactionUtil.begin(10); delegator.create(testValue); 491 Thread.sleep(20*1000); 492 TransactionUtil.commit(transBegin); 493 assertTrue(false); 494 } catch (GenericTransactionException e) { 495 assertTrue(true); 496 } catch (GenericEntityException e) { 497 assertTrue("Other GenericEntityException encountered:" + e.toString(), false); 498 return; 499 } finally { 500 delegator.removeByAnd("Testing", UtilMisc.toMap("testingId", "timeout-test")); 501 } 502 } 503 504 507 public void testTransactionUtilLessThanTimeout() throws Exception { 508 try { 509 GenericValue testValue = delegator.makeValue("Testing", UtilMisc.toMap("testingId", "timeout-test")); 510 boolean transBegin = TransactionUtil.begin(); 511 TransactionUtil.setTransactionTimeout(20); delegator.create(testValue); 513 Thread.sleep(10*1000); 514 TransactionUtil.commit(transBegin); 515 assertTrue(true); 516 } catch (GenericTransactionException e) { 517 assertTrue("Transaction error when testing transaction less than timeout " + e.toString(), false); 518 } catch (GenericEntityException e) { 519 assertTrue("Other GenericEntityException encountered:" + e.toString(), false); 520 return; 521 } finally { 522 delegator.removeByAnd("Testing", UtilMisc.toMap("testingId", "timeout-test")); 523 } 524 } 525 526 529 public void testSetNullBlob() throws Exception { 530 try { 531 delegator.create("TestBlob", UtilMisc.toMap("testBlobId", "null-blob")); 532 } catch (GenericEntityException ex) { 533 assertTrue("GenericEntityException:" + ex.toString(), false); 534 return; 535 } finally { 536 List allTestBlobs = delegator.findAll("TestBlob"); 537 delegator.removeAll(allTestBlobs); 538 } 539 } 540 541 544 public void testBlobCreate() throws Exception { 545 try { 546 byte[] b = new byte[1]; 547 b[0] = (byte)0x01; 548 GenericValue testingBlob = delegator.makeValue("TestBlob", UtilMisc.toMap("testBlobId", "byte-blob")); 549 testingBlob.setBytes("testBlobField", b); 550 testingBlob.create(); 551 552 TestCase.assertTrue("Blob with byte value successfully created...", true); 553 } catch(Exception ex) { 554 TestCase.fail(ex.getMessage()); 555 } finally { 556 List values = delegator.findAll("TestBlob"); 558 delegator.removeAll(values); 559 } 560 } 561 562 565 private String getTestId(String strTestBase, int iNum) { 566 StringBuffer strBufTemp = new StringBuffer (strTestBase); 567 if (iNum < 10000) { 568 strBufTemp.append("0"); 569 } 570 if (iNum < 1000) { 571 strBufTemp.append("0"); 572 } 573 if (iNum < 100) { 574 strBufTemp.append("0"); 575 } 576 if (iNum < 10) { 577 strBufTemp.append("0"); 578 } 579 strBufTemp.append(iNum); 580 return strBufTemp.toString(); 581 } 582 583 } 584 | Popular Tags |