/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.examples.variables;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.test.ActivitiRule;
import org.activiti.engine.*;
import org.junit.*;
import static org.junit.Assert.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.test.Deployment;
/**
* @author Tom Baeyens
*/
public class VariablesTest {
@Rule public ActivitiRule activitiRule = new ActivitiRule();
@Deployment
@Test
public void testBasicVariableOperations() {
Date now = new Date();
List<String> serializable = new ArrayList<String>();
serializable.add("one");
serializable.add("two");
serializable.add("three");
byte[] bytes = "somebytes".getBytes();
// Start process instance with different types of variables
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("longVar", 928374L);
variables.put("shortVar", (short) 123);
variables.put("integerVar", 1234);
variables.put("stringVar", "coca-cola");
variables.put("dateVar", now);
variables.put("nullVar", null);
variables.put("serializableVar", serializable);
variables.put("bytesVar", bytes);
RuntimeService runtimeService = activitiRule.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("taskAssigneeProcess", variables);
variables = runtimeService.getVariables(processInstance.getId());
assertEquals(928374L, variables.get("longVar"));
assertEquals((short) 123, variables.get("shortVar"));
assertEquals(1234, variables.get("integerVar"));
assertEquals("coca-cola", variables.get("stringVar"));
assertEquals(now, variables.get("dateVar"));
assertEquals(null, variables.get("nullVar"));
assertEquals(serializable, variables.get("serializableVar"));
assertTrue(Arrays.equals(bytes, (byte[]) variables.get("bytesVar")));
assertEquals(8, variables.size());
// Set all existing variables values to null
runtimeService.setVariable(processInstance.getId(), "longVar", null);
runtimeService.setVariable(processInstance.getId(), "shortVar", null);
runtimeService.setVariable(processInstance.getId(), "integerVar", null);
runtimeService.setVariable(processInstance.getId(), "stringVar", null);
runtimeService.setVariable(processInstance.getId(), "dateVar", null);
runtimeService.setVariable(processInstance.getId(), "nullVar", null);
runtimeService.setVariable(processInstance.getId(), "serializableVar", null);
runtimeService.setVariable(processInstance.getId(), "bytesVar", null);
variables = runtimeService.getVariables(processInstance.getId());
assertEquals(null, variables.get("longVar"));
assertEquals(null, variables.get("shortVar"));
assertEquals(null, variables.get("integerVar"));
assertEquals(null, variables.get("stringVar"));
assertEquals(null, variables.get("dateVar"));
assertEquals(null, variables.get("nullVar"));
assertEquals(null, variables.get("serializableVar"));
assertEquals(null, variables.get("bytesVar"));
assertEquals(8, variables.size());
// Update existing variable values again, and add a new variable
runtimeService.setVariable(processInstance.getId(), "new var", "hi");
runtimeService.setVariable(processInstance.getId(), "longVar", 9987L);
runtimeService.setVariable(processInstance.getId(), "shortVar", (short) 456);
runtimeService.setVariable(processInstance.getId(), "integerVar", 4567);
runtimeService.setVariable(processInstance.getId(), "stringVar", "colgate");
runtimeService.setVariable(processInstance.getId(), "dateVar", now);
runtimeService.setVariable(processInstance.getId(), "serializableVar", serializable);
runtimeService.setVariable(processInstance.getId(), "bytesVar", bytes);
variables = runtimeService.getVariables(processInstance.getId());
assertEquals("hi", variables.get("new var"));
assertEquals(9987L, variables.get("longVar"));
assertEquals((short)456, variables.get("shortVar"));
assertEquals(4567, variables.get("integerVar"));
assertEquals("colgate", variables.get("stringVar"));
assertEquals(now, variables.get("dateVar"));
assertEquals(null, variables.get("nullVar"));
assertEquals(serializable, variables.get("serializableVar"));
assertTrue(Arrays.equals(bytes, (byte[]) variables.get("bytesVar")));
assertEquals(9, variables.size());
// Try setting the value of the variable that was initially created with value 'null'
runtimeService.setVariable(processInstance.getId(), "nullVar", "a value");
Object newValue = runtimeService.getVariable(processInstance.getId(), "nullVar");
assertNotNull(newValue);
assertEquals("a value", newValue);
}
}