Index: modules/test-db/src/test/java/org/jbpm/test/variables/DeploymentSerializeTest.java =================================================================== --- modules/test-db/src/test/java/org/jbpm/test/variables/DeploymentSerializeTest.java (revision 0) +++ modules/test-db/src/test/java/org/jbpm/test/variables/DeploymentSerializeTest.java (revision 0) @@ -0,0 +1,156 @@ +package org.jbpm.test.variables; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.util.List; + +import org.codehaus.janino.DebuggingInformation; +import org.codehaus.janino.util.StringPattern; +import org.codehaus.janino.util.enumerator.EnumeratorSet; +import org.jbpm.api.ProcessInstance; +import org.jbpm.api.task.Task; +import org.jbpm.test.JbpmTestCase; + +public class DeploymentSerializeTest extends JbpmTestCase { + private File targetDir; + private String deploymentId; + + protected void setUp() throws Exception { + super.setUp(); + + this.targetDir = new File("target/generated/org/jbpm/test/variables"); + + generateClasses(); + + this.deploymentId = repositoryService.createDeployment() + .addResourceFromClasspath("org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml") + .addResourceFromInputStream("org/jbpm/test/variables/DeploymentSerializeBean.class", new FileInputStream(new File(targetDir, "DeploymentSerializeBean.class"))) + .addResourceFromInputStream("org/jbpm/test/variables/DeploymentSerializeCustom1.class", new FileInputStream(new File(targetDir, "DeploymentSerializeCustom1.class"))) + .addResourceFromInputStream("org/jbpm/test/variables/DeploymentSerializeCustom2.class", new FileInputStream(new File(targetDir, "DeploymentSerializeCustom2.class"))) + .deploy(); + } + + protected void tearDown() throws Exception { + repositoryService.deleteDeploymentCascade(deploymentId); + super.tearDown(); + } + + public void testDeploymentVariableSerialization() { + ProcessInstance processInstance = executionService.startProcessInstanceByKey("DeploymentSerializeTest"); + String processInstanceId = processInstance.getId(); + + List tasks = taskService.findPersonalTasks("alex"); + assertTrue(tasks.size() == 1); + Task task = tasks.get(0); + + assertNotNull(taskService.getVariable(task.getId(), "bean")); + assertNotNull(executionService.getVariable(processInstanceId, "bean")); + assertEquals(executionService.getVariable(processInstanceId, "bean").toString(), "DeploymentSerializeBean[value=42]"); + + taskService.completeTask(task.getId()); + } + + private void generateClasses() { + try { + if (!targetDir.exists()) { + targetDir.mkdirs(); + } + + File[] sourceFiles = { + writeFile(targetDir, "DeploymentSerializeBean.java", getBeanSource()), + writeFile(targetDir, "DeploymentSerializeCustom1.java", getCustom1Source()), + writeFile(targetDir, "DeploymentSerializeCustom2.java", getCustom2Source()), + }; + + compileClasses(sourceFiles); + } + catch (Exception e) { + log.error("Error while creating additional resources", e); + } + } + + /** + * This method is used to generate classes that will be used within a process but should not be + * on class path while executing process but retrieved from db. + * + * Uses Janinio to compile the sources. + */ + private void compileClasses(File[] sourceFiles) throws Exception { + log.debug("Inside compileClasses method"); + + File destinationDirectory = org.codehaus.janino.Compiler.NO_DESTINATION_DIRECTORY; + File[] optionalSourcePath = null; + File[] classPath = { new File("."), new File("../api/target/classes") }; + File[] optionalExtDirs = null; + File[] optionalBootClassPath = null; + String optionalCharacterEncoding = null; + boolean verbose = false; + EnumeratorSet debuggingInformation = DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION; + StringPattern[] warningHandlePatterns = org.codehaus.janino.Compiler.DEFAULT_WARNING_HANDLE_PATTERNS; + boolean rebuild = false; + + log.debug("About to run Janinio compiler"); + + org.codehaus.janino.Compiler javac = new org.codehaus.janino.Compiler(optionalSourcePath, classPath, optionalExtDirs, optionalBootClassPath, + destinationDirectory, optionalCharacterEncoding, verbose, debuggingInformation, warningHandlePatterns, rebuild); + javac.compile(sourceFiles); + + log.debug("Class compiled successfully"); + } + + private File writeFile(File directory, String filename, String content) throws Exception { + File sourceFile = new File(directory, filename); + FileWriter writer = new FileWriter(sourceFile); + writer.write(content); + writer.close(); + + log.debug("Source file '" + filename + "' created in '" + directory.getAbsolutePath() + "'"); + + return sourceFile; + } + + private String getBeanSource() { + StringBuffer source = new StringBuffer(); + source.append("package org.jbpm.test.variables;\n"); + source.append("import java.io.Serializable;\n"); + source.append("public class DeploymentSerializeBean implements Serializable {\n"); + source.append(" private int value;\n"); + source.append(" public int getValue() { return value; }\n"); + source.append(" public void setValue(int value) { this.value = value; }\n"); + source.append(" public String toString() { return \"DeploymentSerializeBean[value=\" + value + \"]\"; }\n"); + source.append("}\n"); + return source.toString(); + } + + private String getCustom1Source() { + StringBuffer source = new StringBuffer(); + source.append("package org.jbpm.test.variables;\n"); + source.append("import org.jbpm.api.activity.ActivityBehaviour;\n"); + source.append("import org.jbpm.api.activity.ActivityExecution;\n"); + source.append("public class DeploymentSerializeCustom1 implements ActivityBehaviour {\n"); + source.append(" public void execute(ActivityExecution execution) throws Exception {\n"); + source.append(" DeploymentSerializeBean bean = new DeploymentSerializeBean();\n"); + source.append(" bean.setValue(42);\n"); + source.append(" execution.setVariable(\"bean\", bean);\n"); + source.append(" }\n"); + source.append("}\n"); + return source.toString(); + } + + private String getCustom2Source() { + StringBuffer source = new StringBuffer(); + source.append("package org.jbpm.test.variables;\n"); + source.append("import org.jbpm.api.activity.ActivityBehaviour;\n"); + source.append("import org.jbpm.api.activity.ActivityExecution;\n"); + source.append("public class DeploymentSerializeCustom2 implements ActivityBehaviour {\n"); + source.append(" public void execute(ActivityExecution execution) throws Exception {\n"); + source.append(" DeploymentSerializeBean bean = (DeploymentSerializeBean)execution.getVariable(\"bean\");\n"); + source.append(" if (bean.getValue() != 42) {\n"); + source.append(" throw new Exception(\"Value must be 42!\");\n"); + source.append(" }\n"); + source.append(" }\n"); + source.append("}\n"); + return source.toString(); + } +} Index: modules/test-db/src/test/resources/org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml =================================================================== --- modules/test-db/src/test/resources/org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml (revision 0) +++ modules/test-db/src/test/resources/org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml (revision 0) @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + +