/
VariablesTest.java
464 lines (387 loc) · 21 KB
/
VariablesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* 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.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.activiti.engine.impl.test.PluggableFlowableTestCase;
import org.flowable.common.engine.impl.history.HistoryLevel;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.test.Deployment;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.flowable.variable.api.persistence.entity.VariableInstance;
import org.flowable.variable.api.types.ValueFields;
import org.flowable.variable.api.types.VariableType;
/**
* @author Tom Baeyens
*/
public class VariablesTest extends PluggableFlowableTestCase {
@Deployment
public void testBasicVariableOperations() {
org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl activiti5ProcessEngineConfig = (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl) processEngineConfiguration.getFlowable5CompatibilityHandler()
.getRawProcessConfiguration();
activiti5ProcessEngineConfig.getVariableTypes().addType(CustomVariableType.instance);
Date now = new Date();
List<String> serializable = new ArrayList<String>();
serializable.add("one");
serializable.add("two");
serializable.add("three");
byte[] bytes1 = "somebytes1".getBytes();
byte[] bytes2 = "somebytes2".getBytes();
// 2000 characters * 2 bytes = 4000 bytes
StringBuilder long2000StringBuilder = new StringBuilder();
for (int i = 0; i < 2000; i++) {
long2000StringBuilder.append("z");
}
// 2001 characters * 2 bytes = 4002 bytes
StringBuilder long2001StringBuilder = new StringBuilder();
for (int i = 0; i < 2000; i++) {
long2001StringBuilder.append("a");
}
long2001StringBuilder.append("a");
// 4002 characters
StringBuilder long4001StringBuilder = new StringBuilder();
for (int i = 0; i < 4000; i++) {
long4001StringBuilder.append("a");
}
long4001StringBuilder.append("a");
// 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("longString2000chars", long2000StringBuilder.toString());
variables.put("stringVar", "coca-cola");
variables.put("dateVar", now);
variables.put("nullVar", null);
variables.put("serializableVar", serializable);
variables.put("bytesVar", bytes1);
variables.put("customVar1", new CustomType(bytes2));
variables.put("customVar2", null);
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(long2000StringBuilder.toString(), variables.get("longString2000chars"));
assertEquals(now, variables.get("dateVar"));
assertNull(variables.get("nullVar"));
assertEquals(serializable, variables.get("serializableVar"));
assertTrue(Arrays.equals(bytes1, (byte[]) variables.get("bytesVar")));
assertEquals(new CustomType(bytes2), variables.get("customVar1"));
assertNull(variables.get("customVar2"));
assertEquals(11, 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(), "longString2000chars", null);
runtimeService.setVariable(processInstance.getId(), "longString4000chars", 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);
runtimeService.setVariable(processInstance.getId(), "customVar1", null);
runtimeService.setVariable(processInstance.getId(), "customVar2", null);
variables = runtimeService.getVariables(processInstance.getId());
assertNull(variables.get("longVar"));
assertNull(variables.get("shortVar"));
assertNull(variables.get("integerVar"));
assertNull(variables.get("stringVar"));
assertNull(variables.get("longString2000chars"));
assertNull(variables.get("longString4000chars"));
assertNull(variables.get("dateVar"));
assertNull(variables.get("nullVar"));
assertNull(variables.get("serializableVar"));
assertNull(variables.get("bytesVar"));
assertNull(variables.get("customVar1"));
assertNull(variables.get("customVar2"));
assertEquals(12, 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(), "longString2000chars", long2001StringBuilder.toString());
runtimeService.setVariable(processInstance.getId(), "longString4000chars", long4001StringBuilder.toString());
runtimeService.setVariable(processInstance.getId(), "dateVar", now);
runtimeService.setVariable(processInstance.getId(), "serializableVar", serializable);
runtimeService.setVariable(processInstance.getId(), "bytesVar", bytes1);
runtimeService.setVariable(processInstance.getId(), "customVar1", new CustomType(bytes2));
runtimeService.setVariable(processInstance.getId(), "customVar2", new CustomType(bytes1));
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(long2001StringBuilder.toString(), variables.get("longString2000chars"));
assertEquals(long4001StringBuilder.toString(), variables.get("longString4000chars"));
assertEquals(now, variables.get("dateVar"));
assertNull(variables.get("nullVar"));
assertEquals(serializable, variables.get("serializableVar"));
assertTrue(Arrays.equals(bytes1, (byte[]) variables.get("bytesVar")));
assertEquals(new CustomType(bytes2), variables.get("customVar1"));
assertEquals(new CustomType(bytes1), variables.get("customVar2"));
assertEquals(13, variables.size());
Collection<String> varFilter = new ArrayList<String>(2);
varFilter.add("stringVar");
varFilter.add("integerVar");
Map<String, Object> filteredVariables = runtimeService.getVariables(processInstance.getId(), varFilter);
assertEquals(2, filteredVariables.size());
assertTrue(filteredVariables.containsKey("stringVar"));
assertTrue(filteredVariables.containsKey("integerVar"));
// 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);
org.flowable.task.api.Task task = taskService.createTaskQuery().singleResult();
taskService.complete(task.getId());
}
@Deployment
public void testLocalizeVariables() {
// Start process instance with different types of variables
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("stringVar", "coca-cola");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("localizeVariables", variables);
Map<String, VariableInstance> variableInstances = runtimeService.getVariableInstances(processInstance.getId());
assertEquals(1, variableInstances.size());
assertEquals("stringVar", variableInstances.get("stringVar").getName());
assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
List<String> variableNames = new ArrayList<String>();
variableNames.add("stringVar");
// getVariablesInstances via names
variableInstances = runtimeService.getVariableInstances(processInstance.getId(), variableNames);
assertEquals(1, variableInstances.size());
assertEquals("stringVar", variableInstances.get("stringVar").getName());
assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
// getVariableInstancesLocal
variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId());
assertEquals(1, variableInstances.size());
assertEquals("stringVar", variableInstances.get("stringVar").getName());
assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
// getVariableInstancesLocal via names
variableInstances = runtimeService.getVariableInstancesLocal(processInstance.getId(), variableNames);
assertEquals(1, variableInstances.size());
assertEquals("stringVar", variableInstances.get("stringVar").getName());
assertEquals("coca-cola", variableInstances.get("stringVar").getValue());
// getVariableInstance
VariableInstance variableInstance = runtimeService.getVariableInstance(processInstance.getId(), "stringVar");
assertNotNull(variableInstance);
assertEquals("stringVar", variableInstance.getName());
assertEquals("coca-cola", variableInstance.getValue());
// getVariableInstanceLocal
variableInstance = runtimeService.getVariableInstanceLocal(processInstance.getId(), "stringVar");
assertNotNull(variableInstance);
assertEquals("stringVar", variableInstance.getName());
assertEquals("coca-cola", variableInstance.getValue());
}
// Test case for ACT-1839
@Deployment(resources = { "org/activiti/examples/variables/VariablesTest.testChangeTypeSerializable.bpmn20.xml" })
public void testChangeTypeSerializable() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("variable-type-change-test");
assertNotNull(processInstance);
org.flowable.task.api.Task task = taskService.createTaskQuery().singleResult();
assertEquals("Activiti is awesome!", task.getName());
SomeSerializable myVar = (SomeSerializable) runtimeService.getVariable(processInstance.getId(), "myVar");
assertEquals("someValue", myVar.getValue());
}
public String getVariableInstanceId(String executionId, String name) {
HistoricVariableInstance variable = historyService.createHistoricVariableInstanceQuery().processInstanceId(executionId).variableName(name).singleResult();
return variable.getId();
}
// test case for ACT-1082
@Deployment(resources = { "org/activiti/examples/variables/VariablesTest.testBasicVariableOperations.bpmn20.xml" })
public void testChangeVariableType() {
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);
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"));
assertNull(variables.get("nullVar"));
assertEquals(serializable, variables.get("serializableVar"));
assertTrue(Arrays.equals(bytes, (byte[]) variables.get("bytesVar")));
assertEquals(8, variables.size());
// check if the id of the variable is the same or not
if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.AUDIT)) {
String oldSerializableVarId = getVariableInstanceId(processInstance.getId(), "serializableVar");
String oldLongVar = getVariableInstanceId(processInstance.getId(), "longVar");
// Change type of serializableVar from serializable to Short
Map<String, Object> newVariables = new HashMap<String, Object>();
newVariables.put("serializableVar", (short) 222);
runtimeService.setVariables(processInstance.getId(), newVariables);
variables = runtimeService.getVariables(processInstance.getId());
assertEquals((short) 222, variables.get("serializableVar"));
String newSerializableVarId = getVariableInstanceId(processInstance.getId(), "serializableVar");
assertEquals(oldSerializableVarId, newSerializableVarId);
// Change type of a longVar from Long to Short
newVariables = new HashMap<String, Object>();
newVariables.put("longVar", (short) 123);
runtimeService.setVariables(processInstance.getId(), newVariables);
variables = runtimeService.getVariables(processInstance.getId());
assertEquals((short) 123, variables.get("longVar"));
String newLongVar = getVariableInstanceId(processInstance.getId(), "longVar");
assertEquals(oldLongVar, newLongVar);
}
}
// test case for ACT-1428
@Deployment
public void testNullVariable() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("taskAssigneeProcess");
org.flowable.task.api.Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
Map<String, String> variables = new HashMap<String, String>();
variables.put("testProperty", "434");
formService.submitTaskFormData(task.getId(), variables);
String resultVar = (String) runtimeService.getVariable(processInstance.getId(), "testProperty");
assertEquals("434", resultVar);
task = taskService.createTaskQuery().singleResult();
taskService.complete(task.getId());
// If no variable is given, no variable should be set and script test should throw exception
processInstance = runtimeService.startProcessInstanceByKey("taskAssigneeProcess");
task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
variables = new HashMap<String, String>();
try {
formService.submitTaskFormData(task.getId(), variables);
fail("Should throw exception as testProperty is not defined and used in Script task");
} catch (Exception e) {
runtimeService.deleteProcessInstance(processInstance.getId(), "intentional exception in script task");
assertEquals("class org.flowable.common.engine.api.FlowableException", e.getClass().toString());
}
// No we put null property, This should be put into the variable. We do not expect exceptions
processInstance = runtimeService.startProcessInstanceByKey("taskAssigneeProcess");
task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
variables = new HashMap<String, String>();
variables.put("testProperty", null);
try {
formService.submitTaskFormData(task.getId(), variables);
} catch (Exception e) {
fail("Should not throw exception as the testProperty is defined, although null");
}
resultVar = (String) runtimeService.getVariable(processInstance.getId(), "testProperty");
assertNull(resultVar);
runtimeService.deleteProcessInstance(processInstance.getId(), "intentional exception in script task");
}
/**
* Test added to validate UUID variable type + querying (ACT-1665)
*/
@Deployment
public void testUUIDVariableAndQuery() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
assertNotNull(processInstance);
// Check UUID variable type query on task
org.flowable.task.api.Task task = taskService.createTaskQuery().singleResult();
assertNotNull(task);
UUID randomUUID = UUID.randomUUID();
taskService.setVariableLocal(task.getId(), "conversationId", randomUUID);
org.flowable.task.api.Task resultingTask = taskService.createTaskQuery().taskVariableValueEquals("conversationId", randomUUID).singleResult();
assertNotNull(resultingTask);
assertEquals(task.getId(), resultingTask.getId());
randomUUID = UUID.randomUUID();
// Check UUID variable type query on process
runtimeService.setVariable(processInstance.getId(), "uuidVar", randomUUID);
ProcessInstance result = runtimeService.createProcessInstanceQuery().variableValueEquals("uuidVar", randomUUID).singleResult();
assertNotNull(result);
assertEquals(processInstance.getId(), result.getId());
}
}
class CustomType {
private byte[] value;
public CustomType(byte[] value) {
if (value == null) {
throw new NullPointerException();
}
this.value = value;
}
public byte[] getValue() {
return value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(value);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
CustomType other = (CustomType) obj;
if (!Arrays.equals(value, other.value))
return false;
return true;
}
}
/**
* A custom variable type for testing byte array value handling.
*
* @author Marcus Klimstra (CGI)
*/
class CustomVariableType implements VariableType {
public static final CustomVariableType instance = new CustomVariableType();
@Override
public String getTypeName() {
return "CustomVariableType";
}
@Override
public boolean isCachable() {
return true;
}
@Override
public boolean isAbleToStore(Object value) {
return value == null || value instanceof CustomType;
}
@Override
public void setValue(Object o, ValueFields valueFields) {
// ensure calling setBytes multiple times no longer causes any problems
valueFields.setBytes(new byte[] { 1, 2, 3 });
valueFields.setBytes(null);
valueFields.setBytes(new byte[] { 4, 5, 6 });
byte[] value = (o == null ? null : ((CustomType) o).getValue());
valueFields.setBytes(value);
}
@Override
public Object getValue(ValueFields valueFields) {
byte[] bytes = valueFields.getBytes();
return bytes == null ? null : new CustomType(bytes);
}
}