📄 entitytestsuite.java
字号:
try { EntityCondition isLevel1 = new EntityExpr("description", EntityOperator.EQUALS, "node-level #1"); delegator.removeByCondition("TestingNode", isLevel1); } catch(GenericEntityException e) { Debug.logInfo(e.toString(), module); return; } TestCase.fail("Foreign key referential integrity is not observed for remove (DELETE)"); } /* * Tests the .getRelatedOne method and removeAll for removing entities */ public void testRemoveNodeMemberAndTesting() throws Exception { // // Find the testing entities tru the node member and build a list of them // List values = delegator.findAll("TestingNodeMember"); Iterator i = values.iterator(); ArrayList testings = new ArrayList(); while(i.hasNext()) { GenericValue nodeMember = (GenericValue)i.next(); testings.add(nodeMember.getRelatedOne("Testing")); } // and remove the nodeMember afterwards delegator.removeAll(values); values = delegator.findAll("TestingNodeMember"); TestCase.assertTrue("No more Node Member entities", values.size() == 0); delegator.removeAll(testings); values = delegator.findAll("Testing"); TestCase.assertTrue("No more Testing entities", values.size() == 0); } /* * Tests the storeByCondition operation */ public void testStoreByCondition() throws Exception { // change the description of all the level1 nodes EntityCondition isLevel1 = new EntityExpr("description", EntityOperator.EQUALS, "node-level #1"); Map fieldsToSet = UtilMisc.toMap("description", "node-level #1 (updated)"); int n = 0; try { delegator.storeByCondition("TestingNode", fieldsToSet, isLevel1); List updatedNodes = delegator.findByAnd("TestingNode", fieldsToSet); n = updatedNodes.size(); } catch (GenericEntityException e) { TestCase.fail("testStoreByCondition threw an exception"); } TestCase.assertTrue("testStoreByCondition updated nodes > 0", n > 0); } /* * Tests the .removeByCondition method for removing entities directly */ public void testRemoveByCondition() throws Exception { // // remove all the level1 nodes by using a condition on the description field // EntityCondition isLevel1 = new EntityExpr("description", EntityOperator.EQUALS, "node-level #1 (updated)"); int n = 0; try { n = delegator.removeByCondition("TestingNode", isLevel1); } catch (GenericEntityException e) { TestCase.fail("testRemoveByCondition threw an exception"); } TestCase.assertTrue("testRemoveByCondition nodes > 0", n > 0); } /* * Test the .removeByPrimaryKey by using findByCondition and then retrieving the GenericPk from a GenericValue */ public void testRemoveByPK() throws Exception { // // Find all the root nodes, // delete them their primary key // EntityCondition isRoot = new EntityExpr("primaryParentNodeId", EntityOperator.EQUALS, GenericEntity.NULL_FIELD); List rootValues = delegator.findByCondition("TestingNode", isRoot, UtilMisc.toList("testingNodeId"), null); Iterator it = rootValues.iterator(); while(it.hasNext()) { GenericPK pk = ((GenericValue)it.next()).getPrimaryKey(); int del = delegator.removeByPrimaryKey(pk); TestCase.assertEquals("Removing Root by primary key", del, 1); } // no more TestingNode should be in the data base anymore. List testingNodes = delegator.findAll("TestingNode"); TestCase.assertEquals("No more TestingNode after removing the roots", testingNodes.size(), 0); } /* * Tests the .removeAll method only. */ public void testRemoveType() throws Exception { List values = delegator.findAll("TestingType"); delegator.removeAll(values); // now make sure there are no more of these values = delegator.findAll("TestingType"); TestCase.assertEquals("No more TestingTypes after remove all", values.size(), 0); } /* * This test will create a large number of unique items and add them to the delegator at once */ public void testCreateManyAndStoreAtOnce() throws Exception { try { List newValues = new LinkedList(); for (int i = 0; i < TEST_COUNT; i++) { newValues.add(delegator.makeValue("Testing", UtilMisc.toMap("testingId", getTestId("T1-", i)))); } delegator.storeAll(newValues); List newlyCreatedValues = delegator.findAll("Testing", UtilMisc.toList("testingId")); TestCase.assertEquals("Test to create " + TEST_COUNT + " and store all at once", TEST_COUNT, newlyCreatedValues.size()); } catch (GenericEntityException e) { assertTrue("GenericEntityException:" + e.toString(), false); return; } finally { List newlyCreatedValues = delegator.findAll("Testing", UtilMisc.toList("testingId")); delegator.removeAll(newlyCreatedValues); } } /* * This test will create a large number of unique items and add them to the delegator at once */ public void testCreateManyAndStoreOneAtATime() throws Exception { try { for (int i = 0; i < TEST_COUNT; i++){ delegator.create(delegator.makeValue("Testing", UtilMisc.toMap("testingId", getTestId("T2-", i)))); } List newlyCreatedValues = delegator.findAll("Testing", UtilMisc.toList("testingId")); TestCase.assertEquals("Test to create " + TEST_COUNT + " and store one at a time: ", TEST_COUNT, newlyCreatedValues.size()); } catch (GenericEntityException e){ assertTrue("GenericEntityException:" + e.toString(), false); return; } } /* * This test will use the large number of unique items from above and test the EntityListIterator looping through the list */ public void testEntityListIterator() throws Exception { try { EntityListIterator iterator = delegator.findListIteratorByCondition("Testing", new EntityExpr("testingId", EntityOperator.LIKE, "T2-%"), null, null); assertTrue("Test if EntityListIterator was created: ", iterator != null); int i = 0; GenericValue item = (GenericValue) iterator.next(); while (item != null) { assertTrue("Testing if iterated data matches test data (row " + i + "): ", item.getString("testingId").equals(getTestId("T2-", i))); item = (GenericValue) iterator.next(); i++; } assertTrue("Test if EntitlyListIterator iterates exactly " + TEST_COUNT + " times: " , i == TEST_COUNT); iterator.close(); } catch (GenericEntityException e) { assertTrue("GenericEntityException:" + e.toString(), false); return; } finally { List entitiesToRemove = delegator.findByCondition("Testing", new EntityExpr("testingId", EntityOperator.LIKE, "T2-%"), null, null); delegator.removeAll(entitiesToRemove); } } /* * This test will verify transaction rollbacks using TransactionUtil. */ public void testTransactionUtilRollback() throws Exception { try { GenericValue testValue = delegator.makeValue("Testing", UtilMisc.toMap("testingId", "rollback-test")); boolean transBegin = TransactionUtil.begin(); delegator.create(testValue); TransactionUtil.rollback(transBegin, null, null); GenericValue testValueOut = delegator.findByPrimaryKey("Testing", UtilMisc.toMap("testingId", "rollback-test")); assertEquals("Test that transaction rollback removes value: ", testValueOut, null); } catch (GenericEntityException e) { assertTrue("GenericEntityException:" + e.toString(), false); return; } } /* * This test will verify that a transaction which takes longer than the pre-set timeout are rolled back. */ public void testTransactionUtilMoreThanTimeout() throws Exception { try { GenericValue testValue = delegator.makeValue("Testing", UtilMisc.toMap("testingId", "timeout-test")); boolean transBegin = TransactionUtil.begin(10); // timeout set to 10 seconds delegator.create(testValue); Thread.sleep(20*1000); TransactionUtil.commit(transBegin); assertTrue(false); } catch (GenericTransactionException e) { assertTrue(true); } catch (GenericEntityException e) { assertTrue("Other GenericEntityException encountered:" + e.toString(), false); return; } finally { delegator.removeByAnd("Testing", UtilMisc.toMap("testingId", "timeout-test")); } } /* * This test will verify that the same transaction transaction which takes less time than timeout will be committed. */ public void testTransactionUtilLessThanTimeout() throws Exception { try { GenericValue testValue = delegator.makeValue("Testing", UtilMisc.toMap("testingId", "timeout-test")); boolean transBegin = TransactionUtil.begin(); TransactionUtil.setTransactionTimeout(20); // now set timeout to 20 seconds delegator.create(testValue); Thread.sleep(10*1000); TransactionUtil.commit(transBegin); assertTrue(true); } catch (GenericTransactionException e) { assertTrue("Transaction error when testing transaction less than timeout " + e.toString(), false); } catch (GenericEntityException e) { assertTrue("Other GenericEntityException encountered:" + e.toString(), false); return; } finally { delegator.removeByAnd("Testing", UtilMisc.toMap("testingId", "timeout-test")); } } /* * This will test setting a blob field to null by creating a TestBlob entity whose blob field is not set */ public void testSetNullBlob() throws Exception { try { delegator.create("TestBlob", UtilMisc.toMap("testBlobId", "null-blob")); } catch (GenericEntityException ex) { assertTrue("GenericEntityException:" + ex.toString(), false); return; } finally { List allTestBlobs = delegator.findAll("TestBlob"); delegator.removeAll(allTestBlobs); } } /* * Tests setting a byte value into a blob data type using the GenericValue .setBytes method */ public void testBlobCreate() throws Exception { try { byte[] b = new byte[1]; b[0] = (byte)0x01; GenericValue testingBlob = delegator.makeValue("TestBlob", UtilMisc.toMap("testBlobId", "byte-blob")); testingBlob.setBytes("testBlobField", b); testingBlob.create(); TestCase.assertTrue("Blob with byte value successfully created...", true); } catch(Exception ex) { TestCase.fail(ex.getMessage()); } finally { // Remove all our newly inserted values. List values = delegator.findAll("TestBlob"); delegator.removeAll(values); } } /* * This creates an string id from a number */ private String getTestId(String strTestBase, int iNum) { StringBuffer strBufTemp = new StringBuffer(strTestBase); if (iNum < 10000) { strBufTemp.append("0"); } if (iNum < 1000) { strBufTemp.append("0"); } if (iNum < 100) { strBufTemp.append("0"); } if (iNum < 10) { strBufTemp.append("0"); } strBufTemp.append(iNum); return strBufTemp.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -