Modified: ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java?rev=926987&r1=926986&r2=926987&view=diff ============================================================================== --- ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java (original) +++ ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java Wed Mar 24 09:23:07 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,81 +459,66 @@ 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"); } } - /* - * 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", "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); - } - } - - /* - * 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[100000]; - for (int i = 0; i < b.length; i++) { - b[i] = (byte) i; - } - 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); - for (int i = 0; i < b.length; i++) { - if (b[i] != c[i]) { - TestCase.fail("Blob data mismatch at " + 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); - delegator.removeAll(values); - } - } - - /* - * This creates an string id from a number - */ - private String getTestId(String strTestBase, int iNum) { - StringBuilder strBufTemp = new StringBuilder(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(); - } + /* + * 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", "testBlobId", "null-blob"); + } finally { + List<GenericValue> allTestBlobs = delegator.findList("TestBlob", null, null, null, null, false); + 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[100000]; + for (int i = 0; i < b.length; i++) { + b[i] = (byte) i; + } + GenericValue testingBlob = delegator.makeValue("TestBlob", "testBlobId", "byte-blob"); + testingBlob.setBytes("testBlobField", b); + testingBlob.create(); + testingBlob = delegator.findOne("TestBlob", UtilMisc.toMap("testBlobId", "byte-blob"), false); + byte[] c = testingBlob.getBytes("testBlobField"); + assertEquals("Byte array read from Blob data is the same length", b.length, c.length); + for (int i = 0; i < b.length; i++) { + assertEquals("Blob data[" + i + "]", b[i], c[i]); + } + } finally { + // Remove all our newly inserted values. + List<GenericValue> values = delegator.findList("TestBlob", null, null, null, null, false); + delegator.removeAll(values); + } + } + + /* + * This creates an string id from a number + */ + private String getTestId(String strTestBase, int iNum) { + StringBuilder strBufTemp = new StringBuilder(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(); + } } Modified: ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java URL: http://svn.apache.org/viewvc/ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java?rev=926987&r1=926986&r2=926987&view=diff ============================================================================== --- ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java (original) +++ ofbiz/branches/multitenant20100310/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java Wed Mar 24 09:23:07 2010 @@ -43,16 +43,15 @@ public class SequenceUtil { public static final String module = SequenceUtil.class.getName(); - Map<String, SequenceBank> sequences = new Hashtable<String, SequenceBank>(); - String helperName; - ModelEntity seqEntity; - String tableName; - String nameColName; - String idColName; + private final Map<String, SequenceBank> sequences = new Hashtable<String, SequenceBank>(); + private final String helperName; + private final long bankSize; + private final String tableName; + private final String nameColName; + private final String idColName; public SequenceUtil(String helperName, ModelEntity seqEntity, String nameFieldName, String idFieldName) { this.helperName = helperName; - this.seqEntity = seqEntity; if (seqEntity == null) { throw new IllegalArgumentException("The sequence model entity was null but is required."); } @@ -71,6 +70,11 @@ public class SequenceUtil { throw new IllegalArgumentException("Could not find the field definition for the sequence id field " + idFieldName); } this.idColName = idField.getColName(); + long bankSize = SequenceBank.defaultBankSize; + if (seqEntity.getSequenceBankSize() != null) { + bankSize = seqEntity.getSequenceBankSize().longValue(); + } + this.bankSize = bankSize; } public Long getNextSeqId(String seqName, long staggerMax, ModelEntity seqModelEntity) { @@ -95,7 +99,7 @@ public class SequenceUtil { synchronized(this) { bank = sequences.get(seqName); if (bank == null) { - bank = new SequenceBank(seqName, seqModelEntity, this); + bank = new SequenceBank(seqName); sequences.put(seqName, bank); } } @@ -104,30 +108,26 @@ public class SequenceUtil { return bank; } - class SequenceBank { + private class SequenceBank { public static final long defaultBankSize = 10; public static final long maxBankSize = 5000; public static final long startSeqId = 10000; - public static final int minWaitMillis = 5; - public static final int maxWaitMillis = 50; + public static final long minWaitMillis = 5; + public static final long maxWaitMillis = 50; public static final int maxTries = 5; - long curSeqId; - long maxSeqId; - String seqName; - SequenceUtil parentUtil; - ModelEntity seqModelEntity; + private long curSeqId; + private long maxSeqId; + private final String seqName; - public SequenceBank(String seqName, ModelEntity seqModelEntity, SequenceUtil parentUtil) { + private SequenceBank(String seqName) { this.seqName = seqName; - this.parentUtil = parentUtil; - this.seqModelEntity = seqModelEntity; curSeqId = 0; maxSeqId = 0; - fillBank(1, seqModelEntity); + fillBank(1); } - public synchronized Long getNextSeqId(long staggerMax) { + private synchronized Long getNextSeqId(long staggerMax) { long stagger = 1; if (staggerMax > 1) { stagger = Math.round(Math.random() * staggerMax); @@ -139,7 +139,7 @@ public class SequenceUtil { curSeqId += stagger; return retSeqId; } else { - fillBank(stagger, this.seqModelEntity); + fillBank(stagger); if ((curSeqId + stagger) <= maxSeqId) { Long retSeqId = Long.valueOf(curSeqId); curSeqId += stagger; @@ -151,21 +151,18 @@ public class SequenceUtil { } } - public void refresh(long staggerMax) { + private void refresh(long staggerMax) { this.curSeqId = this.maxSeqId; - this.fillBank(staggerMax, this.seqModelEntity); + this.fillBank(staggerMax); } - protected synchronized void fillBank(long stagger, ModelEntity seqModelEntity) { + private synchronized void fillBank(long stagger) { //Debug.logWarning("[SequenceUtil.SequenceBank.fillBank] Starting fillBank Thread Name is: " + Thread.currentThread().getName() + ":" + Thread.currentThread().toString(), module); // no need to get a new bank, SeqIds available if ((curSeqId + stagger) <= maxSeqId) return; - long bankSize = defaultBankSize; - if (seqModelEntity != null && seqModelEntity.getSequenceBankSize() != null) { - bankSize = seqModelEntity.getSequenceBankSize().longValue(); - } + long bankSize = SequenceUtil.this.bankSize; if (stagger > 1) { // NOTE: could use staggerMax for this, but if that is done it would be easier to guess a valid next id without a brute force attack bankSize = stagger * defaultBankSize; @@ -201,7 +198,7 @@ public class SequenceUtil { ResultSet rs = null; try { - connection = ConnectionFactory.getConnection(parentUtil.helperName); + connection = ConnectionFactory.getConnection(SequenceUtil.this.helperName); } catch (SQLException sqle) { Debug.logWarning("[SequenceUtil.SequenceBank.fillBank]: Unable to esablish a connection with the database... Error was:" + sqle.toString(), module); throw sqle; @@ -221,34 +218,34 @@ public class SequenceUtil { stmt = connection.createStatement(); - sql = "SELECT " + parentUtil.idColName + " FROM " + parentUtil.tableName + " WHERE " + parentUtil.nameColName + "='" + this.seqName + "'"; + sql = "SELECT " + SequenceUtil.this.idColName + " FROM " + SequenceUtil.this.tableName + " WHERE " + SequenceUtil.this.nameColName + "='" + this.seqName + "'"; rs = stmt.executeQuery(sql); boolean gotVal1 = false; if (rs.next()) { - val1 = rs.getLong(parentUtil.idColName); + val1 = rs.getLong(SequenceUtil.this.idColName); gotVal1 = true; } rs.close(); if (!gotVal1) { Debug.logWarning("[SequenceUtil.SequenceBank.fillBank] first select failed: will try to add new row, result set was empty for sequence [" + seqName + "] \nUsed SQL: " + sql + " \n Thread Name is: " + Thread.currentThread().getName() + ":" + Thread.currentThread().toString(), module); - sql = "INSERT INTO " + parentUtil.tableName + " (" + parentUtil.nameColName + ", " + parentUtil.idColName + ") VALUES ('" + this.seqName + "', " + startSeqId + ")"; + sql = "INSERT INTO " + SequenceUtil.this.tableName + " (" + SequenceUtil.this.nameColName + ", " + SequenceUtil.this.idColName + ") VALUES ('" + this.seqName + "', " + startSeqId + ")"; if (stmt.executeUpdate(sql) <= 0) { throw new GenericEntityException("No rows changed when trying insert new sequence row with this SQL: " + sql); } continue; } - sql = "UPDATE " + parentUtil.tableName + " SET " + parentUtil.idColName + "=" + parentUtil.idColName + "+" + bankSize + " WHERE " + parentUtil.nameColName + "='" + this.seqName + "'"; + sql = "UPDATE " + SequenceUtil.this.tableName + " SET " + SequenceUtil.this.idColName + "=" + SequenceUtil.this.idColName + "+" + bankSize + " WHERE " + SequenceUtil.this.nameColName + "='" + this.seqName + "'"; if (stmt.executeUpdate(sql) <= 0) { throw new GenericEntityException("[SequenceUtil.SequenceBank.fillBank] update failed, no rows changes for seqName: " + seqName); } - sql = "SELECT " + parentUtil.idColName + " FROM " + parentUtil.tableName + " WHERE " + parentUtil.nameColName + "='" + this.seqName + "'"; + sql = "SELECT " + SequenceUtil.this.idColName + " FROM " + SequenceUtil.this.tableName + " WHERE " + SequenceUtil.this.nameColName + "='" + this.seqName + "'"; rs = stmt.executeQuery(sql); boolean gotVal2 = false; if (rs.next()) { - val2 = rs.getLong(parentUtil.idColName); + val2 = rs.getLong(SequenceUtil.this.idColName); gotVal2 = true; } @@ -318,7 +315,7 @@ public class SequenceUtil { } // collision happened, wait a bounded random amount of time then continue - int waitTime = (new Double(Math.random() * (maxWaitMillis - minWaitMillis))).intValue() + minWaitMillis; + long waitTime = (long) (Math.random() * (maxWaitMillis - minWaitMillis) + minWaitMillis); Debug.logWarning("[SequenceUtil.SequenceBank.fillBank] Collision found for seqName [" + seqName + "], val1=" + val1 + ", val2=" + val2 + ", val1+bankSize=" + (val1 + bankSize) + ", bankSize=" + bankSize + ", waitTime=" + waitTime, module); Modified: ofbiz/branches/multitenant20100310/framework/example/config/ExampleUiLabels.xml URL: http://svn.apache.org/viewvc/ofbiz/branches/multitenant20100310/framework/example/config/ExampleUiLabels.xml?rev=926987&r1=926986&r2=926987&view=diff ============================================================================== --- ofbiz/branches/multitenant20100310/framework/example/config/ExampleUiLabels.xml (original) +++ ofbiz/branches/multitenant20100310/framework/example/config/ExampleUiLabels.xml Wed Mar 24 09:23:07 2010 @@ -439,8 +439,8 @@ <value xml:lang="zh">详ç»æè¿°</value> </property> <property key="ExampleLookupFields"> - <value xml:lang="en">Examples Lookup Fields</value> - <value xml:lang="fr">Exemples de champs de recherche</value> + <value xml:lang="en">Examples Lookup Fields (first in 'normal' position, second centered)</value> + <value xml:lang="fr">Exemples de champs de recherche (le premier en position 'normal', le second centré)</value> </property> <property key="ExampleMainPage"> <value xml:lang="en">Example Main Page</value> |
Free forum by Nabble | Edit this page |