Author: adrianc Date: Mon Apr 22 19:38:06 2013 New Revision: 1470676 URL: http://svn.apache.org/r1470676 Log: Copy the observable aspect of GenericEntity. Includes unit tests. Also added some FIXME comments.
Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java?rev=1470676&r1=1470675&r2=1470676&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java Mon Apr 22 19:38:06 2013 @@ -207,6 +207,7 @@ public class GenericEntity implements Ma if (value.fields != null) this.fields.putAll(value.fields); this.delegatorName = value.delegatorName; this.internalDelegator = value.internalDelegator; + this.observable = new Observable(value.observable); } public void reset() { @@ -232,11 +233,13 @@ public class GenericEntity implements Ma if (!thisPK.equals(newPK)) { throw new GenericEntityException("Could not refresh value, new value did not have the same primary key; this PK=" + thisPK + ", new value PK=" + newPK); } + // FIXME: This is dangerous - two instances sharing a common field Map is a bad idea. this.fields = newValue.fields; this.setDelegator(newValue.getDelegator()); this.generateHashCode = newValue.generateHashCode; this.cachedHashCode = newValue.cachedHashCode; this.modified = false; + this.observable = new Observable(newValue.observable); } public boolean isModified() { Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java?rev=1470676&r1=1470675&r2=1470676&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java Mon Apr 22 19:38:06 2013 @@ -46,9 +46,11 @@ public class GenericValue extends Generi public static final GenericValue NULL_VALUE = new NullGenericValue(); /** Map to cache various related entity collections */ + // FIXME: This is a bad idea. Another process could change the related values after they are added to the Map. public transient Map<String, List<GenericValue>> relatedCache = null; /** Map to cache various related cardinality one entity collections */ + // FIXME: This is a bad idea. Another process could change the related values after they are added to the Map. public transient Map<String, GenericValue> relatedOneCache = null; /** This Map will contain the original field values from the database iff @@ -146,6 +148,7 @@ public class GenericValue extends Generi * values from the Db. */ public void copyOriginalDbValues() { + // FIXME: There is no guarantee this.fields was not modified. this.originalDbValues = new HashMap<String, Object>(); this.originalDbValues.putAll(this.fields); } 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=1470676&r1=1470675&r2=1470676&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 Mon Apr 22 19:38:06 2013 @@ -29,6 +29,8 @@ import java.util.List; import java.util.Map; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.Observable; +import org.ofbiz.base.util.Observer; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilXml; @@ -95,9 +97,17 @@ public class EntityTestSuite extends Ent // 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 + // Test Observable aspect + TestObserver observer = new TestObserver(); + testValue.addObserver(observer); testValue.put("description", "New Testing Type #1"); + assertEquals("Observer called with original GenericValue field name", "description", observer.arg); + observer.observable = null; + observer.arg = null; + GenericValue clonedValue = (GenericValue) testValue.clone(); + clonedValue.put("description", "New Testing Type #1"); + assertEquals("Observer called with cloned GenericValue field name", "description", observer.arg); + // now store it testValue.store(); // now retrieve it again and make sure that the updated value is correct @@ -848,4 +858,15 @@ public class EntityTestSuite extends Ent testType = delegator.findOne("TestingType", UtilMisc.toMap("testingTypeId", "JUNIT-TEST2"), false); assertNull("Delete TestingType 2", testType); } + + private final class TestObserver implements Observer { + private Observable observable; + private Object arg; + + @Override + public void update(Observable observable, Object arg) { + this.observable = observable; + this.arg = arg; + } + } }