svn commit: r922735 - /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r922735 - /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java

doogie-3
Author: doogie
Date: Sun Mar 14 02:22:10 2010
New Revision: 922735

URL: http://svn.apache.org/viewvc?rev=922735&view=rev
Log:
Follow junit best practices.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java?rev=922735&r1=922734&r2=922735&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java Sun Mar 14 02:22:10 2010
@@ -67,79 +67,64 @@ public class EntityTestSuite extends Ent
      * Tests storing values with the delegator's .create, .makeValue, and .storeAll methods
      */
     public void testMakeValue() throws Exception {
-        try {
-            // This method call directly stores a new value into the entity engine
-            delegator.create("TestingType", "testingTypeId", "TEST-1", "description", "Testing Type #1");
-
-            // This sequence creates the GenericValue entities first, puts them in a List, then calls the delegator to store them all
-            List<GenericValue> newValues = new LinkedList<GenericValue>();
+        // This method call directly stores a new value into the entity engine
+        delegator.create("TestingType", "testingTypeId", "TEST-1", "description", "Testing Type #1");
 
-            newValues.add(delegator.makeValue("TestingType", "testingTypeId", "TEST-2", "description", "Testing Type #2"));
-            newValues.add(delegator.makeValue("TestingType", "testingTypeId", "TEST-3", "description", "Testing Type #3"));
-            newValues.add(delegator.makeValue("TestingType", "testingTypeId", "TEST-4", "description", "Testing Type #4"));
-            delegator.storeAll(newValues);
+        // This sequence creates the GenericValue entities first, puts them in a List, then calls the delegator to store them all
+        List<GenericValue> newValues = new LinkedList<GenericValue>();
 
-            // finds a List of newly created values.  the second parameter specifies the fields to order results by.
-            List<GenericValue> newlyCreatedValues = delegator.findList("TestingType", null, null, UtilMisc.toList("testingTypeId"), null, false);
-            TestCase.assertEquals("4 TestingTypes found", 4, newlyCreatedValues.size());
-        } catch (GenericEntityException ex) {
-            TestCase.fail(ex.getMessage());
-        }
+        newValues.add(delegator.makeValue("TestingType", "testingTypeId", "TEST-2", "description", "Testing Type #2"));
+        newValues.add(delegator.makeValue("TestingType", "testingTypeId", "TEST-3", "description", "Testing Type #3"));
+        newValues.add(delegator.makeValue("TestingType", "testingTypeId", "TEST-4", "description", "Testing Type #4"));
+        delegator.storeAll(newValues);
+
+        // finds a List of newly created values.  the second parameter specifies the fields to order results by.
+        List<GenericValue> newlyCreatedValues = delegator.findList("TestingType", null, null, UtilMisc.toList("testingTypeId"), null, false);
+        assertEquals("4 TestingTypes found", 4, newlyCreatedValues.size());
     }
 
     /*
      * Tests updating entities by doing a GenericValue .put(key, value) and .store()
      */
     public void testUpdateValue() throws Exception {
-        try {
-
-            // retrieve a sample GenericValue, make sure it's correct
-            GenericValue testValue = delegator.findOne("TestingType", false, "testingTypeId", "TEST-1");
-            TestCase.assertEquals("Retrieved value has the correct description", testValue.getString("description"), "Testing Type #1");
-
-            // now update and store it
-            testValue.put("description", "New Testing Type #1");
-            testValue.store();
-
-            // now retrieve it again and make sure that the updated value is correct
-            testValue = delegator.findOne("TestingType", false, "testingTypeId", "TEST-1");
-            TestCase.assertEquals("Retrieved value has the correct description", testValue.getString("description"), "New Testing Type #1");
-
-        } catch (GenericEntityException ex) {
-            TestCase.fail(ex.getMessage());
-        }
+        // retrieve a sample GenericValue, make sure it's correct
+        GenericValue testValue = delegator.findOne("TestingType", false, "testingTypeId", "TEST-1");
+        assertEquals("Retrieved value has the correct description", "Testing Type #1", testValue.getString("description"));
+
+        // now update and store it
+        testValue.put("description", "New Testing Type #1");
+        testValue.store();
+
+        // now retrieve it again and make sure that the updated value is correct
+        testValue = delegator.findOne("TestingType", false, "testingTypeId", "TEST-1");
+        assertEquals("Retrieved value has the correct description", "New Testing Type #1", testValue.getString("description"));
     }
 
     /*
      * Tests XML serialization by serializing/deserializing a GenericValue
      */
     public void testXmlSerialization() throws Exception {
-        try {
-            // Must use the default delegator because the deserialized GenericValue can't
-            // find the randomized one.
-            Delegator localDelegator = DelegatorFactory.getDelegator("default");
-            boolean transBegin = TransactionUtil.begin();
-            localDelegator.create("TestingType", "testingTypeId", "TEST-5", "description", "Testing Type #5");
-            GenericValue testValue = localDelegator.findOne("TestingType", false, "testingTypeId", "TEST-5");
-            TestCase.assertEquals("Retrieved value has the correct description", testValue.getString("description"), "Testing Type #5");
-            String newValueStr = UtilXml.toXml(testValue);
-            GenericValue newValue = (GenericValue) UtilXml.fromXml(newValueStr);
-            TestCase.assertEquals("Retrieved value has the correct description", newValue.getString("description"), "Testing Type #5");
-            newValue.put("description", "XML Testing Type #5");
-            newValue.store();
-            newValue = localDelegator.findOne("TestingType", false, "testingTypeId", "TEST-5");
-            TestCase.assertEquals("Retrieved value has the correct description", newValue.getString("description"), "XML Testing Type #5");
-            TransactionUtil.rollback(transBegin, null, null);
-        } catch (GenericEntityException ex) {
-            TestCase.fail(ex.getMessage());
-        }
+        // Must use the default delegator because the deserialized GenericValue can't
+        // find the randomized one.
+        Delegator localDelegator = DelegatorFactory.getDelegator("default");
+        boolean transBegin = TransactionUtil.begin();
+        localDelegator.create("TestingType", "testingTypeId", "TEST-5", "description", "Testing Type #5");
+        GenericValue testValue = localDelegator.findOne("TestingType", false, "testingTypeId", "TEST-5");
+        assertEquals("Retrieved value has the correct description", "Testing Type #5", testValue.getString("description"));
+        String newValueStr = UtilXml.toXml(testValue);
+        GenericValue newValue = (GenericValue) UtilXml.fromXml(newValueStr);
+        assertEquals("Retrieved value has the correct description", "Testing Type #5", newValue.getString("description"));
+        newValue.put("description", "XML Testing Type #5");
+        newValue.store();
+        newValue = localDelegator.findOne("TestingType", false, "testingTypeId", "TEST-5");
+        assertEquals("Retrieved value has the correct description", "XML Testing Type #5", newValue.getString("description"));
+        TransactionUtil.rollback(transBegin, null, null);
     }
 
     /*
      * Tests storing data with the delegator's .create method.  Also tests .findCountByCondition and .getNextSeqId
      */
     public void testCreateTree() throws Exception {
-        try {
         // get how many child nodes did we have before creating the tree
         EntityCondition isChild = EntityCondition.makeCondition("primaryParentNodeId", EntityOperator.NOT_EQUAL, GenericEntity.NULL_FIELD);
         long alreadyStored = delegator.findCountByCondition("TestingNode", isChild, null, null);
@@ -166,10 +151,7 @@ public class EntityTestSuite extends Ent
         long newlyStored = delegator.findCountByCondition("TestingNode", isChild, null, null);
 
         // Normally, newlyStored = alreadyStored + created
-        TestCase.assertEquals("Created/Stored Nodes", newlyStored, created + alreadyStored);
-        } catch (GenericEntityException e) {
-            Debug.logInfo(e.getMessage(), module);
-        }
+        assertEquals("Created/Stored Nodes", created + alreadyStored, newlyStored);
     }
 
     /*
@@ -206,7 +188,7 @@ public class EntityTestSuite extends Ent
             newValues.add(member);
         }
         int n = delegator.storeAll(newValues);
-        TestCase.assertEquals("Created/Stored Nodes", n, newValues.size());
+        assertEquals("Created/Stored Nodes", newValues.size(), n);
     }
 
     /*
@@ -227,7 +209,7 @@ public class EntityTestSuite extends Ent
             }
         }
         long testingcount = delegator.findCountByCondition("Testing", null, null, null);
-        TestCase.assertEquals("Number of views should equal number of created entities in the test.", nodeWithMembers.size(), testingcount);
+        assertEquals("Number of views should equal number of created entities in the test.", testingcount, nodeWithMembers.size());
     }
 
     /*
@@ -245,7 +227,7 @@ public class EntityTestSuite extends Ent
         List<GenericValue> testingSize10 = delegator.findList("Testing", condition, UtilMisc.toSet("testingSize", "comments"), null, findOptions, false);
         Debug.logInfo("testingSize10 is " + testingSize10.size(), module);
 
-        TestCase.assertEquals("There should only be 1 result found by findDistinct()", testingSize10.size(), 1);
+        assertEquals("There should only be 1 result found by findDistinct()", 1, testingSize10.size());
     }
 
     /*
@@ -254,64 +236,66 @@ public class EntityTestSuite extends Ent
     public void testNotLike() throws Exception {
         EntityCondition cond  = EntityCondition.makeCondition("description", EntityOperator.NOT_LIKE, "root%");
         List<GenericValue> nodes = delegator.findList("TestingNode", cond, null, null, null, false);
-        TestCase.assertTrue("Found nodes", nodes != null);
+        assertNotNull("Found nodes", nodes);
 
         for (GenericValue product: nodes) {
             String nodeId = product.getString("description");
             Debug.logInfo("Testing name - " + nodeId, module);
-            TestCase.assertTrue("No nodes starting w/ root", !nodeId.startsWith("root"));
+            assertFalse("No nodes starting w/ root", nodeId.startsWith("root"));
         }
     }
 
     /*
      * Tests foreign key integrity by trying to remove an entity which has foreign-key dependencies.  Should cause an exception.
      */
