1 package org.jbpm.context.exe; 2 3 import java.util.*; 4 5 import junit.framework.*; 6 7 import org.jbpm.context.def.*; 8 import org.jbpm.graph.def.*; 9 import org.jbpm.graph.exe.*; 10 11 public class VariableScopingTest extends TestCase { 12 13 private static ProcessDefinition pd = createProcessDefinition(); 14 private ProcessInstance pi = new ProcessInstance(pd); 15 private ContextInstance ci = (ContextInstance) pi.getInstance(ContextInstance.class); 16 private Token rootToken = pi.getRootToken(); 17 private Token tokenA = new Token(rootToken, "a"); 18 private Token tokenAB = new Token(tokenA, "ab"); 19 private Token tokenAC = new Token(tokenA, "ac"); 20 21 public static ProcessDefinition createProcessDefinition() { 22 ProcessDefinition pd = new ProcessDefinition(); 23 pd.addDefinition(new ContextDefinition()); 24 return pd; 25 } 26 27 public void testScopeOverriding() { 28 ci.createVariable("a", "one", rootToken); 29 ci.createVariable("a", "two", tokenA); 30 ci.createVariable("a", "three", tokenAB); 31 32 assertEquals("one", ci.getVariable("a", rootToken)); 33 assertEquals("two", ci.getVariable("a", tokenA)); 34 assertEquals("three", ci.getVariable("a", tokenAB)); 35 assertEquals("two", ci.getVariable("a", tokenAC)); 36 } 37 38 public void testScopePermeability() { 39 ci.createVariable("a", "one", rootToken); 40 ci.createVariable("b", "two", tokenA); 41 assertEquals("one", ci.getVariable("a", rootToken)); 42 assertEquals("one", ci.getVariable("a", tokenA)); 43 assertNull(ci.getVariable("b", rootToken)); 44 assertEquals("two", ci.getVariable("b", tokenA)); 45 } 46 47 public void testVariableOverloading() { 48 ci.createVariable("a", "one", tokenAB); 49 ci.createVariable("a", "two", tokenAC); 50 51 assertEquals("one", ci.getVariable("a", tokenAB)); 52 assertEquals("two", ci.getVariable("a", tokenAC)); 53 } 54 55 public void testRootTokenValue() { 56 ci.setVariable("a", new Integer (3)); 57 assertEquals(new Integer (3), ci.getVariable("a")); 58 } 59 60 public void testRootTokenMap() { 61 Map variables = new HashMap(); 62 variables.put("a", new Integer (3)); 63 variables.put("b", new Integer (4)); 64 ci.addVariables(variables); 65 variables = ci.getVariables(); 66 assertEquals(new Integer (3), variables.get("a")); 67 assertEquals(new Integer (4), variables.get("b")); 68 } 69 70 public void testChildTokenValueDefaultCreation() { 71 ci.setVariable("a", new Integer (3), tokenA); 73 assertEquals(new Integer (3), ci.getVariable("a", tokenA)); 74 assertEquals(new Integer (3), ci.getVariable("a", tokenAB)); 75 assertEquals(new Integer (3), ci.getVariable("a", tokenAC)); 76 assertEquals(new Integer (3), ci.getVariable("a", rootToken)); 77 } 78 79 public void testChildTokenValueTokenCreate() { 80 ci.createVariable("a", new Integer (3), tokenA); 81 assertEquals(new Integer (3), ci.getVariable("a", tokenA)); 82 assertEquals(new Integer (3), ci.getVariable("a", tokenAB)); 83 assertEquals(new Integer (3), ci.getVariable("a", tokenAC)); 84 assertNull(ci.getVariable("a", rootToken)); 85 } 86 87 public void testChildTokenLocalValue() { 88 ci.createVariable("a", new Integer (3), tokenA); 89 assertEquals(new Integer (3), ci.getLocalVariable("a", tokenA)); 90 assertNull(ci.getLocalVariable("a", rootToken)); 91 assertNull(ci.getLocalVariable("a", tokenAB)); 92 assertNull(ci.getLocalVariable("a", tokenAC)); 93 } 94 95 public void testChildTokenMapDefaultCreation() { 96 Map variables = new HashMap(); 97 variables.put("a", new Integer (3)); 98 variables.put("b", new Integer (4)); 99 ci.addVariables(variables, tokenA); 100 101 Map rootTokenVariables = ci.getVariables(rootToken); 102 Map tokenAVariables = ci.getVariables(tokenA); 103 Map tokenABVariables = ci.getVariables(tokenAB); 104 Map tokenACVariables = ci.getVariables(tokenAC); 105 106 assertEquals(variables, rootTokenVariables); 107 assertEquals(variables, tokenAVariables); 108 assertEquals(variables, tokenABVariables); 109 assertEquals(variables, tokenACVariables); 110 } 111 112 } 113 | Popular Tags |