[jbpm-commits] JBoss JBPM SVN: r7044 - in jbpm3/branches/jbpm-3.2-soa/core/src: main/java/org/jbpm/context/exe/variableinstance and 2 other directories.
do-not-reply at jboss.org
do-not-reply at jboss.org
Tue Nov 15 04:50:29 EST 2011
Author: marco.rietveld
Date: 2011-11-15 04:50:28 -0500 (Tue, 15 Nov 2011)
New Revision: 7044
Added:
jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/context/exe/variableinstance/
jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/context/exe/variableinstance/SerializableInstanceDbTest.java
Modified:
jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteArray.java
jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java
jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/context/exe/variableinstance/ByteArrayInstance.java
Log:
JBPM3441: Serializable Process Variable de/serialization results in an NPE in a non-trivial case
Modified: jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteArray.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteArray.java 2011-10-11 03:53:53 UTC (rev 7043)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteArray.java 2011-11-15 09:50:28 UTC (rev 7044)
@@ -66,6 +66,18 @@
this.name = other.name;
}
+ public void update(ByteArray value) {
+ List otherByteBlocks = value.getByteBlocks();
+ // Different than constructor:
+ // 1. clear on empty list.
+ this.byteBlocks.clear();
+ if (otherByteBlocks != null) {
+ // 2. use addAll in order to work as much as possible with JPA/ORM
+ this.byteBlocks.addAll(otherByteBlocks);
+ }
+ this.name = value.name;
+ }
+
public byte[] getBytes() {
return ByteBlockChopper.glueChopsBackTogether(byteBlocks);
}
@@ -108,4 +120,6 @@
public List getByteBlocks() {
return byteBlocks;
}
+
+
}
Modified: jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java 2011-10-11 03:53:53 UTC (rev 7043)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/bytes/ByteBlockChopper.java 2011-11-15 09:50:28 UTC (rev 7044)
@@ -79,6 +79,8 @@
int blockCount = byteBlocks.size();
switch (blockCount) {
case 0:
+ // NPE's are never nice
+ byteArray = new byte [0];
break;
case 1:
byteArray = (byte[]) byteBlocks.get(0);
Modified: jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/context/exe/variableinstance/ByteArrayInstance.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/context/exe/variableinstance/ByteArrayInstance.java 2011-10-11 03:53:53 UTC (rev 7043)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/main/java/org/jbpm/context/exe/variableinstance/ByteArrayInstance.java 2011-11-15 09:50:28 UTC (rev 7044)
@@ -21,6 +21,7 @@
*/
package org.jbpm.context.exe.variableinstance;
+import org.hibernate.LockMode;
import org.hibernate.Session;
import org.jbpm.JbpmContext;
import org.jbpm.bytes.ByteArray;
@@ -45,15 +46,25 @@
if (token != null) {
token.addLog(new ByteArrayUpdateLog(this, this.value, (ByteArray) value));
}
- // delete old value, otherwise it will be unreachable
if (this.value != null) {
JbpmContext jbpmContext = JbpmContext.getCurrentJbpmContext();
+ ByteArray valueToUpdate = null;
if (jbpmContext != null) {
Session session = jbpmContext.getSession();
- if (session != null) session.delete(this.value);
+ if (session != null) {
+ valueToUpdate = (ByteArray) session.get(ByteArray.class, new Long(this.value.getId()), LockMode.UPGRADE);
+ valueToUpdate.update((ByteArray) value);
+ }
}
+
+ // if for some reason, this.value is NOT in the (hibernate) session, update it anyway.
+ // -- but then there's something _VERY_ weird and wrong going on.
+ if( valueToUpdate == null ) {
+ this.value.update((ByteArray) value);
+ }
}
- // set new value
- this.value = (ByteArray) value;
+ else {
+ this.value = (ByteArray) value;
+ }
}
}
Added: jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/context/exe/variableinstance/SerializableInstanceDbTest.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/context/exe/variableinstance/SerializableInstanceDbTest.java (rev 0)
+++ jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/context/exe/variableinstance/SerializableInstanceDbTest.java 2011-11-15 09:50:28 UTC (rev 7044)
@@ -0,0 +1,111 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.context.exe.variableinstance;
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.jbpm.context.def.ContextDefinition;
+import org.jbpm.context.exe.ContextInstance;
+import org.jbpm.context.exe.MySerializableClass;
+import org.jbpm.db.AbstractDbTestCase;
+import org.jbpm.graph.def.ProcessDefinition;
+import org.jbpm.graph.exe.ProcessInstance;
+import org.jbpm.persistence.db.DbPersistenceService;
+
+public class SerializableInstanceDbTest extends AbstractDbTestCase {
+
+ ProcessInstance processInstance;
+ ContextInstance contextInstance;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ ProcessDefinition processDefinition = new ProcessDefinition(getName());
+ processDefinition.addDefinition(new ContextDefinition());
+ deployProcessDefinition(processDefinition);
+
+ processInstance = new ProcessInstance(processDefinition);
+ contextInstance = processInstance.getContextInstance();
+
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testSerializableVariableKeepsId() {
+ contextInstance.setVariable("a", new MySerializableClass(4));
+ processInstance = saveAndReload(processInstance);
+
+ int numByteArrayRows = getLatestDbId("ID_", "JBPM_BYTEARRAY");
+ int numByteBlockRows = getLatestDbId("PROCESSFILE_", "JBPM_BYTEBLOCK");
+
+ // check validation ids
+ assertTrue(numByteArrayRows > 0);
+ assertTrue(numByteBlockRows > 0);
+
+ contextInstance = processInstance.getContextInstance();
+ assertEquals(new MySerializableClass(4), contextInstance.getVariable("a"));
+
+ // update serializeable variable with new value
+ contextInstance.setVariable("a", new MySerializableClass(555));
+ processInstance = saveAndReload(processInstance);
+
+ numByteArrayRows = getLatestDbId("ID_", "JBPM_BYTEARRAY");
+ numByteBlockRows = getLatestDbId("PROCESSFILE_", "JBPM_BYTEBLOCK");
+
+ // check db ids again
+ assertTrue(numByteArrayRows > 0);
+ assertTrue(numByteBlockRows > 0);
+
+ contextInstance = processInstance.getContextInstance();
+ assertEquals(new MySerializableClass(555), contextInstance.getVariable("a"));
+
+ // check if db ids are the same
+ // 1. One ByteArray object for var
+ // 2. One newValue for 1rst ByteArrayUpdateLog
+ // 3. One newValue and one oldValue for 2nd ByteArrayUpdateLog
+ // TOTAL = 4
+ // If a _NEW_ ByteArray object is made, then there would be 5 ByteArray objects and byte []'s
+ assertTrue("Too many ByteArray objects have been made [" + numByteArrayRows + "]", numByteArrayRows == 4 );
+ assertTrue("Too many byte [] objects have been persisted [" + numByteBlockRows + "]", numByteBlockRows == 4 );
+ }
+
+ protected int getLatestDbId(String idName, String tableName) {
+ int id = 0;
+ DbPersistenceService persistenceService = (DbPersistenceService) jbpmContext.getServices()
+ .getPersistenceService();
+ if (persistenceService != null) {
+ Session session = persistenceService.getSession();
+ List res = session.createSQLQuery("select " + idName + " from " + tableName
+ + " order by " + idName + " desc").list();
+ if (res != null && res.size() > 0) {
+ Object obj = res.get(0);
+ log.debug(tableName + "::" + idName + "=" + obj);
+ id = ((java.math.BigInteger) obj).intValue();
+ }
+ }
+ return id;
+
+ }
+}
\ No newline at end of file
Property changes on: jbpm3/branches/jbpm-3.2-soa/core/src/test/java/org/jbpm/context/exe/variableinstance/SerializableInstanceDbTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
More information about the jbpm-commits
mailing list