-    public void testForeignKeyCreate() throws Exception {
+    public void testForeignKeyCreate() {
+        GenericEntityException caught = null;
         try {
             delegator.create("Testing", "testingId", delegator.getNextSeqId("Testing"), "testingTypeId", "NO-SUCH-KEY");
         } catch (GenericEntityException e) {
-            Debug.logInfo(e.toString(), module);
-            return;
+            caught = e;
         }
-        TestCase.fail("Foreign key referential integrity is not observed for create (INSERT)");
+        assertNotNull("Foreign key referential integrity is not observed for create (INSERT)", caught);
+        Debug.logInfo(caught.toString(), module);
     }
 
     /*
      * Tests foreign key integrity by trying to remove an entity which has foreign-key dependencies.  Should cause an exception.
      */
-    public void testForeignKeyRemove() throws Exception {
+    public void testForeignKeyRemove() {
+        GenericEntityException caught = null;
         try {
             EntityCondition isLevel1 = EntityCondition.makeCondition("description", EntityOperator.EQUALS, "node-level #1");
             delegator.removeByCondition("TestingNode", isLevel1);
         } catch (GenericEntityException e) {
-            Debug.logInfo(e.toString(), module);
-            return;
+            caught = e;
         }
-        TestCase.fail("Foreign key referential integrity is not observed for remove (DELETE)");
+        assertNotNull("Foreign key referential integrity is not observed for remove (DELETE)", caught);
+        Debug.logInfo(caught.toString(), module);
     }
 
     /*
      * 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<GenericValue> values = delegator.findList("TestingNodeMember", null, null, null, null, false);
+        //
+        // Find the testing entities tru the node member and build a list of them
+        //
+        List<GenericValue> values = delegator.findList("TestingNodeMember", null, null, null, null, false);
 
-            ArrayList<GenericValue> testings = new ArrayList<GenericValue>();
+        ArrayList<GenericValue> testings = new ArrayList<GenericValue>();
 
-            for (GenericValue nodeMember: values) {
-                testings.add(nodeMember.getRelatedOne("Testing"));
-            }
-            // and remove the nodeMember afterwards
-            delegator.removeAll(values);
-            values = delegator.findList("TestingNodeMember", null, null, null, null, false);
-            TestCase.assertTrue("No more Node Member entities", values.size() == 0);
+        for (GenericValue nodeMember: values) {
+            testings.add(nodeMember.getRelatedOne("Testing"));
+        }
+        // and remove the nodeMember afterwards
+        delegator.removeAll(values);
+        values = delegator.findList("TestingNodeMember", null, null, null, null, false);
+        assertEquals("No more Node Member entities", 0, values.size());
 
-            delegator.removeAll(testings);
-            values = delegator.findList("Testing", null, null, null, null, false);
-            TestCase.assertTrue("No more Testing entities", values.size() == 0);
+        delegator.removeAll(testings);
+        values = delegator.findList("Testing", null, null, null, null, false);
+        assertEquals("No more Testing entities", 0, values.size());
     }
 
     /*
@@ -321,16 +305,10 @@ public class EntityTestSuite extends Ent
         // change the description of all the level1 nodes
         EntityCondition isLevel1 = EntityCondition.makeCondition("description", EntityOperator.EQUALS, "node-level #1");
         Map<String, String> fieldsToSet = UtilMisc.toMap("description", "node-level #1 (updated)");
-        int n = 0;
-        try {
-            delegator.storeByCondition("TestingNode", fieldsToSet, isLevel1);
-            List<GenericValue> 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);
+        delegator.storeByCondition("TestingNode", fieldsToSet, isLevel1);
+        List<GenericValue> updatedNodes = delegator.findByAnd("TestingNode", fieldsToSet);
+        int n = updatedNodes.size();
+        assertTrue("testStoreByCondition updated nodes > 0", n > 0);
     }
 
     /*
@@ -341,15 +319,8 @@ public class EntityTestSuite extends Ent
         // remove all the level1 nodes by using a condition on the description field
         //
         EntityCondition isLevel1 = EntityCondition.makeCondition("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);
+        int n = delegator.removeByCondition("TestingNode", isLevel1);
+        assertTrue("testRemoveByCondition nodes > 0", n > 0);
     }
 
     /*
@@ -366,13 +337,13 @@ public class EntityTestSuite extends Ent
         for (GenericValue value: rootValues) {
             GenericPK pk = value.getPrimaryKey();
             int del = delegator.removeByPrimaryKey(pk);
-            TestCase.assertEquals("Removing Root by primary key", del, 1);
+            assertEquals("Removing Root by primary key", 1, del);
         }
 
         // no more TestingNode should be in the data base anymore.
 
         List<GenericValue> testingNodes = delegator.findList("TestingNode", null, null, null, null, false);
-        TestCase.assertEquals("No more TestingNode after removing the roots", testingNodes.size(), 0);
+        assertEquals("No more TestingNode after removing the roots", 0, testingNodes.size());
     }
 
     /*
@@ -384,7 +355,7 @@ public class EntityTestSuite extends Ent
 
         // now make sure there are no more of these
         values = delegator.findList("TestingType", null, null, null, null, false);
-        TestCase.assertEquals("No more TestingTypes after remove all", values.size(), 0);
+        assertEquals("No more TestingTypes after remove all", 0, values.size());
     }
 
     /*
@@ -398,10 +369,7 @@ public class EntityTestSuite extends Ent
             }
             delegator.storeAll(newValues);
             List<GenericValue> newlyCreatedValues = delegator.findList("Testing", null, null, UtilMisc.toList("testingId"), null, false);
-            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;
+            assertEquals("Test to create " + TEST_COUNT + " and store all at once", TEST_COUNT, newlyCreatedValues.size());
         } finally {
             List<GenericValue> newlyCreatedValues = delegator.findList("Testing", null, null, UtilMisc.toList("testingId"), null, false);
             delegator.removeAll(newlyCreatedValues);
@@ -412,16 +380,11 @@ public class EntityTestSuite extends Ent
      * 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", "testingId", getTestId("T2-", i)));
-            }
-            List<GenericValue> newlyCreatedValues = delegator.findList("Testing", null, null, UtilMisc.toList("testingId"), null, false);
-            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;
+        for (int i = 0; i < TEST_COUNT; i++) {
+            delegator.create(delegator.makeValue("Testing", "testingId", getTestId("T2-", i)));
         }
+        List<GenericValue> newlyCreatedValues = delegator.findList("Testing", null, null, UtilMisc.toList("testingId"), null, false);
+        assertEquals("Test to create " + TEST_COUNT + " and store one at a time: ", TEST_COUNT, newlyCreatedValues.size());
     }
 
     /*
@@ -432,16 +395,16 @@ public class EntityTestSuite extends Ent
         try {
             beganTransaction = TransactionUtil.begin();
             EntityListIterator iterator = delegator.find("Testing", EntityCondition.makeCondition("testingId", EntityOperator.LIKE, "T2-%"), null, null, UtilMisc.toList("testingId"), null);
-            assertTrue("Test if EntityListIterator was created: ", iterator != null);
+            assertNotNull("Test if EntityListIterator was created: ", iterator);
 
             int i = 0;
             GenericValue item = iterator.next();
             while (item != null) {
-                assertTrue("Testing if iterated data matches test data (row " + i + "): ", item.getString("testingId").equals(getTestId("T2-", i)));
+                assertEquals("Testing if iterated data matches test data (row " + i + "): ", getTestId("T2-", i), item.getString("testingId"));
                 item = iterator.next();
                 i++;
             }
-            assertTrue("Test if EntitlyListIterator iterates exactly " + TEST_COUNT + " times: " , i == TEST_COUNT);
+            assertEquals("Test if EntitlyListIterator iterates exactly " + TEST_COUNT + " times: " , TEST_COUNT, i);
             iterator.close();
         } catch (GenericEntityException e) {
             TransactionUtil.rollback(beganTransaction, "GenericEntityException occurred while iterating with EntityListIterator", e);
@@ -458,36 +421,29 @@ public class EntityTestSuite extends Ent
      * This test will verify transaction rollbacks using TransactionUtil.
      */
     public void testTransactionUtilRollback() throws Exception {
-        try {
-            GenericValue testValue = delegator.makeValue("Testing", "testingId", "rollback-test");
-            boolean transBegin = TransactionUtil.begin();
-            delegator.create(testValue);
-            TransactionUtil.rollback(transBegin, null, null);
-            GenericValue testValueOut = delegator.findOne("Testing", false, "testingId", "rollback-test");
-            assertEquals("Test that transaction rollback removes value: ", testValueOut, null);
-        } catch (GenericEntityException e) {
-            assertTrue("GenericEntityException:" + e.toString(), false);
-            return;
-        }
+        GenericValue testValue = delegator.makeValue("Testing", "testingId", "rollback-test");
+        boolean transBegin = TransactionUtil.begin();
+        delegator.create(testValue);
+        TransactionUtil.rollback(transBegin, null, null);
+        GenericValue testValueOut = delegator.findOne("Testing", false, "testingId", "rollback-test");
+        assertEquals("Test that transaction rollback removes value: ", testValueOut, null);
     }
 
     /*
      * This test will verify that a transaction which takes longer than the pre-set timeout are rolled back.
      */
     public void testTransactionUtilMoreThanTimeout() throws Exception {
+        GenericTransactionException caught = null;
         try {
             GenericValue testValue = delegator.makeValue("Testing", "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;
+            caught = e;
         } finally {
+            assertNotNull("timeout thrown", caught);
             delegator.removeByAnd("Testing", "testingId", "timeout-test");
         }
     }
@@ -503,12 +459,6 @@ public class EntityTestSuite extends Ent
             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", "testingId", "timeout-test");
         }
@@ -520,9 +470,6 @@ public class EntityTestSuite extends Ent
     public void testSetNullBlob() throws Exception {
         try {
             delegator.create("TestBlob", "testBlobId", "null-blob");
-        } catch (GenericEntityException ex) {
-            assertTrue("GenericEntityException:" + ex.toString(), false);
-            return;
         } finally {
             List<GenericValue> allTestBlobs = delegator.findList("TestBlob", null, null, null, null, false);
             delegator.removeAll(allTestBlobs);
@@ -541,18 +488,12 @@ public class EntityTestSuite extends Ent
             GenericValue testingBlob = delegator.makeValue("TestBlob", "testBlobId", "byte-blob");
             testingBlob.setBytes("testBlobField", b);
             testingBlob.create();
-            TestCase.assertTrue("Blob with byte array created successfully", true);
             testingBlob = delegator.findOne("TestBlob", UtilMisc.toMap("testBlobId", "byte-blob"), false);
             byte[] c = testingBlob.getBytes("testBlobField");
-            TestCase.assertTrue("Byte array read from Blob data is the same length", b.length == c.length);
+            assertEquals("Byte array read from Blob data is the same length", b.length, c.length);
             for (int i = 0; i < b.length; i++) {
-                if (b[i] != c[i]) {
-                    TestCase.fail("Blob data mismatch at " + i);
-                }
+                assertEquals("Blob data[" + i + "]", b[i], c[i]);
             }
-            TestCase.assertTrue("Blob with byte array read successfully", true);
-        } catch (Exception ex) {
-            TestCase.fail(ex.getMessage());
         } finally {
             // Remove all our newly inserted values.
             List<GenericValue> values = delegator.findList("TestBlob", null, null, null, null, false);