1 package org.jbpm.context.log; 2 3 import java.util.Arrays ; 4 import java.util.Date ; 5 import java.util.List ; 6 7 import org.jbpm.bytes.ByteArray; 8 import org.jbpm.context.def.ContextDefinition; 9 import org.jbpm.context.exe.ContextInstance; 10 import org.jbpm.db.AbstractDbTestCase; 11 import org.jbpm.graph.def.ProcessDefinition; 12 import org.jbpm.graph.exe.ProcessInstance; 13 import org.jbpm.logging.exe.LoggingInstance; 14 15 public class VariableUpdateDbTest extends AbstractDbTestCase { 16 17 private ProcessDefinition processDefinition = null; 18 private ProcessInstance processInstance = null; 19 private ContextInstance contextInstance = null; 20 21 public void setUp() { 22 super.setUp(); 23 processDefinition = new ProcessDefinition(); 24 processDefinition.addDefinition(new ContextDefinition()); 25 graphSession.saveProcessDefinition(processDefinition); 26 27 processInstance = new ProcessInstance( processDefinition ); 28 contextInstance = processInstance.getContextInstance(); 29 } 30 31 public void testVariableCreateLogs() { 32 contextInstance.setVariable("a", new Integer (3)); 33 34 graphSession.saveProcessInstance(processInstance); 35 newTransaction(); 36 List logs = loggingSession.findLogsByToken(processInstance.getRootToken().getId()); 37 VariableCreateLog createLog = (VariableCreateLog) LoggingInstance.getLogs(logs, VariableCreateLog.class).get(0); 38 39 assertEquals("a", createLog.getVariableInstance().getName()); 40 } 41 42 public void testByteArrayUpdateLog() { 43 contextInstance.setVariable("a", "first value".getBytes()); 44 contextInstance.setVariable("a", "second value".getBytes()); 45 46 VariableUpdateLog variableLog = saveAndReloadUpdateLog(processInstance); 47 48 assertTrue(Arrays.equals("first value".getBytes(), ((ByteArray) variableLog.getOldValue()).getBytes())); 49 assertTrue(Arrays.equals("second value".getBytes(), ((ByteArray) variableLog.getNewValue()).getBytes())); 50 } 51 52 public void testDateUpdateLog() { 53 Date now = new Date (); 54 Date future = new Date (now.getTime()+5); 55 contextInstance.setVariable("a", now); 56 contextInstance.setVariable("a", future); 57 58 VariableUpdateLog variableLog = saveAndReloadUpdateLog(processInstance); 59 60 assertEquals(now, variableLog.getOldValue()); 61 assertEquals(future, variableLog.getNewValue()); 62 } 63 64 public void testDoubleUpdateLog() { 65 contextInstance.setVariable("a", new Double (3.3)); 66 contextInstance.setVariable("a", new Double (4.4)); 67 68 VariableUpdateLog variableLog = saveAndReloadUpdateLog(processInstance); 69 70 assertEquals(new Double (3.3), variableLog.getOldValue()); 71 assertEquals(new Double (4.4), variableLog.getNewValue()); 72 } 73 74 public void testLongUpdateLog() { 75 contextInstance.setVariable("a", new Integer (3)); 76 contextInstance.setVariable("a", new Integer (5)); 77 78 VariableUpdateLog variableLog = saveAndReloadUpdateLog(processInstance); 79 80 assertEquals(new Long (3), variableLog.getOldValue()); 81 assertEquals(new Long (5), variableLog.getNewValue()); 82 } 83 84 public void testStringUpdateLog() { 85 contextInstance.setVariable("a", "pope"); 86 contextInstance.setVariable("a", "me"); 87 88 VariableUpdateLog variableLog = saveAndReloadUpdateLog(processInstance); 89 90 assertEquals("pope", variableLog.getOldValue()); 91 assertEquals("me", variableLog.getNewValue()); 92 } 93 94 public void testVariableDeleteLog() { 95 contextInstance.setVariable("a", new Integer (3)); 96 contextInstance.deleteVariable("a"); 97 98 graphSession.saveProcessInstance(processInstance); 99 newTransaction(); 100 List logs = loggingSession.findLogsByToken(processInstance.getRootToken().getId()); 101 VariableDeleteLog deleteLog = (VariableDeleteLog) LoggingInstance.getLogs(logs, VariableDeleteLog.class).get(0); 102 103 assertEquals("a", deleteLog.getVariableInstance().getName()); 104 } 105 106 private VariableUpdateLog saveAndReloadUpdateLog(ProcessInstance processInstance) { 107 graphSession.saveProcessInstance(processInstance); 108 newTransaction(); 109 List logs = loggingSession.findLogsByToken(processInstance.getRootToken().getId()); 110 return (VariableUpdateLog) LoggingInstance.getLogs(logs, VariableUpdateLog.class).get(1); 111 } 112 113 } 114 | Popular Tags |