1 package org.smartcomps.twister.engine.core.dynamic; 2 3 import net.sf.hibernate.tool.hbm2ddl.SchemaExport; 4 import net.sf.hibernate.cfg.Configuration; 5 import org.smartcomps.twister.common.transaction.TransactionManager; 6 import org.smartcomps.twister.common.lifecycle.LifecycleManager; 7 import org.smartcomps.twister.engine.priv.core.definition.TwisterProcess; 8 import org.smartcomps.twister.engine.priv.core.definition.ProcessFactory; 9 import org.smartcomps.twister.engine.priv.core.definition.ActivityFactory; 10 import org.smartcomps.twister.engine.priv.core.definition.Sequence; 11 import org.smartcomps.twister.engine.priv.core.definition.While; 12 import org.smartcomps.twister.engine.priv.core.definition.Receive; 13 import org.smartcomps.twister.engine.priv.core.definition.CorrelationRef; 14 import org.smartcomps.twister.engine.priv.core.definition.Assign; 15 import org.smartcomps.twister.engine.priv.core.definition.Assignment; 16 import org.smartcomps.twister.engine.priv.core.dynamic.ProcessInstance; 17 import org.smartcomps.twister.engine.priv.core.dynamic.ProcessInstanceFactory; 18 import org.smartcomps.twister.engine.priv.core.dynamic.impl.xao.VariableXAO; 19 import org.smartcomps.twister.engine.TwisterEngineFactory; 20 import org.smartcomps.twister.engine.TwisterEngine; 21 import org.smartcomps.twister.util.BeanTester; 22 import org.dom4j.Document; 23 import org.dom4j.DocumentHelper; 24 import org.dom4j.Element; 25 import junit.framework.TestCase; 26 27 import java.util.Map; 28 import java.util.HashMap; 29 30 /*** 31 * Testing the While execution through a complete process execution. 32 */ 33 public class TestWhileEC extends TestCase { 34 35 protected void setUp() throws Exception { 36 LifecycleManager.getLifecycleManager().createResources(); 37 LifecycleManager.getLifecycleManager().startResources(); 38 39 SchemaExport schemaExport = new SchemaExport(new Configuration().configure()); 40 schemaExport.create(true, true); 41 42 TransactionManager.beginTransaction(); 43 } 44 45 protected void tearDown() throws Exception { 46 TransactionManager.commitTransaction(); 47 48 LifecycleManager.getLifecycleManager().stopResources(); 49 LifecycleManager.getLifecycleManager().destroyResources(); 50 } 51 52 public void testExecute() throws Exception { 53 createLoopingWhileProcess(); 54 TransactionManager.commitTransaction(); 55 56 String counterId = (String)BeanTester.generateRandomValue(String.class); 57 Document doc = DocumentHelper.createDocument(); 58 Element root = doc.addElement("message").addElement("main"); 59 root.addElement("counter").setText("0"); 60 root.addElement("counterId").setText(counterId); 61 int result = TwisterEngineFactory.getEngine().acknowledge("partner", "pt", "op", doc); 62 assertEquals("Message controller didn't acknowledge the message properly, an error occured", 63 TwisterEngine.ACKNOWLEDGED, result); 64 65 Thread.sleep(10000); 66 TransactionManager.beginTransaction(); 67 Map corrProp = new HashMap(); 68 corrProp.put("counterId", counterId); 69 ProcessInstance createdInstance = ProcessInstanceFactory.findInstanceByCorrelation("counterCorr", corrProp); 70 assertNotNull("Could not find the created process instance from correlation", createdInstance); 71 assertEquals("Process instance is not properly terminated", ProcessInstance.COMPLETED, createdInstance.getStatus()); 72 assertEquals("The counter variable has not been incremented 10 times", 73 "10.0", VariableXAO.queryVariableValue("tenLoops", "counterVar", createdInstance.getId(), "main", "/counter")); 74 } 75 76 private void createLoopingWhileProcess() throws Exception { 77 ////////////////////////// 78 // var counter 79 // process 80 // sequence 81 // | receive : counter <- 0 82 // | while counter < 10 83 // | | assign : counter = counter + 1 84 ////////////////////////// 85 TwisterProcess process = ProcessFactory.createProcess("tenLoops"); 86 Sequence sequence = (Sequence) ActivityFactory.createActivity(Sequence.class, process); 87 Receive receive = (Receive) ActivityFactory.createActivity(Receive.class, sequence); 88 While whileA = (While) ActivityFactory.createActivity(While.class, sequence); 89 Assign assignInWhile = (Assign) ActivityFactory.createActivity(Assign.class, whileA); 90 91 ProcessFactory.addCorrelation(process, "counterCorr", "counterId"); 92 // <property name="counterId" type="xsd:string"/> 93 // <propertyAlias propertyName="counterId" messageType="counterMsg" part="main" query="/counterId"/> 94 ProcessFactory.addProperty(process, "counterId", "xsd:string", "initCounterId", "main", "/counterId"); 95 96 receive.setCreateInstance(true); 97 ActivityFactory.addCorrelationRef(receive, "counterCorr", true, CorrelationRef.IN); 98 receive.setOperation("op"); 99 receive.setPartner("partner"); 100 receive.setPortType("pt"); 101 receive.setVariable("counterVar"); 102 103 whileA.setCondition("bpws:getVariableData('counterVar', 'main', '/counter') < 10"); 104 105 Assignment assignment = ActivityFactory.addAssignment(assignInWhile, Assignment.EXPRESSION, Assignment.VARIABLE_PART); 106 assignment.setFromFirstValue("bpws:getVariableData('counterVar', 'main', '/counter') + 1"); 107 assignment.setToFirstValue("counterVar"); 108 assignment.setToSecondValue("main"); 109 assignment.setToQuery("/counter"); 110 } 111 }

This page was automatically generated by Maven